src/Controller/PaiementController.php line 157

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Service;
  4. use App\Entity\Commande;
  5. use App\Entity\Paiement;
  6. use App\Entity\Foodtruck;
  7. use App\Form\PaiementType;
  8. use App\Entity\LignePaiement;
  9. use App\Form\PaiementEditType;
  10. use App\Service\CommandeService;
  11. use App\Service\FoodtruckService;
  12. use App\Repository\ServiceRepository;
  13. use App\Repository\CommandeRepository;
  14. use App\Repository\PaiementRepository;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use App\Repository\ModePaiementRepository;
  17. use App\Repository\LigneCommandeRepository;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Component\Validator\Constraints\Length;
  22. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  23. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  24. class PaiementController extends AbstractController
  25. {
  26. public function __construct(
  27. private EntityManagerInterface $entityManager,
  28. private CommandeService $commandeService,
  29. private PaiementRepository $paiementRepository,
  30. private ServiceRepository $serviceRepository,
  31. private FoodtruckService $foodtruckService,
  32. private ModePaiementRepository $modePaiementRepository,
  33. )
  34. {}
  35. #[Route('/paiement', name: 'app_paiement')]
  36. public function index(Request $request, SessionInterface $session): Response
  37. {
  38. //Détection du foodtruck
  39. $foodtruck = $this->foodtruckService->controleFoodtruck($request);
  40. if(is_object($foodtruck))
  41. {
  42. } else {
  43. $foodtruck = $session->get("foodtruck", Foodtruck::class);
  44. }
  45. $firstDay = date('Y-m-d',strtotime('2024-01-29'));
  46. $lastDay = date('Y-m-d',strtotime('+6 day'));
  47. $semaine = [];
  48. $dates = [];
  49. $dates['firstDay'] = $firstDay;
  50. $dates['lastDay'] = $lastDay;
  51. // $paiements = $this->paiementRepository->findAll();
  52. // $services = $this->serviceRepository->findBy([],['date' => 'DESC']);
  53. //Récupération des services liés au foodtruck récupéré depuis l'URL
  54. $services = $this->serviceRepository->findByDatesByFoodtruck($dates,$foodtruck);
  55. $semaine = ['Lundi'=>NULL, 'Mardi'=>NULL, 'Mercredi'=>NULL, 'Jeudi'=>NULL, 'Vendredi'=>NULL, 'Samedi'=>NULL, 'Dimanche'=>NULL];
  56. foreach ($services as $key => $service) {
  57. if(date('N' , $service->getDate()->getTimestamp() ) == 1 )
  58. {
  59. $semaine['Lundi'][] = $service;
  60. }
  61. if(date('N' , $service->getDate()->getTimestamp() ) == 2 )
  62. {
  63. $semaine['Mardi'][] = $service;
  64. }
  65. if(date('N' , $service->getDate()->getTimestamp() ) == 3 )
  66. {
  67. $semaine['Mercredi'][] = $service;
  68. }
  69. if(date('N' , $service->getDate()->getTimestamp() ) == 4 )
  70. {
  71. $semaine['Jeudi'][] = $service;
  72. }
  73. if(date('N' , $service->getDate()->getTimestamp() ) == 5 )
  74. {
  75. $semaine['Vendredi'][] = $service;
  76. }
  77. if(date('N' , $service->getDate()->getTimestamp() ) == 6 )
  78. {
  79. $semaine['Samedi'][] = $service;
  80. }
  81. if(date('N' , $service->getDate()->getTimestamp() ) == 7 )
  82. {
  83. $semaine['Dimanche'][] = $service;
  84. }
  85. }
  86. // dd($semaine);
  87. return $this->render('paiement/index.html.twig', [
  88. // 'paiements' => $paiements,
  89. 'services' => $services,
  90. 'semaine' => $semaine,
  91. ]);
  92. }
  93. #[Route('/paiement/service/{id}', name: 'app_paiement_service')]
  94. public function paiementsService(Service $service)
  95. {
  96. try {
  97. $this->denyAccessUnlessGranted('FOODTRUCK_VIEW', $service->getFoodtruck());
  98. } catch (\Throwable $th) {
  99. $this->addFlash('error', 'Vous n\'êtes pas autorisé à visualiser les paiements de ce service !');
  100. return $this->redirectToRoute('services_semaine', [], Response::HTTP_SEE_OTHER);
  101. }
  102. $modePaiements = $this->modePaiementRepository->findAll();
  103. $modesPaiement = [];
  104. $totalService = 0;
  105. $totalPaiements = 0;
  106. $paiements = [];
  107. foreach ($service->getCommandes() as $key => $commande) {
  108. foreach ($commande->getPaiements() as $key => $paiement) {
  109. $paiements[] = $paiement;
  110. }
  111. foreach ($commande->getLignesCommande() as $key => $ligneCommande) {
  112. $totalService += $ligneCommande->getPrix() * $ligneCommande->getQuantite();
  113. }
  114. }
  115. foreach ($modePaiements as $key => $modePaiement) {
  116. $modesPaiement[$modePaiement->getId()]['id'] = $modePaiement->getId();
  117. $modesPaiement[$modePaiement->getId()]['libelle'] = $modePaiement->getLibelle();
  118. $modesPaiement[$modePaiement->getId()]['nb'] = 0;
  119. $modesPaiement[$modePaiement->getId()]['montant'] = 0;
  120. foreach ($paiements as $key => $paiement) {
  121. if($paiement->getModePaiement()->getId() == $modePaiement->getId()){
  122. $modesPaiement[$modePaiement->getId()]['nb']++;
  123. $modesPaiement[$modePaiement->getId()]['montant'] += $paiement->getMontant();
  124. $totalPaiements += $paiement->getMontant();
  125. }
  126. }
  127. }
  128. // dd($modesPaiement);
  129. return $this->render('paiement/paiementsService.html.twig', [
  130. 'service' => $service,
  131. 'modesPaiement' => $modesPaiement,
  132. 'totalService' => $totalService,
  133. 'totalPaiements' => $totalPaiements,
  134. ]);
  135. }
  136. #[Route('/paiement/new/{id}/{place}', name:'app_paiement_new')]
  137. public function new(Commande $commande, string $place = null, Request $request)
  138. {
  139. $paiement = new Paiement();
  140. // dd($paiement);
  141. $paiement->setCommande($commande);
  142. $paiement->setCreatedAt(new \DateTimeImmutable("now",new \DateTimeZone('Europe/Paris')));
  143. $form = $this->createForm(PaiementType::class,$paiement);
  144. $form->handleRequest($request);
  145. if($form->isSubmitted() && $form->isValid()){
  146. $this->entityManager->persist($paiement);
  147. $this->entityManager->flush();
  148. // return $this->redirectToRoute('commande_index',[
  149. // 'id' => $paiement->getCommande()->getService()->getId(),
  150. // 'place' => $place,
  151. // ]);
  152. }
  153. $lignesCommande = [];
  154. $montantRestantAPayer = 0;
  155. foreach ($commande->getLignesCommande() as $key => $ligneCommande) {
  156. if(count($ligneCommande->getLignesPaiement()) === 0){
  157. $lignesCommande[] = [
  158. 'id' => $ligneCommande->getId(),
  159. 'preparation' => $ligneCommande->getPreparation()->getLibelle(),
  160. 'quantite' => $ligneCommande->getQuantite(),
  161. 'prix' => $ligneCommande->getPrix(),
  162. ];
  163. } else {
  164. $quantitePayee = 0;
  165. foreach ($ligneCommande->getLignesPaiement() as $lignePaiement) {
  166. $quantitePayee += $lignePaiement->getQuantite();
  167. }
  168. if($quantitePayee < $ligneCommande->getQuantite()){
  169. $lignesCommande[] = [
  170. 'id' => $ligneCommande->getId(),
  171. 'preparation' => $ligneCommande->getPreparation()->getLibelle(),
  172. 'quantite' => $ligneCommande->getQuantite() - $quantitePayee,
  173. 'prix' => $ligneCommande->getPrix(),
  174. ];
  175. }
  176. }
  177. $montantRestantAPayer += $ligneCommande->getPrix() * $ligneCommande->getQuantite();
  178. }
  179. foreach ($commande->getPaiements() as $paiement) {
  180. $montantRestantAPayer -= $paiement->getMontant();
  181. }
  182. // dd($lignesCommande);
  183. return $this->render('paiement/new.html.twig', [
  184. 'form' => $form->createView(),
  185. 'paiement' => $paiement,
  186. 'place' => $place,
  187. 'lignesCommande' => $lignesCommande,
  188. 'commande' => $commande,
  189. 'montantRestantAPayer' => $montantRestantAPayer,
  190. ]);
  191. }
  192. #[Route('/_api/paiement/add', name:'api_post_paiement_add')]
  193. public function apiPostPaiementAdd(Request $request, CommandeRepository $commandeRepository, ModePaiementRepository $modePaiementRepository, LigneCommandeRepository $ligneCommandeRepository)
  194. {
  195. $commandeId = $request->query->get('commande');
  196. $montant = $request->query->get('montant');
  197. $modePaiementId = $request->query->get('modePaiement');
  198. $modePaiement = $modePaiementRepository->find($modePaiementId);
  199. $lignesPaiement = $request->query->get('lignesPaiement');
  200. $nbLignesPaiement = $request->query->get('nbLignesPaiement');
  201. $paiement = new Paiement();
  202. // dump($paiement);
  203. $commande = $commandeRepository->find($commandeId);
  204. $paiement->setCommande($commande);
  205. $paiement->setMontant($montant);
  206. $paiement->setModePaiement($modePaiement);
  207. $paiement->setCreatedAt(new \DateTimeImmutable("now",new \DateTimeZone('Europe/Paris')));
  208. for($i = 0; $i < $nbLignesPaiement; $i++){
  209. $lignePaiement = new LignePaiement();
  210. $lignePaiement->setQuantite($lignesPaiement[$i]['quantite']);
  211. $lignePaiement->setPaiement($paiement);
  212. $ligneCommande = $ligneCommandeRepository->find($lignesPaiement[$i]['id']);
  213. $lignePaiement->setLigneCommande($ligneCommande);
  214. $this->entityManager->persist($lignePaiement);
  215. // dd($lignePaiement);
  216. }
  217. $this->entityManager->persist($paiement);
  218. $this->entityManager->flush();
  219. // dd($paiement);
  220. return $this->json($paiement->getId(),200,[],['groups' => 'paiement']);
  221. }
  222. #[Route('/paiement/cloture/{id}/{place}', name:'app_paiement_cloture')]
  223. public function cloture(Commande $commande, string $place = null)
  224. {
  225. //On vérifie si l'utilisateur connecté a le droit d'encaisser la commande
  226. try {
  227. $this->denyAccessUnlessGranted('FOODTRUCK_EDIT', $commande->getService()->getFoodtruck());
  228. } catch (\Throwable $th) {
  229. $this->addFlash('error', 'Vous n\'êtes pas autorisé à encaisser cette commande !');
  230. return $this->redirectToRoute('services_semaine', [], Response::HTTP_SEE_OTHER);
  231. }
  232. $this->commandeService->workflowCommande($commande,'payer');
  233. return $this->redirectToRoute('commande_index', ['id' => $commande->getService()->getId(),'place' => $place], Response::HTTP_SEE_OTHER);
  234. }
  235. #[Route('/paiement/{id}/delete/{place}', name: 'app_paiement_delete', methods: ['POST'])]
  236. public function delete(Request $request, Paiement $paiement, string $place): Response
  237. {
  238. $commande = $paiement->getCommande();
  239. if ($this->isCsrfTokenValid('delete'.$paiement->getId(), $request->request->get('_token'))) {
  240. $this->entityManager->remove($paiement);
  241. $this->entityManager->flush();
  242. }
  243. return $this->redirectToRoute('app_paiement_new', ['id' => $commande->getId(),'place' => $place], Response::HTTP_SEE_OTHER);
  244. }
  245. /**
  246. * Fonction d'impression d'un paiement
  247. */
  248. #[Route('/paiement/{id}/print', name: 'app_paiement_print', methods: ['GET'])]
  249. public function print(Paiement $paiement): Response
  250. {
  251. return $this->render('paiement/print.html.twig',[
  252. 'paiement' => $paiement,
  253. ]);
  254. }
  255. #[Route('/paiement/{id}/edit', name: 'app_paiement_edit', methods: ['GET','POST'])]
  256. public function edit(Request $request, Paiement $paiement): Response
  257. {
  258. $form = $this->createForm(PaiementEditType::class, $paiement);
  259. $form->handleRequest($request);
  260. if ($form->isSubmitted() && $form->isValid()) {
  261. $this->entityManager->flush();
  262. //TODO: Gérer la redirection vers la page appelante
  263. return $this->redirectToRoute('app_transactions_service', ['id' => $paiement->getCommande()->getService()->getId()], Response::HTTP_SEE_OTHER);
  264. }
  265. return $this->render('paiement/edit.html.twig', [
  266. 'paiement' => $paiement,
  267. 'form' => $form->createView(),
  268. ]);
  269. }
  270. }