2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-02-15 09:04:15 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`invoiceInTax_getFromEntries`(IN vInvoiceInFk INT)
|
2024-01-15 11:31:03 +00:00
|
|
|
BEGIN
|
|
|
|
DECLARE vRate DOUBLE DEFAULT 1;
|
|
|
|
DECLARE vDated DATE;
|
|
|
|
DECLARE vExpenseFk VARCHAR(10);
|
2024-02-15 09:04:15 +00:00
|
|
|
DECLARE vIsBooked BOOLEAN DEFAULT FALSE;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-02-15 09:04:15 +00:00
|
|
|
SELECT isBooked INTO vIsBooked
|
|
|
|
FROM invoiceIn ii
|
|
|
|
WHERE id = vInvoiceInFk;
|
|
|
|
|
|
|
|
IF vIsBooked THEN
|
|
|
|
CALL util.throw('A booked invoice cannot be modified');
|
|
|
|
END IF;
|
|
|
|
|
2024-01-15 11:31:03 +00:00
|
|
|
SELECT MAX(rr.dated) INTO vDated
|
|
|
|
FROM referenceRate rr
|
2024-02-15 09:04:15 +00:00
|
|
|
JOIN invoiceIn ii ON ii.id = vInvoiceInFk
|
2024-01-15 11:31:03 +00:00
|
|
|
WHERE rr.dated <= ii.issued
|
|
|
|
AND rr.currencyFk = ii.currencyFk ;
|
|
|
|
|
|
|
|
IF vDated THEN
|
|
|
|
SELECT `value` INTO vRate
|
|
|
|
FROM referenceRate
|
|
|
|
WHERE dated = vDated;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
SELECT id INTO vExpenseFk
|
|
|
|
FROM vn.expense
|
|
|
|
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
|
|
|
|
|
|
|
INSERT INTO invoiceInTax(invoiceInFk, taxableBase, expenseFk, foreignValue, taxTypeSageFk, transactionTypeSageFk)
|
|
|
|
SELECT ii.id,
|
|
|
|
SUM(b.buyingValue * b.quantity) / IFNULL(vRate,1) taxableBase,
|
|
|
|
vExpenseFk,
|
|
|
|
IF(ii.currencyFk = 1,NULL,SUM(b.buyingValue * b.quantity )) divisa,
|
|
|
|
taxTypeSageFk,
|
|
|
|
transactionTypeSageFk
|
|
|
|
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
|
|
|
|
LEFT JOIN referenceRate rr ON rr.currencyFk = ii.currencyFk
|
|
|
|
AND rr.dated = ii.issued
|
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 ;
|