<?php
namespace App\Controller;
use App\Entity\Precommission;
use App\Form\ReportFilterType;
use App\Repository\CommissionRepository;
use App\Repository\NouvelleorderRepository;
use App\Repository\OrdersRepository;
use App\Repository\PrecommissionRepository;
use App\Repository\UserRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\Request;
use \DateTime;
class RapportController extends AbstractController
{
private $security;
private $orderRepository;
private $nouvelleorderRepository;
public function __construct(Security $security, OrdersRepository $orderRepository , NouvelleorderRepository $nouvelleorderRepository)
{
$this->security = $security;
$this->orderRepository = $orderRepository;
$this->nouvelleordersRepository = $nouvelleorderRepository;
}
/**
* @Route("/reports", name="user_reports")
*/
public function reports(Request $request, CommissionRepository $commissionRepo): Response
{
$user = $this->security->getUser();
$isAdmin = in_array('ROLE_ADMIN', $user->getRoles());
// $generations = range(1, 5); // Générations de G1 à G5
$generations = range(1, 4); // Générations de G1 à G5
// Calculer le premier jour du mois en cours et le jour actuel
$startOfMonth = new DateTime('first day of this month');
$endOfMonth = new DateTime(); // Pour le jour actuel
// Initialiser les valeurs par défaut du formulaire
$defaultData = ['startDate' => $startOfMonth, 'endDate' => $endOfMonth];
$form = $this->createForm(ReportFilterType::class, $defaultData, ['is_admin' => $isAdmin]);
$form->handleRequest($request);
$formatter = new \IntlDateFormatter('fr_FR', \IntlDateFormatter::LONG, \IntlDateFormatter::NONE, null, \IntlDateFormatter::GREGORIAN);
// Déterminez le nom du mois pour l'affichage, basé sur la startDate ou le mois actuel par défaut
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$selectedUser = $isAdmin && $data['user'] ? $data['user'] : $user;
$startDate = $data['startDate'] ?: $startOfMonth;
$endDate = $data['endDate'] ?: $endOfMonth;
$startMonthName = $formatter->format($startDate);
$endMonthName = $endDate ? $formatter->format($endDate) : $startMonthName;
// Gérer le cas où la plage de dates s'étend sur plus d'un mois
$monthName = $startMonthName === $endMonthName ? $startMonthName : $startMonthName . ' à ' . $endMonthName;
// Utilisez la méthode personnalisée du repository pour filtrer les commandes
$orders = $this->orderRepository->findOrdersByUserAndDateRange($selectedUser, $startDate, $endDate);
$totalAmount = $this->orderRepository->findTotalAmountByUserAndDateRange($selectedUser, $startDate, $endDate);
$commissionsByLevel = $commissionRepo->getTotalCommissionsByLevel($selectedUser, $startDate, $endDate);
// Créez un tableau pour garder les totaux des commandes par niveau
$totalOrdersByLevel = [];
// Obtenez les ID de commande par niveau pour les commissions
$orderIdsByLevel = $commissionRepo->getOrderIdsByLevel($selectedUser, $startDate, $endDate);
// Groupez les ID de commande par niveau
$groupedOrderIds = [];
foreach ($orderIdsByLevel as $item) {
$groupedOrderIds[$item['level']][] = $item['orderId'];
}
// Pour chaque niveau, obtenez le montant total des commandes
foreach ($groupedOrderIds as $level => $orderIds) {
$totalOrdersByLevel[$level] = $this->orderRepository->getTotalAmountForOrderIds($orderIds);
}
$detailsByGeneration = [];
foreach ($generations as $generation) {
$detailsByGeneration['G' . $generation] = $commissionRepo->getDetailsByGeneration($selectedUser, $generation, $startDate, $endDate);
}
$totalOrdersAmount = $this->orderRepository->getTotalOrdersAmountBetweenDates($startDate, $endDate);
$totalCommissionsAmount = $commissionRepo->getTotalCommissionsAmountBetweenDates($startDate, $endDate);
} else {
// Affichez les commandes du mois en cours par défaut
$orders = $this->orderRepository->findOrdersByUserAndDateRange($user, $startOfMonth, $endOfMonth);
$totalAmount = $this->orderRepository->findTotalAmountByUserAndDateRange($user, $startOfMonth, $endOfMonth);
$monthName = $formatter->format(new \DateTime());
$commissionsByLevel = $commissionRepo->getTotalCommissionsByLevel($user, $startOfMonth, $endOfMonth);
$totalOrdersByLevel = [];
$orderIdsByLevel = $commissionRepo->getOrderIdsByLevel($user, $startOfMonth, $endOfMonth);
$groupedOrderIds = [];
foreach ($orderIdsByLevel as $item) {
$groupedOrderIds[$item['level']][] = $item['orderId'];
}
foreach ($groupedOrderIds as $level => $orderIds) {
$totalOrdersByLevel[$level] = $this->orderRepository->getTotalAmountForOrderIds($orderIds);
}
$detailsByGeneration = [];
foreach ($generations as $generation) {
$detailsByGeneration['G' . $generation] = $commissionRepo->getDetailsByGeneration($user, $generation, $startOfMonth, $endOfMonth);
}
$totalOrdersAmount = $this->orderRepository->getTotalOrdersAmountBetweenDates($startOfMonth, $endOfMonth);
$totalCommissionsAmount = $commissionRepo->getTotalCommissionsAmountBetweenDates($startOfMonth, $endOfMonth);
}
return $this->render('rapport/index.html.twig', [
'filterForm' => $form->createView(),
'orders' => $orders,
'monthName' => ucfirst($monthName), // Assurez-vous que le nom du mois commence par une majuscule
'totalAmount' => $totalAmount, // Ajoutez cette ligne
'commissionsByLevel' => $commissionsByLevel,
'totalOrdersByLevel' => $totalOrdersByLevel,
'detailsByGeneration' => $detailsByGeneration,
'totalOrdersAmount' => $totalOrdersAmount,
'totalCommissionsAmount' => $totalCommissionsAmount,
]);
}
/**
* @Route("/non-actif", name="user_reports_non")
*/
public function indexnonactif(UserRepository $userRepository): Response
{
$user = $this->getUser(); // Supposons que ceci retourne l'utilisateur connecté
$userId = $user->getId();
$formatter = new \IntlDateFormatter('fr_FR', \IntlDateFormatter::LONG, \IntlDateFormatter::NONE, null, \IntlDateFormatter::GREGORIAN, 'MMMM');
$currentMonthName = $formatter->format(new \DateTime());
$usersWithoutOrders = $userRepository->findReferredUsersWithoutOrdersThisMonth();
return $this->render('rapport/nonactif.html.twig', [
'usersWithoutOrders' => $usersWithoutOrders,
'month'=>$currentMonthName
]);
}
/**
* @Route("/reports/neworders", name="user_reports_new_orders")
*/
public function reportsnewOrders(Request $request, PrecommissionRepository $precommissionRepository): Response
{
$user = $this->security->getUser();
$isAdmin = in_array('ROLE_ADMIN', $user->getRoles());
// $generations = range(1, 5); // Générations de G1 à G5
$generations = range(1, 4); // Générations de G1 à G5
// Calculer le premier jour du mois en cours et le jour actuel
$startOfMonth = new DateTime('first day of this month');
$endOfMonth = new DateTime(); // Pour le jour actuel
// Initialiser les valeurs par défaut du formulaire
$defaultData = ['startDate' => $startOfMonth, 'endDate' => $endOfMonth];
$form = $this->createForm(ReportFilterType::class, $defaultData, ['is_admin' => $isAdmin]);
$form->handleRequest($request);
$formatter = new \IntlDateFormatter('fr_FR', \IntlDateFormatter::LONG, \IntlDateFormatter::NONE, null, \IntlDateFormatter::GREGORIAN);
// Déterminez le nom du mois pour l'affichage, basé sur la startDate ou le mois actuel par défaut
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$selectedUser = $isAdmin && $data['user'] ? $data['user'] : $user;
$startDate = $data['startDate'] ?: $startOfMonth;
$endDate = $data['endDate'] ?: $endOfMonth;
$startMonthName = $formatter->format($startDate);
$endMonthName = $endDate ? $formatter->format($endDate) : $startMonthName;
// Gérer le cas où la plage de dates s'étend sur plus d'un mois
$monthName = $startMonthName === $endMonthName ? $startMonthName : $startMonthName . ' à ' . $endMonthName;
// Utilisez la méthode personnalisée du repository pour filtrer les commandes
$ordersnew = $this->nouvelleordersRepository->findOrdersByUserAndDateRangenouvelleorders($selectedUser, $startDate, $endDate);
$totalAmount = $this->nouvelleordersRepository->findTotalAmountByUserAndDateRangenouvelleorders($selectedUser, $startDate, $endDate);
$commissionsByLevelnew = $precommissionRepository->getTotalpreCommissionsByLevel($selectedUser, $startDate, $endDate);
// Créez un tableau pour garder les totaux des commandes par niveau
$totalOrdersByLevel = [];
// Obtenez les ID de commande par niveau pour les commissions
$orderIdsByLevel = $precommissionRepository->getPrOrderIdsByLevel($selectedUser, $startDate, $endDate);
// Groupez les ID de commande par niveau
$groupedOrderIds = [];
foreach ($orderIdsByLevel as $item) {
$groupedOrderIds[$item['level']][] = $item['orderId'];
}
// Pour chaque niveau, obtenez le montant total des commandes
foreach ($groupedOrderIds as $level => $orderIds) {
$totalOrdersByLevel[$level] = $this->nouvelleordersRepository->getTotalAmountFornewOrderIds($orderIds);
}
$detailsByGeneration = [];
foreach ($generations as $generation) {
$detailsByGeneration['G' . $generation] = $precommissionRepository->getDetailsByGenerationneworders($selectedUser, $generation, $startDate, $endDate);
}
$totalOrdersAmount = $this->nouvelleordersRepository->getTotalOrdersAmountBetweenDatesnew($startDate, $endDate);
$totalCommissionsAmount = $precommissionRepository->getTotalCommissionsAmountBetweenDatesnew($startDate, $endDate);
} else {
// Affichez les commandes du mois en cours par défaut
$ordersnew = $this->nouvelleordersRepository->findOrdersByUserAndDateRangenouvelleorders($user, $startOfMonth, $endOfMonth);
$totalAmount = $this->nouvelleordersRepository->findTotalAmountByUserAndDateRangenouvelleorders($user, $startOfMonth, $endOfMonth);
$monthName = $formatter->format(new \DateTime());
$commissionsByLevelnew = $precommissionRepository->getTotalpreCommissionsByLevel($user, $startOfMonth, $endOfMonth);
$totalOrdersByLevel = [];
$orderIdsByLevel = $precommissionRepository->getPrOrderIdsByLevel($user, $startOfMonth, $endOfMonth);
$groupedOrderIds = [];
foreach ($orderIdsByLevel as $item) {
$groupedOrderIds[$item['level']][] = $item['orderId'];
}
foreach ($groupedOrderIds as $level => $orderIds) {
$totalOrdersByLevel[$level] = $this->nouvelleordersRepository->getTotalAmountFornewOrderIds($orderIds);
}
$detailsByGeneration = [];
foreach ($generations as $generation) {
$detailsByGeneration['G' . $generation] = $precommissionRepository->getDetailsByGenerationneworders($user, $generation, $startOfMonth, $endOfMonth);
}
$totalOrdersAmount = $this->nouvelleordersRepository->getTotalOrdersAmountBetweenDatesnew($startOfMonth, $endOfMonth);
$totalCommissionsAmount = $precommissionRepository->getTotalCommissionsAmountBetweenDatesnew($startOfMonth, $endOfMonth);
}
return $this->render('rapport/Rapportprecommande.html.twig', [
'filterForm' => $form->createView(),
'orders' => $ordersnew,
'monthName' => ucfirst($monthName), // Assurez-vous que le nom du mois commence par une majuscule
'totalAmount' => $totalAmount, // Ajoutez cette ligne
'commissionsByLevel' => $commissionsByLevelnew,
'totalOrdersByLevel' => $totalOrdersByLevel,
'detailsByGeneration' => $detailsByGeneration,
'totalOrdersAmount' => $totalOrdersAmount,
'totalCommissionsAmount' => $totalCommissionsAmount,
]);
}
}