feat(insurance): bill an appointment with a chosen service kind and insurance
An appointment can now carry the insurance it is billed with: the service kind (outpatient/inpatient) and the basic insurance. Confirming it no longer hands the whole amount to the patient — the visit is split through BillingCalculator with the coverage percent of that service kind, and the choice travels to the encounter and the invoice built from it. The enabled service kinds are a tenant-wide setting (all of that tenant's insurances share it), so a tenant covering only one kind is never asked which one: the panel resolves it the same way the server does. - add tenant_service_category_settings + TenantServiceCategoryService, exposed on the existing insurance-pricing endpoint (service_categories, default_service_category); at least one kind must stay enabled - add appointments.insurance_service_category / insurance_base_id with AppointmentInsuranceService validating them against the tenant's own settings and active contracts (basic only), accepted by PATCH and by confirm - snapshot the kind on patient_sessions and invoices; the visit's coverage rule is resolved per kind (services keep using their own ServiceItem.service_category) - lib/insuranceShares becomes the single client-side mirror of BillingCalculator, shared by the confirm modal, the appointment edit page and the session form - surface the selection: confirm modal (with live shares), turns timeline chip, appointment edit page, patient record service card and invoice summary - the session form shows the insurance block whenever the tenant has an active contract and prefills the patient's own insurance, so it can be changed - fix: the confirm modal showed a zero visit price when the appointment had none — it now falls back to the tenant's free-visit price like the server - fix: useServiceCategories read one level too shallow, so Persian labels never arrived and raw enum keys leaked into the contract summary - fix: BlogsPage test asserted the public blogs endpoint after the page moved to the admin one Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -18,6 +18,7 @@ use App\Insurance\Repository\TenantInsuranceRepository;
|
||||
use App\Insurance\Repository\TenantServiceCoverageRepository;
|
||||
use App\Insurance\Service\InsuranceCoverageDefaultService;
|
||||
use App\Insurance\Service\TenantInsuranceService;
|
||||
use App\Insurance\Service\TenantServiceCategoryService;
|
||||
use App\Shared\Constant\ErrorCodes;
|
||||
use App\Secretary\Security\SecretaryAccessChecker;
|
||||
use App\Shared\Controller\BaseController;
|
||||
@@ -43,6 +44,7 @@ class InsuranceController extends BaseController
|
||||
private readonly TenantServiceCoverageRepository $serviceCoverageRepo,
|
||||
private readonly TenantInsuranceService $tenantInsuranceService,
|
||||
private readonly InsuranceCoverageDefaultService $coverageDefaults,
|
||||
private readonly TenantServiceCategoryService $serviceCategories,
|
||||
private readonly ServiceItemRepository $serviceItemRepo,
|
||||
private readonly FileValidatorService $fileValidator,
|
||||
private readonly \App\Clinic\Security\ClinicDoctorPermissionChecker $permChecker,
|
||||
@@ -350,11 +352,15 @@ class InsuranceController extends BaseController
|
||||
}, $catalog);
|
||||
|
||||
return [
|
||||
'entity_type' => $entityType,
|
||||
'entity_id' => $entityId,
|
||||
'free_visit_price_rials' => $freeVisitPriceRials,
|
||||
'require_visit_price' => $requireVisitPrice,
|
||||
'insurances' => $insurances,
|
||||
'entity_type' => $entityType,
|
||||
'entity_id' => $entityId,
|
||||
'free_visit_price_rials' => $freeVisitPriceRials,
|
||||
'require_visit_price' => $requireVisitPrice,
|
||||
'insurances' => $insurances,
|
||||
// نوع خدماتِ بیمهایِ این tenant — سراسری برای همهٔ بیمهها.
|
||||
'service_categories' => $this->serviceCategories->settingsRows($entityType, $entityId),
|
||||
// null یعنی چند نوع فعال است و کاربر باید سرِ پذیرش انتخاب کند.
|
||||
'default_service_category' => $this->serviceCategories->defaultCategory($entityType, $entityId)?->value,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -411,6 +417,10 @@ class InsuranceController extends BaseController
|
||||
|
||||
$this->pricingRepo->getEntityManager()->flush();
|
||||
|
||||
if (array_key_exists('service_categories', $data)) {
|
||||
$this->serviceCategories->save($entityType, $entityId, (array) ($data['service_categories'] ?? []));
|
||||
}
|
||||
|
||||
return $this->success($this->pricingPayload($entityType, $entityId));
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Insurance\Entity;
|
||||
|
||||
use App\Insurance\Enum\ServiceCategory;
|
||||
use App\Insurance\Repository\TenantServiceCategorySettingRepository;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
/**
|
||||
* Which kinds of service a doctor/clinic covers with insurance at all. This is a
|
||||
* tenant-wide switch shared by every insurance of that tenant — not a per-insurance
|
||||
* setting. A missing row reads as enabled, so tenants created before this setting
|
||||
* keep both kinds available.
|
||||
*/
|
||||
#[ORM\Entity(repositoryClass: TenantServiceCategorySettingRepository::class)]
|
||||
#[ORM\Table(name: 'tenant_service_category_settings')]
|
||||
#[ORM\UniqueConstraint(name: 'uniq_tenant_service_category_setting', columns: ['entity_type', 'entity_id', 'service_category'])]
|
||||
class TenantServiceCategorySetting
|
||||
{
|
||||
public const TYPE_DOCTOR = 'doctor';
|
||||
public const TYPE_CLINIC = 'clinic';
|
||||
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(name: 'entity_type', type: 'string', length: 10)]
|
||||
private string $entityType;
|
||||
|
||||
#[ORM\Column(name: 'entity_id', type: 'integer')]
|
||||
private int $entityId;
|
||||
|
||||
#[ORM\Column(name: 'service_category', type: 'string', length: 30, enumType: ServiceCategory::class)]
|
||||
private ServiceCategory $serviceCategory;
|
||||
|
||||
#[ORM\Column(type: 'boolean', options: ['default' => true])]
|
||||
private bool $enabled = true;
|
||||
|
||||
#[ORM\Column(name: 'updated_at', type: 'integer')]
|
||||
private int $updatedAt;
|
||||
|
||||
public function __construct(string $entityType, int $entityId, ServiceCategory $serviceCategory, bool $enabled = true)
|
||||
{
|
||||
$this->entityType = $entityType;
|
||||
$this->entityId = $entityId;
|
||||
$this->serviceCategory = $serviceCategory;
|
||||
$this->enabled = $enabled;
|
||||
$this->updatedAt = time();
|
||||
}
|
||||
|
||||
public function getId(): ?int { return $this->id; }
|
||||
public function getEntityType(): string { return $this->entityType; }
|
||||
public function getEntityId(): int { return $this->entityId; }
|
||||
public function getServiceCategory(): ServiceCategory { return $this->serviceCategory; }
|
||||
public function isEnabled(): bool { return $this->enabled; }
|
||||
|
||||
public function setEnabled(bool $v): self
|
||||
{
|
||||
$this->enabled = $v;
|
||||
$this->updatedAt = time();
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'key' => $this->serviceCategory->value,
|
||||
'label' => $this->serviceCategory->label(),
|
||||
'enabled' => $this->enabled,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Insurance\Repository;
|
||||
|
||||
use App\Insurance\Entity\TenantServiceCategorySetting;
|
||||
use App\Insurance\Enum\ServiceCategory;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
class TenantServiceCategorySettingRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, TenantServiceCategorySetting::class);
|
||||
}
|
||||
|
||||
/** @return array<string, bool> service_category => enabled, only stored rows */
|
||||
public function enabledMapFor(string $entityType, int $entityId): array
|
||||
{
|
||||
$rows = $this->findBy(['entityType' => $entityType, 'entityId' => $entityId]);
|
||||
|
||||
$map = [];
|
||||
foreach ($rows as $row) {
|
||||
$map[$row->getServiceCategory()->value] = $row->isEnabled();
|
||||
}
|
||||
|
||||
return $map;
|
||||
}
|
||||
|
||||
public function findOneFor(string $entityType, int $entityId, ServiceCategory $category): ?TenantServiceCategorySetting
|
||||
{
|
||||
return $this->findOneBy([
|
||||
'entityType' => $entityType,
|
||||
'entityId' => $entityId,
|
||||
'serviceCategory' => $category,
|
||||
]);
|
||||
}
|
||||
|
||||
public function save(TenantServiceCategorySetting $entity, bool $flush = true): void
|
||||
{
|
||||
$this->getEntityManager()->persist($entity);
|
||||
if ($flush) {
|
||||
$this->getEntityManager()->flush();
|
||||
}
|
||||
}
|
||||
|
||||
public function flush(): void
|
||||
{
|
||||
$this->getEntityManager()->flush();
|
||||
}
|
||||
}
|
||||
@@ -216,12 +216,16 @@ class TenantInsuranceService
|
||||
}
|
||||
|
||||
/**
|
||||
* قانون پوشش ویزیت برای tenant جاری (برای BillingCalculator).
|
||||
* ویزیت خدمتِ سرپایی است، پس درصد همان نوع خدمت resolve میشود.
|
||||
* قانون پوشش ویزیت برای tenant جاری (برای BillingCalculator). درصد بر پایهٔ نوع
|
||||
* خدمتِ دادهشده resolve میشود؛ پیشفرض سرپایی است چون ویزیت آیتم سرویس نیست.
|
||||
* اگر قرارداد فعالی نباشد، notCovered برمیگردد.
|
||||
*/
|
||||
public function coverageRule(string $entityType, int $entityId, ?int $insuranceId): CoverageRule
|
||||
{
|
||||
public function coverageRule(
|
||||
string $entityType,
|
||||
int $entityId,
|
||||
?int $insuranceId,
|
||||
ServiceCategory $category = ServiceCategory::Outpatient,
|
||||
): CoverageRule {
|
||||
if ($insuranceId === null) {
|
||||
return CoverageRule::notCovered();
|
||||
}
|
||||
@@ -231,7 +235,7 @@ class TenantInsuranceService
|
||||
return CoverageRule::notCovered();
|
||||
}
|
||||
|
||||
return $this->buildRule($contract, ServiceCategory::Outpatient, null);
|
||||
return $this->buildRule($contract, $category, null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace App\Insurance\Service;
|
||||
|
||||
use App\Insurance\Entity\TenantServiceCategorySetting;
|
||||
use App\Insurance\Enum\ServiceCategory;
|
||||
use App\Insurance\Repository\TenantServiceCategorySettingRepository;
|
||||
use App\Shared\Constant\ErrorCodes;
|
||||
use App\Shared\Exception\AppException;
|
||||
|
||||
/**
|
||||
* The service kinds a tenant covers with insurance. One switch per kind, shared by
|
||||
* all of that tenant's insurances. A kind with no stored row counts as enabled.
|
||||
*/
|
||||
class TenantServiceCategoryService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly TenantServiceCategorySettingRepository $repo,
|
||||
) {}
|
||||
|
||||
/** @return list<string> enabled service_category values */
|
||||
public function enabledKeys(string $entityType, int $entityId): array
|
||||
{
|
||||
$stored = $this->repo->enabledMapFor($entityType, $entityId);
|
||||
|
||||
return array_values(array_filter(
|
||||
ServiceCategory::values(),
|
||||
static fn(string $key) => $stored[$key] ?? true,
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Rows shaped for the settings UI — always every category, so the panel renders a
|
||||
* complete list without knowing which kinds exist.
|
||||
*
|
||||
* @return list<array{key: string, label: string, enabled: bool}>
|
||||
*/
|
||||
public function settingsRows(string $entityType, int $entityId): array
|
||||
{
|
||||
$stored = $this->repo->enabledMapFor($entityType, $entityId);
|
||||
|
||||
return array_map(static fn(ServiceCategory $c) => [
|
||||
'key' => $c->value,
|
||||
'label' => $c->label(),
|
||||
'enabled' => $stored[$c->value] ?? true,
|
||||
], ServiceCategory::cases());
|
||||
}
|
||||
|
||||
/**
|
||||
* The kind to bill with when nobody picked one: the single enabled kind, or null
|
||||
* when more than one is enabled (then the panel must ask).
|
||||
*/
|
||||
public function defaultCategory(string $entityType, int $entityId): ?ServiceCategory
|
||||
{
|
||||
$enabled = $this->enabledKeys($entityType, $entityId);
|
||||
|
||||
return count($enabled) === 1 ? ServiceCategory::from($enabled[0]) : null;
|
||||
}
|
||||
|
||||
public function isEnabled(string $entityType, int $entityId, ServiceCategory $category): bool
|
||||
{
|
||||
return in_array($category->value, $this->enabledKeys($entityType, $entityId), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array{key?: string, enabled?: mixed}> $rows
|
||||
* @throws AppException on an unknown category, or when the change would disable every kind
|
||||
*/
|
||||
public function save(string $entityType, int $entityId, array $rows): void
|
||||
{
|
||||
$wanted = [];
|
||||
foreach ($rows as $row) {
|
||||
$category = ServiceCategory::tryFromValue(isset($row['key']) ? (string) $row['key'] : null);
|
||||
if ($category === null) {
|
||||
throw new AppException(
|
||||
ErrorCodes::ERR_VALIDATION_001,
|
||||
'نوع خدمت نامعتبر است: ' . implode('، ', ServiceCategory::values()),
|
||||
422,
|
||||
'service_categories',
|
||||
);
|
||||
}
|
||||
|
||||
$wanted[$category->value] = (bool) ($row['enabled'] ?? false);
|
||||
}
|
||||
|
||||
$resulting = array_filter(
|
||||
ServiceCategory::values(),
|
||||
fn(string $key) => $wanted[$key] ?? in_array($key, $this->enabledKeys($entityType, $entityId), true),
|
||||
);
|
||||
|
||||
if ($resulting === []) {
|
||||
throw new AppException(
|
||||
ErrorCodes::ERR_VALIDATION_001,
|
||||
'حداقل یک نوع خدمت باید فعال باشد',
|
||||
422,
|
||||
'service_categories',
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($wanted as $key => $enabled) {
|
||||
$category = ServiceCategory::from($key);
|
||||
$entity = $this->repo->findOneFor($entityType, $entityId, $category)
|
||||
?? new TenantServiceCategorySetting($entityType, $entityId, $category);
|
||||
|
||||
$this->repo->save($entity->setEnabled($enabled), false);
|
||||
}
|
||||
|
||||
$this->repo->flush();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user