feat(billing): patient payments list + patient invoices detail (doctor/clinic)
Port two nobat724 Figma screens into the admin SPA for the doctor/clinic
tenant panel:
- node 1 — لیست پرداختها (/admin/my-payments): per-patient payment summary
(invoice count, paid, remaining, derived status paid/unsettled/unpaid),
filters by national code / status / Jalali date range, pagination.
- node 2 — پرداختهای ثبتشده (/admin/my-payments/:patientUuid): a patient's
recorded invoices with patient header, service title, total, status badge,
and an expandable per-invoice item breakdown.
Backend (App\Billing):
- InvoiceRepository::patientPaymentSummary/countPatientPaymentSummary — DQL
aggregation grouped by patient record (arbitrary join Invoice→PatientRecord
→User), draft/void excluded, derived-status HAVING filters.
- InvoiceRepository::invoicesForPatient/count + InvoiceService methods that
shape rows and derive status.
- BillingController: GET /api/v1/my/billing/patient-payments and
GET /api/v1/my/billing/patients/{patientUuid}/invoices (thin, resolveEntity,
tenant-scoped, 403/404). Invoice::getIssuedAt / InvoiceItem::getTitle added.
- docs/api/billing.md documents both endpoints.
Frontend: useMyPayments hooks, MyPaymentsPage, MyPaymentDetailPage, routes in
App.tsx (doctor/secretary/clinic, blockClinicScope) and a sidebar entry.
Persian strings hardcoded per existing admin convention (no i18n infra).
Tests: tests/Billing/PatientPaymentsTest.php (8), useMyPayments + both page
tests (11). Note: pre-existing LoginPage.test failures are unrelated (proven
by stashing this change).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -3,7 +3,10 @@
|
||||
namespace App\Billing\Repository;
|
||||
|
||||
use App\Billing\Entity\Invoice;
|
||||
use App\Patient\Entity\PatientRecord;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\ORM\Query\Expr\Join;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
class InvoiceRepository extends ServiceEntityRepository
|
||||
@@ -23,6 +26,117 @@ class InvoiceRepository extends ServiceEntityRepository
|
||||
return $this->findOneBy(['patientSessionId' => $patientSessionId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @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}>
|
||||
*/
|
||||
public function patientPaymentSummary(string $entityType, int $entityId, array $filters, int $page, int $limit): array
|
||||
{
|
||||
$rows = $this->summaryQuery($entityType, $entityId, $filters)
|
||||
->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'],
|
||||
], $rows);
|
||||
}
|
||||
|
||||
/** Number of patients (groups) matching the same filters — for pagination. */
|
||||
public function countPatientPaymentSummary(string $entityType, int $entityId, array $filters): int
|
||||
{
|
||||
return count($this->summaryQuery($entityType, $entityId, $filters)->getQuery()->getArrayResult());
|
||||
}
|
||||
|
||||
/**
|
||||
* A patient's recorded (finalized/paid) invoices for a tenant, newest first.
|
||||
* @return list<Invoice>
|
||||
*/
|
||||
public function invoicesForPatient(string $entityType, int $entityId, int $recordId, int $page, int $limit): array
|
||||
{
|
||||
return $this->patientInvoicesQuery($entityType, $entityId, $recordId)
|
||||
->orderBy('i.issuedAt', 'DESC')
|
||||
->setFirstResult(($page - 1) * $limit)
|
||||
->setMaxResults($limit)
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
|
||||
public function countInvoicesForPatient(string $entityType, int $entityId, int $recordId): int
|
||||
{
|
||||
return (int) $this->patientInvoicesQuery($entityType, $entityId, $recordId)
|
||||
->select('COUNT(i.id)')
|
||||
->getQuery()
|
||||
->getSingleScalarResult();
|
||||
}
|
||||
|
||||
private function patientInvoicesQuery(string $entityType, int $entityId, int $recordId): QueryBuilder
|
||||
{
|
||||
return $this->createQueryBuilder('i')
|
||||
->where('i.entityType = :type')
|
||||
->andWhere('i.entityId = :id')
|
||||
->andWhere('i.patientRecordId = :record')
|
||||
->andWhere('i.status IN (:active)')
|
||||
->setParameter('type', $entityType)
|
||||
->setParameter('id', $entityId)
|
||||
->setParameter('record', $recordId)
|
||||
->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);
|
||||
|
||||
Reference in New Issue
Block a user