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:
2026-11-25 14:40:37 +03:30
co-authored by Cursor
parent db47fa4c0c
commit 6c0995e355
2 changed files with 395 additions and 159 deletions
@@ -5,23 +5,38 @@ import {
Alert as AAlert,
Button as AButton,
Card as ACard,
Col as ACol,
Divider as ADivider,
Empty as AEmpty,
Flex as AFlex,
Form as AForm,
FormItem as AFormItem,
List as AList,
Descriptions as ADescriptions,
DescriptionsItem as ADescriptionsItem,
Row as ARow,
Space as ASpace,
Table as ATable,
Tabs as ATabs,
Typography,
message,
} from 'ant-design-vue';
import type { TableColumnsType } from 'ant-design-vue';
import { DownloadOutlined } from '@ant-design/icons-vue';
import { DownloadOutlined, SearchOutlined } from '@ant-design/icons-vue';
import * as XLSX from 'xlsx';
import type { TreasuryPartyBalanceDto } from '@mento/shared';
import { api } from '@/lib/api';
import JalaliDateInput from '@/components/JalaliDateInput.vue';
import TreasuryListPagination from '@/components/treasury/TreasuryListPagination.vue';
import TreasuryPartyBalanceTable from '@/components/treasury/TreasuryPartyBalanceTable.vue';
import {
dateToJalali,
formatJalaliNumeric,
formatLocalDateInput,
jalaliMonthName,
jalaliToDate,
toFaDigits,
} from '@/lib/jalali';
import { toman } from './treasury-format';
import { useTreasuryListPaging } from './useTreasuryListPaging';
import { useTreasuryMobile } from './useTreasuryMobile';
@@ -51,13 +66,23 @@ type MetricRow = {
previous: string;
};
type PayableMonth = { month: string; label?: string; amount: string };
const todayJalali = dateToJalali(new Date());
const fromDate = ref(formatLocalDateInput(jalaliToDate(todayJalali.jy, todayJalali.jm, 1)));
const toDate = ref(formatLocalDateInput(new Date()));
const rangeFrom = ref('');
const rangeTo = ref('');
const previousFrom = ref('');
const previousTo = ref('');
const current = ref<Period | null>(null);
const previous = ref<Period | null>(null);
const payablesByMonth = ref<Array<{ month: string; amount: string }>>([]);
const payablesByMonth = ref<PayableMonth[]>([]);
const receivablesByParty = ref<Array<{ name: string; amount: string }>>([]);
const debtors = ref<TreasuryPartyBalanceDto[]>([]);
const creditors = ref<TreasuryPartyBalanceDto[]>([]);
const reportTab = ref('periods');
const loading = ref(false);
const error = ref('');
const balanceError = ref('');
@@ -78,11 +103,30 @@ const lines = [
['خالص جریان نقدی', 'cashNet'],
] as const;
const metricColumns: TableColumnsType<MetricRow> = [
const currentRangeLabel = computed(() =>
rangeFrom.value && rangeTo.value
? `${formatJalaliNumeric(new Date(rangeFrom.value))} تا ${formatJalaliNumeric(new Date(rangeTo.value))}`
: '—',
);
const previousRangeLabel = computed(() =>
previousFrom.value && previousTo.value
? `${formatJalaliNumeric(new Date(previousFrom.value))} تا ${formatJalaliNumeric(new Date(previousTo.value))}`
: '—',
);
const metricColumns = computed<TableColumnsType<MetricRow>>(() => [
{ title: 'شاخص', dataIndex: 'label', key: 'label' },
{ title: 'این دوره', dataIndex: 'current', key: 'current' },
{ title: 'دوره قبل', dataIndex: 'previous', key: 'previous' },
];
{
title: `این دوره (${currentRangeLabel.value})`,
dataIndex: 'current',
key: 'current',
},
{
title: `دوره قبل (${previousRangeLabel.value})`,
dataIndex: 'previous',
key: 'previous',
},
]);
const metricRows = computed<MetricRow[]>(() => {
if (!current.value || !previous.value) return [];
@@ -127,17 +171,84 @@ const {
'treasury.reports-expense-cats',
);
function monthTitle(item: PayableMonth) {
if (item.label) return item.label;
const match = /^(\d{4})-(\d{2})$/.exec(item.month);
if (!match) return item.month;
const year = Number(match[1]);
const month = Number(match[2]);
if (year > 1700) {
const j = dateToJalali(new Date(year, month - 1, 15));
return `${jalaliMonthName(j.jm)} ${toFaDigits(j.jy)}`;
}
return `${jalaliMonthName(month)} ${toFaDigits(year)}`;
}
async function load() {
const { data } = await api.get<{
current: Period;
previous: Period;
payablesByMonth: Array<{ month: string; amount: string }>;
receivablesByParty: Array<{ name: string; amount: string }>;
}>('/treasury/reports');
current.value = data.current;
previous.value = data.previous;
payablesByMonth.value = data.payablesByMonth;
receivablesByParty.value = data.receivablesByParty;
loading.value = true;
error.value = '';
try {
const { data } = await api.get<{
from?: string;
to?: string;
previousFrom?: string;
previousTo?: string;
current: Period;
previous: Period;
payablesByMonth: PayableMonth[];
receivablesByParty: Array<{ name: string; amount: string }>;
}>('/treasury/reports', {
params: {
from: fromDate.value || undefined,
to: toDate.value || undefined,
},
});
const fromIso = data.from || `${fromDate.value}T00:00:00`;
const toIso = data.to || `${toDate.value}T23:59:59`;
const fromJ = dateToJalali(new Date(fromIso));
const prevMonth = {
jy: fromJ.jm === 1 ? fromJ.jy - 1 : fromJ.jy,
jm: fromJ.jm === 1 ? 12 : fromJ.jm - 1,
};
const fallbackPrevFrom = jalaliToDate(prevMonth.jy, prevMonth.jm, 1);
const nextOfPrev = jalaliToDate(
prevMonth.jm === 12 ? prevMonth.jy + 1 : prevMonth.jy,
prevMonth.jm === 12 ? 1 : prevMonth.jm + 1,
1,
);
const fallbackPrevTo = new Date(nextOfPrev.getTime() - 1);
rangeFrom.value = fromIso;
rangeTo.value = toIso;
previousFrom.value = data.previousFrom || fallbackPrevFrom.toISOString();
previousTo.value = data.previousTo || fallbackPrevTo.toISOString();
current.value = data.current;
previous.value = data.previous;
payablesByMonth.value = data.payablesByMonth;
receivablesByParty.value = data.receivablesByParty;
} catch {
error.value = 'گزارش بارگذاری نشد.';
} finally {
loading.value = false;
}
}
function applyRange() {
if (!fromDate.value || !toDate.value) {
message.warning('از تاریخ و تا تاریخ را مشخص کنید.');
return;
}
if (fromDate.value > toDate.value) {
message.warning('بازهٔ تاریخ نامعتبر است.');
return;
}
void load();
}
function resetToCurrentJalaliMonth() {
const today = dateToJalali(new Date());
fromDate.value = formatLocalDateInput(jalaliToDate(today.jy, today.jm, 1));
toDate.value = formatLocalDateInput(new Date());
void load();
}
function sumRemaining(rows: TreasuryPartyBalanceDto[]) {
@@ -185,7 +296,13 @@ async function loadBalances() {
}
async function download() {
const { data } = await api.get<Blob>('/treasury/reports/export', { responseType: 'blob' });
const { data } = await api.get<Blob>('/treasury/reports/export', {
responseType: 'blob',
params: {
from: fromDate.value || undefined,
to: toDate.value || undefined,
},
});
const url = URL.createObjectURL(data);
const link = document.createElement('a');
link.href = url;
@@ -195,9 +312,7 @@ async function download() {
}
onMounted(() => {
void load().catch(() => {
error.value = 'گزارش بارگذاری نشد.';
});
void load();
void loadBalances().catch((e: unknown) => {
if (axios.isAxiosError(e) && (e.response?.status === 404 || e.response?.status === 403)) {
debtors.value = [];
@@ -211,152 +326,206 @@ onMounted(() => {
<template>
<a-flex vertical :gap="16">
<a-flex justify="flex-end">
<a-button @click="download">
<template #icon><DownloadOutlined /></template>
خروجی اکسل
</a-button>
</a-flex>
<a-card :bordered="false">
<a-form layout="vertical" @finish="applyRange">
<a-row :gutter="[16, 0]">
<a-col :xs="24" :sm="12" :md="8">
<a-form-item label="از تاریخ">
<JalaliDateInput v-model="fromDate" style="width: 100%" />
</a-form-item>
</a-col>
<a-col :xs="24" :sm="12" :md="8">
<a-form-item label="تا تاریخ">
<JalaliDateInput v-model="toDate" style="width: 100%" />
</a-form-item>
</a-col>
<a-col :xs="24" :md="8">
<a-form-item label=" ">
<a-space :size="8" wrap>
<a-button type="primary" html-type="submit" :loading="loading">
<template #icon><SearchOutlined /></template>
اعمال بازه
</a-button>
<a-button :disabled="loading" @click="resetToCurrentJalaliMonth">
ماه جاری شمسی
</a-button>
<a-button :disabled="loading" @click="download">
<template #icon><DownloadOutlined /></template>
خروجی اکسل
</a-button>
</a-space>
</a-form-item>
</a-col>
</a-row>
</a-form>
<a-typography-text type="secondary">
پیش‌فرض: از اول ماه شمسی جاری تا امروز. «دوره قبل» همیشه ماه شمسی کاملِ قبل (اول تا آخر ماه) است.
</a-typography-text>
</a-card>
<a-alert v-if="error" type="error" show-icon :message="error" />
<a-alert v-if="balanceError" type="error" show-icon :message="balanceError" />
<a-alert v-if="error" type="error" show-icon :message="error" />
<a-alert v-if="balanceError" type="error" show-icon :message="balanceError" />
<a-card :bordered="false">
<a-tabs v-model:activeKey="reportTab">
<a-tab-pane key="periods" tab="مقایسه دوره‌ها">
<a-flex vertical :gap="16">
<a-list v-if="isMobile && metricRows.length" :data-source="metricRows">
<template #renderItem="{ item }">
<a-list-item>
<a-card :title="item.label" style="width: 100%">
<a-descriptions :column="1" bordered>
<a-descriptions-item label="این دوره">{{ item.current }}</a-descriptions-item>
<a-descriptions-item label="دوره قبل">{{ item.previous }}</a-descriptions-item>
</a-descriptions>
</a-card>
</a-list-item>
</template>
</a-list>
<a-table
v-else-if="metricRows.length"
:columns="metricColumns"
:data-source="metricRows"
:pagination="false"
row-key="key"
/>
<a-empty v-else description="داده‌ای برای نمایش نیست." />
<a-card :bordered="false">
<a-tabs v-model:activeKey="reportTab">
<a-tab-pane key="periods" tab="مقایسه دوره‌ها">
<a-flex vertical :gap="16">
<a-descriptions bordered :column="isMobile ? 1 : 2">
<a-descriptions-item label="این دوره">{{ currentRangeLabel }}</a-descriptions-item>
<a-descriptions-item label="دوره قبل">{{ previousRangeLabel }}</a-descriptions-item>
</a-descriptions>
<a-divider orientation="right">مطالبات به تفکیک طرف حساب</a-divider>
<a-empty v-if="!receivablesByParty.length" description="مورد بازی نیست." />
<a-list v-else :item-layout="isMobile ? 'vertical' : 'horizontal'" :data-source="pagedPartyRows">
<template #renderItem="{ item }">
<a-list-item>
<template #actions>
<a-typography-text strong>{{ toman(item.amount) }}</a-typography-text>
<a-list v-if="isMobile && metricRows.length" :data-source="metricRows">
<template #renderItem="{ item }">
<a-list-item>
<a-card :title="item.label" style="width: 100%">
<a-descriptions :column="1" bordered>
<a-descriptions-item :label="`این دوره (${currentRangeLabel})`">
{{ item.current }}
</a-descriptions-item>
<a-descriptions-item :label="`دوره قبل (${previousRangeLabel})`">
{{ item.previous }}
</a-descriptions-item>
</a-descriptions>
</a-card>
</a-list-item>
</template>
<a-list-item-meta>
<template #title>{{ item.name }}</template>
</a-list-item-meta>
</a-list-item>
</template>
</a-list>
<TreasuryListPagination
v-model:page="partyPage"
:total="partyTotal"
:page-size="partyPageSize"
:page-size-options="partyPageSizeOptions"
:show-total="partyShowTotal"
@update:page-size="onPartyPageSizeChange"
/>
</a-list>
<a-table
v-else-if="metricRows.length"
:columns="metricColumns"
:data-source="metricRows"
:pagination="false"
:loading="loading"
row-key="key"
/>
<a-empty v-else description="داده‌ای برای نمایش نیست." />
<a-divider orientation="right">تعهدات به تفکیک ماه</a-divider>
<a-empty v-if="!payablesByMonth.length" description="تعهد بازی نیست." />
<a-list v-else :item-layout="isMobile ? 'vertical' : 'horizontal'" :data-source="pagedMonthRows">
<template #renderItem="{ item }">
<a-list-item>
<template #actions>
<a-typography-text strong>{{ toman(item.amount) }}</a-typography-text>
<a-divider orientation="right">مطالبات به تفکیک طرف حساب</a-divider>
<a-empty v-if="!receivablesByParty.length" description="مورد بازی نیست." />
<a-list
v-else
:item-layout="isMobile ? 'vertical' : 'horizontal'"
:data-source="pagedPartyRows"
>
<template #renderItem="{ item }">
<a-list-item>
<template #actions>
<a-typography-text strong>{{ toman(item.amount) }}</a-typography-text>
</template>
<a-list-item-meta>
<template #title>{{ item.name }}</template>
</a-list-item-meta>
</a-list-item>
</template>
<a-list-item-meta>
<template #title>{{ item.month }}</template>
</a-list-item-meta>
</a-list-item>
</template>
</a-list>
<TreasuryListPagination
v-model:page="monthPage"
:total="monthTotal"
:page-size="monthPageSize"
:page-size-options="monthPageSizeOptions"
:show-total="monthShowTotal"
@update:page-size="onMonthPageSizeChange"
/>
</a-list>
<TreasuryListPagination
v-model:page="partyPage"
:total="partyTotal"
:page-size="partyPageSize"
:page-size-options="partyPageSizeOptions"
:show-total="partyShowTotal"
@update:page-size="onPartyPageSizeChange"
/>
<template v-if="current">
<a-divider orientation="right">سهم هزینه‌ها</a-divider>
<a-empty v-if="!current.expenseCategories.length" description="دسته‌ای نیست." />
<a-list v-else :item-layout="isMobile ? 'vertical' : 'horizontal'" :data-source="pagedExpenseCats">
<template #renderItem="{ item }">
<a-list-item>
<template #actions>
<a-typography-text strong>
{{ toman(item.amount) }} · {{ item.share }}٪
</a-typography-text>
<a-divider orientation="right">تعهدات به تفکیک ماه شمسی</a-divider>
<a-empty v-if="!payablesByMonth.length" description="تعهد بازی نیست." />
<a-list
v-else
:item-layout="isMobile ? 'vertical' : 'horizontal'"
:data-source="pagedMonthRows"
>
<template #renderItem="{ item }">
<a-list-item>
<template #actions>
<a-typography-text strong>{{ toman(item.amount) }}</a-typography-text>
</template>
<a-list-item-meta>
<template #title>{{ monthTitle(item) }}</template>
</a-list-item-meta>
</a-list-item>
</template>
</a-list>
<TreasuryListPagination
v-model:page="monthPage"
:total="monthTotal"
:page-size="monthPageSize"
:page-size-options="monthPageSizeOptions"
:show-total="monthShowTotal"
@update:page-size="onMonthPageSizeChange"
/>
<template v-if="current">
<a-divider orientation="right">سهم هزینه‌ها (این دوره)</a-divider>
<a-empty v-if="!current.expenseCategories.length" description="دسته‌ای نیست." />
<a-list
v-else
:item-layout="isMobile ? 'vertical' : 'horizontal'"
:data-source="pagedExpenseCats"
>
<template #renderItem="{ item }">
<a-list-item>
<template #actions>
<a-typography-text strong>
{{ toman(item.amount) }} · {{ item.share }}٪
</a-typography-text>
</template>
<a-list-item-meta>
<template #title>{{ item.name }}</template>
</a-list-item-meta>
</a-list-item>
</template>
<a-list-item-meta>
<template #title>{{ item.name }}</template>
</a-list-item-meta>
</a-list-item>
</a-list>
<TreasuryListPagination
v-model:page="expenseCatPage"
:total="expenseCatTotal"
:page-size="expenseCatPageSize"
:page-size-options="expenseCatPageSizeOptions"
:show-total="expenseCatShowTotal"
@update:page-size="onExpenseCatPageSizeChange"
/>
</template>
</a-list>
<TreasuryListPagination
v-model:page="expenseCatPage"
:total="expenseCatTotal"
:page-size="expenseCatPageSize"
:page-size-options="expenseCatPageSizeOptions"
:show-total="expenseCatShowTotal"
@update:page-size="onExpenseCatPageSizeChange"
/>
</template>
</a-flex>
</a-tab-pane>
</a-flex>
</a-tab-pane>
<a-tab-pane key="debtors" tab="بدهکاران">
<a-flex vertical :gap="16">
<a-tab-pane key="debtors" tab="بدهکاران">
<a-flex vertical :gap="16">
<a-typography-text type="secondary">
ماندهٔ فاکتور صادرشده و اسناد دریافتِ باز. صدور فاکتور سند درآمد جدا نمی‌سازد.
</a-typography-text>
<a-flex justify="space-between" align="center">
<a-typography-text type="secondary">
ماندهٔ فاکتور صادرشده و اسناد دریافتِ باز. صدور فاکتور سند درآمد جدا نمی‌سازد.
{{ debtors.length }} طرف حساب · جمع مانده {{ toman(sumRemaining(debtors)) }}
</a-typography-text>
<a-flex justify="space-between" align="center">
<a-typography-text type="secondary">
{{ debtors.length }} طرف حساب · جمع مانده {{ toman(sumRemaining(debtors)) }}
</a-typography-text>
<a-button @click="exportParties(debtors, 'بدهکاران', 'دریافت‌شده', 'debtors.xlsx')">
<template #icon><DownloadOutlined /></template>
خروجی اکسل
</a-button>
</a-flex>
<TreasuryPartyBalanceTable :rows="debtors" settled-label="دریافت‌شده" />
<a-button @click="exportParties(debtors, 'بدهکاران', 'دریافت‌شده', 'debtors.xlsx')">
<template #icon><DownloadOutlined /></template>
خروجی اکسل
</a-button>
</a-flex>
</a-tab-pane>
<TreasuryPartyBalanceTable :rows="debtors" settled-label="دریافت‌شده" />
</a-flex>
</a-tab-pane>
<a-tab-pane key="creditors" tab="بستانکاران">
<a-flex vertical :gap="16">
<a-tab-pane key="creditors" tab="بستانکاران">
<a-flex vertical :gap="16">
<a-typography-text type="secondary">
ماندهٔ فاکتور برگشتی و اسناد هزینهٔ باز. برگشتی سند هزینهٔ جدا نمی‌سازد.
</a-typography-text>
<a-flex justify="space-between" align="center">
<a-typography-text type="secondary">
ماندهٔ فاکتور برگشتی و اسناد هزینهٔ باز. برگشتی سند هزینهٔ جدا نمی‌سازد.
{{ creditors.length }} طرف حساب · جمع مانده {{ toman(sumRemaining(creditors)) }}
</a-typography-text>
<a-flex justify="space-between" align="center">
<a-typography-text type="secondary">
{{ creditors.length }} طرف حساب · جمع مانده {{ toman(sumRemaining(creditors)) }}
</a-typography-text>
<a-button @click="exportParties(creditors, 'بستانکاران', 'پرداخت‌شده', 'creditors.xlsx')">
<template #icon><DownloadOutlined /></template>
خروجی اکسل
</a-button>
</a-flex>
<TreasuryPartyBalanceTable :rows="creditors" settled-label="پرداخت‌شده" />
<a-button
@click="exportParties(creditors, 'بستانکاران', 'پرداخت‌شده', 'creditors.xlsx')"
>
<template #icon><DownloadOutlined /></template>
خروجی اکسل
</a-button>
</a-flex>
</a-tab-pane>
</a-tabs>
</a-card>
<TreasuryPartyBalanceTable :rows="creditors" settled-label="پرداخت‌شده" />
</a-flex>
</a-tab-pane>
</a-tabs>
</a-card>
</a-flex>
</template>