<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\RefRole;
use App\Entity\Utilisateur;
use App\Entity\UtilisateurInformation;
use App\Entity\Workflow;
use App\Entity\RefAffectation;
use Doctrine\ORM\EntityManager;
use JMS\Serializer\SerializationContext;
use Doctrine\Persistence\ManagerRegistry;
use App\Services\ExportConge;
use App\Services\ExportCongeByUser;
use JMS\Serializer\SerializerInterface;
use App\Entity\RefAffectationCalendrier;
/**
* Class ExportCongeController
* @package App\Controller
*/
class ExportCongeController extends AbstractController
{
final public const BEGIN_YEAR = 2019;
private $doctrine;
private $exportConge;
private $exportCongeByUser;
private $serializer;
public function __construct(ManagerRegistry $doctrine, ExportConge $exportConge, ExportCongeByUser $exportCongeByUser, SerializerInterface $serializer)
{
$this->doctrine = $doctrine;
$this->exportConge = $exportConge;
$this->exportCongeByUser = $exportCongeByUser;
$this->serializer = $serializer;
}
/**
* @param $year
* @return JsonResponse
* @throws \Exception
*/
#[Route(path: '/api/export/conges/{year}', name: 'ctsweb_export-conges', methods: ['GET'], requirements: ['year' => '\d+'])]
public function list($year)
{
$role = $this->getUser()->getUtilisateur()->getIdRole();
if (!in_array($role, Utilisateur::MANAGE)) {
return new JsonResponse([], JsonResponse::HTTP_FORBIDDEN);
}
$affectation = $this->getUser()->getIdAffectation();
$federation = $this->getUser()->getIdFederation();
if (RefRole::ROLE_FEDE === $role || RefRole::ROLE_FEDE_INVITE === $role) {
$affectation = null;
} else if (RefRole::ROLE_DR === $role || RefRole::ROLE_DR_SIGNATAIRE === $role) {
$federation = null;
} else {
$affectation = null;
$federation = null;
}
/** @var EntityManager $entityManager */
$entityManager = $this->doctrine;
$data = $entityManager
->getRepository(UtilisateurInformation::class)
->getMyCtsDayOffInformation($federation, $affectation, $year);
$fileName = "synthese_" . (new \DateTime())->format("Y-m-d_H_i_s");
$response = $this->exportConge->exportList(
$data,
$year,
$affectation,
$federation,
false
);
$handle = fopen('php://memory', 'r+');
rewind($handle);
$dispositionHeader = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
"$fileName.xls"
);
$response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');
$response->headers->set('Pragma', 'public');
$response->headers->set('Cache-Control', 'maxage=1');
$response->headers->set('Content-Disposition', $dispositionHeader);
return $response;
}
#[Route(path: '/api/export/conges/{idAff}/{year}/{calendrier}/{archive}', name: 'ctsweb_conges-annuel-annees-affectation-export', methods: ['GET'], requirements: ['id' => '\d+'])]
public function listAffectation(
$year,
$calendrier,
$archive
) {
$affectation = $this->getUser()->getIdAffectation();
$role = $this->getUser()->getUtilisateur()->getIdRole();
if (!in_array($role, Utilisateur::MANAGE)) {
return new JsonResponse([], JsonResponse::HTTP_FORBIDDEN);
}
$federation = $this->getUser()->getIdFederation();
if (RefRole::ROLE_FEDE === $role || RefRole::ROLE_FEDE_INVITE === $role) {
$affectation = null;
} else if (RefRole::ROLE_DR === $role || RefRole::ROLE_DR_SIGNATAIRE === $role) {
$federation = null;
} else {
$affectation = null;
$federation = null;
}
/** @var EntityManager $entityManager */
$entityManager = $this->doctrine;
$data = $entityManager
->getRepository(UtilisateurInformation::class)
->getMyCtsDayOffInformation($federation, $affectation, $year, $archive, $calendrier);
$fileName = "synthese_" . (new \DateTime())->format("Y-m-d_H_i_s");
$response = $this->exportConge->exportList(
$data,
$year,
$affectation,
$federation,
$calendrier
);
$handle = fopen('php://memory', 'r+');
rewind($handle);
$dispositionHeader = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
"$fileName.xls"
);
$response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');
$response->headers->set('Pragma', 'public');
$response->headers->set('Cache-Control', 'maxage=1');
$response->headers->set('Content-Disposition', $dispositionHeader);
return $response;
}
/**
* @param Request $request
* @param Utilisateur $user
* @return mixed
* @throws \Exception
*/
#[Route(path: '/api/export/conges/user/{id}/{year}', name: 'ctsweb_conges-export-year', methods: ['GET'], requirements: ['id' => '\d+', 'year' => '\d+'])]
public function listByUserConges(
int $id,
int $year,
// string $calendrier = "c",
int $archive = 0
) {
$archive = boolval($archive);
$workflow = 'accepted';
$entityManager = $this->doctrine->getManager();
$user = $entityManager
->getRepository(Utilisateur::class)
->findOneBy(["idUtilisateur" => $id]);
$utilisateurInformation = $entityManager
->getRepository(UtilisateurInformation::class)
->findOneBy(["idUtilisateur" => $id]);
$Idaffectation = $utilisateurInformation->getIdAffectation()->getIdAffectation();
$affectation = $entityManager
->getRepository(RefAffectation::class)
->findOneBy(["idAffectation" => $Idaffectation]);
if ($workflow && (in_array($workflow, ['waiting', 'accepted', 'signed']))) {
if ($workflow == 'accepted') {
$workflow = [Workflow::STATES[$workflow], Workflow::CO_SIGN];
} else {
$workflow = [Workflow::STATES[$workflow]];
}
} else {
$workflow = [Workflow::CO_ACCEPTED, Workflow::CO_REJECT, Workflow::CO_SIGN];
}
$fileName = "Synthese__" . (new \DateTime())->format("Y-m-d_H_i_s");
$today = (new \DateTime());
$year = $today->format("Y");
$refcalendrier = $entityManager
->getRepository(RefAffectationCalendrier::class)
->findAllByRefAffectation($Idaffectation);
$tableaubascule = $entityManager
->getRepository(RefAffectationCalendrier::class)
->getClosestDates($refcalendrier);
if (isset($tableaubascule[0]) && !empty($tableaubascule[0])) {
$calendrierScolaire = $tableaubascule[0]['calendrier_scolaire'];
$dateBasculeCongeSolaire = $tableaubascule[0]['date_bascule'];
} else {
$calendrierScolaire = false;
$dateBasculeCongeSolaire = null;
}
$today = new \DateTime();
if ($dateBasculeCongeSolaire) {
$yearBascule = $this->anneeMax($dateBasculeCongeSolaire);
if ($today >= $dateBasculeCongeSolaire) {
$congeScolaire = $calendrierScolaire;
} else {
$congeScolaire = !$calendrierScolaire;
}
} else {
$congeScolaire = $calendrierScolaire;
$yearBascule = null;
}
// $dateBasculeCongeSolaire = $affectation->getDateBascule();
$calendrier = ($congeScolaire) ? 's' : 'c';
if ((true === $congeScolaire) && ($dateBasculeCongeSolaire < $today) && ($today < new \DateTime("$year-09-01"))) {
$year = $year - 1;
} else {
$year = $year;
}
$response = $this->exportCongeByUser
->exportList(
$user,
$workflow,
$year,
$calendrier,
$archive
);
$handle = fopen('php://memory', 'r+');
rewind($handle);
$dispositionHeader = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
"$fileName.xls"
);
$response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');
$response->headers->set('Pragma', 'public');
$response->headers->set('Cache-Control', 'maxage=1');
$response->headers->set('Content-Disposition', $dispositionHeader);
return $response;
}
/**
* @param Request $request
* @param Utilisateur $user
* @return mixed
* @throws \Exception
*/
#[Route(path: '/api/conges-droits-annees-export/{id}/{year}/{calendrier}/{archive}', name: 'ctsweb_conges-annuel-annees-export', methods: ['GET'], requirements: ['id' => '\d+', 'year' => '\d+'])]
public function listByUser(
int $id,
int $year,
string $calendrier = "c",
int $archive = 0
) {
$archive = boolval($archive);
$workflow = 'accepted';
$entityManager = $this->doctrine->getManager();
$user = $entityManager
->getRepository(Utilisateur::class)
->findOneBy(["idUtilisateur" => $id]);
$utilisateurInformation = $entityManager
->getRepository(UtilisateurInformation::class)
->findOneBy(["idUtilisateur" => $id]);
$Idaffectation = $utilisateurInformation->getIdAffectation()->getIdAffectation();
$affectation = $entityManager
->getRepository(RefAffectation::class)
->findOneBy(["idAffectation" => $Idaffectation]);
if ($workflow && (in_array($workflow, ['waiting', 'accepted', 'signed']))) {
if ($workflow == 'accepted') {
$workflow = [Workflow::STATES[$workflow], Workflow::CO_SIGN];
} else {
$workflow = [Workflow::STATES[$workflow]];
}
} else {
$workflow = [Workflow::CO_ACCEPTED, Workflow::CO_REJECT, Workflow::CO_SIGN];
}
$fileName = "Synthese__" . (new \DateTime())->format("Y-m-d_H_i_s");
$response = $this->exportCongeByUser
->exportList(
$user,
$workflow,
$year,
$calendrier,
$archive
);
$handle = fopen('php://memory', 'r+');
rewind($handle);
$dispositionHeader = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
"$fileName.xls"
);
$response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');
$response->headers->set('Pragma', 'public');
$response->headers->set('Cache-Control', 'maxage=1');
$response->headers->set('Content-Disposition', $dispositionHeader);
return $response;
}
/**
* @return JsonResponse
* @throws \Exception
*/
#[Route(path: '/api/export/years', name: 'ctsweb_export-conges-list-year', methods: ['GET'])]
public function listYear()
{
$year = [];
$todayYear = (new \DateTime())->format("Y");
$begin = self::BEGIN_YEAR;
for ($begin; $begin <= $todayYear; $begin++) {
$year[] = ['year' => $begin];
}
return new JsonResponse(
$this->serialize($year),
JsonResponse::HTTP_OK,
[],
true
);
}
/**
* @param array $states
* @return mixed
*/
private function serialize($item)
{
$data = ["items" => $item];
$context = new SerializationContext();
$context->setSerializeNull(true);
return $this->serializer->serialize($data, 'json', $context);
}
public function anneeMax($dates)
{
$maxYear = null;
foreach ((array) $dates as $date) {
$year = intval(substr($date, 0, 4));
if ($maxYear === null || $year > $maxYear) {
$maxYear = $year;
}
}
return $maxYear;
}
}