2026-01-16 14:32:20 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controller\Dashboard;
|
|
|
|
|
|
2026-01-19 18:22:53 +01:00
|
|
|
use App\Entity\CustomerAddress;
|
2026-01-16 16:00:00 +01:00
|
|
|
use App\Entity\Devis;
|
2026-01-17 22:29:04 +01:00
|
|
|
use App\Entity\DevisLine;
|
2026-01-22 10:36:26 +01:00
|
|
|
use App\Entity\DevisOptions;
|
2026-01-19 19:40:27 +01:00
|
|
|
use App\Event\Signature\DevisSend;
|
2026-01-16 16:00:00 +01:00
|
|
|
use App\Form\NewDevisType;
|
2026-01-16 14:32:20 +01:00
|
|
|
use App\Logger\AppLogger;
|
|
|
|
|
use App\Repository\AccountRepository;
|
2026-01-19 18:22:53 +01:00
|
|
|
use App\Repository\CustomerAddressRepository;
|
2026-01-17 22:29:04 +01:00
|
|
|
use App\Repository\CustomerRepository;
|
2026-01-16 15:04:50 +01:00
|
|
|
use App\Repository\DevisRepository;
|
2026-01-22 10:36:26 +01:00
|
|
|
use App\Repository\OptionsRepository;
|
2026-01-19 17:56:57 +01:00
|
|
|
use App\Repository\ProductRepository;
|
2026-01-17 22:29:04 +01:00
|
|
|
use App\Service\Pdf\DevisPdfService;
|
2026-01-19 18:22:53 +01:00
|
|
|
use App\Service\Signature\Client;
|
2026-01-22 10:36:26 +01:00
|
|
|
use DateTimeImmutable;
|
2026-01-17 22:29:04 +01:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2026-01-16 15:04:50 +01:00
|
|
|
use Knp\Bundle\PaginatorBundle\KnpPaginatorBundle;
|
|
|
|
|
use Knp\Component\Pager\PaginatorInterface;
|
2026-01-16 14:32:20 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2026-01-19 19:40:27 +01:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2026-01-19 18:22:53 +01:00
|
|
|
use Symfony\Component\HttpFoundation\File\File;
|
|
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
2026-01-16 15:04:50 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2026-01-16 14:32:20 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2026-01-17 22:29:04 +01:00
|
|
|
use Symfony\Component\HttpKernel\KernelInterface;
|
2026-01-16 14:32:20 +01:00
|
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
|
|
|
|
|
|
|
|
class DevisController extends AbstractController
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Liste des administrateurs
|
|
|
|
|
*/
|
2026-01-19 19:40:27 +01:00
|
|
|
#[Route(path: '/crm/devis', name: 'app_crm_devis', options: ['sitemap' => false], methods: ['GET', 'POST'])]
|
|
|
|
|
public function devis(
|
|
|
|
|
EventDispatcherInterface $eventDispatcher,
|
|
|
|
|
EntityManagerInterface $entityManager,
|
|
|
|
|
DevisRepository $devisRepository,
|
|
|
|
|
AppLogger $appLogger,
|
|
|
|
|
PaginatorInterface $paginator,
|
|
|
|
|
Request $request,
|
2026-01-22 10:36:26 +01:00
|
|
|
KernelInterface $kernel,
|
2026-01-19 19:40:27 +01:00
|
|
|
|
|
|
|
|
): Response {
|
|
|
|
|
|
|
|
|
|
// Gestion du renvoi de la signature
|
|
|
|
|
if ($request->query->has('resend')) {
|
|
|
|
|
$quoteId = $request->query->get('resend');
|
|
|
|
|
$quote = $devisRepository->find($quoteId);
|
|
|
|
|
|
|
|
|
|
if ($quote instanceof Devis) {
|
|
|
|
|
// Déclenchement de l'événement de renvoi
|
|
|
|
|
$event = new DevisSend($quote);
|
|
|
|
|
$eventDispatcher->dispatch($event);
|
|
|
|
|
|
|
|
|
|
// Journalisation et notification
|
|
|
|
|
$appLogger->record('RESEND', 'Relance signature pour le devis ' . $quote->getNum());
|
|
|
|
|
$this->addFlash("success", "Le lien de signature pour le devis " . $quote->getNum() . " a été renvoyé au client.");
|
|
|
|
|
|
|
|
|
|
return $this->redirectToRoute('app_crm_devis');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->addFlash("error", "Devis introuvable.");
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 14:32:20 +01:00
|
|
|
$appLogger->record('VIEW', 'Consultation de la liste des devis');
|
2026-01-19 19:40:27 +01:00
|
|
|
|
|
|
|
|
// Pagination (Tri décroissant sur la date de création pour voir les plus récents en premier)
|
|
|
|
|
$pagination = $paginator->paginate(
|
|
|
|
|
$devisRepository->findBy([], ['createA' => 'DESC']),
|
|
|
|
|
$request->query->getInt('page', 1),
|
|
|
|
|
20
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $this->render('dashboard/devis/list.twig', [
|
|
|
|
|
'quotes' => $pagination,
|
2026-01-16 14:32:20 +01:00
|
|
|
]);
|
|
|
|
|
}
|
2026-01-17 22:29:04 +01:00
|
|
|
#[Route(path: '/crm/devis/add', name: 'app_crm_devis_add', options: ['sitemap' => false], methods: ['GET','POST'])]
|
2026-01-22 10:36:26 +01:00
|
|
|
public function devisAdd(Client $client,OptionsRepository $optionsRepository,EventDispatcherInterface $eventDispatcher,KernelInterface $kernel,CustomerAddressRepository $customerAddress,ProductRepository $productRepository,EntityManagerInterface $entityManager,CustomerRepository $customerRepository,DevisRepository $devisRepository, AppLogger $appLogger,Request $request): Response
|
2026-01-16 15:04:50 +01:00
|
|
|
{
|
2026-01-16 16:00:00 +01:00
|
|
|
$devisNumber ="DEVIS-".sprintf('%05d',$devisRepository->count()+1);
|
|
|
|
|
$appLogger->record('VIEW', 'Consultation de la création d\'un devis');
|
2026-01-16 14:32:20 +01:00
|
|
|
|
2026-01-16 16:00:00 +01:00
|
|
|
$devis = new Devis();
|
|
|
|
|
$devis->setNum($devisNumber);
|
|
|
|
|
$devis->setState("draft");
|
|
|
|
|
$devis->setCreateA(new \DateTimeImmutable());
|
|
|
|
|
$devis->setUpdateAt(new \DateTimeImmutable());
|
|
|
|
|
|
|
|
|
|
$form = $this->createForm(NewDevisType::class,$devis);
|
2026-01-17 22:29:04 +01:00
|
|
|
if($request->isMethod('POST')){
|
2026-01-22 10:36:26 +01:00
|
|
|
|
|
|
|
|
$devis->setStartAt( new DateTimeImmutable($_POST['new_devis']['startAt']));
|
|
|
|
|
$devis->setEndAt( new DateTimeImmutable($_POST['new_devis']['endAt']));
|
2026-01-19 18:22:53 +01:00
|
|
|
$devis->setBillAddress($customerAddress->find($_POST['devis']['bill_address']));
|
|
|
|
|
$devis->setAddressShip($customerAddress->find($_POST['devis']['ship_address']));
|
2026-01-22 10:36:26 +01:00
|
|
|
$devis->setCustomer($customerRepository->find($_POST['new_devis']['customer']));
|
2026-01-17 22:29:04 +01:00
|
|
|
foreach ($_POST['lines'] as $cd=>$line) {
|
|
|
|
|
$rLine = new DevisLine();
|
|
|
|
|
$rLine->setDevi($devis);
|
|
|
|
|
$rLine->setPos($cd);
|
2026-01-19 17:56:57 +01:00
|
|
|
$rLine->setProduct($productRepository->find($line['product_id']));
|
|
|
|
|
$rLine->setDay($line['days']);
|
|
|
|
|
$rLine->setPriceHt(floatval($line['price_ht']));
|
|
|
|
|
$rLine->setPriceHtSup(floatval($line['price_sup_ht']));
|
2026-01-17 22:29:04 +01:00
|
|
|
$entityManager->persist($rLine);
|
|
|
|
|
}
|
2026-01-22 10:36:26 +01:00
|
|
|
foreach ($_POST['options'] as $line) {
|
|
|
|
|
$rLineOptions = new DevisOptions();
|
|
|
|
|
$rLineOptions->setDevis($devis);
|
|
|
|
|
$rLineOptions->setOption($optionsRepository->find($line['product_id']));
|
|
|
|
|
$rLineOptions->setPriceHt(floatval($line['price_ht']));
|
|
|
|
|
$entityManager->persist($rLineOptions);
|
|
|
|
|
}
|
2026-01-17 22:29:04 +01:00
|
|
|
$entityManager->persist($devis);
|
2026-01-22 10:36:26 +01:00
|
|
|
$entityManager->flush();
|
2026-01-19 18:22:53 +01:00
|
|
|
|
|
|
|
|
$docusealService = new DevisPdfService($kernel, $devis, true);
|
|
|
|
|
$contentDocuseal = $docusealService->generate();
|
|
|
|
|
|
2026-01-22 10:36:26 +01:00
|
|
|
|
2026-01-19 18:22:53 +01:00
|
|
|
$tmpPathDocuseal = sys_get_temp_dir() . '/docuseal_' . uniqid() . '.pdf';
|
|
|
|
|
file_put_contents($tmpPathDocuseal, $contentDocuseal);
|
|
|
|
|
|
|
|
|
|
$fileDocuseal = new UploadedFile($tmpPathDocuseal,'dc_'.$devis->getNum() . '.pdf','application/pdf',0,true);
|
|
|
|
|
$devis->setDevisDocuSealFile($fileDocuseal);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$devisService = new DevisPdfService($kernel, $devis, false);
|
|
|
|
|
$contentDevis = $devisService->generate();
|
|
|
|
|
|
|
|
|
|
$tmpPathDevis = sys_get_temp_dir() . '/devis_' . uniqid() . '.pdf';
|
|
|
|
|
file_put_contents($tmpPathDevis, $contentDevis);
|
|
|
|
|
|
|
|
|
|
$fileDevis = new UploadedFile($tmpPathDevis,'devis_'.$devis->getNum() . '.pdf','application/pdf',0,true);
|
|
|
|
|
$devis->setDevisFile($fileDevis);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$devis->setState("created_waitsign");
|
|
|
|
|
$devis->setUpdateAt(new \DateTimeImmutable());
|
|
|
|
|
$entityManager->flush();
|
|
|
|
|
$client->createSubmissionDevis($devis);
|
2026-01-19 19:40:27 +01:00
|
|
|
|
|
|
|
|
$event = new DevisSend($devis);
|
|
|
|
|
$eventDispatcher->dispatch($event);
|
2026-01-19 18:22:53 +01:00
|
|
|
return $this->redirectToRoute('app_crm_devis');
|
2026-01-17 22:29:04 +01:00
|
|
|
}
|
2026-01-16 16:00:00 +01:00
|
|
|
return $this->render('dashboard/devis/add.twig',[
|
|
|
|
|
'form' => $form->createView(),
|
|
|
|
|
]);
|
2026-01-16 15:04:50 +01:00
|
|
|
}
|
2026-01-16 14:32:20 +01:00
|
|
|
|
|
|
|
|
}
|