feat(discount): apply a discount rule to a session with audit

Extend PatientService::applyDiscount to carry the source rule id/label and
add applyDiscountRule, which computes the rial amount via the engine
(DiscountEngine::computeForRule now public) and records the rule for audit.
updateSession accepts discount_rule_uuid (owner-scoped, takes precedence over
the manual discount; ''/null clears). Verified end-to-end.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
hamed
2026-07-17 11:55:16 +03:30
co-authored by Claude Fable 5
parent 6cc42c941e
commit b3b1dad832
4 changed files with 62 additions and 15 deletions
+16 -2
View File
@@ -60,6 +60,7 @@ class PatientController extends BaseController
private readonly \App\Settlement\Repository\WalletTransactionRepository $walletRepo,
private readonly \App\Settlement\Repository\SettlementRepository $settlementRepo,
private readonly \App\Settlement\Service\WalletService $walletService,
private readonly \App\Discount\Repository\DiscountRuleRepository $discountRuleRepo,
private readonly LoggerInterface $logger,
) {}
@@ -1046,8 +1047,21 @@ class PatientController extends BaseController
if (isset($data['notes'])) { $session->setNotes($data['notes']); }
// تخفیف تسویه: discount_type = percent|fixed|null (null = حذف تخفیف)
if (array_key_exists('discount_type', $data)) {
// تخفیف بر اساس قانون (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);
} 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);
}
} 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));
}