update useBill reset value and calculator sum generic function
This commit is contained in:
@@ -58,8 +58,8 @@ export function useBill() {
|
||||
title: "New Bill",
|
||||
currency: Currency.INR,
|
||||
createdAt: new Date(),
|
||||
taxPercent: 18,
|
||||
taxType: TaxType.GST,
|
||||
taxPercent: 0,
|
||||
taxType: TaxType.None,
|
||||
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 {
|
||||
const computedItems = bill.items.map(computeItem);
|
||||
|
||||
const subtotal = computedItems.reduce(
|
||||
(sum, item) => sum + item.finalPrice,
|
||||
0,
|
||||
const subtotal = sumBy(
|
||||
computedItems,
|
||||
(computedItem) => computedItem.finalPrice,
|
||||
);
|
||||
|
||||
const taxAmount = (subtotal * bill.taxPercent) / 100;
|
||||
|
||||
Reference in New Issue
Block a user