From fbd423f84d0a6741b8c1c3fa8765917364a23434 Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Thu, 6 Mar 2025 15:19:27 +0700 Subject: [PATCH] feat: add customer data to report --- src/controllers/00-stats-controller.ts | 77 +++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 9 deletions(-) diff --git a/src/controllers/00-stats-controller.ts b/src/controllers/00-stats-controller.ts index 668f1e7..8cabae2 100644 --- a/src/controllers/00-stats-controller.ts +++ b/src/controllers/00-stats-controller.ts @@ -32,9 +32,19 @@ export class StatsController extends Controller { @Query() endDate?: Date, ) { this.setHeader("Content-Type", "text/csv"); - return json2csv(await this.quotationReport(req, limit, startDate, endDate), { - useDateIso8601Format: true, - }); + return json2csv( + await this.quotationReport(req, limit, startDate, endDate).then((v) => + v.map((v) => ({ + ...v, + customerBranch: { + ...v.customerBranch, + customerType: v.customerBranch.customer.customerType, + customer: undefined, + }, + })), + ), + { useDateIso8601Format: true }, + ); } @Get("quotation") @@ -48,6 +58,10 @@ export class StatsController extends Controller { select: { code: true, quotationStatus: true, + customerBranch: { + select: { customer: true }, + }, + finalPrice: true, createdAt: true, updatedAt: true, }, @@ -63,8 +77,11 @@ export class StatsController extends Controller { document: "quotation", code: v.code, status: v.quotationStatus, + amount: v.finalPrice, createdAt: v.createdAt, updatedAt: v.updatedAt, + + customerBranch: v.customerBranch, })); } @@ -76,9 +93,21 @@ export class StatsController extends Controller { @Query() endDate?: Date, ) { this.setHeader("Content-Type", "text/csv"); - return json2csv(await this.invoiceReport(req, limit, startDate, endDate), { - useDateIso8601Format: true, - }); + return json2csv( + await this.invoiceReport(req, limit, startDate, endDate).then((v) => + v.map((v) => ({ + ...v, + customerBranch: { + ...v.customerBranch, + customerType: v.customerBranch.customer.customerType, + customer: undefined, + }, + })), + ), + { + useDateIso8601Format: true, + }, + ); } @Get("invoice") @@ -91,6 +120,11 @@ export class StatsController extends Controller { const record = await prisma.invoice.findMany({ select: { code: true, + quotation: { + select: { + customerBranch: { select: { customer: true } }, + }, + }, payment: { select: { paymentStatus: true, @@ -116,6 +150,8 @@ export class StatsController extends Controller { status: v.payment?.paymentStatus, amount: v.amount, createdAt: v.createdAt, + + customerBranch: v.quotation.customerBranch, })); } @@ -127,9 +163,21 @@ export class StatsController extends Controller { @Query() endDate?: Date, ) { this.setHeader("Content-Type", "text/csv"); - return json2csv(await this.receiptReport(req, limit, startDate, endDate), { - useDateIso8601Format: true, - }); + return json2csv( + await this.receiptReport(req, limit, startDate, endDate).then((v) => + v.map((v) => ({ + ...v, + customerBranch: { + ...v.customerBranch, + customerType: v.customerBranch.customer.customerType, + customer: undefined, + }, + })), + ), + { + useDateIso8601Format: true, + }, + ); } @Get("receipt") @@ -142,6 +190,14 @@ export class StatsController extends Controller { const record = await prisma.payment.findMany({ select: { code: true, + invoice: { + select: { + quotation: { + select: { customerBranch: { select: { customer: true } } }, + }, + }, + }, + amount: true, paymentStatus: true, createdAt: true, }, @@ -162,8 +218,11 @@ export class StatsController extends Controller { return record.map((v) => ({ document: "receipt", code: v.code, + amount: v.amount, status: v.paymentStatus, createdAt: v.createdAt, + + customerBranch: v.invoice.quotation.customerBranch, })); }