Skip to content

Commit

Permalink
fix(stock): only exit expired stock on loss
Browse files Browse the repository at this point in the history
Fixes the stock exit logic to only exit expired stock on loss.

Closes #6570.
  • Loading branch information
jniles committed Apr 26, 2022
1 parent 88a28f5 commit 898c0e8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
17 changes: 10 additions & 7 deletions client/src/modules/stock/StockExitForm.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ function StockExitFormService(
this.cache = AppCache(cacheKey);
this.details = { is_exit : 1 };
this.store = new Store({ identifier : 'uuid', data : [] });
this.allowExpired = true;
this.exitTypePredefined = false;

// this flag will filter out lots from the display that contain expired elements
this.allowExpired = true;

// this variable is private and will contain the stock for the current depot.
this._pool = new Pool('lot_uuid', []);

Expand Down Expand Up @@ -159,11 +161,7 @@ function StockExitFormService(
// get the quantity in stock for this depot
return Depots.getStockQuantityForDate(depotUuid, parameters)
.then(stock => {

const rawStock = this.allowExpired ? stock
: stock.filter(lot => lot.is_asset || !lot.is_expired);

const available = rawStock
const available = stock
.map(item => {
const lot = new Lot(item);

Expand Down Expand Up @@ -195,6 +193,7 @@ function StockExitFormService(
StockExitForm.prototype.listLotsForInventory = function listLotsForInventory(inventoryUuid, lotUuid) {
const available = this._pool.list()
.filter(row => row.inventory_uuid === inventoryUuid)
.filter(row => (this.allowExpired ? true : !row.isExpired(this.details.date)))
.sort((a, b) => a.expiration_date > b.expiration_date);

if (lotUuid) {
Expand All @@ -208,7 +207,11 @@ function StockExitFormService(
};

StockExitForm.prototype.listAvailableInventory = function listAvailableInventory() {
return util.getUniqueBy(this._pool.list(), 'inventory_uuid');
// prefilter by the expiration condition
const available = this._pool.list()
.filter(row => (this.allowExpired ? true : !row.isExpired(this.details.date)));

return util.getUniqueBy(available, 'inventory_uuid');
};

/**
Expand Down
15 changes: 12 additions & 3 deletions client/src/modules/stock/exit/exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ StockExitController.$inject = [
'StockExitFormService', 'StockEntryExitTypeService', 'uiGridConstants', 'GridExportService', 'ShipmentService',
'DepotService', '$timeout', 'BarcodeService',
];

/**
* @class StockExitController
/** @class StockExitController
*
* @description
* This controller is responsible to handle stock exit module.
Expand All @@ -25,6 +23,9 @@ function StockExitController(

vm.stockForm = new StockForm('StockExit');

// set allowExpired to be false
vm.stockForm.setAllowExpired(false);

vm.gridApi = {};
vm.ROW_ERROR_FLAG = bhConstants.grid.ROW_ERROR_FLAG;
vm.DATE_FMT = bhConstants.dates.format;
Expand Down Expand Up @@ -182,6 +183,13 @@ function StockExitController(
break;
}

// only allow expired stock if we are exiting to stock loss
if (exitType.label === 'loss') {
vm.stockForm.setAllowExpired(true);
} else {
vm.stockForm.setAllowExpired(false);
}

vm.validate();
}

Expand All @@ -204,6 +212,7 @@ function StockExitController(
// Handle startups from a shipment
if (params.shipment) {
vm.loading = true;

Shipments.readAll(params.shipment)
.then(shipment => {
vm.shipment = shipment;
Expand Down

0 comments on commit 898c0e8

Please sign in to comment.