2024-03-12 07:25:58 +00:00
|
|
|
DELIMITER $$
|
2024-03-12 07:40:11 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`invoiceInTax_recalc`(
|
|
|
|
vInvoiceInFk INT
|
2024-03-12 07:25:58 +00:00
|
|
|
)
|
|
|
|
BEGIN
|
2024-03-12 07:35:13 +00:00
|
|
|
/**
|
2024-03-12 07:40:11 +00:00
|
|
|
* Recalcula y actualiza los impuestos de la factura
|
|
|
|
* usando la última tasa de cambio y detalles de compra.
|
2024-03-12 07:35:13 +00:00
|
|
|
*
|
2024-03-12 07:40:11 +00:00
|
|
|
* @param vInvoiceInFk Id de factura recibida
|
2024-03-12 07:35:13 +00:00
|
|
|
*/
|
2024-03-12 07:25:58 +00:00
|
|
|
DECLARE vRate DOUBLE DEFAULT 1;
|
|
|
|
DECLARE vDated DATE;
|
2024-03-21 11:06:59 +00:00
|
|
|
DECLARE vCurrencyFk INT;
|
|
|
|
DECLARE vExpenseFk VARCHAR(10);
|
2024-03-12 07:25:58 +00:00
|
|
|
|
2024-03-21 11:06:59 +00:00
|
|
|
SELECT MAX(rr.dated), ii.currencyFk INTO vDated, vCurrencyFk
|
2024-03-12 07:25:58 +00:00
|
|
|
FROM referenceRate rr
|
2024-03-12 07:40:11 +00:00
|
|
|
JOIN invoiceIn ii ON ii.id = vInvoiceInFk
|
2024-03-12 07:25:58 +00:00
|
|
|
WHERE rr.dated <= ii.issued
|
|
|
|
AND rr.currencyFk = ii.currencyFk;
|
|
|
|
|
2024-03-21 11:06:59 +00:00
|
|
|
IF vDated AND vCurrencyFk THEN
|
2024-03-12 07:25:58 +00:00
|
|
|
SELECT `value` INTO vRate
|
|
|
|
FROM referenceRate
|
2024-03-21 11:06:59 +00:00
|
|
|
WHERE dated = vDated
|
|
|
|
AND currencyFk = vCurrencyFk;
|
2024-03-12 07:25:58 +00:00
|
|
|
END IF;
|
|
|
|
|
2024-03-12 07:40:11 +00:00
|
|
|
DELETE FROM invoiceInTax WHERE invoiceInFk = vInvoiceInFk;
|
2024-03-12 07:25:58 +00:00
|
|
|
|
|
|
|
SELECT id INTO vExpenseFk
|
|
|
|
FROM expense
|
|
|
|
WHERE code = 'extraCommGoodsAcquisition';
|
|
|
|
|
|
|
|
IF vExpenseFk IS NULL THEN
|
|
|
|
CALL util.throw('Expense extraCommGoodsAcquisition not exists');
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
INSERT INTO invoiceInTax(
|
|
|
|
invoiceInFk,
|
|
|
|
taxableBase,
|
|
|
|
expenseFk,
|
|
|
|
foreignValue,
|
|
|
|
taxTypeSageFk,
|
|
|
|
transactionTypeSageFk
|
|
|
|
)
|
|
|
|
SELECT ii.id,
|
|
|
|
SUM(b.buyingValue * b.quantity) / vRate bi,
|
|
|
|
vExpenseFk,
|
|
|
|
IF(c.code = 'EUR', NULL, SUM(b.buyingValue * b.quantity)),
|
|
|
|
s.taxTypeSageFk,
|
|
|
|
s.transactionTypeSageFk
|
|
|
|
FROM invoiceIn ii
|
|
|
|
JOIN currency c ON c.id = ii.currencyFk
|
|
|
|
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-03-12 07:40:11 +00:00
|
|
|
WHERE ii.id = vInvoiceInFk
|
2024-03-12 07:25:58 +00:00
|
|
|
HAVING bi IS NOT NULL;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|