salix/db/routines/vn/procedures/itemCampaign_add.sql

55 lines
1.4 KiB
MySQL
Raw Normal View History

2024-10-22 13:00:16 +00:00
DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`itemCampaign_add`()
proc: BEGIN
/**
* Añade registros a tabla itemCampaign.
*
* @param vDateFrom Fecha desde
* @param vDateTo Fecha hasta
* @param vCampaign Código de la campaña
*/
DECLARE vYesterday DATE;
DECLARE vCampaign VARCHAR(100);
2024-10-23 06:23:28 +00:00
DECLARE vScopeDays INT;
2024-10-22 13:00:16 +00:00
DECLARE vPreviousDays INT;
DECLARE vDateSumFrom DATE;
DECLARE vDateSumTo DATE;
2024-10-23 06:23:28 +00:00
SET vYesterday = util.yesterday();
2024-10-22 13:00:16 +00:00
SELECT dated, code, scopeDays, previousDays
INTO vDateSumTo, vCampaign, vScopeDays, vPreviousDays
FROM campaign
2024-10-23 06:23:28 +00:00
WHERE dated >= vYesterday
2024-10-22 13:00:16 +00:00
ORDER BY dated
LIMIT 1;
IF vCampaign IS NULL THEN
CALL util.throw('Missing data in campaign table');
END IF;
2024-10-23 06:23:28 +00:00
IF NOT vYesterday BETWEEN vDateSumTo - INTERVAL vPreviousDays DAY
2024-10-22 13:00:16 +00:00
AND vDateSumTo THEN
LEAVE proc;
END IF;
SET vDateSumFrom = vDateSumTo - INTERVAL vScopeDays DAY;
2024-10-23 06:30:21 +00:00
SET vDateSumTo = vDateSumTo - INTERVAL 1 DAY;
2024-10-22 13:00:16 +00:00
2024-10-23 06:23:28 +00:00
INSERT INTO itemCampaign(dated, itemFk, quantity, total, campaign)
SELECT vYesterday,
2024-10-22 13:00:16 +00:00
s.itemFk,
2024-10-23 06:23:28 +00:00
SUM(s.quantity) quantity,
SUM((s.quantity * s.price) * (100 - s.discount) / 100) total,
2024-10-22 13:00:16 +00:00
vCampaign
FROM sale s
JOIN ticket t ON t.id = s.ticketFk
JOIN client c ON c.id = t.clientFk
2024-10-23 06:30:21 +00:00
WHERE t.shipped BETWEEN vDateSumFrom AND util.dayEnd(vDateSumTo)
2024-10-22 13:00:16 +00:00
AND c.typeFk = 'normal'
2024-10-23 06:23:28 +00:00
AND NOT t.isDeleted
GROUP BY s.itemFk
2024-10-22 13:00:16 +00:00
HAVING quantity;
END$$
DELIMITER ;