fix: wrong stats
All checks were successful
Spell Check / Spell Check with Typos (push) Successful in 8s

This commit is contained in:
Methapon2001 2025-03-10 10:25:01 +07:00
parent 5f499eeda5
commit 250d69d122

View file

@ -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 };
});
}
}