<?php
namespace App\Service;
use App\Repository\BookingRepository;
use DateTime;
use App\Repository\ContactFormRepository;
use App\Repository\TestimonialRepository;
use Doctrine\ORM\EntityManagerInterface;
class StatsService{
public function __construct(EntityManagerInterface $manager, ContactFormRepository $contactRepo, TestimonialRepository $testimonialRepo, BookingRepository $bookingRepo){
$this->manager = $manager;
$this->testimonialRepo = $testimonialRepo;
$this->contactRepo = $contactRepo;
$this->bookingRepo = $bookingRepo;
}
/**
* Permet de recuperer le resultats des fonctions ci-après
*
* @return void
*/
public function getStats(){
$bookings = $this->getBookings();
$currentCA = $this->getCurrentCA();
$yearCA = $this->getYearCA();
$lastComments = $this->getLastComments();
$lastContacts = $this->getLastContacts();
return compact('bookings', 'lastComments', 'lastContacts', 'currentCA', 'yearCA');
}
public function getCurrentCA(){
$month = date('m');
// Je get toutes les commande payée du moi en cour
$monthBooking = $this->bookingRepo->findByMonth($month);
$total = 0;
foreach($monthBooking as $booking){
$total += ($booking->getAmount() / 100);
}
if($month == 1){
$month = "Janvier";
}
else if($month == 2){
$month = "Février";
}
else if($month == 3){
$month = "Mars";
}
else if($month == 4){
$month = "Avril";
}
else if($month == 5){
$month = "Mai";
}
else if($month == 6){
$month = "Juin";
}
else if($month == 7){
$month = "Juillet";
}
else if($month == 8){
$month = "Aout";
}
else if($month == 9){
$month = "Septembre";
}
else if($month == 10){
$month = "Octobre";
}
else if($month == 11){
$month = "Novembre";
}
else if($month == 12){
$month = "Décembre";
}
return [
'total' => $total,
'month' => $month
];
}
public function getYearCA(){
$year = date('Y');
$yearBookings = $this->bookingRepo->findByYear($year);
$temp = [];
$yearTotalCA = 0;
foreach($yearBookings as $booking){
$yearTotalCA += $booking->getAmount() / 100;
$bookingMonth = intval(date_format($booking->getEndDate() ,'m'));
if(!array_key_exists($bookingMonth, $temp)) {
//Si non, on l'a creer et l'initialise à 0
$temp[$bookingMonth] = 0;
}
//ajout de des value de chaque dates
$temp[$bookingMonth] += $booking->getAmount() / 100;
}
for ($i=1; $i <= 12; $i++) {
if (!array_key_exists($i, $temp)) {
$temp[$i] = 0;
}
}
return [
'year' => $year,
'total' => $yearTotalCA,
'monthsCA' => $temp
];
}
/**
* Permet de recuperer les resarvations à venir
*
* @return void
*/
public function getBookings(){
$todayDate = new DateTime();
$limite = 6;
// $today = $todayDate->format('d-m-Y');
$bookings = $this->bookingRepo->customFindByDate($todayDate, $limite);
if ($bookings != null) {
return $bookings;
}else {
return null;
}
}
/**
* Permet de récuperer les 5 derniers commantaires
*
* @return void
*/
public function getLastComments(){
$comments = $this->testimonialRepo->findBy([], ['createdAt'=>'DESC'], 6, 0);
return $comments;
}
/**
* Permet de récuperer les 5 derniers commantaires
*
* @return void
*/
public function getLastContacts(){
$contact = $this->contactRepo->findBy([], ['createdAt'=>'DESC'], 6, 0);
return $contact;
}
}