Skip to content

Commit

Permalink
O3-2887 Duplicate Bill items on Billing form (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
ODORA0 authored Mar 6, 2024
1 parent 1594307 commit d38642d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 19 deletions.
60 changes: 41 additions & 19 deletions src/billing-form/billing-form.component.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useRef, useEffect } from 'react';
import React, { useState, useEffect } from 'react';
import {
ButtonSet,
Button,
Expand All @@ -19,6 +19,7 @@ import { useFetchSearchResults, processBillItems } from '../billing.resource';
import { mutate } from 'swr';
import { convertToCurrency } from '../helpers';
import { z } from 'zod';
import { TrashCan } from '@carbon/react/icons';

type BillingFormProps = {
patientUuid: string;
Expand All @@ -35,6 +36,7 @@ const BillingForm: React.FC<BillingFormProps> = ({ patientUuid, closeWorkspace }
const [searchVal, setSearchVal] = useState('');
const [category, setCategory] = useState('');
const [saveDisabled, setSaveDisabled] = useState<boolean>(false);
const [addedItems, setAddedItems] = useState([]);

const toggleSearch = (choiceSelected) => {
(document.getElementById('searchField') as HTMLInputElement).disabled = false;
Expand Down Expand Up @@ -95,11 +97,24 @@ const BillingForm: React.FC<BillingFormProps> = ({ patientUuid, closeWorkspace }
const updatedItems = [...billItems, newItem];
setBillItems(updatedItems);

setAddedItems([...addedItems, newItem]);

setSearchOptions([]);
calculateTotalAfterAddBillItem(updatedItems);
(document.getElementById('searchField') as HTMLInputElement).value = '';
};

const removeItemFromBill = (uuid) => {
const updatedItems = billItems.filter((item) => item.uuid !== uuid);
setBillItems(updatedItems);

// Update the list of added items
setAddedItems(addedItems.filter((item) => item.uuid !== uuid));

const updatedGrandTotal = updatedItems.reduce((acc, item) => acc + item.Total, 0);
setGrandTotal(updatedGrandTotal);
};

const { data, error, isLoading, isValidating } = useFetchSearchResults(searchVal, category);

const filterItems = (val) => {
Expand All @@ -109,25 +124,28 @@ const BillingForm: React.FC<BillingFormProps> = ({ patientUuid, closeWorkspace }
const res = data as { results: any[] };

const options = res.results.map((o) => {
if (o.commonName && o.commonName !== '') {
return {
uuid: o.uuid || '',
Item: o.commonName,
Qnty: 1,
Price: 10,
Total: 10,
category: 'StockItem',
};
} else if (o.name.toLowerCase().includes(val.toLowerCase())) {
return {
uuid: o.uuid || '',
Item: o.name,
Qnty: 1,
Price: o.servicePrices[0].price,
Total: o.servicePrices[0].price,
category: 'Service',
};
if (!addedItems.some((item) => item.uuid === o.uuid)) {
if (o.commonName && o.commonName !== '') {
return {
uuid: o.uuid || '',
Item: o.commonName,
Qnty: 1,
Price: 10,
Total: 10,
category: 'StockItem',
};
} else if (o.name.toLowerCase().includes(val.toLowerCase())) {
return {
uuid: o.uuid || '',
Item: o.name,
Qnty: 1,
Price: o.servicePrices[0].price,
Total: o.servicePrices[0].price,
category: 'Service',
};
}
}
return null;
});

setSearchOptions(options.filter((option) => option)); // Filter out undefined/null values
Expand Down Expand Up @@ -229,6 +247,7 @@ const BillingForm: React.FC<BillingFormProps> = ({ patientUuid, closeWorkspace }
<TableHeader>Quantity</TableHeader>
<TableHeader>Price</TableHeader>
<TableHeader>Total</TableHeader>
<TableHeader>Action</TableHeader>
</TableRow>
</TableHead>
<TableBody>
Expand All @@ -254,6 +273,9 @@ const BillingForm: React.FC<BillingFormProps> = ({ patientUuid, closeWorkspace }
<TableCell id={row.Item + 'Total'} className="totalValue">
{row.Total}
</TableCell>
<TableCell>
<TrashCan onClick={() => removeItemFromBill(row.uuid)} className={styles.removeButton} />
</TableCell>
</TableRow>
))
) : (
Expand Down
6 changes: 6 additions & 0 deletions src/billing-form/billing-form.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@

.invalidInput {
border: 2px solid red;
}

.removeButton {
color: #ee0909;
right: 20px;
cursor: pointer;
}

0 comments on commit d38642d

Please sign in to comment.