Files
clinicpro/tests/Settlement/FinancialChainTenantTest.php
hamedandClaude Opus 5 c9d4348c46 feat(tenant): mark the financial tables with their owning environment
Phase 6 of the tenant series. GlobalTables::DEFERRED is now empty and the
coverage test asserts it stays that way.

payments carries the (entity_type, entity_id) pair and belongs to the
receiving side, never the payer: an appointment payment takes the
appointment's environment, a subscription takes the environment its buyer
owns, and an SMS wallet top-up takes the wallet's. The patient never chose
an environment, so TenantFilter stays off for them and they still see their
own payment.

Three corrections to the analysis the phase was planned on, each backed by
the code or the data rather than the plan:

- A third payment type exists. Payment::TYPE_SMS_WALLET is created in
  SmsWalletController and already carries its environment in the metadata;
  without assigning it the write would fail at flush.
- clinic_subscriptions has no user_id, and its trial rows carry no payment,
  so it cannot drive the subscription backfill. The environment is derived
  the way handleSubscriptionActivation derives it — and that method now
  reads the pair off the payment instead of re-deriving it, so a payment and
  the subscription it buys can no longer land on different environments.
- WalletTransaction is not a child of Payment. payment_id is nullable and
  none of the four creation sites set it; the wallet is a person's, with a
  running balance per user. It and Settlement, which withdraws from that same
  wallet, are global with a recorded reason instead.

bank_accounts and pos_devices move from the registering user to the
environment. Their pair is deliberately nullable: nothing in the existing
data says which of a multi-environment owner's cards belongs where, and
guessing would point real money at the wrong account. Ambiguous rows stay
unassigned and the migration reports how many. The cost is that such a row
is invisible in every environment, so the owner reaches it through a
user-scoped lookup that runs outside the filter, and assigns it with
PATCH .../{uuid}/environment. The admin panel marks those rows and offers
the assignment.

Tests: 896 backend (+11), 570 frontend (+4). PHPStan unchanged at its 17
pre-existing errors.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-28 15:06:28 +03:30

144 lines
5.5 KiB
PHP

<?php
namespace App\Tests\Settlement;
use App\Auth\Entity\User;
use App\Doctor\Entity\Doctor;
use App\Payment\Entity\Payment;
use App\Secretary\Entity\SecretaryEarning;
use App\Secretary\Repository\SecretaryEarningRepository;
use App\Settlement\Entity\FinancialBreakdown;
use App\Settlement\Entity\WalletTransaction;
use App\Settlement\Repository\WalletTransactionRepository;
use App\Shared\Tenant\TenantFilter;
use App\Tests\ApiTestCase;
use Doctrine\ORM\EntityManagerInterface;
/**
* زنجیرهٔ مالی ستون محیط ندارد و آن را از `payments` به ارث می‌برد:
*
* Payment ─┬─ PaymentLog (payment_id اسکالر)
* └─ FinancialBreakdown ── SecretaryEarning
*
* تضمین فقط تا جایی است که کوئری به ریشه لنگر بزند؛ `reportFor` این کار را با
* join('b.payment','p') می‌کند و همان join است که TenantFilter رویش می‌نشیند.
*
* کیف پول عمداً بیرون این زنجیره است: مالِ شخص است نه محیط، و همین‌جا پین می‌شود
* تا اگر روزی کسی ستون محیط رویش گذاشت، این تصمیم دوباره دیده شود.
*/
class FinancialChainTenantTest extends ApiTestCase
{
private function em(): EntityManagerInterface
{
return static::getContainer()->get(EntityManagerInterface::class);
}
protected function tearDown(): void
{
$filters = $this->em()->getFilters();
if ($filters->isEnabled(TenantFilter::NAME)) {
$filters->disable(TenantFilter::NAME);
}
parent::tearDown();
}
private function enableFilterFor(string $type, int $id): void
{
$this->em()->getFilters()
->enable(TenantFilter::NAME)
->setParameter(TenantFilter::PARAM_TYPE, $type, 'string')
->setParameter(TenantFilter::PARAM_ID, $id, 'integer');
}
private function makeDoctor(): Doctor
{
$doctor = new Doctor($this->createUser(['ROLE_DOCTOR']), 'دکتر زنجیره');
$this->em->persist($doctor);
$this->em->flush();
return $doctor;
}
/** یک سهم منشی از پرداختِ محیطِ داده‌شده، با کل زنجیره‌اش. */
private function earningFor(Doctor $doctor, User $secretary): SecretaryEarning
{
$payment = new Payment($this->createUser(), 1_000_000, 'mock', Payment::TYPE_APPOINTMENT);
$payment->assignTenantPair('doctor', $doctor->getId());
$this->em->persist($payment);
$breakdown = new FinancialBreakdown(
$payment, FinancialBreakdown::SOURCE_APPOINTMENT, $secretary,
1_000_000, 0, '0.00', 0, 1_000_000, '10.00', 100_000, 900_000, null, null, null,
);
$this->em->persist($breakdown);
$earning = new SecretaryEarning($breakdown, $secretary, 'rel-' . bin2hex(random_bytes(4)), 5.0, 50_000);
$this->em->persist($earning);
$this->em->flush();
return $earning;
}
/** @return string[] */
private function reportedUuids(User $secretary): array
{
$repo = static::getContainer()->get(SecretaryEarningRepository::class);
$report = $repo->reportFor($secretary, 1, 50);
return array_column($report['items'] ?? [], 'uuid');
}
/** ✅ سهمِ محیط خودی از راه لنگر به پرداخت دیده می‌شود. */
public function testAnEarningIsVisibleInsideItsOwnEnvironment(): void
{
$doctor = $this->makeDoctor();
$secretary = $this->createUser(['ROLE_SECRETARY']);
$earning = $this->earningFor($doctor, $secretary);
$this->em->clear();
$this->enableFilterFor('doctor', $doctor->getId());
self::assertContains($earning->getUuid(), $this->reportedUuids($secretary));
}
/**
* ❌ همان سهم در محیط دیگر ناپدید می‌شود — نه به‌خاطر شرط دستی، بلکه چون
* کوئری به `payments` لنگر زده و فیلتر روی همان join نشسته است.
*/
public function testTheSameEarningDisappearsInAnotherEnvironment(): void
{
$doctor = $this->makeDoctor();
$secretary = $this->createUser(['ROLE_SECRETARY']);
$earning = $this->earningFor($doctor, $secretary);
$this->em->clear();
$this->enableFilterFor('doctor', $doctor->getId() + 1000);
self::assertNotContains($earning->getUuid(), $this->reportedUuids($secretary));
}
/**
* ⚠️ مرزی: کیف پول شخص محیط ندارد و در هر محیطی دیده می‌شود. این نشتی نیست،
* تصمیم است — موجودی از مجموع credit−debitِ همان کاربر مشتق می‌شود و تفکیکش
* به محیط، خودِ موجودی را بی‌معنا می‌کند.
*/
public function testTheUserWalletStaysGlobalAcrossEnvironments(): void
{
$patient = $this->createUser(['ROLE_USER']);
$txn = new WalletTransaction($patient, 500_000, WalletTransaction::TYPE_CREDIT, 500_000);
$this->em->persist($txn);
$this->em->flush();
$uuid = $txn->getUuid();
$this->em->clear();
$this->enableFilterFor('clinic', 987_654);
$repo = static::getContainer()->get(WalletTransactionRepository::class);
self::assertNotNull(
$repo->findOneBy(['uuid' => $uuid]),
'کیف پول شخص نباید به محیط قفل شود',
);
}
}