112 lines
2.9 KiB
SQL
112 lines
2.9 KiB
SQL
DELIMITER $$
|
|
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` PROCEDURE `vn`.`duaInvoiceInBooking`(
|
|
vDuaFk INT
|
|
)
|
|
BEGIN
|
|
/**
|
|
* Genera el asiento de un DUA y marca las entradas como confirmadas
|
|
*
|
|
* @param vDuaFk Id del dua a recalcular
|
|
*/
|
|
DECLARE vDone BOOL DEFAULT FALSE;
|
|
DECLARE vInvoiceFk INT;
|
|
DECLARE vBookEntry INT;
|
|
DECLARE vFiscalYear INT;
|
|
DECLARE vIncorrectInvoiceInDueDay INT;
|
|
|
|
DECLARE vInvoicesIn CURSOR FOR
|
|
SELECT DISTINCT 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 vDone = TRUE;
|
|
|
|
SELECT GROUP_CONCAT(ii.id) INTO vIncorrectInvoiceInDueDay
|
|
FROM invoiceInDueDay iidd
|
|
JOIN invoiceIn ii ON iidd.invoiceInFk = ii.id
|
|
JOIN `entry` e ON e.invoiceInFk = ii.id
|
|
JOIN duaEntry de ON de.entryFk = e.id
|
|
JOIN invoiceInConfig iic
|
|
WHERE de.duaFk = vDuaFk
|
|
AND iidd.dueDated <= util.VN_CURDATE() + INTERVAL iic.dueDateMarginDays DAY;
|
|
|
|
IF vIncorrectInvoiceInDueDay THEN
|
|
CALL util.throw(CONCAT('Incorrect due date, invoice: ', vIncorrectInvoiceInDueDay));
|
|
END IF;
|
|
|
|
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.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)
|
|
WHERE d.id = vDuaFk;
|
|
|
|
SELECT ASIEN INTO vBookEntry FROM dua WHERE id = vDuaFk;
|
|
|
|
IF vBookEntry IS NULL THEN
|
|
SELECT YEAR(IFNULL(ii.bookEntried, d.bookEntried)) INTO vFiscalYear
|
|
FROM 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
|
|
WHERE d.id = vDuaFk
|
|
LIMIT 1;
|
|
CALL ledger_nextTx(vFiscalYear, vBookEntry);
|
|
END IF;
|
|
|
|
OPEN vInvoicesIn;
|
|
|
|
l: LOOP
|
|
SET vDone = FALSE;
|
|
FETCH vInvoicesIn INTO vInvoiceFk;
|
|
|
|
IF vDone THEN
|
|
LEAVE l;
|
|
END IF;
|
|
|
|
CALL invoiceIn_booking(vInvoiceFk, vBookEntry);
|
|
END LOOP;
|
|
|
|
CLOSE vInvoicesIn;
|
|
|
|
UPDATE dua
|
|
SET ASIEN = vBookEntry
|
|
WHERE id = vDuaFk;
|
|
|
|
UPDATE invoiceIn ii
|
|
JOIN duaInvoiceIn dii ON dii.invoiceInFk = ii.id
|
|
SET ii.isBooked = TRUE
|
|
WHERE dii.duaFk = vDuaFk;
|
|
|
|
UPDATE `entry` e
|
|
JOIN (
|
|
WITH entries AS (
|
|
SELECT e.id, de.duaFk
|
|
FROM vn.`entry` e
|
|
JOIN vn.duaEntry de ON de.entryFk = e.id
|
|
WHERE de.duaFk = vDuaFk
|
|
AND (NOT e.isBooked OR NOT e.isConfirmed)
|
|
),
|
|
notBookedEntries AS (
|
|
SELECT entryFk
|
|
FROM vn.duaEntry
|
|
WHERE duaFk = vDuaFk
|
|
AND NOT customsValue
|
|
)
|
|
SELECT e.id
|
|
FROM entries e
|
|
LEFT JOIN notBookedEntries nbe ON nbe.entryFk = e.id
|
|
WHERE nbe.entryFk IS NULL
|
|
) sub ON sub.id = e.id
|
|
SET e.isBooked = TRUE,
|
|
e.isConfirmed = TRUE;
|
|
END$$
|
|
DELIMITER ;
|