salix/db/routines/vn/procedures/invoiceInTax_afterUpsert.sql

39 lines
1.1 KiB
MySQL
Raw Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`invoiceInTax_afterUpsert`(vInvoiceInFk INT)
BEGIN
/**
* Triggered actions when a invoiceInTax is updated or inserted.
*
* @param vInvoiceInFk The invoiceIn id
*/
DECLARE vTaxRowLimit INT;
DECLARE vLines INT;
DECLARE vHasDistinctTransactions INT;
CALL invoiceIn_checkBooked(vInvoiceInFk);
SELECT taxRowLimit INTO vTaxRowLimit FROM invoiceInConfig;
SELECT COUNT(*) INTO vLines
FROM invoiceInTax
2024-01-25 16:33:54 +00:00
WHERE invoiceInFk = vInvoiceInFk
AND (taxTypeSageFk OR transactionTypeSageFk);
IF vLines >= vTaxRowLimit THEN
CALL util.throw (CONCAT('The maximum number of lines is ', vTaxRowLimit));
END IF;
SELECT COUNT(DISTINCT transactionTypeSageFk) INTO vHasDistinctTransactions
FROM invoiceIn ii
JOIN invoiceInTax iit ON iit.invoiceInFk = ii.id
JOIN invoiceInSerial iis ON iis.code = ii.serial
WHERE ii.id = vInvoiceInFk
AND iis.taxAreaFk = 'CEE'
AND transactionTypeSageFk;
IF vHasDistinctTransactions > 1 THEN
2024-02-21 12:15:46 +00:00
CALL util.throw ('This invoice does not allow different types of transactions');
END IF;
END$$
DELIMITER ;