salix/db/routines/vn/procedures/ticket_close.sql

112 lines
2.9 KiB
MySQL
Raw Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_close`()
BEGIN
/**
2024-07-22 08:26:40 +00:00
* Realiza el cierre de todos los
* tickets de la tabla tmp.ticket_close.
*
* @table tmp.ticket_close(ticketFk) Identificadores de los tickets a cerrar
*/
DECLARE vDone BOOL;
DECLARE vClientFk INT;
DECLARE vCurTicketFk INT;
DECLARE vIsTaxDataChecked BOOL;
DECLARE vCompanyFk INT;
DECLARE vShipped DATE;
DECLARE vNewInvoiceId INT;
DECLARE vHasDailyInvoice BOOL;
DECLARE vWithPackage BOOL;
DECLARE vHasToInvoice BOOL;
2024-09-06 10:52:24 +00:00
DECLARE vSerial VARCHAR(2);
DECLARE cur CURSOR FOR
SELECT ticketFk FROM tmp.ticket_close;
2024-07-22 08:26:40 +00:00
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
DECLARE EXIT HANDLER FOR SQLEXCEPTION BEGIN
RESIGNAL;
END;
OPEN cur;
proc: LOOP
SET vDone = FALSE;
2024-07-22 08:26:40 +00:00
FETCH cur INTO vCurTicketFk;
IF vDone THEN
LEAVE proc;
END IF;
SELECT
c.id,
c.isTaxDataChecked,
t.companyFk,
t.shipped,
IFNULL(a.hasDailyInvoice, co.hasDailyInvoice),
w.isManaged,
2024-07-12 10:10:43 +00:00
c.hasToInvoice
INTO vClientFk,
vIsTaxDataChecked,
2024-07-22 08:26:40 +00:00
vCompanyFk,
vShipped,
vHasDailyInvoice,
vWithPackage,
2024-07-12 10:10:43 +00:00
vHasToInvoice
2024-07-22 08:26:40 +00:00
FROM ticket t
JOIN `client` c ON c.id = t.clientFk
JOIN province p ON p.id = c.provinceFk
LEFT JOIN autonomy a ON a.id = p.autonomyFk
JOIN country co ON co.id = p.countryFk
JOIN warehouse w ON w.id = t.warehouseFk
WHERE t.id = vCurTicketFk;
INSERT INTO ticketPackaging (ticketFk, packagingFk, quantity)
(SELECT vCurTicketFk, p.id, COUNT(*)
2024-07-22 08:26:40 +00:00
FROM expedition e
JOIN packaging p ON p.id = e.packagingFk
2024-07-12 10:10:43 +00:00
JOIN ticket t ON t.id = e.ticketFk
2024-07-12 10:13:21 +00:00
LEFT JOIN agencyMode am ON am.id = t.agencyModeFk
LEFT JOIN deliveryMethod dm ON dm.id = am.deliveryMethodFk
WHERE e.ticketFk = vCurTicketFk AND p.isPackageReturnable
AND vWithPackage
2024-07-12 10:10:43 +00:00
AND NOT dm.`code`= 'PICKUP'
GROUP BY p.itemFk);
-- No retornables o no catalogados
2024-07-22 08:26:40 +00:00
INSERT INTO sale (itemFk, ticketFk, concept, quantity, price, isPriceFixed)
(SELECT e.freightItemFk, vCurTicketFk, i.name, COUNT(*) AS amount, getSpecialPrice(e.freightItemFk, vClientFk), 1
2024-07-22 08:26:40 +00:00
FROM expedition e
JOIN item i ON i.id = e.freightItemFk
LEFT JOIN packaging p ON p.itemFk = i.id
WHERE e.ticketFk = vCurTicketFk AND IFNULL(p.isPackageReturnable, 0) = 0
AND getSpecialPrice(e.freightItemFk, vClientFk) > 0
GROUP BY e.freightItemFk);
2024-07-22 08:26:40 +00:00
IF(vHasDailyInvoice) AND vHasToInvoice THEN
2024-09-06 10:52:24 +00:00
SELECT invoiceSerial(vClientFk, vCompanyFk, 'quick') INTO vSerial;
IF NOT vSerial THEN
CALL util.throw('Cannot booking without a serial');
END IF;
CALL ticket_setState(vCurTicketFk, 'DELIVERED');
2024-09-06 10:52:24 +00:00
IF vIsTaxDataChecked THEN
CALL invoiceOut_newFromClient(
2024-07-22 08:26:40 +00:00
vClientFk,
2024-09-06 10:52:24 +00:00
vSerial,
2024-07-22 08:26:40 +00:00
vShipped,
vCompanyFk,
NULL,
NULL,
vNewInvoiceId);
END IF;
ELSE
CALL ticket_setState(vCurTicketFk, (SELECT vn.getAlert3State(vCurTicketFk)));
END IF;
END LOOP;
CLOSE cur;
END$$
DELIMITER ;