diff --git a/assets/modules/entreprise-search.js b/assets/modules/entreprise-search.js index 0fe1360..0e24b70 100644 --- a/assets/modules/entreprise-search.js +++ b/assets/modules/entreprise-search.js @@ -79,6 +79,8 @@ const renderResult = (e, onSelect) => { fillField('address', addr) fillField('zipCode', s.code_postal || '') fillField('city', s.libelle_commune || '') + fillField('geoLat', s.latitude || '') + fillField('geoLong', s.longitude || '') const typeCompany = resolveTypeCompany(e.nature_juridique) if (typeCompany) fillField('typeCompany', typeCompany) diff --git a/migrations/Version20260404092101.php b/migrations/Version20260404092101.php new file mode 100644 index 0000000..ed3b716 --- /dev/null +++ b/migrations/Version20260404092101.php @@ -0,0 +1,33 @@ +addSql('ALTER TABLE customer ADD geo_lat NUMERIC(10, 7) DEFAULT NULL'); + $this->addSql('ALTER TABLE customer ADD geo_long NUMERIC(10, 7) DEFAULT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE customer DROP geo_lat'); + $this->addSql('ALTER TABLE customer DROP geo_long'); + } +} diff --git a/src/Controller/Admin/ClientsController.php b/src/Controller/Admin/ClientsController.php index 9ed32a1..ad36345 100644 --- a/src/Controller/Admin/ClientsController.php +++ b/src/Controller/Admin/ClientsController.php @@ -40,6 +40,7 @@ class ClientsController extends AbstractController MeilisearchService $meilisearch, UserManagementService $userService, LoggerInterface $logger, + HttpClientInterface $httpClient, #[Autowire(env: 'STRIPE_SK')] string $stripeSecretKey, ): Response { if ('POST' !== $request->getMethod()) { @@ -55,6 +56,7 @@ class ClientsController extends AbstractController $customer = new Customer($user); $this->populateCustomerData($request, $customer); + $this->geocodeIfNeeded($customer, $httpClient); $this->initStripeCustomer($customer, $stripeSecretKey); @@ -64,7 +66,7 @@ class ClientsController extends AbstractController $this->finalizeStripeCustomer($customer, $user, $stripeSecretKey); $codeComptable = trim($request->request->getString('codeComptable')); - $customer->setCodeComptable('' !== $codeComptable ? $codeComptable : $customerRepository->generateUniqueCodeComptable($customer)); + $customer->setCodeComptable('' !== $codeComptable ? '411_'.$codeComptable : $customerRepository->generateUniqueCodeComptable($customer)); $em->flush(); $this->indexInMeilisearch($meilisearch, $customer, $logger); @@ -98,6 +100,32 @@ class ClientsController extends AbstractController $customer->setZipCode(trim($request->request->getString('zipCode')) ?: null); $customer->setCity(trim($request->request->getString('city')) ?: null); $customer->setTypeCompany(trim($request->request->getString('typeCompany')) ?: null); + $customer->setGeoLat(trim($request->request->getString('geoLat')) ?: null); + $customer->setGeoLong(trim($request->request->getString('geoLong')) ?: null); + } + + /** @codeCoverageIgnore */ + private function geocodeIfNeeded(Customer $customer, HttpClientInterface $httpClient): void + { + if (null !== $customer->getGeoLat() || null === $customer->getAddress()) { + return; + } + + $q = implode(' ', array_filter([$customer->getAddress(), $customer->getZipCode(), $customer->getCity()])); + + try { + $response = $httpClient->request('GET', 'https://data.geopf.fr/geocodage/search', [ + 'query' => ['q' => $q, 'limit' => 1], + ]); + $data = $response->toArray(); + $coords = $data['features'][0]['geometry']['coordinates'] ?? null; + + if (null !== $coords) { + $customer->setGeoLong((string) $coords[0]); + $customer->setGeoLat((string) $coords[1]); + } + } catch (\Throwable) { + } } /** @codeCoverageIgnore */ diff --git a/src/Entity/Customer.php b/src/Entity/Customer.php index 1336da1..e8f4241 100644 --- a/src/Entity/Customer.php +++ b/src/Entity/Customer.php @@ -74,6 +74,12 @@ class Customer #[ORM\Column(length: 255, nullable: true)] private ?string $city = null; + #[ORM\Column(type: 'decimal', precision: 10, scale: 7, nullable: true)] + private ?string $geoLat = null; + + #[ORM\Column(type: 'decimal', precision: 10, scale: 7, nullable: true)] + private ?string $geoLong = null; + #[ORM\Column(length: 14, nullable: true)] private ?string $siret = null; @@ -271,6 +277,30 @@ class Customer return $this; } + public function getGeoLat(): ?string + { + return $this->geoLat; + } + + public function setGeoLat(?string $geoLat): static + { + $this->geoLat = $geoLat; + + return $this; + } + + public function getGeoLong(): ?string + { + return $this->geoLong; + } + + public function setGeoLong(?string $geoLong): static + { + $this->geoLong = $geoLong; + + return $this; + } + public function getSiret(): ?string { return $this->siret; diff --git a/templates/admin/clients/create.html.twig b/templates/admin/clients/create.html.twig index 7bed114..5253849 100644 --- a/templates/admin/clients/create.html.twig +++ b/templates/admin/clients/create.html.twig @@ -75,8 +75,10 @@