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

76 lines
1.6 KiB
MySQL
Raw Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`duaInvoiceInBooking`(vDuaFk INT)
BEGIN
2024-02-15 09:04:15 +00:00
/**
* Genera el asiento de un DUA y marca las entradas como confirmadas
*
2024-02-15 09:04:15 +00:00
* @param vDuaFk Id del dua a recalcular
*/
DECLARE done BOOL DEFAULT FALSE;
DECLARE vInvoiceFk INT;
2024-02-15 09:04:15 +00:00
DECLARE vASIEN BIGINT DEFAULT 0;
DECLARE vCounter INT DEFAULT 0;
DECLARE rs CURSOR FOR
2024-02-15 09:04:15 +00:00
SELECT DISTINCT e.invoiceInFk
FROM entry e
JOIN duaEntry de ON de.entryFk = e.id
2024-02-15 09:04:15 +00:00
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
2024-02-15 09:04:15 +00:00
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),
2024-02-15 09:04:15 +00:00
ii.issued = IFNULL(ii.issued,d.issued),
ii.bookEntried = IFNULL(ii.bookEntried,d.bookEntried),
e.isBooked = TRUE,
2024-02-15 09:04:15 +00:00
e.isConfirmed = TRUE
WHERE d.id = vDuaFk;
2024-02-15 09:04:15 +00:00
SELECT IFNULL(ASIEN,0) INTO vASIEN
FROM dua
WHERE id = vDuaFk;
2024-02-15 09:04:15 +00:00
FETCH rs INTO vInvoiceFk;
WHILE NOT done DO
CALL invoiceIn_booking(vInvoiceFk);
2024-02-15 09:04:15 +00:00
IF vCounter > 0 OR vASIEN > 0 THEN
2024-02-23 07:14:58 +00:00
UPDATE XDiario x
2024-02-15 09:04:15 +00:00
JOIN ledgerConfig lc ON lc.lastBookEntry = x.ASIEN
SET x.ASIEN = vASIEN;
2024-02-15 09:04:15 +00:00
ELSE
2024-02-15 09:04:15 +00:00
SELECT lastBookEntry INTO vASIEN FROM ledgerConfig;
END IF;
2024-02-15 09:04:15 +00:00
SET vCounter = vCounter + 1;
FETCH rs INTO vInvoiceFk;
END WHILE;
2024-02-15 09:04:15 +00:00
CLOSE rs;
UPDATE dua
2024-02-15 09:04:15 +00:00
SET ASIEN = vASIEN
WHERE id = vDuaFk;
END$$
DELIMITER ;