diff --git a/constants.js b/constants.js index 4200725..92c382a 100644 --- a/constants.js +++ b/constants.js @@ -49,6 +49,7 @@ export const dispenseHeaders = [ ]; export const stockReceiptHeaders = [ + { key: "serialNo", header: "S.No"}, { key: "itemId", header: "Item Id" }, { key: "item", header: "Item" }, { key: "supplierName", header: "Supplier Name" }, diff --git a/src/inventory/aushada/aushada.jsx b/src/inventory/aushada/aushada.jsx index 5afbf57..2dc6e87 100644 --- a/src/inventory/aushada/aushada.jsx +++ b/src/inventory/aushada/aushada.jsx @@ -3,6 +3,8 @@ import { ButtonSet, Column, DataTable, + DatePicker, + DatePickerInput, Grid, Loading, Row, @@ -26,18 +28,24 @@ import { import '../../../index.scss'; import { ResponseNotification } from '../../components/notifications/response-notification'; import { useItemStockContext } from '../../context/item-stock-context'; -import saveReceipt from '../../service/save-receipt'; import { fetcher, fetcherPost, + getLocationAttributes, + getRequest, + globalPropertyUrl, + stockInwardURL, stockReceiptURL, stockRoomURL } from '../../utils/api-utils'; import { getCalculatedQuantity, + getInwardStockReceiptObj, getStockReceiptObj, } from './eaushadha-response-mapper'; import styles from './aushada.module.scss'; +import { convertToDateTimeFormat } from '../../utils/date-utils'; +import { inwardSaveReceipt, saveReceipt } from '../../service/save-receipt'; const Aushada = (props) => { const [items, setItems] = useState([]); @@ -52,53 +60,97 @@ const Aushada = (props) => { const [stockEmptyResonseMessage, setStockEmptyResonseMessage] = useState(false); const [negativeError, setNegativeError] = useState(false); const [outwardNumberExists, setOutwardNumberExists] = useState(false); + const [inwardNumberExists, setInwardNumberExists] = useState(false); const [cookies] = useCookies(); const { setReloadData } = props; const { itemStock} = useItemStockContext(); + const [isDateSelected, setIsDateSelected] = useState(false); + const [selectedDate, setSelectedDate] = useState(''); + const [enableEaushadhaInwardApi, setEnableEaushadhaInwardApi] = useState(''); + const [instituteId, setInstituteId] = useState([]); + const [instituteIdExists, setInstituteIdExists] = useState(false); + const locationUuid = cookies[locationCookieName].uuid; const { data: stockRoom, error: stockRoomError } = useSWR( stockRoomURL(cookies[locationCookieName]?.name.trim()), fetcher, ); + useEffect(async ()=>{ + const response=await getRequest(globalPropertyUrl('eAushadha.inward')) + if(response==true){ + setEnableEaushadhaInwardApi(true); + } + const fetchInstituteIdByLocation = async () => { + const response = await getRequest(getLocationAttributes(locationUuid)); + response?.results?.forEach((result) => { + if (result?.attributeType?.display === "Institute Id" && result?.value) { + setInstituteId(result?.value); + setInstituteIdExists(true) + return; + } + else{ + setInstituteId("Contact Admin to set Institute Id") + } + }); + }; + + fetchInstituteIdByLocation(); + },[enableEaushadhaInwardApi]) + useEffect(() => { if (stockIntakeButtonClick) { - let outwardMatch = false; - for (const result of itemStock) { - for(const stockOperation of result.details){ - if (stockOperation.batchOperation.outwardId === outwardNumber) { - outwardMatch = true; - break; + let outwardMatch = false; + let inwardNumberMatch = false; + + for (const result of itemStock) { + for (const stockOperation of result.details) { + const formattedDate = convertToDateTimeFormat(selectedDate); + if (stockOperation.batchOperation.inwardDate === formattedDate) { + inwardNumberMatch = true; + break; + } + if (stockOperation.batchOperation.outwardId === outwardNumber) { + outwardMatch = true; + break; + } } - } - if(outwardMatch){ - break; - } + if (inwardNumberMatch || outwardMatch) break; } - if(outwardMatch){ - setOutwardNumberExists(true); - setStockIntakeButtonClick(false); - } - else{ - setOutwardNumberExists(false); - const fetchData = async () => { - try { - const response = await fetcherPost(stockReceiptURL(), { ouid: outwardNumber }); - if (response) { - setItems(getStockReceiptObj(response)); - setReceivedResponse(response); - setStockEmptyResonseMessage(response.length === 0); - } - } catch (error) { - setStockReceiptError(error); - } - setStockIntakeButtonClick(false); - }; - fetchData(); + setInwardNumberExists(inwardNumberMatch); + setOutwardNumberExists(outwardMatch); + setStockIntakeButtonClick(false); + + if(!inwardNumberMatch && !outwardMatch) + { + const fetchData = async () => { + try { + let url, requestBody; + + if (enableEaushadhaInwardApi) { + url = stockInwardURL(); + const formattedDate= convertToDateTimeFormat(selectedDate); + requestBody = { "inwardDate":formattedDate, "instituteId":instituteId}; + } else { + url = stockReceiptURL(); + requestBody = { ouid: outwardNumber }; + } + const response = await fetcherPost(url, requestBody); + if (response) { + setItems(enableEaushadhaInwardApi ? getInwardStockReceiptObj(response) : getStockReceiptObj(response)); + setReceivedResponse(response); + setStockEmptyResonseMessage(response.length === 0); + } + } catch (error) { + setStockReceiptError(error); + } + setStockIntakeButtonClick(false); + }; + fetchData(); } } - }, [stockIntakeButtonClick, outwardNumber]); + }, [stockIntakeButtonClick, outwardNumber,selectedDate,instituteId,items]); useEffect(() => { if (items.length > 0) { @@ -113,7 +165,7 @@ const Aushada = (props) => { }, [items]); useEffect(() => { - if (outwardNumber.length > 0) { + if (outwardNumber.length > 0 || (isDateSelected && instituteIdExists)) { setIsOutwardNumberDisabled(false); setIsFetchStockDisabled(false); } else { @@ -125,7 +177,7 @@ const Aushada = (props) => { setIsOutwardNumberDisabled(true); setIsFetchStockDisabled(true); } - }, [items, outwardNumber]); + }, [items, outwardNumber, isDateSelected,instituteIdExists]); useEffect(() => { if (onSuccesful) { @@ -167,7 +219,8 @@ const Aushada = (props) => { }; const handleSave = async () => { try { - const response = await saveReceipt(items, outwardNumber, stockRoom.results[0]?.uuid); + const formattedDate = convertToDateTimeFormat(selectedDate); + const response = await(enableEaushadhaInwardApi ? inwardSaveReceipt(items, instituteId,formattedDate, stockRoom.results[0]?.uuid) : saveReceipt(items, outwardNumber, stockRoom.results[0]?.uuid)); if (response && response.ok) { setReloadData(true); setOnSuccesful(true); @@ -184,6 +237,11 @@ const Aushada = (props) => { setOnFailure(status); }; + + const handleDateChange = (date) => { + setSelectedDate(date[0]); + setIsDateSelected(true); + }; return ( <> @@ -196,16 +254,43 @@ const Aushada = (props) => { ResponseNotification('error', 'Error', failureMessage, setOnSuccessAndFailure)} - - setOutwardNumber(e.target.value)} - disabled={isOutwardNumberDisabled} - /> - + {enableEaushadhaInwardApi ? ( + <> + +

+ Institute ID: {instituteId} +

+
+ + + + + + ) : ( + <> + + setOutwardNumber(e.target.value)} + disabled={isOutwardNumberDisabled} + /> + + + )}