DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`pay`(
	vDated DATE,
	vSupplierFk INT,
	vAmount DOUBLE,
	vCurrencyFk INT,
	vDivisa DOUBLE,
	vBankFk INT,
	vPayMethodFk INT,
	vExpenseFk DOUBLE,
	vConcept VARCHAR(40),
	vCompanyFk INT)
BEGIN
/**
 * Registra un pago realizado a un proveedor.
 *
 * @param vDated Fecha del pago
 * @param vSupplierFk Id del proveedor
 * @param vAmount Cantidad a pagar
 * @param vCurrencyFk Id de la moneda
 * @param vDivisa Tipo de cambio utilizado
 * @param vBankFk Id del banco
 * @param vPayMethodFk Id del método de pago
 * @param vExpenseFk Id de gasto
 * @param vConcept Concepto del pago
 * @param vCompanyFk Id de la empresa
 * @return paymentFk Id de pago insertado
 */
	INSERT INTO till(
			concept,
			serie,
			`number`,
			`out`,
			dated,
			isAccountable,
			bankFk,
			workerFk,
			companyFk,
			isConciliate
	) 
		SELECT CONCAT('n/pago a ', `name`),
				'R',
				vSupplierFk,
				vAmount, 
				vDated,
				1,
				vBankFk,
				account.myUser_getId(),
				vCompanyFk,
				1
			FROM supplier
			WHERE id = vSupplierFk;

	INSERT INTO payment(
			received,
			dueDated,
			supplierFk,
			amount,
			currencyFk,
			divisa,
			bankFk,
			payMethodFk,
			bankingFees,
			concept,
			companyFk
	)
		VALUES(
			vDated,
			vDated,
			vSupplierFk,
			vAmount,
			vCurrencyFk,
			IF(NOT vDivisa, NULL, vDivisa),
			vBankFk,
			vPayMethodFk,
			vExpenseFk,
			vConcept,
			vCompanyFk
		);
			
	SELECT LAST_INSERT_ID() paymentFk;
END$$
DELIMITER ;
