-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(stock): improve find modals
Consolidates code related to finding depots and services for the stock exit modal. The logic is all contained in a single file now so that we have uniform error handling between the two different modals.
- Loading branch information
Showing
9 changed files
with
158 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
client/src/modules/stock/exit/modals/RequisitionHelper.service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
angular.module('bhima.services') | ||
.service('RequisitionHelperService', RequisitionHelperService); | ||
|
||
RequisitionHelperService.$inject = ['StockService']; | ||
|
||
/** | ||
* @function RequisitionHelperService | ||
* | ||
* @description | ||
* This function combines shared options from the FindDepot and FindService modals | ||
* into a single file to not repeat code. | ||
* | ||
*/ | ||
function RequisitionHelperService(Stock) { | ||
|
||
const service = this; | ||
|
||
/** | ||
* @function lookupRequisitionByReference | ||
* | ||
* @description | ||
* This function looks up the requisition by the reference and asserts that | ||
* the requisition is available for use. If it passes checks, the requisition | ||
* API is queried again with the balance check. | ||
*/ | ||
function lookupRequisitionByReference(reference) { | ||
return Stock.stockRequisition.read(null, { reference }) | ||
.then(([requisition]) => { | ||
|
||
// check that the requisition actually exists | ||
if (!requisition || !requisition.uuid) { | ||
const err = new Error('REQUISITION.VOUCHER_NOT_FOUND'); | ||
err.label = 'label label-warning'; | ||
throw err; | ||
} | ||
|
||
// check that the requisition is available to use | ||
const statuses = ['done', 'completed', 'excessive']; | ||
if (statuses.includes(requisition.status_key)) { | ||
const err = new Error('REQUISITION.ALREADY_USED'); | ||
err.label = 'label label-success'; | ||
throw err; | ||
} | ||
|
||
// check that the requisition hasn't been canceled | ||
if (requisition.status_key === 'cancelled') { | ||
const err = new Error('REQUISITION.CANCELLED'); | ||
err.label = 'label label-danger'; | ||
throw err; | ||
} | ||
|
||
return Stock.stockRequisition.read(requisition.uuid, { balance : true }); | ||
}); | ||
} | ||
|
||
/** | ||
* @function isRequisitionForDepot | ||
* | ||
* @description | ||
* Checks if the requisition is for the correct depot and sets the appropriate | ||
* error flags if not. | ||
*/ | ||
function isRequisitionForDepot(requisition, depot) { | ||
if (depot.uuid !== requisition.depot_uuid) { | ||
const err = new Error('REQUISITION.NOT_FOR_DEPOT'); | ||
err.label = 'label label-warning'; | ||
throw err; | ||
} | ||
return requisition; | ||
} | ||
|
||
/** | ||
* @function isRequisitionForService | ||
* | ||
* @description | ||
* Checks if the requisition is for the correct service and sets the appropriate | ||
* error flags if not. | ||
*/ | ||
function isRequisitionForService(requisition, reqService) { | ||
if (!reqService || !reqService.uuid) { | ||
const err = new Error('REQUISITION.NOT_FOR_SERVICE'); | ||
err.label = 'label label-warning'; | ||
throw err; | ||
} | ||
return requisition; | ||
} | ||
|
||
// bind functions/properties to service | ||
Object.assign(service, { | ||
lookupRequisitionByReference, | ||
isRequisitionForDepot, | ||
isRequisitionForService, | ||
}); | ||
|
||
return service; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters