<?php
namespace App\Controller;
use App\Entity\DotationType;
use App\Services\ArticleStockService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Entity\Article;
use App\Entity\Reseau;
use App\Entity\User;
use App\Entity\Dotation;
use App\Entity\Commande;
use App\Form\CommandeClassementType;
use App\Form\DotationListType;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends AbstractController
{
/**
* @Route("/", name="app_homepage")
*/
public function indexAction(
Request $request,
EntityManagerInterface $em,
ArticleStockService $articleStockService
)
{
if ($this->getUser() != null) {
// Affichage du tableau de bord
if ($this->getUser()->getProfil()->getAccesBo()) {
$date = date("Y-m-d");
$dateMonthAfter = date('Y-m-d', strtotime('+6 month', strtotime($date)));
//// récupère les articles en fin de visa (6 mois)
$articles = $em->getRepository(Article::class)->getArticleFinVisa($date, $dateMonthAfter);
//// Date il y a 6 mois
$dateSixMonths = date('Y-m-d', strtotime('-6 month', strtotime($date)));
$classementArticles = $em->getRepository(Article::class)->getArticlesThird($date, $dateSixMonths);
$cptReseau = 0;
$tabClassement = [];
$nbArticleClassement = 3;
$reseau = "";
foreach ($classementArticles as $lignes) {
if ($reseau != $lignes['Reseau']) {
$cptReseau = 0;
$reseau = $lignes['Reseau'];
}
if ($reseau == $lignes['Reseau']) {
if ($cptReseau < $nbArticleClassement) {
$tabClassement[$lignes['Reseau']][] = $lignes;
$cptReseau++;
}
}
}
$commandeValidationAdmin = $em->getRepository(Commande::class)->getCommandeAttenteValidationAdmin();
$commandeValidationResponsable = $em->getRepository(Commande::class)
->getCommandeAttenteValidationResponsable();
$nbCommandeValAdmin = count($commandeValidationAdmin);
$nbCommandeValResponsable = count($commandeValidationResponsable);
//// SELECT DR / Fichier Js (commande_classement.js = ajax) / CommandeClassementType.php (form)
$form = $this->createForm(CommandeClassementType::class);
$formdotation = $this->createForm(DotationListType::class);
// Statistiques articles alertes
$articlesAlerteStockTemps = $em->getRepository(Article::class)->getArticleAlerteStockTDB();
$artAlerte = [];
$tabNbVM = $articleStockService->getNbVMParReseau();
foreach ($articlesAlerteStockTemps as $art) {
$infoStock = $articleStockService->getInfoStockArticle($art);
$nbVM = $articleStockService->getNBVMArticle($art, $tabNbVM);
$qteParVm = 0;
if ($nbVM > 0 && $art->getConditionnement() > 0) {
$qteParVm = floor(($infoStock["total"] / $nbVM) / $art->getConditionnement()) * $art->getConditionnement();
}
if ($qteParVm < $art->getQteMin()) {
$art->datas = [
"disponible" => $infoStock["disponible"],
"reserve" => $infoStock["reserve"],
"reserveCommande" => $infoStock["reserveCommande"],
"reassort" => $infoStock["reassort"],
"total" => $infoStock["total"],
"nbVM" => $nbVM,
"quantiteParVM" => $qteParVm,
];
$artAlerte[] = $art;
}
}
$nowTime = time();
$date5Months = date('Y-m-d', strtotime('-5 month', $nowTime));
$dateDebut = date('Y-m-d', strtotime('first day of this month', strtotime($date5Months)));
$dateFin = date('Y-m-d', strtotime('last day of this month', $nowTime));
//$dateDebut = "2018-01-01";
//$dateFin = "2018-07-31";
//$dateDebut = "2018-12-01";
//$dateFin = "2019-05-01";
// echo $dateDebut." * ".$dateFin;
//exit;
$dateCptTimeStamp = strtotime($dateDebut);
$dateFinTimeStamp = strtotime($dateFin);
$moisStatsArticles = array();
while ($dateCptTimeStamp < $dateFinTimeStamp) {
$dateCptStr = date('Y-m-d', $dateCptTimeStamp);
$moisStatsArticles[] = date_format(date_create_from_format("Y-m-d", $dateCptStr), "m/Y");
$dateCptTimeStamp = strtotime('+1 month', $dateCptTimeStamp);
}
$reseauxStatsArticles = $em->getRepository(Reseau::class)->getAllForStatsOrder();
$top3articlesByReseauAndByMois = $em->getRepository(Article::class)
->getArticlesClassementByReseauAndByMois($dateDebut, $dateFin);
//Renvoie un tableau de la forme : $tab["NOM_RESEAU"]["MM/YYYY"]["CODE_THEA"] = {"CodeThea"=>"XX", "libelle"=>"XXX", "qte"=>X}
} else {
$repoDotation = $em->getRepository(Dotation::class);
$dotations = $repoDotation->getDotationOuverteForUser($this->getUser());
if (count($dotations) == 1) {
$request->getSession()->set('dotation', $dotations[0]);
}
return $this->redirect($this->generateUrl('frontoffice'));
}
return $this->render('Default/index.html.twig',
array(
'articles' => $articles,
'classementArticles' => $tabClassement,
'nbCommandeValAdmin' => $nbCommandeValAdmin,
'nbCommandeValResponsable' => $nbCommandeValResponsable,
'form' => $form->createView(),
'formdotation' => $formdotation->createView(),
'articlesAlerteStock' => $artAlerte,
'top3articlesByReseauAndByMois' => $top3articlesByReseauAndByMois,
'reseauxStatsArticles' => $reseauxStatsArticles,
'moisStatsArticles' => $moisStatsArticles,
)
);
} else {
return $this->redirect($this->generateUrl('app_login'));
}
}
/**
* Fonction qui retourne la liste des commandes en fonction du DR
* Ne prends pas en compte le DR dans l'affichage du classement
*
* @Route("/getCommandeClassement/", name="getCommandeClassement", methods={"POST"}, options={"expose"= "true"})
*
* @param Request $request [description]
* @return [type] [description]
*/
public function getCommandeClassement(
Request $request,
EntityManagerInterface $entityManager
)
{
if (!$request->isXmlHttpRequest()) {
return new Response("Not Found", 404);
}
$em = $entityManager;
$em->getFilters()->disable('softdeleteable');
/// Récupère l'id du DR select
$userId = $request->request->get('user_id');
$date = date("Y-m-d");
$dateCommandes = date('Y-m-d', strtotime('-6 month', strtotime($date)));
$ligneClassements = $em->getRepository(Commande::class)->getCommmandeClassement($userId, $date, $dateCommandes);
$tabClassement = [];
$reseau = null;
$user = null;
foreach ($ligneClassements as $ligne) {
if ($reseau != $ligne['Reseau']) {
$reseau = $ligne['Reseau'];
$tabClassement[$reseau] = [];
}
if ($user !== $ligne['idUser']) {
$user = $ligne['idUser'];
$tabClassement[$reseau][$user]["commandeTotale"] = 0;
$tabClassement[$reseau][$user]["nbCommandeSieCgs"] = 0;
$tabClassement[$reseau][$user]["nbCommandeSysRea"] = 0;
$tabClassement[$reseau][$user]["michel"] = 0;
$tabClassement[$reseau][$user]["name"] = $ligne['name'];
$tabClassement[$reseau][$user]["firstname"] = $ligne['firstname'];
$tabClassement[$reseau][$user]["couleur"] = $ligne['couleur'];
$tabClassement[$reseau][$user]["reseau"] = $ligne['Reseau'];
}
$tabClassement[$reseau][$user]['commandeTotale']++;
if ($ligne['DotationType'] == DotationType::SIEGE) {
$tabClassement[$reseau][$user]['nbCommandeSieCgs']++;
} else if ($ligne['DotationType'] == DotationType::CONGRES) {
$tabClassement[$reseau][$user]['nbCommandeSieCgs']++;
} else if ($ligne['DotationType'] == DotationType::REAPPRO) {
$tabClassement[$reseau][$user]['nbCommandeSysRea']++;
} else if ($ligne['DotationType'] == DotationType::SYSTEMATIQUE) {
$tabClassement[$reseau][$user]['nbCommandeSysRea']++;
}
}
foreach ($ligneClassements as $ligne) {
usort($tabClassement[$ligne['Reseau']], [$this, 'triTableau']);
}
return new JsonResponse($tabClassement);
}
public function triTableau(
$a,
$b
)
{
if ($a["commandeTotale"] == $b["commandeTotale"]) {
return 0;
}
return ($a["commandeTotale"] > $b["commandeTotale"]) ? -1 : 1;
}
/**
* Fonction qui retourn le nombre de délégué ou nombre de commande selon type
*
* @Route("/getInfoDotByType", name="getInfoDotByType", methods={"POST"}, options={"expose"= "true"})
*
* @param Request $request [description]
* @return [type] [description]
*/
public function getInfoDotByType(
Request $request,
EntityManagerInterface $entityManager
)
{
if (!$request->isXmlHttpRequest()) {
return new Response("Not Found", 404);
} else {
$em = $entityManager;
$em->getFilters()->disable('softdeleteable');
$id = $request->request->get('dotation_id');
$dotation = $em->getRepository(Dotation::class)->getInfoDotation($id);
$type = $dotation->getType()->getCode();
$totalCommande = null;
$tab = null;
if ($type == "REA") {
$users = $em->getRepository(User::class)->getNBCommandeByReseau($dotation);
foreach ($dotation->getReseaux() as $reseau) {
$tab[$reseau->getId()] = [];
$tab[$reseau->getId()]['reseaux'] = $reseau;
$tab[$reseau->getId()]['utilisateurs'] = [];
foreach ($users as $user) {
if ($user->getReseau() == $reseau) {
$tab[$user->getReseau()->getId()]['utilisateurs'][] = $user;
}
}
$tab[$reseau->getId()]['nbVM'] = count($tab[$reseau->getId()]['utilisateurs']);
}
} elseif ($type == "CGS" || $type == "SIE") {
$commandes = $em->getRepository(Commande::class)->getCommandesByTypeDotation($id);
$totalCommande = count($commandes);
}
}
$response = new JsonResponse(array("Dotation" => $dotation, "Total" => $totalCommande, "NombreDelegues" => $tab));
return $response;
}
/**
* @Route("/conditions", name="condition_page")
*/
public function getConditionPageAction()
{
return $this->render('Default/condition.html.twig');
}
}