Add the resource↔service link that decides who offers what, and for how much

Until now a resource was picked by type and skill alone, so two devices of the
same type were indistinguishable even when only one of them performed the
service — and there was nowhere to say that this doctor takes 30 minutes for a
filler while that one takes 45.

ResourceServiceOffering is that link: resource ↔ service item, with an optional
duration, an optional price and an active flag. Because a "service option" here
is itself a ServiceItem inside an ItemGroup, one table covers both levels the
spec asks for — a row against the parent item is "resource + service", a row
against a member item is "resource + option". A third table would have meant
two sources of truth for one concept and a rewrite of every path that already
speaks ServiceItem.

It is an aggregate child of ClinicResource, like ResourceSkill: no tenant
columns of its own, since the resource already carries the pair and a copy is
just something that can drift. The constructor refuses a resource and a service
from different environments — TenantFilter does not cover that case, as both
uuids arrive from the request body and the filter does not apply to aggregate
children.

null means inherit, not zero: an explicit zero is a duration that does not
exist, while null means this resource has nothing to say and the resolver
should look one level up. Zero and negative values are rejected outright.

Tests cover the pair being stored, the duplicate pair hitting the unique
constraint, the cross-environment guard, null-means-inherit, one service across
two devices with different numbers, and deactivating without losing them.

Suite 1264 green, phpstan at its 14-error baseline.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
hamed
2026-08-01 21:01:18 +03:30
co-authored by Claude Opus 5
parent 8280aa7578
commit 826b940c00
7 changed files with 434 additions and 8 deletions
@@ -0,0 +1,80 @@
<?php
namespace App\Resource\Repository;
use App\ClinicService\Entity\ServiceItem;
use App\Resource\Entity\ClinicResource;
use App\Resource\Entity\ResourceServiceOffering;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<ResourceServiceOffering>
*/
class ResourceServiceOfferingRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, ResourceServiceOffering::class);
}
public function findOneFor(ClinicResource $resource, ServiceItem $item): ?ResourceServiceOffering
{
return $this->findOneBy(['resource' => $resource, 'serviceItem' => $item]);
}
/** @return ResourceServiceOffering[] سرویس‌های یک منبع، برای تب پنل */
public function findForResource(ClinicResource $resource): array
{
return $this->createQueryBuilder('o')
->join('o.serviceItem', 'i')
->addSelect('i')
->where('o.resource = :resource')
->setParameter('resource', $resource)
->orderBy('i.name', 'ASC')
->getQuery()
->getResult();
}
/**
* آیا برای این سرویس **اصلاً** رابطه‌ای تعریف شده؟
*
* پایهٔ سازگاری عقب‌روِ {@see ClinicResourceRepository::findEligible()}: محیطی که هنوز
* رابطه‌ها را پر نکرده نباید یک‌شبه بدون وقت آزاد شود، پس فیلتر فقط وقتی اعمال
* می‌شود که کلینیک دست‌کم یک ردیف برای آن سرویس ساخته باشد.
*/
public function hasAnyFor(ServiceItem $item): bool
{
return (bool) $this->createQueryBuilder('o')
->select('1')
->where('o.serviceItem = :item')
->setParameter('item', $item)
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
}
/** @return int[] شناسهٔ منابعی که این سرویس را فعال ارائه می‌دهند */
public function activeResourceIdsFor(ServiceItem $item): array
{
$rows = $this->createQueryBuilder('o')
->select('IDENTITY(o.resource) AS resource_id')
->where('o.serviceItem = :item')
->andWhere('o.active = true')
->setParameter('item', $item)
->getQuery()
->getArrayResult();
return array_map(static fn (array $row): int => (int) $row['resource_id'], $rows);
}
public function deleteForResource(ClinicResource $resource): int
{
return (int) $this->createQueryBuilder('o')
->delete()
->where('o.resource = :resource')
->setParameter('resource', $resource)
->getQuery()
->execute();
}
}