This commit is contained in:
@@ -5,7 +5,6 @@ import {
|
||||
invoiceItems,
|
||||
timeEntries,
|
||||
members,
|
||||
timeEntryTags,
|
||||
tags,
|
||||
} from "../../../../db/schema";
|
||||
import {
|
||||
@@ -48,7 +47,11 @@ export const POST: APIRoute = async ({ request, params, locals, redirect }) => {
|
||||
// Set end date to end of day
|
||||
endDate.setHours(23, 59, 59, 999);
|
||||
|
||||
const invoice = await db.select().from(invoices).where(eq(invoices.id, id)).get();
|
||||
const invoice = await db
|
||||
.select()
|
||||
.from(invoices)
|
||||
.where(eq(invoices.id, id))
|
||||
.get();
|
||||
|
||||
if (!invoice) {
|
||||
return new Response("Invoice not found", { status: 404 });
|
||||
@@ -60,8 +63,8 @@ export const POST: APIRoute = async ({ request, params, locals, redirect }) => {
|
||||
.where(
|
||||
and(
|
||||
eq(members.userId, user.id),
|
||||
eq(members.organizationId, invoice.organizationId)
|
||||
)
|
||||
eq(members.organizationId, invoice.organizationId),
|
||||
),
|
||||
)
|
||||
.get();
|
||||
|
||||
@@ -70,7 +73,9 @@ export const POST: APIRoute = async ({ request, params, locals, redirect }) => {
|
||||
}
|
||||
|
||||
if (invoice.status !== "draft") {
|
||||
return new Response("Can only import time into draft invoices", { status: 400 });
|
||||
return new Response("Can only import time into draft invoices", {
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
const entries = await db
|
||||
@@ -79,8 +84,7 @@ export const POST: APIRoute = async ({ request, params, locals, redirect }) => {
|
||||
tag: tags,
|
||||
})
|
||||
.from(timeEntries)
|
||||
.leftJoin(timeEntryTags, eq(timeEntries.id, timeEntryTags.timeEntryId))
|
||||
.leftJoin(tags, eq(timeEntryTags.tagId, tags.id))
|
||||
.leftJoin(tags, eq(timeEntries.tagId, tags.id))
|
||||
.where(
|
||||
and(
|
||||
eq(timeEntries.organizationId, invoice.organizationId),
|
||||
@@ -88,8 +92,8 @@ export const POST: APIRoute = async ({ request, params, locals, redirect }) => {
|
||||
isNull(timeEntries.invoiceId),
|
||||
isNotNull(timeEntries.endTime),
|
||||
gte(timeEntries.startTime, startDate),
|
||||
lte(timeEntries.startTime, endDate)
|
||||
)
|
||||
lte(timeEntries.startTime, endDate),
|
||||
),
|
||||
)
|
||||
.orderBy(desc(timeEntries.startTime));
|
||||
|
||||
@@ -238,16 +242,20 @@ export const POST: APIRoute = async ({ request, params, locals, redirect }) => {
|
||||
|
||||
let discountAmount = 0;
|
||||
if (invoice.discountType === "percentage") {
|
||||
discountAmount = Math.round(subtotal * ((invoice.discountValue || 0) / 100));
|
||||
discountAmount = Math.round(
|
||||
subtotal * ((invoice.discountValue || 0) / 100),
|
||||
);
|
||||
} else {
|
||||
discountAmount = Math.round((invoice.discountValue || 0) * 100);
|
||||
if (invoice.discountValue && invoice.discountValue > 0) {
|
||||
discountAmount = Math.round((invoice.discountValue || 0) * 100);
|
||||
discountAmount = Math.round((invoice.discountValue || 0) * 100);
|
||||
}
|
||||
}
|
||||
|
||||
const taxableAmount = Math.max(0, subtotal - discountAmount);
|
||||
const taxAmount = Math.round(taxableAmount * ((invoice.taxRate || 0) / 100));
|
||||
const taxAmount = Math.round(
|
||||
taxableAmount * ((invoice.taxRate || 0) / 100),
|
||||
);
|
||||
const total = subtotal - discountAmount + taxAmount;
|
||||
|
||||
await db
|
||||
|
||||
Reference in New Issue
Block a user