<?php
namespace App\Controller\GestionComerciale;
use App\Entity\GestionComerciale\Commande;
use App\Entity\GestionComerciale\Concurrence;
use App\Form\GestionComerciale\SupprimerConcurrenceType;
use App\Form\GestionComerciale\ConcurrenceType;
use App\Library\Datatable\Util\Datatable;
use App\Security\Voter\EntityVoter;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class ConcurrenceController extends AbstractController
{
/**
* @Route("/concurrence-commande/nouveau/{id}/{type}", name="dtc_concurrence_commande_ajouter")
*/
public function ajouterAction(Request $request, $id,$type="", EntityManagerInterface $em, TranslatorInterface $translator, ValidatorInterface $validator)
{
$url = "";
$titre_modal =$translator->trans("Nouvelle concurrence");
$repo_objet = $em->getRepository(Commande::class);
$get = $request->query->all();
$concurrence = new Concurrence();
$objet = $repo_objet->find($id);
if(is_object($objet)){
$concurrence->setCommande($objet);
//$concurrence->setLibelle($objet->getLibelle());
}
$user = $this->getUser();
//$concurrence->setUtilisateur($user);
$form = $this->createForm(ConcurrenceType::class, $concurrence);
$errors = "";
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$em->persist($concurrence);
$em->flush();
if (array_key_exists('table', $get) && $get["table"]!="") {
return new JsonResponse(['rendu' =>'', 'valide' =>'1', 'url' =>$url, 'type' =>'recharger_datatable', 'id_datatable' =>$get["table"]]);
}
else {
$this->addFlash('notice',$translator->trans('Concurrence ajoutée avec succès !'));
return new JsonResponse(['rendu' =>'', 'valide' =>'1', 'url' =>$url]);
}
}
else {
$errors = $validator->validate($concurrence);
$rendu = $this->renderView('GestionComerciale/Concurrence/ajouter.html.twig', ['form' => $form->createView(), 'errors' =>$errors, 'id' =>$id, 'type' =>$type, 'concurrence' =>$concurrence]
);
return new JsonResponse(['rendu' =>$rendu, 'valide' =>'0', 'url' =>'', 'titre' =>$titre_modal]);
}
}
$rendu = $this->renderView('GestionComerciale/Concurrence/ajouter.html.twig', ['form' => $form->createView(), 'errors' =>$errors, 'id' =>$id, 'type' =>$type, 'concurrence' =>$concurrence]
);
return new JsonResponse(['rendu' =>$rendu, 'valide' =>'0', 'url' =>'', 'titre' =>$titre_modal]);
//return $this->render('GestionComerciale/Concurrence/ajouter.html.twig', array('form' => $form->createView(),'errors'=>$errors,'id'=>$id,'type'=>$type));
}
/**
* @Route("/concurrence-commande/modifier/{objet}/{id}/{type}", name="dtc_concurrence_commande_modifier")
*/
public function modifierAction(Request $request, Concurrence $objet,$id,$type, EntityManagerInterface $em, TranslatorInterface $translator, ValidatorInterface $validator)
{
$url = "";
$concurrence = $objet;
$titre_modal =$translator->trans("Modifier la concurrence");
$get = $request->query->all();
$user = $this->getUser();
//$concurrence->setUtilisateur($user);
$form = $this->createForm(ConcurrenceType::class, $concurrence);
$errors = "";
$form->handleRequest($request);
if ($form->isSubmitted()) {
$droit = $this->isGranted(EntityVoter::UPDATE, Concurrence::class);
/*
if(!$droit) {
$rendu = $this->renderView('GestionComerciale/Concurrence/ajouter.html.twig', array('form' => $form->createView(),'errors'=>$errors,'id'=>$id,'type'=>$type,'objet'=>$objet));
return new Response(json_encode(array('rendu'=>$rendu,'valide'=>'0','url'=>'','titre'=>$titre_modal)), 200, array('Content-Type'=>'application/json'));
}
*/
if ($form->isValid()) {
$em->persist($concurrence);
$em->flush();
if (array_key_exists('table', $get) && $get["table"]!="") {
return new JsonResponse(['rendu' =>'', 'valide' =>'1', 'url' =>$url, 'type' =>'recharger_datatable', 'id_datatable' =>$get["table"]]);
}
else {
$this->addFlash('notice',$translator->trans('Concurrence modifiée avec succès !'));
return new JsonResponse(['rendu' =>'', 'valide' =>'1', 'url' =>$url]);
}
}
else {
$errors = $validator->validate($concurrence);
$rendu = $this->renderView('GestionComerciale/Concurrence/ajouter.html.twig', ['form' => $form->createView(), 'errors' =>$errors, 'id' =>$id, 'type' =>$type, 'objet' =>$objet, 'concurrence' =>$concurrence]
);
return new JsonResponse(['rendu' =>$rendu, 'valide' =>'0', 'url' =>'', 'titre' =>$titre_modal]);
}
}
$rendu = $this->renderView('GestionComerciale/Concurrence/ajouter.html.twig', ['form' => $form->createView(), 'errors' =>$errors, 'id' =>$id, 'type' =>$type, 'objet' =>$objet, 'concurrence' =>$concurrence]
);
return new JsonResponse(['rendu' =>$rendu, 'valide' =>'0', 'url' =>'', 'titre' =>$titre_modal]);
}
/**
* @Route("/concurrence-commande/supprimer/{objet}/{id}/{type}", name="dtc_concurrence_commande_supprimer")
*/
public function supprimerAction(Request $request, Concurrence $objet,$id,$type, EntityManagerInterface $em, TranslatorInterface $translator, ValidatorInterface $validator) {
$concurrence = $objet;
$titre_modal =$translator->trans("Demande de confirmation");
$get = $request->query->all();
$user = $this->getUser();
$form = $this->createForm(SupprimerConcurrenceType::class, $concurrence);
$errors = "";
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$em->remove($concurrence);
$em->flush();
$url = "";
if (array_key_exists('table', $get) && $get["table"]!="") {
return new JsonResponse(['rendu' =>'', 'valide' =>'1', 'url' =>$url, 'type' =>'recharger_datatable', 'id_datatable' =>$get["table"]]);
}
else {
$this->addFlash('notice',$translator->trans('Concurrence supprimée avec succès !'));
return new JsonResponse(['rendu' =>'', 'valide' =>'1', 'url' =>$url]);
}
}
else {
$errors = $validator->validate($concurrence);
$rendu = $this->renderView('FO/Supprimer/supprimer.html.twig', ['form' => $form->createView(), 'errors' =>$errors, 'id' =>$id, 'type' =>$type, 'objet' =>$objet]);
return new JsonResponse(['rendu' =>$rendu, 'valide' =>'0', 'url' =>'', 'titre' =>$titre_modal]);
}
}
$rendu = $this->renderView('FO/Supprimer/supprimer.html.twig', ['form' => $form->createView(), 'errors' =>$errors, 'id' =>$id, 'type' =>$type, 'objet' =>$objet]);
return new JsonResponse(['rendu' =>$rendu, 'valide' =>'0', 'url' =>'', 'titre' =>$titre_modal]);
}
/**
* @Route("/concurrence-commande/charger-tableau/{id}/{type}", name="dtc_concurrence_commande_charger_tableau")
*/
public function chargerTableauAction(Request $request, $id="0",$type="a") {
$rendu = $this->renderView('GestionComerciale/Concurrence/charger_tableau.html.twig', ["id" =>$id, "type" =>$type]);
return new JsonResponse(['rendu' =>$rendu], 200, ['Content-Type' =>'application/json']);
}
/**
* @Route("/concurrence-commande", name="dtc_concurrence_commande_liste")
*/
public function listerAction(Request $request, $id="0",$type="a", EntityManagerInterface $em, Datatable $datatable, TranslatorInterface $translator)
{
$tableau_class_cellule = [];
$tableau_class_cellule[]= ["searchable" => false, "className" =>"visible_export colonne_id text-center", "targets" => [0], "visible" =>true];
$tableau_class_cellule[]= ["searchable" => false, "className" =>"visible_export colonne_id ", "targets" => [1], "visible" =>true];
$tableau_class_cellule[]= ["searchable" => false, "className" =>"visible_export colonne_id text-right", "targets" => [2], "visible" =>true];
$tableau_class_cellule[]= ["searchable" => false, "className" =>"visible_export colonne_id text-right", "targets" => [3], "visible" =>true];
$tableau_class_cellule[]= ["orderable" =>false, "searchable" => false, "className" =>"visible_export colonne_id", "targets" => [4], "visible" =>true];
$modal = $request->query->get('modal');
$qte = $request->query->get('qte');
if($type == "commande") $repo_objet = $em->getRepository(Commande::class);
$objet = $repo_objet->find($id);
if(is_object($objet)) {
if($type == "commande") {
$this->datatableCommande($objet,$type, $datatable, $translator);
}
return $this->render('GestionComerciale/Concurrence/lister.html.twig', ['tableauClassColonne' =>$tableau_class_cellule, "id" =>$id, "type" =>$type]);
}
}
/**
* set datatable configs
*
* @return \App\Library\Datatable\Util\Datatable
*/
private function datatableCommande($objet,$type="", Datatable $datatable, TranslatorInterface $translator)
{
$type_jointure = 'x.'.$type;
$datatable ->setDatatableId('dta-concurrence')
->setEntity(Concurrence::class, "x")
->setFields(
[
//$translator->trans("ID") => 'x.id',
$translator->trans("Date") => 'x.date',
$translator->trans("Date offre") => 'x.dateOffre',
$translator->trans("Concurrent") => 'x.libelle',
$translator->trans("Montant") => 'x.montant',
$translator->trans("Actions") => 'x.id',
"_identifier_" => 'x.id'
]
)
//->addJoin('x.devise', 'd', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
//->addJoin('x.utilisateur', 'u', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
// ->addJoin('f.devise', 'd', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
// ->addJoin('x.uniteMesure', 'u', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
/*->setWhere(
$type_jointure.' = :objet',
array('objet' => $objet)
)
*/
->setWhere(
'x.commande = :objet',
['objet' => $objet]
)
->setRenderers(
[
0 => [
'view' => 'FO/DataTable/date.html.twig',
],
1 => [
'view' => 'FO/DataTable/date.html.twig',
],
2 => [
'view' => 'FO/DataTable/prix.html.twig',
'params' => [
'afficher_total'=>false,
],
],
3 => [
'view' => 'FO/DataTable/prix.html.twig',
'params' => [
]
],
4=> [
'view' => 'FO/DataTable/actions_modal.html.twig',
'params' => [
'edit_route' => 'dtc_concurrence_commande_modifier',
'supprimer_route' => 'dtc_concurrence_commande_supprimer',
'id' => $objet->getId(),
'type' => $type,
'table' => "dta-concurrence",
'entite' => '',
'objet' => Concurrence::class,
'width_modal'=>800
],
],
]
)
->setOrder("x.id", "desc")
->setSearch(true)
//->setSearchFields(array(1,2))
;
return $datatable;
}
/**
* @Route("/concurrence-commande/grid/{id}/{type}", name="dtc_concurrence_commande_liste_grid")
*/
public function gridAction(Request $request, $id="",$type="", EntityManagerInterface $em, Datatable $datatable, TranslatorInterface $translator)
{
if($type == "commande") $repo_objet = $em->getRepository(Commande::class);
$objet = $repo_objet->find($id);
if(is_object($objet) and $type == "commande") return $this->datatableCommande($objet,$type, $datatable, $translator)->execute();
}
}