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>
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
namespace App\Resource\Service;
|
||||
|
||||
use App\Representation\Service\JalaliDateService;
|
||||
use App\Resource\Entity\NationalHoliday;
|
||||
use App\Resource\Entity\TenantHolidayOverride;
|
||||
use App\Resource\Repository\NationalHolidayRepository;
|
||||
use App\Resource\Repository\TenantHolidayOverrideRepository;
|
||||
use App\Shared\Constant\ErrorCodes;
|
||||
use App\Shared\Exception\AppException;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
/**
|
||||
* تعطیلات رسمی کشور و استثناهای هر محیط رویشان.
|
||||
*
|
||||
* تبدیل شمسی از {@see JalaliDateService} میآید و اینجا دوباره پیاده نمیشود — آن
|
||||
* سرویس از فاز نمایندگی موجود است و درست کار میکند. (نامِ فضایش
|
||||
* `App\Representation` است که برای مصرف مشترک جای درستی نیست؛ جابهجا کردنش یک
|
||||
* تغییر مکانیکی در کل کدبیس است و به این تسک ربطی ندارد.)
|
||||
*/
|
||||
final class HolidayService
|
||||
{
|
||||
/** تهران مرجع «روز» است: تعطیل رسمی کشوری است، نه محلیِ شعبه. */
|
||||
public const NATIONAL_TIMEZONE = 'Asia/Tehran';
|
||||
|
||||
public function __construct(
|
||||
private readonly NationalHolidayRepository $holidays,
|
||||
private readonly TenantHolidayOverrideRepository $overrides,
|
||||
private readonly JalaliDateService $jalali,
|
||||
private readonly EntityManagerInterface $em,
|
||||
) {}
|
||||
|
||||
/** نیمهشبِ یک تاریخ شمسی به وقت تهران. */
|
||||
public function jalaliToMidnight(int $jy, int $jm, int $jd): int
|
||||
{
|
||||
[$gy, $gm, $gd] = $this->jalali->jalaliToGregorian($jy, $jm, $jd);
|
||||
|
||||
return (new \DateTimeImmutable(
|
||||
sprintf('%04d-%02d-%02d 00:00:00', $gy, $gm, $gd),
|
||||
new \DateTimeZone(self::NATIONAL_TIMEZONE),
|
||||
))->getTimestamp();
|
||||
}
|
||||
|
||||
/**
|
||||
* یک تعطیل رسمی را ثبت یا بهروز میکند. idempotent: تکیهگاهش `date` است که
|
||||
* یکتاست، پس import دوباره ردیف تکراری نمیسازد.
|
||||
*
|
||||
* خودش flush میکند و این عمدی است: سپردنِ flush به فراخوان یعنی اگر او
|
||||
* EntityManager دیگری در دست داشته باشد، persist روی یکی و flush روی دیگری
|
||||
* میافتد و **هیچ ردیفی نوشته نمیشود، بیهیچ خطایی**. همین در تستها اتفاق افتاد،
|
||||
* چون هر درخواست HTTP کرنل را از نو میسازد و سرویس از کانتینر تازه میآید.
|
||||
*/
|
||||
public function upsertNational(int $jy, int $jm, int $jd, string $title): NationalHoliday
|
||||
{
|
||||
$date = $this->jalaliToMidnight($jy, $jm, $jd);
|
||||
$existing = $this->holidays->findByDate($date);
|
||||
|
||||
if ($existing !== null) {
|
||||
$existing->setTitle($title);
|
||||
$this->em->flush();
|
||||
|
||||
return $existing;
|
||||
}
|
||||
|
||||
$holiday = new NationalHoliday(
|
||||
$date,
|
||||
sprintf('%04d-%02d-%02d', $jy, $jm, $jd),
|
||||
$jy,
|
||||
$title,
|
||||
);
|
||||
|
||||
$this->em->persist($holiday);
|
||||
$this->em->flush();
|
||||
|
||||
return $holiday;
|
||||
}
|
||||
|
||||
/** @return NationalHoliday[] */
|
||||
public function forYear(int $jalaliYear): array
|
||||
{
|
||||
return $this->holidays->findForYear($jalaliYear);
|
||||
}
|
||||
|
||||
/**
|
||||
* استثنای محیط. روی همان تاریخ دوباره فرستادن، همان ردیف را عوض میکند — وگرنه
|
||||
* کلید یکتا با خطای خام دیتابیس میشکست.
|
||||
*/
|
||||
public function setOverride(string $entityType, int $entityId, int $date, bool $isWorking, ?string $note): TenantHolidayOverride
|
||||
{
|
||||
$midnight = $this->midnightTehran($date);
|
||||
$existing = $this->overrides->findForDate($entityType, $entityId, $midnight);
|
||||
|
||||
if ($existing !== null) {
|
||||
$existing->setWorking($isWorking)->setNote($note);
|
||||
$this->em->flush();
|
||||
|
||||
return $existing;
|
||||
}
|
||||
|
||||
$override = new TenantHolidayOverride($entityType, $entityId, $midnight, $isWorking);
|
||||
$override->setNote($note);
|
||||
|
||||
$this->em->persist($override);
|
||||
$this->em->flush();
|
||||
|
||||
return $override;
|
||||
}
|
||||
|
||||
public function deleteOverride(TenantHolidayOverride $override): void
|
||||
{
|
||||
$this->em->remove($override);
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
/** @return TenantHolidayOverride[] */
|
||||
public function overridesForYear(string $entityType, int $entityId, int $jalaliYear): array
|
||||
{
|
||||
$from = $this->jalaliToMidnight($jalaliYear, 1, 1);
|
||||
$to = $this->jalaliToMidnight($jalaliYear + 1, 1, 1);
|
||||
|
||||
return array_values($this->overrides->mapForRange($entityType, $entityId, $from, $to));
|
||||
}
|
||||
|
||||
public function assertJalaliYear(mixed $value): int
|
||||
{
|
||||
if (!is_numeric($value) || (int) $value < 1300 || (int) $value > 1500) {
|
||||
throw new AppException(ErrorCodes::ERR_VALIDATION_001, 'سال شمسی نامعتبر است', 422, 'year');
|
||||
}
|
||||
|
||||
return (int) $value;
|
||||
}
|
||||
|
||||
private function midnightTehran(int $timestamp): int
|
||||
{
|
||||
return (new \DateTimeImmutable('@' . $timestamp))
|
||||
->setTimezone(new \DateTimeZone(self::NATIONAL_TIMEZONE))
|
||||
->setTime(0, 0)
|
||||
->getTimestamp();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user