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