salix/db/routines/vn/functions/invoiceOut_getMaxIssued.sql

33 lines
848 B
MySQL
Raw Normal View History

2023-02-23 08:25:23 +00:00
DELIMITER $$
2024-08-20 08:06:10 +00:00
CREATE OR REPLACE DEFINER=`vn`@`localhost` FUNCTION `vn`.`invoiceOut_getMaxIssued`(vSerial VARCHAR(2),
2023-02-23 08:25:23 +00:00
vCompanyFk INT,
vYear INT
)
RETURNS date
NOT DETERMINISTIC
READS SQL DATA
2023-02-23 08:25:23 +00:00
BEGIN
/**
* Retorna la fecha a partir de la cual es válido emitir una factura
*
* @param vSerial Serie de facturación
* @param vCompanyFk Empresa factura emitida
* @param vYear Año contable
* @return vInvoiceOutIssued fecha factura válida
*/
DECLARE vInvoiceOutIssued DATE;
DECLARE vFirstDayOfYear DATE;
SET vFirstDayOfYear := MAKEDATE(vYear, 1);
SELECT IFNULL(MAX(io.issued), vFirstDayOfYear) INTO vInvoiceOutIssued
FROM invoiceOut io
WHERE io.serial = vSerial
AND io.companyFk = vCompanyFk
AND io.issued BETWEEN vFirstDayOfYear
AND util.lastDayOfYear(vFirstDayOfYear);
RETURN vInvoiceOutIssued;
END$$
DELIMITER ;