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:
@@ -4,12 +4,7 @@ import { invoices, members } from "../../../../db/schema";
|
||||
import { eq, and } from "drizzle-orm";
|
||||
import { recalculateInvoiceTotals } from "../../../../utils/invoice";
|
||||
|
||||
export const POST: APIRoute = async ({
|
||||
request,
|
||||
redirect,
|
||||
locals,
|
||||
params,
|
||||
}) => {
|
||||
export const POST: APIRoute = async ({ request, redirect, locals, params }) => {
|
||||
const user = locals.user;
|
||||
if (!user) {
|
||||
return redirect("/login");
|
||||
@@ -38,8 +33,8 @@ export const POST: APIRoute = async ({
|
||||
.where(
|
||||
and(
|
||||
eq(members.userId, user.id),
|
||||
eq(members.organizationId, invoice.organizationId)
|
||||
)
|
||||
eq(members.organizationId, invoice.organizationId),
|
||||
),
|
||||
)
|
||||
.get();
|
||||
|
||||
@@ -53,6 +48,8 @@ export const POST: APIRoute = async ({
|
||||
const issueDateStr = formData.get("issueDate") as string;
|
||||
const dueDateStr = formData.get("dueDate") as string;
|
||||
const taxRateStr = formData.get("taxRate") as string;
|
||||
const discountType = (formData.get("discountType") as string) || "percentage";
|
||||
const discountValueStr = formData.get("discountValue") as string;
|
||||
const notes = formData.get("notes") as string;
|
||||
|
||||
if (!number || !currency || !issueDateStr || !dueDateStr) {
|
||||
@@ -64,6 +61,11 @@ export const POST: APIRoute = async ({
|
||||
const dueDate = new Date(dueDateStr);
|
||||
const taxRate = taxRateStr ? parseFloat(taxRateStr) : 0;
|
||||
|
||||
let discountValue = discountValueStr ? parseFloat(discountValueStr) : 0;
|
||||
if (discountType === "fixed") {
|
||||
discountValue = Math.round(discountValue * 100);
|
||||
}
|
||||
|
||||
await db
|
||||
.update(invoices)
|
||||
.set({
|
||||
@@ -72,6 +74,8 @@ export const POST: APIRoute = async ({
|
||||
issueDate,
|
||||
dueDate,
|
||||
taxRate,
|
||||
discountType: discountType as "percentage" | "fixed",
|
||||
discountValue,
|
||||
notes: notes || null,
|
||||
})
|
||||
.where(eq(invoices.id, invoiceId));
|
||||
|
||||
Reference in New Issue
Block a user