refactor: calc from invoice only
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 8s

This commit is contained in:
Methapon2001 2025-03-10 14:12:39 +07:00
parent ffabcd6ea0
commit a9de9a6469

View file

@ -646,8 +646,8 @@ export class StatsController extends Controller {
@Get("customer-dept") @Get("customer-dept")
async reportCustomerDept(@Request() req: RequestWithUser) { async reportCustomerDept(@Request() req: RequestWithUser) {
let query = prisma.$kysely let query = prisma.$kysely
.selectFrom("Quotation") .selectFrom("Invoice")
.leftJoin("Invoice", "Quotation.id", "Invoice.quotationId") .leftJoin("Quotation", "Quotation.id", "Invoice.quotationId")
.leftJoin("Payment", "Invoice.id", "Payment.invoiceId") .leftJoin("Payment", "Invoice.id", "Payment.invoiceId")
.leftJoin("CustomerBranch", "CustomerBranch.id", "Quotation.customerBranchId") .leftJoin("CustomerBranch", "CustomerBranch.id", "Quotation.customerBranchId")
.leftJoin("Customer", "Customer.id", "CustomerBranch.customerId") .leftJoin("Customer", "Customer.id", "CustomerBranch.customerId")
@ -702,38 +702,21 @@ export class StatsController extends Controller {
paid: number; paid: number;
unpaid: number; unpaid: number;
customerBranch: CustomerBranch & { customer: { customerType: CustomerType } }; customerBranch: CustomerBranch & { customer: { customerType: CustomerType } };
_quotation: { id: string; code: string; value: number }[];
}[] }[]
>((acc, item) => { >((acc, item) => {
const exists = acc.find((v) => v.customerBranch.id === item.customerBranch.id); const exists = acc.find((v) => v.customerBranch.id === item.customerBranch.id);
const quotation = { if (!item.amount) return acc;
id: item.quotationId,
code: item.quotationCode,
value:
item.quotationValue -
(item.paymentStatus === "PaymentSuccess" && item.amount ? item.amount : 0),
};
if (!exists) { if (!exists) {
return acc.concat({ return acc.concat({
_quotation: [quotation],
customerBranch: item.customerBranch, customerBranch: item.customerBranch,
paid: item.paymentStatus === "PaymentSuccess" && item.amount ? item.amount : 0, paid: item.paymentStatus === "PaymentSuccess" ? item.amount : 0,
unpaid: quotation.value, unpaid: item.paymentStatus !== "PaymentSuccess" ? item.amount : 0,
}); });
} }
const same = exists._quotation.find((v) => v.id === item.quotationId); exists[item.paymentStatus === "PaymentSuccess" ? "paid" : "unpaid"] += item.amount;
if (item.paymentStatus === "PaymentSuccess" && item.amount) {
exists.paid += item.amount;
if (same) same.value -= item.amount;
}
if (!same) exists._quotation.push(quotation);
exists.unpaid = exists._quotation.reduce((a, c) => a + c.value, 0);
return acc; return acc;
}, []) }, [])