DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`xdiario_new`(
	vBookNumber 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,
	vCompanyFk INT
)
	RETURNS int(11)
	NOT DETERMINISTIC
	NO SQL
BEGIN
	IF vBookNumber IS NULL THEN
		CALL ledger_next(YEAR(vDated), vBookNumber);
	END IF;

	INSERT INTO XDiario
		SET ASIEN = vBookNumber,
			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 = vCompanyFk;

	RETURN vBookNumber;
END$$
DELIMITER ;