From 2d7d1bd9e84c32207e06735c71c1007ea6a72098 Mon Sep 17 00:00:00 2001 From: deepthi-mantena <96411257+deepthi-mantena@users.noreply.github.com> Date: Mon, 18 Sep 2023 19:32:44 +0530 Subject: [PATCH] Gok 348-Inventory - Refactor the "Load Stock" button to initiate the "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 --- constants.js | 6 +- src/inventory/stock-receipt/stock-receipt.jsx | 57 +++++++++++-------- src/service/save-initial.js | 45 +++++++++++++++ 3 files changed, 81 insertions(+), 27 deletions(-) create mode 100644 src/service/save-initial.js diff --git a/constants.js b/constants.js index 3360c8f..4200725 100644 --- a/constants.js +++ b/constants.js @@ -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 = [ { diff --git a/src/inventory/stock-receipt/stock-receipt.jsx b/src/inventory/stock-receipt/stock-receipt.jsx index 637d3f1..c180ff3 100644 --- a/src/inventory/stock-receipt/stock-receipt.jsx +++ b/src/inventory/stock-receipt/stock-receipt.jsx @@ -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, @@ -38,6 +39,7 @@ import { locationCookieName, stockReceiptHeaders, successMessage, + } from '../../../constants'; import styles from './stock-receipt.module.scss'; @@ -211,6 +213,7 @@ const StockReceipt = (props) => { setItems(''); }; + const updateActualQuantity = (quantity, row, cell) => { const updatedValue = items.map((item) => { if (item.id === row.id) { @@ -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); } }; @@ -286,6 +283,7 @@ const StockReceipt = (props) => { const setOnSuccessAndFailure = (status) => { setOnSuccesful(status); setOnFailure(status); + }; const handleCloseModal = () => { @@ -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 ( <> @@ -347,9 +353,9 @@ const StockReceipt = (props) => { {onSuccesful && ResponseNotification('success', 'Success', successMessage, setOnSuccessAndFailure)} - {onFailure && + {onFailure && ResponseNotification('error', 'Error', failureMessage, setOnSuccessAndFailure)} - + { 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]) } diff --git a/src/service/save-initial.js b/src/service/save-initial.js new file mode 100644 index 0000000..1272a5b --- /dev/null +++ b/src/service/save-initial.js @@ -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;