Skip to content

Commit

Permalink
4834 - Fix calculation and status about expired and at risk lots
Browse files Browse the repository at this point in the history
  • Loading branch information
mbayopanda committed Nov 19, 2020
1 parent 9e6206d commit 4da5bd1
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
1 change: 1 addition & 0 deletions client/src/i18n/en/stock.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"LEGEND" : "Legend",
"LIFETIME" : "Lifetime",
"LOT_LIFETIME" : "Lifetime By Lot",
"CURRENT_QUANTITY" : "Current Quantity",
"RISK" : "Risk",
"RISK_QUANTITY" : "Risk Quantity",
"CMM" : "CMM",
Expand Down
1 change: 1 addition & 0 deletions client/src/i18n/fr/stock.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"LIFETIME" : "Durée de vie",
"LOT_LIFETIME" : "Durée par lot",
"RISK" : "Risque",
"CURRENT_QUANTITY" : "Quantité actuelle",
"RISK_QUANTITY" : "Quantité à risque",
"CMM" : "CMM",
"MSD" : "MSD",
Expand Down
12 changes: 6 additions & 6 deletions server/controllers/stock/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,21 +296,21 @@ async function getLotsDepot(depotUuid, params, finalClause) {
}

const inventoriesWithManagementData = await stockManagementProcess(resultFromProcess);
const inventoriesWithLotsProcessed = await processMultipleLots(inventoriesWithManagementData);
let inventoriesWithLotsProcessed = await processMultipleLots(inventoriesWithManagementData);

if (_status) {
return inventoriesWithLotsProcessed.filter(row => row.status === _status);
inventoriesWithLotsProcessed = inventoriesWithLotsProcessed.filter(row => row.status === _status);
}

// Since the status of a product risking expiry is only defined
// after the comparison with the CMM, reason why the filtering
// is not carried out with an SQL request
if (params.is_expiry_risk === '1') {
return inventoriesWithLotsProcessed.filter(item => (item.S_RISK < 0 && item.lifetime > 0));
if (parseInt(params.is_expiry_risk, 10) === 1) {
inventoriesWithLotsProcessed = inventoriesWithLotsProcessed.filter(item => (item.S_RISK < 0 && item.lifetime > 0));
}

if (params.is_expiry_risk === '0') {
return inventoriesWithLotsProcessed.filter(item => (item.S_RISK >= 0 && item.lifetime > 0));
if (parseInt(params.is_expiry_risk, 10) === 0) {
inventoriesWithLotsProcessed = inventoriesWithLotsProcessed.filter(item => (item.S_RISK >= 0 && item.lifetime > 0));
}

return inventoriesWithLotsProcessed;
Expand Down
28 changes: 19 additions & 9 deletions server/controllers/stock/reports/stock/expiration_report.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const stockCore = require('../../core');
*
*/
async function stockExpirationReport(req, res, next) {
const today = new Date();

try {
const params = { includeEmptyLot : 0, ...req.query };
Expand All @@ -21,7 +22,6 @@ async function stockExpirationReport(req, res, next) {

// set up the report with report manager
const report = new ReportManager(STOCK_EXPIRATION_REPORT_TEMPLATE, req.session, optionReport);

const options = params;

if (req.session.stock_settings.enable_strict_depot_permission) {
Expand All @@ -38,40 +38,50 @@ async function stockExpirationReport(req, res, next) {
// clean off the label if it exists so it doesn't mess up the PDF export
delete options.label;

// define month average and the algo to use
options.monthAverageConsumption = req.session.stock_settings.month_average_consumption;
options.averageConsumptionAlgo = req.session.stock_settings.average_consumption_algo;

// get the lots for this depot
const lots = await stockCore.getLotsDepot(options.depot_uuid, options);

// get the lots that are "at risk"
const risky = lots.filter(lot => lot.IS_IN_RISK_EXPIRATION);
const risky = lots.filter(lot => (lot.S_RISK < 0 && lot.lifetime > 0));

// get expired lots
const expired = lots.filter(lot => (lot.S_RISK <= 0 && lot.expiration_date <= today));

// merge risky and expired
const riskyAndExpiredLots = risky.concat(expired);

// make sure lots are grouped by depot.
const groupedByDepot = _.groupBy(risky, 'depot_uuid');
const groupedByDepot = _.groupBy(riskyAndExpiredLots, 'depot_uuid');

// grand totals
const totals = {
expired : { value : 0, quantity : 0 },
at_risk : { value : 0, quantity : 0 },
};

const today = new Date();

const values = _.map(groupedByDepot, (rows) => {
let total = 0;

rows.forEach(lot => {
lot.value = (lot.mvt_quantity * lot.unit_cost);
total += lot.value;

if (lot.expiration_date < today) {
lot.value = (lot.mvt_quantity * lot.unit_cost);
lot.statusKey = 'STOCK.EXPIRED';
lot.classKey = 'bg-danger text-danger';
totals.expired.value += lot.value;
totals.expired.quantity += lot.mvt_quantity;
total += lot.value;
} else {
lot.quantity_at_risk = (lot.S_RISK_QUANTITY * -1);
lot.value = (lot.quantity_at_risk * lot.unit_cost);
lot.statusKey = 'STOCK.STATUS.IS_IN_RISK_OF_EXPIRATION';
lot.classKey = 'bg-warning text-warning';
totals.at_risk.value += lot.value;
totals.at_risk.quantity += lot.mvt_quantity;
totals.at_risk.quantity += (lot.S_RISK_QUANTITY * -1);
total += lot.value;
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
<th>{{translate 'STOCK.INVENTORY'}}</th>
<th>{{translate 'STOCK.LOT'}}</th>
<th>{{translate 'STOCK.EXPIRATION'}}</th>
<th>{{translate 'STOCK.QUANTITY'}}</th>
<th>{{translate 'STOCK.CMM'}}</th>
<th>{{translate 'STOCK.CURRENT_QUANTITY'}}</th>
<th>{{translate 'STOCK.RISK_QUANTITY'}}</th>
<th>{{translate 'TABLE.COLUMNS.UNIT'}}</th>
<th>{{translate 'REPORT.STOCK.UNIT_COST'}}</th>
<th>{{translate 'FORM.LABELS.VALUE'}}</th>
Expand All @@ -72,7 +74,9 @@
<td>{{lot.text}}</td>
<td>{{lot.label}}</td>
<td>{{date lot.expiration_date}}</td>
<td class="text-right">{{lot.cmms.algo_msh}}</td>
<td class="text-right">{{lot.quantity}}</td>
<td class="text-right">{{lot.quantity_at_risk}}</td>
<td>{{lot.unit_type}}</td>
<td>{{currency lot.unit_cost ../../metadata.enterprise.currency_id}}</td>
<td class="text-right">{{currency lot.value ../../metadata.enterprise.currency_id}}</td>
Expand All @@ -82,7 +86,7 @@
</tbody>
<tfoot>
<tr>
<th colspan="6">{{translate 'TABLE.COLUMNS.TOTAL'}}</th>
<th colspan="8">{{translate 'TABLE.COLUMNS.TOTAL'}}</th>
<th class="text-right">{{currency depot.total ../metadata.enterprise.currency_id}}</th>
<td></td>
</tr>
Expand All @@ -91,7 +95,7 @@
<br />
{{else}}
<table>
<tbody>{{> emptyTable columns=8}}</tbody>
<tbody>{{> emptyTable columns=10}}</tbody>
</table>
{{/each}}
</body>

0 comments on commit 4da5bd1

Please sign in to comment.