From 250d69d122ea3f79c4bf597d4bfebeca18f52885 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Mon, 10 Mar 2025 10:25:01 +0700 Subject: [PATCH] fix: wrong stats --- src/controllers/00-stats-controller.ts | 37 +++++++++++++++----------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/controllers/00-stats-controller.ts b/src/controllers/00-stats-controller.ts index 1f32345..8cb5d53 100644 --- a/src/controllers/00-stats-controller.ts +++ b/src/controllers/00-stats-controller.ts @@ -645,9 +645,9 @@ export class StatsController extends Controller { @Get("customer-dept") async reportCustomerDept(@Request() req: RequestWithUser) { let query = prisma.$kysely - .selectFrom("Invoice") + .selectFrom("Quotation") + .leftJoin("Invoice", "Quotation.id", "Invoice.quotationId") .leftJoin("Payment", "Invoice.id", "Payment.invoiceId") - .leftJoin("Quotation", "Quotation.id", "Invoice.quotationId") .leftJoin("CustomerBranch", "CustomerBranch.id", "Quotation.customerBranchId") .leftJoin("Customer", "Customer.id", "CustomerBranch.customerId") .select([ @@ -661,11 +661,12 @@ export class StatsController extends Controller { "CustomerBranch.lastNameEN as customerBranchFirstNameEN", "Customer.customerType", "Quotation.id as quotationId", + "Quotation.code as quotationCode", "Quotation.finalPrice as quotationValue", ]) .select(["Payment.paymentStatus"]) .selectAll(["Invoice"]) - .distinctOn("Invoice.id"); + .distinctOn("Quotation.id"); if (!isSystem(req.user)) { query = query.where(({ eb, exists }) => @@ -713,6 +714,7 @@ export class StatsController extends Controller { } return data as Invoice & { quotationId: string; + quotationCode: string; quotationValue: number; paymentStatus: PaymentStatus; customerBranch: CustomerBranch & { customer: { customerType: CustomerType } }; @@ -725,13 +727,14 @@ export class StatsController extends Controller { paid: number; unpaid: number; customerBranch: CustomerBranch & { customer: { customerType: CustomerType } }; - _quotation: { id: string; value: number }[]; + _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), @@ -744,21 +747,23 @@ export class StatsController extends Controller { paid: item.paymentStatus === "PaymentSuccess" && item.amount ? item.amount : 0, unpaid: quotation.value, }); - } else { - 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; - else exists._quotation.push(quotation); - } - - exists.unpaid = exists._quotation.reduce((a, c) => a + c.value, 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); + return acc; }, []) - .map((v) => ({ ...v, _quotation: undefined })); + .map((v) => { + return { ...v, _quotation: undefined }; + }); } }