diff --git a/frontend/i18n/locales/fr.json b/frontend/i18n/locales/fr.json index cf7bc63..39a87f1 100644 --- a/frontend/i18n/locales/fr.json +++ b/frontend/i18n/locales/fr.json @@ -259,7 +259,11 @@ "commercial_client": "Client", "commercial_clientaddress": "Adresse client", "commercial_clientcontact": "Contact client", - "commercial_clientrib": "RIB client" + "commercial_clientrib": "RIB client", + "commercial_supplier": "Fournisseur", + "commercial_supplieraddress": "Adresse fournisseur", + "commercial_suppliercontact": "Contact fournisseur", + "commercial_supplierrib": "RIB fournisseur" }, "empty": "Aucune activité enregistrée", "no_results": "Aucun résultat pour ces filtres", diff --git a/src/Module/Commercial/Domain/Entity/Supplier.php b/src/Module/Commercial/Domain/Entity/Supplier.php new file mode 100644 index 0000000..3ac103c --- /dev/null +++ b/src/Module/Commercial/Domain/Entity/Supplier.php @@ -0,0 +1,582 @@ + Category). Embarquee en LISTE + // ET DETAIL (coherence M1/ERP-62) ; maillon (c) : le contexte inclut + // 'category:read' pour exposer id/code/name. + /** @var Collection */ + #[ORM\ManyToMany(targetEntity: CategoryInterface::class)] + #[ORM\JoinTable(name: 'supplier_category')] + #[ORM\JoinColumn(name: 'supplier_id', referencedColumnName: 'id', onDelete: 'CASCADE')] + #[ORM\InverseJoinColumn(name: 'category_id', referencedColumnName: 'id', onDelete: 'RESTRICT')] + #[Assert\Count(min: 1, minMessage: 'Au moins une catégorie est obligatoire.')] + #[Groups(['supplier:read', 'supplier:write:main'])] + private Collection $categories; + + // === Onglet Information === + #[ORM\Column(type: 'text', nullable: true)] + #[Groups(['supplier:read', 'supplier:write:information'])] + private ?string $description = null; + + #[ORM\Column(length: 255, nullable: true)] + #[Assert\Length(max: 255, maxMessage: 'Ce champ ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')] + #[Groups(['supplier:read', 'supplier:write:information'])] + private ?string $competitors = null; + + #[ORM\Column(type: 'date_immutable', nullable: true)] + #[Groups(['supplier:read', 'supplier:write:information'])] + private ?DateTimeImmutable $foundedAt = null; + + #[ORM\Column(nullable: true)] + #[Assert\PositiveOrZero(message: 'L\'effectif doit être un nombre positif ou nul.')] + #[Groups(['supplier:read', 'supplier:write:information'])] + private ?int $employeesCount = null; + + #[ORM\Column(type: 'decimal', precision: 15, scale: 2, nullable: true)] + #[Groups(['supplier:read', 'supplier:write:information'])] + private ?string $revenueAmount = null; + + #[ORM\Column(length: 120, nullable: true)] + #[Assert\Length(max: 120, maxMessage: 'Le nom du dirigeant ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')] + #[Groups(['supplier:read', 'supplier:write:information'])] + private ?string $directorName = null; + + #[ORM\Column(type: 'decimal', precision: 15, scale: 2, nullable: true)] + #[Groups(['supplier:read', 'supplier:write:information'])] + private ?string $profitAmount = null; + + // NEW vs Client : Volume previsionnel (entier). + #[ORM\Column(nullable: true)] + #[Assert\PositiveOrZero(message: 'Le volume prévisionnel doit être un nombre positif ou nul.')] + #[Groups(['supplier:read', 'supplier:write:information'])] + private ?int $volumeForecast = null; + + // === Onglet Comptabilite === + // Lecture conditionnee via le groupe `supplier:read:accounting` (ajoute au + // contexte par le SupplierProvider si l'user a accounting.view, ERP-87). + // Ecriture via `supplier:write:accounting` (le Processor exige accounting.manage). + #[ORM\Column(length: 20, nullable: true)] + #[Assert\Length(max: 20, maxMessage: 'Le SIREN ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')] + #[Groups(['supplier:read:accounting', 'supplier:write:accounting'])] + private ?string $siren = null; + + #[ORM\Column(length: 40, nullable: true)] + #[Assert\Length(max: 40, maxMessage: 'Le numéro de compte ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')] + #[Groups(['supplier:read:accounting', 'supplier:write:accounting'])] + private ?string $accountNumber = null; + + #[ORM\ManyToOne(targetEntity: TvaMode::class)] + #[ORM\JoinColumn(name: 'tva_mode_id', referencedColumnName: 'id', nullable: true, onDelete: 'RESTRICT')] + #[Groups(['supplier:read:accounting', 'supplier:write:accounting'])] + private ?TvaMode $tvaMode = null; + + #[ORM\Column(length: 40, nullable: true)] + #[Assert\Length(max: 40, maxMessage: 'Le numéro de TVA ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')] + #[Groups(['supplier:read:accounting', 'supplier:write:accounting'])] + private ?string $nTva = null; + + #[ORM\ManyToOne(targetEntity: PaymentDelay::class)] + #[ORM\JoinColumn(name: 'payment_delay_id', referencedColumnName: 'id', nullable: true, onDelete: 'RESTRICT')] + #[Groups(['supplier:read:accounting', 'supplier:write:accounting'])] + private ?PaymentDelay $paymentDelay = null; + + #[ORM\ManyToOne(targetEntity: PaymentType::class)] + #[ORM\JoinColumn(name: 'payment_type_id', referencedColumnName: 'id', nullable: true, onDelete: 'RESTRICT')] + #[Groups(['supplier:read:accounting', 'supplier:write:accounting'])] + private ?PaymentType $paymentType = null; + + #[ORM\ManyToOne(targetEntity: Bank::class)] + #[ORM\JoinColumn(name: 'bank_id', referencedColumnName: 'id', nullable: true, onDelete: 'RESTRICT')] + #[Groups(['supplier:read:accounting', 'supplier:write:accounting'])] + private ?Bank $bank = null; + + // === Sous-collections — EMBARQUEES dans le DETAIL (RETEX M1 §2) === + // Maillon (a) : le read-group est porte par le GETTER (getContacts / getAddresses + // / getRibs) — sans #[Groups], jamais serialisees. Edition via sous-ressources + // POST/PATCH/DELETE (ERP-88). + /** @var Collection */ + #[ORM\OneToMany(mappedBy: 'supplier', targetEntity: SupplierContact::class, cascade: ['persist', 'remove'], orphanRemoval: true)] + private Collection $contacts; + + /** @var Collection */ + #[ORM\OneToMany(mappedBy: 'supplier', targetEntity: SupplierAddress::class, cascade: ['persist', 'remove'], orphanRemoval: true)] + private Collection $addresses; + + /** @var Collection */ + #[ORM\OneToMany(mappedBy: 'supplier', targetEntity: SupplierRib::class, cascade: ['persist', 'remove'], orphanRemoval: true)] + private Collection $ribs; + + // === Archive / Soft delete === + // Groupe d'ECRITURE uniquement sur la propriete (denormalisation PATCH archive). + // Le groupe de LECTURE est declare sur le getter isArchived() avec + // SerializedName('isArchived') : sans cela, Symfony strip le prefixe "is" et + // exposerait la cle JSON "archived" — en pratique la cle est totalement + // DROPPEE (piege n°3 du M1). Pattern corrige : Groups + SerializedName sur le getter. + #[ORM\Column(name: 'is_archived', options: ['default' => false])] + #[Groups(['supplier:write:archive'])] + private bool $isArchived = false; + + #[ORM\Column(type: 'datetime_immutable', nullable: true)] + #[Groups(['supplier:read'])] + private ?DateTimeImmutable $archivedAt = null; + + // Soft delete technique (HP M3) : non expose en lecture/ecriture au M2. + #[ORM\Column(type: 'datetime_immutable', nullable: true)] + private ?DateTimeImmutable $deletedAt = null; + + public function __construct() + { + $this->categories = new ArrayCollection(); + $this->contacts = new ArrayCollection(); + $this->addresses = new ArrayCollection(); + $this->ribs = new ArrayCollection(); + } + + public function getId(): ?int + { + return $this->id; + } + + public function getCompanyName(): ?string + { + return $this->companyName; + } + + public function setCompanyName(string $companyName): static + { + $this->companyName = $companyName; + + return $this; + } + + /** @return Collection */ + 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; + } + + public function getDescription(): ?string + { + return $this->description; + } + + public function setDescription(?string $description): static + { + $this->description = $description; + + return $this; + } + + public function getCompetitors(): ?string + { + return $this->competitors; + } + + public function setCompetitors(?string $competitors): static + { + $this->competitors = $competitors; + + return $this; + } + + public function getFoundedAt(): ?DateTimeImmutable + { + return $this->foundedAt; + } + + public function setFoundedAt(?DateTimeImmutable $foundedAt): static + { + $this->foundedAt = $foundedAt; + + return $this; + } + + public function getEmployeesCount(): ?int + { + return $this->employeesCount; + } + + public function setEmployeesCount(?int $employeesCount): static + { + $this->employeesCount = $employeesCount; + + return $this; + } + + public function getRevenueAmount(): ?string + { + return $this->revenueAmount; + } + + public function setRevenueAmount(?string $revenueAmount): static + { + $this->revenueAmount = $revenueAmount; + + return $this; + } + + public function getDirectorName(): ?string + { + return $this->directorName; + } + + public function setDirectorName(?string $directorName): static + { + $this->directorName = $directorName; + + return $this; + } + + public function getProfitAmount(): ?string + { + return $this->profitAmount; + } + + public function setProfitAmount(?string $profitAmount): static + { + $this->profitAmount = $profitAmount; + + return $this; + } + + public function getVolumeForecast(): ?int + { + return $this->volumeForecast; + } + + public function setVolumeForecast(?int $volumeForecast): static + { + $this->volumeForecast = $volumeForecast; + + return $this; + } + + public function getSiren(): ?string + { + return $this->siren; + } + + public function setSiren(?string $siren): static + { + $this->siren = $siren; + + return $this; + } + + public function getAccountNumber(): ?string + { + return $this->accountNumber; + } + + public function setAccountNumber(?string $accountNumber): static + { + $this->accountNumber = $accountNumber; + + return $this; + } + + public function getTvaMode(): ?TvaMode + { + return $this->tvaMode; + } + + public function setTvaMode(?TvaMode $tvaMode): static + { + $this->tvaMode = $tvaMode; + + return $this; + } + + public function getNTva(): ?string + { + return $this->nTva; + } + + public function setNTva(?string $nTva): static + { + $this->nTva = $nTva; + + return $this; + } + + public function getPaymentDelay(): ?PaymentDelay + { + return $this->paymentDelay; + } + + public function setPaymentDelay(?PaymentDelay $paymentDelay): static + { + $this->paymentDelay = $paymentDelay; + + return $this; + } + + public function getPaymentType(): ?PaymentType + { + return $this->paymentType; + } + + public function setPaymentType(?PaymentType $paymentType): static + { + $this->paymentType = $paymentType; + + return $this; + } + + public function getBank(): ?Bank + { + return $this->bank; + } + + public function setBank(?Bank $bank): static + { + $this->bank = $bank; + + return $this; + } + + /** @return Collection */ + #[Groups(['supplier:item:read'])] + public function getContacts(): Collection + { + return $this->contacts; + } + + public function addContact(SupplierContact $contact): static + { + if (!$this->contacts->contains($contact)) { + $this->contacts->add($contact); + $contact->setSupplier($this); + } + + return $this; + } + + public function removeContact(SupplierContact $contact): static + { + if ($this->contacts->removeElement($contact) && $contact->getSupplier() === $this) { + $contact->setSupplier(null); + } + + return $this; + } + + /** @return Collection */ + #[Groups(['supplier:item:read'])] + public function getAddresses(): Collection + { + return $this->addresses; + } + + public function addAddress(SupplierAddress $address): static + { + if (!$this->addresses->contains($address)) { + $this->addresses->add($address); + $address->setSupplier($this); + } + + return $this; + } + + public function removeAddress(SupplierAddress $address): static + { + if ($this->addresses->removeElement($address) && $address->getSupplier() === $this) { + $address->setSupplier(null); + } + + return $this; + } + + /** + * Sites distincts rattaches a au moins une adresse du fournisseur (RG-2.06). + * Le fournisseur ne porte pas de sites en propre : ils vivent sur les + * adresses. Agrege en lecture seule pour la colonne « Site(s) » du Repertoire + * (badges colores) — expose en LISTE via le groupe supplier:read (les adresses + * completes restent reservees au detail, supplier:item:read). Site n'a pas de + * champ `code` : libelle = `name`, prefixe = `postalCode` (§ 2.4 / § 4.0.ter). + * + * Fetch-join obligatoire (addresses.sites) cote repository pour eviter le N+1 + * a la serialisation de la liste (cf. DoctrineSupplierRepository, § 2.12). + * + * @return list + */ + #[Groups(['supplier:read'])] + public function getSites(): array + { + $sites = []; + foreach ($this->addresses as $address) { + foreach ($address->getSites() as $site) { + // Deduplication par identite d'objet : un meme site peut etre + // rattache a plusieurs adresses du fournisseur. + $sites[spl_object_id($site)] = $site; + } + } + + return array_values($sites); + } + + // Embed gate sur le groupe COMPTABLE (et non supplier:item:read comme contacts/ + // adresses) : supplier:read:accounting n'est ajoute au contexte que si l'user a + // accounting.view (SupplierProvider, ERP-87). Resultat : la cle `ribs` est + // TOTALEMENT ABSENTE du detail pour un user sans accounting.view (ex. Commerciale), + // au meme titre que les scalaires comptables — evite la fuite IBAN/BIC (piege n°4 M1). + /** @return Collection */ + #[Groups(['supplier:read:accounting'])] + public function getRibs(): Collection + { + return $this->ribs; + } + + public function addRib(SupplierRib $rib): static + { + if (!$this->ribs->contains($rib)) { + $this->ribs->add($rib); + $rib->setSupplier($this); + } + + return $this; + } + + public function removeRib(SupplierRib $rib): static + { + if ($this->ribs->removeElement($rib) && $rib->getSupplier() === $this) { + $rib->setSupplier(null); + } + + return $this; + } + + // Groupe de lecture + nom serialise explicite : sans SerializedName, Symfony + // exposerait la cle "archived" (strip du prefixe "is" sur les getters) et + // droppait silencieusement la cle du JSON (piege n°3 du M1). + #[Groups(['supplier:read'])] + #[SerializedName('isArchived')] + public function isArchived(): bool + { + return $this->isArchived; + } + + public function setIsArchived(bool $isArchived): static + { + $this->isArchived = $isArchived; + + return $this; + } + + public function getArchivedAt(): ?DateTimeImmutable + { + return $this->archivedAt; + } + + public function setArchivedAt(?DateTimeImmutable $archivedAt): static + { + $this->archivedAt = $archivedAt; + + return $this; + } + + public function getDeletedAt(): ?DateTimeImmutable + { + return $this->deletedAt; + } + + public function setDeletedAt(?DateTimeImmutable $deletedAt): static + { + $this->deletedAt = $deletedAt; + + return $this; + } +} diff --git a/src/Module/Commercial/Domain/Entity/SupplierAddress.php b/src/Module/Commercial/Domain/Entity/SupplierAddress.php new file mode 100644 index 0000000..549a2cf --- /dev/null +++ b/src/Module/Commercial/Domain/Entity/SupplierAddress.php @@ -0,0 +1,353 @@ + '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 */ + #[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 */ + #[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 */ + #[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(); + } + + 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 */ + 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 */ + 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 */ + 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; + } +} diff --git a/src/Module/Commercial/Domain/Entity/SupplierContact.php b/src/Module/Commercial/Domain/Entity/SupplierContact.php new file mode 100644 index 0000000..37a51ce --- /dev/null +++ b/src/Module/Commercial/Domain/Entity/SupplierContact.php @@ -0,0 +1,187 @@ + 0])] + private int $position = 0; + + 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 getFirstName(): ?string + { + return $this->firstName; + } + + public function setFirstName(?string $firstName): static + { + $this->firstName = $firstName; + + return $this; + } + + public function getLastName(): ?string + { + return $this->lastName; + } + + public function setLastName(?string $lastName): static + { + $this->lastName = $lastName; + + return $this; + } + + public function getJobTitle(): ?string + { + return $this->jobTitle; + } + + public function setJobTitle(?string $jobTitle): static + { + $this->jobTitle = $jobTitle; + + return $this; + } + + public function getPhonePrimary(): ?string + { + return $this->phonePrimary; + } + + public function setPhonePrimary(?string $phonePrimary): static + { + $this->phonePrimary = $phonePrimary; + + return $this; + } + + public function getPhoneSecondary(): ?string + { + return $this->phoneSecondary; + } + + public function setPhoneSecondary(?string $phoneSecondary): static + { + $this->phoneSecondary = $phoneSecondary; + + return $this; + } + + public function getEmail(): ?string + { + return $this->email; + } + + public function setEmail(?string $email): static + { + $this->email = $email; + + return $this; + } + + public function getPosition(): int + { + return $this->position; + } + + public function setPosition(int $position): static + { + $this->position = $position; + + return $this; + } +} diff --git a/src/Module/Commercial/Domain/Entity/SupplierRib.php b/src/Module/Commercial/Domain/Entity/SupplierRib.php new file mode 100644 index 0000000..60806d7 --- /dev/null +++ b/src/Module/Commercial/Domain/Entity/SupplierRib.php @@ -0,0 +1,136 @@ + 0])] + private int $position = 0; + + 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 getLabel(): ?string + { + return $this->label; + } + + public function setLabel(string $label): static + { + $this->label = $label; + + return $this; + } + + public function getBic(): ?string + { + return $this->bic; + } + + public function setBic(string $bic): static + { + $this->bic = $bic; + + return $this; + } + + public function getIban(): ?string + { + return $this->iban; + } + + public function setIban(string $iban): static + { + $this->iban = $iban; + + return $this; + } + + public function getPosition(): int + { + return $this->position; + } + + public function setPosition(int $position): static + { + $this->position = $position; + + return $this; + } +} diff --git a/src/Module/Commercial/Domain/Repository/SupplierAddressRepositoryInterface.php b/src/Module/Commercial/Domain/Repository/SupplierAddressRepositoryInterface.php new file mode 100644 index 0000000..ff3ed5d --- /dev/null +++ b/src/Module/Commercial/Domain/Repository/SupplierAddressRepositoryInterface.php @@ -0,0 +1,14 @@ + uniquement les archives (is_archived = true) ; + * - sinon $includeArchived = true -> actifs + archives (echappatoire) ; + * - sinon (defaut) -> uniquement les actifs (is_archived = false). + * $archivedOnly a la priorite sur $includeArchived. + * - Tri par defaut : companyName ASC (RG-2.17). + * - $search : recherche fuzzy insensible a la casse sur companyName + les + * contacts lies (firstName / lastName / email) via sous-requete (D1, + * refonte-contact §4.1). Metacaracteres LIKE echappes. Ignore si null/vide. + * - $categoryCodes : restreint aux fournisseurs possedant au moins une + * categorie dont le code est dans la liste (OR). Liste vide = pas de filtre. + * - $siteIds : restreint aux fournisseurs ayant au moins une adresse rattachee + * a l'un des sites donnes (OR — RG-2.06). Liste vide = pas de filtre. + * + * Filtrage centralise ICI (et non dans le provider/controller) pour que la + * liste paginee (SupplierProvider) et l'export (SupplierExportController) + * partagent strictement la meme logique de selection. + * + * Contrat = SELECTION uniquement (filtres + tri). Aucun fetch-join to-many : + * l'hydratation des collections affichees est deleguee a + * {@see self::hydrateListCollections()} pour ne pas imposer le cout d'un + * produit cartesien aux chemins non pagines (cf. M1/ERP-100). + * + * @param list $categoryCodes + * @param list $siteIds + */ + public function createListQueryBuilder( + bool $includeArchived = false, + ?string $search = null, + array $categoryCodes = [], + array $siteIds = [], + bool $archivedOnly = false, + ): QueryBuilder; + + /** + * Hydrate en lot les collections affichees par le repertoire (categories, + * adresses et leurs sites) sur un jeu de fournisseurs DEJA charges, via + * l'identity map Doctrine (memes instances). A appeler apres une selection + * bornee (page courante ou jeu d'export) pour eviter le N+1 a la + * serialisation, sans imposer de fetch-join au QueryBuilder de selection + * (anti N+1, § 2.12). + * + * Charge les categories et les adresses/sites en DEUX requetes distinctes + * (et non un triple fetch-join) pour ne pas multiplier categories x adresses + * x sites en un seul produit cartesien. + * + * @param list $suppliers + */ + public function hydrateListCollections(array $suppliers): void; +} diff --git a/src/Module/Commercial/Domain/Repository/SupplierRibRepositoryInterface.php b/src/Module/Commercial/Domain/Repository/SupplierRibRepositoryInterface.php new file mode 100644 index 0000000..4f9c36b --- /dev/null +++ b/src/Module/Commercial/Domain/Repository/SupplierRibRepositoryInterface.php @@ -0,0 +1,14 @@ + + */ +class DoctrineSupplierAddressRepository extends ServiceEntityRepository implements SupplierAddressRepositoryInterface +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, SupplierAddress::class); + } + + public function findById(int $id): ?SupplierAddress + { + return $this->find($id); + } + + public function save(SupplierAddress $address): void + { + $this->getEntityManager()->persist($address); + $this->getEntityManager()->flush(); + } +} diff --git a/src/Module/Commercial/Infrastructure/Doctrine/DoctrineSupplierContactRepository.php b/src/Module/Commercial/Infrastructure/Doctrine/DoctrineSupplierContactRepository.php new file mode 100644 index 0000000..72ffb95 --- /dev/null +++ b/src/Module/Commercial/Infrastructure/Doctrine/DoctrineSupplierContactRepository.php @@ -0,0 +1,32 @@ + + */ +class DoctrineSupplierContactRepository extends ServiceEntityRepository implements SupplierContactRepositoryInterface +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, SupplierContact::class); + } + + public function findById(int $id): ?SupplierContact + { + return $this->find($id); + } + + public function save(SupplierContact $contact): void + { + $this->getEntityManager()->persist($contact); + $this->getEntityManager()->flush(); + } +} diff --git a/src/Module/Commercial/Infrastructure/Doctrine/DoctrineSupplierRepository.php b/src/Module/Commercial/Infrastructure/Doctrine/DoctrineSupplierRepository.php new file mode 100644 index 0000000..95d503a --- /dev/null +++ b/src/Module/Commercial/Infrastructure/Doctrine/DoctrineSupplierRepository.php @@ -0,0 +1,239 @@ + + */ +class DoctrineSupplierRepository extends ServiceEntityRepository implements SupplierRepositoryInterface +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Supplier::class); + } + + public function findById(int $id): ?Supplier + { + return $this->find($id); + } + + public function save(Supplier $supplier): void + { + $this->getEntityManager()->persist($supplier); + $this->getEntityManager()->flush(); + } + + public function createListQueryBuilder( + bool $includeArchived = false, + ?string $search = null, + array $categoryCodes = [], + array $siteIds = [], + bool $archivedOnly = false, + ): QueryBuilder { + // SELECTION uniquement (filtres + tri) : pas de fetch-join to-many ici. + // L'hydratation des collections affichees (Catégories / Site(s)) est + // deleguee a hydrateListCollections() une fois le jeu borne, pour ne pas + // imposer un produit cartesien aux chemins non pagines (export, + // ?pagination=false) — § 2.12 (cf. M1/ERP-100). + $qb = $this->createQueryBuilder('s') + ->andWhere('s.deletedAt IS NULL') + ->orderBy('s.companyName', 'ASC') + ; + + // Perimetre d'archivage : archivedOnly prioritaire sur includeArchived. + if ($archivedOnly) { + $qb->andWhere('s.isArchived = true'); + } elseif (!$includeArchived) { + $qb->andWhere('s.isArchived = false'); + } + + $this->applySearch($qb, $search); + $this->applyCategoryCodes($qb, $categoryCodes); + $this->applySiteIds($qb, $siteIds); + + return $qb; + } + + public function hydrateListCollections(array $suppliers): void + { + if ([] === $suppliers) { + return; + } + + // Ids des fournisseurs deja charges (entites managees). Les requetes + // ci-dessous renvoient les MEMES instances Supplier (identity map), dont + // les collections sont alors remplies — anti N+1 a la serialisation. + $ids = []; + foreach ($suppliers as $supplier) { + $id = $supplier->getId(); + if (null !== $id) { + $ids[] = $id; + } + } + if ([] === $ids) { + return; + } + + // 1re passe : categories (colonne « Catégories »). Produit s x cat seul. + $this->createQueryBuilder('s') + ->leftJoin('s.categories', 'cat')->addSelect('cat') + ->where('s.id IN (:ids)')->setParameter('ids', $ids) + ->getQuery() + ->getResult() + ; + + // 2e passe : adresses + sites (colonne « Site(s) », sites portes par les + // adresses — RG-2.06). Le join addr -> site reste imbrique mais n'est plus + // multiplie par les categories : le cartesien global est casse. + $this->createQueryBuilder('s') + ->leftJoin('s.addresses', 'addr')->addSelect('addr') + ->leftJoin('addr.sites', 'site')->addSelect('site') + ->where('s.id IN (:ids)')->setParameter('ids', $ids) + ->getQuery() + ->getResult() + ; + } + + /** + * Recherche fuzzy insensible a la casse sur companyName ET sur les contacts + * lies (firstName / lastName / email) — decision D1, refonte-contact (§ 4.1). + * Les deux criteres sont unis par OR : un fournisseur matche si son nom de + * societe OU l'un de ses contacts matche. Le critere contact passe par une + * sous-requete IN (plutot qu'un JOIN sur la collection) pour ne pas perturber + * le DISTINCT / ORDER BY / pagination principal. Les metacaracteres LIKE + * (%, _, \) saisis sont echappes pour rester litteraux. + */ + private function applySearch(QueryBuilder $qb, ?string $search): void + { + if (null === $search || '' === trim($search)) { + return; + } + + $escaped = str_replace(['\\', '%', '_'], ['\\\\', '\%', '\_'], trim($search)); + $pattern = '%'.mb_strtolower($escaped, 'UTF-8').'%'; + + $contactSub = $this->getEntityManager()->createQueryBuilder() + ->select('s2.id') + ->from(Supplier::class, 's2') + ->join('s2.contacts', 'sc2') + ->where('LOWER(sc2.firstName) LIKE :search') + ->orWhere('LOWER(sc2.lastName) LIKE :search') + ->orWhere('LOWER(sc2.email) LIKE :search') + ; + + $qb->andWhere( + $qb->expr()->orX( + 'LOWER(s.companyName) LIKE :search', + $qb->expr()->in('s.id', $contactSub->getDQL()), + ), + )->setParameter('search', $pattern); + } + + /** + * Restreint aux fournisseurs possedant au moins une categorie dont le code + * figure dans la liste (OR). Alimente le filtre « Catégories » du drawer. + * Sous-requete IN (plutot qu'un JOIN sur la collection M2M) pour ne pas + * perturber le DISTINCT / ORDER BY principal. + * + * @param list $categoryCodes + */ + private function applyCategoryCodes(QueryBuilder $qb, array $categoryCodes): void + { + $codes = $this->normalizeStringList($categoryCodes); + if ([] === $codes) { + return; + } + + $sub = $this->getEntityManager()->createQueryBuilder() + ->select('s3.id') + ->from(Supplier::class, 's3') + ->join('s3.categories', 'cat3') + ->where('cat3.code IN (:categoryCodes)') + ; + + $qb->andWhere($qb->expr()->in('s.id', $sub->getDQL())) + ->setParameter('categoryCodes', $codes) + ; + } + + /** + * Restreint aux fournisseurs ayant au moins une adresse rattachee a l'un des + * sites donnes (OR — RG-2.06 : les sites vivent sur les adresses). Sous-requete + * IN pour ne pas perturber le tri/pagination principal. + * + * @param list $siteIds + */ + private function applySiteIds(QueryBuilder $qb, array $siteIds): void + { + $ids = $this->normalizeIntList($siteIds); + if ([] === $ids) { + return; + } + + $sub = $this->getEntityManager()->createQueryBuilder() + ->select('s4.id') + ->from(Supplier::class, 's4') + ->join('s4.addresses', 'addr4') + ->join('addr4.sites', 'site4') + ->where('site4.id IN (:siteIds)') + ; + + $qb->andWhere($qb->expr()->in('s.id', $sub->getDQL())) + ->setParameter('siteIds', $ids) + ; + } + + /** + * Nettoie une liste de chaines : trim, retrait des vides, reindexation. + * Defensive : tolere des elements scalaires non-string (cast) et ignore le + * reste sans lever de TypeError, le contrat etant de normaliser une entree + * potentiellement brute (query params). + * + * @param array $values + * + * @return list + */ + private function normalizeStringList(array $values): array + { + $out = []; + foreach ($values as $value) { + if (is_string($value) || is_int($value) || is_float($value)) { + $trimmed = trim((string) $value); + if ('' !== $trimmed) { + $out[] = $trimmed; + } + } + } + + return $out; + } + + /** + * Nettoie une liste d'identifiants : cast int, retrait des <= 0, reindexation. + * Defensive (cf. normalizeStringList) : accepte des entiers ou des chaines + * numeriques ('1', '2') sans TypeError, ignore le reste. + * + * @param array $values + * + * @return list + */ + private function normalizeIntList(array $values): array + { + $out = []; + foreach ($values as $value) { + if (is_numeric($value) && (int) $value > 0) { + $out[] = (int) $value; + } + } + + return $out; + } +} diff --git a/src/Module/Commercial/Infrastructure/Doctrine/DoctrineSupplierRibRepository.php b/src/Module/Commercial/Infrastructure/Doctrine/DoctrineSupplierRibRepository.php new file mode 100644 index 0000000..8c5869d --- /dev/null +++ b/src/Module/Commercial/Infrastructure/Doctrine/DoctrineSupplierRibRepository.php @@ -0,0 +1,32 @@ + + */ +class DoctrineSupplierRibRepository extends ServiceEntityRepository implements SupplierRibRepositoryInterface +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, SupplierRib::class); + } + + public function findById(int $id): ?SupplierRib + { + return $this->find($id); + } + + public function save(SupplierRib $rib): void + { + $this->getEntityManager()->persist($rib); + $this->getEntityManager()->flush(); + } +} diff --git a/tests/Architecture/EntityConstraintsHaveFrenchMessageTest.php b/tests/Architecture/EntityConstraintsHaveFrenchMessageTest.php index 9673d5b..fb5d338 100644 --- a/tests/Architecture/EntityConstraintsHaveFrenchMessageTest.php +++ b/tests/Architecture/EntityConstraintsHaveFrenchMessageTest.php @@ -52,6 +52,10 @@ final class EntityConstraintsHaveFrenchMessageTest extends TestCase private const array EXCLUDED_LENGTH_MIRROR = [ // Le Regex /^[0-9]{4,5}$/ borne deja la longueur a 5 caracteres (< 20). 'ClientAddress::postalCode' => 'Regex {4,5} borne deja la longueur.', + // Idem cote fournisseur (meme Regex CP). + 'SupplierAddress::postalCode' => 'Regex {4,5} borne deja la longueur.', + // Le Choice {PROSPECT,DEPART,RENDU} borne les valeurs (<= 8 < 20). + 'SupplierAddress::addressType' => 'Choice {PROSPECT,DEPART,RENDU} borne deja les valeurs.', // Le Regex /^#[0-9A-Fa-f]{6}$/ borne la longueur a exactement 7 caracteres. 'Site::color' => 'Regex code hex #RRGGBB borne deja la longueur.', ]; @@ -70,6 +74,7 @@ final class EntityConstraintsHaveFrenchMessageTest extends TestCase Assert\NotBlank::class, Assert\NotNull::class, Assert\Email::class, + Assert\Choice::class, Assert\Regex::class, Assert\Bic::class, Assert\Iban::class,