salix/db/routines/vn/procedures/invoiceOut_exportationFromC...

33 lines
1.1 KiB
MySQL
Raw Permalink Normal View History

DELIMITER $$
2024-08-20 08:06:10 +00:00
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`invoiceOut_exportationFromClient`(
vMaxTicketDate DATETIME,
vClientFk INT,
vCompanyFk INT)
BEGIN
/**
* Genera tabla temporal tmp.ticketToInvoice necesaría para el proceso de facturación
* Los abonos quedan excluidos en las exportaciones
*
* @param vMaxTicketDate Fecha hasta la cual cogerá tickets para facturar
* @param vClientFk Id del cliente a facturar
* @param vCompanyFk Id de la empresa desde la que se factura
*/
DECLARE vMinTicketDate DATE;
SET vMinTicketDate = util.firstDayOfYear(vMaxTicketDate - INTERVAL 1 YEAR);
SET vMaxTicketDate = util.dayend(vMaxTicketDate);
DROP TEMPORARY TABLE IF EXISTS `tmp`.`ticketToInvoice`;
CREATE TEMPORARY TABLE `tmp`.`ticketToInvoice`
(PRIMARY KEY (`id`))
ENGINE = MEMORY
SELECT t.id
FROM ticket t
JOIN agencyMode am ON am.id = t.agencyModeFk
WHERE t.clientFk = vClientFk
AND t.refFk IS NULL
AND t.companyFk = vCompanyFk
AND t.shipped BETWEEN vMinTicketDate AND vMaxTicketDate
AND (am.`code` IS NULL OR am.`code` <> 'refund');
END$$
DELIMITER ;