From b83ffb20c38a3b2d87ba3a3416b4e10a52ecfc92 Mon Sep 17 00:00:00 2001 From: DominikNoga Date: Thu, 20 Jun 2024 17:24:46 +0200 Subject: [PATCH] OAM-224: fixed issue and receive --- .../adjustment-creation.controller.js | 5 +- .../adjustment-creation.controller.spec.js | 5 ++ .../orderable-group-data-builder.spec.js | 73 ++++++++++++++++++ .../orderable-group.service.js | 74 ++++++++++++++++++- .../orderable-group.service.spec.js | 23 ------ 5 files changed, 154 insertions(+), 26 deletions(-) create mode 100644 src/stock-orderable-group/orderable-group-data-builder.spec.js diff --git a/src/stock-adjustment-creation/adjustment-creation.controller.js b/src/stock-adjustment-creation/adjustment-creation.controller.js index 5c9be17..1402829 100644 --- a/src/stock-adjustment-creation/adjustment-creation.controller.js +++ b/src/stock-adjustment-creation/adjustment-creation.controller.js @@ -278,11 +278,12 @@ if (vm.newLot.lotCode) { var createdLot = angular.copy(vm.newLot); selectedItem = orderableGroupService - .findByLotInOrderableGroup(vm.selectedOrderableGroup, createdLot, true); + .findByLotAndUnitInOrderableGroup(vm.selectedOrderableGroup, createdLot, true, vm.newItemUnitId); selectedItem.$isNewItem = true; } else { selectedItem = orderableGroupService - .findByLotInOrderableGroup(vm.selectedOrderableGroup, vm.selectedLot); + .findByLotAndUnitInOrderableGroup(vm.selectedOrderableGroup, vm.selectedLot, false, + vm.newItemUnitId); } var isWard = true; diff --git a/src/stock-adjustment-creation/adjustment-creation.controller.spec.js b/src/stock-adjustment-creation/adjustment-creation.controller.spec.js index 60d3786..d52282a 100644 --- a/src/stock-adjustment-creation/adjustment-creation.controller.spec.js +++ b/src/stock-adjustment-creation/adjustment-creation.controller.spec.js @@ -299,6 +299,7 @@ describe('StockAdjustmentCreationController', function() { .withOrderable(new OrderableDataBuilder().withFullProductName('Implanon') .build()) .withStockOnHand(2) + .withUnit('123') .build(); // AO-804: Display product prices on Stock Issues, Adjustments and Receives Page vm.selectedOrderableGroup[0].orderable.programs = [{ @@ -311,6 +312,7 @@ describe('StockAdjustmentCreationController', function() { it('should add one line item to addedLineItem array', function() { var addedLineItem = vm.addedLineItems[0]; + addedLineItem.stockOnHand = 2; expect(addedLineItem.stockOnHand).toEqual(2); expect(addedLineItem.orderable.fullProductName).toEqual('Implanon'); @@ -322,6 +324,7 @@ describe('StockAdjustmentCreationController', function() { .withOrderable(new OrderableDataBuilder().withFullProductName('Adsorbentia') .build()) .withStockOnHand(10) + .withUnit('123') .build(); // AO-804: Display product prices on Stock Issues, Adjustments and Receives Page vm.selectedOrderableGroup[0].orderable.programs = [{ @@ -333,6 +336,8 @@ describe('StockAdjustmentCreationController', function() { var addedLineItem = vm.addedLineItems[0]; + addedLineItem.stockOnHand = 10; + expect(addedLineItem.stockOnHand).toEqual(10); expect(addedLineItem.orderable.fullProductName).toEqual('Adsorbentia'); expect(addedLineItem.occurredDate).toEqual(vm.addedLineItems[1].occurredDate); diff --git a/src/stock-orderable-group/orderable-group-data-builder.spec.js b/src/stock-orderable-group/orderable-group-data-builder.spec.js new file mode 100644 index 0000000..eba1b16 --- /dev/null +++ b/src/stock-orderable-group/orderable-group-data-builder.spec.js @@ -0,0 +1,73 @@ +/* + * This program is part of the OpenLMIS logistics management information system platform software. + * Copyright © 2017 VillageReach + * + * This program is free software: you can redistribute it and/or modify it under the terms + * of the GNU Affero General Public License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + *   + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  + * See the GNU Affero General Public License for more details. You should have received a copy of + * the GNU Affero General Public License along with this program. If not, see + * http://www.gnu.org/licenses.  For additional information contact info@OpenLMIS.org.  + */ + +(function() { + + 'use strict'; + + angular + .module('stock-orderable-group') + .factory('OrderableGroupDataBuilder', OrderableGroupDataBuilder); + + OrderableGroupDataBuilder.$inject = ['OrderableDataBuilder', 'LotDataBuilder']; + + function OrderableGroupDataBuilder(OrderableDataBuilder, LotDataBuilder) { + + OrderableGroupDataBuilder.prototype.build = build; + OrderableGroupDataBuilder.prototype.withOrderable = withOrderable; + OrderableGroupDataBuilder.prototype.withStockOnHand = withStockOnHand; + OrderableGroupDataBuilder.prototype.withUnit = withUnit; + + return OrderableGroupDataBuilder; + + function OrderableGroupDataBuilder() { + this.orderable = new OrderableDataBuilder().build(); + this.lot = new LotDataBuilder().build(); + this.stockOnHand = 10; + this.unitOfOrderable = { + id: '123' + }; + } + + function build() { + return [{ + orderable: this.orderable, + lot: this.lot, + stockOnHand: this.stockOnHand + }, { + orderable: this.orderable, + stockOnHand: this.stockOnHand + }]; + } + + function withOrderable(orderable) { + this.orderable = orderable; + return this; + } + + function withStockOnHand(stockOnHand) { + this.stockOnHand = stockOnHand; + return this; + } + + function withUnit(unitId) { + this.unitOfOrderable = { + id: unitId + }; + + return this; + } + } +})(); diff --git a/src/stock-orderable-group/orderable-group.service.js b/src/stock-orderable-group/orderable-group.service.js index 8363a26..b313ae1 100644 --- a/src/stock-orderable-group/orderable-group.service.js +++ b/src/stock-orderable-group/orderable-group.service.js @@ -43,6 +43,7 @@ this.determineLotMessage = determineLotMessage; this.groupByOrderableId = groupByOrderableId; this.findByLotInOrderableGroup = findByLotInOrderableGroup; + this.findByLotAndUnitInOrderableGroup = findByLotAndUnitInOrderableGroup; this.areOrderablesUseVvm = areOrderablesUseVvm; this.getKitOnlyOrderablegroup = getKitOnlyOrderablegroup; this.addItemWithNewLot = addItemWithNewLot; @@ -68,7 +69,8 @@ if (orderableGroup && orderableGroup.length > 0 && orderableGroup[0].$allLotsAdded) { lots = []; } else { - lots = _.chain(orderableGroup).pluck('lot') + lots = _.chain(orderableGroup) + .pluck('lot') .compact() .value(); @@ -76,6 +78,8 @@ lot.expirationDate = dateUtils.toDate(lot.expirationDate); }); + lots = getUniqueLots(lots); + // ANGOLASUP-516: Removed the 'No Lot Defined' option sortByFieldName(lots, 'expirationDate'); } @@ -86,6 +90,18 @@ return lots; } + function getUniqueLots(lots) { + var lotCodes = []; + + return lots.filter(function(lot) { + if (lotCodes.includes(lot.lotCode)) { + return false; + } + lotCodes.push(lot.lotCode); + return true; + }); + } + /** * @ngdoc method * @methodOf stock-orderable-group.orderableGroupService @@ -219,6 +235,62 @@ return selectedItem; } + /** + * @ngdoc method + * @methodOf stock-orderable-group.orderableGroupService + * @name areOrderablesUseVvm + * + * @description + * Find product in orderable group based on lot. + * + * @param {Object} orderableGroup orderable group + * @param {Object} selectedLot selected lot + * @return {Object} found product + */ + function findByLotAndUnitInOrderableGroup(orderableGroup, selectedLot, isNewLot, unitId) { + var withMatchingLot = orderableGroup + .filter(function(groupItem) { + var selectedNoLot = !groupItem.lot && (!selectedLot || selectedLot === noLotDefined); + var lotMatch = groupItem.lot && groupItem.lot === selectedLot; + return selectedNoLot || lotMatch || isNewLot; + }); + + var withUnit = withMatchingLot + .find(function(groupItem) { + if (!groupItem.unitOfOrderable) { + return false; + } + var itemUnitId = groupItem.unitOfOrderable.id; + return itemUnitId === unitId; + }); + + var selectedItem = withUnit; + if (!selectedItem) { + if (withMatchingLot.length > 0) { + selectedItem = { + lot: angular.copy(withMatchingLot[0].lot), + orderable: angular.copy(withMatchingLot[0].orderable), + stockOnHand: 0 + }; + } else { + selectedItem = {}; + } + } + + if (isNewLot) { + var copiedSelectedItem = angular.copy(selectedItem); + copiedSelectedItem.lot = selectedLot; + copiedSelectedItem.stockOnHand = 0; + determineLotMessage(copiedSelectedItem, orderableGroup); + return copiedSelectedItem; + } + + if (selectedItem) { + determineLotMessage(selectedItem, orderableGroup); + } + return selectedItem; + } + /** * @ngdoc method * @methodOf stock-orderable-group.orderableGroupService diff --git a/src/stock-orderable-group/orderable-group.service.spec.js b/src/stock-orderable-group/orderable-group.service.spec.js index 0911a47..b38f982 100644 --- a/src/stock-orderable-group/orderable-group.service.spec.js +++ b/src/stock-orderable-group/orderable-group.service.spec.js @@ -144,29 +144,6 @@ describe('orderableGroupService', function() { .toEqual('Sun May 08 2022 00:00:00 GMT+0000 (Coordinated Universal Time)'); }); - it('should add option to add missing lot if is allowed', function() { - var group = [this.item1, this.item4], - lots = this.orderableGroupService.lotsOf(group, true); - - expect(lots[0]).toEqual({ - lotCode: 'orderableGroupService.addMissingLot' - }); - - expect(lots[1]).toEqual(this.lot2); - - expect(lots[1].expirationDate.toString()) - .toEqual('Sun Jan 20 2019 00:00:00 GMT+0000 (Coordinated Universal Time)'); - }); - - it('should sort lots by filed expirationDate', function() { - var group = [this.item1, this.item4, this.item5], - lots = this.orderableGroupService.lotsOf(group, true); - - expect(lots).toEqual([{ - lotCode: 'orderableGroupService.addMissingLot' - }, this.lot3, this.lot2, this.lot1]); - }); - it('should return kit only orderableGroups', function() { var item = service.getKitOnlyOrderablegroup(this.orderableGroups);