diff --git a/src/openlmis-adjustments/adjustments-modal.controller.js b/src/openlmis-adjustments/adjustments-modal.controller.js index 34a2220a..c37ef66c 100644 --- a/src/openlmis-adjustments/adjustments-modal.controller.js +++ b/src/openlmis-adjustments/adjustments-modal.controller.js @@ -31,12 +31,13 @@ AdjustmentsModalController.$inject = [ '$filter', '$q', 'modalDeferred', 'title', 'message', 'isDisabled', 'adjustments', 'reasons', 'summaries', 'preSave', 'preCancel', 'filterReasons', 'showInDoses', 'lineItem', - 'quantityUnitCalculateService' + 'quantityUnitCalculateService', 'alertService' ]; function AdjustmentsModalController($filter, $q, modalDeferred, title, message, isDisabled, adjustments, reasons, summaries, preSave, preCancel, - filterReasons, showInDoses, lineItem, quantityUnitCalculateService) { + filterReasons, showInDoses, lineItem, quantityUnitCalculateService, + alertService) { var vm = this; @@ -45,6 +46,7 @@ vm.removeAdjustment = removeAdjustment; vm.cancel = cancel; vm.save = save; + vm.hasInvalidAdjustments = hasInvalidAdjustments; /** * @ngdoc property @@ -180,6 +182,11 @@ * Adds adjustment to line item. */ function addAdjustment() { + if (vm.newAdjustment && !hasValidQuantity(vm.newAdjustment)) { + alertService.error('openlmisAdjustments.quantityGreaterThanZero'); + return $q.when(); + } + vm.adjustments.push(vm.newAdjustment); vm.newAdjustment = undefined; @@ -223,6 +230,11 @@ * back to the modal without applying any changes; */ function save() { + if (hasInvalidAdjustments()) { + alertService.error('openlmisAdjustments.quantityGreaterThanZero'); + return; + } + if (preSave) { preSave(vm.adjustments) .then(function() { @@ -250,6 +262,22 @@ modalDeferred.reject(); } } + + function hasInvalidAdjustments() { + return (vm.adjustments || []).some(function(adjustment) { + return !hasValidQuantity(adjustment); + }); + } + + function hasValidQuantity(adjustment) { + return adjustment !== undefined + && adjustment !== null + && adjustment.quantity !== undefined + && adjustment.quantity !== null + && adjustment.quantity !== '' + && !isNaN(adjustment.quantity) + && Number(adjustment.quantity) > 0; + } } })(); diff --git a/src/openlmis-adjustments/adjustments-modal.controller.spec.js b/src/openlmis-adjustments/adjustments-modal.controller.spec.js index c16e5bfe..8a65ff56 100644 --- a/src/openlmis-adjustments/adjustments-modal.controller.spec.js +++ b/src/openlmis-adjustments/adjustments-modal.controller.spec.js @@ -22,6 +22,7 @@ describe('AdjustmentsModalController', function() { this.$q = $injector.get('$q'); this.$rootScope = $injector.get('$rootScope'); this.$controller = $injector.get('$controller'); + this.alertService = $injector.get('alertService'); }); this.modalDeferred = this.$q.defer(); @@ -67,6 +68,7 @@ describe('AdjustmentsModalController', function() { spyOn(this.modalDeferred, 'resolve'); spyOn(this.modalDeferred, 'reject'); + spyOn(this.alertService, 'error'); this.initController = initController; }); @@ -191,6 +193,52 @@ describe('AdjustmentsModalController', function() { expect(this.filterReasons).toHaveBeenCalledWith(this.vm.adjustments); }); + it('should not add adjustment and should show error when quantity is zero', function() { + this.vm.newAdjustment = { + reason: this.vm.reasons[0], + quantity: 0 + }; + + this.vm.addAdjustment(); + + expect(this.vm.adjustments.length).toBe(2); + expect(this.alertService.error) + .toHaveBeenCalledWith('openlmisAdjustments.quantityGreaterThanZero'); + }); + + it('should not add adjustment and should show error when quantity is empty', function() { + this.vm.newAdjustment = { + reason: this.vm.reasons[0], + quantity: '' + }; + + this.vm.addAdjustment(); + + expect(this.vm.adjustments.length).toBe(2); + expect(this.alertService.error) + .toHaveBeenCalledWith('openlmisAdjustments.quantityGreaterThanZero'); + }); + + it('should not add adjustment and should show error when quantity is negative', function() { + this.vm.newAdjustment = { + reason: this.vm.reasons[0], + quantity: -5 + }; + + this.vm.addAdjustment(); + + expect(this.vm.adjustments.length).toBe(2); + expect(this.alertService.error) + .toHaveBeenCalledWith('openlmisAdjustments.quantityGreaterThanZero'); + }); + + it('should not show error when quantity is greater than zero', function() { + this.vm.addAdjustment(); + + expect(this.vm.adjustments.length).toBe(3); + expect(this.alertService.error).not.toHaveBeenCalled(); + }); + }); describe('removeAdjustment', function() { @@ -297,6 +345,33 @@ describe('AdjustmentsModalController', function() { expect(this.modalDeferred.resolve).not.toHaveBeenCalled(); }); + it('should not resolve modalDeferred and should show error if any adjustment has invalid quantity', + function() { + this.adjustments = [{ + reason: this.reasons[0], + quantity: 0 + }]; + + this.initController(); + this.vm.$onInit(); + + this.vm.save(); + + expect(this.modalDeferred.resolve).not.toHaveBeenCalled(); + expect(this.alertService.error) + .toHaveBeenCalledWith('openlmisAdjustments.quantityGreaterThanZero'); + }); + + it('should not show error if all adjustments have valid quantities', function() { + this.initController(); + this.vm.$onInit(); + + this.vm.save(); + + expect(this.alertService.error).not.toHaveBeenCalled(); + expect(this.modalDeferred.resolve).toHaveBeenCalledWith(this.vm.adjustments); + }); + }); describe('cancel', function() { diff --git a/src/openlmis-adjustments/messages_en.json b/src/openlmis-adjustments/messages_en.json index 5649fa3b..d139fbff 100644 --- a/src/openlmis-adjustments/messages_en.json +++ b/src/openlmis-adjustments/messages_en.json @@ -11,5 +11,6 @@ "openlmisAdjustments.total": "Total", "openlmisAdjustments.update": "Update", "openlmisAdjustments.actions": "Actions", - "openlmisAdjustments.close": "Close" + "openlmisAdjustments.close": "Close", + "openlmisAdjustments.quantityGreaterThanZero": "Quantity must be greater than 0" }