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>
This commit is contained in:
@@ -12,6 +12,8 @@ use App\Payment\Repository\PaymentRepository;
|
||||
use App\Payment\Service\PaymentManager;
|
||||
use App\Secretary\Security\SecretaryAccessChecker;
|
||||
use App\Shared\Constant\ErrorCodes;
|
||||
use App\Shared\Context\EntityContext;
|
||||
use App\Shared\Context\EntityContextResolver;
|
||||
use App\Shared\Controller\BaseController;
|
||||
use OpenApi\Attributes as OA;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
@@ -38,6 +40,7 @@ class PaymentController extends BaseController
|
||||
private readonly SiteConfigRepository $configRepo,
|
||||
private readonly SecretaryAccessChecker $secretaryAccess,
|
||||
private readonly \App\Clinic\Security\ClinicDoctorAccessChecker $clinicDoctorAccess,
|
||||
private readonly EntityContextResolver $contextResolver,
|
||||
private readonly string $appBaseUrl,
|
||||
private readonly string $allowedFrontendHosts = '',
|
||||
) {}
|
||||
@@ -118,6 +121,7 @@ class PaymentController extends BaseController
|
||||
$feeRials = (int) $this->configRepo->get('appointment_fee_rials');
|
||||
$payment = new Payment($user, $feeRials, $gatewayName, Payment::TYPE_APPOINTMENT, $frontendAddress);
|
||||
$payment->setAppointment($appointment);
|
||||
$payment->assignTenant(EntityContext::forBooking($appointment->getDoctor(), $appointment->getClinic()));
|
||||
$this->paymentRepo->save($payment);
|
||||
|
||||
// مرورگر به این endpoint بکاند میرود؛ آنجا صلاحیت نهایی + ارتباط با بانک
|
||||
@@ -201,6 +205,7 @@ class PaymentController extends BaseController
|
||||
$feeRials = (int) $this->configRepo->get('appointment_fee_rials');
|
||||
$payment = new Payment($appointment->getUser(), $feeRials, $gatewayName, Payment::TYPE_APPOINTMENT, $return);
|
||||
$payment->setAppointment($appointment);
|
||||
$payment->assignTenant(EntityContext::forBooking($appointment->getDoctor(), $appointment->getClinic()));
|
||||
$this->paymentRepo->save($payment);
|
||||
return $payment;
|
||||
}
|
||||
@@ -405,8 +410,16 @@ class PaymentController extends BaseController
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'درگاه پرداخت نامعتبر یا غیرفعال است', 422, 'gateway');
|
||||
}
|
||||
|
||||
// اشتراک به حساب محیطی مینشیند که کاربر صاحبش است؛ همان مرجعی که
|
||||
// PaymentManager::handleSubscriptionActivation خودِ اشتراک را با آن میسازد.
|
||||
$owner = $this->contextResolver->ownedEntity($user);
|
||||
if (!$owner->isResolved()) {
|
||||
return $this->error(ErrorCodes::ERR_PAYMENT_004, ErrorCodes::message(ErrorCodes::ERR_PAYMENT_004), 422);
|
||||
}
|
||||
|
||||
$periodUuid = trim($data['period_uuid'] ?? '');
|
||||
$payment = new Payment($user, $amountRials, $gatewayName, Payment::TYPE_SUBSCRIPTION, $frontendAddress);
|
||||
$payment->assignTenant($owner);
|
||||
if ($periodUuid !== '') {
|
||||
$payment->setMetadata(['period_uuid' => $periodUuid]);
|
||||
}
|
||||
|
||||
@@ -4,16 +4,25 @@ namespace App\Payment\Entity;
|
||||
|
||||
use App\Appointment\Entity\Appointment;
|
||||
use App\Auth\Entity\User;
|
||||
use App\Shared\Tenant\TenantOwnedTrait;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use App\Payment\Repository\PaymentRepository;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
/**
|
||||
* پرداخت همیشه به محیطِ گیرنده تعلق دارد، نه به پرداختکننده: نوبت → محیط همان
|
||||
* نوبت، اشتراک و شارژ پیامک → محیطی که برایش خریداری شده. بیمار همچنان پرداخت
|
||||
* خودش را میبیند چون TenantFilter برای کاربرِ بیمحیط خاموش میماند.
|
||||
*/
|
||||
#[ORM\Entity(repositoryClass: PaymentRepository::class)]
|
||||
#[ORM\Table(name: 'payments')]
|
||||
#[ORM\Index(columns: ['order_id'], name: 'idx_payments_order')]
|
||||
#[ORM\Index(columns: ['user_id'], name: 'idx_payments_user')]
|
||||
#[ORM\Index(columns: ['entity_type', 'entity_id', 'created_at'], name: 'idx_payments_entity_date')]
|
||||
class Payment
|
||||
{
|
||||
use TenantOwnedTrait;
|
||||
|
||||
public const STATUS_PENDING = 'pending';
|
||||
public const STATUS_SUCCESS = 'success';
|
||||
public const STATUS_FAILED = 'failed';
|
||||
|
||||
@@ -14,6 +14,7 @@ use App\Payment\Repository\PaymentLogRepository;
|
||||
use App\Payment\Repository\PaymentRepository;
|
||||
use App\Representation\Service\JalaliDateService;
|
||||
use App\Settlement\Service\CommissionService;
|
||||
use App\Shared\Context\EntityContext;
|
||||
use App\Sms\Entity\SmsLog;
|
||||
use App\Sms\Service\SmsService;
|
||||
use App\Sms\Service\SmsWalletService;
|
||||
@@ -346,20 +347,27 @@ final class PaymentManager
|
||||
return;
|
||||
}
|
||||
|
||||
$user = $payment->getUser();
|
||||
$doctor = $this->doctorRepo->findByUser($user);
|
||||
// محیط از خودِ پرداخت خوانده میشود، نه دوباره از کاربر: اشتراک باید دقیقاً
|
||||
// روی همان محیطی بنشیند که هنگام خرید پرداختش ثبت شد، حتی اگر کاربر بین
|
||||
// خرید و بازگشت از درگاه محیط تازهای پیدا کرده باشد.
|
||||
$bookingRepId = $this->bookingRepresentationIdFor($payment);
|
||||
$entityId = $payment->getEntityId();
|
||||
|
||||
if ($payment->getEntityType() === EntityContext::TYPE_DOCTOR) {
|
||||
$doctor = $this->doctorRepo->find($entityId);
|
||||
if ($doctor === null) {
|
||||
return;
|
||||
}
|
||||
$this->subscriptionService->createFromPayment($payment, 'doctor', $entityId, $periodUuid);
|
||||
$this->commissionService->processSubscription($payment, $doctor->getRepresentationId(), $bookingRepId, $entityId, null);
|
||||
|
||||
if ($doctor !== null) {
|
||||
$this->subscriptionService->createFromPayment($payment, 'doctor', $doctor->getId(), $periodUuid);
|
||||
$this->commissionService->processSubscription($payment, $doctor->getRepresentationId(), $bookingRepId, $doctor->getId(), null);
|
||||
return;
|
||||
}
|
||||
|
||||
$clinic = $this->clinicRepo->findByUser($user);
|
||||
$clinic = $this->clinicRepo->find($entityId);
|
||||
if ($clinic !== null) {
|
||||
$this->subscriptionService->createFromPayment($payment, 'clinic', $clinic->getId(), $periodUuid);
|
||||
$this->commissionService->processSubscription($payment, $clinic->getRepresentationId(), $bookingRepId, null, $clinic->getId());
|
||||
$this->subscriptionService->createFromPayment($payment, 'clinic', $entityId, $periodUuid);
|
||||
$this->commissionService->processSubscription($payment, $clinic->getRepresentationId(), $bookingRepId, null, $entityId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user