Skip to content

Commit

Permalink
(Enhancement) Enhance Line Items Billing Status
Browse files Browse the repository at this point in the history
  • Loading branch information
ODORA0 committed Jun 4, 2024
1 parent e4b087f commit 6697a49
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
7 changes: 3 additions & 4 deletions src/invoice/payments/payments.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export type PaymentFormValue = {

const Payments: React.FC<PaymentProps> = ({ bill, selectedLineItems, mutate }) => {
const { t } = useTranslation();
const { defaultCurrency } = useConfig();
const { billableServices, isLoading, isValidating, error } = useBillableServices();
const paymentSchema = z.object({
method: z.string().refine((value) => !!value, 'Payment method is required'),
Expand All @@ -43,6 +42,7 @@ const Payments: React.FC<PaymentProps> = ({ bill, selectedLineItems, mutate }) =

const paymentFormSchema = z.object({ payment: z.array(paymentSchema) });
const { currentVisit } = useVisit(bill?.patientUuid);
const { defaultCurrency } = useConfig();
const methods = useForm<PaymentFormValue>({
mode: 'all',
defaultValues: {},
Expand All @@ -58,7 +58,6 @@ const Payments: React.FC<PaymentProps> = ({ bill, selectedLineItems, mutate }) =
const computedTotal = hasMoreThanOneLineItem ? computeTotalPrice(selectedLineItems) : bill?.totalAmount ?? 0;
const totalAmountTendered = formValues?.reduce((curr: number, prev) => curr + Number(prev.amount) ?? 0, 0) ?? 0;
const amountDue = Number(computedTotal) - (Number(bill?.tenderedAmount) + Number(totalAmountTendered));
const newAmountDue = Number(bill?.totalAmount - bill?.tenderedAmount);

const handleNavigateToBillingDashboard = () =>
navigate({
Expand All @@ -68,7 +67,7 @@ const Payments: React.FC<PaymentProps> = ({ bill, selectedLineItems, mutate }) =
const handleProcessPayment = () => {
const paymentPayload = createPaymentPayload(
bill,
bill.patientUuid,
bill?.patientUuid,
formValues,
amountDue,
billableServices,
Expand Down Expand Up @@ -109,7 +108,7 @@ const Payments: React.FC<PaymentProps> = ({ bill, selectedLineItems, mutate }) =
</CardHeader>
<div>
{bill && <PaymentHistory bill={bill} />}
<PaymentForm disablePayment={computedTotal <= 0} amountDue={amountDue} />
<PaymentForm disablePayment={amountDue <= 0} amountDue={amountDue} />
</div>
</div>
<div className={styles.divider} />
Expand Down
24 changes: 13 additions & 11 deletions src/invoice/payments/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { type OpenmrsResource } from '@openmrs/esm-framework';
import { type LineItem, type MappedBill } from '../../types';
import { type Payment } from './payments.component';

Expand All @@ -20,8 +19,8 @@ export const createPaymentPayload = (
) => {
const { cashier } = bill;
const totalAmount = bill?.totalAmount;
const totalPaymentStatus = amountDue <= 0 ? 'PAID' : 'PENDING';
const previousPayments = bill.payments.map((payment) => ({
const paymentStatus = amountDue <= 0 ? 'PAID' : 'PENDING';
const previousPayments = bill?.payments.map((payment) => ({
amount: payment.amount,
amountTendered: payment.amountTendered,
attributes: [],
Expand All @@ -39,27 +38,29 @@ export const createPaymentPayload = (

const updatedPayments = [...newPayments, ...previousPayments];
const totalAmountRendered = updatedPayments.reduce((acc, payment) => acc + payment.amountTendered, 0);

const updatedLineItems = bill?.lineItems.map((lineItem) => ({
...lineItem,
billableService: getBillableServiceUuid(billableServices, lineItem.billableService),
item: lineItem?.item,
paymentStatus: hasLineItem(selectedLineItems ?? [], lineItem)
? totalAmountRendered >= lineItem.price * lineItem.quantity
? 'PAID'
: 'PENDING'
: lineItem.paymentStatus,
item: processBillItem?.(lineItem),
paymentStatus:
bill?.lineItems.length > 1
? hasLineItem(selectedLineItems ?? [], lineItem) && totalAmountRendered >= lineItem.price * lineItem.quantity
? 'PAID'
: 'PENDING'
: paymentStatus,
}));

const allItemsBillPaymentStatus =
updatedLineItems.filter((item) => item.paymentStatus === 'PENDING').length === 0 ? 'PAID' : 'PENDING';

const processedPayment = {
cashPoint: bill.cashPointUuid,
cashPoint: bill?.cashPointUuid,
cashier: cashier.uuid,
lineItems: updatedLineItems,
payments: [...updatedPayments],
patient: patientUuid,
status: selectedLineItems?.length > 0 ? allItemsBillPaymentStatus : totalPaymentStatus,
status: selectedLineItems?.length > 0 ? allItemsBillPaymentStatus : paymentStatus,
};

return processedPayment;
Expand All @@ -68,3 +69,4 @@ export const createPaymentPayload = (
export const getBillableServiceUuid = (billableServices: Array<any>, serviceName: string) => {
return billableServices.length ? billableServices.find((service) => service.name === serviceName).uuid : null;
};
const processBillItem = (item) => (item.item || item.billableService)?.split(':')[0];

0 comments on commit 6697a49

Please sign in to comment.