src/Service/StatsService.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Repository\BookingRepository;
  4. use DateTime;
  5. use App\Repository\ContactFormRepository;
  6. use App\Repository\TestimonialRepository;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. class StatsService{
  9.   
  10.     public function __construct(EntityManagerInterface $managerContactFormRepository $contactRepoTestimonialRepository $testimonialRepoBookingRepository $bookingRepo){
  11.         $this->manager $manager;
  12.         $this->testimonialRepo $testimonialRepo;
  13.         $this->contactRepo $contactRepo;
  14.         $this->bookingRepo $bookingRepo;
  15.     }
  16.     /**
  17.      * Permet de recuperer le resultats des fonctions ci-après
  18.      *
  19.      * @return void
  20.      */
  21.     public function getStats(){
  22.         $bookings $this->getBookings();
  23.         $currentCA $this->getCurrentCA();
  24.         $yearCA $this->getYearCA();
  25.         $lastComments $this->getLastComments();
  26.         $lastContacts $this->getLastContacts();
  27.         return compact('bookings''lastComments''lastContacts''currentCA''yearCA');
  28.     }
  29.     public function getCurrentCA(){
  30.         $month date('m');
  31.         // Je get toutes les commande payée du moi en cour
  32.         $monthBooking $this->bookingRepo->findByMonth($month);
  33.         $total 0;
  34.         foreach($monthBooking as $booking){
  35.             $total += ($booking->getAmount() / 100);
  36.         }
  37.         if($month == 1){
  38.             $month "Janvier";
  39.         } 
  40.         else if($month == 2){
  41.             $month "Février";
  42.         }
  43.         else if($month == 3){
  44.             $month "Mars";
  45.         }
  46.         else if($month == 4){
  47.             $month "Avril";
  48.         }
  49.         else if($month == 5){
  50.             $month "Mai";
  51.         }
  52.         else if($month == 6){
  53.             $month "Juin";
  54.         }
  55.         else if($month == 7){
  56.             $month "Juillet";
  57.         }
  58.         else if($month == 8){
  59.             $month "Aout";
  60.         }
  61.         else if($month == 9){
  62.             $month "Septembre";
  63.         }
  64.         else if($month == 10){
  65.             $month "Octobre";
  66.         }
  67.         else if($month == 11){
  68.             $month "Novembre";
  69.         }
  70.         else if($month == 12){
  71.             $month "Décembre";
  72.         }
  73.         return [
  74.             'total' => $total,
  75.             'month' => $month
  76.         ];
  77.     }
  78.     public function getYearCA(){
  79.         $year date('Y');
  80.         $yearBookings $this->bookingRepo->findByYear($year);
  81.         $temp = [];
  82.         $yearTotalCA 0;
  83.         foreach($yearBookings as $booking){
  84.             $yearTotalCA += $booking->getAmount() / 100;
  85.             $bookingMonth intval(date_format($booking->getEndDate() ,'m'));
  86.             if(!array_key_exists($bookingMonth$temp)) {
  87.                 //Si non, on l'a creer et l'initialise à 0
  88.                 $temp[$bookingMonth] = 0;
  89.             }
  90.             //ajout de des value de chaque dates
  91.             $temp[$bookingMonth] += $booking->getAmount() / 100;
  92.         }
  93.         for ($i=1$i <= 12$i++) { 
  94.             if (!array_key_exists($i$temp)) {
  95.                 $temp[$i] = 0;
  96.             }
  97.         }
  98.         return [
  99.             'year' => $year,
  100.             'total' => $yearTotalCA,
  101.             'monthsCA' => $temp
  102.         ];
  103.     }
  104.     /**
  105.      * Permet de recuperer les resarvations à venir
  106.      *
  107.      * @return void
  108.      */
  109.     public function getBookings(){
  110.         $todayDate = new DateTime();
  111.         $limite 6;
  112.         // $today = $todayDate->format('d-m-Y');
  113.         $bookings $this->bookingRepo->customFindByDate($todayDate$limite);
  114.         if ($bookings != null) {
  115.             return $bookings;
  116.         }else {
  117.             return null;
  118.         }
  119.     }
  120.     /**
  121.      * Permet de récuperer les 5 derniers commantaires 
  122.      *
  123.      * @return void
  124.      */
  125.     public function getLastComments(){
  126.         $comments $this->testimonialRepo->findBy([], ['createdAt'=>'DESC'], 60);
  127.         return $comments;
  128.     }
  129.     /**
  130.      * Permet de récuperer les 5 derniers commantaires 
  131.      *
  132.      * @return void
  133.      */
  134.     public function getLastContacts(){
  135.         $contact $this->contactRepo->findBy([], ['createdAt'=>'DESC'], 60);
  136.         return $contact;
  137.     }
  138. }