Files
clinicpro/tests/Secretary/SecretaryOnlineShareTest.php
T
hamedandClaude Opus 5 2e0888e0ef refactor(tenant): give every table one spelling of the tenant pair
Phase 3 of the tenant-marking series. The same concept was written four ways,
and the Doctrine filter arriving in phase 4 keys on the field name — so the
tables using a different spelling would have been skipped silently, which is
exactly the leak this work exists to prevent.

- discount_rules: owner_type/owner_id renamed to entity_type/entity_id. Pure
  rename, no data moves.
- doctor_secretaries: owner_type plus a nullable clinic_id replaced by the
  shared pair. The environment now comes from the clinic argument alone, so the
  inconsistent combination (owner_type='clinic', clinic_id=NULL) can no longer
  be constructed, and the redundant constructor parameter is gone.
- user_active_context: added db_type, so resolving an environment is one lookup
  instead of "try clinics, then try doctors". Filled from the type already
  present in available_contexts.
- entity_type is VARCHAR(10) in all twenty tenant tables; four of them were 20.

Behaviour change, the only one in this series: the doctor_secretaries unique key
went from (doctor_id, secretary_id, owner_type) to (doctor_id, secretary_id,
entity_type, entity_id). With clinic_id outside the key, one secretary could not
be assigned to the same doctor in two clinics — the second row collided on
owner_type='clinic'. The duplicate check in SecretaryController had the same
blind spot and would have rejected the request before the database saw it; both
are fixed together.

Correcting an assumption from the phase-3 plan: mobile_verification_otp.entity_type
really is a tenant pair. NotificationMobileController validates the target against
['doctor','clinic'] and stores that entity's id, so the column was normalised with
the rest rather than treated as unrelated.

TenantOwnedTrait gained assignTenantPair() for callers that resolved the pair as
scalars and hold no entity — building an EntityContext from scalars would produce
one where isClinic() is true but ->clinic is null, breaking consumers silently.

tests/ApiTestCase::createUser now retries on a duplicate mobile. db_test is never
reset and already holds ~38k users, so the 9-digit random draw collided often
enough to fail unrelated tests a few percent of runs.

Tests: 830 passing. PHPStan reports no new errors on the changed files.

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

197 lines
8.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Tests\Secretary;
use App\Appointment\Entity\Appointment;
use App\Auth\Entity\User;
use App\Clinic\Entity\Clinic;
use App\Config\Repository\SiteConfigRepository;
use App\Doctor\Entity\Doctor;
use App\Payment\Entity\Payment;
use App\Secretary\Entity\DoctorSecretary;
use App\Secretary\Repository\SecretaryEarningRepository;
use App\Settlement\Repository\FinancialBreakdownRepository;
use App\Settlement\Repository\SettlementRepository;
use App\Settlement\Service\CommissionService;
use App\Tests\ApiTestCase;
/**
* سهم منشی از نوبت آنلاین: درصدِ رابطهٔ منشی روی «خالصِ پس از مالیات» — همان مبنایی
* که پورسانت نماینده از آن گرفته می‌شود — و مستقل از وجود نماینده.
*/
class SecretaryOnlineShareTest extends ApiTestCase
{
private const GROSS = 10_000_000;
protected function setUp(): void
{
parent::setUp();
$config = static::getContainer()->get(SiteConfigRepository::class);
// کسورات را قطعی می‌کنیم تا انتظارِ عددی تست پایدار بماند.
$config->set('sms_panel_fee_rials', '0');
$config->set('tax_enabled', '0');
$config->set('tax_percent', '0');
}
private function makeDoctor(): Doctor
{
$user = $this->createUser(['ROLE_USER', 'ROLE_DOCTOR']);
$doctor = new Doctor($user, 'دکتر تست سهم منشی');
$this->em->persist($doctor);
$this->em->flush();
return $doctor;
}
private function makeSecretary(Doctor $doctor, float $percent, bool $enabled = true, ?Clinic $clinic = null): DoctorSecretary
{
$user = $this->createUser(['ROLE_USER', 'ROLE_SECRETARY']);
$relation = new DoctorSecretary($doctor, $user, $clinic);
$relation->setOnlineShareEnabled($enabled)->setOnlineSharePercent($percent);
$this->em->persist($relation);
$this->em->flush();
return $relation;
}
private function makeOnlinePayment(Doctor $doctor, ?Clinic $clinic = null): Payment
{
$start = strtotime('+30 days') + random_int(0, 500_000) * 7;
$appointment = $this->newAppointment($doctor, $this->createUser(), $start, $start + 900, $clinic);
$this->em->persist($appointment);
$payment = new Payment($this->createUser(), self::GROSS, 'mock', Payment::TYPE_APPOINTMENT, '');
$payment->setAppointment($appointment);
$this->em->persist($payment);
$this->em->flush();
return $payment;
}
private function commission(): CommissionService
{
return static::getContainer()->get(CommissionService::class);
}
private function earnings(): SecretaryEarningRepository
{
return static::getContainer()->get(SecretaryEarningRepository::class);
}
public function testSecretaryEarnsItsPercentWithoutAnyRepresentation(): void
{
$doctor = $this->makeDoctor();
$relation = $this->makeSecretary($doctor, 5.0);
$payment = $this->makeOnlinePayment($doctor);
$this->commission()->processAppointment($payment, null, null, $doctor->getId());
$breakdown = static::getContainer()->get(FinancialBreakdownRepository::class)->findOneBy(['payment' => $payment]);
self::assertNotNull($breakdown, 'نوبتِ بدون نماینده هم باید تفکیک مالی بسازد');
self::assertSame(500_000, $breakdown->getSecretaryShareRials()); // ۵٪ از ۱۰,۰۰۰,۰۰۰
self::assertSame(self::GROSS - 500_000, $breakdown->toArray()['system_share_rials']);
self::assertSame(500_000, $this->earnings()->sumFor($relation->getSecretary()));
self::assertSame(
500_000,
static::getContainer()->get(SettlementRepository::class)->getWalletBalance($relation->getSecretary()),
);
}
public function testDisabledOrZeroPercentEarnsNothing(): void
{
$doctor = $this->makeDoctor();
$off = $this->makeSecretary($doctor, 5.0, enabled: false);
$zero = $this->makeSecretary($doctor, 0.0);
$payment = $this->makeOnlinePayment($doctor);
$this->commission()->processAppointment($payment, null, null, $doctor->getId());
self::assertFalse(static::getContainer()->get(FinancialBreakdownRepository::class)->existsForPayment($payment));
self::assertSame(0, $this->earnings()->sumFor($off->getSecretary()));
self::assertSame(0, $this->earnings()->sumFor($zero->getSecretary()));
}
public function testEachSecretaryGetsItsOwnPercent(): void
{
$doctor = $this->makeDoctor();
$first = $this->makeSecretary($doctor, 5.0);
$second = $this->makeSecretary($doctor, 10.0);
$payment = $this->makeOnlinePayment($doctor);
$this->commission()->processAppointment($payment, null, null, $doctor->getId());
self::assertSame(500_000, $this->earnings()->sumFor($first->getSecretary()));
self::assertSame(1_000_000, $this->earnings()->sumFor($second->getSecretary()));
}
public function testPaymentIsNeverSplitTwice(): void
{
$doctor = $this->makeDoctor();
$relation = $this->makeSecretary($doctor, 5.0);
$payment = $this->makeOnlinePayment($doctor);
$this->commission()->processAppointment($payment, null, null, $doctor->getId());
$this->commission()->processAppointment($payment, null, null, $doctor->getId());
self::assertSame(500_000, $this->earnings()->sumFor($relation->getSecretary()));
self::assertSame(1, $this->earnings()->countFor($relation->getSecretary()));
}
/** مجموع درصدها > ۱۰۰ → کلیپ می‌شود و سهم سیستم منفی نمی‌شود. */
public function testSharesAreClippedSoTheSystemShareStaysPositive(): void
{
$doctor = $this->makeDoctor();
$a = $this->makeSecretary($doctor, 80.0);
$b = $this->makeSecretary($doctor, 60.0);
$payment = $this->makeOnlinePayment($doctor);
$this->commission()->processAppointment($payment, null, null, $doctor->getId());
$breakdown = static::getContainer()->get(FinancialBreakdownRepository::class)->findOneBy(['payment' => $payment]);
self::assertGreaterThanOrEqual(0, $breakdown->toArray()['system_share_rials']);
self::assertSame(self::GROSS, $breakdown->getSecretaryShareRials() + $breakdown->toArray()['system_share_rials']);
self::assertGreaterThan(0, $this->earnings()->sumFor($a->getSecretary()));
self::assertGreaterThan(0, $this->earnings()->sumFor($b->getSecretary()));
}
public function testClinicAppointmentCreditsTheClinicSecretaries(): void
{
$doctor = $this->makeDoctor();
$owner = $this->createUser(['ROLE_USER', 'ROLE_CLINIC']);
$clinic = new Clinic($owner);
$clinic->setName('کلینیک تست سهم منشی');
$clinic->getDoctors()->add($doctor);
$this->em->persist($clinic);
$this->em->flush();
$clinicSecretary = $this->makeSecretary($doctor, 7.0, clinic: $clinic);
$officeSecretary = $this->makeSecretary($doctor, 5.0); // مطب شخصی — نباید سهم بگیرد
$payment = $this->makeOnlinePayment($doctor, $clinic);
$this->commission()->processAppointment($payment, null, null, $doctor->getId());
self::assertSame(700_000, $this->earnings()->sumFor($clinicSecretary->getSecretary()));
self::assertSame(0, $this->earnings()->sumFor($officeSecretary->getSecretary()));
}
/** سهم از «خالص» گرفته می‌شود: با مالیات و هزینهٔ پیامک، مبنا کمتر است. */
public function testShareIsCalculatedOnTheNetAmount(): void
{
$config = static::getContainer()->get(SiteConfigRepository::class);
$config->set('sms_panel_fee_rials', '1000000');
$config->set('tax_enabled', '1');
$config->set('tax_percent', '10');
$doctor = $this->makeDoctor();
$relation = $this->makeSecretary($doctor, 10.0);
$payment = $this->makeOnlinePayment($doctor);
$this->commission()->processAppointment($payment, null, null, $doctor->getId());
// ۱۰,۰۰۰,۰۰۰ − ۱,۰۰۰,۰۰۰ = ۹,۰۰۰,۰۰۰ ؛ مالیات = 9,000,000×10/110 = 818,182
// خالص = 8,181,818 ؛ سهم ۱۰٪ = 818,182
self::assertSame(818_182, $this->earnings()->sumFor($relation->getSecretary()));
}
}