salix/db/changes/10110-postCampaign/00-duaInvoicingBooking.sql

75 lines
1.7 KiB
SQL

DROP procedure IF EXISTS `vn`.`duaInvoiceInBooking`;
DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `vn`.`duaInvoiceInBooking`(vDuaFk INT)
BEGIN
DECLARE done BOOL DEFAULT FALSE;
DECLARE vInvoiceFk INT;
DECLARE vASIEN BIGINT DEFAULT 0;
DECLARE vCounter INT DEFAULT 0;
DECLARE rs CURSOR FOR
SELECT e.invoiceInFk
FROM entry e
JOIN duaEntry de ON de.entryFk = e.id
JOIN invoiceIn ii ON ii.id = e.invoiceInFk
WHERE de.duaFk = vDuaFk
AND de.customsValue
AND ii.isBooked = FALSE;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN rs;
UPDATE invoiceIn ii
JOIN entry e ON e.invoiceInFk = ii.id
JOIN duaEntry de ON de.entryFk = e.id
JOIN dua d ON d.id = de.duaFk
SET ii.isBooked = TRUE,
ii.booked = IFNULL(ii.booked,d.booked),
ii.operated = IFNULL(ii.operated,d.operated),
ii.issued = IFNULL(ii.issued,d.issued),
ii.bookEntried = IFNULL(ii.bookEntried,d.bookEntried),
e.isConfirmed = TRUE
WHERE d.id = vDuaFk;
SELECT IFNULL(ASIEN,0) INTO vASIEN
FROM dua
WHERE id = vDuaFk;
FETCH rs INTO vInvoiceFk;
WHILE NOT done DO
CALL invoiceInBookingMain(vInvoiceFk);
IF vCounter > 0 OR vASIEN > 0 THEN
UPDATE XDiario x
JOIN config c ON c.ASIEN = x.ASIEN
SET x.ASIEN = vASIEN;
ELSE
SELECT ASIEN INTO vASIEN FROM config;
END IF;
SET vCounter = vCounter + 1;
FETCH rs INTO vInvoiceFk;
END WHILE;
CLOSE rs;
UPDATE dua
SET ASIEN = vASIEN
WHERE id = vDuaFk;
END$$
DELIMITER ;