salix/db/routines/vn/procedures/boxPicking_print.sql

283 lines
6.9 KiB
MySQL
Raw Normal View History

2024-03-25 12:03:11 +00:00
DELIMITER $$
$$
2024-03-26 13:59:06 +00:00
CREATE OR REPLACE PROCEDURE vn.sale_boxPickingPrint(
2024-03-25 12:03:11 +00:00
IN vPrinterFk INT,
IN vSaleFk INT,
IN vPacking INT,
IN vSectorFk INT,
IN vUserFk INT,
IN vPackagingFk INT,
IN vPackingSiteFk INT)
BEGIN
/** Splits a line of sale to a different ticket and prints the transport sticker
*/
DECLARE vAgencyModeFk INT;
2024-03-26 13:59:06 +00:00
DECLARE vConcept VARCHAR(30);
2024-03-25 12:03:11 +00:00
DECLARE vExpeditionFk INT;
DECLARE vItemFk INT;
DECLARE vItemShelvingFk INT;
DECLARE vItemShelvingSaleFk INT;
DECLARE vItemShelvingSaleFk_old INT;
DECLARE vLastExpeditionTimeStamp DATETIME;
DECLARE vMaxPhoneLength INT DEFAULT 11;
DECLARE vMaxStreetLength INT DEFAULT 36;
DECLARE vNewSaleFk INT;
DECLARE vNewTicketFk INT;
DECLARE vParkingCode VARCHAR(10);
DECLARE vQuantity INT;
2024-03-27 11:22:22 +00:00
DECLARE vRemainder INT DEFAULT 0;
2024-03-27 11:25:57 +00:00
DECLARE vRemainderSaleFk INT;
2024-03-25 12:03:11 +00:00
DECLARE vShelving VARCHAR(10);
DECLARE vTicketFk INT;
SELECT s.quantity,
s.quantity MOD vPacking,
s.ticketFk,
s.itemFk,
s.concept
INTO vQuantity,
2024-03-27 11:25:57 +00:00
vRemainder,
2024-03-25 12:03:11 +00:00
vTicketFk,
vItemFk,
vConcept
FROM sale s
WHERE s.id = vSaleFk;
2024-03-27 11:22:22 +00:00
IF vRemainder THEN
UPDATE sale SET quantity = quantity - vRemainder WHERE id = vSaleFk;
2024-03-25 12:03:11 +00:00
2024-03-27 11:22:22 +00:00
INSERT INTO sale(ticketFk, itemFk, quantity, price, discount, concept)
2024-03-27 11:25:57 +00:00
SELECT ticketFk, itemFk, vRemainder, price, discount, concept
2024-03-27 11:22:22 +00:00
FROM sale
2024-03-25 12:03:11 +00:00
WHERE id = vSaleFk;
2024-03-27 11:25:57 +00:00
SET vRemainderSaleFk = LAST_INSERT_ID();
2024-03-25 12:03:11 +00:00
2024-03-27 11:22:22 +00:00
INSERT INTO saleComponent(saleFk, componentFk, value)
2024-03-27 11:25:57 +00:00
SELECT vRemainderSaleFk, componentFk, value
2024-03-27 11:22:22 +00:00
FROM saleComponent
2024-03-25 12:03:11 +00:00
WHERE saleFk = vSaleFk;
END IF;
w1: WHILE vQuantity >= vPacking DO
2024-03-26 13:59:06 +00:00
SET vItemShelvingFk = NULL;
2024-03-25 12:03:11 +00:00
SELECT sub.id
INTO vItemShelvingFk
2024-03-27 11:22:22 +00:00
FROM productionConfig pc
2024-03-25 12:03:11 +00:00
JOIN (
SELECT ish.id,
2024-03-27 11:22:22 +00:00
ish.visible - IFNULL(SUM(iss.quantity),0) available,
2024-03-25 12:03:11 +00:00
p.pickingOrder,
ish.created
2024-03-27 11:22:22 +00:00
FROM itemShelving ish
JOIN shelving sh ON sh.code = ish.shelvingFk
JOIN parking p ON p.id = sh.parkingFk
LEFT JOIN itemShelvingSale iss
2024-03-25 12:03:11 +00:00
ON iss.itemShelvingFk = ish.id
AND iss.created >= CURDATE()
AND iss.isPicked = FALSE
WHERE ish.itemFk = vItemFk
AND p.sectorFk = vSectorFk
GROUP BY ish.id
HAVING available >= vPacking) sub
ORDER BY IF(pc.orderMode = 'Location',sub.pickingOrder, sub.created)
LIMIT 1;
IF vItemShelvingFk THEN
2024-03-27 11:22:22 +00:00
INSERT INTO itemShelvingSale
2024-03-25 12:03:11 +00:00
SET itemShelvingFk = vItemShelvingFk,
saleFk = vSaleFk,
quantity = vPacking,
userFk = vUserFk,
isPicked = TRUE;
SET vItemShelvingSaleFk = LAST_INSERT_ID();
2024-03-27 11:22:22 +00:00
UPDATE sale SET isPicked = FALSE WHERE id = vSaleFk;
2024-03-25 12:03:11 +00:00
ELSE
LEAVE w1;
END IF;
2024-03-26 13:59:06 +00:00
SET vNewTicketFk = NULL;
2024-03-25 12:03:11 +00:00
SELECT MAX(t.id) INTO vNewTicketFk
2024-03-27 11:22:22 +00:00
FROM ticket t
JOIN ticketLastState tls ON tls.ticketFk = t.id
2024-03-25 12:03:11 +00:00
JOIN (SELECT addressFk, clientFk, date(shipped) shipped, warehouseFk
2024-03-27 11:22:22 +00:00
FROM ticket
2024-03-25 12:03:11 +00:00
WHERE id = vTicketFk) tt
ON tt.addressFk = t.addressFk
AND tt.clientFk = t.clientFk
AND t.shipped BETWEEN tt.shipped AND util.dayend(tt.shipped)
AND t.warehouseFk = tt.warehouseFk
WHERE tls.name = 'Encajado' ;
2024-03-26 13:59:06 +00:00
IF ISNULL(vNewTicketFk) THEN
2024-03-27 11:22:22 +00:00
INSERT INTO ticket( clientFk,
2024-03-25 12:03:11 +00:00
shipped,
addressFk,
agencyModeFk,
nickname,
warehouseFk,
companyFk,
landed,
zoneFk,
zonePrice,
zoneBonus,
routeFk,
priority,
hasPriority,
clonedFrom)
SELECT clientFk,
shipped,
addressFk,
agencyModeFk,
nickname,
warehouseFk,
companyFk,
landed,
zoneFk,
zonePrice,
zoneBonus,
routeFk,
priority,
hasPriority,
id
2024-03-27 11:22:22 +00:00
FROM ticket
2024-03-25 12:03:11 +00:00
WHERE id = vTicketFk;
SET vNewTicketFk = LAST_INSERT_ID();
INSERT INTO ticketTracking(ticketFk, stateFk, userFk)
SELECT vNewTicketFk, id, vUserFk
FROM state
WHERE code = 'PACKED';
END IF;
UPDATE sale SET quantity = quantity - vPacking WHERE id = vSaleFk;
UPDATE itemShelving SET visible = visible - vPacking WHERE id = vItemShelvingFk;
2024-03-26 13:59:06 +00:00
SET vNewSaleFk = NULL;
2024-03-25 12:03:11 +00:00
SELECT MAX(id) INTO vNewSaleFk
FROM sale
WHERE ticketFk = vNewTicketFk
AND itemFk = vItemFk;
IF vNewSaleFk THEN
UPDATE sale
SET quantity = quantity + vPacking
WHERE id = vNewSaleFk;
2024-03-26 13:59:06 +00:00
SET vItemShelvingSaleFk_old = NULL;
2024-03-25 12:03:11 +00:00
SELECT MAX(id) INTO vItemShelvingSaleFk_old
2024-03-27 11:22:22 +00:00
FROM itemShelvingSale
2024-03-25 12:03:11 +00:00
WHERE itemShelvingFk = vItemShelvingFk
AND saleFk = vNewSaleFk;
IF vItemShelvingSaleFk_old THEN
2024-03-27 11:22:22 +00:00
UPDATE itemShelvingSale
2024-03-25 12:03:11 +00:00
SET quantity = quantity + vPacking
WHERE id = vItemShelvingSaleFk_old;
2024-03-27 11:22:22 +00:00
DELETE FROM itemShelvingSale
2024-03-25 12:03:11 +00:00
WHERE id = vItemShelvingSaleFk;
SET vItemShelvingSaleFk = vItemShelvingSaleFk_old;
ELSE
UPDATE itemShelvingSale
SET saleFk = vNewSaleFk
WHERE id = vItemShelvingSaleFk;
END IF;
ELSE
2024-03-27 11:22:22 +00:00
INSERT INTO sale(ticketFk, itemFk, concept, quantity, discount, price)
2024-03-25 12:03:11 +00:00
SELECT vNewTicketFk, itemFk, concept, vPacking, discount, price
2024-03-27 11:22:22 +00:00
FROM sale
2024-03-25 12:03:11 +00:00
WHERE id = vSaleFk;
SET vNewSaleFk = LAST_INSERT_ID();
2024-03-27 11:22:22 +00:00
INSERT INTO saleComponent(saleFk, componentFk, value, isGreuge)
2024-03-25 12:03:11 +00:00
SELECT vNewSaleFk, componentFk, value, isGreuge
2024-03-27 11:22:22 +00:00
FROM saleComponent
2024-03-25 12:03:11 +00:00
WHERE saleFk = vSaleFk;
UPDATE itemShelvingSale
SET saleFk = vNewSaleFk
WHERE id = vItemShelvingSaleFk;
END IF;
2024-03-27 11:22:22 +00:00
INSERT IGNORE INTO saleTracking(saleFk, isChecked, workerFk, stateFk)
2024-03-25 12:03:11 +00:00
SELECT vNewSaleFk, TRUE, vUserFk, id
2024-03-27 11:22:22 +00:00
FROM state
2024-03-25 12:03:11 +00:00
WHERE code = 'PREPARED';
SELECT agencyModeFk INTO vAgencyModeFk
FROM ticket
WHERE id = vNewTicketFk;
2024-03-27 11:22:22 +00:00
INSERT INTO expedition(
2024-03-25 12:03:11 +00:00
agencyModeFk,
ticketFk,
freightItemFk,
workerFk,
packagingFk,
itemPackingTypeFk,
hostFk,
packingSiteFk,
monitorId,
started,
ended
)
SELECT vAgencyModeFk,
vNewTicketFk,
i.id,
vUserFk,
vPackagingFk,
ps.code,
h.code,
vPackingSiteFk,
ps.monitorId,
2024-03-27 11:22:22 +00:00
IFNULL(vLastExpeditionTimeStamp, NOW()),
NOW()
2024-03-25 12:03:11 +00:00
FROM packingSite ps
JOIN host h ON h.id = ps.hostFk
JOIN item i ON i.name = 'Shipping cost'
WHERE ps.id = vPackingSiteFk
LIMIT 1;
SET vExpeditionFk = LAST_INSERT_ID();
2024-03-27 11:22:22 +00:00
SET vLastExpeditionTimeStamp = NOW();
2024-03-25 12:03:11 +00:00
CALL dipole.expedition_Add(vExpeditionFk,vPrinterFk, TRUE);
SELECT shelvingFk, p.code
INTO vShelving, vParkingCode
FROM itemShelving ish
JOIN shelving sh ON sh.code = ish.shelvingFk
JOIN parking p ON p.id = sh.parkingFk
WHERE ish.id = vItemShelvingFk;
2024-03-26 13:59:06 +00:00
UPDATE dipole.expedition_PrintOut
SET isPrinted = FALSE,
itemFk = vItemFk,
quantity = vPacking,
longName = vConcept,
shelvingFk = vShelving,
parkingCode = vParkingCode,
phone = RIGHT(phone,vMaxPhoneLength),
street = RIGHT(street, vMAxStreetLength)
2024-03-25 12:03:11 +00:00
WHERE expeditionFk = vExpeditionFk;
2024-03-27 11:22:22 +00:00
DELETE FROM sale
2024-03-25 12:03:11 +00:00
WHERE quantity = 0
AND id = vSaleFk;
END WHILE;
END$$
DELIMITER ;