Make treasury report periods Jalali-aware with full previous month and custom range.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -3,6 +3,15 @@ import { Cron } from '@nestjs/schedule';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import ExcelJS from 'exceljs';
|
||||
import type { TreasuryPartyBalanceDto } from '@mento/shared';
|
||||
import {
|
||||
addJalaliMonths,
|
||||
dateToJalali,
|
||||
endOfJalaliMonth,
|
||||
formatTehranJalali,
|
||||
jalaliMonthKey,
|
||||
jalaliMonthName,
|
||||
startOfJalaliMonth,
|
||||
} from '../common/jalali';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import type { Actor } from '../common/record-scope';
|
||||
import { invoiceSalesInPeriod } from './invoice-ledger';
|
||||
@@ -22,6 +31,29 @@ function parseDate(iso?: string | null) {
|
||||
const d = new Date(iso);
|
||||
return Number.isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
/** روز محلی از YYYY-MM-DD (بدون جابهجایی UTC) */
|
||||
function parseDay(iso?: string | null) {
|
||||
if (!iso) return null;
|
||||
const match = /^(\d{4})-(\d{2})-(\d{2})/.exec(iso.trim());
|
||||
if (match) {
|
||||
return new Date(Number(match[1]), Number(match[2]) - 1, Number(match[3]));
|
||||
}
|
||||
return parseDate(iso);
|
||||
}
|
||||
function startOfDay(d: Date) {
|
||||
return new Date(d.getFullYear(), d.getMonth(), d.getDate(), 0, 0, 0, 0);
|
||||
}
|
||||
function endOfDay(d: Date) {
|
||||
return new Date(d.getFullYear(), d.getMonth(), d.getDate(), 23, 59, 59, 999);
|
||||
}
|
||||
function faDigits(value: string | number) {
|
||||
return String(value).replace(/\d/g, (d) => '۰۱۲۳۴۵۶۷۸۹'[Number(d)] ?? d);
|
||||
}
|
||||
function jalaliMonthLabel(key: string) {
|
||||
const match = /^(\d{4})-(\d{2})$/.exec(key);
|
||||
if (!match) return key;
|
||||
return `${jalaliMonthName(Number(match[2]))} ${faDigits(match[1])}`;
|
||||
}
|
||||
function payrollStatus(payable: Prisma.Decimal, paid: Prisma.Decimal) {
|
||||
if (paid.lessThanOrEqualTo(0)) return 'open';
|
||||
if (paid.greaterThanOrEqualTo(payable)) return 'settled';
|
||||
@@ -449,15 +481,32 @@ export class TreasuryOpsService {
|
||||
}
|
||||
|
||||
async report(actor: Actor, fromIso?: string, toIso?: string) {
|
||||
const to = parseDate(toIso) ?? new Date();
|
||||
const from = parseDate(fromIso) ?? new Date(to.getFullYear(), to.getMonth(), 1);
|
||||
const span = to.getTime() - from.getTime();
|
||||
const prevTo = new Date(from.getTime() - 1);
|
||||
const prevFrom = new Date(prevTo.getTime() - span);
|
||||
const now = new Date();
|
||||
const jalaliNow = dateToJalali(now);
|
||||
const to = endOfDay(parseDay(toIso) ?? now);
|
||||
const from = startOfDay(
|
||||
parseDay(fromIso) ?? startOfJalaliMonth(jalaliNow.jy, jalaliNow.jm),
|
||||
);
|
||||
if (from.getTime() > to.getTime()) {
|
||||
throw new BadRequestException('بازهٔ تاریخ نامعتبر است.');
|
||||
}
|
||||
// دوره قبل = ماه شمسی کاملِ قبل از ماهی که «از تاریخ» در آن است
|
||||
const fromJalali = dateToJalali(from);
|
||||
const prevMonth = addJalaliMonths(fromJalali.jy, fromJalali.jm, -1);
|
||||
const prevFrom = startOfDay(startOfJalaliMonth(prevMonth.jy, prevMonth.jm));
|
||||
const prevTo = endOfDay(endOfJalaliMonth(prevMonth.jy, prevMonth.jm));
|
||||
const current = await this.period(actor.organizationId, from, to);
|
||||
const previous = await this.period(actor.organizationId, prevFrom, prevTo);
|
||||
const breakdown = await this.breakdown(actor.organizationId);
|
||||
return { from: from.toISOString(), to: to.toISOString(), current, previous, ...breakdown };
|
||||
return {
|
||||
from: from.toISOString(),
|
||||
to: to.toISOString(),
|
||||
previousFrom: prevFrom.toISOString(),
|
||||
previousTo: prevTo.toISOString(),
|
||||
current,
|
||||
previous,
|
||||
...breakdown,
|
||||
};
|
||||
}
|
||||
|
||||
async reportExcel(
|
||||
@@ -469,6 +518,15 @@ export class TreasuryOpsService {
|
||||
const data = await this.report(actor, from, to);
|
||||
const book = new ExcelJS.Workbook();
|
||||
const sheet = book.addWorksheet('گزارش مالی');
|
||||
sheet.addRow([
|
||||
'بازه این دوره',
|
||||
`${formatTehranJalali(new Date(data.from))} تا ${formatTehranJalali(new Date(data.to))}`,
|
||||
]);
|
||||
sheet.addRow([
|
||||
'بازه دوره قبل',
|
||||
`${formatTehranJalali(new Date(data.previousFrom))} تا ${formatTehranJalali(new Date(data.previousTo))}`,
|
||||
]);
|
||||
sheet.addRow([]);
|
||||
sheet.addRow(['شاخص', 'این دوره', 'دوره قبل']);
|
||||
const rows: Array<[string, string, string]> = [
|
||||
['درآمد ثبتشده', data.current.bookedIncome, data.previous.bookedIncome],
|
||||
@@ -591,7 +649,7 @@ export class TreasuryOpsService {
|
||||
]);
|
||||
const payablesByMonth = new Map<string, Prisma.Decimal>();
|
||||
const addMonth = (date: Date, amount: Prisma.Decimal) => {
|
||||
const key = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}`;
|
||||
const key = jalaliMonthKey(date);
|
||||
payablesByMonth.set(key, (payablesByMonth.get(key) ?? new Prisma.Decimal(0)).plus(amount));
|
||||
};
|
||||
for (const doc of docs) {
|
||||
@@ -611,8 +669,17 @@ export class TreasuryOpsService {
|
||||
receivablesByParty.set(name, (receivablesByParty.get(name) ?? new Prisma.Decimal(0)).plus(left));
|
||||
}
|
||||
return {
|
||||
payablesByMonth: [...payablesByMonth.entries()].map(([month, amount]) => ({ month, amount: money(amount) })),
|
||||
receivablesByParty: [...receivablesByParty.entries()].map(([name, amount]) => ({ name, amount: money(amount) })),
|
||||
payablesByMonth: [...payablesByMonth.entries()]
|
||||
.sort((a, b) => b[0].localeCompare(a[0]))
|
||||
.map(([month, amount]) => ({
|
||||
month,
|
||||
label: jalaliMonthLabel(month),
|
||||
amount: money(amount),
|
||||
})),
|
||||
receivablesByParty: [...receivablesByParty.entries()].map(([name, amount]) => ({
|
||||
name,
|
||||
amount: money(amount),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user