feat: auto-création contact Directeur à la création client et à l'ouverture fiche

ensureDefaultContact() :
- Vérifie si le client a au moins 1 contact dans CustomerContact
- Si aucun contact : crée automatiquement un contact avec
  firstName/lastName du client, email, phone, role='Directeur',
  isBillingEmail=true
- Appelé à la création du client (après flush)
- Appelé à l'ouverture de la fiche client (show) pour les clients
  existants qui n'ont pas encore de contact

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Serreau Jovann
2026-04-04 17:55:13 +02:00
parent e7e3b2c6b1
commit bf4a0fcb38

View File

@@ -72,6 +72,7 @@ class ClientsController extends AbstractController
$em->flush();
$this->indexInMeilisearch($meilisearch, $customer, $logger);
$this->ensureDefaultContact($customer, $em);
$this->addFlash('success', 'Client '.$customer->getFullName().' cree.');
@@ -117,6 +118,32 @@ class ClientsController extends AbstractController
return $info;
}
private function ensureDefaultContact(Customer $customer, EntityManagerInterface $em): void
{
$contactRepo = $em->getRepository(\App\Entity\CustomerContact::class);
$existing = $contactRepo->findBy(['customer' => $customer]);
if ([] !== $existing) {
return;
}
$firstName = $customer->getFirstName();
$lastName = $customer->getLastName();
if (null === $firstName || null === $lastName || '' === $firstName || '' === $lastName) {
return;
}
$contact = new \App\Entity\CustomerContact($customer, $firstName, $lastName);
$contact->setEmail($customer->getEmail());
$contact->setPhone($customer->getPhone());
$contact->setRole('Directeur');
$contact->setIsBillingEmail(true);
$em->persist($contact);
$em->flush();
}
private function populateCustomerData(Request $request, Customer $customer): void
{
$customer->setFirstName(trim($request->request->getString('firstName')));
@@ -279,6 +306,7 @@ class ClientsController extends AbstractController
return $this->handleContactForm($request, $customer, $em);
}
$this->ensureDefaultContact($customer, $em);
$contacts = $em->getRepository(\App\Entity\CustomerContact::class)->findBy(['customer' => $customer], ['createdAt' => 'DESC']);
$domains = $em->getRepository(\App\Entity\Domain::class)->findBy(['customer' => $customer]);