Added discounts to invoices
Some checks failed
Docker Deploy / build-and-push (push) Has been cancelled
Some checks failed
Docker Deploy / build-and-push (push) Has been cancelled
This commit is contained in:
@@ -27,16 +27,34 @@ export async function recalculateInvoiceTotals(invoiceId: string) {
|
||||
// Note: amounts are in cents
|
||||
const subtotal = items.reduce((acc, item) => acc + item.amount, 0);
|
||||
|
||||
const taxRate = invoice.taxRate || 0;
|
||||
const taxAmount = Math.round(subtotal * (taxRate / 100));
|
||||
// Calculate discount
|
||||
const discountType = invoice.discountType || "percentage";
|
||||
const discountValue = invoice.discountValue || 0;
|
||||
let discountAmount = 0;
|
||||
|
||||
const total = subtotal + taxAmount;
|
||||
if (discountType === "percentage") {
|
||||
discountAmount = Math.round(subtotal * (discountValue / 100));
|
||||
} else {
|
||||
// Fixed amount is assumed to be in cents
|
||||
discountAmount = Math.round(discountValue);
|
||||
}
|
||||
|
||||
// Ensure discount doesn't exceed subtotal
|
||||
discountAmount = Math.max(0, Math.min(discountAmount, subtotal));
|
||||
|
||||
const taxableAmount = subtotal - discountAmount;
|
||||
|
||||
const taxRate = invoice.taxRate || 0;
|
||||
const taxAmount = Math.round(taxableAmount * (taxRate / 100));
|
||||
|
||||
const total = taxableAmount + taxAmount;
|
||||
|
||||
// Update invoice
|
||||
await db
|
||||
.update(invoices)
|
||||
.set({
|
||||
subtotal,
|
||||
discountAmount,
|
||||
taxAmount,
|
||||
total,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user