DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`xdiario_new`(vAsiento INT,
	vDated DATE,
	vSubaccount VARCHAR(12),
	vAccount VARCHAR(12),
	vConcept VARCHAR(25),
	vDebit DOUBLE,
	vCredit DOUBLE,
	vEuro DOUBLE,
	vSerie CHAR(1),
	vInvoice VARCHAR(8),
	vVat DOUBLE,
	vRe DOUBLE,
	vAux TINYINT,
	vCompany INT
)
	RETURNS int(11)
	NOT DETERMINISTIC
	NO SQL
BEGIN
	IF vAsiento IS NULL THEN
        CALL vn.ledger_next(vAsiento);
	END IF;

	INSERT INTO XDiario
	SET ASIEN = vAsiento,
		FECHA = vDated,
		SUBCTA = vSubaccount,
		CONTRA = vAccount,
		CONCEPTO = vConcept,
		EURODEBE = vDebit,
		EUROHABER = vCredit,
		BASEEURO = vEuro,
		SERIE = vSerie,
		FACTURA = vInvoice,
		IVA = vVat,
		RECEQUIV = vRe,
		AUXILIAR = IF(vAux = FALSE, NULL, '*'),
		MONEDAUSO = 2,
		empresa_id = vCompany;

	RETURN vAsiento;
END$$
DELIMITER ;
