feat(patient): session edit + payment PATCH/DELETE + audit-log endpoints

updateSession now accepts services/consumables/visit_price/insurance and calls
updateSessionServices; discount paths pass the actor for audit. Add
PATCH/DELETE /session/{uuid}/payments/{paymentUuid} and GET
/session/{uuid}/audit-log (owner-scoped). Inject the payment + audit repos.
Verified end-to-end (edit visit price, payment edit-exceeds guard, delete +
recompute, audit trail). Docs updated.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
hamed
2026-07-17 15:14:49 +03:30
co-authored by Claude Fable 5
parent cd1c9529cb
commit 7da7d968b9
2 changed files with 115 additions and 3 deletions
+69 -3
View File
@@ -61,6 +61,8 @@ class PatientController extends BaseController
private readonly \App\Settlement\Repository\SettlementRepository $settlementRepo,
private readonly \App\Settlement\Service\WalletService $walletService,
private readonly \App\Discount\Repository\DiscountRuleRepository $discountRuleRepo,
private readonly \App\Patient\Repository\SessionPaymentRepository $sessionPaymentRepo,
private readonly \App\Patient\Repository\SessionAuditLogRepository $sessionAuditRepo,
private readonly LoggerInterface $logger,
) {}
@@ -1052,23 +1054,30 @@ class PatientController extends BaseController
// آرشیو نرم: مخفی‌سازی مراجعه‌ی اشتباه بدون حذف سابقه.
if (array_key_exists('archived', $data)) { $session->setArchived((bool) $data['archived']); }
// ویرایش سرویس‌ها/کالاها/قیمت ویزیت/بیمه — با بازمحاسبه و ثبت تاریخچه.
if (array_key_exists('services', $data) || array_key_exists('consumables', $data)
|| array_key_exists('visit_price_rials', $data) || array_key_exists('insurance_base_id', $data)
|| array_key_exists('base_insurance_discount_percent', $data)) {
$this->patientService->updateSessionServices($session, $data, $entityType, $entityId, $user);
}
// تخفیف بر اساس قانون (discount_rule_uuid): مقدار از خود قانون، با ثبت منبع.
// '' یا null → حذف تخفیف. اولویت بر تخفیف دستی.
if (array_key_exists('discount_rule_uuid', $data)) {
$ruleUuid = $data['discount_rule_uuid'];
if ($ruleUuid === null || $ruleUuid === '') {
$this->patientService->applyDiscount($session, null, 0);
$this->patientService->applyDiscount($session, null, 0, null, null, $user);
} else {
$rule = $this->discountRuleRepo->findByUuidForOwner((string) $ruleUuid, $entityType, $entityId);
if ($rule === null) {
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'قانون تخفیف یافت نشد', 404, 'discount_rule_uuid');
}
$this->patientService->applyDiscountRule($session, $rule);
$this->patientService->applyDiscountRule($session, $rule, $user);
}
} elseif (array_key_exists('discount_type', $data)) {
// تخفیف دستی: discount_type = percent|fixed|null (null = حذف تخفیف)
$type = $data['discount_type'] !== null ? (string) $data['discount_type'] : null;
$this->patientService->applyDiscount($session, $type, (int) ($data['discount_value'] ?? 0));
$this->patientService->applyDiscount($session, $type, (int) ($data['discount_value'] ?? 0), null, null, $user);
}
if (isset($data['paid_at'])) { $session->setPaidAt((int) $data['paid_at']); }
@@ -1121,6 +1130,63 @@ class PatientController extends BaseController
return $this->success($this->sessionWithBilling($session), 201);
}
/** ویرایش یک پرداخت ثبت‌شده. body: { method?, amount_rials?, paid_at? }. */
#[Route('/api/v1/session/{uuid}/payments/{paymentUuid}', methods: ['PATCH'])]
public function updateSessionPayment(string $uuid, string $paymentUuid, Request $request, #[CurrentUser] User $user): JsonResponse
{
[$entityType, $entityId] = $this->resolveEntity($user);
$this->assertPatientGate($entityType, $entityId);
$session = $this->sessionRepo->findByUuid($uuid);
if ($session === null || !$this->ownsRecord($session->getRecord(), $entityType, $entityId)) {
return $this->error(ErrorCodes::ERR_SESSION_NOT_FOUND, ErrorCodes::message(ErrorCodes::ERR_SESSION_NOT_FOUND), 404);
}
$payment = $this->sessionPaymentRepo->findByUuid($paymentUuid);
if ($payment === null || $payment->getSession()->getUuid() !== $uuid) {
return $this->error(ErrorCodes::ERR_SESSION_PAYMENT_INVALID, 'پرداخت یافت نشد', 404, 'paymentUuid');
}
$this->patientService->updatePayment($payment, json_decode($request->getContent(), true) ?? [], $user);
return $this->success($this->sessionWithBilling($session));
}
/** حذف یک پرداخت ثبت‌شده. */
#[Route('/api/v1/session/{uuid}/payments/{paymentUuid}', methods: ['DELETE'])]
public function deleteSessionPayment(string $uuid, string $paymentUuid, #[CurrentUser] User $user): JsonResponse
{
[$entityType, $entityId] = $this->resolveEntity($user);
$this->assertPatientGate($entityType, $entityId);
$session = $this->sessionRepo->findByUuid($uuid);
if ($session === null || !$this->ownsRecord($session->getRecord(), $entityType, $entityId)) {
return $this->error(ErrorCodes::ERR_SESSION_NOT_FOUND, ErrorCodes::message(ErrorCodes::ERR_SESSION_NOT_FOUND), 404);
}
$payment = $this->sessionPaymentRepo->findByUuid($paymentUuid);
if ($payment === null || $payment->getSession()->getUuid() !== $uuid) {
return $this->error(ErrorCodes::ERR_SESSION_PAYMENT_INVALID, 'پرداخت یافت نشد', 404, 'paymentUuid');
}
$this->patientService->deletePayment($payment, $user);
return $this->success($this->sessionWithBilling($session));
}
/** تاریخچه‌ی تغییرات مالی/خدماتی مراجعه (Audit Log). */
#[Route('/api/v1/session/{uuid}/audit-log', methods: ['GET'])]
public function sessionAuditLog(string $uuid, #[CurrentUser] User $user): JsonResponse
{
[$entityType, $entityId] = $this->resolveEntity($user);
$this->assertPatientGate($entityType, $entityId);
$session = $this->sessionRepo->findByUuid($uuid);
if ($session === null || !$this->ownsRecord($session->getRecord(), $entityType, $entityId)) {
return $this->error(ErrorCodes::ERR_SESSION_NOT_FOUND, ErrorCodes::message(ErrorCodes::ERR_SESSION_NOT_FOUND), 404);
}
return $this->success($this->sessionAuditRepo->findBySessionUuid($uuid));
}
private function resolveEntity(User $user): array
{
if ($user->hasRole('ROLE_DOCTOR')) {