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

33 lines
849 B
MySQL
Raw Normal View History

DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`ticket_isTooLittle`(
vSelf INT
)
RETURNS tinyint(1)
READS SQL DATA
BEGIN
/**
* Check if the ticket is small based on the volume and amount parameters.
*
* @param vSelf Id ticket
* @return BOOL
*/
DECLARE vIsTooLittle TINYINT(1);
WITH tickets AS (
SELECT addressFk, DATE(shipped) dated
FROM vn.ticket
WHERE id = vSelf
)
SELECT (SUM(IFNULL(sv.litros, 0)) < vc.minTicketVolume
AND SUM(IFNULL(t.totalWithoutVat, 0)) < vc.minTicketValue) INTO vIsTooLittle
FROM vn.ticket t
JOIN tickets ts ON ts.addressFk = t.addressFk
JOIN vn.volumeConfig vc
LEFT JOIN vn.saleVolume sv ON sv.ticketFk = t.id
WHERE t.shipped BETWEEN ts.dated AND util.dayEnd(ts.dated)
AND sv.litros > 0
AND t.totalWithoutVat > 0;
RETURN vIsTooLittle;
END$$
DELIMITER ;