Files
clinicpro/tests/Patient/PatientWalletSessionSettleTest.php
T
hamedandClaude Opus 4.8 ef97b2b249 feat: unify wallet with real payment methods, full transaction transparency, pay-session-from-wallet
Address wallet feedback: use the clinic's real payment infrastructure,
redesign the tab to match the admin panel, and make every wallet movement
fully auditable.

Backend:
- WalletTransaction: add createdBy (acting user) + createdByName, payment_method,
  reference, status; toArray exposes them (migration Version20260716083939).
- WalletService (Settlement): balance/charge/withdraw + settleSessionFromWallet,
  records actor/method/reason; insufficient balance throws ERR_WALLET_INSUFFICIENT.
- PatientController: charge/withdraw delegate to WalletService and accept
  payment_method/reference; PATCH /session/{uuid} with payment_method=wallet
  debits the patient's final share from the wallet (reference=session:{uuid}).
- docs/api/patient.md updated.

Frontend:
- Wallet modal redesigned to panel style (no gradient); payment method now uses
  the clinic's real bank accounts + POS devices (usePaymentMethods) plus cash.
- Wallet tab: panel balance card + DataTable ledger with columns مبلغ/نوع/روش/
  دلیل/ثبت‌کننده/تاریخ/ساعت/وضعیت + همه/واریزی/برداشت filters.
- Session card «تکمیل پرداخت» opens a payment-method chooser incl. کیف پول.

Tests: backend transparency + session-from-wallet (success/insufficient/cash);
frontend modal (real methods, toman→rials) + wallet tab + settle chooser.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-16 12:22:37 +03:30

103 lines
4.2 KiB
PHP

<?php
namespace App\Tests\Patient;
use App\Doctor\Entity\Doctor;
use App\Patient\Entity\PatientRecord;
use App\Patient\Entity\PatientSession;
use App\Settlement\Entity\WalletTransaction;
use App\Tests\ApiTestCase;
/**
* تسویهٔ یک مراجعه (PatientSession) از کیف پول: PATCH /session/{uuid} با
* payment_method=wallet مبلغِ نهایی را به‌صورت debit از موجودی کسر می‌کند و در
* صورتِ ناکافی بودن موجودی رد می‌شود.
*/
class PatientWalletSessionSettleTest extends ApiTestCase
{
/** @return array{0: \App\Auth\Entity\User, 1: PatientRecord, 2: \App\Auth\Entity\User} */
private function recordFor(): array
{
$owner = $this->createUser(['ROLE_DOCTOR']);
$doctor = new Doctor($owner, 'دکتر');
$this->em->persist($doctor);
$this->em->flush();
$patient = $this->createUser(['ROLE_USER']);
$record = new PatientRecord('doctor', $doctor->getId(), $patient, 'doctor', $doctor->getId());
$this->em->persist($record);
$this->em->flush();
return [$owner, $record, $patient];
}
private function sessionFor(PatientRecord $record, int $finalPriceRials): PatientSession
{
$session = new PatientSession($record);
$session->setFinalPriceRials($finalPriceRials);
$this->em->persist($session);
$this->em->flush();
return $session;
}
public function testSettleSessionFromWalletDebitsBalanceAndMarksPaid(): void
{
[$owner, $record, $patient] = $this->recordFor();
$this->em->persist(new WalletTransaction($patient, 1_000_000, 'credit', 1_000_000));
$this->em->flush();
$session = $this->sessionFor($record, 400_000);
$res = $this->authJson('PATCH', '/api/v1/session/' . $session->getUuid(), $owner, [
'payment_method' => 'wallet',
]);
self::assertSame(200, $this->responseCode());
self::assertSame('wallet', $res['data']['payment_method']);
self::assertTrue($res['data']['is_paid']);
// موجودی از ۱٬۰۰۰٬۰۰۰ به ۶۰۰٬۰۰۰ رسید و یک تراکنشِ debitِ گره‌خورده به مراجعه ثبت شد.
$wallet = $this->authJson('GET', '/api/v1/patient/' . $record->getUuid() . '/wallet', $owner);
self::assertSame(600_000, $wallet['data']['balance_rials']);
$debit = $wallet['data']['recent_transactions'][0];
self::assertSame('debit', $debit['type']);
self::assertSame('wallet', $debit['payment_method']);
self::assertSame('session:' . $session->getUuid(), $debit['reference']);
self::assertSame($owner->getMobileNumber(), $debit['created_by_name']);
}
public function testSettleSessionFromWalletRejectedWhenInsufficient(): void
{
[$owner, $record, $patient] = $this->recordFor();
$this->em->persist(new WalletTransaction($patient, 100_000, 'credit', 100_000));
$this->em->flush();
$session = $this->sessionFor($record, 400_000);
$res = $this->authJson('PATCH', '/api/v1/session/' . $session->getUuid(), $owner, [
'payment_method' => 'wallet',
]);
self::assertSame(422, $this->responseCode());
self::assertSame('ERR_WALLET_INSUFFICIENT', $res['errors'][0]['code']);
// مراجعه تسویه نشد و موجودی دست‌نخورده ماند.
$wallet = $this->authJson('GET', '/api/v1/patient/' . $record->getUuid() . '/wallet', $owner);
self::assertSame(100_000, $wallet['data']['balance_rials']);
}
public function testSettleSessionWithCashDoesNotTouchWallet(): void
{
[$owner, $record, $patient] = $this->recordFor();
$this->em->persist(new WalletTransaction($patient, 500_000, 'credit', 500_000));
$this->em->flush();
$session = $this->sessionFor($record, 300_000);
$this->authJson('PATCH', '/api/v1/session/' . $session->getUuid(), $owner, ['payment_method' => 'cash']);
self::assertSame(200, $this->responseCode());
$wallet = $this->authJson('GET', '/api/v1/patient/' . $record->getUuid() . '/wallet', $owner);
self::assertSame(500_000, $wallet['data']['balance_rials']);
}
}