Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 37 additions & 40 deletions src/pod-add-discrepancy-modal/add-discrepancy-modal.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
.module('pod-add-discrepancy-modal')
.controller('podAddDiscrepancyModalController', controller);

controller.$inject = ['pointOfDeliveryService', 'rejectionReasons','$filter', 'shipmentType', 'notificationService', 'modalDeferred','discrepancies'];
function controller( pointOfDeliveryService, rejectionReasons, $filter, shipmentType, notificationService, modalDeferred, discrepancies) {
var vm = this;
controller.$inject = ['rejectionReasons', '$filter', 'shipmentType', 'notificationService', 'modalDeferred', 'discrepancies'];

function controller(rejectionReasons, $filter, shipmentType, notificationService, modalDeferred, discrepancies) {
var vm = this;

vm.$onInit = onInit;
vm.currentShipmentType = shipmentType; //Storing Selected ShipmentType
Expand Down Expand Up @@ -85,42 +85,39 @@
}

function confirm (){
if(vm.discrepancies.length!=0){
var rejection = {};
angular.forEach(vm.discrepancies, function(reason){
// Use $filter to find the matching object in rejectionReasons
var reasonDetails = $filter('filter')(vm.rejectionReasons, { name: reason.name }, true);
// If a match is found, build the rejection object
if (reasonDetails.length > 0) {
rejection = {
rejectionReason: angular.copy(reasonDetails[0]),
quantityAffected: reason.quantity,
shipmentType: reason.shipmentType,
comments: reason.comments
}
pointOfDeliveryService.addDiscrepancies(rejection);
vm.discrepancies = [];
rejection = {};
}
});
modalDeferred.resolve();
}
else{
notificationService.error('Add discrepancies before saving them.');
}
}

function populateModalWithCurrentDiscrepancies (currentDiscrepancies){
if(currentDiscrepancies.length!=0){
angular.forEach(currentDiscrepancies, function(reason){
reason.quantity = reason.quantityAffected;
reason.name = reason.rejectionReason.name

});
return currentDiscrepancies;
}
else{
return [];
var resolvedDiscrepancies = [];

angular.forEach(vm.discrepancies, function(reason) {
var reasonDetails = $filter('filter')(vm.rejectionReasons, {
name: reason.name
}, true);

if (reasonDetails.length > 0) {
resolvedDiscrepancies.push({
rejectionReason: angular.copy(reasonDetails[0]),
quantityAffected: reason.quantity,
shipmentType: reason.shipmentType,
comments: reason.comments
});
}
});

modalDeferred.resolve(resolvedDiscrepancies);
}

function populateModalWithCurrentDiscrepancies (currentDiscrepancies){
if(currentDiscrepancies && currentDiscrepancies.length!=0){
return currentDiscrepancies.map(function(reason) {
return {
shipmentType: reason.shipmentType,
name: reason.name || (reason.rejectionReason ? reason.rejectionReason.name : ''),
quantity: angular.isDefined(reason.quantity) ? reason.quantity : reason.quantityAffected,
comments: reason.comments
};
});
}
else{
return [];
}
}

Expand Down
116 changes: 116 additions & 0 deletions src/pod-add-discrepancy-modal/add-discrepancy-modal.controller.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* 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. 
*/

describe('podAddDiscrepancyModalController', function() {

beforeEach(function() {
module('pod-add-discrepancy-modal');

inject(function($injector) {
this.$controller = $injector.get('$controller');
});

this.damagedReason = {
name: 'Damaged',
rejectionReasonCategory: {
code: 'POD'
}
};
this.missingReason = {
name: 'Missing',
rejectionReasonCategory: {
code: 'POD'
}
};
this.nonPODReason = {
name: 'Other',
rejectionReasonCategory: {
code: 'OTHER'
}
};

this.rejectionReasons = {
content: [this.damagedReason, this.missingReason, this.nonPODReason]
};
this.modalDeferred = jasmine.createSpyObj('modalDeferred', ['resolve']);
this.notificationService = jasmine.createSpyObj('notificationService', ['error']);

this.createController = function(discrepancies) {
this.vm = this.$controller('podAddDiscrepancyModalController', {
rejectionReasons: this.rejectionReasons,
shipmentType: 'carton',
notificationService: this.notificationService,
modalDeferred: this.modalDeferred,
discrepancies: discrepancies || []
});
this.vm.$onInit();
};
});

it('should prepopulate existing discrepancies for editing', function() {
this.createController([{
rejectionReason: this.damagedReason,
quantityAffected: 4,
shipmentType: 'carton',
comments: 'Crushed'
}]);

expect(this.vm.discrepancies).toEqual([{
name: 'Damaged',
quantity: 4,
shipmentType: 'carton',
comments: 'Crushed'
}]);
});

it('should add and remove discrepancy rows', function() {
this.createController();
this.vm.selectedDiscrepancy = 'Damaged';

this.vm.addDiscrepancy();
expect(this.vm.discrepancies.length).toEqual(1);
expect(this.vm.discrepancies[0].name).toEqual('Damaged');

this.vm.removeDispency(0);
expect(this.vm.discrepancies).toEqual([]);
});

it('should resolve saved discrepancies in POD backend shape', function() {
this.createController();
this.vm.discrepancies = [{
name: 'Damaged',
quantity: 4,
shipmentType: 'carton',
comments: 'Crushed'
}];

this.vm.confirm();

expect(this.modalDeferred.resolve).toHaveBeenCalledWith([{
rejectionReason: this.damagedReason,
quantityAffected: 4,
shipmentType: 'carton',
comments: 'Crushed'
}]);
});

it('should allow saving an empty discrepancy list', function() {
this.createController();

this.vm.confirm();

expect(this.modalDeferred.resolve).toHaveBeenCalledWith([]);
});
});
1 change: 1 addition & 0 deletions src/point-of-delivery-manage/messages_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"proofOfDeliveryManage.quantityRejected":"Rejected Quantity",
"proofOfDeliveryManage.action":"Discrepancy",
"proofOfDeliveryManage.addDiscrepancy":"Add",
"pointOfDeliveryManage.discrepancies": "Discrepancies",
"proofOfDeliveryManage.cartons": "Cartons",
"pointOfDeliveryManage.containers": "Containers",
"pointOfDeliveryManage.emptyConsignment": "Waybill Quantity and Accepted Quantity cannot be zero. Please enter correct values"
Expand Down
57 changes: 44 additions & 13 deletions src/point-of-delivery-manage/point-of-delivery-manage.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@

vm.homeFacilities = [facility];
vm.validateConsignment = validateConsignment;
vm.addDiscrepancyOnModal = addDiscrepancyOnModal;
vm.getShipmentDiscrepancies = getShipmentDiscrepancies;
vm.hasShipmentDiscrepancies = hasShipmentDiscrepancies;
vm.getDiscrepancySummary = getDiscrepancySummary;

/**
* @ngdoc method
Expand All @@ -68,6 +72,8 @@
vm.supplyingFacilities = facilities;
vm.offline = $stateParams.offline === 'true' || offlineService.isOffline();
vm.POD.referenceNo = $rootScope.referenceNoPOD; // Getting Ref Number from Quality Checks
vm.POD.receivingFacility = facility;
vm.POD.discrepancies = [];
$rootScope.referenceNoPOD = undefined; // Clear Var on Root Scope
if ($stateParams.podId) {
vm.tempPOD = filterShipmentById(podEvents, $stateParams.podId);
Expand All @@ -84,18 +90,44 @@
}
}

vm.addDiscrepancyOnModal = function (shipmentType, currentDiscrepancies) {
pointOfDeliveryService.show(shipmentType, currentDiscrepancies).then(function () {
$stateParams.noReload = true;
draft.$modified = true;
vm.cacheDraft();
//Only reload current state and avoid reloading parent state
$state.go($state.current.name, $stateParams, {
reload: $state.current.name
});
function addDiscrepancyOnModal(shipmentType) {
pointOfDeliveryService.show(shipmentType, vm.POD.discrepancies).then(function (discrepancies) {
vm.POD.discrepancies = discrepancies || [];
});
}

function getShipmentDiscrepancies(shipmentType) {
return (vm.POD.discrepancies || []).filter(function(discrepancy) {
return discrepancy.shipmentType === shipmentType;
});
}

function hasShipmentDiscrepancies(shipmentType) {
return getShipmentDiscrepancies(shipmentType).length > 0;
}

function getDiscrepancySummary(shipmentType) {
var discrepancies = getShipmentDiscrepancies(shipmentType);

if (!discrepancies.length) {
return '';
}

if (discrepancies.length === 1) {
return getDiscrepancyName(discrepancies[0]);
}

return discrepancies.length + ' Discrepancies';
}

function getDiscrepancyName(discrepancy) {
if (discrepancy.name) {
return discrepancy.name;
}

return discrepancy.rejectionReason ? discrepancy.rejectionReason.name : '';
}

/**
* @ngdoc method
* @methodOf point-of-delivery-manage.controller:pointOfDeliveryManageController
Expand All @@ -111,6 +143,7 @@
vm.POD.cartonsQuantityOnWaybill = podObject.cartonsQuantityOnWaybill;
vm.POD.cartonsQuantityAccepted = podObject.cartonsQuantityAccepted;
vm.POD.cartonsQuantityRejected = podObject.cartonsQuantityRejected;
vm.POD.discrepancies = angular.copy(podObject.discrepancies || []);
// vm.POD.containersQuantityOnWayBill = podObject.containersQuantityOnWaybill;
// vm.POD.containersQuantityAccepted = podObject.containersQuantityAccepted;
// vm.POD.containersQuantityRejected = podObject.containersQuantityRejected;
Expand All @@ -128,8 +161,6 @@
*/
vm.buildPayload = function () {

var discrepancyList = pointOfDeliveryService.getDiscrepancies();

var payloadData = {
sourceId: vm.POD.supplyingFacility.id,
destinationId: vm.POD.receivingFacility.id,
Expand All @@ -142,7 +173,7 @@
// containersQuantityOnWaybill: vm.POD ? vm.POD.containersQuantityOnWayBill : null,
// containersQuantityShipped: vm.POD ? (vm.POD.containersQuantityAccepted + vm.POD.containersQuantityRejected) : null,
// containersQuantityAccepted: vm.POD ? vm.POD.containersQuantityAccepted : null,
discrepancies: discrepancyList
discrepancies: vm.POD.discrepancies || []
};
const inputsValid = vm.validatePODinputs(payloadData);
const consignmentValid = validateConsignment(payloadData);
Expand Down Expand Up @@ -261,9 +292,9 @@
*/
vm.clearForm = function () {
vm.POD = {};
vm.POD.discrepancies = [];
vm.discrepancy = [];
vm.proofOfDelivery = {};
pointOfDeliveryService.clearDiscrepancies();
$scope.podManageForm.$setPristine();
$scope.podManageForm.$setUntouched();
};
Expand Down
Loading