<?php
namespace App\Controller;
use App\Entity\Service;
use App\Entity\Commande;
use App\Entity\Paiement;
use App\Entity\Foodtruck;
use App\Form\PaiementType;
use App\Entity\LignePaiement;
use App\Form\PaiementEditType;
use App\Service\CommandeService;
use App\Service\FoodtruckService;
use App\Repository\ServiceRepository;
use App\Repository\CommandeRepository;
use App\Repository\PaiementRepository;
use Doctrine\ORM\EntityManagerInterface;
use App\Repository\ModePaiementRepository;
use App\Repository\LigneCommandeRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class PaiementController extends AbstractController
{
public function __construct(
private EntityManagerInterface $entityManager,
private CommandeService $commandeService,
private PaiementRepository $paiementRepository,
private ServiceRepository $serviceRepository,
private FoodtruckService $foodtruckService,
private ModePaiementRepository $modePaiementRepository,
)
{}
#[Route('/paiement', name: 'app_paiement')]
public function index(Request $request, SessionInterface $session): Response
{
//Détection du foodtruck
$foodtruck = $this->foodtruckService->controleFoodtruck($request);
if(is_object($foodtruck))
{
} else {
$foodtruck = $session->get("foodtruck", Foodtruck::class);
}
$firstDay = date('Y-m-d',strtotime('2024-01-29'));
$lastDay = date('Y-m-d',strtotime('+6 day'));
$semaine = [];
$dates = [];
$dates['firstDay'] = $firstDay;
$dates['lastDay'] = $lastDay;
// $paiements = $this->paiementRepository->findAll();
// $services = $this->serviceRepository->findBy([],['date' => 'DESC']);
//Récupération des services liés au foodtruck récupéré depuis l'URL
$services = $this->serviceRepository->findByDatesByFoodtruck($dates,$foodtruck);
$semaine = ['Lundi'=>NULL, 'Mardi'=>NULL, 'Mercredi'=>NULL, 'Jeudi'=>NULL, 'Vendredi'=>NULL, 'Samedi'=>NULL, 'Dimanche'=>NULL];
foreach ($services as $key => $service) {
if(date('N' , $service->getDate()->getTimestamp() ) == 1 )
{
$semaine['Lundi'][] = $service;
}
if(date('N' , $service->getDate()->getTimestamp() ) == 2 )
{
$semaine['Mardi'][] = $service;
}
if(date('N' , $service->getDate()->getTimestamp() ) == 3 )
{
$semaine['Mercredi'][] = $service;
}
if(date('N' , $service->getDate()->getTimestamp() ) == 4 )
{
$semaine['Jeudi'][] = $service;
}
if(date('N' , $service->getDate()->getTimestamp() ) == 5 )
{
$semaine['Vendredi'][] = $service;
}
if(date('N' , $service->getDate()->getTimestamp() ) == 6 )
{
$semaine['Samedi'][] = $service;
}
if(date('N' , $service->getDate()->getTimestamp() ) == 7 )
{
$semaine['Dimanche'][] = $service;
}
}
// dd($semaine);
return $this->render('paiement/index.html.twig', [
// 'paiements' => $paiements,
'services' => $services,
'semaine' => $semaine,
]);
}
#[Route('/paiement/service/{id}', name: 'app_paiement_service')]
public function paiementsService(Service $service)
{
try {
$this->denyAccessUnlessGranted('FOODTRUCK_VIEW', $service->getFoodtruck());
} catch (\Throwable $th) {
$this->addFlash('error', 'Vous n\'êtes pas autorisé à visualiser les paiements de ce service !');
return $this->redirectToRoute('services_semaine', [], Response::HTTP_SEE_OTHER);
}
$modePaiements = $this->modePaiementRepository->findAll();
$modesPaiement = [];
$totalService = 0;
$totalPaiements = 0;
$paiements = [];
foreach ($service->getCommandes() as $key => $commande) {
foreach ($commande->getPaiements() as $key => $paiement) {
$paiements[] = $paiement;
}
foreach ($commande->getLignesCommande() as $key => $ligneCommande) {
$totalService += $ligneCommande->getPrix() * $ligneCommande->getQuantite();
}
}
foreach ($modePaiements as $key => $modePaiement) {
$modesPaiement[$modePaiement->getId()]['id'] = $modePaiement->getId();
$modesPaiement[$modePaiement->getId()]['libelle'] = $modePaiement->getLibelle();
$modesPaiement[$modePaiement->getId()]['nb'] = 0;
$modesPaiement[$modePaiement->getId()]['montant'] = 0;
foreach ($paiements as $key => $paiement) {
if($paiement->getModePaiement()->getId() == $modePaiement->getId()){
$modesPaiement[$modePaiement->getId()]['nb']++;
$modesPaiement[$modePaiement->getId()]['montant'] += $paiement->getMontant();
$totalPaiements += $paiement->getMontant();
}
}
}
// dd($modesPaiement);
return $this->render('paiement/paiementsService.html.twig', [
'service' => $service,
'modesPaiement' => $modesPaiement,
'totalService' => $totalService,
'totalPaiements' => $totalPaiements,
]);
}
#[Route('/paiement/new/{id}/{place}', name:'app_paiement_new')]
public function new(Commande $commande, string $place = null, Request $request)
{
$paiement = new Paiement();
// dd($paiement);
$paiement->setCommande($commande);
$paiement->setCreatedAt(new \DateTimeImmutable("now",new \DateTimeZone('Europe/Paris')));
$form = $this->createForm(PaiementType::class,$paiement);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$this->entityManager->persist($paiement);
$this->entityManager->flush();
// return $this->redirectToRoute('commande_index',[
// 'id' => $paiement->getCommande()->getService()->getId(),
// 'place' => $place,
// ]);
}
$lignesCommande = [];
$montantRestantAPayer = 0;
foreach ($commande->getLignesCommande() as $key => $ligneCommande) {
if(count($ligneCommande->getLignesPaiement()) === 0){
$lignesCommande[] = [
'id' => $ligneCommande->getId(),
'preparation' => $ligneCommande->getPreparation()->getLibelle(),
'quantite' => $ligneCommande->getQuantite(),
'prix' => $ligneCommande->getPrix(),
];
} else {
$quantitePayee = 0;
foreach ($ligneCommande->getLignesPaiement() as $lignePaiement) {
$quantitePayee += $lignePaiement->getQuantite();
}
if($quantitePayee < $ligneCommande->getQuantite()){
$lignesCommande[] = [
'id' => $ligneCommande->getId(),
'preparation' => $ligneCommande->getPreparation()->getLibelle(),
'quantite' => $ligneCommande->getQuantite() - $quantitePayee,
'prix' => $ligneCommande->getPrix(),
];
}
}
$montantRestantAPayer += $ligneCommande->getPrix() * $ligneCommande->getQuantite();
}
foreach ($commande->getPaiements() as $paiement) {
$montantRestantAPayer -= $paiement->getMontant();
}
// dd($lignesCommande);
return $this->render('paiement/new.html.twig', [
'form' => $form->createView(),
'paiement' => $paiement,
'place' => $place,
'lignesCommande' => $lignesCommande,
'commande' => $commande,
'montantRestantAPayer' => $montantRestantAPayer,
]);
}
#[Route('/_api/paiement/add', name:'api_post_paiement_add')]
public function apiPostPaiementAdd(Request $request, CommandeRepository $commandeRepository, ModePaiementRepository $modePaiementRepository, LigneCommandeRepository $ligneCommandeRepository)
{
$commandeId = $request->query->get('commande');
$montant = $request->query->get('montant');
$modePaiementId = $request->query->get('modePaiement');
$modePaiement = $modePaiementRepository->find($modePaiementId);
$lignesPaiement = $request->query->get('lignesPaiement');
$nbLignesPaiement = $request->query->get('nbLignesPaiement');
$paiement = new Paiement();
// dump($paiement);
$commande = $commandeRepository->find($commandeId);
$paiement->setCommande($commande);
$paiement->setMontant($montant);
$paiement->setModePaiement($modePaiement);
$paiement->setCreatedAt(new \DateTimeImmutable("now",new \DateTimeZone('Europe/Paris')));
for($i = 0; $i < $nbLignesPaiement; $i++){
$lignePaiement = new LignePaiement();
$lignePaiement->setQuantite($lignesPaiement[$i]['quantite']);
$lignePaiement->setPaiement($paiement);
$ligneCommande = $ligneCommandeRepository->find($lignesPaiement[$i]['id']);
$lignePaiement->setLigneCommande($ligneCommande);
$this->entityManager->persist($lignePaiement);
// dd($lignePaiement);
}
$this->entityManager->persist($paiement);
$this->entityManager->flush();
// dd($paiement);
return $this->json($paiement->getId(),200,[],['groups' => 'paiement']);
}
#[Route('/paiement/cloture/{id}/{place}', name:'app_paiement_cloture')]
public function cloture(Commande $commande, string $place = null)
{
//On vérifie si l'utilisateur connecté a le droit d'encaisser la commande
try {
$this->denyAccessUnlessGranted('FOODTRUCK_EDIT', $commande->getService()->getFoodtruck());
} catch (\Throwable $th) {
$this->addFlash('error', 'Vous n\'êtes pas autorisé à encaisser cette commande !');
return $this->redirectToRoute('services_semaine', [], Response::HTTP_SEE_OTHER);
}
$this->commandeService->workflowCommande($commande,'payer');
return $this->redirectToRoute('commande_index', ['id' => $commande->getService()->getId(),'place' => $place], Response::HTTP_SEE_OTHER);
}
#[Route('/paiement/{id}/delete/{place}', name: 'app_paiement_delete', methods: ['POST'])]
public function delete(Request $request, Paiement $paiement, string $place): Response
{
$commande = $paiement->getCommande();
if ($this->isCsrfTokenValid('delete'.$paiement->getId(), $request->request->get('_token'))) {
$this->entityManager->remove($paiement);
$this->entityManager->flush();
}
return $this->redirectToRoute('app_paiement_new', ['id' => $commande->getId(),'place' => $place], Response::HTTP_SEE_OTHER);
}
/**
* Fonction d'impression d'un paiement
*/
#[Route('/paiement/{id}/print', name: 'app_paiement_print', methods: ['GET'])]
public function print(Paiement $paiement): Response
{
return $this->render('paiement/print.html.twig',[
'paiement' => $paiement,
]);
}
#[Route('/paiement/{id}/edit', name: 'app_paiement_edit', methods: ['GET','POST'])]
public function edit(Request $request, Paiement $paiement): Response
{
$form = $this->createForm(PaiementEditType::class, $paiement);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->entityManager->flush();
//TODO: Gérer la redirection vers la page appelante
return $this->redirectToRoute('app_transactions_service', ['id' => $paiement->getCommande()->getService()->getId()], Response::HTTP_SEE_OTHER);
}
return $this->render('paiement/edit.html.twig', [
'paiement' => $paiement,
'form' => $form->createView(),
]);
}
}