diff --git a/db/routines/vn/events/ticket_doSelfConsumptionPackaging.sql b/db/routines/vn/events/ticket_doSelfConsumptionPackaging.sql new file mode 100644 index 000000000..0876a38aa --- /dev/null +++ b/db/routines/vn/events/ticket_doSelfConsumptionPackaging.sql @@ -0,0 +1,8 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`root`@`localhost` EVENT `vn`.`ticket_doSelfConsumptionPackaging` + ON SCHEDULE EVERY 1 DAY + STARTS '2024-04-19 04:05:00.000' + ON COMPLETION PRESERVE + ENABLE +DO CALL ticket_doSelfConsumptionPackaging(util.yesterday(), util.yesterday())$$ +DELIMITER ; diff --git a/db/routines/vn/procedures/ticket_doSelfConsumptionPackaging.sql b/db/routines/vn/procedures/ticket_doSelfConsumptionPackaging.sql new file mode 100644 index 000000000..0866118f3 --- /dev/null +++ b/db/routines/vn/procedures/ticket_doSelfConsumptionPackaging.sql @@ -0,0 +1,39 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_doSelfConsumptionPackaging`( + vDateFrom DATE, + vDateTo DATE +) +BEGIN +/** +* Crea un ticket de autoconsumo de un rango determinado de fechas. +* +* @param vDateFrom Fecha desde +* @param vDateTo Fecha hasta +*/ + DECLARE vClientFk INT; + DECLARE vAddressFk INT; + DECLARE vTicketFk INT; + DECLARE vWarehouseFk INT; + + SELECT clientSelfConsumptionFk, addressSelfConsumptionFk + INTO vClientFk, vAddressFk + FROM productionConfig; + + SELECT id INTO vWarehouseFk + FROM warehouse + WHERE code = 'alg'; + + INSERT INTO ticket(clientFk, warehouseFk, shipped, nickname, addressFk) + SELECT vClientFk, vWarehouseFk, vDateFrom, 'AUTOCONSUMO CAJAS', vAddressFk; + + SET vTicketFk = LAST_INSERT_ID(); + + INSERT INTO sale(itemFk, ticketFk, concept, quantity) + SELECT i.id, vTicketFk, i.name, COUNT(*) + FROM expedition e + JOIN packaging p ON p.id = e.packagingFk + JOIN item i ON i.id = p.itemFk + WHERE e.created BETWEEN vDateFrom AND util.dayEnd(vDateTo) + GROUP BY e.packagingFk; +END$$ +DELIMITER ;