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

33 lines
1.1 KiB
SQL

DELIMITER $$
CREATE OR REPLACE DEFINER=`vn-admin`@`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 ;