src/Controller/ExportCongeController.php line 275

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\JsonResponse;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use App\Entity\RefRole;
  9. use App\Entity\Utilisateur;
  10. use App\Entity\UtilisateurInformation;
  11. use App\Entity\Workflow;
  12. use App\Entity\RefAffectation;
  13. use Doctrine\ORM\EntityManager;
  14. use JMS\Serializer\SerializationContext;
  15. use Doctrine\Persistence\ManagerRegistry;
  16. use App\Services\ExportConge;
  17. use App\Services\ExportCongeByUser;
  18. use JMS\Serializer\SerializerInterface;
  19. use App\Entity\RefAffectationCalendrier;
  20. /**
  21.  * Class ExportCongeController
  22.  * @package App\Controller
  23.  */
  24. class ExportCongeController extends AbstractController
  25. {
  26.     final public const BEGIN_YEAR 2019;
  27.     private $doctrine;
  28.     private $exportConge;
  29.     private $exportCongeByUser;
  30.     private $serializer;
  31.     public function __construct(ManagerRegistry $doctrineExportConge $exportCongeExportCongeByUser $exportCongeByUserSerializerInterface $serializer)
  32.     {
  33.         $this->doctrine $doctrine;
  34.         $this->exportConge $exportConge;
  35.         $this->exportCongeByUser $exportCongeByUser;
  36.         $this->serializer $serializer;
  37.     }
  38.     /**
  39.      * @param $year
  40.      * @return JsonResponse
  41.      * @throws \Exception
  42.      */
  43.     #[Route(path'/api/export/conges/{year}'name'ctsweb_export-conges'methods: ['GET'], requirements: ['year' => '\d+'])]
  44.     public function list($year)
  45.     {
  46.         $role $this->getUser()->getUtilisateur()->getIdRole();
  47.         if (!in_array($roleUtilisateur::MANAGE)) {
  48.             return new JsonResponse([], JsonResponse::HTTP_FORBIDDEN);
  49.         }
  50.         $affectation $this->getUser()->getIdAffectation();
  51.         $federation $this->getUser()->getIdFederation();
  52.         if (RefRole::ROLE_FEDE === $role || RefRole::ROLE_FEDE_INVITE === $role) {
  53.             $affectation null;
  54.         } else if (RefRole::ROLE_DR === $role || RefRole::ROLE_DR_SIGNATAIRE === $role) {
  55.             $federation null;
  56.         } else {
  57.             $affectation null;
  58.             $federation null;
  59.         }
  60.         /** @var EntityManager $entityManager */
  61.         $entityManager $this->doctrine;
  62.         $data $entityManager
  63.             ->getRepository(UtilisateurInformation::class)
  64.             ->getMyCtsDayOffInformation($federation$affectation$year);
  65.         $fileName "synthese_" . (new \DateTime())->format("Y-m-d_H_i_s");
  66.         $response $this->exportConge->exportList(
  67.             $data,
  68.             $year,
  69.             $affectation,
  70.             $federation,
  71.             false
  72.         );
  73.         $handle fopen('php://memory''r+');
  74.         rewind($handle);
  75.         $dispositionHeader $response->headers->makeDisposition(
  76.             ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  77.             "$fileName.xls"
  78.         );
  79.         $response->headers->set('Content-Type''text/vnd.ms-excel; charset=utf-8');
  80.         $response->headers->set('Pragma''public');
  81.         $response->headers->set('Cache-Control''maxage=1');
  82.         $response->headers->set('Content-Disposition'$dispositionHeader);
  83.         return $response;
  84.     }
  85.     #[Route(path'/api/export/conges/{idAff}/{year}/{calendrier}/{archive}'name'ctsweb_conges-annuel-annees-affectation-export'methods: ['GET'], requirements: ['id' => '\d+'])]
  86.     public function listAffectation(
  87.         $year,
  88.         $calendrier,
  89.         $archive
  90.     ) {
  91.         $affectation $this->getUser()->getIdAffectation();
  92.         $role $this->getUser()->getUtilisateur()->getIdRole();
  93.         if (!in_array($roleUtilisateur::MANAGE)) {
  94.             return new JsonResponse([], JsonResponse::HTTP_FORBIDDEN);
  95.         }
  96.         $federation $this->getUser()->getIdFederation();
  97.         if (RefRole::ROLE_FEDE === $role || RefRole::ROLE_FEDE_INVITE === $role) {
  98.             $affectation null;
  99.         } else if (RefRole::ROLE_DR === $role || RefRole::ROLE_DR_SIGNATAIRE === $role) {
  100.             $federation null;
  101.         } else {
  102.             $affectation null;
  103.             $federation null;
  104.         }
  105.         /** @var EntityManager $entityManager */
  106.         $entityManager $this->doctrine;
  107.         $data $entityManager
  108.             ->getRepository(UtilisateurInformation::class)
  109.             ->getMyCtsDayOffInformation($federation$affectation$year$archive$calendrier);
  110.         $fileName "synthese_" . (new \DateTime())->format("Y-m-d_H_i_s");
  111.         $response $this->exportConge->exportList(
  112.             $data,
  113.             $year,
  114.             $affectation,
  115.             $federation,
  116.             $calendrier
  117.         );
  118.         $handle fopen('php://memory''r+');
  119.         rewind($handle);
  120.         $dispositionHeader $response->headers->makeDisposition(
  121.             ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  122.             "$fileName.xls"
  123.         );
  124.         $response->headers->set('Content-Type''text/vnd.ms-excel; charset=utf-8');
  125.         $response->headers->set('Pragma''public');
  126.         $response->headers->set('Cache-Control''maxage=1');
  127.         $response->headers->set('Content-Disposition'$dispositionHeader);
  128.         return $response;
  129.     }
  130.     /**
  131.      * @param Request $request
  132.      * @param Utilisateur $user
  133.      * @return mixed
  134.      * @throws \Exception
  135.      */
  136.     #[Route(path'/api/export/conges/user/{id}/{year}'name'ctsweb_conges-export-year'methods: ['GET'], requirements: ['id' => '\d+''year' => '\d+'])]
  137.     public function listByUserConges(
  138.         int $id,
  139.         int $year,
  140.         // string $calendrier = "c",
  141.         int $archive 0
  142.     ) {
  143.         $archive boolval($archive);
  144.         $workflow 'accepted';
  145.         $entityManager $this->doctrine->getManager();
  146.         $user $entityManager
  147.             ->getRepository(Utilisateur::class)
  148.             ->findOneBy(["idUtilisateur" => $id]);
  149.         $utilisateurInformation $entityManager
  150.             ->getRepository(UtilisateurInformation::class)
  151.             ->findOneBy(["idUtilisateur" => $id]);
  152.         $Idaffectation $utilisateurInformation->getIdAffectation()->getIdAffectation();
  153.         $affectation $entityManager
  154.             ->getRepository(RefAffectation::class)
  155.             ->findOneBy(["idAffectation" => $Idaffectation]);
  156.         if ($workflow && (in_array($workflow, ['waiting''accepted''signed']))) {
  157.             if ($workflow == 'accepted') {
  158.                 $workflow = [Workflow::STATES[$workflow], Workflow::CO_SIGN];
  159.             } else {
  160.                 $workflow = [Workflow::STATES[$workflow]];
  161.             }
  162.         } else {
  163.             $workflow = [Workflow::CO_ACCEPTEDWorkflow::CO_REJECTWorkflow::CO_SIGN];
  164.         }
  165.         $fileName "Synthese__" . (new \DateTime())->format("Y-m-d_H_i_s");
  166.         $today = (new \DateTime());
  167.         $year $today->format("Y");
  168.         $refcalendrier =  $entityManager
  169.             ->getRepository(RefAffectationCalendrier::class)
  170.             ->findAllByRefAffectation($Idaffectation);
  171.         $tableaubascule $entityManager
  172.             ->getRepository(RefAffectationCalendrier::class)
  173.             ->getClosestDates($refcalendrier);
  174.         if (isset($tableaubascule[0]) &&  !empty($tableaubascule[0])) {
  175.             $calendrierScolaire $tableaubascule[0]['calendrier_scolaire'];
  176.             $dateBasculeCongeSolaire $tableaubascule[0]['date_bascule'];
  177.         } else {
  178.             $calendrierScolaire false;
  179.             $dateBasculeCongeSolaire null;
  180.         }
  181.         $today = new \DateTime();
  182.         if ($dateBasculeCongeSolaire) {
  183.             $yearBascule $this->anneeMax($dateBasculeCongeSolaire);
  184.             if ($today >= $dateBasculeCongeSolaire) {
  185.                 $congeScolaire $calendrierScolaire;
  186.             } else {
  187.                 $congeScolaire = !$calendrierScolaire;
  188.             }
  189.         } else {
  190.             $congeScolaire $calendrierScolaire;
  191.             $yearBascule null;
  192.         }
  193.         // $dateBasculeCongeSolaire = $affectation->getDateBascule();
  194.         $calendrier = ($congeScolaire) ? 's' 'c';
  195.         if ((true === $congeScolaire) && ($dateBasculeCongeSolaire $today) && ($today < new \DateTime("$year-09-01"))) {
  196.             $year $year 1;
  197.         } else {
  198.             $year $year;
  199.         }
  200.         $response $this->exportCongeByUser
  201.             ->exportList(
  202.                 $user,
  203.                 $workflow,
  204.                 $year,
  205.                 $calendrier,
  206.                 $archive
  207.             );
  208.         $handle fopen('php://memory''r+');
  209.         rewind($handle);
  210.         $dispositionHeader $response->headers->makeDisposition(
  211.             ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  212.             "$fileName.xls"
  213.         );
  214.         $response->headers->set('Content-Type''text/vnd.ms-excel; charset=utf-8');
  215.         $response->headers->set('Pragma''public');
  216.         $response->headers->set('Cache-Control''maxage=1');
  217.         $response->headers->set('Content-Disposition'$dispositionHeader);
  218.         return $response;
  219.     }
  220.     /**
  221.      * @param Request $request
  222.      * @param Utilisateur $user
  223.      * @return mixed
  224.      * @throws \Exception
  225.      */
  226.     #[Route(path'/api/conges-droits-annees-export/{id}/{year}/{calendrier}/{archive}'name'ctsweb_conges-annuel-annees-export'methods: ['GET'], requirements: ['id' => '\d+''year' => '\d+'])]
  227.     public function listByUser(
  228.         int $id,
  229.         int $year,
  230.         string $calendrier "c",
  231.         int $archive 0
  232.     ) {
  233.         $archive boolval($archive);
  234.         $workflow 'accepted';
  235.         $entityManager $this->doctrine->getManager();
  236.         $user $entityManager
  237.             ->getRepository(Utilisateur::class)
  238.             ->findOneBy(["idUtilisateur" => $id]);
  239.         $utilisateurInformation $entityManager
  240.             ->getRepository(UtilisateurInformation::class)
  241.             ->findOneBy(["idUtilisateur" => $id]);
  242.         $Idaffectation $utilisateurInformation->getIdAffectation()->getIdAffectation();
  243.         $affectation $entityManager
  244.             ->getRepository(RefAffectation::class)
  245.             ->findOneBy(["idAffectation" => $Idaffectation]);
  246.         if ($workflow && (in_array($workflow, ['waiting''accepted''signed']))) {
  247.             if ($workflow == 'accepted') {
  248.                 $workflow = [Workflow::STATES[$workflow], Workflow::CO_SIGN];
  249.             } else {
  250.                 $workflow = [Workflow::STATES[$workflow]];
  251.             }
  252.         } else {
  253.             $workflow = [Workflow::CO_ACCEPTEDWorkflow::CO_REJECTWorkflow::CO_SIGN];
  254.         }
  255.         $fileName "Synthese__" . (new \DateTime())->format("Y-m-d_H_i_s");
  256.         $response $this->exportCongeByUser
  257.             ->exportList(
  258.                 $user,
  259.                 $workflow,
  260.                 $year,
  261.                 $calendrier,
  262.                 $archive
  263.             );
  264.         $handle fopen('php://memory''r+');
  265.         rewind($handle);
  266.         $dispositionHeader $response->headers->makeDisposition(
  267.             ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  268.             "$fileName.xls"
  269.         );
  270.         $response->headers->set('Content-Type''text/vnd.ms-excel; charset=utf-8');
  271.         $response->headers->set('Pragma''public');
  272.         $response->headers->set('Cache-Control''maxage=1');
  273.         $response->headers->set('Content-Disposition'$dispositionHeader);
  274.         return $response;
  275.     }
  276.     /**
  277.      * @return JsonResponse
  278.      * @throws \Exception
  279.      */
  280.     #[Route(path'/api/export/years'name'ctsweb_export-conges-list-year'methods: ['GET'])]
  281.     public function listYear()
  282.     {
  283.         $year = [];
  284.         $todayYear = (new \DateTime())->format("Y");
  285.         $begin self::BEGIN_YEAR;
  286.         for ($begin$begin <= $todayYear$begin++) {
  287.             $year[] = ['year' => $begin];
  288.         }
  289.         return new JsonResponse(
  290.             $this->serialize($year),
  291.             JsonResponse::HTTP_OK,
  292.             [],
  293.             true
  294.         );
  295.     }
  296.     /**
  297.      * @param array $states
  298.      * @return mixed
  299.      */
  300.     private function serialize($item)
  301.     {
  302.         $data = ["items" => $item];
  303.         $context = new SerializationContext();
  304.         $context->setSerializeNull(true);
  305.         return $this->serializer->serialize($data'json'$context);
  306.     }
  307.     public  function anneeMax($dates)
  308.     {
  309.         $maxYear null;
  310.         foreach ((array) $dates as $date) {
  311.             $year intval(substr($date04));
  312.             if ($maxYear === null || $year $maxYear) {
  313.                 $maxYear $year;
  314.             }
  315.         }
  316.         return $maxYear;
  317.     }
  318. }