Section 10 of the design document, and the payoff for tasks 01–05. The engine slides a multi-segment plan across resource calendars and answers which times are actually possible, with a suggested resource for each role. Until now the only conflict the system checked was the doctor's; rooms, devices and operators did not exist. Allocation is per *role*, not per segment, and that is what returns the wasted capacity. An operator with no requirement during "waiting for the cream" is simply not examined for those minutes, so another patient can use them. The reference test encodes exactly that: patient A holds 10:00–11:00 while the operator is only busy 10:00–10:05 and 10:35–11:00, and patient B is offered a slot inside the gap with the second room assigned. The spec says the task is not verified without that scenario. One resource is chosen for every segment that needs its role, not independently per segment — otherwise the operator in segment 1 and segment 3 could be two different people and the patient would change hands mid-treatment. Occupancy is stored one row per (segment × resource) rather than one per appointment. The granularity is the whole point; a row per appointment would re-create the single-interval model the design rejects. Reserved intervals are widened by each resource's setup/cleanup, because the resource genuinely is not available then. booking_mode gains a third value, resource, alongside slot and service. It is purely additive: the default stays slot, no environment moves on its own, and a location that has not opted in keeps the untouched legacy path. The frozen slot-mode contract stays green. Performance is a test, not a hope: 30 days, 20 resources and 500 existing bookings complete well inside the 500ms budget. Every input is read once and the rest is in memory — no query inside the day or candidate loop — and candidates are generated only from the free windows of the scarcest role, which turns tens of thousands of candidates into a few hundred. An empty result is not an error and not a 404: it carries reason: "no_capacity_in_range" so the caller does not have to infer meaning from emptiness. Also fixed a genuinely intermittent test defect: NumericFieldNormalizerTest padded a random number with the three-byte Persian "۰" using byte-based str_pad, producing broken UTF-8 whenever the number was short. It failed roughly at random. The improved assertion message added earlier is what identified it immediately. 1196 tests / 3414 assertions. phpstan at its 14-error baseline. Resource-picking strategies, the availability cache and the settings UI are recorded as outstanding in the checklist with reasons — the cache in particular would be premature while the performance test passes comfortably without it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
176 lines
7.1 KiB
PHP
176 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace App\Appointment\Entity;
|
|
|
|
use App\Clinic\Entity\Clinic;
|
|
use App\Doctor\Entity\Doctor;
|
|
use App\Shared\Context\EntityContext;
|
|
use App\Shared\Tenant\TenantOwnedTrait;
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
use App\Appointment\Repository\WeeklyScheduleRepository;
|
|
use Symfony\Component\Uid\Uuid;
|
|
|
|
/**
|
|
* برنامهٔ هفتگی نوبتدهی یک پزشک در یک context مشخص.
|
|
*
|
|
* context با ستون clinic_id بیان میشود: NULL یعنی مطب شخصی پزشک، و مقدار غیر-NULL
|
|
* یعنی همان پزشک در آن کلینیک. یک پزشک میتواند همزمان چند برنامه داشته باشد
|
|
* (شخصی + یکی به ازای هر کلینیک) و این برنامهها کاملاً مستقلاند.
|
|
*/
|
|
#[ORM\Entity(repositoryClass: WeeklyScheduleRepository::class)]
|
|
#[ORM\Table(name: 'weekly_schedules')]
|
|
// doctor_id در کلید میماند: در یک کلینیک چند پزشک هستند و هر کدام برنامهٔ خودش را
|
|
// دارد، پس (entity_type, entity_id) بهتنهایی برای پزشک دوم نقض یکتایی میسازد.
|
|
#[ORM\UniqueConstraint(name: 'uniq_weekly_schedule_doctor_tenant', columns: ['doctor_id', 'entity_type', 'entity_id'])]
|
|
class WeeklySchedule
|
|
{
|
|
use TenantOwnedTrait;
|
|
|
|
public const DAYS = ['saturday', 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday'];
|
|
|
|
public const META_KEY = 'meta';
|
|
|
|
public const MODE_SLOT = 'slot'; // نوبتدهی اسلاتی (رفتار پیشفرض)
|
|
public const MODE_SERVICE = 'service'; // نوبتدهی بر اساس مدت سرویس
|
|
|
|
/**
|
|
* نوبتدهی چندمنبعی: برنامهٔ چندبخشی روی تقویم منابع (بند ۱۰ مستند).
|
|
*
|
|
* افزودنی محض است — پیشفرض همچنان `slot` میماند و هیچ محیطی خودبهخود به این
|
|
* حالت نمیرود؛ ارتقا داوطلبانه و صریح است.
|
|
*/
|
|
public const MODE_RESOURCE = 'resource';
|
|
|
|
public const MODES = [self::MODE_SLOT, self::MODE_SERVICE, self::MODE_RESOURCE];
|
|
|
|
/** واحدهای مجاز بازهٔ رزرو آنلاین؛ همان کلیدواژههای strtotime. */
|
|
public const BOOKING_WINDOW_UNITS = ['day', 'week', 'month'];
|
|
|
|
public const DEFAULT_META = [
|
|
'online_booking_enabled' => true,
|
|
'booking_window_value' => 3,
|
|
'booking_window_unit' => 'month',
|
|
'booking_mode' => self::MODE_SLOT,
|
|
'buffer_minutes' => 0,
|
|
];
|
|
|
|
#[ORM\Id]
|
|
#[ORM\GeneratedValue]
|
|
#[ORM\Column(type: 'integer')]
|
|
private ?int $id = null;
|
|
|
|
#[ORM\Column(type: 'string', length: 36, unique: true)]
|
|
private string $uuid;
|
|
|
|
#[ORM\ManyToOne(targetEntity: Doctor::class)]
|
|
#[ORM\JoinColumn(name: 'doctor_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
|
|
private Doctor $doctor;
|
|
|
|
/** NULL = مطب شخصی پزشک. */
|
|
#[ORM\ManyToOne(targetEntity: Clinic::class)]
|
|
#[ORM\JoinColumn(name: 'clinic_id', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
|
|
private ?Clinic $clinic = null;
|
|
|
|
#[ORM\Column(type: 'json')]
|
|
private array $setting = [];
|
|
|
|
#[ORM\Column(name: 'created_at', type: 'integer')]
|
|
private int $createdAt;
|
|
|
|
#[ORM\Column(name: 'updated_at', type: 'integer')]
|
|
private int $updatedAt;
|
|
|
|
public function __construct(Doctor $doctor, array $setting, ?Clinic $clinic = null)
|
|
{
|
|
$this->uuid = Uuid::v4()->toRfc4122();
|
|
$this->doctor = $doctor;
|
|
$this->clinic = $clinic;
|
|
$this->setting = $setting;
|
|
$this->createdAt = time();
|
|
$this->updatedAt = time();
|
|
}
|
|
|
|
public function getId(): ?int { return $this->id; }
|
|
public function getUuid(): string { return $this->uuid; }
|
|
public function getDoctor(): Doctor { return $this->doctor; }
|
|
public function getClinic(): ?Clinic { return $this->clinic; }
|
|
|
|
/** فقط برای انتقال دستی برنامههای قدیمی به محیط کلینیک (app:schedule:assign-clinic). */
|
|
public function setClinic(?Clinic $clinic): self
|
|
{
|
|
$this->clinic = $clinic;
|
|
$this->updatedAt = time();
|
|
$this->assignTenant(EntityContext::forBooking($this->doctor, $clinic));
|
|
return $this;
|
|
}
|
|
public function getSetting(): array { return $this->setting; }
|
|
|
|
public function setSetting(array $setting): self
|
|
{
|
|
$meta = $this->setting[self::META_KEY] ?? null;
|
|
unset($setting[self::META_KEY]);
|
|
if ($meta !== null) {
|
|
$setting[self::META_KEY] = $meta;
|
|
}
|
|
$this->setting = $setting;
|
|
$this->updatedAt = time();
|
|
return $this;
|
|
}
|
|
|
|
public function getMeta(): array
|
|
{
|
|
return array_merge(self::DEFAULT_META, $this->setting[self::META_KEY] ?? []);
|
|
}
|
|
|
|
/**
|
|
* booking_mode ذخیرهشده بهصورت خام (بدون merge پیشفرض). null یعنی هنوز
|
|
* صریحاً ثبت نشده — تا وقتی null است، انتخاب روش قابلتغییر است؛ پس از اولین
|
|
* ثبت، قفل میشود.
|
|
*/
|
|
public function getStoredBookingMode(): ?string
|
|
{
|
|
return $this->setting[self::META_KEY]['booking_mode'] ?? null;
|
|
}
|
|
|
|
public function setMeta(array $meta): self
|
|
{
|
|
$current = $this->getMeta();
|
|
$this->setting[self::META_KEY] = [
|
|
'online_booking_enabled' => (bool)($meta['online_booking_enabled'] ?? $current['online_booking_enabled']),
|
|
'booking_window_value' => max(1, (int)($meta['booking_window_value'] ?? $current['booking_window_value'])),
|
|
'booking_window_unit' => in_array($meta['booking_window_unit'] ?? null, self::BOOKING_WINDOW_UNITS, true)
|
|
? $meta['booking_window_unit']
|
|
: $current['booking_window_unit'],
|
|
'booking_mode' => in_array($meta['booking_mode'] ?? null, self::MODES, true)
|
|
? $meta['booking_mode']
|
|
: $current['booking_mode'],
|
|
'buffer_minutes' => max(0, (int)($meta['buffer_minutes'] ?? $current['buffer_minutes'])),
|
|
];
|
|
$this->updatedAt = time();
|
|
return $this;
|
|
}
|
|
|
|
public function getDaySchedule(): array
|
|
{
|
|
$schedule = $this->setting;
|
|
unset($schedule[self::META_KEY]);
|
|
return $schedule;
|
|
}
|
|
|
|
public function toArray(): array
|
|
{
|
|
return [
|
|
'uuid' => $this->uuid,
|
|
'doctor_uuid' => $this->doctor->getUuid(),
|
|
'clinic_uuid' => $this->clinic?->getUuid(),
|
|
'context' => $this->clinic === null ? 'personal' : 'clinic',
|
|
'schedule' => $this->getDaySchedule(),
|
|
'meta' => $this->getMeta(),
|
|
// نوع نوبتدهی پس از اولین ثبت قفل میشود (پنل توگل را غیرفعال میکند).
|
|
'booking_mode_locked' => $this->getStoredBookingMode() !== null,
|
|
'created_at' => $this->createdAt,
|
|
'updated_at' => $this->updatedAt,
|
|
];
|
|
}
|
|
}
|