Phase 6 of the tenant series. GlobalTables::DEFERRED is now empty and the
coverage test asserts it stays that way.
payments carries the (entity_type, entity_id) pair and belongs to the
receiving side, never the payer: an appointment payment takes the
appointment's environment, a subscription takes the environment its buyer
owns, and an SMS wallet top-up takes the wallet's. The patient never chose
an environment, so TenantFilter stays off for them and they still see their
own payment.
Three corrections to the analysis the phase was planned on, each backed by
the code or the data rather than the plan:
- A third payment type exists. Payment::TYPE_SMS_WALLET is created in
SmsWalletController and already carries its environment in the metadata;
without assigning it the write would fail at flush.
- clinic_subscriptions has no user_id, and its trial rows carry no payment,
so it cannot drive the subscription backfill. The environment is derived
the way handleSubscriptionActivation derives it — and that method now
reads the pair off the payment instead of re-deriving it, so a payment and
the subscription it buys can no longer land on different environments.
- WalletTransaction is not a child of Payment. payment_id is nullable and
none of the four creation sites set it; the wallet is a person's, with a
running balance per user. It and Settlement, which withdraws from that same
wallet, are global with a recorded reason instead.
bank_accounts and pos_devices move from the registering user to the
environment. Their pair is deliberately nullable: nothing in the existing
data says which of a multi-environment owner's cards belongs where, and
guessing would point real money at the wrong account. Ambiguous rows stay
unassigned and the migration reports how many. The cost is that such a row
is invisible in every environment, so the owner reaches it through a
user-scoped lookup that runs outside the filter, and assigns it with
PATCH .../{uuid}/environment. The admin panel marks those rows and offers
the assignment.
Tests: 896 backend (+11), 570 frontend (+4). PHPStan unchanged at its 17
pre-existing errors.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
116 lines
4.8 KiB
TypeScript
116 lines
4.8 KiB
TypeScript
import React, { useState } from 'react';
|
|
import { toast } from 'sonner';
|
|
import SettingsLayout from '../components/layout/SettingsLayout';
|
|
import PageHeader from '../components/ui/PageHeader';
|
|
import PaymentMethodHead, { type PaymentTab } from '../components/paymentMethods/PaymentMethodHead';
|
|
import BankAccountTable from '../components/paymentMethods/BankAccountTable';
|
|
import PosTable from '../components/paymentMethods/PosTable';
|
|
import BankAccountFormModal from '../components/paymentMethods/BankAccountFormModal';
|
|
import PosFormModal from '../components/paymentMethods/PosFormModal';
|
|
import {
|
|
useAssignBankAccountEnvironment,
|
|
useAssignPosEnvironment,
|
|
useBankAccounts,
|
|
usePosDevices,
|
|
useToggleBankAccountStatus,
|
|
useTogglePosStatus,
|
|
type BankAccount,
|
|
type Pos,
|
|
} from '../hooks/usePaymentMethods';
|
|
import { useAuthStore } from '../stores/authStore';
|
|
|
|
/**
|
|
* صفحهٔ «مدیریت پرداخت» (/admin/my-financial) — پورت تب PaymentManagement از
|
|
* clinic-pro-tauri. روشهای پرداختِ **محیط فعال** (حساب بانکی + کارتخوان) که بعداً
|
|
* از فاکتور مراجعهکننده برای ثبت روش پرداخت یک سرویس ارجاع میشوند.
|
|
*
|
|
* نام محیط در سرتیتر میآید چون از فاز ۶ کارتها به محیط تعلق دارند نه به کاربر؛
|
|
* بدون آن، کاربرِ چندمحیطی با تعویض محیط فکر میکند کارتهایش گم شدهاند.
|
|
*/
|
|
function MyFinancialPageContent() {
|
|
const [activeTab, setActiveTab] = useState<PaymentTab>('bank');
|
|
const [bankModalOpen, setBankModalOpen] = useState(false);
|
|
const [posModalOpen, setPosModalOpen] = useState(false);
|
|
const [editingBank, setEditingBank] = useState<BankAccount | null>(null);
|
|
const [editingPos, setEditingPos] = useState<Pos | null>(null);
|
|
|
|
const bankQuery = useBankAccounts();
|
|
const posQuery = usePosDevices();
|
|
const toggleBank = useToggleBankAccountStatus();
|
|
const togglePos = useTogglePosStatus();
|
|
const assignBank = useAssignBankAccountEnvironment();
|
|
const assignPos = useAssignPosEnvironment();
|
|
|
|
const context = useAuthStore(s => s.context);
|
|
const environmentName = context?.name || 'محیط فعال';
|
|
|
|
const banks = bankQuery.data?.data ?? [];
|
|
const posDevices = posQuery.data?.data ?? [];
|
|
|
|
const openAdd = () => {
|
|
if (activeTab === 'bank') { setEditingBank(null); setBankModalOpen(true); }
|
|
else { setEditingPos(null); setPosModalOpen(true); }
|
|
};
|
|
|
|
const onError = (e: unknown) => toast.error(e instanceof Error ? e.message : 'خطا در تغییر وضعیت');
|
|
const onAssignError = (e: unknown) => toast.error(e instanceof Error ? e.message : 'خطا در تعیین محیط');
|
|
|
|
return (
|
|
<div className="page">
|
|
<PageHeader
|
|
title="مدیریت پرداخت"
|
|
description={`روشهای پرداخت «${environmentName}» (حساب بانکی و کارتخوان)`}
|
|
/>
|
|
|
|
<div className="card card-pad">
|
|
<PaymentMethodHead activeTab={activeTab} onTabChange={setActiveTab} onAdd={openAdd} />
|
|
|
|
{activeTab === 'bank' ? (
|
|
<BankAccountTable
|
|
data={banks}
|
|
loading={bankQuery.isLoading}
|
|
togglingUuid={toggleBank.isPending ? toggleBank.variables ?? null : null}
|
|
assigningUuid={assignBank.isPending ? assignBank.variables ?? null : null}
|
|
environmentName={environmentName}
|
|
onToggle={(uuid) => toggleBank.mutate(uuid, { onError })}
|
|
onEdit={(account) => { setEditingBank(account); setBankModalOpen(true); }}
|
|
onAssign={(uuid) => assignBank.mutate(uuid, { onError: onAssignError })}
|
|
onAdd={openAdd}
|
|
/>
|
|
) : (
|
|
<PosTable
|
|
data={posDevices}
|
|
loading={posQuery.isLoading}
|
|
togglingUuid={togglePos.isPending ? togglePos.variables ?? null : null}
|
|
assigningUuid={assignPos.isPending ? assignPos.variables ?? null : null}
|
|
environmentName={environmentName}
|
|
onToggle={(uuid) => togglePos.mutate(uuid, { onError })}
|
|
onEdit={(pos) => { setEditingPos(pos); setPosModalOpen(true); }}
|
|
onAssign={(uuid) => assignPos.mutate(uuid, { onError: onAssignError })}
|
|
onAdd={openAdd}
|
|
/>
|
|
)}
|
|
</div>
|
|
|
|
<BankAccountFormModal
|
|
open={bankModalOpen}
|
|
account={editingBank}
|
|
onClose={() => { setBankModalOpen(false); setEditingBank(null); }}
|
|
/>
|
|
<PosFormModal
|
|
open={posModalOpen}
|
|
pos={editingPos}
|
|
onClose={() => { setPosModalOpen(false); setEditingPos(null); }}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default function MyFinancialPage() {
|
|
return (
|
|
<SettingsLayout active="payment">
|
|
<MyFinancialPageContent />
|
|
</SettingsLayout>
|
|
);
|
|
}
|