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") @Get("customer-dept")
async reportCustomerDept(@Request() req: RequestWithUser) { async reportCustomerDept(@Request() req: RequestWithUser) {
let query = prisma.$kysely let query = prisma.$kysely
.selectFrom("Invoice") .selectFrom("Quotation")
.leftJoin("Invoice", "Quotation.id", "Invoice.quotationId")
.leftJoin("Payment", "Invoice.id", "Payment.invoiceId") .leftJoin("Payment", "Invoice.id", "Payment.invoiceId")
.leftJoin("Quotation", "Quotation.id", "Invoice.quotationId")
.leftJoin("CustomerBranch", "CustomerBranch.id", "Quotation.customerBranchId") .leftJoin("CustomerBranch", "CustomerBranch.id", "Quotation.customerBranchId")
.leftJoin("Customer", "Customer.id", "CustomerBranch.customerId") .leftJoin("Customer", "Customer.id", "CustomerBranch.customerId")
.select([ .select([
@ -661,11 +661,12 @@ export class StatsController extends Controller {
"CustomerBranch.lastNameEN as customerBranchFirstNameEN", "CustomerBranch.lastNameEN as customerBranchFirstNameEN",
"Customer.customerType", "Customer.customerType",
"Quotation.id as quotationId", "Quotation.id as quotationId",
"Quotation.code as quotationCode",
"Quotation.finalPrice as quotationValue", "Quotation.finalPrice as quotationValue",
]) ])
.select(["Payment.paymentStatus"]) .select(["Payment.paymentStatus"])
.selectAll(["Invoice"]) .selectAll(["Invoice"])
.distinctOn("Invoice.id"); .distinctOn("Quotation.id");
if (!isSystem(req.user)) { if (!isSystem(req.user)) {
query = query.where(({ eb, exists }) => query = query.where(({ eb, exists }) =>
@ -713,6 +714,7 @@ export class StatsController extends Controller {
} }
return data as Invoice & { return data as Invoice & {
quotationId: string; quotationId: string;
quotationCode: string;
quotationValue: number; quotationValue: number;
paymentStatus: PaymentStatus; paymentStatus: PaymentStatus;
customerBranch: CustomerBranch & { customer: { customerType: CustomerType } }; customerBranch: CustomerBranch & { customer: { customerType: CustomerType } };
@ -725,13 +727,14 @@ export class StatsController extends Controller {
paid: number; paid: number;
unpaid: number; unpaid: number;
customerBranch: CustomerBranch & { customer: { customerType: CustomerType } }; customerBranch: CustomerBranch & { customer: { customerType: CustomerType } };
_quotation: { id: string; value: number }[]; _quotation: { id: string; code: string; value: number }[];
}[] }[]
>((acc, item) => { >((acc, item) => {
const exists = acc.find((v) => v.customerBranch.id === item.customerBranch.id); const exists = acc.find((v) => v.customerBranch.id === item.customerBranch.id);
const quotation = { const quotation = {
id: item.quotationId, id: item.quotationId,
code: item.quotationCode,
value: value:
item.quotationValue - item.quotationValue -
(item.paymentStatus === "PaymentSuccess" && item.amount ? item.amount : 0), (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, paid: item.paymentStatus === "PaymentSuccess" && item.amount ? item.amount : 0,
unpaid: quotation.value, 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; return acc;
}, []) }, [])
.map((v) => ({ ...v, _quotation: undefined })); .map((v) => {
return { ...v, _quotation: undefined };
});
} }
} }