Skip to content

Commit

Permalink
wip(stock exit): implement import from requisition
Browse files Browse the repository at this point in the history
This commit implements the "import from requisition" feature for both
services and depots.
  • Loading branch information
jniles committed Mar 4, 2022
1 parent 4378f69 commit 67f87aa
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 24 deletions.
4 changes: 2 additions & 2 deletions client/src/i18n/en/stock.json
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@
"INFO_QUANTITY_IN_STOCK" : "This depot has {{numInventoryItems}} inventory items in {{numLotItems}} lots.",
"WARN_PAST_DATE" : "You have chosen a date in the past. Only the lots available on the chosen date will be visible as well as the quantity on this date.",
"WARN_NOT_CONSUMABLE_INVOICE" : "The invoice does not have any consumable items in it. No lots have been automatically filled.",
"WARN_INSUFFICIENT_QUANTITY" : "There is not enough stock of {{hrText}} to fulfill to requisition. The requisition will be partially filled.",
"WARN_OUT_OF_STOCK_QUANTITY" : "There is no stock of {{hrText}} to fill to requisition. These item(s) have been skipped.",
"WARN_INSUFFICIENT_QUANTITY" : "There is not enough stock of {{hrText}} to fulfill the requisition. The requisition will be partially filled.",
"WARN_OUT_OF_STOCK_QUANTITY" : "There is no stock of {{hrText}} to fill the requisition. These item(s) have been skipped.",
"ERR_NO_DESTINATION" : "No destination is defined for this exit type. Please choose a destination to go with the exit type.",
"ERR_LOT_ERRORS" : "There are errors in the lots grid to be addressed.",
"SUCCESS_FILLED_N_ITEMS" : "Automatically selected {{count}} items from stock to fulfill requisition. Please check the lot numbers to ensure they are correct as you distribute the items.",
Expand Down
28 changes: 22 additions & 6 deletions client/src/modules/stock/StockExitForm.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function StockExitFormService(
* two lines are always present in the form.
*/
StockExitForm.prototype.setup = function setup() {
this._messages.length = 0;
this._messages.clear();

this.details = {
date : new Date(),
Expand Down Expand Up @@ -194,8 +194,12 @@ function StockExitFormService(
*
*/
StockExitForm.prototype.setDepot = function setDepot(depot) {
// clear everything and reload the form.
this.setup();

this.details.depot_uuid = depot.uuid;
this.depot = depot;

return this.fetchQuantityInStock(this.details.depot_uuid, this.details.date);
};

Expand Down Expand Up @@ -327,22 +331,32 @@ function StockExitFormService(
});

function makeUniqueLabels(array) {
return array
const items = array
.map(row => row.text)
.filter((label, index, arr) => arr.indexOf(label) === index)
.sort((a, b) => a.localeCompare(b))
.join(', ');
.sort((a, b) => a.localeCompare(b));

if (items.length > 5) {
const len = items.length - 4;
return [...items.slice(0, 5), `(+${len} ...), `].join(', ');
}

return items.join(', ');
}

// make nice text for error messages
const unavailableLabels = makeUniqueLabels(unavailable);
const insufficientLabels = makeUniqueLabels(insufficient);

// finally, toggle compute the error codes
this._toggleInfoMessage(unavailable.length > 0, 'error', WARN_OUT_OF_STOCK_QUANTITY, { hrText : unavailableLabels });
this._toggleInfoMessage(
unavailable.length > 0, 'error', WARN_OUT_OF_STOCK_QUANTITY, { hrText : unavailableLabels },
);

this._toggleInfoMessage(
insufficient.length > 0, 'warn', WARN_INSUFFICIENT_QUANTITY, { hrText : insufficientLabels },
);

this._toggleInfoMessage(available.length > 0, 'success', SUCCESS_FILLED_N_ITEMS, { count : available.length });
};

Expand Down Expand Up @@ -377,6 +391,7 @@ function StockExitFormService(

if (service.requisition) {
this.details.stock_requisition_uuid = service.requisition.uuid;
this.setLotsFromInventoryList(service.requisition.items, 'inventory_uuid');
}
};

Expand All @@ -393,6 +408,7 @@ function StockExitFormService(

if (depot.requisition) {
this.details.stock_requisition_uuid = depot.requisition.uuid;
this.setLotsFromInventoryList(depot.requisition.items, 'inventory_uuid');
}
};

Expand Down Expand Up @@ -568,7 +584,7 @@ function StockExitFormService(

this._toggleInfoMessage(showNeedsLotsInfoMessage, 'info', INFO_NEEDS_LOTS, this.details);

return hasRequiredDetails
return !!hasRequiredDetails
&& hasValidLots
&& !hasDestinationError;
};
Expand Down
2 changes: 1 addition & 1 deletion client/src/modules/stock/exit/exit.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<bh-dropdown-menu>
<bh-dropdown-menu-item>
<a href>
<bh-change-depot on-select="StockCtrl.stockForm.setDepot(depot)"></bh-change-depot>
<bh-change-depot on-select="StockCtrl.setDepot(depot)"></bh-change-depot>
</a>
</bh-dropdown-menu-item>
</bh-dropdown-menu>
Expand Down
34 changes: 22 additions & 12 deletions client/src/modules/stock/exit/exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ StockExitController.$inject = [
'NotifyService', 'SessionService', 'util',
'bhConstants', 'ReceiptModal', 'StockExitFormService',
'StockModalService', 'uiGridConstants', '$translate',
'GridExportService', 'Store', 'BarcodeService', '$timeout',
'GridExportService', '$timeout',
];

/**
Expand Down Expand Up @@ -121,6 +121,11 @@ function StockExitController(
vm.messages = vm.stockForm.messages();
};

vm.setDepot = function setDepot(depot) {
vm.stockForm.setDepot(depot);
vm.validate();
};

vm.configureItem = function configureItem(row, lot) {
vm.stockForm.configureItem(row, lot);
vm.validate();
Expand Down Expand Up @@ -168,6 +173,10 @@ function StockExitController(
// vm.gridApi.core.notifyDataChange(uiGridConstants.dataChange.ALL);
}

/**
*
*
*/
vm.setDate = function setDate(date) {
vm.stockForm.setDate(date);
vm.validate();
Expand Down Expand Up @@ -240,15 +249,12 @@ function StockExitController(
* on the row.
*
*/
function errorLineHighlight(rowIdx, store) {
function errorLineHighlight(row) {
const { ROW_ERROR_FLAG } = bhConstants.grid;
// set and unset error flag for allowing to highlight again the row
// when the user click again on the submit button
const row = store.data[rowIdx];
row[ROW_ERROR_FLAG] = true;
$timeout(() => {
row[ROW_ERROR_FLAG] = false;
}, 3000);
$timeout(() => { row[ROW_ERROR_FLAG] = false; }, 3000);
}

function submit(form) {
Expand All @@ -257,16 +263,18 @@ function StockExitController(
// run validation
vm.validate();

const isValidForSubmission = vm.stockForm.validate();

// check if the form is valid
if (vm.stockForm.validate() === false) {
if (isValidForSubmission === false) {

let firstElement = true;

vm.stockForm.store.data.forEach((row, idx) => {
vm.stockForm.store.data.forEach(row => {
const hasErrors = row.errors().length > 0;
if (hasErrors) {
// flash the error highlight
errorLineHighlight(idx, this.store);
errorLineHighlight(row);

// scroll to the first invalid item
if (firstElement) {
Expand All @@ -276,15 +284,17 @@ function StockExitController(
}
});

// flash the first error message to the user
const [msg] = vm.stockForm.messages();
Notify.danger(msg.text, 5000);

return null;
}

const renderReceipt = ReceiptModal.getReceiptFnByFluxId(vm.stockForm.details.flux_id);

return vm.stockForm.submit()
.then(result => {
return renderReceipt(result.uuid, true);
})
.then(result => renderReceipt(result.uuid, true))
.then(() => {
vm.stockForm.clear();
vm.validate();
Expand Down
6 changes: 3 additions & 3 deletions server/controllers/stock/requisition/requisition.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async function getDetails(identifier) {
const uuid = identifier;
const sqlRequisition = `${SELECT_QUERY} WHERE sr.uuid = ?;`;
const sqlRequisitionItems = `
SELECT BUID(i.uuid) inventory_uuid, i.code, i.text, it.text as inventoryType, sri.quantity
SELECT BUID(i.uuid) inventory_uuid, i.code, i.consumable, i.text, it.text as inventoryType, sri.quantity
FROM stock_requisition_item sri
JOIN inventory i ON i.uuid = sri.inventory_uuid
JOIN inventory_type it ON i.type_id = it.id
Expand All @@ -55,10 +55,10 @@ async function getDetailsBalance(identifier) {
const sqlRequisition = `${SELECT_QUERY} WHERE sr.uuid = ?;`;

const sql = `
SELECT req.inventory_uuid, req.code, req.text, req.inventoryType,
SELECT req.inventory_uuid, req.code, req.text, req.consumable, req.inventoryType,
(req.quantity - IF(mouv.quantity, mouv.quantity, 0)) AS quantity
FROM (
SELECT BUID(i.uuid) inventory_uuid, i.code, i.text, it.text as inventoryType, sri.quantity
SELECT BUID(i.uuid) inventory_uuid, i.code, i.consumable, i.text, it.text as inventoryType, sri.quantity
FROM stock_requisition_item sri
JOIN inventory i ON i.uuid = sri.inventory_uuid
JOIN inventory_type it ON i.type_id = it.id
Expand Down

0 comments on commit 67f87aa

Please sign in to comment.