145d4362db
Auto Tag Develop / tag (push) Successful in 7s
## ERP-88 — Sous-ressources M2 (contacts / adresses / ribs) Étape 4/7 du pipeline M2. Dépend de #86 (entités) et #87 (Provider/Processor). Bloque #92. ### Contenu Opérations API Platform + Processors d'écriture des sous-collections du fournisseur (POST/PATCH/DELETE + GET unitaire). **SupplierContactProcessor** - Rattachement au fournisseur parent (404 si absent). - Normalisation serveur RG-2.12 (Title Case nom/prénom, téléphones chiffres seuls, email lowercase). - RG-2.04 : firstName **ou** lastName obligatoire (422 sur `firstName`). - DELETE libre (RG-2.13 front-driven : collection peut rester vide côté back). **SupplierAddressProcessor** - Rattachement au fournisseur parent. - RG-2.05 (CP `^[0-9]{4,5}$`), RG-2.06 (≥1 site), RG-2.09 (type d'adresse) portées par les contraintes d'entité (ERP-86). - RG-2.10 (catégorie de type FOURNISSEUR) ajoutée via `Assert\Callback validateCategoryType` (propertyPath=`categories`). **SupplierRibProcessor** - Rattachement au fournisseur parent. - RG-2.08 : refus du DELETE du dernier RIB quand `paymentType.code = LCR` → **409**. ### Security différenciée | Sous-ressource | Écriture | Lecture | |---|---|---| | contacts / adresses | `commercial.suppliers.manage` | `commercial.suppliers.view` | | ribs | `commercial.suppliers.accounting.manage` | `commercial.suppliers.accounting.view` | POST en `read:false` (parent rattaché manuellement) — parade NonUniqueResult héritée du M1. Messages FR (ERP-107) + `violations[].propertyPath` aligné (ERP-101). ### Vérifications - `make php-cs-fixer-allow-risky` : 0 fichier à corriger - `make test` : 483 tests OK - `debug:router` : 12 routes générées (4 par sous-ressource) ### Hors périmètre (tickets suivants) - Déclaration RBAC `commercial.suppliers.*` dans `CommercialModule` (#7) — sans elle, l'accès reste 403. - Tests fonctionnels de la matrice RG (#8) — dépendent du RBAC + fixtures Supplier. ### Notes de review (non bloquantes, alignées M1) - `position` des sous-collections non exposé à l'API (décision ERP-86, géré serveur). - M2M `SupplierAddress.contacts` non vérifié same-supplier — comportement identique au M1 (ClientAddress), à traiter globalement si besoin. --------- Co-authored-by: Matthieu <contact@malio.fr> Reviewed-on: #67 Co-authored-by: THOLOT DECHENE Matthieu <matthieu@yuno.malio.fr> Co-committed-by: THOLOT DECHENE Matthieu <matthieu@yuno.malio.fr>
438 lines
16 KiB
PHP
438 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 de ce type sont autorisees sur une adresse
|
|
* fournisseur. S'appuie sur CategoryInterface::getCategoryTypeCode() (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::getCategoryTypeCode() (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
|
|
&& self::REQUIRED_CATEGORY_TYPE_CODE !== $category->getCategoryTypeCode()) {
|
|
$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;
|
|
}
|
|
}
|