diff --git a/src/controllers/00-stats-controller.ts b/src/controllers/00-stats-controller.ts index 668f1e7..afd03df 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: { + include: { 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: { include: { 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: { include: { 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, })); } @@ -444,6 +503,7 @@ export class StatsController extends Controller { select: { agentPrice: true, creditNote: true, + createdAt: true, }, }, }, @@ -487,6 +547,8 @@ export class StatsController extends Controller { const netProfit = creditNote ? 0 : precisionRound(finalPrice - expenses); return { + month: v.quotation.createdAt.getMonth() + 1, + year: v.quotation.createdAt.getFullYear(), income, expenses, netProfit, @@ -494,15 +556,28 @@ export class StatsController extends Controller { }); }); - return data.flat().reduce( - (a, c) => { - a.income += c.income; - a.expenses += c.expenses; - a.netProfit += c.netProfit; - return a; - }, - { income: 0, expenses: 0, netProfit: 0 }, - ); + return data + .flat() + .reduce<{ income: number; expenses: 0; netProfit: 0; dataset: (typeof data)[number] }>( + (a, c) => { + const current = a.dataset.find((v) => v.month === c.month && v.year === c.year); + + if (current) { + current.income += c.income; + current.expenses += c.expenses; + current.netProfit += c.netProfit; + } else { + a.dataset.push(c); + } + + a.income += c.income; + a.expenses += c.expenses; + a.netProfit += c.netProfit; + + return a; + }, + { income: 0, expenses: 0, netProfit: 0, dataset: [] }, + ); } @Get("payment")