48 lines
1.0 KiB
TypeScript
48 lines
1.0 KiB
TypeScript
import type { Bill, BillItem, ComputedBill, ComputedBillItem } from "../types";
|
|
|
|
export function findById<T extends { id: string }>(
|
|
items: T[],
|
|
id: string,
|
|
): T | undefined {
|
|
return items.find((item) => item.id === id);
|
|
}
|
|
|
|
export function computeItem(item: BillItem): ComputedBillItem {
|
|
return {
|
|
...item,
|
|
finalPrice: item.unitPrice * item.quantity,
|
|
};
|
|
}
|
|
|
|
export function computeBill(bill: Bill): ComputedBill {
|
|
const computedItems = bill.items.map(computeItem);
|
|
|
|
const subtotal = computedItems.reduce(
|
|
(sum, item) => sum + item.finalPrice,
|
|
0,
|
|
);
|
|
|
|
const taxAmount = (subtotal * bill.taxPercent) / 100;
|
|
const total = subtotal + taxAmount;
|
|
|
|
return {
|
|
...bill,
|
|
items: computedItems,
|
|
subtotal,
|
|
taxAmount,
|
|
total,
|
|
};
|
|
}
|
|
|
|
export function formatCurrency(amount: number, currency: string): string {
|
|
return new Intl.NumberFormat("en-IN", {
|
|
style: "currency",
|
|
currency,
|
|
minimumFractionDigits: 2,
|
|
}).format(amount);
|
|
}
|
|
|
|
export function generateId(): string {
|
|
return Math.random().toString(36).slice(2, 9);
|
|
}
|