Skip to content

Commit

Permalink
Gok 348-Inventory - Refactor the "Load Stock" button to initiate the …
Browse files Browse the repository at this point in the history
…"Stock Initials" API (#41)

* Deepthi M|Gok-348|Updated the Loadstock save (Post api call) to create an instance type Initial instead of Receipt.

* Deepthi M|Gok-348|Updated the resolve issue with date picker
  • Loading branch information
deepthi-mantena authored Sep 18, 2023
1 parent 2f09a46 commit 2d7d1bd
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 27 deletions.
6 changes: 4 additions & 2 deletions constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ export const getLocationName = (name) => `Location: ${name}`;
export const inventoryMenu = ["Inventory", "Dispense"];
export const locationCookieName = "bahmni.user.location";
export const activePatients = "Active Patients";
export const successMessage = "Stock Receipt Saved Successfully";
export const failureMessage = "Stock Receipt Failed to Save";
export const successMessage = "Saved Successfully";
export const failureMessage = "Failed to Save";



export const inventoryHeaders = [
{
Expand Down
57 changes: 32 additions & 25 deletions src/inventory/stock-receipt/stock-receipt.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import React, { useEffect, useState } from 'react';
import { useCookies } from 'react-cookie';
import useSWR from 'swr';
import saveReceipt from '../../service/save-receipt';
import saveStockInitial from '../../service/save-initial';
import {
fetcher,
fetcherPost,
Expand All @@ -38,6 +39,7 @@ import {
locationCookieName,
stockReceiptHeaders,
successMessage,

} from '../../../constants';
import styles from './stock-receipt.module.scss';

Expand Down Expand Up @@ -211,6 +213,7 @@ const StockReceipt = (props) => {
setItems('');
};


const updateActualQuantity = (quantity, row, cell) => {
const updatedValue = items.map((item) => {
if (item.id === row.id) {
Expand Down Expand Up @@ -256,25 +259,19 @@ const StockReceipt = (props) => {
return;
}
};
const getTomorrowDate = () => {
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate()+1);
return tomorrow;
};


useEffect(() => {
const saveData = async () => {
try {
const response = await saveReceipt(addDrugItems, outwardNumber, stockRoom.results[0]?.uuid);
const response = await saveStockInitial(addDrugItems, outwardNumber, stockRoom.results[0]?.uuid);
if (response && response.ok) {
setReloadData(true);
setOnSuccesful(true);
setOnSuccesful(true);
} else {
setOnFailure(true);
}
} catch (error) {
console.error('An error occurred during saveReceipt:', error);
console.error('An error occurred during save:', error);
}
};

Expand All @@ -286,6 +283,7 @@ const StockReceipt = (props) => {
const setOnSuccessAndFailure = (status) => {
setOnSuccesful(status);
setOnFailure(status);

};

const handleCloseModal = () => {
Expand Down Expand Up @@ -326,19 +324,27 @@ const StockReceipt = (props) => {
return row ? row.invalid : false;
};

const handleInputChange = (id, field, value) => {
if (field === 'totalQuantity') {
setRows((prevRows) =>
prevRows.map((row) =>
row.id === id ? { ...row, totalQuantity: value, invalid: value <= 0 } : row,
),
);
} else
setRows((prevRows) =>
prevRows.map((row) => (row.id === id ? { ...row, [field]: value } : row)),
);
};

const handleInputChange = (id, field, value) => {
if (field === 'totalQuantity') {
setRows((prevRows) =>
prevRows.map((row) =>
row.id === id ? { ...row, totalQuantity: value, invalid: value <= 0 } : row,
),
);
} else if (field === 'expiryDate') {


setRows((prevRows) =>
prevRows.map((row) =>
row.id === id ? { ...row, expiryDate: value } : row,
),
);
} else {
setRows((prevRows) =>
prevRows.map((row) => (row.id === id ? { ...row, [field]: value } : row)),
);
}
};
const filterItems = (menu) => menu?.item?.toLowerCase().includes(menu?.inputValue?.toLowerCase());
return (
<>
Expand All @@ -347,9 +353,9 @@ const StockReceipt = (props) => {
<Column lg={3}>
{onSuccesful &&
ResponseNotification('success', 'Success', successMessage, setOnSuccessAndFailure)}
{onFailure &&
{onFailure &&
ResponseNotification('error', 'Error', failureMessage, setOnSuccessAndFailure)}
</Column>
</Column>
<Row>
<Column sm={8} lg={4}>
<TextInput
Expand Down Expand Up @@ -451,7 +457,8 @@ const StockReceipt = (props) => {
id={`expiryDate-${row.id}`}
dateFormat='d/m/Y'
value={row.expiryDate}
minDate={getTomorrowDate()+1}
minDate={new Date(new Date().setDate(new Date().getDate() + 1)).toLocaleDateString('en-GB')}

onChange={(date) =>
handleInputChange(row.id, 'expiryDate', date[0])
}
Expand Down
45 changes: 45 additions & 0 deletions src/service/save-initial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
postRequest,
getRequest,
stockOperationURL,
stockOperationTypeURL,
} from '../utils/api-utils';
import { getFormattedDate } from '../utils/date-utils';

const saveStockInitial = async (items, outwardNumber, destinationUuid) => {
const instanceTypeResponse = await getRequest(stockOperationTypeURL('Initial'));
const instanceTypeUuids = instanceTypeResponse.results[0].uuid;
const itemsArray = [];
await Promise.all(
items.map(async (item) => {
const itemName = encodeURIComponent(item.item);
const response = await getRequest(`/openmrs/ws/rest/v2/inventory/item?v=full&q=${itemName}`);
const itemUuid = response.results[0]?.uuid;
// Add the item to the requestBody.items array with all corresponding properties
itemsArray.push({
item: itemUuid,
quantity: item.totalQuantity,
expiration: item.expiration,
batchNumber: item.batchNumber,
calculatedExpiration: true,
});
}),
);

const requestBody = {
status: 'NEW',
attributes: [],
items: itemsArray,
operationNumber: '',
instanceType: instanceTypeUuids,
operationDate: getFormattedDate(),
source: '',
destination: destinationUuid,
institution: '',
department: '',
outwardId: outwardNumber,
};

return postRequest(stockOperationURL, requestBody);
};
export default saveStockInitial;

0 comments on commit 2d7d1bd

Please sign in to comment.