feat: refs #7704 Requested changes
gitea/salix/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Guillermo Bonet 2024-07-12 07:58:59 +02:00
parent 69e0d78537
commit 5c80a99543
9 changed files with 57 additions and 16 deletions

View File

@ -995,10 +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 4 DAY, 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, NULL, 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);
INSERT INTO `vn`.`expeditionBoxVol`(`boxFk`, `m3`, `ratio`) INSERT INTO `vn`.`expeditionBoxVol`(`boxFk`, `m3`, `ratio`)
VALUES VALUES

View File

@ -59,8 +59,8 @@ BEGIN
SELECT quantity INTO vMinQuantity SELECT quantity INTO vMinQuantity
FROM vn.itemMinimumQuantity FROM vn.itemMinimumQuantity
WHERE itemFk = vItem WHERE itemFk = vItem
AND `started` <= util.VN_CURDATE() AND `started` <= vShipment
AND (`ended` >= util.VN_CURDATE() 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; LIMIT 1;

View File

@ -59,12 +59,11 @@ BEGIN
price DECIMAL(10,2), price DECIMAL(10,2),
priceKg DECIMAL(10,2), priceKg DECIMAL(10,2),
`grouping` INT(10) UNSIGNED, `grouping` INT(10) UNSIGNED,
minQuantity INT(11), minQuantity INT(10) UNSIGNED,
PRIMARY KEY `itemFk` (`itemFk`) PRIMARY KEY `itemFk` (`itemFk`)
) ENGINE = MEMORY DEFAULT CHARSET=utf8; ) ENGINE = MEMORY DEFAULT CHARSET=utf8;
OPEN cTravelTree; OPEN cTravelTree;
l: LOOP l: LOOP
SET vDone = FALSE; SET vDone = FALSE;
FETCH cTravelTree INTO vZoneFk, vWarehouseFk, vShipped; FETCH cTravelTree INTO vZoneFk, vWarehouseFk, vShipped;
@ -151,8 +150,7 @@ BEGIN
priceKg, priceKg,
`grouping`, `grouping`,
minQuantity) minQuantity)
SELECT SELECT tl.itemFk,
tl.itemFk,
SUM(tl.available) available, SUM(tl.available) available,
p.name producer, p.name producer,
i.name item, i.name item,
@ -183,9 +181,9 @@ BEGIN
) bl ON bl.itemFk = tl.itemFk ) bl ON bl.itemFk = tl.itemFk
LEFT JOIN ( LEFT JOIN (
SELECT itemFk, quantity, warehouseFk SELECT itemFk, quantity, warehouseFk
FROM vn.itemMinimumQuantity FROM itemMinimumQuantity
WHERE `started` <= util.VN_CURDATE() WHERE `started` <= vShipped
AND (`ended` >= util.VN_CURDATE() OR `ended` IS NULL) AND (`ended` >= vShipped OR `ended` IS NULL)
GROUP BY itemFk, warehouseFk GROUP BY itemFk, warehouseFk
ORDER BY warehouseFk DESC ORDER BY warehouseFk DESC
) mq ON mq.itemFk = tl.itemFk ) mq ON mq.itemFk = tl.itemFk
@ -193,9 +191,7 @@ BEGIN
WHERE tl.zoneFk = vZoneFk AND tl.warehouseFk = vWarehouseFk WHERE tl.zoneFk = vZoneFk 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;
CLOSE cTravelTree; CLOSE cTravelTree;
END$$ END$$
DELIMITER ; DELIMITER ;

View File

@ -0,0 +1,8 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`itemMinimumQuantity_beforeInsert`
BEFORE INSERT ON `itemMinimumQuantity`
FOR EACH ROW
BEGIN
SET NEW.editorFk = account.myUser_getId();
END$$
DELIMITER ;

View File

@ -0,0 +1,8 @@
DELIMITER $$
CREATE OR REPLACE DEFINER=`root`@`localhost` TRIGGER `vn`.`itemMinimumQuantity_beforeUpdate`
BEFORE UPDATE ON `itemMinimumQuantity`
FOR EACH ROW
BEGIN
SET NEW.editorFk = account.myUser_getId();
END$$
DELIMITER ;

View File

@ -9,14 +9,17 @@ CREATE TABLE `vn`.`itemMinimumQuantity` (
`ended` date DEFAULT NULL, `ended` date DEFAULT NULL,
`warehouseFk` smallint(5) unsigned DEFAULT NULL, `warehouseFk` smallint(5) unsigned DEFAULT NULL,
`created` timestamp NOT NULL DEFAULT current_timestamp(), `created` timestamp NOT NULL DEFAULT current_timestamp(),
`editorFk` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
UNIQUE KEY `itemMinimumQuantity_UNIQUE` (`itemFk`, `started`, `ended`, `warehouseFk`), UNIQUE KEY `itemMinimumQuantity_UNIQUE` (`itemFk`, `started`, `ended`, `warehouseFk`),
KEY `itemFk` (`itemFk`), KEY `itemFk` (`itemFk`),
KEY `started` (`started`), KEY `started` (`started`),
KEY `ended` (`ended`), KEY `ended` (`ended`),
KEY `warehouseFk` (`warehouseFk`), KEY `warehouseFk` (`warehouseFk`),
KEY `editorFk` (`editorFk`),
CONSTRAINT `itemMinimumQuantity_ibfk_1` FOREIGN KEY (`itemFk`) REFERENCES `item` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `itemMinimumQuantity_ibfk_1` FOREIGN KEY (`itemFk`) REFERENCES `item` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `itemMinimumQuantity_ibfk_2` FOREIGN KEY (`warehouseFk`) REFERENCES `warehouse` (`id`) ON UPDATE CASCADE, CONSTRAINT `itemMinimumQuantity_ibfk_2` FOREIGN KEY (`warehouseFk`) REFERENCES `warehouse` (`id`) ON UPDATE CASCADE,
CONSTRAINT `itemMinimumQuantity_ibfk_3` FOREIGN KEY (`editorFk`) REFERENCES `account`.`user` (`id`),
CONSTRAINT `itemMinimumQuantity_check_1` CHECK (`started` <= `ended`) CONSTRAINT `itemMinimumQuantity_check_1` CHECK (`started` <= `ended`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci; ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci;

View File

@ -32,6 +32,9 @@
}, },
"created": { "created": {
"type": "date" "type": "date"
},
"editorFk": {
"type": "number"
} }
}, },
"relations": { "relations": {

View File

@ -88,14 +88,13 @@ module.exports = Self => {
if (await models.ACL.checkAccessAcl(ctx, 'Ticket', 'isRoleAdvanced', '*')) return; if (await models.ACL.checkAccessAcl(ctx, 'Ticket', 'isRoleAdvanced', '*')) return;
const now = Date.vnNew();
const minQuantity = await models.ItemMinimumQuantity.findOne({ const minQuantity = await models.ItemMinimumQuantity.findOne({
fields: ['quantity'], fields: ['quantity'],
where: { where: {
itemFk: itemId, itemFk: itemId,
started: {lte: now}, started: {lte: ticket.shipped},
or: [ or: [
{ended: {gte: now}}, {ended: {gte: ticket.shipped}},
{ended: null} {ended: null}
], ],
// eslint-disable-next-line no-dupe-keys // eslint-disable-next-line no-dupe-keys

View File

@ -210,6 +210,29 @@ describe('sale model ', () => {
expect(e).toEqual(new Error('The price of the item changed')); expect(e).toEqual(new Error('The price of the item changed'));
} }
}); });
it('should throw an error if the quantity is greater than the minimum quantity of the item but is out of range', async() => {
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getCtx(employeeId, true));
const saleId = 25;
const newQuantity = 5;
try {
spyOn(models.Sale, 'rawSql').and.callFake((sqlStatement, params, opts) => {
if (sqlStatement.includes('catalog_calcFromItem')) {
sqlStatement = `
CREATE OR REPLACE TEMPORARY TABLE tmp.ticketCalculateItem ENGINE = MEMORY SELECT ${newQuantity} as available;
CREATE OR REPLACE TEMPORARY TABLE tmp.ticketComponentPrice ENGINE = MEMORY SELECT 1 as grouping, 100000 as price;`;
params = null;
}
return models.Ticket.rawSql(sqlStatement, params, opts);
});
await models.Sale.updateQuantity(ctx, saleId, newQuantity, opts);
} catch (e) {
expect(e).toEqual(new Error('The amount cannot be less than the minimum'));
}
});
}); });
}); });