feat: refs #4948 Requested changes
gitea/salix/pipeline/pr-dev There was a failure building this commit Details

This commit is contained in:
Guillermo Bonet 2024-11-08 09:40:27 +01:00
parent ad3b9ce365
commit 927e837a29
3 changed files with 43 additions and 92 deletions

View File

@ -1,11 +1,12 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`expedition_doSelfConsumptionPackaging`(
vSelf INT
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`expedition_selfConsumptionPackaging`(
vSelf INT,
vAction ENUM('add', 'remove')
)
proc: BEGIN
BEGIN
/**
* Comprueba si existe un ticket de cajas autoconsumo ese mismo día,
* si no lo está lo crea y añade la caja utilizada.
* Maneja el consumo de cajas para autoconsumo, permitiendo
* añadir o quitar cajas utilizadas según la acción indicada.
*
* @param vSelf Id de expedición
*/
@ -14,20 +15,27 @@ proc: BEGIN
DECLARE vItemFk INT;
DECLARE vItemName VARCHAR(50);
DECLARE vWarehouseFk INT;
DECLARE vCurdate DATE;
DECLARE vCreated DATE;
DECLARE vTicketFk INT;
DECLARE vSaleFk INT;
DECLARE vQuantity INT;
IF vAction NOT IN ('add', 'remove') THEN
CALL util.throw('Action not supported');
END IF;
SELECT pc.clientSelfConsumptionFk,
pc.addressSelfConsumptionFk,
i.id,
i.name,
t.warehouseFk
t.warehouseFk,
e.created
INTO vClientFk,
vAddressFk,
vItemFk,
vItemName,
vWarehouseFk
vWarehouseFk,
vCreated
FROM expedition e
JOIN packaging p ON p.id = e.packagingFk
JOIN item i ON i.id = p.itemFk
@ -39,35 +47,47 @@ proc: BEGIN
CALL util.throw('Some config parameters are not set');
END IF;
SET vCurdate = util.VN_CURDATE();
SET vCreated = DATE(vCreated);
SELECT id INTO vTicketFk
FROM ticket
WHERE shipped BETWEEN vCurdate AND util.dayEnd(vCurdate)
WHERE shipped BETWEEN vCreated AND util.dayEnd(vCreated)
AND clientFk = vClientFk
AND addressFk = vAddressFk
AND warehouseFk = vWarehouseFk;
IF vTicketFk IS NULL THEN
IF vTicketFk IS NULL AND vAction = 'add' THEN
INSERT INTO ticket(clientFk, warehouseFk, shipped, nickname, addressFk)
SELECT vClientFk, vWarehouseFk, vCurdate, 'CAJAS AUTOCONSUMO', vAddressFk;
VALUES (vClientFk, vWarehouseFk, vCreated, 'CAJAS AUTOCONSUMO', vAddressFk);
SET vTicketFk = LAST_INSERT_ID();
ELSE
SELECT id INTO vSaleFk
FROM sale
WHERE itemFk = vItemFk
AND ticketFk = vTicketFk;
END IF;
SELECT id, quantity INTO vSaleFk, vQuantity
FROM sale
WHERE itemFk = vItemFk
AND ticketFk = vTicketFk
LIMIT 1;
IF vAction = 'add' THEN
IF vSaleFk IS NOT NULL THEN
UPDATE sale
SET quantity = quantity + 1
WHERE id = vSaleFk;
LEAVE proc;
END IF;
ELSE
INSERT INTO sale(itemFk, ticketFk, concept, quantity)
VALUES (vItemFk, vTicketFk, vItemName, 1);
END IF;
ELSE
IF vSaleFk IS NOT NULL THEN
IF vQuantity > 1 THEN
UPDATE sale
SET quantity = quantity - 1
WHERE id = vSaleFk;
ELSE
DELETE FROM sale WHERE id = vSaleFk;
END IF;
END IF;
END IF;
INSERT INTO sale(itemFk, ticketFk, concept, quantity)
SELECT vItemFk, vTicketFk, vItemName, 1;
END$$
DELIMITER ;

View File

@ -1,69 +0,0 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`expedition_undoSelfConsumptionPackaging`(
vSelf INT
)
BEGIN
/**
* Comprueba si existe un ticket de cajas autoconsumo ese mismo día,
* si lo está quita la caja utilizada.
*
* @param vSelf Id de expedición
*/
DECLARE vClientFk INT;
DECLARE vAddressFk INT;
DECLARE vItemFk INT;
DECLARE vItemName VARCHAR(50);
DECLARE vWarehouseFk INT;
DECLARE vCurdate DATE;
DECLARE vTicketFk INT;
DECLARE vSaleFk INT;
DECLARE vQuantity INT;
SELECT pc.clientSelfConsumptionFk,
pc.addressSelfConsumptionFk,
i.id,
i.name,
t.warehouseFk
INTO vClientFk,
vAddressFk,
vItemFk,
vItemName,
vWarehouseFk
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
JOIN productionConfig pc
WHERE e.id = vSelf;
IF vClientFk IS NULL OR vAddressFk IS NULL THEN
CALL util.throw('Some config parameters are not set');
END IF;
SET vCurdate = util.VN_CURDATE();
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 NOT NULL THEN
SELECT id, quantity INTO vSaleFk, vQuantity
FROM sale
WHERE itemFk = vItemFk
AND ticketFk = vTicketFk;
IF vSaleFk IS NOT NULL THEN
IF vQuantity <= 1 THEN
DELETE FROM sale WHERE id = vSaleFk;
ELSE
UPDATE sale
SET quantity = quantity - 1
WHERE id = vSaleFk;
END IF;
END IF;
END IF;
END$$
DELIMITER ;

View File

@ -9,6 +9,6 @@ BEGIN
`changedModelId` = OLD.id,
`userFk` = account.myUser_getId();
CALL expedition_undoSelfConsumptionPackaging(OLD.id);
CALL expedition_selfConsumptionPackaging(OLD.id, 'remove');
END$$
DELIMITER ;