fix(tenant): scope the patient wallet ledger to the environment reading it

ownsRecord guards the patient record, not the rows underneath it, so
GET /api/v1/patient/{uuid}/wallet/transactions — and the recent_transactions
in the balance summary — returned the patient's entire history. Clinic A
could read what the patient paid at clinic B, down to the name of the staff
member who entered it.

The wallet stays the person's: the balance is still the sum of that user's
credits minus debits across every environment. Scoping it would show a
patient part of their own money and would make the running balance_after
meaningless. So this is attribution per row, not ownership per wallet.

The columns are deliberately named recorded_entity_type / recorded_entity_id
rather than entity_type / entity_id. TenantFilter keys on the latter and
would then scope the balance query too — the exact bug this avoids. The
naming is load-bearing, and both the entity and the architecture doc say so.

Rows that cannot be attributed — entered before this split, or outside any
environment such as a representation's commission — stay NULL and remain
visible everywhere; hiding them would make an existing patient's history
look deleted. The migration reports how many there are (0 in dev, all
attributable from payments and session references).

Consequence, documented in both docs/api/patient.md and the wallet tab: the
listed rows no longer sum to the displayed balance.

Removing the fix turns 3 of the 6 new tests red.

Tests: 902 backend (+6), 570 frontend. PHPStan unchanged at 17.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hamed
2026-07-28 15:21:55 +03:30
co-authored by Claude Opus 5
parent c9d4348c46
commit 6ab1eb6483
10 changed files with 410 additions and 10 deletions
@@ -67,6 +67,27 @@ class WalletTransaction
#[ORM\Column(type: 'string', length: 15, options: ['default' => self::STATUS_CONFIRMED])]
private string $status = self::STATUS_CONFIRMED;
/**
* محیطی که این تراکنش **در آن ثبت شده** — نه مالکِ تراکنش.
*
* کیف پول مالِ شخص است و موجودی از مجموع credit−debitِ همهٔ ردیف‌های او مشتق
* می‌شود؛ ولی دفترِ تراکنش را کلینیک هم می‌بیند و بدون این ستون، کلینیک A
* می‌دید بیمار در کلینیک B چه پرداختی کرده و چه کسی ثبتش کرده.
*
* ⚠️ نام ستون عمداً `entity_type/entity_id` **نیست**: آن نام را TenantFilter
* می‌شناسد و خودکار روی هر کوئری می‌نشیند — از جمله روی محاسبهٔ موجودی، که
* آن‌وقت per-محیط می‌شد و پول بیمار را نصف نشان می‌داد. اینجا انتساب است، نه
* مالکیت؛ فیلتر نباید ببیندش. {@see docs/architecture/tenancy.md}
*
* تهی یعنی «ثبت‌شده پیش از این تفکیک، یا بیرون از هر محیط» (مثل سهم نماینده)؛
* چنین ردیفی در همهٔ محیط‌ها دیده می‌شود تا تاریخچهٔ کسی ناپدید نشود.
*/
#[ORM\Column(name: 'recorded_entity_type', type: 'string', length: 10, nullable: true)]
private ?string $recordedEntityType = null;
#[ORM\Column(name: 'recorded_entity_id', type: 'integer', nullable: true)]
private ?int $recordedEntityId = null;
#[ORM\Column(name: 'created_at', type: 'integer')]
private int $createdAt;
@@ -93,6 +114,8 @@ class WalletTransaction
public function getPaymentMethod(): ?string { return $this->paymentMethod; }
public function getReference(): ?string { return $this->reference; }
public function getStatus(): string { return $this->status; }
public function getRecordedEntityType(): ?string { return $this->recordedEntityType; }
public function getRecordedEntityId(): ?int { return $this->recordedEntityId; }
public function setPayment(?Payment $p): self { $this->payment = $p; return $this; }
public function setDescription(?string $d): self { $this->description = $d; return $this; }
@@ -102,6 +125,15 @@ class WalletTransaction
public function setReference(?string $r): self { $this->reference = $r; return $this; }
public function setStatus(string $s): self { $this->status = $s; return $this; }
/** محیطی که تراکنش در آن ثبت شد؛ تهی برای ثبت‌های بیرون از هر محیط. */
public function setRecordedEnvironment(?string $entityType, ?int $entityId): self
{
$this->recordedEntityType = $entityType;
$this->recordedEntityId = $entityId;
return $this;
}
public function toArray(): array
{
return [