2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-07-22 09:52:42 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` FUNCTION `vn`.`hasSomeNegativeBase`(vTicket INT)
|
2024-01-15 11:31:03 +00:00
|
|
|
RETURNS tinyint(1)
|
|
|
|
NOT DETERMINISTIC
|
|
|
|
READS SQL DATA
|
|
|
|
BEGIN
|
|
|
|
DECLARE vCountry INT;
|
|
|
|
DECLARE hasSomeNegativeBase BOOLEAN;
|
|
|
|
|
|
|
|
SELECT s.countryFk
|
|
|
|
INTO vCountry
|
|
|
|
FROM supplier s
|
|
|
|
JOIN ticket t ON t.companyFk = s.id
|
|
|
|
WHERE t.id = vTicket;
|
|
|
|
|
|
|
|
SELECT COUNT(*) INTO hasSomeNegativeBase
|
|
|
|
FROM (
|
|
|
|
SELECT SUM(ROUND(s.quantity * s.price * (100 - s.discount)/100,2)) taxableBase
|
|
|
|
FROM sale s
|
|
|
|
JOIN item i ON i.id = s.itemFk
|
|
|
|
JOIN itemTaxCountry itc
|
|
|
|
ON itc.itemFk = i.id AND itc.countryFk = vCountry
|
|
|
|
WHERE s.ticketFk = vTicket
|
|
|
|
GROUP BY itc.taxClassFk
|
|
|
|
HAVING taxableBase < 0
|
|
|
|
) t1 ;
|
|
|
|
|
|
|
|
RETURN hasSomeNegativeBase;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|