2024-11-11 06:40:17 +00:00
|
|
|
DELIMITER $$
|
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`expedition_selfConsumptionPackaging`(
|
|
|
|
vSelf INT,
|
|
|
|
vAction ENUM('add', 'remove')
|
|
|
|
)
|
2024-11-26 08:01:04 +00:00
|
|
|
proc:BEGIN
|
2024-11-11 06:40:17 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
DECLARE vClientFk INT;
|
|
|
|
DECLARE vAddressFk INT;
|
|
|
|
DECLARE vItemFk INT;
|
|
|
|
DECLARE vItemName VARCHAR(50);
|
|
|
|
DECLARE vWarehouseFk INT;
|
|
|
|
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,
|
|
|
|
e.created
|
|
|
|
INTO vClientFk,
|
|
|
|
vAddressFk,
|
|
|
|
vItemFk,
|
|
|
|
vItemName,
|
|
|
|
vWarehouseFk,
|
|
|
|
vCreated
|
|
|
|
FROM expedition e
|
2024-11-26 08:01:04 +00:00
|
|
|
LEFT JOIN packaging p ON p.id = e.packagingFk
|
|
|
|
LEFT JOIN item i ON i.id = p.itemFk
|
2024-11-11 06:40:17 +00:00
|
|
|
JOIN ticket t ON t.id = e.ticketFk
|
|
|
|
JOIN productionConfig pc
|
|
|
|
WHERE e.id = vSelf;
|
|
|
|
|
2024-11-26 08:11:36 +00:00
|
|
|
IF vClientFk IS NULL OR vItemFk IS NULL THEN
|
2024-11-26 08:01:04 +00:00
|
|
|
LEAVE proc;
|
|
|
|
END IF;
|
|
|
|
|
2024-11-11 06:40:17 +00:00
|
|
|
SET vCreated = DATE(vCreated);
|
|
|
|
|
|
|
|
SELECT id INTO vTicketFk
|
|
|
|
FROM ticket
|
|
|
|
WHERE shipped BETWEEN vCreated AND util.dayEnd(vCreated)
|
|
|
|
AND clientFk = vClientFk
|
|
|
|
AND addressFk = vAddressFk
|
|
|
|
AND warehouseFk = vWarehouseFk;
|
|
|
|
|
|
|
|
IF vTicketFk IS NULL AND vAction = 'add' THEN
|
|
|
|
INSERT INTO ticket(clientFk, warehouseFk, shipped, nickname, addressFk)
|
|
|
|
VALUES (vClientFk, vWarehouseFk, vCreated, 'CAJAS AUTOCONSUMO', vAddressFk);
|
|
|
|
|
|
|
|
SET vTicketFk = LAST_INSERT_ID();
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|