feat(invoice): synchronize invoice totals with patient session updates and add resync command

This commit is contained in:
hamed
2026-08-04 13:12:24 +03:30
parent 4653cd0e4a
commit 0dca245246
8 changed files with 411 additions and 23 deletions
+37 -3
View File
@@ -108,6 +108,39 @@ class PaymentsSummaryTest extends ApiTestCase
self::assertSame(2, $data['invoices_count']);
}
/**
* ⚠️ مرزی: فاکتورِ نیمه‌پرداخت. پیش از این، «پرداخت‌شده» فقط فاکتورهای کاملاً
* تسویه‌شده را می‌شمرد، پس پولِ گرفته‌شده نامرئی می‌شد و بدهی بزرگ‌تر از واقعیت.
*/
public function testPartiallyPaidInvoiceCountsItsCollectedAmount(): void
{
[$owner, $doctor] = $this->doctor();
$record = $this->patientRecord($doctor);
// سهم بیمار ۱٬۰۰۰٬۰۰۰ و فقط ۴۰۰٬۰۰۰ وصول شده.
$this->invoice($doctor, $record, 1_000_000, Invoice::STATUS_FINALIZED, 1_700_000_000, 400_000);
$data = $this->authJson('GET', '/api/v1/my/billing/payments/summary', $owner)['data'];
self::assertSame(1_000_000, $data['total_rials']);
self::assertSame(400_000, $data['paid_rials']);
self::assertSame(600_000, $data['unsettled_rials']);
}
/** ⚠️ مرزی: اضافه‌پرداخت نباید «تسویه‌نشده» را منفی کند. */
public function testOverpaymentIsCappedAtTheInvoiceShare(): void
{
[$owner, $doctor] = $this->doctor();
$record = $this->patientRecord($doctor);
$this->invoice($doctor, $record, 500_000, Invoice::STATUS_FINALIZED, 1_700_000_000, 900_000);
$data = $this->authJson('GET', '/api/v1/my/billing/payments/summary', $owner)['data'];
self::assertSame(500_000, $data['paid_rials']);
self::assertSame(0, $data['unsettled_rials']);
}
public function testSummaryHonoursStatusAndDateFilters(): void
{
[$owner, $doctor] = $this->doctor();
@@ -168,11 +201,12 @@ class PaymentsSummaryTest extends ApiTestCase
$res = $this->authJson('GET', '/api/v1/my/billing/patients/' . $record->getUuid() . '/invoices', $owner);
self::assertSame(200, $this->responseCode());
// the patient view sums total_rials (invoice grand total), not the patient share
// ستون «مجموع» جمعِ کلِ صورتحساب است، ولی وصولی و بدهی روی سهم بیمار سنجیده
// می‌شوند: فاکتور اول (سهم ۱٬۰۰۰٬۰۰۰) تسویه شده و فاکتور دوم (۵۰۰٬۰۰۰) نه.
$summary = $res['data']['summary'];
self::assertSame(3_000_000, $summary['total_rials']);
self::assertSame(2_000_000, $summary['paid_rials']);
self::assertSame(1_000_000, $summary['unsettled_rials']);
self::assertSame(1_000_000, $summary['paid_rials']);
self::assertSame(500_000, $summary['unsettled_rials']);
self::assertSame(2, $summary['invoices_count']);
self::assertSame(2, $res['data']['meta']['totalRecords']);
}