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,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Resource\Repository;
|
||||
|
||||
use App\Resource\Entity\NationalHoliday;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<NationalHoliday>
|
||||
*/
|
||||
class NationalHolidayRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, NationalHoliday::class);
|
||||
}
|
||||
|
||||
/** @return NationalHoliday[] */
|
||||
public function findForYear(int $jalaliYear): array
|
||||
{
|
||||
return $this->createQueryBuilder('h')
|
||||
->where('h.jalaliYear = :year')
|
||||
->setParameter('year', $jalaliYear)
|
||||
->orderBy('h.date', 'ASC')
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, NationalHoliday> کلید = نیمهشب همان روز
|
||||
*/
|
||||
public function mapForRange(int $from, int $to): array
|
||||
{
|
||||
$rows = $this->createQueryBuilder('h')
|
||||
->where('h.date >= :from')
|
||||
->andWhere('h.date <= :to')
|
||||
->setParameter('from', $from)
|
||||
->setParameter('to', $to)
|
||||
->getQuery()
|
||||
->getResult();
|
||||
|
||||
$map = [];
|
||||
foreach ($rows as $holiday) {
|
||||
$map[$holiday->getDate()] = $holiday;
|
||||
}
|
||||
|
||||
return $map;
|
||||
}
|
||||
|
||||
public function findByDate(int $date): ?NationalHoliday
|
||||
{
|
||||
return $this->findOneBy(['date' => $date]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace App\Resource\Repository;
|
||||
|
||||
use App\Resource\Entity\ClinicResource;
|
||||
use App\Resource\Entity\ResourceCalendar;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<ResourceCalendar>
|
||||
*/
|
||||
class ResourceCalendarRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, ResourceCalendar::class);
|
||||
}
|
||||
|
||||
/** @return ResourceCalendar[] */
|
||||
public function findForResource(ClinicResource $resource): array
|
||||
{
|
||||
return $this->createQueryBuilder('c')
|
||||
->where('c.resource = :resource')
|
||||
->setParameter('resource', $resource)
|
||||
->orderBy('c.dayOfWeek', 'ASC')
|
||||
->addOrderBy('c.sequence', 'ASC')
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
|
||||
public function deleteForResource(ClinicResource $resource): int
|
||||
{
|
||||
return (int) $this->createQueryBuilder('c')
|
||||
->delete()
|
||||
->where('c.resource = :resource')
|
||||
->setParameter('resource', $resource)
|
||||
->getQuery()
|
||||
->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int[] $resourceIds
|
||||
* @return array<int, int> شناسهٔ منبع => تعداد شیفت
|
||||
*/
|
||||
public function countByResourceIds(array $resourceIds): array
|
||||
{
|
||||
if ($resourceIds === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$rows = $this->createQueryBuilder('c')
|
||||
->select('IDENTITY(c.resource) AS resource_id, COUNT(c.id) AS total')
|
||||
->where('c.resource IN (:ids)')
|
||||
->setParameter('ids', $resourceIds)
|
||||
->groupBy('c.resource')
|
||||
->getQuery()
|
||||
->getArrayResult();
|
||||
|
||||
$counts = [];
|
||||
foreach ($rows as $row) {
|
||||
$counts[(int) $row['resource_id']] = (int) $row['total'];
|
||||
}
|
||||
|
||||
return $counts;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Resource\Repository;
|
||||
|
||||
use App\Resource\Entity\ClinicResource;
|
||||
use App\Resource\Entity\ResourceException;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<ResourceException>
|
||||
*/
|
||||
class ResourceExceptionRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, ResourceException::class);
|
||||
}
|
||||
|
||||
public function findByUuid(string $uuid): ?ResourceException
|
||||
{
|
||||
return $this->findOneBy(['uuid' => $uuid]);
|
||||
}
|
||||
|
||||
/**
|
||||
* استثناهایی که با بازهٔ [from, to) **تداخل** دارند — نه فقط آنهایی که کاملاً
|
||||
* درونشاند. مرخصیِ سهروزهای که وسطش این بازه است باید برگردد.
|
||||
*
|
||||
* @return ResourceException[]
|
||||
*/
|
||||
public function findOverlapping(ClinicResource $resource, int $from, int $to): array
|
||||
{
|
||||
return $this->createQueryBuilder('e')
|
||||
->where('e.resource = :resource')
|
||||
->andWhere('e.startsAt < :to')
|
||||
->andWhere('e.endsAt > :from')
|
||||
->setParameter('resource', $resource)
|
||||
->setParameter('from', $from)
|
||||
->setParameter('to', $to)
|
||||
->orderBy('e.startsAt', 'ASC')
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Resource\Repository;
|
||||
|
||||
use App\Resource\Entity\TenantHolidayOverride;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<TenantHolidayOverride>
|
||||
*/
|
||||
class TenantHolidayOverrideRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, TenantHolidayOverride::class);
|
||||
}
|
||||
|
||||
public function findByUuid(string $uuid): ?TenantHolidayOverride
|
||||
{
|
||||
return $this->findOneBy(['uuid' => $uuid]);
|
||||
}
|
||||
|
||||
public function findForDate(string $entityType, int $entityId, int $date): ?TenantHolidayOverride
|
||||
{
|
||||
return $this->findOneBy(['entityType' => $entityType, 'entityId' => $entityId, 'date' => $date]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, TenantHolidayOverride> کلید = نیمهشب همان روز
|
||||
*/
|
||||
public function mapForRange(string $entityType, int $entityId, int $from, int $to): array
|
||||
{
|
||||
$rows = $this->createQueryBuilder('o')
|
||||
->where('o.entityType = :type')
|
||||
->andWhere('o.entityId = :id')
|
||||
->andWhere('o.date >= :from')
|
||||
->andWhere('o.date <= :to')
|
||||
->setParameter('type', $entityType)
|
||||
->setParameter('id', $entityId)
|
||||
->setParameter('from', $from)
|
||||
->setParameter('to', $to)
|
||||
->getQuery()
|
||||
->getResult();
|
||||
|
||||
$map = [];
|
||||
foreach ($rows as $override) {
|
||||
$map[$override->getDate()] = $override;
|
||||
}
|
||||
|
||||
return $map;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user