diff --git a/src/Controller/Admin/AdvertController.php b/src/Controller/Admin/AdvertController.php index 991da5c..a7f1bba 100644 --- a/src/Controller/Admin/AdvertController.php +++ b/src/Controller/Admin/AdvertController.php @@ -268,6 +268,74 @@ class AdvertController extends AbstractController ]); } + /** + * Enregistre un paiement manuel (virement, cheque, especes, etc.). + */ + #[Route('/{id}/manual-payment', name: 'manual_payment', requirements: ['id' => '\d+'], methods: ['POST'])] + public function manualPayment( + int $id, + \Symfony\Component\HttpFoundation\Request $request, + FactureService $factureService, + ): Response { + $advert = $this->em->getRepository(Advert::class)->find($id); + if (null === $advert) { + throw $this->createNotFoundException(self::MSG_NOT_FOUND); + } + + $amount = $request->request->getString('amount'); + $method = $request->request->getString('method'); + $reference = trim($request->request->getString('reference')); + + if ('' === $amount || '' === $method) { + $this->addFlash('error', 'Montant et methode de paiement requis.'); + + return $this->redirectToRoute('app_admin_clients_show', [ + 'id' => $advert->getCustomer()?->getId() ?? 0, + 'tab' => 'avis', + ]); + } + + $methodLabel = match ($method) { + 'virement' => 'Virement bancaire', + 'cheque' => 'Cheque', + 'especes' => 'Especes', + 'cb_externe' => 'CB (terminal externe)', + default => 'Autre', + }; + + if ('' !== $reference) { + $methodLabel .= ' (Ref: '.$reference.')'; + } + + // Creer l'AdvertPayment + $payment = new \App\Entity\AdvertPayment($advert, \App\Entity\AdvertPayment::TYPE_SUCCESS, number_format((float) $amount, 2, '.', '')); + $payment->setMethod($method); + $this->em->persist($payment); + + // Mettre a jour l'etat de l'avis + $advert->setState(Advert::STATE_ACCEPTED); + + // Tracker l'evenement + $this->em->persist(new \App\Entity\AdvertEvent($advert, \App\Entity\AdvertEvent::TYPE_PAY, 'Paiement manuel : '.$methodLabel.' - '.$amount.' EUR')); + $this->em->flush(); + + // Generer la facture + try { + $factureService->createPaidFactureFromAdvert($advert, number_format((float) $amount, 2, '.', ''), $methodLabel); + } catch (\Throwable $e) { + $this->addFlash('warning', 'Paiement enregistre mais erreur generation facture : '.$e->getMessage()); + } + + $this->meilisearch->indexAdvert($advert); + + $this->addFlash('success', 'Paiement de '.$amount.' EUR enregistre ('.$methodLabel.') pour l\'avis '.$advert->getOrderNumber()->getNumOrder().'.'); + + return $this->redirectToRoute('app_admin_clients_show', [ + 'id' => $advert->getCustomer()?->getId() ?? 0, + 'tab' => 'avis', + ]); + } + #[Route('/{id}/sync-payment', name: 'sync_payment', requirements: ['id' => '\d+'], methods: ['POST'])] public function syncPayment( int $id, diff --git a/templates/admin/clients/show.html.twig b/templates/admin/clients/show.html.twig index 1d30483..81f45e2 100644 --- a/templates/admin/clients/show.html.twig +++ b/templates/admin/clients/show.html.twig @@ -641,6 +641,10 @@
+ {% endif %} {% if a.state == 'accepted' and a.factures|length == 0 %}
@@ -682,6 +686,44 @@
Aucun avis de paiement.
{% endif %} + {# Modals paiement manuel #} + {% for a in advertsList %} + {% if a.state == 'send' %} +
+ {% endif %} + {% endfor %} + {# Tab: Devis #} {% elseif tab == 'devis' %}