feat: refs #4948 Added ticket_doSelfConsumptionPackaging proc & event #2262

Closed
guillermo wants to merge 15 commits from 4948-procTicketSelfConsumption into dev
2 changed files with 48 additions and 29 deletions
Showing only changes of commit 2e482d85ad - Show all commits

View File

@ -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 ;

View File

@ -1,36 +1,63 @@
DELIMITER $$ DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `vn`.`ticket_doSelfConsumptionPackaging`( CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`expedition_doSelfConsumptionPackaging`(
vDateFrom DATE, vSelf DATE,
vDateTo DATE
) )
BEGIN 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 vSelf Id de expedición
* @param vDateTo Fecha hasta
*/ */
DECLARE vCurdate DATE;
DECLARE vClientFk INT; DECLARE vClientFk INT;
DECLARE vAddressFk INT; DECLARE vAddressFk INT;
DECLARE vTicketFk INT; DECLARE vTicketFk INT;
DECLARE vWarehouseFk INT; DECLARE vItemFk INT;
DECLARE vItemName VARCHAR(50);
DECLARE vExists BOOL;
SELECT pc.clientSelfConsumptionFk, pc.addressSelfConsumptionFk, w.id SET vCurdate = util.VN_CURDATE();
INTO vClientFk, vAddressFk, vWarehouseFk
FROM productionConfig pc
JOIN warehouse w ON w.code = 'alg';
INSERT INTO ticket(clientFk, warehouseFk, shipped, nickname, addressFk) SELECT clientSelfConsumptionFk, addressSelfConsumptionFk
SELECT vClientFk, vWarehouseFk, vDateFrom, 'CAJAS AUTOCONSUMO', vAddressFk; 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, t.warehouseFk, i.name
SELECT i.id, vTicketFk, i.name, COUNT(*) INTO vItemFk, vWarehouseFk, vItemName
FROM expedition e FROM expedition e
JOIN packaging p ON p.id = e.packagingFk JOIN packaging p ON p.id = e.packagingFk
JOIN item i ON i.id = p.itemFk JOIN item i ON i.id = p.itemFk
WHERE e.created BETWEEN vDateFrom AND util.dayEnd(vDateTo) JOIN ticket t ON t.id = e.ticketFk
GROUP BY e.packagingFk; 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$$ END$$
DELIMITER ; DELIMITER ;