feat: refs #4948 Version expedition_doSelfConsumptionPackaging

This commit is contained in:
Guillermo Bonet 2024-10-29 14:25:06 +01:00
parent 03b06b906e
commit 2e482d85ad
2 changed files with 48 additions and 29 deletions

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 $$
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();
SELECT clientSelfConsumptionFk, addressSelfConsumptionFk
INTO vClientFk, vAddressFk
FROM productionConfig;
IF vClientFk IS NULL OR vAddressFk IS NULL THEN
CALL util.throw('Some config parameters are not set');
END IF;
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, vDateFrom, 'CAJAS AUTOCONSUMO', vAddressFk;
SELECT vClientFk, vWarehouseFk, vCurdate, 'CAJAS AUTOCONSUMO', 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;
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 ;