Files
clinicpro/migrations/Version20260730143130.php
hamedandClaude Opus 5 1fdfdf9e48 feat(resource): resource calendars, exceptions and national holidays
Section 9 of the design document builds free time by subtracting seven layers.
Four existed and all of them hung off the doctor. This adds the missing ones and
puts them on the resource:

  branch hours ∩ resource shifts − national holidays − resource exceptions

Booked appointments and holds are deliberately NOT subtracted here — those are
tasks 06/07, as is intersecting several resources. The method is called
rawAvailability() so nobody mistakes the output for bookable time. Nothing in this
change calls SlotCalculatorService; the existing slot path stays frozen.

Four types of exception (leave, absence, maintenance, ad-hoc closure) share one
table because all four are "an interval subtracted from a resource's calendar";
splitting them would mean four queries per availability lookup instead of one.
Holiday overrides work in both directions: a clinic that opens on a public holiday,
and a clinic that closes on an ordinary day.

Every empty day carries a reason (national_holiday, no_shift, branch_closed,
outside_branch_hours, exception, …). Without it an empty response is
indistinguishable from a bug and the first person debugging has to read four tables
by hand.

Three real defects found on the way:

JalaliDateService.gregorianToJalali() was wrong — it returned [3006, 7, 3] for
2026-07-30 instead of [1405, 5, 8], roughly 1601 years off. jalaliYear(),
jalaliMonth(), jalaliMonthRange() and jalaliYearRange() all inherit that, so the
representation reports built on them have been filtering by nonsense ranges. The
class's own formatDateTime() was already correct because it used IntlDateFormatter,
so both conversions now go through the same mechanism, and JalaliDateServiceTest
pins Nowruz and the 6/31→7/1 boundary. There were no tests before, which is why
nobody noticed.

TimeInterval added a seconds-based midnight to a minutes-based interval, turning an
eight-hour shift into eight seconds. The conversion is now an explicitly named
minutesToAbsolute() so the unit change cannot happen silently again.

HolidayService.upsertNational() persisted but left flushing to the caller. Every
HTTP request reboots the kernel, so the caller often held a different
EntityManager: persist landed on one, flush on the other, and nothing was written
with no error at all. The write is now self-contained.

119 tests across tests/Resource, tests/Branch and tests/Representation. phpstan
clean on both touched domains.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-30 18:14:23 +03:30

44 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Resource calendars, exceptions and the national holiday table.
*
* These are the missing layers of the availability subtraction in section 9 of the
* design document. They sit on the *resource*, parallel to the existing per-doctor
* WeeklySchedule/DateOverride/Holiday path, which stays untouched in this phase.
*/
final class Version20260730143130 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add resource calendars, resource exceptions, national holidays and tenant holiday overrides';
}
public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE national_holidays (id INT AUTO_INCREMENT NOT NULL, uuid VARCHAR(36) NOT NULL, date INT NOT NULL, jalali_date VARCHAR(10) NOT NULL, jalali_year SMALLINT NOT NULL, title VARCHAR(200) NOT NULL, created_at INT NOT NULL, UNIQUE INDEX UNIQ_8262602CD17F50A6 (uuid), INDEX idx_holiday_year (jalali_year), UNIQUE INDEX uniq_holiday_date (date), PRIMARY KEY (id)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE resource_calendars (id INT AUTO_INCREMENT NOT NULL, day_of_week SMALLINT NOT NULL, sequence SMALLINT DEFAULT 0 NOT NULL, start_minute SMALLINT NOT NULL, end_minute SMALLINT NOT NULL, active TINYINT DEFAULT 1 NOT NULL, resource_id INT NOT NULL, INDEX IDX_2C16D19089329D25 (resource_id), INDEX idx_rc_resource_day (resource_id, day_of_week, active), UNIQUE INDEX uniq_rc_resource_day_seq (resource_id, day_of_week, sequence), PRIMARY KEY (id)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE resource_exceptions (id INT AUTO_INCREMENT NOT NULL, uuid VARCHAR(36) NOT NULL, type VARCHAR(20) NOT NULL, starts_at INT NOT NULL, ends_at INT NOT NULL, reason VARCHAR(200) DEFAULT NULL, created_at INT NOT NULL, updated_at INT NOT NULL, entity_type VARCHAR(10) NOT NULL, entity_id INT NOT NULL, resource_id INT NOT NULL, UNIQUE INDEX UNIQ_486A5CE6D17F50A6 (uuid), INDEX IDX_486A5CE689329D25 (resource_id), INDEX idx_rex_tenant (entity_type, entity_id), INDEX idx_rex_resource_range (resource_id, starts_at, ends_at), PRIMARY KEY (id)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('CREATE TABLE tenant_holiday_overrides (id INT AUTO_INCREMENT NOT NULL, uuid VARCHAR(36) NOT NULL, date INT NOT NULL, is_working TINYINT NOT NULL, note VARCHAR(200) DEFAULT NULL, created_at INT NOT NULL, entity_type VARCHAR(10) NOT NULL, entity_id INT NOT NULL, UNIQUE INDEX UNIQ_DB0B49E4D17F50A6 (uuid), UNIQUE INDEX uniq_tho_tenant_date (entity_type, entity_id, date), PRIMARY KEY (id)) DEFAULT CHARACTER SET utf8mb4');
$this->addSql('ALTER TABLE resource_calendars ADD CONSTRAINT FK_2C16D19089329D25 FOREIGN KEY (resource_id) REFERENCES clinic_resources (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE resource_exceptions ADD CONSTRAINT FK_486A5CE689329D25 FOREIGN KEY (resource_id) REFERENCES clinic_resources (id) ON DELETE CASCADE');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE resource_calendars DROP FOREIGN KEY FK_2C16D19089329D25');
$this->addSql('ALTER TABLE resource_exceptions DROP FOREIGN KEY FK_486A5CE689329D25');
$this->addSql('DROP TABLE national_holidays');
$this->addSql('DROP TABLE resource_calendars');
$this->addSql('DROP TABLE resource_exceptions');
$this->addSql('DROP TABLE tenant_holiday_overrides');
}
}