diff --git a/db/routines/cache/procedures/available_refresh.sql b/db/routines/cache/procedures/available_refresh.sql index abf023a41..87c003648 100644 --- a/db/routines/cache/procedures/available_refresh.sql +++ b/db/routines/cache/procedures/available_refresh.sql @@ -1,5 +1,10 @@ DELIMITER $$ -CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `cache`.`available_refresh`(OUT `vCalc` INT, IN `vRefresh` INT, IN `vWarehouse` INT, IN `vDated` DATE) +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `cache`.`available_refresh`( + OUT `vCalc` INT, + `vRefresh` INT, + `vWarehouse` INT, + `vDated` DATE +) proc: BEGIN DECLARE vStartDate DATE; DECLARE vReserveDate DATETIME; diff --git a/db/routines/cache/procedures/available_updateItem.sql b/db/routines/cache/procedures/available_updateItem.sql new file mode 100644 index 000000000..8e94a9d75 --- /dev/null +++ b/db/routines/cache/procedures/available_updateItem.sql @@ -0,0 +1,31 @@ +DELIMITER $$ +CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `cache`.`available_updateItem`( + `vItem` INT, + `vWarehouse` INT, + `vDated` DATE, + `vQuantity` INT +) +BEGIN +/** + * Immediately deduct/add an amount from the available cache (if exists). + * + * @param vItem The item id + * @param vWarehouse The warehouse id + * @param vDated Available cache date + * @param vQuantity The amount to be deducted from the cache + */ + DECLARE vCalc INT; + + SELECT id INTO vCalc + FROM cache_calc + WHERE cacheName = 'available' + AND params = CONCAT_WS('/', vWarehouse, vDated); + + IF vCalc IS NOT NULL THEN + UPDATE available + SET available = available - vQuantity + WHERE calc_id = vCalc + AND item_id = vItem; + END IF; +END$$ +DELIMITER ; diff --git a/db/routines/hedera/procedures/order_addItem.sql b/db/routines/hedera/procedures/order_addItem.sql index f690f9aa6..204dcb6bf 100644 --- a/db/routines/hedera/procedures/order_addItem.sql +++ b/db/routines/hedera/procedures/order_addItem.sql @@ -1,8 +1,8 @@ DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `hedera`.`order_addItem`( vSelf INT, - vWarehouse INT, - vItem INT, + vWarehouse INT, + vItem INT, vAmount INT) BEGIN /** @@ -37,7 +37,7 @@ BEGIN ROLLBACK; RESIGNAL; END; - + CALL order_calcCatalogFromItem(vSelf, vItem); START TRANSACTION; @@ -102,6 +102,8 @@ BEGIN amount = vAdd, price = vPrice; + CALL cache.available_updateItem(vItem, vWarehouse, vShipment, vAdd); + SET vRow = LAST_INSERT_ID(); INSERT INTO orderRowComponent (rowFk, componentFk, price) @@ -121,6 +123,6 @@ BEGIN END IF; COMMIT; - CALL vn.ticketCalculatePurge; + CALL vn.ticketCalculatePurge; END$$ DELIMITER ; diff --git a/modules/item/back/methods/item/specs/getVisibleAvailable.spec.js b/modules/item/back/methods/item/specs/getVisibleAvailable.spec.js index 61bf6b3e7..adf9b7f76 100644 --- a/modules/item/back/methods/item/specs/getVisibleAvailable.spec.js +++ b/modules/item/back/methods/item/specs/getVisibleAvailable.spec.js @@ -1,7 +1,7 @@ const models = require('vn-loopback/server/server').models; describe('item getVisibleAvailable()', () => { - it('should check available visible for today', async() => { + it('should check available visible for tomorrow', async() => { const tx = await models.Item.beginTransaction({}); const options = {transaction: tx}; @@ -9,6 +9,7 @@ describe('item getVisibleAvailable()', () => { const itemFk = 1; const warehouseFk = 1; const dated = Date.vnNew(); + dated.setDate(dated.getDate() + 1); const result = await models.Item.getVisibleAvailable(itemFk, warehouseFk, dated, options);