32 lines
839 B
SQL
32 lines
839 B
SQL
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 tickets ts
|
|
JOIN vn.ticket t ON t.addressFk = ts.addressFk
|
|
LEFT JOIN vn.saleVolume sv ON sv.ticketFk = t.id
|
|
JOIN vn.volumeConfig vc
|
|
WHERE t.shipped BETWEEN ts.dated AND util.dayEnd(ts.dated)
|
|
AND ticket_isProblemCalcNeeded(t.id);
|
|
|
|
RETURN vIsTooLittle;
|
|
END$$
|
|
DELIMITER ; |