Files
clinicpro/assets/admin/hooks/useMyPayments.ts
T
hamedandClaude Opus 4.8 818506bf36 refactor(billing): rebuild payments list as flat invoice list (tauri parity)
Align /admin/my-payments with the tauri /payments source (per user): the list
is now a flat, newest-first list of the tenant's recorded invoices — one row
per invoice — instead of the per-patient aggregation built from Figma.

Backend:
- replace InvoiceRepository::patientPaymentSummary aggregation with
  tenantInvoices/countTenantInvoices (flat, joins patient name/national code).
- InvoiceService::patientPaymentList → tenantInvoiceList.
- BillingController: GET /api/v1/my/billing/patient-payments →
  GET /api/v1/my/billing/payments returning
  { invoice_uuid, patient_uuid, patient_name, national_code, issued_at,
    amount_rials, status } rows.
- node-2 patient invoices endpoint unchanged.

Frontend:
- useMyPayments: usePatientPayments → usePayments (flat PaymentRow).
- MyPaymentsPage columns match tauri DetailT: row #, avatar+name, national
  code, date-time, amount paid, مشاهده (no status column); 'اضافه کردن بیمار'
  links to /admin/patients/new. Filters (national code / status / Jalali date
  range) kept.

Tests + docs/api/billing.md updated. Intentionally omitted tauri extras:
mobile Cards view and the advanced ModalFilter.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-14 19:00:17 +03:30

72 lines
2.5 KiB
TypeScript

import { useQuery } from '@tanstack/react-query';
import { api } from '../lib/api';
import type { ApiResponse, PaginatedResponse } from '../lib/api';
/** Two-state invoice status shown on both the list (node 1) and detail (node 2). */
export type PaymentRowStatus = 'paid' | 'unsettled';
/** One recorded invoice on the flat payments list (node 1). */
export interface PaymentRow {
invoice_uuid: string;
patient_uuid: string;
patient_name: string | null;
national_code: string | null;
issued_at: number;
amount_rials: number;
status: PaymentRowStatus;
}
export interface PaymentFilters {
page: number;
national_code?: string;
status?: string; // paid | unsettled
from?: number; // unix seconds
to?: number; // unix seconds
}
/** A single invoice status on the detail table (node 2) — two-state. */
export type InvoiceRowStatus = 'paid' | 'unsettled';
export interface PatientInvoiceRow {
uuid: string;
number: number;
issued_at: number;
total_rials: number;
status: InvoiceRowStatus;
service_title: string | null;
items: Array<{ uuid: string; title: string; quantity: number; total_rials: number; patient_rials: number }>;
}
export interface PatientInvoicesPayload {
patient: { uuid: string; name: string | null; national_code: string | null };
data: PatientInvoiceRow[];
meta: { totalRecords: number; totalPages: number; currentPage: number };
}
const LIMIT = 20;
/** Node 1 — flat, paginated list of the current tenant's recorded invoices. */
export function usePayments(filters: PaymentFilters) {
const qs = new URLSearchParams({ page: String(filters.page), limit: String(LIMIT) });
if (filters.national_code) qs.set('national_code', filters.national_code);
if (filters.status) qs.set('status', filters.status);
if (filters.from) qs.set('from', String(filters.from));
if (filters.to) qs.set('to', String(filters.to));
return useQuery<PaginatedResponse<PaymentRow>>({
queryKey: ['payments', filters],
queryFn: () => api.get(`/api/v1/my/billing/payments?${qs.toString()}`),
});
}
/** Node 2 — a single patient's recorded invoices (header + paginated list). */
export function usePatientInvoices(patientUuid: string | undefined, page: number) {
return useQuery<ApiResponse<PatientInvoicesPayload>>({
queryKey: ['patient-invoices', patientUuid, page],
queryFn: () => api.get(`/api/v1/my/billing/patients/${patientUuid}/invoices?page=${page}&limit=${LIMIT}`),
enabled: !!patientUuid,
});
}
export const MY_PAYMENTS_LIMIT = LIMIT;