src/Controller/SearchCaisseController.php line 78

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Caisse;
  4. use App\Form\CaisseSearchFormType;
  5. use App\Repository\CaisseRepository;
  6. use App\Repository\ProductStatsRepository;
  7. use App\Repository\ProductToSellStatsRepository;
  8. use App\Repository\SiteRepository;
  9. use App\Repository\EncaissementReelRepository;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Omines\DataTablesBundle\Adapter\ArrayAdapter;
  14. use Omines\DataTablesBundle\Column\TextColumn;
  15. use Omines\DataTablesBundle\DataTableFactory;
  16. use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapter;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\JsonResponse;
  19. use App\Services\ExportExcel;
  20. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  21. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  22. use PhpOffice\PhpSpreadsheet\IOFactory;
  23. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  24. use PhpOffice\PhpSpreadsheet\Style\Fill;
  25. use PhpOffice\PhpSpreadsheet\Style\Border;
  26. class SearchCaisseController extends AbstractController
  27. {
  28.     private $factory;
  29. public function __construct(
  30.     DataTableFactory $factory,
  31.     private CaisseRepository $caisseRepository,
  32.     private ProductStatsRepository $productStatsRepository,
  33.     private ProductToSellStatsRepository $productToSellStatsRepository,
  34.     private SiteRepository $siteRepository,
  35.     private EncaissementReelRepository $encaissementReelRepository,
  36.     private ExportExcel $ExportExcel
  37. ) {
  38.     $this->factory $factory;
  39.     $this->caisseRepository $caisseRepository;
  40.     $this->productStatsRepository $productStatsRepository;
  41.     $this->productToSellStatsRepository $productToSellStatsRepository;
  42.     $this->siteRepository $siteRepository;
  43.     $this->encaissementReelRepository $encaissementReelRepository;
  44. }
  45. /**
  46.  * Creates and returns a basic DataTable instance.
  47.  *
  48.  * @param array $options Options to be passed
  49.  * @return DataTable
  50.  */
  51. protected function createDataTable(array $options = [])
  52. {
  53.     return $this->factory->create($options);
  54. }
  55. /**
  56.  * Creates and returns a DataTable based upon a registered DataTableType or an FQCN.
  57.  *
  58.  * @param string $type FQCN or service name
  59.  * @param array $typeOptions Type-specific options to be considered
  60.  * @param array $options Options to be passed
  61.  * @return DataTable
  62.  */
  63. protected function createDataTableFromType($type, array $typeOptions = [], array $options = [])
  64. {
  65.     return $this->factory->createFromType($type$typeOptions$options);
  66. }
  67.     #[Route('/'name'app_index_home')]
  68.     public function showAction(Request $requestDataTableFactory $dataTableFactory): Response
  69.     {
  70.         $caisseRecords = [];
  71.         $productStats = [];
  72.         $productToSellStats = [];
  73.         $startDate '';
  74.         $endDate '';
  75.         $arraySiteIds = [];
  76.         $caisse['euroVisiteurAvg'] = null;
  77.         $caisse['visiteurs'] = null;
  78.         $caisse['panierMoyenAvg'] = null;
  79.         // Créer le formulaire
  80.         $form $this->createForm(CaisseSearchFormType::class);
  81.         // Traiter la requête
  82.         $form->handleRequest($request);
  83.         if ($form->isSubmitted() && $form->isValid()) {
  84.             // Récupérer les données
  85.             $data $form->getData();
  86.              // Supposons que $data['date1'] est un objet DateTime
  87.             $date1 $data['date1'];
  88.             // Convertir en string au format jour-mois-année
  89.             $startDate $date1->format('Y-m-d');
  90.             $date2 $data['date2'];
  91.             // Convertir en string au format jour-mois-année
  92.             $endDate $date2->format('Y-m-d');
  93.             // Traiter les données ici...
  94.             $siteIds $data['site'];
  95.             foreach($siteIds as $key => $siteId) {
  96.                 $arraySiteIds[$key] = $siteId->getId();
  97.             }
  98.             $caisseRecords $this->caisseRepository->findByDateRangeAndSiteId($startDate$endDate$arraySiteIds);
  99.             $productStats $this->productStatsRepository->findStatsByDateRangeAndSiteId($startDate$endDate$siteIds);
  100.             $productToSellStats $this->productToSellStatsRepository->findStatsByDateRangeSiteIdAndProductType($startDate$endDate$siteIds);
  101.         }
  102.         // Afficher le formulaire
  103.         return $this->render('admin/caisse.html.twig', [
  104.             'form' => $form->createView(),
  105.             'caisseRecords' => $caisseRecords,
  106.             'productStats' => $productStats,
  107.             'productToSellStats' => $productToSellStats,
  108.             'date1' => $startDate,
  109.             'date2' => $endDate,
  110.             'siteIds' => json_encode($arraySiteIds)
  111.         ]);
  112.     }
  113.     // Assurez-vous d'inclure les autres dépendances nécessaires
  114.     
  115.     /**
  116.      * @Route("/ajax-caisse-export-stats", name="export_caisse_stats", methods={"POST"})
  117.      */
  118.     public function exportCaisseStats(Request $request)
  119.     {
  120.         if ($request->isXMLHttpRequest()) {    
  121.             $startDate $_POST['date1'];
  122.             $endDate $_POST['date2'];
  123.             $siteIds json_decode($_POST['sites']);
  124.             $caisseRecords $this->caisseRepository->findByDateRangeAndSiteId($startDate$endDate$siteIds);
  125.             $productStats $this->productStatsRepository->findStatsByDateRangeAndSiteId($startDate$endDate$siteIds);
  126.             $productToSellStats $this->productToSellStatsRepository->findStatsByDateRangeSiteIdAndProductType($startDate$endDate$siteIds);
  127.             
  128.             $filePath $this->exportFeuilleCaisse($startDate$endDate$siteIds$caisseRecords$productStats$productToSellStats);
  129.             
  130.             //unlink($filePath);
  131.             return new JsonResponse($filePath);
  132.         }
  133.         // Ici, insérez la logique de traitement de vos données
  134.     
  135.         // Supposons que $exportData est le résultat de votre traitement
  136.         // Vous devrez générer un fichier Excel et le renvoyer comme réponse
  137.     
  138.         // Retourne une réponse (exemple fictif)
  139.     }
  140.     public function exportFeuilleCaisse($startDate$endDate$siteIds$caisseRecords$productStats$productToSellStats)
  141.     {
  142.         $spreadsheet = new Spreadsheet();
  143.         $sheet $spreadsheet->getActiveSheet();
  144.         $siteString '';
  145.         $siteName '';
  146.         foreach($siteIds as $key =>  $siteId) {
  147.             if ($key !== 0) {
  148.                 $siteString .=', ';
  149.                 $siteName .='_';
  150.             }
  151.             $siteString .= strtoupper($this->siteRepository->findOneById($siteId)->getName());
  152.             $siteName .= strtoupper($this->siteRepository->findOneById($siteId)->getName());
  153.         }
  154.         foreach(range('A''Z') as $columnID) { // Ajustez la plage selon vos besoins
  155.             $sheet->getColumnDimension($columnID)
  156.                 ->setAutoSize(true);
  157.         }
  158.         $centerCell = [
  159.             'alignment' => [
  160.                 'horizontal' => Alignment::HORIZONTAL_CENTER,
  161.                 'vertical' => Alignment::VERTICAL_CENTER,
  162.             ],
  163.         ];
  164.         $yellowLight = [
  165.             'fill' => [
  166.                 'fillType' => Fill::FILL_SOLID,
  167.                 'startColor' => [
  168.                     'argb' => 'F5EEC8'// Utilisez le code couleur ARGB souhaité
  169.                 ],
  170.             ],
  171.             'borders' => [
  172.                 'allBorders' => [
  173.                     'borderStyle' => Border::BORDER_THIN,
  174.                     'color' => ['argb' => 'FF000000'], // Code couleur ARGB pour la bordure
  175.                 ],
  176.             ],
  177.             // Ajoutez ici d'autres styles si nécessaire
  178.         ];
  179.         $grey = [
  180.             'fill' => [
  181.                 'fillType' => Fill::FILL_SOLID,
  182.                 'startColor' => [
  183.                     'argb' => 'D0D4CA'// Utilisez le code couleur ARGB souhaité
  184.                 ],
  185.                 
  186.             ],
  187.             'borders' => [
  188.                 'allBorders' => [
  189.                     'borderStyle' => Border::BORDER_THIN,
  190.                     'color' => ['argb' => 'FF000000'], // Code couleur ARGB pour la bordure
  191.                 ],
  192.             ],
  193.             // Ajoutez ici d'autres styles si nécessaire
  194.         ];
  195.         $bold = [
  196.             'font' => [
  197.                 'bold' => true,
  198.             ],
  199.             // Ajoutez ici d'autres styles si nécessaire
  200.         ];
  201.     
  202.         
  203.          $sheet->setCellValue('A' 1'Date :');
  204.          $sheet->getStyle('A1')->applyFromArray($grey);
  205.          $sheet->setCellValue('B' 1date('d-m-Y'));
  206.          $sheet->getStyle('B1')->applyFromArray($yellowLight);
  207.          $sheet->setCellValue('A' 3'Lieu :');
  208.          $sheet->getStyle('A3')->applyFromArray($grey);
  209.          $sheet->setCellValue('B' 3$siteString);
  210.          $sheet->getStyle('B3')->applyFromArray($yellowLight);
  211.          $sheet->setCellValue('A' 5'Période du :');
  212.          $sheet->getStyle('A5')->applyFromArray($grey);
  213.          $sheet->setCellValue('B' 5date('d-m-Y'strtotime($startDate)));
  214.          $sheet->getStyle('B5')->applyFromArray($yellowLight);
  215.          $sheet->setCellValue('C' 5'au');
  216.          $sheet->getStyle('C5')->applyFromArray($grey);
  217.          $sheet->setCellValue('D' 5date('d-m-Y'strtotime($endDate)));
  218.          $sheet->getStyle('D5')->applyFromArray($yellowLight);
  219.          $sheet->getStyle('A1:E5')->applyFromArray($centerCell); // Applique le style à la plage de cellules de A1 à C10
  220.         
  221.          $letter 'B';
  222.          foreach ($productStats as $productStat) {
  223.             $sheet->setCellValue($letter 7ucfirst($productStat['productTypeName']));
  224.             $sheet->getStyle($letter 7)->applyFromArray($centerCell); // Appliquer le style à la cellule A1
  225.             $sheet->getStyle($letter 7)->applyFromArray($grey); // Appliquer le style à la cellule A1
  226.             $sheet->setCellValue($letter 8$productStat['productionSum']);
  227.             $sheet->setCellValue($letter 9$productStat['invendusSum']);
  228.             $sheet->setCellValue($letter 10$productStat['totalVentesSansOffertsSum']);
  229.             $sheet->setCellValue($letter 11round($productStat['pourcentageVentesAvg']));
  230.             $sheet->getStyle($letter .'8'.':'.$letter.'11')->applyFromArray($yellowLight); // Appliquer le style à la cellule A1
  231.             ++$letter;
  232.          }
  233.          $sheet->getStyle('A7:A11')->applyFromArray($grey); // Appliquer le style à la cellule A1
  234.          $sheet->getStyle('A8:A11')->applyFromArray($centerCell); // Applique le style à la plage de cellules de A1 à C10
  235.          $sheet->setCellValue('A8''Production');
  236.          $sheet->setCellValue('A9''Invendus');
  237.          $sheet->setCellValue('A10''Total des ventes');
  238.          $sheet->setCellValue('A11''% des ventes');
  239.          $letter 'B';
  240.          foreach ($productToSellStats as $productToSell) {
  241.             if ($productToSell['printToExport'] == 1) {
  242.                 $sheet->setCellValue($letter 13ucfirst($productToSell['productToSellTypeName']));
  243.                 $sheet->getStyle($letter 13)->applyFromArray($centerCell); 
  244.                 $sheet->getStyle($letter 13)->applyFromArray($grey); // Appliquer le style à la cellule A1
  245.                 $sheet->setCellValue($letter 14$productToSell['totalSum']);
  246.                 $sheet->setCellValue($letter 15round($productToSell['ratioAvg'], 2));
  247.                 $sheet->getStyle($letter .'14'.':'.$letter.'15')->applyFromArray($yellowLight); // Appliquer le style à la cellule A1
  248.                 ++$letter;
  249.             }
  250.         }
  251.         $sheet->getStyle('A14:A15')->applyFromArray($grey); // Appliquer le style à la cellule A1
  252.         $sheet->getStyle('A14:A15')->applyFromArray($centerCell); // Applique le style à la plage de cellules de A1 à C10
  253.         $sheet->setCellValue('A14''Nombre');
  254.         $sheet->setCellValue('A15''%');
  255.         $nombreTicket $this->productToSellStatsRepository->findNombreTickets($startDate$endDate$siteIds);
  256.         $sheet->setCellValue('A17''Total ticket');
  257.         $sheet->setCellValue('B17'$nombreTicket[0]['totalSum']);
  258.         $sheet->setCellValue('A18''Total article vendu');
  259.         $sheet->setCellValue('B18'$caisseRecords['totalVentesSum']);
  260.         $sheet->getStyle('A17:A18')->applyFromArray($grey); // Appliquer le style à la cellule A1
  261.         $sheet->getStyle('A17:A18')->applyFromArray($centerCell); // Applique le style à la plage de cellules de A1 à C10
  262.         $sheet->getStyle('B17:B18')->applyFromArray($yellowLight); // Appliquer le style à la cellule A1
  263.         $caTotal $this->encaissementReelRepository->findSumTotalByDateRangeAndSiteIds($startDate$endDate$siteIds);
  264.         $sheet->setCellValue('A20''Visiteurs');
  265.         $sheet->setCellValue('B20'$caisseRecords['visiteurs']);
  266.         $sheet->setCellValue('A21''%');
  267.         $sheet->setCellValue('B21'$caisseRecords['pourcentageShootAvg']);
  268.         $sheet->setCellValue('A22''€');
  269.         $sheet->setCellValue('B22',  $caisseRecords['euroVisiteurAvg']);
  270.         $sheet->setCellValue('A23''Panier moyen');
  271.         $sheet->setCellValue('B23'$caisseRecords['panierMoyenAvg']);
  272.         $sheet->setCellValue('A24'"Chiffre d'affaire");
  273.         $sheet->setCellValue('B24'$caTotal);
  274.         $sheet->getStyle('A20:A24')->applyFromArray($grey); // Appliquer le style à la cellule A1
  275.         $sheet->getStyle('A20:A24')->applyFromArray($centerCell); // Applique le style à la plage de cellules de A1 à C10
  276.         $sheet->getStyle('B20:B24')->applyFromArray($yellowLight); // Appliquer le style à la cellule A1
  277.         $fileName 'stats_caisse_'.$siteName.'_'.$startDate.'_'.$endDate;
  278.         $filePath $fileName.'.xlsx';
  279.         $writer = new Xlsx($spreadsheet);
  280.         $writer->save($filePath);
  281.         return $filePath;
  282.     }
  283.     /**
  284.      * @Route("/ajax-remove-file", name="ajax-remove-file", methods={"POST"})
  285.      */
  286.     public function removeFile(Request $request) {
  287.         if ($request->isXMLHttpRequest()) { 
  288.             $filePath $_POST['filePath'];
  289.             unlink($filePath);
  290.             return new JsonResponse('success');
  291.         }
  292.     }
  293.     #[Route('/caisse'name'app_import_manuel')]
  294.     public function caisseAction(Request $requestDataTableFactory $dataTableFactory)
  295.     {
  296.         $table $this->createDataTable()
  297.             ->add('name'TextColumn::class, ['label' => 'Feuille de caisse''className' => 'bold''data' => 'non renseigné'])
  298.             ->add('caisseDate'TextColumn::class, ['label' => 'Date''className' => 'bold''data' => 'non renseigné'])
  299.             ->add('site'TextColumn::class, ['field' => 'site.name''label' => 'Site''className' => 'bold''data' => 'non renseigné',  "globalSearchable" => true])
  300.             ->add('panier_moyen'TextColumn::class, ['label' => 'Panier moyen''className' => 'bold''data' => 'non renseigné'])
  301.             ->add('pourcentage_shoot'TextColumn::class, ['label' => 'Pourcentage shoot''className' => 'bold''data' => 'non renseigné'])
  302.             ->add('euro_visiteur'TextColumn::class, ['label' => 'Euro visiteur''className' => 'bold''data' => 'non renseigné'])
  303.             ->add('totalVente'TextColumn::class, ['label' => 'Vente totale''className' => 'bold''data' => 'non renseigné'])
  304.             ->createAdapter(ORMAdapter::class, [
  305.                 'entity' => Caisse::class,
  306.             ])
  307.             ->handleRequest($request);
  308.         if ($table->isCallback()) {
  309.             return $table->getResponse();
  310.         }
  311.         return $this->render('admin/list.html.twig', ['datatable' => $table]);
  312.     }
  313. }