diff --git a/db/routines/vn/events/ticket_doSelfConsumptionPackaging.sql b/db/routines/vn/events/ticket_doSelfConsumptionPackaging.sql deleted file mode 100644 index 0876a38aa..000000000 --- a/db/routines/vn/events/ticket_doSelfConsumptionPackaging.sql +++ /dev/null @@ -1,8 +0,0 @@ -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 index 4f2dc1401..4c7edd49c 100644 --- a/db/routines/vn/procedures/ticket_doSelfConsumptionPackaging.sql +++ b/db/routines/vn/procedures/ticket_doSelfConsumptionPackaging.sql @@ -1,36 +1,63 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_doSelfConsumptionPackaging`( - vDateFrom DATE, - vDateTo DATE +CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`expedition_doSelfConsumptionPackaging`( + vSelf DATE, ) BEGIN /** -* Crea un ticket de autoconsumo de un rango determinado de fechas. +* Comprueba si existe un ticket de autoconsumo ese mismo día +* si no lo está lo crea, y añade la caja utilizada. * -* @param vDateFrom Fecha desde -* @param vDateTo Fecha hasta +* @param vSelf Id de expedición */ + DECLARE vCurdate DATE; DECLARE vClientFk INT; DECLARE vAddressFk INT; DECLARE vTicketFk INT; - DECLARE vWarehouseFk INT; + DECLARE vItemFk INT; + DECLARE vItemName VARCHAR(50); + DECLARE vExists BOOL; - SELECT pc.clientSelfConsumptionFk, pc.addressSelfConsumptionFk, w.id - INTO vClientFk, vAddressFk, vWarehouseFk - FROM productionConfig pc - JOIN warehouse w ON w.code = 'alg'; + SET vCurdate = util.VN_CURDATE(); - INSERT INTO ticket(clientFk, warehouseFk, shipped, nickname, addressFk) - SELECT vClientFk, vWarehouseFk, vDateFrom, 'CAJAS AUTOCONSUMO', vAddressFk; + SELECT clientSelfConsumptionFk, addressSelfConsumptionFk + INTO vClientFk, vAddressFk + FROM productionConfig; - SET vTicketFk = LAST_INSERT_ID(); + IF vClientFk IS NULL OR vAddressFk IS NULL THEN + CALL util.throw('Some config parameters are not set'); + END IF; - 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; + SELECT i.id, t.warehouseFk, i.name + INTO vItemFk, vWarehouseFk, vItemName + FROM expedition e + JOIN packaging p ON p.id = e.packagingFk + JOIN item i ON i.id = p.itemFk + JOIN ticket t ON t.id = e.ticketFk + WHERE e.id = vSelf; + + SELECT id INTO vTicketFk + FROM ticket + WHERE shipped BETWEEN vCurdate AND util.dayEnd(vCurdate) + AND clientFk = vClientFk + AND addressFk = vAddressFk + AND warehouseFk = vWarehouseFk; + + IF vTicketFk IS NULL THEN + INSERT INTO ticket(clientFk, warehouseFk, shipped, nickname, addressFk) + SELECT vClientFk, vWarehouseFk, vCurdate, 'CAJAS AUTOCONSUMO', vAddressFk; + + SET vTicketFk = LAST_INSERT_ID(); + + INSERT INTO sale(itemFk, ticketFk, concept, quantity) + SELECT vItemFk, vTicketFk, vItemName, 1; + ELSE + SELECT id INTO vExists + FROM sale + WHERE itemFk = vItemFk + AND ticketFk = vTicketFk; + + -- Comprobar si existe o no y si existe, sumar +1 la quantity, si no, lo creamos + + END IF; END$$ DELIMITER ;