Skip to content
Snippets Groups Projects
AnnonceDetailController.php 8.75 KiB
<?php

namespace App\Controller;

use App\Entity\Notification;
use App\Entity\Personne;
use App\Entity\Prestations;
use App\Entity\Pret;
use App\Entity\Services;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use DateTime;

class AnnonceDetailController extends AbstractController
{
    private $doctrine;

    public function __construct(ManagerRegistry $doctrine) {
        $this->doctrine = $doctrine;
    }

    private function getCommonData($id)
    {
        $entityManager = $this->doctrine->getManager();
        $annonce = $entityManager->getRepository(Prestations::class)->find($id);
        $nbAimees = $annonce->getUtilisateursAimant()->count();
        $prets = $entityManager->getRepository(Pret::class)->findAll();
        $services = $entityManager->getRepository(Services::class)->findAll();
        $user = $this->getUser();

        return [
            'entityManager' => $entityManager,
            'annonce' => $annonce,
            'nbAimees' => $nbAimees,
            'prets' => $prets,
            'services' => $services,
            'user' => $user,
        ];
    }

    /**
     * @Route("/annonce/{id}", name="annonce_detail")
     */
    public function detail($id)
    {
        $data = $this->getCommonData($id);
        $prest = $data['annonce'];
        if ($prest) {
            $prest->setNbVues($prest->getNbVues() + 1);
            $data['entityManager']->flush();
        }
        return $this->render('annonce_detail/annonce.html.twig', $data);
    }

    #[Route('/deleteTransaction/{prestationId}', name: 'app_delete_transaction')]
    public function deleteTransaction($prestationId): Response
    {
        $entityManager = $this->doctrine->getManager();
        $prestation  = $entityManager->getRepository(Prestations::class)->find($prestationId);

        if (!$prestation) {
            return $this->redirectToRoute('app_home_page');
        }

        if ($prestation->getIdClient() != null) {
            $this->makeNotif("L'utilisateur " . $this->getUser()->getLogin() . " a supprimé l'annonce " . $prestation->getTitre() . " que vous aviez réservé.",
                             "Suppression annonce réservée",
                             $entityManager->getRepository(Personne::class)->find($prestation->getIdClient()));
        }
        
        foreach ($prestation->getUtilisateursAimant() as $u) {
            $this->makeNotif("L'utilisateur " . $this->getUser()->getLogin() . " a supprimé l'annonce " . $prestation->getTitre() . " que vous aviez aimé.",
                             "Suppression annonce aimée",
                             $u);
        }

        $entityManager->remove($prestation);
        $entityManager->flush();

        return $this->redirectToRoute('app_home_page');
    }

    #[Route('/reserverPrestation/{id}', name: 'app_reserver_prestation')]
    public function reserverPrestation(Request $request,$id): Response
    {
        $data = $this->getCommonData($id);
        $interet = $request->get('interet');
        $clientID = $data['annonce']->getIdClient();

        if ($clientID != null) {
            if ($interet) {
                return $this->redirectToRoute('app_home_page');
            } else {
                $entityManager = $this->doctrine->getManager();

                $prestation = $data['annonce'];

                //On reverse l'argent dans le compte du client
                $data['user']->setNbFlorains($data['user']->getNbFlorains() + $prestation->getCoutPrestation());

                //On enlève les validations des deux partis
                $prestation->setFournisseurValide(false);
                $prestation->setClientValide(false);

                $this->makeNotif("Annulation de la réservation de l'annonce " . $prestation->getTitre() . " par " . $this->getUser()->getLogin(),
                             "Annulation réservation annonce",
                             $prestation->getProprietaire());
                $prestation->setIdClient(null);

                $litiges = $prestation->getLitiges();

                foreach($litiges as $l){
                    $entityManager->remove($l);
                }
            }
        } else {
            if ($interet) {
                $data['annonce']->setIdClient($data['user']);

                //On prélève le compte du client sans verser le propriétaire
                $data['user']->setNbFlorains($data['user']->getNbFlorains() - $data['annonce']->getCoutPrestation());

                $this->makeNotif("Réservation de l'annonce " . $data['annonce']->getTitre() . " par " . $this->getUser()->getLogin(),
                             "Réservation annonce",
                             $data['annonce']->getProprietaire());
            }
        }

        $data['entityManager']->flush();
        return $this->redirectToRoute('annonce_detail', ['id' => $id]);
    }

    #[Route('/aimerPrestation/{id}', name: 'app_aimer_prestation')]
    public function aimerPrestation($id): Response
    {
        $data = $this->getCommonData($id);
        $user = $data['user'];
        $prestation = $data['annonce'];

        if ($user != null) {
            $this->makeNotif($user->getLogin(). " a aimé votre annonce " . $prestation->getTitre(),
                             "Annonce aimée",
                             $prestation->getProprietaire());
            $prestation->setIdClient(null);
        } else return $this->redirectToRoute('app_login');

        if ($user->getPrestationsAimees()->contains($prestation)) {
            $user->removePrestationsAimee($prestation);
            $prestation->removeUtilisateursAimant($user);
        } else {
            $user->addPrestationsAimee($prestation);
            $prestation->addUtilisateursAimant($user);
        }
        $data['entityManager']->flush();
        return $this->redirectToRoute('annonce_detail', ['id' => $id]);
    }

    #[Route('/validerPrestation/{id}', name: 'app_valider_prestation')]
    public function validerPrestation($id): Response{
        $data = $this->getCommonData($id);
        $user = $data['user'];
        $prestation = $data['annonce'];

        if($user->getId() == $prestation->getFournisseur()->getId()) {
            $this->makeNotif("Le fournisseur de la prestation " . $prestation->getTitre() . " a validé votre réservation.",
                             "Réservation validée",
                             $data['entityManager']->getRepository(Personne::class)->find($prestation->getIdClient()));
            $prestation->setFournisseurValide(true);
        }else if ($user->getId() == $prestation->getIdClient()->getId()){
            $this->makeNotif("Le client de la prestation " . $prestation->getTitre() . " a validé sa réservation.",
                             "Réservation validée",
                             $data['entityManager']->getRepository(Personne::class)->find($prestation->getFournisseur()));
            $prestation->setClientValide(true);
        }

        $data['entityManager']->persist($prestation);
        $data['entityManager']->flush();

        if(count($prestation->getLitiges()) == 0 && $prestation->isFournisseurValide() && $prestation->isClientValide()){
            //La prestation est validé, donc on verse le montant de florain au fournisseur
            // Notif pour le propriétaire
            $this->makeNotif("La prestation " . $prestation->getTitre() . " a été validé des deux côtés. Elle est terminée.",
                             "Prestation conclue",
                             $data['entityManager']->getRepository(Personne::class)->find($prestation->getFournisseur()));
            // Notif pour le client
            $this->makeNotif("La prestation " . $prestation->getTitre() . " a été validé des deux côtés. Elle est terminée.",
                             "Prestation conclue",
                             $data['entityManager']->getRepository(Personne::class)->find($prestation->getIdClient()));
            $prestation->getFournisseur()->setNbFlorains($prestation->getFournisseur()->getNbFlorains() + $prestation->getCoutPrestation());

            $prestation->setTermine(true);
        }

        $data['entityManager']->persist($prestation);
        $data['entityManager']->flush();

        return $this->redirectToRoute('annonce_detail', ['id' => $id]);
    }

    public function makeNotif($desc,$type,$userID) {
        $notification = new Notification();
        $notification->setDescription($desc);
        $notification->setType($type);
        $notification->setDate(new DateTime());
        $notification->setUserId($userID);
        $this->doctrine->getManager()->persist($notification);
        $this->doctrine->getManager()->flush();
    }
}