feat(catalog): dual durations, item groups, relations and branch overrides

Section 5 of the design document rejects summing service durations. "Face + bikini"
is not 15+12=27 minutes but 15+8=23 — preparation and settling the patient do not
happen twice. Seven wasted minutes times twenty appointments a day is an hour of
capacity lost daily, and AppointmentController was doing exactly that plain sum.

Each item now carries a solo duration and an additional duration. One item counts at
its solo duration and the rest at their additional; the anchor is the item with the
*largest* solo duration rather than the first one selected. Anchoring on selection
order would have let the same basket cost different amounts depending on click order,
so a patient could buy a shorter appointment by reordering. Largest-first is also
conservative: no combination is ever under-estimated, and under-estimating pushes the
next appointment on top of this one.

additional_duration_minutes stays NULL by default and the entity reads NULL as "same
as solo", so every existing service keeps behaving exactly as before — the 236
appointment-domain tests pass unchanged. The old duration_minutes column is kept and
written in step rather than renamed, because other consumers still read it.

ServiceBookingCalculator now delegates to DurationCalculator, which is the one-line
change task 00 predicted when it deliberately preserved the naive sum.

Selection rules are data, not policy: min/max per group is a number, and "bikini does
not combine with full body" is a relation. Putting either in a rules engine means
several rules per service and nobody able to explain a rejection. Validation returns
*all* errors at once rather than the first, since a user with three problems should
not make three round trips. Prerequisite cycles are rejected at write time — storing
both "A requires B" and "B requires A" would make every selection permanently invalid.

Named CatalogCategory, not ServiceCategory: that name is already an insurance enum
(outpatient/inpatient) living on ServiceItem itself, so the two would have collided in
the same file's imports.

Also fixed a defect the tests caught: breakdown() used $overrides[$id]?->… on a key
that may not exist, which warns instead of yielding null.

1175 tests / 3289 assertions. phpstan measured at 14 errors both with and without
this change (verified by stashing). Slot-mode frozen contract green.

The admin UI tab for groups and relations is not built; the checklist records it as
outstanding with a target. The backend is complete and
POST /service-selection/validate is consumable without it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hamed
2026-07-30 18:44:02 +03:30
co-authored by Claude Opus 5
parent 0f4db93fb8
commit b1b06c1b36
21 changed files with 2388 additions and 72 deletions
+92 -1
View File
@@ -68,9 +68,36 @@ class ServiceItem
#[ORM\Column(name: 'insurance_price_rials', type: 'integer', nullable: true)]
private ?int $insurancePriceRials = null;
/**
* @deprecated مقدار قدیمی؛ منبع حقیقتِ مدت اکنون `soloDurationMinutes` است.
* هنوز نوشته می‌شود تا مصرف‌کننده‌های موجود نشکنند.
*/
#[ORM\Column(name: 'duration_minutes', type: 'integer', nullable: true)]
private ?int $durationMinutes = null;
/** مدت این آیتم وقتی **تنها** انجام شود. */
#[ORM\Column(name: 'solo_duration_minutes', type: 'smallint', nullable: true)]
private ?int $soloDurationMinutes = null;
/**
* مدت این آیتم وقتی **کنار آیتم دیگری** در همان نوبت انجام شود.
*
* بند ۵ مستند: جمع سادهٔ مدت‌ها ظرفیت را هدر می‌دهد. «صورت + بیکینی» ۱۵+۱۲=۲۷
* نیست، ۱۵+۸=۲۳ است؛ آماده‌سازی و استقرار بیمار دو بار انجام نمی‌شود.
*
* `null` یعنی «همان مدت تنها» — سازگاری با دادهٔ موجودی که فقط یک عدد داشت.
*/
#[ORM\Column(name: 'additional_duration_minutes', type: 'smallint', nullable: true)]
private ?int $additionalDurationMinutes = null;
/** تعداد جلسات؛ ۱ یعنی تک‌جلسه‌ای. پروتکل کامل دوره در تسک ۱۲. */
#[ORM\Column(name: 'session_count', type: 'smallint', options: ['default' => 1])]
private int $sessionCount = 1;
#[ORM\ManyToOne(targetEntity: CatalogCategory::class)]
#[ORM\JoinColumn(name: 'catalog_category_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
private ?CatalogCategory $catalogCategory = null;
/** نمایش این سرویس در نوبت‌دهی (پزشک ممکن است همهٔ سرویس‌ها را ارائه ندهد). */
#[ORM\Column(type: 'boolean', options: ['default' => false])]
private bool $bookable = false;
@@ -203,7 +230,67 @@ class ServiceItem
public function setActive(bool $active): self { $this->active = $active; $this->updatedAt = time(); return $this; }
public function setInsuranceCovered(bool $v): self { $this->insuranceCovered = $v; $this->updatedAt = time(); return $this; }
public function setServiceCategory(ServiceCategory $v): self { $this->serviceCategory = $v; $this->updatedAt = time(); return $this; }
public function setDurationMinutes(?int $v): self { $this->durationMinutes = $v; $this->updatedAt = time(); return $this; }
/** هر دو ستون را هم‌زمان می‌نویسد تا «مدت» یک منبع حقیقت داشته باشد. */
public function setDurationMinutes(?int $v): self
{
$this->durationMinutes = $v;
$this->soloDurationMinutes = $v;
$this->updatedAt = time();
return $this;
}
public function getSoloDurationMinutes(): ?int { return $this->soloDurationMinutes ?? $this->durationMinutes; }
public function getAdditionalDurationMinutes(): ?int { return $this->additionalDurationMinutes; }
public function getSessionCount(): int { return $this->sessionCount; }
public function getCatalogCategory(): ?CatalogCategory { return $this->catalogCategory; }
/**
* مدتِ «کنار بقیه». آیتمی که مقدارش را نگذاشته، همان مدت تنها را می‌گیرد — پس
* دادهٔ موجود دقیقاً مثل قبل حساب می‌شود و این تغییر افزایشی است.
*/
public function effectiveAdditionalMinutes(): ?int
{
return $this->additionalDurationMinutes ?? $this->getSoloDurationMinutes();
}
public function setSoloDurationMinutes(?int $v): self
{
$this->soloDurationMinutes = $v;
$this->durationMinutes = $v; // ستون قدیمی هم‌گام می‌ماند
$this->updatedAt = time();
return $this;
}
public function setAdditionalDurationMinutes(?int $v): self
{
$this->additionalDurationMinutes = $v;
$this->updatedAt = time();
return $this;
}
/** @throws \InvalidArgumentException روی تعداد جلسهٔ کمتر از ۱ */
public function setSessionCount(int $v): self
{
if ($v < 1) {
throw new \InvalidArgumentException('session_count must be at least 1.');
}
$this->sessionCount = $v;
$this->updatedAt = time();
return $this;
}
public function setCatalogCategory(?CatalogCategory $v): self
{
$this->catalogCategory = $v;
$this->updatedAt = time();
return $this;
}
public function setBookable(bool $v): self { $this->bookable = $v; $this->updatedAt = time(); return $this; }
public function setInventoryPackageId(?int $v): self { $this->inventoryPackageId = $v; $this->updatedAt = time(); return $this; }
@@ -237,6 +324,10 @@ class ServiceItem
'service_category' => $this->getServiceCategory()->value,
'service_category_label' => $this->getServiceCategory()->label(),
'duration_minutes' => $this->durationMinutes,
'solo_duration_minutes' => $this->getSoloDurationMinutes(),
'additional_duration_minutes' => $this->effectiveAdditionalMinutes(),
'session_count' => $this->sessionCount,
'catalog_category_uuid' => $this->catalogCategory?->getUuid(),
'bookable' => $this->bookable,
'inventory_package_id' => $this->inventoryPackageId,
'consumables' => array_map(