<?php
namespace App\Controller\Articles;
use App\Entity\Articles\Article;
use App\Entity\Articles\Concurrence;
use App\Entity\Fournisseurs\Fournisseur;
use App\Form\Articles\ConcurrenceType;
use App\Form\Articles\SupprimerConcurrenceType;
use App\Library\Datatable\Util\Datatable;
use App\Security\Voter\EntityVoter;
use App\Service\Articles\ArticleService;
use App\Service\Utilisateur\ColonneTableauService;
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-article/nouveau/{id}/{type}", name="dtc_concurrence_article_ajouter")
*/
public function ajouterAction(Request $request, $id, $type = "", EntityManagerInterface $em, ArticleService $articleService, TranslatorInterface $translator, ValidatorInterface $validator)
{
$titre_modal = $translator->trans("Nouvelle concurrence");
$repo_objet = $em->getRepository(Article::class);
$get = $request->query->all();
$concurrence = new Concurrence();
$objet = $repo_objet->find($id);
if (is_object($objet)) {
$concurrence->setArticle($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()) {
if ($type == 'article') {
$repo_objet = $em->getRepository(Article::class);
$objet = $repo_objet->find($id);
$url = $this->generateUrl('dtc_article_modifier', ['id' => $objet->getId(), 'tab' => 'concurrence']);
}
$em->persist($concurrence);
$em->persist($objet);
$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"]],
200,
['Content-Type' => 'application/json']);
} 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(
'Articles/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(
'Articles/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('Articles/Concurrence/ajouter.html.twig', array('form' => $form->createView(),'errors'=>$errors,'id'=>$id,'type'=>$type));
}
/**
* @Route("/concurrence-article/modifier/{objet}/{id}/{type}", name="dtc_concurrence_article_modifier")
*/
public function modifierAction(Request $request, Concurrence $objet, $id, $type, EntityManagerInterface $em, ArticleService $articleService, TranslatorInterface $translator, ValidatorInterface $validator)
{
$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('Articles/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()) {
if ($type == 'article') {
$repo_objet = $em->getRepository(Article::class);
$article = $repo_objet->find($id);
if (is_object($article)) {
$concurrence->setArticle($article);
}
$url = $this->generateUrl('dtc_article_modifier', ['id' => $article->getId(), 'tab' => 'concurrence']);
}
$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"]],
200,
['Content-Type' => 'application/json']);
} 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(
'Articles/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(
'Articles/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-article/supprimer/{objet}/{id}/{type}", name="dtc_concurrence_article_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 ($type == 'article') {
$url = $this->generateUrl('dtc_article_modifier', ['id' => $id, 'tab' => 'concurrence']);
}
if (array_key_exists('table', $get) && $get["table"] != "") {
return new JsonResponse(['rendu' => '', 'valide' => '1', 'url' => $url, 'type' => 'recharger_datatable', 'id_datatable' => $get["table"]],
200,
['Content-Type' => 'application/json']);
} 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-article/charger-tableau/{id}/{type}", name="dtc_concurrence_charger_tableau")
*/
public function chargerTableauAction(Request $request, $id = "0", $type = "a")
{
$rendu = $this->renderView('Articles/Concurrence/charger_tableau.html.twig', ["id" => $id, "type" => $type]);
return new JsonResponse(['rendu' => $rendu]);
}
/**
* @Route("/concurrence-article", name="dtc_concurrence_article_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-center", "targets" => [3], "visible" => true];
$tableau_class_cellule[] = ["searchable" => false, "className" => "visible_export colonne_id text-center", "targets" => [4], "visible" => true];
$tableau_class_cellule[] = ["orderable" => false, "searchable" => false, "className" => "visible_export colonne_id text-center", "targets" => [5], "visible" => true];
/*
$tableau_class_cellule[]=array("searchable"=> false,"className"=>"visible_export colonne_id text-center","targets"=>array(0),"visible"=>true);
$tableau_class_cellule[]=array("searchable"=> false,"className"=>"visible_export colonne_id text-center","targets"=>array(1),"visible"=>true);
$tableau_class_cellule[]=array("searchable"=> false,"className"=>"visible_export colonne_id","targets"=>array(2),"visible"=>true);
$tableau_class_cellule[]=array("searchable"=> false,"className"=>"visible_export colonne_id ","targets"=>array(3),"visible"=>true);
$tableau_class_cellule[]=array("searchable"=> false,"className"=>"visible_export colonne_id text-right","targets"=>array(4),"visible"=>true);
$tableau_class_cellule[]=array("searchable"=> false,"className"=>"visible_export colonne_id text-center","targets"=>array(5),"visible"=>true);
$tableau_class_cellule[]=array("searchable"=> false,"className"=>"visible_export colonne_id text-center","targets"=>array(6),"visible"=>true);
$tableau_class_cellule[]=array("searchable"=> false,"className"=>"visible_export colonne_id text-center","targets"=>array(7),"visible"=>true);
$tableau_class_cellule[]=array("searchable"=> false,"className"=>"visible_export colonne_id text-center","targets"=>array(8),"visible"=>true);
$tableau_class_cellule[]=array("searchable"=> false,"className"=>"visible_export colonne_id text-right","targets"=>array(9),"visible"=>true);
$tableau_class_cellule[]=array("searchable"=> false,"className"=>"visible_export colonne_id text-center","targets"=>array(10),"visible"=>true);
$tableau_class_cellule[]=array("searchable"=> false,"className"=>"visible_export colonne_id text-right","targets"=>array(11),"visible"=>true);
$tableau_class_cellule[]=array("searchable"=> false,"className"=>"visible_export colonne_id text-center","targets"=>array(12),"visible"=>true);
$tableau_class_cellule[]=array("orderable"=> false,"searchable"=> false,"className"=>" colonne_id text-center","targets"=>array(13),"visible"=>true);
$tableau_class_cellule[]=array("orderable"=> false,"searchable"=> false,"className"=>" colonne_id text-center","targets"=>array(14),"visible"=>true);
$tableau_class_cellule[]=array("orderable"=> false,"searchable"=> false,"className"=>" colonne_id text-center","targets"=>array(15),"visible"=>true);
*/
$modal = $request->query->get('modal');
$articleId = $request->query->get('id');
$qte = $request->query->get('qte');
if ($type == "article") {
$repo_objet = $em->getRepository(Article::class);
}
if ($type == "fournisseur") {
$repo_objet = $em->getRepository(Fournisseur::class);
}
$objet = $repo_objet->find($id);
if ($articleId != '') {
$article = $repo_objet->find($articleId);
if ($modal == 1 && is_object($article)) {
$titre_modal = $translator->trans('Concurrence');
$this->datatableArticle($article, $type, $datatable, $translator);
$rendu = $this->renderView('Articles/Concurrence/lister.html.twig', ['tableauClassColonne' => $tableau_class_cellule, "id" => $articleId, "type" => $type, 'qte' => $qte]
);
//$rendu = $this->renderView('FO/Supprimer/supprimer.html.twig', array('form' => $form->createView(),'errors'=>$errors,'id'=>$id,'type'=>$type,'objet'=>$objet));
return new Response(json_encode(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]));
}
}
if (is_object($objet)) {
if ($type == "article") {
$this->datatableArticle($objet, $type, $datatable, $translator);
} elseif ($type == "fournisseur") {
$tableau_class_cellule = [];
$tableau_class_cellule[] = ["searchable" => false, "className" => "visible_export colonne_id", "targets" => [0], "visible" => true];
$tableau_class_cellule[] = ["searchable" => true, "className" => "visible_export colonne_id", "targets" => [1], "visible" => true];
$tableau_class_cellule[] = ["searchable" => true, "className" => "visible_export colonne_id", "targets" => [2], "visible" => true];
$tableau_class_cellule[] = ["searchable" => true, "className" => "visible_export colonne_id text-center", "targets" => [3], "visible" => true];
$tableau_class_cellule[] = ["searchable" => true, "className" => "visible_export colonne_id text-center", "targets" => [4], "visible" => true];
$tableau_class_cellule[] = ["searchable" => true, "className" => "visible_export colonne_id text-center", "targets" => [5], "visible" => true];
$tableau_class_cellule[] = ["searchable" => true, "className" => "visible_export colonne_id text-center", "targets" => [6], "visible" => true];
$tableau_class_cellule[] = ["searchable" => true, "className" => "visible_export colonne_id text-right", "targets" => [7], "visible" => true];
$tableau_class_cellule[] = ["searchable" => true, "className" => "visible_export colonne_id text-center", "targets" => [8], "visible" => true];
$tableau_class_cellule[] = ["searchable" => true, "className" => "visible_export colonne_id text-center", "targets" => [9], "visible" => true];
$tableau_class_cellule[] = ["orderable" => false, "className" => "colonne_id visible_export text-right", "targets" => [10], "visible" => true];
$this->datatableFournisseur($objet, $type, $datatable, $translator);
}
return $this->render('Articles/Concurrence/lister.html.twig', ['tableauClassColonne' => $tableau_class_cellule, "id" => $id, "type" => $type]);
} else {
$this->datatable($datatable, $translator);
return $this->render('Articles/Concurrence/lister.html.twig', ['tableauClassColonne' => $tableau_class_cellule, "id" => $id, "type" => $type]);
}
}
/**
* set datatable configs
*
* @return \App\Library\Datatable\Util\Datatable
*/
private function datatable(Datatable $datatable, TranslatorInterface $translator)
{
$datatable->setDatatableId('dta-concurrence')
->setEntity(Concurrence::class, "x")
->setFields(
[
$translator->trans("ID") => 'x.id',
//$translator->trans("Libelle") => 'x.libelle',
$translator->trans("Actions") => 'x.id',
"_identifier_" => 'x.id',
]
)
//->addJoin('x.statutCommande', 's', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
/*->setWhere(
$type_jointure.' = :objet',
array('objet' => $objet)
)
*/
->setRenderers(
[
2 => [
'view' => 'FO/DataTable/actions.html.twig',
'params' => [
//'edit_route' => 'dtc_garantie_modifier',
//'supprimer_route' => 'dtc_garantie_supprimer',
'entite' => 'garantie',
'objet' => Concurrence::class,
],
],
]
)
->setOrder("x.id", "desc")
->setSearch(true)
->setSearchFields([1, 4]);
return $datatable;
}
/**
* set datatable configs
*
* @return \App\Library\Datatable\Util\Datatable
*/
private function datatableFournisseur($objet, $type = "", Datatable $datatable, TranslatorInterface $translator)
{
$type_jointure = 'x.'.$type;
$datatable->setDatatableId('dta-concurrence-fournisseur')
->setEntity(Concurrence::class, "x")
->setFields(
[
//$translator->trans("ID") => 'x.id',
$translator->trans("Réf") => 'a.reference',
$translator->trans("Réf fourn") => 'x.referenceFournisseur',
$translator->trans("Libellé") => 'a.libelle',
$translator->trans("Qté mini cond") => 'x.quantite',
$translator->trans("Qté mini art") => 'a.seuilMiniCommandeFournisseur',
$translator->trans("Qté maxi art") => 'a.seuilMaxiCommandeFournisseur',
$translator->trans("Conditionnement") => 'x.conditionnement',
$translator->trans("Prix achat brut") => 'x.prixAchatBrut',
$translator->trans("Tx remise") => 'x.tauxRemise',
$translator->trans("Tx remise suppl.") => 'x.tauxRemiseSupplementaire',
$translator->trans("Prix achat net") => 'x.prixAchatNet',
//$translator->trans("Fournisseur") => 'f.libelle',
//$translator->trans("Devise") => "d.name",
$translator->trans("Défaut") => 'x.defaut',
$translator->trans("Actions") => 'x.id',
"_identifier_" => 'x.id',
]
)
->addJoin('x.article', 'a', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
->addJoin('x.fournisseur', 'f', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
->addJoin('f.devise', 'd', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
->setWhere(
'x.fournisseur = :objet',
['objet' => $objet]
)
->setRenderers(
[
/*
1 => array(
'view' => 'FO/DataTable/actions_modal.html.twig',
'params' => array(
'edit_route' => 'dtc_article_modifier',
'id' => '',
'type' => $type,
//'entite' => 'article',
//'objet' => "DTCArticlesBundle:Article",
'typeDocument' => 'article'
),
),
*
*/
2 => [
'view' => 'FO/DataTable/actions_modal.html.twig',
'params' => [
'edit_route' => 'dtc_condition_achat_modifier',
'id' => $objet->getId(),
'type' => $type,
'entite' => 'garantie',
'objet' => Concurrence::class,
'affichage' => 'item',
],
],
7 => [
'view' => 'FO/DataTable/prix.html.twig',
],
10 => [
'view' => 'FO/DataTable/prix.html.twig',
],
11 => [
'view' => 'FO/DataTable/booleen.html.twig',
],
12 => [
'view' => 'FO/DataTable/actions_modal.html.twig',
'params' => [
'edit_route' => 'dtc_condition_achat_modifier',
'supprimer_route' => 'dtc_condition_achat_supprimer',
'id' => $objet->getId(),
'type' => $type,
'table' => "dta-concurrence",
'entite' => 'garantie',
'objet' => Concurrence::class
,
'width_modal' => 800,
],
],
]
)
->setOrder("x.id", "desc")
->setSearch(true)
->setSearchFields([1, 2, 3, 6, 7, 8, 9, 10]);
return $datatable;
}
/**
* set datatable configs
*
* @return \App\Library\Datatable\Util\Datatable
*/
private function datatableArticle($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("Concurrent") => 'x.concurrent',
$translator->trans("Prix") => 'x.prix',
$translator->trans("Devise") => 'd.name',
$translator->trans("Équiper") => 'u.reference',
$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.article = :objet',
['objet' => $objet]
)
->setRenderers(
[
0 => [
'view' => 'FO/DataTable/date.html.twig',
],
2 => [
'view' => 'FO/DataTable/prix.html.twig',
'params' => [
'afficher_total' => false,
],
],
4 => [
'view' => 'FO/DataTable/avec_lien_edit_route.html.twig',
'params' => [
'edit_route' => 'dtc_utilisateur_modifier',
'typeDocument' => 'equipier',
],
],
5 => [
'view' => 'FO/DataTable/actions_modal.html.twig',
'params' => [
'edit_route' => 'dtc_concurrence_article_modifier',
'supprimer_route' => 'dtc_concurrence_article_supprimer',
'id' => $objet->getId(),
'type' => $type,
'table' => "dta-concurrence",
'entite' => 'garantie',
'objet' => Concurrence::class,
'width_modal' => 800,
],
],
]
)
->setOrder("x.id", "desc")
->setSearch(true)//->setSearchFields(array(1,2))
;
return $datatable;
}
/**
* @Route("/concurrence-article/grid/{id}/{type}", name="dtc_concurrence_article_liste_grid")
*/
public function gridAction($id = "", $type = "",EntityManagerInterface $em, Datatable $datatable, TranslatorInterface $translator)
{
if ($type == "article") {
$repo_objet = $em->getRepository(Article::class);
} elseif ($type == "fournisseur") {
$repo_objet = $em->getRepository(Fournisseur::class);
}
$objet = $repo_objet->find($id);
if (is_object($objet) and $type == "article") {
return $this->datatableArticle($objet, $type, $datatable, $translator)->execute();
} elseif (is_object($objet) and $type == "fournisseur") {
return $this->datatableFournisseur($objet, $type, $datatable, $translator)->execute();
} else {
return $this->datatable($datatable, $translator)->execute();
}
}
}