salix/db/routines/vn/functions/hasAnyNegativeBase.sql

38 lines
786 B
MySQL
Raw Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`hasAnyNegativeBase`()
RETURNS tinyint(1)
DETERMINISTIC
BEGIN
/* Calcula si existe alguna base imponible negativa
* Requiere la tabla temporal tmp.ticketToInvoice(id)
*
* returns BOOLEAN
*/
DECLARE hasAnyNegativeBase BOOLEAN;
DROP TEMPORARY TABLE IF EXISTS tmp.ticket;
CREATE TEMPORARY TABLE tmp.ticket
(KEY (ticketFk))
ENGINE = MEMORY
SELECT id ticketFk
FROM tmp.ticketToInvoice;
CALL ticket_getTax(NULL);
SELECT COUNT(*) INTO hasAnyNegativeBase
FROM(
SELECT SUM(taxableBase) as taxableBase
FROM tmp.ticketTax
GROUP BY pgcFk
HAVING taxableBase < 0
) t;
DROP TEMPORARY TABLE tmp.ticketTax;
DROP TEMPORARY TABLE tmp.ticket;
RETURN hasAnyNegativeBase;
END$$
DELIMITER ;