<?php
namespace App\Controller\App;
use App\Core\CoreBundle\Controller\AppControllerInterface;
use App\Core\CoreBundle\Service\Security;
use App\Data\Config;
use App\Entity\Commande;
use App\Entity\CommandeArticle;
use App\Entity\Univers;
use App\Entity\User;
use App\Manager\ArticleManager;
use App\Manager\CommandeArticleManager;
use App\Manager\CommandeManager;
use App\Manager\ConfigManager;
use App\Manager\FraisCommandeArticleManager;
use App\Manager\UniversManager;
use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
use Knp\Snappy\Pdf;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
/**
* @Route("/app", name="app_")
*/
class AppController extends AbstractController implements AppControllerInterface
{
private CommandeManager $commandeManager;
private CommandeArticleManager $commandeArticleManager;
private Security $security;
private UniversManager $universManager;
public function __construct(
CommandeManager $commandeManager,
CommandeArticleManager $commandeArticleManager,
Security $security,
UniversManager $universManager
)
{
$this->commandeManager = $commandeManager;
$this->security = $security;
$this->commandeArticleManager = $commandeArticleManager;
$this->universManager = $universManager;
}
/**
* @Route("/", name="homepage", options={"expose"=true}, methods={"GET"})
*/
public function index(Request $request)
{
/** @var Commande $commande */
$commande = null;
$showModal = false;
if ($request->get('eanscan')) {
$this->get('session')->set('eanscan', $request->get('eanscan'));
}
// si une commande est passé en parametre
if ($request->get('commande')) {
/** @var Commande $commande */
$commande = $this->commandeManager->find($request->get('commande'));
// on verifie que l'utilisateur courant puisse editer la commande
if (!$commande->isCommandeModifiable() or !in_array($commande->getMagasin()->getId(), $this->security->getUser()->getMagasinsId())) {
// on remove la commande courante en session
$this->get('session')->remove('commandeCourante');
$this->get('session')->remove('commandeModification');
throw new AccessDeniedException('Accès interdit');
}
// on set l'univers et la commande courante en session
$this->get('session')->set('univers', $commande->getUniver()->getId());
$this->get('session')->set('commandeCourante', $commande->getId());
$this->get('session')->set('commandeModification', $commande->getId());
} else {
// on remove la commande courante en session
$this->get('session')->remove('commandeCourante');
$this->get('session')->remove('commandeModification');
// si on a deja l'univers et le magasin
if (
$this->get('session')->get('univers') and
$this->get('session')->get('magasin')
) {
// on regarde si la commande courante existe déja
/** @var Commande $commande */
$commande = $this->commandeManager->findOneBy(
[
'univer' => $this->get('session')->get('univers'),
'magasin' => $this->get('session')->get('magasin'),
'statut' => Config::COMMANDE_BROUILLON,
]
);
// si elle existe, qu'elle n'est pas vide et que la modal de l'univers et du magasin n'a pas été affiché
if (
$commande and
sizeof($commande->getCommandeArticles()->toArray()) != 0 and
!$this->get('session')->get('modalCommande_' . $this->get('session')->get('univers') . '_' . $this->get('session')->get('magasin'))
) {
$showModal = true;
$this->get('session')->set('modalCommande_' . $this->get('session')->get('univers') . '_' . $this->get('session')->get('magasin'), true);
}
}
}
/** @var Univers $univers */
$univers = $this->universManager->find($this->get('session')->get('univers'));
$universSemaine = ($univers and $univers->getPrecommande()) ? $univers->getPrecommandeNombreSemaine() : null;
return $this->render(
'app/home.html.twig',
[
'showModal' => $showModal,
'commande' => $commande,
'univers' => $univers,
'universSemaine' => $universSemaine,
'universFrais' => ($univers) ? (int)$univers->getFrais() : null,
'titreColonneHistorique' => ($univers) ? $univers->getTitreColonneHistorique() : null,
]
);
}
/**
* @Route("/{commande}/panier", name="panier", options={"expose"=true}, methods={"GET"})
*/
public function panier($commande)
{
/** @var User $user */
$user = $this->security->getUser();
/** @var Commande $commande */
$commande = $this->commandeManager->findOneBy(['id' => $commande]);
// verification que la commande est bien associé à un magasin que gere l'utilisateur
if (
!$commande or
!in_array($commande->getMagasin()->getId(), $user->getMagasinsId())
) {
throw new AccessDeniedException('Accès interdit');
}
$this->get('session')->set('commandeCourante', $commande->getId());
$this->get('session')->set('commandeModifiable', $commande->isCommandeModifiable());
$this->get('session')->set('modalCommande_' . $commande->getUniver()->getId() . '_' . $commande->getMagasin()->getId(), true);
return $this->render(
'app/panier.html.twig',
[
'hideSelecteurUnivers' => true,
'hideSelecteurClient' => true,
'statut' => json_encode($commande->getStatutData()),
'commande' => $commande->getId(),
'commandeName' => $commande->getName(),
'magasin' => $commande->getMagasin()->getName(),
'univer' => $commande->getUniver()->__toString(),
'universSemaine' => $commande->getUniver()->getPrecommandeNombreSemaine(),
'universFrais' => (int)$commande->getUniver()->getFrais(),
'commandeDateLivraison' => ($commande->getDateLivraison()) ? $commande->getDateLivraison()->format('Y-m-d') : '',
'dateLivraisonEstimation' => $commande->getDateLivraisonEstimation(),
'titreColonneHistorique' => $commande->getUniver()->getTitreColonneHistorique()
]
);
}
/**
* @Route("/historique", name="historique", options={"expose"=true}, methods={"GET"})
*/
public function historique()
{
return $this->render(
'app/historique.html.twig',
[
'hideSelecteurUnivers' => true,
]
);
}
/**
* @Route("/express", name="express", options={"expose"=true}, methods={"GET"})
*/
public function express()
{
return $this->render(
'app/express.html.twig',
[
]
);
}
/**
* @Route("/{commande}/renouveler", name="renouveler", options={"expose"=true}, methods={"GET"})
*/
public function renouveler(Commande $commande)
{
$newCommande = $this->commandeManager->dupliquer($commande, Config::COMMANDE_RENOUVELEMENT);
$this->commandeManager->calculMontantCommande($newCommande);
return $this->redirectToRoute('app_panier', ['commande' => $newCommande->getId(),]);
}
/**
* @Route("/{commande}/reset", name="reset", options={"expose"=true}, methods={"GET"})
*/
public function reset($commande)
{
/** @var User $user */
$user = $this->security->getUser();
/** @var Commande $commande */
$commande = $this->commandeManager->findOneBy(['id' => $commande]);
// verification que la commande est bien associé à un magasin que gere l'utilisateur et que la commande n'est pas modifiable
if (!$commande or
!in_array($commande->getMagasin()->getId(), $user->getMagasinsId()) or
!$commande->isCommandeModifiable()
) {
throw new AccessDeniedException('Accès interdit');
}
/** @var CommandeArticle $commandeArticle */
foreach ($commande->getCommandeArticles() as $commandeArticle) {
$this->commandeArticleManager->removeAndFlush($commandeArticle);
}
$this->commandeManager->calculMontantCommande($commande);
return $this->redirectToRoute('app_homepage');
}
/**
* @Route("/{commande}/imprimer", name="imprimer", options={"expose"=true}, methods={"GET"})
*/
public function imprimer($commande, Pdf $pdf, ArticleManager $articleManager, FraisCommandeArticleManager $fraisCommandeArticleManager)
{
ini_set('max_execution_time', 0); // for infinite time of execution
ini_set('memory_limit', -1);
set_time_limit(0);
/** @var User $user */
$user = $this->security->getUser();
/** @var Commande $commande */
$commande = $this->commandeManager->findOneBy(['id' => $commande]);
$repartition = [];
if ($commande->getUniver()->getFrais()) {
$fraisCommandeArticle = $fraisCommandeArticleManager->findForCatalogue($commande->getUniver()->getId());
foreach ($commande->getCommandeArticles() as $commandeArticle) {
$repartition[$commandeArticle->getArticle()->getId()] = [
'commande' => $commandeArticle->getQuantiteCommande(),
'stock' => $commandeArticle->getQuantite(),
'arrivage' => $commandeArticle->getQuantiteTC(),
'reste' => ($commandeArticle->getQuantite() + $commandeArticle->getQuantiteTC()) - $commandeArticle->getQuantiteCommande(),
];
}
}
// verification que la commande est bien associé à un magasin que gere l'utilisateur
if (!$this->security->hasRoles(['ROLE_SUPER_ADMIN'])) {
if (!$commande or
!in_array($commande->getMagasin()->getId(), $user->getMagasinsId())
) {
throw new AccessDeniedException('Accès interdit');
}
}
$html = $this->renderView(
'app/commande_print.pdf.twig',
[
'commande' => $commande,
'repartition' => $repartition,
]
);
// return new Response($html);
return new PdfResponse(
$pdf->getOutputFromHtml($html),
$commande->getNumero() . '.pdf'
);
}
/**
* @Route("/documentation", name="documentation", options={"expose"=true}, methods={"GET"})
*/
public function documentation(ConfigManager $configManager)
{
return $this->render('app/documentation.html.twig', [
'config' => $configManager->find(1),
]);
}
/**
* @Route("/mobile", name="mobile", options={"expose"=true}, methods={"GET"})
*/
public function mobile(ConfigManager $configManager)
{
return $this->render('app/mobile.html.twig', [
'config' => $configManager->find(1),
]);
}
/**
* @Route("/scan", name="scan", options={"expose"=true}, methods={"GET"})
*/
public function scan()
{
return $this->render('app/scan.html.twig', [
]);
}
/**
* @Route("/calendrier", name="calendrier", options={"expose"=true}, methods={"GET"})
*/
public function calendrier(UniversManager $universManager)
{
return $this->render('app/calendrier.html.twig', [
'univers' => json_encode($universManager->getUniversforStatsFullCalendar())
]);
}
}