diff --git a/src/controllers/00-stats-controller.ts b/src/controllers/00-stats-controller.ts index 08acfdc..14a287a 100644 --- a/src/controllers/00-stats-controller.ts +++ b/src/controllers/00-stats-controller.ts @@ -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; }, [])