Add owner reminders for recurring treasury patterns with once-or-daily cadence.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-25 16:35:45 +03:30
co-authored by Cursor
parent c5ff3597db
commit f3f043d4e8
7 changed files with 666 additions and 101 deletions
@@ -1,23 +1,28 @@
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { computed, onMounted, ref } from 'vue';
import axios from 'axios';
import {
Alert as AAlert,
Button as AButton,
Card as ACard,
Checkbox as ACheckbox,
Col as ACol,
Divider as ADivider,
Empty as AEmpty,
Flex as AFlex,
Form as AForm,
FormItem as AFormItem,
Input as AInput,
List as AList,
Modal as AModal,
Row as ARow,
Select as ASelect,
Space as ASpace,
Switch as ASwitch,
Tag as ATag,
Typography,
message,
} from 'ant-design-vue';
import { EditOutlined, PlusOutlined } from '@ant-design/icons-vue';
import { api } from '@/lib/api';
import MoneyInput from '@/components/MoneyInput.vue';
import JalaliDateInput from '@/components/JalaliDateInput.vue';
@@ -25,14 +30,12 @@ import TreasuryListPagination from '@/components/treasury/TreasuryListPagination
import { toman } from './treasury-format';
import { useTreasuryListPaging } from './useTreasuryListPaging';
import { useTreasuryMobile } from './useTreasuryMobile';
import { formatJalaliNumeric } from '@/lib/jalali';
import { formatJalaliNumeric, formatLocalDateInput, toFaDigits } from '@/lib/jalali';
import { parseMoneyNumber } from '@/lib/money';
const { Text: ATypographyText } = Typography;
const isMobile = useTreasuryMobile();
const AListItem = AList.Item;
const AListItemMeta = AList.Item.Meta;
const ASelectOption = ASelect.Option;
type Row = {
id: string;
@@ -40,13 +43,94 @@ type Row = {
kind: string;
amount: string;
interval: string;
startDate: string;
nextDue: string;
isActive: boolean;
reminderDaysBefore: number | null;
reminderRepeatDaily: boolean;
reminderNotifyApp: boolean;
reminderNotifySms: boolean;
};
type FormState = {
title: string;
kind: string;
amount: string;
interval: string;
dueDate: string;
reminderDaysBefore: string;
reminderRepeatDaily: string;
reminderNotifyApp: boolean;
reminderNotifySms: boolean;
isActive: boolean;
};
const REMINDER_DAY_OPTIONS = [
{ value: '', label: 'بدون یادآور' },
{ value: '0', label: 'روز سررسید' },
{ value: '1', label: '۱ روز قبل' },
{ value: '2', label: '۲ روز قبل' },
{ value: '3', label: '۳ روز قبل' },
{ value: '5', label: '۵ روز قبل' },
{ value: '7', label: '۷ روز قبل' },
];
const REMINDER_FREQ_OPTIONS = [
{ value: 'once', label: 'فقط یک‌بار' },
{ value: 'daily', label: 'هر روز تا سررسید' },
];
const INTERVAL_LABEL: Record<string, string> = {
weekly: 'هفتگی',
monthly: 'ماهانه',
yearly: 'سالانه',
};
function emptyForm(): FormState {
return {
title: '',
kind: 'expense',
amount: '',
interval: 'monthly',
dueDate: '',
reminderDaysBefore: '3',
reminderRepeatDaily: 'once',
reminderNotifyApp: true,
reminderNotifySms: true,
isActive: true,
};
}
function reminderToSelect(days: number | null | undefined) {
return days == null ? '' : String(days);
}
function reminderFromSelect(value: string) {
return value === '' ? null : Number(value);
}
function freqToSelect(daily: boolean | undefined) {
return daily === false ? 'once' : 'daily';
}
function freqFromSelect(value: string) {
return value === 'daily';
}
function reminderLabel(days: number | null | undefined, daily: boolean | undefined) {
if (days == null) return 'بدون یادآور';
const when = days === 0 ? 'روز سررسید' : `${toFaDigits(days)} روز قبل`;
if (days === 0) return when;
return daily === false ? `${when} · یک‌بار` : `${when} · هر روز`;
}
const isMobile = useTreasuryMobile();
const rows = ref<Row[]>([]);
const error = ref('');
const ok = ref('');
const saving = ref(false);
const createOpen = ref(false);
const editOpen = ref(false);
const editingId = ref<string | null>(null);
const createForm = ref(emptyForm());
const editForm = ref(emptyForm());
const {
page,
pageSize,
@@ -56,39 +140,125 @@ const {
onPageSizeChange,
showTotal,
} = useTreasuryListPaging(() => rows.value, 'treasury.recurring');
const form = ref({ title: '', kind: 'expense', amount: '', interval: 'monthly', startDate: '' });
const createReminderEnabled = computed(() => createForm.value.reminderDaysBefore !== '');
const editReminderEnabled = computed(() => editForm.value.reminderDaysBefore !== '');
const modalWidth = computed(() => (isMobile.value ? '100%' : 720));
async function load() {
const { data } = await api.get<Row[]>('/treasury/recurring');
rows.value = data;
}
function openCreate() {
error.value = '';
createForm.value = emptyForm();
createOpen.value = true;
}
function openEdit(item: Row) {
error.value = '';
editingId.value = item.id;
editForm.value = {
title: item.title,
kind: item.kind,
amount: String(item.amount ?? ''),
interval: item.interval,
dueDate: formatLocalDateInput(new Date(item.nextDue)),
reminderDaysBefore: reminderToSelect(item.reminderDaysBefore),
reminderRepeatDaily: freqToSelect(item.reminderRepeatDaily),
reminderNotifyApp: item.reminderNotifyApp,
reminderNotifySms: item.reminderNotifySms,
isActive: item.isActive,
};
editOpen.value = true;
}
function reminderPayload(form: FormState) {
const days = reminderFromSelect(form.reminderDaysBefore);
return {
reminderDaysBefore: days,
reminderRepeatDaily: days == null ? true : freqFromSelect(form.reminderRepeatDaily),
reminderNotifyApp: days == null ? false : form.reminderNotifyApp,
reminderNotifySms: days == null ? false : form.reminderNotifySms,
};
}
function onFinishFailed() {
message.warning('لطفاً فیلدهای الزامی را کامل کنید.');
}
async function createItem() {
error.value = '';
ok.value = '';
saving.value = true;
try {
const amount = parseMoneyNumber(String(form.value.amount ?? ''), 0);
if (!createForm.value.title.trim()) {
error.value = 'عنوان الزامی است.';
throw new Error(error.value);
}
const amount = parseMoneyNumber(String(createForm.value.amount ?? ''), 0);
if (amount == null || amount <= 0) {
error.value = 'مبلغ معتبر نیست.';
return;
throw new Error(error.value);
}
await api.post('/treasury/recurring', {
...form.value,
title: createForm.value.title,
kind: createForm.value.kind,
amount,
startDate: form.value.startDate || undefined,
interval: createForm.value.interval,
startDate: createForm.value.dueDate || undefined,
...reminderPayload(createForm.value),
});
form.value = { title: '', kind: 'expense', amount: '', interval: 'monthly', startDate: '' };
ok.value = 'الگو ثبت شد. در سررسید فقط سند باز ساخته می‌شود.';
createOpen.value = false;
createForm.value = emptyForm();
message.success('الگو ثبت شد.');
await load();
} catch (e) {
error.value = axios.isAxiosError(e)
? String((e.response?.data as { message?: string })?.message ?? 'ثبت ناموفق بود.')
: 'ثبت ناموفق بود.';
if (!error.value) {
error.value = axios.isAxiosError(e)
? String((e.response?.data as { message?: string })?.message ?? 'ثبت ناموفق بود.')
: 'ثبت ناموفق بود.';
}
throw e;
} finally {
saving.value = false;
}
}
async function saveEdit() {
if (!editingId.value) return;
error.value = '';
saving.value = true;
try {
if (!editForm.value.title.trim()) {
error.value = 'عنوان الزامی است.';
throw new Error(error.value);
}
const amount = parseMoneyNumber(String(editForm.value.amount ?? ''), 0);
if (amount == null || amount <= 0) {
error.value = 'مبلغ معتبر نیست.';
throw new Error(error.value);
}
await api.patch(`/treasury/recurring/${editingId.value}`, {
title: editForm.value.title,
kind: editForm.value.kind,
amount,
interval: editForm.value.interval,
nextDue: editForm.value.dueDate || undefined,
isActive: editForm.value.isActive,
...reminderPayload(editForm.value),
});
editOpen.value = false;
editingId.value = null;
message.success('الگو ذخیره شد.');
await load();
} catch (e) {
if (!error.value) {
error.value = axios.isAxiosError(e)
? String((e.response?.data as { message?: string })?.message ?? 'ذخیره ناموفق بود.')
: 'ذخیره ناموفق بود.';
}
throw e;
} finally {
saving.value = false;
}
@@ -101,89 +271,252 @@ onMounted(() => {
<template>
<a-flex vertical :gap="16">
<a-alert v-if="error" type="error" show-icon :message="error" />
<a-alert v-if="ok" type="success" show-icon :message="ok" />
<a-alert v-if="error && !createOpen && !editOpen" type="error" show-icon :message="error" />
<a-card :bordered="false">
<a-divider orientation="right">الگوی جدید</a-divider>
<a-form
layout="vertical"
:model="form"
@finish="createItem"
@finishFailed="onFinishFailed"
>
<a-row :gutter="[16, 0]">
<a-col :xs="24" :md="12">
<a-form-item
label="عنوان"
name="title"
:rules="[{ required: true, message: 'عنوان الزامی است' }]"
>
<a-input v-model:value="form.title" />
</a-form-item>
</a-col>
<a-col :xs="24" :md="8">
<a-form-item label="نوع" name="kind" :rules="[{ required: true }]">
<a-select show-search option-filter-prop="label" v-model:value="form.kind" style="width: 100%">
<a-select-option value="expense" label="هزینه">هزینه</a-select-option>
<a-select-option value="income" label="درآمد">درآمد</a-select-option>
</a-select>
</a-form-item>
</a-col>
<a-col :xs="24" :md="8">
<a-form-item
label="مبلغ"
name="amount"
:rules="[{ required: true, message: 'مبلغ الزامی است' }]"
>
<MoneyInput addon-after="تومان" v-model="form.amount" :decimals="0" />
</a-form-item>
</a-col>
<a-col :xs="24" :md="8">
<a-form-item label="دوره" name="interval" :rules="[{ required: true }]">
<a-select show-search option-filter-prop="label" v-model:value="form.interval" style="width: 100%">
<a-select-option value="monthly" label="ماهانه">ماهانه</a-select-option>
<a-select-option value="weekly" label="هفتگی">هفتگی</a-select-option>
<a-select-option value="yearly" label="سالانه">سالانه</a-select-option>
</a-select>
</a-form-item>
</a-col>
<a-col :xs="24" :md="8">
<a-form-item label="شروع" name="startDate">
<JalaliDateInput v-model="form.startDate" />
</a-form-item>
</a-col>
</a-row>
<a-button type="primary" html-type="submit" :loading="saving">ثبت الگو</a-button>
</a-form>
<a-card :bordered="false">
<a-flex justify="space-between" align="center" :gap="12" wrap="wrap" style="margin-bottom: 16px">
<a-typography-text strong>فهرست الگوها</a-typography-text>
<a-button type="primary" @click="openCreate">
<template #icon><PlusOutlined /></template>
الگوی جدید
</a-button>
</a-flex>
<a-divider orientation="right">فهرست الگوها</a-divider>
<a-empty v-if="!rows.length" description="الگویی ثبت نشده است." />
<a-list v-else :item-layout="isMobile ? 'vertical' : 'horizontal'" :data-source="pagedRows">
<template #renderItem="{ item }">
<a-list-item>
<template #actions>
<a-typography-text strong>{{ toman(item.amount) }}</a-typography-text>
<a-empty v-if="!rows.length" description="الگویی ثبت نشده است." />
<a-list v-else :item-layout="isMobile ? 'vertical' : 'horizontal'" :data-source="pagedRows">
<template #renderItem="{ item }">
<a-list-item>
<template #actions>
<a-button type="link" @click="openEdit(item)">
<template #icon><EditOutlined /></template>
ویرایش
</a-button>
</template>
<a-list-item-meta>
<template #title>
<a-flex :gap="8" align="center" wrap="wrap">
<span>{{ item.title }}</span>
<a-tag v-if="!item.isActive" color="default">غیرفعال</a-tag>
</a-flex>
</template>
<a-list-item-meta>
<template #title>{{ item.title }}</template>
<template #description>
{{ item.kind === 'income' ? 'درآمد' : 'هزینه' }}
· {{ item.interval }}
· بعدی {{ formatJalaliNumeric(new Date(item.nextDue)) }}
</template>
</a-list-item-meta>
</a-list-item>
</template>
</a-list>
<TreasuryListPagination
v-model:page="page"
:total="total"
:page-size="pageSize"
:page-size-options="pageSizeOptions"
:show-total="showTotal"
@update:page-size="onPageSizeChange"
/>
</a-card>
<template #description>
<a-flex vertical :gap="4">
<a-typography-text type="secondary">
{{ item.kind === 'income' ? 'درآمد' : 'هزینه' }}
· {{ INTERVAL_LABEL[item.interval] || item.interval }}
· بعدی {{ formatJalaliNumeric(new Date(item.nextDue)) }}
</a-typography-text>
<a-typography-text type="secondary">
{{ reminderLabel(item.reminderDaysBefore, item.reminderRepeatDaily) }}
<template v-if="item.reminderDaysBefore != null">
·
{{
[
item.reminderNotifyApp ? 'ناتیف' : null,
item.reminderNotifySms ? 'پیامک' : null,
]
.filter(Boolean)
.join(' و ') || 'بدون کانال'
}}
</template>
</a-typography-text>
</a-flex>
</template>
</a-list-item-meta>
<div>
<a-typography-text strong>{{ toman(item.amount) }}</a-typography-text>
</div>
</a-list-item>
</template>
</a-list>
<TreasuryListPagination
v-model:page="page"
:total="total"
:page-size="pageSize"
:page-size-options="pageSizeOptions"
:show-total="showTotal"
@update:page-size="onPageSizeChange"
/>
</a-card>
<a-modal
v-model:open="createOpen"
title="الگوی جدید"
ok-text="ثبت الگو"
cancel-text="انصراف"
:confirm-loading="saving"
:width="modalWidth"
:centered="isMobile"
destroy-on-close
@ok="createItem"
>
<a-alert v-if="error && createOpen" type="error" show-icon :message="error" style="margin-bottom: 12px" />
<a-form layout="vertical" :model="createForm" @finish="createItem" @finishFailed="onFinishFailed">
<a-row :gutter="[16, 0]">
<a-col :xs="24" :md="12">
<a-form-item label="عنوان" name="title" :rules="[{ required: true, message: 'عنوان الزامی است' }]">
<a-input v-model:value="createForm.title" />
</a-form-item>
</a-col>
<a-col :xs="24" :sm="12" :md="8">
<a-form-item label="نوع" name="kind" :rules="[{ required: true }]">
<a-select
show-search
option-filter-prop="label"
v-model:value="createForm.kind"
style="width: 100%"
>
<a-select-option value="expense" label="هزینه">هزینه</a-select-option>
<a-select-option value="income" label="درآمد">درآمد</a-select-option>
</a-select>
</a-form-item>
</a-col>
<a-col :xs="24" :sm="12" :md="8">
<a-form-item label="مبلغ" name="amount" :rules="[{ required: true, message: 'مبلغ الزامی است' }]">
<MoneyInput addon-after="تومان" v-model="createForm.amount" :decimals="0" />
</a-form-item>
</a-col>
<a-col :xs="24" :sm="12" :md="8">
<a-form-item label="دوره" name="interval" :rules="[{ required: true }]">
<a-select
show-search
option-filter-prop="label"
v-model:value="createForm.interval"
style="width: 100%"
>
<a-select-option value="monthly" label="ماهانه">ماهانه</a-select-option>
<a-select-option value="weekly" label="هفتگی">هفتگی</a-select-option>
<a-select-option value="yearly" label="سالانه">سالانه</a-select-option>
</a-select>
</a-form-item>
</a-col>
<a-col :xs="24" :sm="12" :md="8">
<a-form-item label="شروع / سررسید بعدی" name="dueDate">
<JalaliDateInput v-model="createForm.dueDate" style="width: 100%" />
</a-form-item>
</a-col>
<a-col :xs="24" :sm="12" :md="8">
<a-form-item label="زمان یادآور">
<a-select
v-model:value="createForm.reminderDaysBefore"
style="width: 100%"
:options="REMINDER_DAY_OPTIONS"
/>
</a-form-item>
</a-col>
<a-col v-if="createReminderEnabled" :xs="24" :sm="12" :md="8">
<a-form-item label="تکرار یادآور">
<a-select
v-model:value="createForm.reminderRepeatDaily"
style="width: 100%"
:options="REMINDER_FREQ_OPTIONS"
/>
</a-form-item>
</a-col>
<a-col v-if="createReminderEnabled" :span="24">
<a-form-item label="کانال یادآور">
<a-space :size="24" wrap>
<a-checkbox v-model:checked="createForm.reminderNotifyApp">ناتیفیکیشن</a-checkbox>
<a-checkbox v-model:checked="createForm.reminderNotifySms">پیامک</a-checkbox>
</a-space>
</a-form-item>
</a-col>
</a-row>
<a-typography-text type="secondary">
مثلاً «۲ روز قبل · فقط یک‌بار»: یک پیام در همان روز؛ «۳ روز قبل · هر روز»: هر روز تا سررسید برای مالک.
</a-typography-text>
</a-form>
</a-modal>
<a-modal
v-model:open="editOpen"
title="ویرایش الگو"
ok-text="ذخیره"
cancel-text="انصراف"
:confirm-loading="saving"
:width="modalWidth"
:centered="isMobile"
destroy-on-close
@ok="saveEdit"
>
<a-alert v-if="error && editOpen" type="error" show-icon :message="error" style="margin-bottom: 12px" />
<a-form layout="vertical" :model="editForm" @finish="saveEdit" @finishFailed="onFinishFailed">
<a-row :gutter="[16, 0]">
<a-col :xs="24" :md="12">
<a-form-item label="عنوان" name="title" :rules="[{ required: true, message: 'عنوان الزامی است' }]">
<a-input v-model:value="editForm.title" />
</a-form-item>
</a-col>
<a-col :xs="24" :sm="12" :md="8">
<a-form-item label="نوع" name="kind" :rules="[{ required: true }]">
<a-select
show-search
option-filter-prop="label"
v-model:value="editForm.kind"
style="width: 100%"
>
<a-select-option value="expense" label="هزینه">هزینه</a-select-option>
<a-select-option value="income" label="درآمد">درآمد</a-select-option>
</a-select>
</a-form-item>
</a-col>
<a-col :xs="24" :sm="12" :md="8">
<a-form-item label="مبلغ" name="amount" :rules="[{ required: true, message: 'مبلغ الزامی است' }]">
<MoneyInput addon-after="تومان" v-model="editForm.amount" :decimals="0" />
</a-form-item>
</a-col>
<a-col :xs="24" :sm="12" :md="8">
<a-form-item label="دوره" name="interval" :rules="[{ required: true }]">
<a-select
show-search
option-filter-prop="label"
v-model:value="editForm.interval"
style="width: 100%"
>
<a-select-option value="monthly" label="ماهانه">ماهانه</a-select-option>
<a-select-option value="weekly" label="هفتگی">هفتگی</a-select-option>
<a-select-option value="yearly" label="سالانه">سالانه</a-select-option>
</a-select>
</a-form-item>
</a-col>
<a-col :xs="24" :sm="12" :md="8">
<a-form-item label="سررسید بعدی" name="dueDate">
<JalaliDateInput v-model="editForm.dueDate" style="width: 100%" />
</a-form-item>
</a-col>
<a-col :xs="24" :sm="12" :md="8">
<a-form-item label="زمان یادآور">
<a-select
v-model:value="editForm.reminderDaysBefore"
style="width: 100%"
:options="REMINDER_DAY_OPTIONS"
/>
</a-form-item>
</a-col>
<a-col v-if="editReminderEnabled" :xs="24" :sm="12" :md="8">
<a-form-item label="تکرار یادآور">
<a-select
v-model:value="editForm.reminderRepeatDaily"
style="width: 100%"
:options="REMINDER_FREQ_OPTIONS"
/>
</a-form-item>
</a-col>
<a-col v-if="editReminderEnabled" :span="24">
<a-form-item label="کانال یادآور">
<a-space :size="24" wrap>
<a-checkbox v-model:checked="editForm.reminderNotifyApp">ناتیفیکیشن</a-checkbox>
<a-checkbox v-model:checked="editForm.reminderNotifySms">پیامک</a-checkbox>
</a-space>
</a-form-item>
</a-col>
<a-col :span="24">
<a-form-item label="فعال">
<a-switch v-model:checked="editForm.isActive" />
</a-form-item>
</a-col>
</a-row>
</a-form>
</a-modal>
</a-flex>
</template>