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>
This commit is contained in:
@@ -157,13 +157,13 @@ class BillingController extends BaseController
|
||||
}
|
||||
|
||||
/**
|
||||
* لیست پرداختها — یک ردیف بهازای هر بیمار با جمع صورتحسابها.
|
||||
* فیلترها: national_code، status (paid|unsettled|unpaid)، from/to (unix ثانیه).
|
||||
* پاسخ صفحهبندی: هر ردیف { patient_uuid, patient_name, national_code,
|
||||
* invoice_count, paid_rials, remaining_rials, status }.
|
||||
* لیست پرداختها — یک ردیف بهازای هر صورتحساب ثبتشدهی tenant (flat).
|
||||
* فیلترها: national_code، status (paid|unsettled)، from/to (unix ثانیه).
|
||||
* پاسخ صفحهبندی: هر ردیف { invoice_uuid, patient_uuid, patient_name,
|
||||
* national_code, issued_at, amount_rials, status }.
|
||||
*/
|
||||
#[Route('/api/v1/my/billing/patient-payments', methods: ['GET'])]
|
||||
public function listPatientPayments(Request $request, #[CurrentUser] User $user): JsonResponse
|
||||
#[Route('/api/v1/my/billing/payments', methods: ['GET'])]
|
||||
public function listPayments(Request $request, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
[$entityType, $entityId] = $this->resolveEntity($user);
|
||||
if ($entityId === null) {
|
||||
@@ -179,7 +179,7 @@ class BillingController extends BaseController
|
||||
$page = max(1, (int) $request->query->get('page', 1));
|
||||
$limit = min(100, max(1, (int) $request->query->get('limit', 20)));
|
||||
|
||||
$result = $this->invoiceService->patientPaymentList($entityType, $entityId, $filters, $page, $limit);
|
||||
$result = $this->invoiceService->tenantInvoiceList($entityType, $entityId, $filters, $page, $limit);
|
||||
|
||||
return $this->paginated($result['items'], $result['total'], $page, $limit);
|
||||
}
|
||||
|
||||
@@ -27,36 +27,74 @@ class InvoiceRepository extends ServiceEntityRepository
|
||||
}
|
||||
|
||||
/**
|
||||
* One row per patient (record) with their invoice totals for a tenant.
|
||||
* paid = patient share on paid invoices; remaining = patient share on
|
||||
* finalized-but-unpaid invoices. draft/void invoices are ignored.
|
||||
* A flat, newest-first page of a tenant's recorded (finalized/paid) invoices,
|
||||
* one row per invoice with the patient's name and national code joined in.
|
||||
*
|
||||
* @param array{national_code?:?string,status?:?string,from?:?int,to?:?int} $filters
|
||||
* status: paid|unsettled|unpaid — derived from paid/remaining via HAVING.
|
||||
* @return list<array{patient_uuid:string,patient_name:?string,national_code:?string,invoice_count:int,paid_rials:int,remaining_rials:int}>
|
||||
* status: paid | unsettled (maps to invoice paid / finalized).
|
||||
* @return list<array{invoice_uuid:string,patient_uuid:string,patient_name:?string,national_code:?string,issued_at:int,amount_rials:int,status:string}>
|
||||
*/
|
||||
public function patientPaymentSummary(string $entityType, int $entityId, array $filters, int $page, int $limit): array
|
||||
public function tenantInvoices(string $entityType, int $entityId, array $filters, int $page, int $limit): array
|
||||
{
|
||||
$rows = $this->summaryQuery($entityType, $entityId, $filters)
|
||||
$rows = $this->tenantInvoicesQuery($entityType, $entityId, $filters)
|
||||
->select(
|
||||
'i.uuid AS invoice_uuid', 'r.uuid AS patient_uuid', 'u.realName AS patient_name',
|
||||
'u.nationalCode AS national_code', 'i.issuedAt AS issued_at',
|
||||
'i.patientRials AS amount_rials', 'i.status AS status',
|
||||
)
|
||||
->orderBy('i.issuedAt', 'DESC')
|
||||
->setFirstResult(($page - 1) * $limit)
|
||||
->setMaxResults($limit)
|
||||
->getQuery()
|
||||
->getArrayResult();
|
||||
|
||||
return array_map(static fn(array $r): array => [
|
||||
'patient_uuid' => $r['patient_uuid'],
|
||||
'patient_name' => $r['patient_name'],
|
||||
'national_code' => $r['national_code'],
|
||||
'invoice_count' => (int) $r['invoice_count'],
|
||||
'paid_rials' => (int) $r['paid_rials'],
|
||||
'remaining_rials' => (int) $r['remaining_rials'],
|
||||
'invoice_uuid' => $r['invoice_uuid'],
|
||||
'patient_uuid' => $r['patient_uuid'],
|
||||
'patient_name' => $r['patient_name'],
|
||||
'national_code' => $r['national_code'],
|
||||
'issued_at' => (int) $r['issued_at'],
|
||||
'amount_rials' => (int) $r['amount_rials'],
|
||||
'status' => $r['status'] === Invoice::STATUS_PAID ? 'paid' : 'unsettled',
|
||||
], $rows);
|
||||
}
|
||||
|
||||
/** Number of patients (groups) matching the same filters — for pagination. */
|
||||
public function countPatientPaymentSummary(string $entityType, int $entityId, array $filters): int
|
||||
public function countTenantInvoices(string $entityType, int $entityId, array $filters): int
|
||||
{
|
||||
return count($this->summaryQuery($entityType, $entityId, $filters)->getQuery()->getArrayResult());
|
||||
return (int) $this->tenantInvoicesQuery($entityType, $entityId, $filters)
|
||||
->select('COUNT(i.id)')
|
||||
->getQuery()
|
||||
->getSingleScalarResult();
|
||||
}
|
||||
|
||||
private function tenantInvoicesQuery(string $entityType, int $entityId, array $filters): QueryBuilder
|
||||
{
|
||||
$qb = $this->createQueryBuilder('i')
|
||||
->innerJoin(PatientRecord::class, 'r', Join::WITH, 'r.id = i.patientRecordId')
|
||||
->innerJoin('r.user', 'u')
|
||||
->where('i.entityType = :type')
|
||||
->andWhere('i.entityId = :id')
|
||||
->andWhere('i.status IN (:active)')
|
||||
->setParameter('type', $entityType)
|
||||
->setParameter('id', $entityId)
|
||||
->setParameter('active', [Invoice::STATUS_FINALIZED, Invoice::STATUS_PAID]);
|
||||
|
||||
if (!empty($filters['national_code'])) {
|
||||
$qb->andWhere('u.nationalCode LIKE :nc')->setParameter('nc', '%' . $filters['national_code'] . '%');
|
||||
}
|
||||
if (!empty($filters['from'])) {
|
||||
$qb->andWhere('i.issuedAt >= :from')->setParameter('from', (int) $filters['from']);
|
||||
}
|
||||
if (!empty($filters['to'])) {
|
||||
$qb->andWhere('i.issuedAt <= :to')->setParameter('to', (int) $filters['to']);
|
||||
}
|
||||
if (($filters['status'] ?? null) === 'paid') {
|
||||
$qb->andWhere('i.status = :st')->setParameter('st', Invoice::STATUS_PAID);
|
||||
} elseif (($filters['status'] ?? null) === 'unsettled') {
|
||||
$qb->andWhere('i.status = :st')->setParameter('st', Invoice::STATUS_FINALIZED);
|
||||
}
|
||||
|
||||
return $qb;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,49 +132,6 @@ class InvoiceRepository extends ServiceEntityRepository
|
||||
->setParameter('active', [Invoice::STATUS_FINALIZED, Invoice::STATUS_PAID]);
|
||||
}
|
||||
|
||||
private function summaryQuery(string $entityType, int $entityId, array $filters): QueryBuilder
|
||||
{
|
||||
$paidSum = 'SUM(CASE WHEN i.status = :paid THEN i.patientRials ELSE 0 END)';
|
||||
$remSum = 'SUM(CASE WHEN i.status = :finalized THEN i.patientRials ELSE 0 END)';
|
||||
|
||||
$qb = $this->createQueryBuilder('i')
|
||||
->select('r.uuid AS patient_uuid', 'u.realName AS patient_name', 'u.nationalCode AS national_code')
|
||||
->addSelect('COUNT(i.id) AS invoice_count')
|
||||
->addSelect("$paidSum AS paid_rials")
|
||||
->addSelect("$remSum AS remaining_rials")
|
||||
->innerJoin(PatientRecord::class, 'r', Join::WITH, 'r.id = i.patientRecordId')
|
||||
->innerJoin('r.user', 'u')
|
||||
->where('i.entityType = :type')
|
||||
->andWhere('i.entityId = :id')
|
||||
->andWhere('i.status IN (:active)')
|
||||
->setParameter('type', $entityType)
|
||||
->setParameter('id', $entityId)
|
||||
->setParameter('active', [Invoice::STATUS_FINALIZED, Invoice::STATUS_PAID])
|
||||
->setParameter('paid', Invoice::STATUS_PAID)
|
||||
->setParameter('finalized', Invoice::STATUS_FINALIZED)
|
||||
->groupBy('r.id')->addGroupBy('r.uuid')->addGroupBy('u.realName')->addGroupBy('u.nationalCode')
|
||||
->orderBy('MAX(i.issuedAt)', 'DESC');
|
||||
|
||||
if (!empty($filters['national_code'])) {
|
||||
$qb->andWhere('u.nationalCode LIKE :nc')->setParameter('nc', '%' . $filters['national_code'] . '%');
|
||||
}
|
||||
if (!empty($filters['from'])) {
|
||||
$qb->andWhere('i.issuedAt >= :from')->setParameter('from', (int) $filters['from']);
|
||||
}
|
||||
if (!empty($filters['to'])) {
|
||||
$qb->andWhere('i.issuedAt <= :to')->setParameter('to', (int) $filters['to']);
|
||||
}
|
||||
|
||||
// Derived-status filters applied on the aggregates.
|
||||
switch ($filters['status'] ?? null) {
|
||||
case 'paid': $qb->having("$remSum = 0"); break;
|
||||
case 'unpaid': $qb->having("$paidSum = 0")->andHaving("$remSum > 0"); break;
|
||||
case 'unsettled': $qb->having("$paidSum > 0")->andHaving("$remSum > 0"); break;
|
||||
}
|
||||
|
||||
return $qb;
|
||||
}
|
||||
|
||||
public function save(Invoice $entity, bool $flush = true): void
|
||||
{
|
||||
$this->getEntityManager()->persist($entity);
|
||||
|
||||
@@ -76,25 +76,18 @@ class InvoiceService
|
||||
}
|
||||
|
||||
/**
|
||||
* Paginated per-patient payment summary for a tenant. Each row gains a
|
||||
* derived status: `paid` (nothing outstanding), `unpaid` (nothing paid
|
||||
* yet), `unsettled` (partially paid).
|
||||
* Paginated flat list of a tenant's recorded (finalized/paid) invoices for
|
||||
* the payments list (node 1). Rows arrive ready-shaped from the repository;
|
||||
* this only pairs them with the total for pagination.
|
||||
*
|
||||
* @param array{national_code?:?string,status?:?string,from?:?int,to?:?int} $filters
|
||||
* @return array{items: list<array<string, mixed>>, total: int}
|
||||
*/
|
||||
public function patientPaymentList(string $entityType, int $entityId, array $filters, int $page, int $limit): array
|
||||
public function tenantInvoiceList(string $entityType, int $entityId, array $filters, int $page, int $limit): array
|
||||
{
|
||||
$items = array_map(static function (array $row): array {
|
||||
$row['status'] = $row['remaining_rials'] === 0
|
||||
? 'paid'
|
||||
: ($row['paid_rials'] === 0 ? 'unpaid' : 'unsettled');
|
||||
return $row;
|
||||
}, $this->invoiceRepo->patientPaymentSummary($entityType, $entityId, $filters, $page, $limit));
|
||||
|
||||
return [
|
||||
'items' => $items,
|
||||
'total' => $this->invoiceRepo->countPatientPaymentSummary($entityType, $entityId, $filters),
|
||||
'items' => $this->invoiceRepo->tenantInvoices($entityType, $entityId, $filters, $page, $limit),
|
||||
'total' => $this->invoiceRepo->countTenantInvoices($entityType, $entityId, $filters),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user