Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gok 366 | Global Property for Inward API #48

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
193 changes: 147 additions & 46 deletions src/inventory/aushada/aushada.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
ButtonSet,
Column,
DataTable,
DatePicker,
DatePickerInput,
Grid,
Loading,
Row,
Expand All @@ -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([]);
Expand All @@ -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) {
Expand All @@ -113,7 +165,7 @@ const Aushada = (props) => {
}, [items]);

useEffect(() => {
if (outwardNumber.length > 0) {
if (outwardNumber.length > 0 || (isDateSelected && instituteIdExists)) {
setIsOutwardNumberDisabled(false);
setIsFetchStockDisabled(false);
} else {
Expand All @@ -125,7 +177,7 @@ const Aushada = (props) => {
setIsOutwardNumberDisabled(true);
setIsFetchStockDisabled(true);
}
}, [items, outwardNumber]);
}, [items, outwardNumber, isDateSelected,instituteIdExists]);

useEffect(() => {
if (onSuccesful) {
Expand Down Expand Up @@ -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);
Expand All @@ -184,6 +237,11 @@ const Aushada = (props) => {
setOnFailure(status);

};

const handleDateChange = (date) => {
setSelectedDate(date[0]);
setIsDateSelected(true);
};

return (
<>
Expand All @@ -196,16 +254,43 @@ const Aushada = (props) => {
ResponseNotification('error', 'Error', failureMessage, setOnSuccessAndFailure)}
</Column>
<Row>
<Column sm={8} lg={4}>
<TextInput
id='stock-receipt'
labelText='Outward Number'
value={outwardNumber}
style={{ width: '80%' }}
onChange={(e) => setOutwardNumber(e.target.value)}
disabled={isOutwardNumberDisabled}
/>
</Column>
{enableEaushadhaInwardApi ? (
<>
<Column sm={8} lg={4}>
<h4 style={{ paddingTop: '1.7rem', fontSize: '1.2rem', color: '#333' }}>
Institute ID: {instituteId}
</h4>
</Column>
<Column sm={8} lg={4}>
<DatePicker
datePickerType='single'
dateFormat='d-m-Y'
value={selectedDate}
onChange={handleDateChange}
>
<DatePickerInput
id='myDatePicker'
labelText='Select a date'
placeholder='dd/mm/yyyy'
pattern='d{1,2}/d{1,2}/d{4}'
disabled={!instituteIdExists}
/>
</DatePicker>
</Column>
</>) : (
<>
<Column sm={8} lg={4}>
<TextInput
id='stock-receipt'
labelText='Outward Number'
value={outwardNumber}
style={{ width: '80%' }}
onChange={(e) => setOutwardNumber(e.target.value)}
disabled={isOutwardNumberDisabled}
/>
</Column>
</>
)}
<Column sm={8} lg={4} style={{ paddingTop: '1.5rem' }}>
<Button
onClick={() => setStockIntakeButtonClick(true)}
Expand Down Expand Up @@ -234,6 +319,15 @@ const Aushada = (props) => {
)}
</div>
)}
{inwardNumberExists && (
<h3 style={{ paddingTop: '1rem' }}>
{ResponseNotification(
'error',
'Error',
'Institute Id with this Date already fetched. Please enter a new institute id and date',setInwardNumberExists
)}
</h3>
)}
{outwardNumberExists && (
<h3 style={{ paddingTop: '1rem' }}>
{ResponseNotification(
Expand Down Expand Up @@ -270,10 +364,17 @@ const Aushada = (props) => {
</TableRow>
</TableHead>
<TableBody>
{rows.map((row) => (
{rows.map((row,index) => (
<>
<TableRow {...getRowProps({ row })} key='stock-fetch'>
{row.cells.map((cell) => {
if(cell.id.includes('serialNo')){
return(
<TableCell key={cell.id}>
{++index}
</TableCell>
)
}
if (
cell.id.includes('totalQuantity') ||
cell.id.includes('quantity')
Expand Down
16 changes: 16 additions & 0 deletions src/inventory/aushada/aushada.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
.stockReceiptTable {
max-height: calc(100vh - 500px);
overflow-y: scroll;
border: 1px solid #ccc;

&::-webkit-scrollbar {
width: 12px;
border-width: 1px;
border-radius: 6px;
}

&::-webkit-scrollbar-thumb {
background-color: darkgray;
border-radius: 6px;
}

&::-webkit-scrollbar-track {
background-color: lightgray;
}
}

.buttonSet {
Expand Down
28 changes: 27 additions & 1 deletion src/inventory/aushada/eaushadha-response-mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,30 @@ export const getCalculatedQuantity = (quantity, unitPack) => {
unitPackQuantity *= element;
});
return parseInt(unitPackQuantity * quantity, 10);
};
};
export const getInwardStockReceiptObj = (response) => {
const stockReceiptArray = [];
response?.forEach((element,index) => {
const dateString = element.exp_Date;
const convertedDate = new Date(dateString.split("/").reverse().join("-"));
const itemName = element.drug_Name.split(/\s+/).join(' ');
const rowObj = {
id: `${element.drug_Id}-${index}}`,
itemId: element.drug_Id,
item: itemName,
supplierName: element.supplier,
batchNumber: element.batch_Number,
expiration: convertedDate
.toLocaleDateString("en-GB")
.split("/")
.join("-"),
quantity: element.quantity_In_Pack,
totalQuantity: element.quantity_In_Units,
unitPack: element.unitPack,
invalid: false,
inwardNo: element.inwardNo,
};
stockReceiptArray.push(rowObj);
})
return stockReceiptArray;
}
Loading
Loading