feat(invoice): implement recorder identity resolution for payments and update related tests

This commit is contained in:
hamed
2026-08-04 11:19:07 +03:30
parent 2c7b86d917
commit 810e9351a9
7 changed files with 321 additions and 13 deletions
+34
View File
@@ -12,6 +12,7 @@ use App\Insurance\Repository\InsuranceRepository;
use App\Insurance\Service\TenantInsuranceService;
use App\Patient\Entity\PatientSession;
use App\Patient\Repository\PatientSessionRepository;
use App\Shared\Service\ActorIdentityResolver;
use Psr\EventDispatcher\EventDispatcherInterface;
class InvoiceService
@@ -23,6 +24,7 @@ class InvoiceService
private readonly PatientSessionRepository $sessionRepo,
private readonly InsuranceRepository $insuranceRepo,
private readonly EventDispatcherInterface $events,
private readonly ActorIdentityResolver $actorIdentity,
) {}
/** @var array<int, string|null> نام بیمه‌ها، یک‌بار در هر درخواست. */
@@ -112,6 +114,10 @@ class InvoiceService
$session = $sessionId !== null ? $this->sessionRepo->find($sessionId) : null;
$data['session'] = $session?->toArray();
if ($session !== null) {
$data['session']['payments'] = $this->withRecorderIdentity($session, $data['session']['payments'] ?? []);
}
// نام بیمه‌ها برای چاپ روی فاکتور؛ فاکتور فقط شناسه را نگه می‌دارد.
$data['base_insurance_name'] = $this->insuranceName($invoice->getBaseInsuranceId());
$data['supplementary_insurance_name'] = $this->insuranceName($invoice->getSupplementaryInsuranceId());
@@ -119,6 +125,34 @@ class InvoiceService
return $data;
}
/**
* ستون «ثبت‌کننده»ی خلاصهٔ فاکتور: هویتِ کاربرِ ثبت‌کننده کنار هر پرداخت.
*
* `created_by_name` روی ردیف، عکسِ لحظهٔ ثبت است و برای کاربری که آن‌وقت پروفایل
* نداشته شمارهٔ موبایل ذخیره شده؛ پس نام از خودِ کاربر دوباره حل می‌شود و عکس فقط
* برای کاربرِ حذف‌شده می‌ماند. `created_by` هم شناسه‌ها را می‌دهد تا کلاینت بتواند
* به پروفایلش لینک کند.
*
* @param list<array<string, mixed>> $rows
* @return list<array<string, mixed>>
*/
private function withRecorderIdentity(PatientSession $session, array $rows): array
{
$actorByUuid = [];
foreach ($session->getPayments() as $payment) {
$actorByUuid[$payment->getUuid()] = $payment->getCreatedBy();
}
return array_map(function (array $row) use ($actorByUuid): array {
$identity = $this->actorIdentity->identity($actorByUuid[$row['uuid'] ?? ''] ?? null);
$row['created_by'] = $identity;
$row['created_by_name'] = $identity['name'] ?? ($row['created_by_name'] ?? null);
return $row;
}, $rows);
}
/**
* Paginated flat list of a tenant's recorded (finalized/paid) invoices for
* the payments list (node 1). Rows arrive ready-shaped from the repository;