update useBill reset value and calculator sum generic function
This commit is contained in:
@@ -58,8 +58,8 @@ export function useBill() {
|
|||||||
title: "New Bill",
|
title: "New Bill",
|
||||||
currency: Currency.INR,
|
currency: Currency.INR,
|
||||||
createdAt: new Date(),
|
createdAt: new Date(),
|
||||||
taxPercent: 18,
|
taxPercent: 0,
|
||||||
taxType: TaxType.GST,
|
taxType: TaxType.None,
|
||||||
items: [],
|
items: [],
|
||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|||||||
@@ -14,12 +14,16 @@ export function computeItem(item: BillItem): ComputedBillItem {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function sumBy<T>(items: T[], getValue: (item: T) => number): number {
|
||||||
|
return items.reduce((sum, currentItem) => sum + getValue(currentItem), 0);
|
||||||
|
}
|
||||||
|
|
||||||
export function computeBill(bill: Bill): ComputedBill {
|
export function computeBill(bill: Bill): ComputedBill {
|
||||||
const computedItems = bill.items.map(computeItem);
|
const computedItems = bill.items.map(computeItem);
|
||||||
|
|
||||||
const subtotal = computedItems.reduce(
|
const subtotal = sumBy(
|
||||||
(sum, item) => sum + item.finalPrice,
|
computedItems,
|
||||||
0,
|
(computedItem) => computedItem.finalPrice,
|
||||||
);
|
);
|
||||||
|
|
||||||
const taxAmount = (subtotal * bill.taxPercent) / 100;
|
const taxAmount = (subtotal * bill.taxPercent) / 100;
|
||||||
|
|||||||
Reference in New Issue
Block a user