2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-08-20 08:06:10 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`invoiceInTax_getFromEntries`(
|
2024-05-16 12:40:02 +00:00
|
|
|
IN vInvoiceInFk INT
|
|
|
|
)
|
2024-01-15 11:31:03 +00:00
|
|
|
BEGIN
|
|
|
|
DECLARE vRate DOUBLE DEFAULT 1;
|
|
|
|
DECLARE vExpenseFk VARCHAR(10);
|
|
|
|
|
2024-05-16 12:40:02 +00:00
|
|
|
WITH rate AS(
|
|
|
|
SELECT MAX(rr.dated) dated, ii.currencyFk
|
|
|
|
FROM vn.invoiceIn ii
|
|
|
|
JOIN vn.referenceRate rr ON rr.currencyFk = ii.currencyFk
|
|
|
|
WHERE ii.id = vInvoiceInFk
|
|
|
|
AND rr.dated <= ii.issued
|
|
|
|
) SELECT `value` INTO vRate
|
|
|
|
FROM vn.referenceRate rr
|
|
|
|
JOIN rate r ON r.dated = rr.dated
|
|
|
|
AND r.currencyFk = rr.currencyFk;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
|
|
|
SELECT id INTO vExpenseFk
|
2024-05-16 12:40:02 +00:00
|
|
|
FROM expense
|
2024-01-15 11:31:03 +00:00
|
|
|
WHERE `name` = 'Adquisición mercancia Extracomunitaria'
|
|
|
|
GROUP BY id
|
|
|
|
LIMIT 1;
|
|
|
|
|
|
|
|
DELETE FROM invoiceInTax
|
2024-02-15 09:04:15 +00:00
|
|
|
WHERE invoiceInFk = vInvoiceInFk;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-05-16 12:40:02 +00:00
|
|
|
INSERT INTO invoiceInTax(
|
|
|
|
invoiceInFk,
|
|
|
|
taxableBase,
|
|
|
|
expenseFk,
|
|
|
|
foreignValue,
|
|
|
|
taxTypeSageFk,
|
2024-01-15 11:31:03 +00:00
|
|
|
transactionTypeSageFk
|
2024-05-16 12:40:02 +00:00
|
|
|
)SELECT ii.id,
|
|
|
|
SUM(b.buyingValue * b.quantity) / vRate taxableBase,
|
|
|
|
vExpenseFk,
|
|
|
|
IF(ii.currencyFk = 1,
|
|
|
|
NULL,
|
|
|
|
SUM(b.buyingValue * b.quantity )),
|
|
|
|
taxTypeSageFk,
|
|
|
|
transactionTypeSageFk
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM invoiceIn ii
|
|
|
|
JOIN entry e ON e.invoiceInFk = ii.id
|
|
|
|
JOIN supplier s ON s.id = e.supplierFk
|
|
|
|
JOIN buy b ON b.entryFk = e.id
|
2024-02-15 09:04:15 +00:00
|
|
|
WHERE ii.id = vInvoiceInFk
|
2024-01-15 11:31:03 +00:00
|
|
|
HAVING taxableBase IS NOT NULL;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|