Phase 2 of the tenant-marking series. appointments, weekly_schedules and date_overrides kept their environment implicit in a nullable clinic_id, so every query that wanted "this environment's rows" had to rebuild clinic_id IS NULL ? doctor : clinic itself. The two calendar tables also depended on a MariaDB-only generated column, clinic_key = IFNULL(clinic_id, 0), purely to make a unique key work across NULLs. All three now carry the (entity_type, entity_id) pair that service_sections, patient_records and clinic_staff already use, via a shared TenantOwnedTrait. The pair is a deliberate denormalisation of clinic_id/doctor_id: the automatic tenant filter and the tenant-leading indexes both need a real column, and neither can be built on an IF() expression. - Unique keys keep doctor_id alongside the pair. A clinic has several doctors and each has their own schedule, so (entity_type, entity_id) alone would reject the second doctor. - clinic_key is gone from both calendar tables. - appointments gained tenant-leading indexes; EXPLAIN on the panel's list query now picks idx_appointments_tenant_slot. Deliberately unchanged, both with the reason already recorded in the code: active_slot_key stays keyed on doctor + slot, since adding the environment would let one doctor be booked in their own practice and a clinic at the same moment. holidays keeps its nullable clinic_id, where NULL means "every environment" rather than "personal practice" — a meaning the pair cannot carry. The migration adds the columns nullable, backfills, aborts if any row is left without an owner, and only then tightens to NOT NULL. It creates each replacement unique index before dropping the old one, so the tables are never left unprotected — MariaDB commits implicitly on DDL, so ordering is the only safety net. It runs its statements through the connection rather than addSql() because the guard has to sit between the backfill and the NOT NULL change. Columns are NOT NULL with no default on purpose: a construction site that forgets assignTenant() fails at flush instead of silently writing entity_id 0, which the phase 4 filter would then hide from everyone. Verified on the dev database: 0 rows without a tenant, 0 personal bookings mismatched against their doctor, 0 clinic bookings mismatched against their clinic. Tests: 819 passing (813 + 6 new in BookingTenantTest). PHPStan clean on every changed file. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
202 lines
8.6 KiB
PHP
202 lines
8.6 KiB
PHP
<?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 !== null ? DoctorSecretary::OWNER_CLINIC : DoctorSecretary::OWNER_DOCTOR,
|
||
$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()));
|
||
}
|
||
}
|