fix: refs #7704 Warehouse priority

This commit is contained in:
Guillermo Bonet 2024-07-12 14:04:49 +02:00
parent f8f6950de1
commit 1e264c2102
5 changed files with 38 additions and 14 deletions

View File

@ -995,11 +995,11 @@ INSERT INTO `vn`.`priceFixed`(`id`, `itemFk`, `rate0`, `rate1`, `rate2`, `rate3`
INSERT INTO `vn`.`itemMinimumQuantity`(`itemFk`, `quantity`, `started`, `ended`, `warehouseFk`) INSERT INTO `vn`.`itemMinimumQuantity`(`itemFk`, `quantity`, `started`, `ended`, `warehouseFk`)
VALUES VALUES
(1, 5, util.VN_CURDATE() - INTERVAL 2 MONTH, util.VN_CURDATE() + INTERVAL 1 MONTH, 1), (1, 5, util.VN_CURDATE() - INTERVAL 2 MONTH, util.VN_CURDATE() + INTERVAL 1 MONTH, 1),
(2, 10, util.VN_CURDATE() - INTERVAL 2 DAY, NULL, 2), (2, 10, util.VN_CURDATE() - INTERVAL 2 DAY, util.VN_CURDATE() - INTERVAL 1 DAY, 2),
(3, 15, util.VN_CURDATE() + INTERVAL 3 DAY, util.VN_CURDATE() + INTERVAL 2 WEEK, 3), (3, 15, util.VN_CURDATE() + INTERVAL 3 DAY, util.VN_CURDATE() + INTERVAL 2 WEEK, 3),
(2, 10, util.VN_CURDATE() + INTERVAL 2 MONTH, NULL, 5), (2, 10, util.VN_CURDATE() + INTERVAL 2 MONTH, NULL, 5),
(4, 8, util.VN_CURDATE() - INTERVAL 3 MONTH, NULL, NULL); (4, 8, util.VN_CURDATE() - INTERVAL 3 MONTH, NULL, NULL);
INSERT INTO `vn`.`expeditionBoxVol`(`boxFk`, `m3`, `ratio`) INSERT INTO `vn`.`expeditionBoxVol`(`boxFk`, `m3`, `ratio`)
VALUES VALUES

View File

@ -56,13 +56,21 @@ BEGIN
CALL util.throw ('ORDER_ROW_UNAVAILABLE'); CALL util.throw ('ORDER_ROW_UNAVAILABLE');
END IF; END IF;
SELECT quantity INTO vMinQuantity WITH itemMinQuantityPriority AS (
SELECT quantity,
ROW_NUMBER() OVER (
PARTITION BY itemFk
ORDER BY warehouseFk IS NULL
) priority
FROM vn.itemMinimumQuantity FROM vn.itemMinimumQuantity
WHERE itemFk = vItem WHERE itemFk = vItem
AND `started` <= vShipment AND `started` <= vShipment
AND (`ended` >= vShipment OR `ended` IS NULL) AND (`ended` >= vShipment OR `ended` IS NULL)
AND (warehouseFk = vWarehouse OR warehouseFk IS NULL) AND (warehouseFk = vWarehouse OR warehouseFk IS NULL)
LIMIT 1; )
SELECT quantity INTO vMinQuantity
FROM itemMinQuantityPriority
WHERE priority = 1;
IF vAmount < LEAST(IFNULL(vMinQuantity, 0), vAvailable) THEN IF vAmount < LEAST(IFNULL(vMinQuantity, 0), vAvailable) THEN
CALL util.throw ('quantityLessThanMin'); CALL util.throw ('quantityLessThanMin');

View File

@ -180,15 +180,24 @@ BEGIN
GROUP BY itemFk GROUP BY itemFk
) bl ON bl.itemFk = tl.itemFk ) bl ON bl.itemFk = tl.itemFk
LEFT JOIN ( LEFT JOIN (
SELECT itemFk, quantity, warehouseFk WITH itemMinQuantityPriority AS (
FROM itemMinimumQuantity SELECT itemFk,
quantity,
ROW_NUMBER() OVER (
PARTITION BY itemFk
ORDER BY warehouseFk IS NULL
) priority
FROM vn.itemMinimumQuantity
WHERE `started` <= vShipped WHERE `started` <= vShipped
AND (`ended` >= vShipped OR `ended` IS NULL) AND (`ended` >= vShipped OR `ended` IS NULL)
GROUP BY itemFk, warehouseFk AND (warehouseFk = vWarehouseFk OR warehouseFk IS NULL)
ORDER BY warehouseFk DESC )
SELECT itemFk, quantity
FROM itemMinQuantityPriority
WHERE priority = 1
) mq ON mq.itemFk = tl.itemFk ) mq ON mq.itemFk = tl.itemFk
AND (mq.warehouseFk = tl.warehouseFk OR mq.warehouseFk IS NULL) WHERE tl.zoneFk = vZoneFk
WHERE tl.zoneFk = vZoneFk AND tl.warehouseFk = vWarehouseFk AND tl.warehouseFk = vWarehouseFk
GROUP BY tl.itemFk GROUP BY tl.itemFk
ON DUPLICATE KEY UPDATE available = available + VALUES(available); ON DUPLICATE KEY UPDATE available = available + VALUES(available);
END LOOP; END LOOP;

View File

@ -0,0 +1,2 @@
ALTER TABLE vn.itemMinimumQuantity
ADD CONSTRAINT itemMinimumQuantity_check CHECK (quantity > 0);

View File

@ -88,8 +88,8 @@ module.exports = Self => {
if (await models.ACL.checkAccessAcl(ctx, 'Ticket', 'isRoleAdvanced', '*')) return; if (await models.ACL.checkAccessAcl(ctx, 'Ticket', 'isRoleAdvanced', '*')) return;
const minQuantity = await models.ItemMinimumQuantity.findOne({ const itemMinimalQuantity = await models.ItemMinimumQuantity.find({
fields: ['quantity'], fields: ['quantity', 'warehouseFk'],
where: { where: {
itemFk: itemId, itemFk: itemId,
started: {lte: ticket.shipped}, started: {lte: ticket.shipped},
@ -103,9 +103,14 @@ module.exports = Self => {
{warehouseFk: null} {warehouseFk: null}
] ]
}, },
limit: 1
}, ctx.options); }, ctx.options);
const minQuantity = itemMinimalQuantity.reduce((selected, item) => {
if (item.warehouseFk === ticket.warehouseFk) return item;
if (item.warehouseFk === null && !selected) return item;
return selected;
}, null);
if (newQuantity < minQuantity?.quantity && newQuantity != available) if (newQuantity < minQuantity?.quantity && newQuantity != available)
throw new UserError('The amount cannot be less than the minimum'); throw new UserError('The amount cannot be less than the minimum');