2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-06-10 06:38:58 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`duaInvoiceInBooking`(
|
|
|
|
vDuaFk INT
|
|
|
|
)
|
2024-01-15 11:31:03 +00:00
|
|
|
BEGIN
|
2024-02-15 09:04:15 +00:00
|
|
|
/**
|
2024-06-10 06:47:02 +00:00
|
|
|
* Genera el asiento de un DUA y marca las entradas como confirmadas
|
|
|
|
*
|
|
|
|
* @param vDuaFk Id del dua a recalcular
|
|
|
|
*/
|
2024-05-28 10:30:17 +00:00
|
|
|
DECLARE vDone BOOL DEFAULT FALSE;
|
2024-01-15 11:31:03 +00:00
|
|
|
DECLARE vInvoiceFk INT;
|
2024-05-28 10:30:17 +00:00
|
|
|
DECLARE vBookEntry INT;
|
|
|
|
DECLARE vFiscalYear INT;
|
2024-02-15 09:04:15 +00:00
|
|
|
|
2024-05-28 10:30:17 +00:00
|
|
|
DECLARE vInvoicesIn CURSOR FOR
|
2024-02-15 09:04:15 +00:00
|
|
|
SELECT DISTINCT e.invoiceInFk
|
2024-01-15 11:31:03 +00:00
|
|
|
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
|
2024-01-15 11:31:03 +00:00
|
|
|
AND de.customsValue
|
|
|
|
AND ii.isBooked = FALSE;
|
|
|
|
|
2024-05-28 10:30:17 +00:00
|
|
|
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
|
2024-02-15 09:14:19 +00:00
|
|
|
|
2024-05-28 10:30:17 +00:00
|
|
|
SELECT ASIEN INTO vBookEntry FROM dua WHERE id = vDuaFk;
|
2024-02-15 09:14:19 +00:00
|
|
|
|
2024-05-28 10:30:17 +00:00
|
|
|
IF vBookEntry IS NULL THEN
|
|
|
|
SELECT YEAR(IFNULL(ii.bookEntried, d.bookEntried)) INTO vFiscalYear
|
|
|
|
FROM invoiceIn ii
|
2024-06-10 06:47:02 +00:00
|
|
|
JOIN `entry` e ON e.invoiceInFk = ii.id
|
2024-05-28 10:30:17 +00:00
|
|
|
JOIN duaEntry de ON de.entryFk = e.id
|
|
|
|
JOIN dua d ON d.id = de.duaFk
|
2024-05-28 12:50:58 +00:00
|
|
|
WHERE d.id = vDuaFk
|
|
|
|
LIMIT 1;
|
2024-06-03 18:16:27 +00:00
|
|
|
CALL ledger_nextTx(vFiscalYear, vBookEntry);
|
2024-05-28 10:30:17 +00:00
|
|
|
END IF;
|
2024-02-15 09:14:19 +00:00
|
|
|
|
2024-05-28 10:30:17 +00:00
|
|
|
OPEN vInvoicesIn;
|
2024-02-15 09:14:19 +00:00
|
|
|
|
2024-05-28 10:30:17 +00:00
|
|
|
l: LOOP
|
|
|
|
SET vDone = FALSE;
|
|
|
|
FETCH vInvoicesIn INTO vInvoiceFk;
|
2024-02-15 09:14:19 +00:00
|
|
|
|
2024-05-28 10:30:17 +00:00
|
|
|
IF vDone THEN
|
|
|
|
LEAVE l;
|
2024-01-15 11:31:03 +00:00
|
|
|
END IF;
|
2024-02-15 09:14:19 +00:00
|
|
|
|
2024-05-28 10:30:17 +00:00
|
|
|
CALL invoiceIn_booking(vInvoiceFk, vBookEntry);
|
|
|
|
END LOOP;
|
2024-02-15 09:04:15 +00:00
|
|
|
|
2024-05-28 10:30:17 +00:00
|
|
|
CLOSE vInvoicesIn;
|
2024-02-15 09:04:15 +00:00
|
|
|
|
2024-02-15 09:14:19 +00:00
|
|
|
UPDATE dua
|
2024-05-28 12:50:58 +00:00
|
|
|
SET ASIEN = vBookEntry
|
2024-02-15 09:04:15 +00:00
|
|
|
WHERE id = vDuaFk;
|
2024-02-15 09:14:19 +00:00
|
|
|
|
2024-05-07 09:35:07 +00:00
|
|
|
UPDATE invoiceIn ii
|
|
|
|
JOIN duaInvoiceIn dii ON dii.invoiceInFk = ii.id
|
|
|
|
SET ii.isBooked = TRUE
|
|
|
|
WHERE dii.duaFk = vDuaFk;
|
2024-06-10 06:47:02 +00:00
|
|
|
|
|
|
|
UPDATE `entry` e
|
|
|
|
JOIN (
|
|
|
|
WITH entries AS (
|
|
|
|
SELECT e.id, de.duaFk
|
|
|
|
FROM `entry` e
|
|
|
|
JOIN duaEntry de ON de.entryFk = e.id
|
|
|
|
WHERE de.duaFk = vDuaFk
|
|
|
|
AND (NOT e.isBooked OR NOT e.isConfirmed)
|
|
|
|
),
|
|
|
|
notBookedEntries AS (
|
|
|
|
SELECT e.id
|
|
|
|
FROM entries e
|
|
|
|
JOIN duaEntry de ON de.entryFk = e.id
|
|
|
|
JOIN dua d ON d.id = de.duaFk
|
|
|
|
WHERE d.ASIEN IS NULL)
|
|
|
|
SELECT e.id
|
|
|
|
FROM entries e
|
|
|
|
LEFT JOIN notBookedEntries nbe ON nbe.id = e.id
|
|
|
|
WHERE nbe.id IS NULL
|
|
|
|
) sub ON sub.id = e.id
|
|
|
|
SET e.isBooked = TRUE,
|
|
|
|
e.isConfirmed = TRUE;
|
2024-01-15 11:31:03 +00:00
|
|
|
END$$
|
|
|
|
DELIMITER ;
|