a9c14704b7
Auto Tag Develop / tag (push) Successful in 7s
## Contexte Une `Category` ne pouvait appartenir qu'à **un seul** `CategoryType` (ManyToOne). Le besoin métier : plusieurs types par catégorie. Cette MR fait passer la relation en **ManyToMany** et ajoute le bouton **« Filtres »** à droite de la liste des catégories (modèle Répertoire Clients). Slice vertical complet (le passage M:N casse mécaniquement le contrat inter-module `CategoryInterface`, consommé par la RG-2.10 fournisseurs). ## Volet A — Relation M:N - `Category.categoryType` (ManyToOne) → `categoryTypes` (ManyToMany, jonction `category_category_type`). Au moins un type obligatoire (`Assert\\Count(min:1)`). - **Unicité du nom GLOBALE** parmi les actifs (`uq_category_name_active`, remplace `uq_category_name_type_active`). Message 409 reformulé. - Migration : table de jonction + backfill + drop colonne `category_type_id` + nouvel index. Validée **rejouable sur base fraîche**. - Contrat Shared : `getCategoryTypeCode()` → `getCategoryTypeCodes(): array`. `Supplier`/`SupplierAddress`/`SupplierFixtures` revalident « contient FOURNISSEUR » (RG-2.10). - Provider/Repository : filtre type via sous-requête `EXISTS` (ne tronque pas la collection embarquée), eager-load anti-N+1. ## Volet B — Bouton « Filtres » - Drawer recherche par nom + types multi (OR). Compteur de filtres actifs. État local, jamais persisté en URL. - Back : filtres `?name=` et `?typeId[]=` sur la collection. ## Front - Multi-select `MalioSelectCheckbox`, `useCategoryForm` en `categoryTypeIds[]`, colonne « Types », clés i18n. ## Tests / vérifs - `make test` : **582 tests, 2474 assertions, 0 échec** ✅ - `make nuxt-test` : **236 tests** ✅ - `make php-cs-fixer-allow-risky` ✅ - Migration rejouée sur base fraîche (`make db-reset`) ✅ - Nouveau `CategoryFilterTest` (name partiel + typeId[] OR + multi-type non dupliqué) --------- Co-authored-by: Matthieu <contact@malio.fr> Reviewed-on: #75 Co-authored-by: THOLOT DECHENE Matthieu <matthieu@yuno.malio.fr> Co-committed-by: THOLOT DECHENE Matthieu <matthieu@yuno.malio.fr>
439 lines
16 KiB
PHP
439 lines
16 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Module\Commercial\Domain\Entity;
|
|
|
|
use ApiPlatform\Metadata\ApiResource;
|
|
use ApiPlatform\Metadata\Delete;
|
|
use ApiPlatform\Metadata\Get;
|
|
use ApiPlatform\Metadata\Link;
|
|
use ApiPlatform\Metadata\Patch;
|
|
use ApiPlatform\Metadata\Post;
|
|
use App\Module\Commercial\Infrastructure\ApiPlatform\State\Processor\SupplierAddressProcessor;
|
|
use App\Module\Commercial\Infrastructure\Doctrine\DoctrineSupplierAddressRepository;
|
|
use App\Shared\Domain\Attribute\Auditable;
|
|
use App\Shared\Domain\Contract\BlamableInterface;
|
|
use App\Shared\Domain\Contract\CategoryInterface;
|
|
use App\Shared\Domain\Contract\SiteInterface;
|
|
use App\Shared\Domain\Contract\TimestampableInterface;
|
|
use App\Shared\Domain\Trait\TimestampableBlamableTrait;
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
|
use Doctrine\Common\Collections\Collection;
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
use Symfony\Component\Serializer\Attribute\Groups;
|
|
use Symfony\Component\Serializer\Attribute\SerializedName;
|
|
use Symfony\Component\Validator\Constraints as Assert;
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
|
|
|
/**
|
|
* Adresse d'un fournisseur (1:n) — onglet Adresse. Le type d'adresse est un enum
|
|
* exclusif PROSPECT | DEPART | RENDU (radio cote front — RG-2.09), qui remplace
|
|
* les 3 booleens prospect/livraison/facturation du Client (M1) ; pas d'email de
|
|
* facturation au M2. Ajoute deux champs specifiques fournisseur : `bennes`
|
|
* (entier nullable) et `triageProvider` (prestataire de triage, booleen).
|
|
*
|
|
* Relations M2M :
|
|
* - sites : SiteInterface (module Sites) via resolve_target_entities — au moins
|
|
* un site obligatoire (RG-2.06, Assert\Count). Site n'a pas de `code`.
|
|
* - contacts : SupplierContact (meme module).
|
|
* - categories : CategoryInterface (module Catalog) via resolve_target_entities —
|
|
* type FOURNISSEUR attendu (RG-2.10, Assert\Callback validateCategoryType).
|
|
*
|
|
* Embarquee sous `supplier.addresses` au detail (groupe supplier:item:read,
|
|
* maillon (a)).
|
|
*
|
|
* Sous-ressource API (ERP-88, spec § 4.5) :
|
|
* - POST /api/suppliers/{supplierId}/addresses : creation rattachee au
|
|
* fournisseur parent (Link toProperty 'supplier'), security
|
|
* commercial.suppliers.manage.
|
|
* - PATCH / DELETE /api/supplier_addresses/{id} : security
|
|
* commercial.suppliers.manage.
|
|
* - GET /api/supplier_addresses/{id} : lecture unitaire (security view) — la
|
|
* lecture courante reste via le parent. Pas de GET collection autonome.
|
|
* Tout passe par le SupplierAddressProcessor (rattachement parent). Les regles
|
|
* RG-2.05/2.06/2.09/2.10 sont portees par les contraintes de l'entite (jouees
|
|
* avant le processor).
|
|
*
|
|
* Audite (#[Auditable]) + Timestampable / Blamable.
|
|
*/
|
|
#[ApiResource(
|
|
operations: [
|
|
new Get(
|
|
security: "is_granted('commercial.suppliers.view')",
|
|
// site:read + category:read : embarquent les Site / Category lies
|
|
// (maillon (c)) plutot que des IRI nus dans le retour.
|
|
normalizationContext: ['groups' => ['supplier:item:read', 'site:read', 'category:read', 'default:read']],
|
|
),
|
|
new Post(
|
|
uriTemplate: '/suppliers/{supplierId}/addresses',
|
|
uriVariables: [
|
|
'supplierId' => new Link(fromClass: Supplier::class, toProperty: 'supplier'),
|
|
],
|
|
// read:false : pas de stade lecture du parent. Le Link toProperty
|
|
// resoudrait l'enfant (SELECT SupplierAddress ... WHERE supplier = :id)
|
|
// et casse en NonUniqueResult des >= 2 enfants. Le parent est rattache
|
|
// manuellement par SupplierAddressProcessor::linkParent (404 si absent).
|
|
read: false,
|
|
security: "is_granted('commercial.suppliers.manage')",
|
|
normalizationContext: ['groups' => ['supplier:item:read', 'site:read', 'category:read', 'default:read']],
|
|
denormalizationContext: ['groups' => ['supplier:write:addresses']],
|
|
processor: SupplierAddressProcessor::class,
|
|
),
|
|
new Patch(
|
|
security: "is_granted('commercial.suppliers.manage')",
|
|
normalizationContext: ['groups' => ['supplier:item:read', 'site:read', 'category:read', 'default:read']],
|
|
denormalizationContext: ['groups' => ['supplier:write:addresses']],
|
|
processor: SupplierAddressProcessor::class,
|
|
),
|
|
new Delete(
|
|
security: "is_granted('commercial.suppliers.manage')",
|
|
processor: SupplierAddressProcessor::class,
|
|
),
|
|
],
|
|
)]
|
|
#[ORM\Entity(repositoryClass: DoctrineSupplierAddressRepository::class)]
|
|
#[ORM\Table(name: 'supplier_address')]
|
|
#[ORM\Index(name: 'idx_supplier_address_supplier', columns: ['supplier_id'])]
|
|
#[Auditable]
|
|
class SupplierAddress implements TimestampableInterface, BlamableInterface
|
|
{
|
|
use TimestampableBlamableTrait;
|
|
|
|
/**
|
|
* Valeurs autorisees de address_type (RG-2.09). Miroir applicatif du CHECK BDD
|
|
* chk_supplier_address_type : alimente l'Assert\Choice (422 propre rattachee
|
|
* au champ avant la base) et reste la source des options cote front.
|
|
*/
|
|
public const array ADDRESS_TYPES = ['PROSPECT', 'DEPART', 'RENDU'];
|
|
|
|
/**
|
|
* RG-2.10 : seules les categories PORTANT ce type sont autorisees sur une
|
|
* adresse fournisseur. S'appuie sur CategoryInterface::getCategoryTypeCodes()
|
|
* (pas d'import du module Catalog — regle ABSOLUE n°1).
|
|
*/
|
|
private const string REQUIRED_CATEGORY_TYPE_CODE = 'FOURNISSEUR';
|
|
|
|
#[ORM\Id]
|
|
#[ORM\GeneratedValue]
|
|
#[ORM\Column]
|
|
#[Groups(['supplier:item:read'])]
|
|
private ?int $id = null;
|
|
|
|
#[ORM\ManyToOne(targetEntity: Supplier::class, inversedBy: 'addresses')]
|
|
#[ORM\JoinColumn(name: 'supplier_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
|
|
private ?Supplier $supplier = null;
|
|
|
|
// RG-2.09 : enum exclusif. La valeur est bornee par Assert\Choice (longueur de
|
|
// fait <= 8), d'ou la whitelist du miroir Assert\Length == ORM length (ERP-107,
|
|
// EntityConstraintsHaveFrenchMessageTest::EXCLUDED_LENGTH_MIRROR).
|
|
#[ORM\Column(length: 20)]
|
|
#[Assert\NotBlank(message: 'Le type d\'adresse est obligatoire.', normalizer: 'trim')]
|
|
#[Assert\Choice(choices: self::ADDRESS_TYPES, message: 'Le type d\'adresse doit être Prospect, Départ ou Rendu.')]
|
|
#[Groups(['supplier:item:read', 'supplier:write:addresses'])]
|
|
private ?string $addressType = null;
|
|
|
|
#[ORM\Column(length: 80, options: ['default' => 'France'])]
|
|
#[Assert\Length(max: 80, maxMessage: 'Le pays ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
|
#[Groups(['supplier:item:read', 'supplier:write:addresses'])]
|
|
private string $country = 'France';
|
|
|
|
// RG-2.05 : code postal a 4 ou 5 chiffres (pas de controle CP/ville serveur).
|
|
// Le Regex borne deja la longueur (<= 5) : pas de Length redondant (whitelist).
|
|
#[ORM\Column(length: 20)]
|
|
#[Assert\NotBlank(message: 'Le code postal est obligatoire.', normalizer: 'trim')]
|
|
#[Assert\Regex(pattern: '/^[0-9]{4,5}$/', message: 'Le code postal doit comporter 4 ou 5 chiffres.')]
|
|
#[Groups(['supplier:item:read', 'supplier:write:addresses'])]
|
|
private ?string $postalCode = null;
|
|
|
|
#[ORM\Column(length: 120)]
|
|
#[Assert\NotBlank(message: 'La ville est obligatoire.', normalizer: 'trim')]
|
|
#[Assert\Length(max: 120, maxMessage: 'La ville ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
|
#[Groups(['supplier:item:read', 'supplier:write:addresses'])]
|
|
private ?string $city = null;
|
|
|
|
#[ORM\Column(length: 255)]
|
|
#[Assert\NotBlank(message: 'La rue est obligatoire.', normalizer: 'trim')]
|
|
#[Assert\Length(max: 255, maxMessage: 'La rue ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
|
#[Groups(['supplier:item:read', 'supplier:write:addresses'])]
|
|
private ?string $street = null;
|
|
|
|
#[ORM\Column(length: 255, nullable: true)]
|
|
#[Assert\Length(max: 255, maxMessage: 'Le complément d\'adresse ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
|
#[Groups(['supplier:item:read', 'supplier:write:addresses'])]
|
|
private ?string $streetComplement = null;
|
|
|
|
// Specifique fournisseur : nombre de bennes sur le site.
|
|
#[ORM\Column(nullable: true)]
|
|
#[Assert\PositiveOrZero(message: 'Le nombre de bennes doit être un nombre positif ou nul.')]
|
|
#[Groups(['supplier:item:read', 'supplier:write:addresses'])]
|
|
private ?int $bennes = null;
|
|
|
|
// Specifique fournisseur : prestataire de triage sur cette adresse. Groupe
|
|
// d'ECRITURE uniquement sur la propriete ; le groupe de LECTURE est porte par
|
|
// le getter isTriageProvider() avec SerializedName('triageProvider') — sinon
|
|
// Symfony strip le prefixe "is" et droppe la cle (piege n°3 du M1).
|
|
#[ORM\Column(name: 'triage_provider', options: ['default' => false])]
|
|
#[Groups(['supplier:write:addresses'])]
|
|
private bool $triageProvider = false;
|
|
|
|
// Ordre d'affichage de l'adresse (gere serveur, non expose au M2).
|
|
#[ORM\Column(options: ['default' => 0])]
|
|
private int $position = 0;
|
|
|
|
// RG-2.06 : au moins un site rattache a chaque adresse.
|
|
/** @var Collection<int, SiteInterface> */
|
|
#[ORM\ManyToMany(targetEntity: SiteInterface::class)]
|
|
#[ORM\JoinTable(name: 'supplier_address_site')]
|
|
#[ORM\JoinColumn(name: 'supplier_address_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
|
|
#[ORM\InverseJoinColumn(name: 'site_id', referencedColumnName: 'id', onDelete: 'RESTRICT')]
|
|
#[Assert\Count(min: 1, minMessage: 'Au moins un site est obligatoire.')]
|
|
#[Groups(['supplier:item:read', 'supplier:write:addresses'])]
|
|
private Collection $sites;
|
|
|
|
/** @var Collection<int, SupplierContact> */
|
|
#[ORM\ManyToMany(targetEntity: SupplierContact::class)]
|
|
#[ORM\JoinTable(name: 'supplier_address_contact')]
|
|
#[ORM\JoinColumn(name: 'supplier_address_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
|
|
#[ORM\InverseJoinColumn(name: 'supplier_contact_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
|
|
#[Groups(['supplier:item:read', 'supplier:write:addresses'])]
|
|
private Collection $contacts;
|
|
|
|
// RG-2.10 : categories d'adresse de type FOURNISSEUR (controle au Processor).
|
|
/** @var Collection<int, CategoryInterface> */
|
|
#[ORM\ManyToMany(targetEntity: CategoryInterface::class)]
|
|
#[ORM\JoinTable(name: 'supplier_address_category')]
|
|
#[ORM\JoinColumn(name: 'supplier_address_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
|
|
#[ORM\InverseJoinColumn(name: 'category_id', referencedColumnName: 'id', onDelete: 'RESTRICT')]
|
|
#[Groups(['supplier:item:read', 'supplier:write:addresses'])]
|
|
private Collection $categories;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->sites = new ArrayCollection();
|
|
$this->contacts = new ArrayCollection();
|
|
$this->categories = new ArrayCollection();
|
|
}
|
|
|
|
/**
|
|
* RG-2.10 : toute categorie posee sur une adresse fournisseur doit etre de
|
|
* type FOURNISSEUR -> sinon 422 avec violation sur le champ `categories`
|
|
* (propertyPath aligne ERP-101, message FR ERP-107). S'appuie sur
|
|
* CategoryInterface::getCategoryTypeCodes() (multi-type — la categorie est
|
|
* acceptee des qu'elle PORTE le type FOURNISSEUR ; pas d'import du module
|
|
* Catalog, regle ABSOLUE n°1). Joue avant la base via la validation API Platform.
|
|
*/
|
|
#[Assert\Callback]
|
|
public function validateCategoryType(ExecutionContextInterface $context): void
|
|
{
|
|
foreach ($this->categories as $category) {
|
|
if ($category instanceof CategoryInterface
|
|
&& !in_array(self::REQUIRED_CATEGORY_TYPE_CODE, $category->getCategoryTypeCodes(), true)) {
|
|
$context->buildViolation('Type de catégorie non autorisé (FOURNISSEUR attendu).')
|
|
->atPath('categories')
|
|
->addViolation()
|
|
;
|
|
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function getId(): ?int
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function getSupplier(): ?Supplier
|
|
{
|
|
return $this->supplier;
|
|
}
|
|
|
|
public function setSupplier(?Supplier $supplier): static
|
|
{
|
|
$this->supplier = $supplier;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getAddressType(): ?string
|
|
{
|
|
return $this->addressType;
|
|
}
|
|
|
|
public function setAddressType(?string $addressType): static
|
|
{
|
|
$this->addressType = $addressType;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getCountry(): string
|
|
{
|
|
return $this->country;
|
|
}
|
|
|
|
public function setCountry(string $country): static
|
|
{
|
|
$this->country = $country;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getPostalCode(): ?string
|
|
{
|
|
return $this->postalCode;
|
|
}
|
|
|
|
public function setPostalCode(?string $postalCode): static
|
|
{
|
|
$this->postalCode = $postalCode;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getCity(): ?string
|
|
{
|
|
return $this->city;
|
|
}
|
|
|
|
public function setCity(?string $city): static
|
|
{
|
|
$this->city = $city;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getStreet(): ?string
|
|
{
|
|
return $this->street;
|
|
}
|
|
|
|
public function setStreet(?string $street): static
|
|
{
|
|
$this->street = $street;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getStreetComplement(): ?string
|
|
{
|
|
return $this->streetComplement;
|
|
}
|
|
|
|
public function setStreetComplement(?string $streetComplement): static
|
|
{
|
|
$this->streetComplement = $streetComplement;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getBennes(): ?int
|
|
{
|
|
return $this->bennes;
|
|
}
|
|
|
|
public function setBennes(?int $bennes): static
|
|
{
|
|
$this->bennes = $bennes;
|
|
|
|
return $this;
|
|
}
|
|
|
|
// Groupe de lecture + nom serialise explicite (cf. note sur la propriete) :
|
|
// sans SerializedName, Symfony exposerait la cle "triage" (strip du prefixe
|
|
// "is") et, le groupe etant sur la propriete `triageProvider`, droppait
|
|
// silencieusement la cle du JSON.
|
|
#[Groups(['supplier:item:read'])]
|
|
#[SerializedName('triageProvider')]
|
|
public function isTriageProvider(): bool
|
|
{
|
|
return $this->triageProvider;
|
|
}
|
|
|
|
public function setTriageProvider(bool $triageProvider): static
|
|
{
|
|
$this->triageProvider = $triageProvider;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getPosition(): int
|
|
{
|
|
return $this->position;
|
|
}
|
|
|
|
public function setPosition(int $position): static
|
|
{
|
|
$this->position = $position;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/** @return Collection<int, SiteInterface> */
|
|
public function getSites(): Collection
|
|
{
|
|
return $this->sites;
|
|
}
|
|
|
|
public function addSite(SiteInterface $site): static
|
|
{
|
|
if (!$this->sites->contains($site)) {
|
|
$this->sites->add($site);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function removeSite(SiteInterface $site): static
|
|
{
|
|
$this->sites->removeElement($site);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/** @return Collection<int, SupplierContact> */
|
|
public function getContacts(): Collection
|
|
{
|
|
return $this->contacts;
|
|
}
|
|
|
|
public function addContact(SupplierContact $contact): static
|
|
{
|
|
if (!$this->contacts->contains($contact)) {
|
|
$this->contacts->add($contact);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function removeContact(SupplierContact $contact): static
|
|
{
|
|
$this->contacts->removeElement($contact);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/** @return Collection<int, CategoryInterface> */
|
|
public function getCategories(): Collection
|
|
{
|
|
return $this->categories;
|
|
}
|
|
|
|
public function addCategory(CategoryInterface $category): static
|
|
{
|
|
if (!$this->categories->contains($category)) {
|
|
$this->categories->add($category);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function removeCategory(CategoryInterface $category): static
|
|
{
|
|
$this->categories->removeElement($category);
|
|
|
|
return $this;
|
|
}
|
|
}
|