2024-04-16 15:01:14 +00:00
|
|
|
DELIMITER $$
|
2024-05-02 16:13:12 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`ticket_isTooLittle`(
|
|
|
|
vSelf INT
|
|
|
|
)
|
2024-04-16 15:01:14 +00:00
|
|
|
RETURNS tinyint(1)
|
|
|
|
READS SQL DATA
|
|
|
|
BEGIN
|
|
|
|
/**
|
2024-05-02 16:13:12 +00:00
|
|
|
* Check if the ticket is small based on the volume and amount parameters.
|
2024-04-16 15:01:14 +00:00
|
|
|
*
|
2024-05-13 11:31:58 +00:00
|
|
|
* @param vSelf Id ticket
|
2024-04-16 15:01:14 +00:00
|
|
|
* @return BOOL
|
|
|
|
*/
|
|
|
|
DECLARE vIsTooLittle TINYINT(1);
|
2024-08-27 15:12:41 +00:00
|
|
|
|
|
|
|
WITH tickets AS (
|
|
|
|
SELECT addressFk, DATE(shipped) dated
|
|
|
|
FROM vn.ticket
|
|
|
|
WHERE id = vSelf
|
|
|
|
)
|
2024-04-16 15:01:14 +00:00
|
|
|
SELECT (SUM(IFNULL(sv.litros, 0)) < vc.minTicketVolume
|
2024-08-27 15:12:41 +00:00
|
|
|
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;
|
2024-04-16 15:01:14 +00:00
|
|
|
|
|
|
|
RETURN vIsTooLittle;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|