<?php
namespace App\Controller\GestionComerciale;
use App\Entity\GestionComerciale\Commande;
use App\Entity\Rangements\Caisse;
use App\Entity\GestionComerciale\StatutCommande;
use App\Form\GestionComerciale\CommandeAnomalieType;
use App\Form\GestionComerciale\CaisseAnomalieType;
use App\Library\Datatable\Util\Datatable;
use App\Service\GestionComerciale\CommandeService;
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 AnomaliesController extends AbstractController
{
/**
* @Route("/vente/commande-anomalies", name="dtc_commande_anomalies_liste")
*/
public function listerAction(Request $request, $fabricationParente = '', $fabricationMultiple = '', $prestation = '', $commandeFournisseur = '', Datatable $datatable,
TranslatorInterface $translator
) {
$this->datatable($datatable, $translator);
$tableauClassColonne = [];
$tableauClassColonne = [];
$tableauClassColonne[] = ["className" => "colonne_id", "targets" => [0], "visible" => false, "orderable" => false];
$tableauClassColonne[] = ["className" => "colonne_id text-center", "targets" => [1], "visible" => true, "orderable" => false];
$tableauClassColonne[] = ["className" => "colonne_id text-center", "targets" => [2], "visible" => true, "orderable" => true];
$tableauClassColonne[] = ["className" => "colonne_id", "targets" => [3], "visible" => true, "orderable" => true];
$tableauClassColonne[] = ["className" => "colonne_id", "targets" => [4], "visible" => true, "orderable" => true];
$tableauClassColonne[] = ["className" => "colonne_id", "targets" => [5], "visible" => true, "orderable" => true];
$tableauClassColonne[] = ["className" => "colonne_id text-center", "targets" => [6], "visible" => true, "orderable" => true];
$tableauClassColonne[] = ["className" => "colonne_id text-center", "targets" => [7], "visible" => true, "orderable" => true];
$tableauClassColonne[] = ["className" => "colonne_id text-center", "targets" => [8], "visible" => true, "orderable" => true];
$tableauClassColonne[] = ["className" => "colonne_id text-left", "targets" => [9], "visible" => true, "orderable" => true];
$tableauClassColonne[] = ["className" => "colonne_id", "targets" => [10], "visible" => true, "orderable" => false];
$tableauClassColonne = [];
$template = 'GestionComerciale/Anomalies/lister.html.twig';
return $this->render($template, [
'tableauClassColonne' => $tableauClassColonne,
]);
}
/**
* @Route("/vente/commande-anomalies/grid", name="dtc_commande_anomalies_liste_grid")
*/
public function gridAction(Request $request, Datatable $datatable, TranslatorInterface $translator)
{
return $this->datatable($datatable, $translator)->execute();
}
/**
* set datatable configs
*
* @return \App\Library\Datatable\Util\Datatable
*/
private function datatable(Datatable $datatable, TranslatorInterface $translator)
{
$parameters = [];
$datatable->setEntity(Caisse::class, "x")
->setFields(
[
$translator->trans("ID") => 'x.id',
$translator->trans("Libelle") => 'x.libelle',
$translator->trans("Code barre") => 'x.ean',
$translator->trans("Anomalie") => 'ra.libelle',
$translator->trans("Client") => 'c.nom',
$translator->trans("Commentaire") => 'x.commentaireAnomalie',
$translator->trans("Actions") => 'x.id',
"_identifier_" => 'x.id',
]
)
->addJoin('x.raisonAnomalie', 'ra', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
->addJoin('x.client', 'c', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
->setRenderers(
[
6 => [
'view' => 'FO/DataTable/actions.html.twig',
'params' => [
'__edit_route' => 'dtc_document_commercial_modifier',
//'supprimer_route' => 'dtc_commande_supprimer',
'objet' => Caisse::class,
'deplier_caisse' => true,
'objet_deplier' => "deplier-tr-caisse-simplifie",
'page_affiche' => "anomalies",
],
],
]
)
->setSearch(true)
//->setSearchFields(array(3,4,5,7,8,9,10,11))
->setSearchFields([])
//->setOrder('x.date', 'ASC');
;
$where = 'x.raisonAnomalie is not null';
if ($where != '') {
$datatable->setWhere($where, $parameters);
}
return $datatable;
}
/**
* set datatable configs
*
* @return \App\Library\Datatable\Util\Datatable
*/
private function __datatable(Datatable $datatable, TranslatorInterface $translator)
{
$parameters = [];
$datatable->setEntity(Commande::class, "x")
->setFields(
[
$translator->trans("ID") => 'x.id',
$translator->trans("Date") => 'x.date',
$translator->trans("Réf") => 'x.reference',
$translator->trans("Réf marktepplace") => 'x.referenceMarketPlace',
$translator->trans("Origine") => 'cmp.libelle',
$translator->trans("Statut") => 's.libelle',
$translator->trans("Transporteur") => 't.libelle',
$translator->trans("Anomalie") => 'ra.libelle',
$translator->trans("Actions") => 'x.id',
"_identifier_" => 'x.id',
]
)
->addJoin('x.compteMarketPlace', 'cmp', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
->addJoin('x.statutCommande', 's', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
->addJoin('x.transporteur', 't', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
->addJoin('x.raisonAnomalie', 'ra', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
->setRenderers(
[
1 => [
'view' => 'FO/DataTable/heure.html.twig',
],
2 => [
'view' => 'FO/DataTable/avec_lien_edit_route.html.twig',
'params' => [
//'edit_route' => 'dtc_bon_preparation_modifier'
'edit_route' => 'auto',
],
],
3 => [
'view' => 'FO/DataTable/avec_lien_edit_route.html.twig',
'params' => [
//'edit_route' => 'dtc_bon_preparation_modifier'
'edit_route' => 'auto',
],
],
6 => [
'view' => 'FO/DataTable/modifier_transporteur.html.twig',
'params' => [
'edit_route' => '',
],
],
8 => [
'view' => 'FO/DataTable/actions.html.twig',
'params' => [
'__edit_route' => 'dtc_document_commercial_modifier',
//'supprimer_route' => 'dtc_commande_supprimer',
'objet' => Commande::class,
'deplier' => true,
'mode_deplier' => "simplifie",
'page_affiche' => "anomalies",
],
],
/*
0 => array(
'view' => 'FO/DataTable/actions-interface-simplifie.html.twig',
'params' => array(
'afficher_icone_reception' => false,
'edit_route' => 'dtc_fabrication_simplifie_reception',
//'supprimer_route' => 'dtc_fabrication_supprimer',
'objet' => 'DTCGestionComercialeBundle:Fabrication',
'fabrication' => true,
'commandeFournisseur' => $commandeFournisseur,
'fabricationMultiple' => $fabricationMultiple
),
),
*/
]
)
->setSearch(true)
//->setSearchFields(array(3,4,5,7,8,9,10,11))
->setSearchFields([])
->setOrder('x.date', 'ASC');
$parameters["typeDocumentCommercial"] = 4;
$parameters["statutEmballe"] = 31;
$parameters["statutCommande"] = [32, 33];
$where = '(x.typeDocumentCommercial = :typeDocumentCommercial and x.statutCommande IN (:statutCommande))';
$where .= ' OR (x.transporteur is null and x.statutCommande = :statutCommandeAccepte and x.compteMarketPlace is not null)';
$where .= ' OR (x.statutCommande != :statutEmballe and x.raisonAnomalie is not null and x.typeDocumentCommercial = :typeDocumentCommercial )';
$parameters["statutCommandeAccepte"] = 9;
if ($where != '') {
$datatable->setWhere($where, $parameters);
}
return $datatable;
}
/**
* @Route("/vente/commande-anomalies/{id}", name="dtc_commande_anomalies_ajouter")
*/
public function ajouterAction(Request $request, Caisse $caisse, EntityManagerInterface $em, TranslatorInterface $translator, ValidatorInterface $validator, CommandeService $commandeService)
{
$titre_modal = $translator->trans("Indiquer une anomalie sur la boite");
$get = $request->query->all();
if ( ! empty($get["type"]) && $get["type"] == "etiquette") {
$titre_modal = $translator->trans("Indiquer une anomalie d'étiquette");
}
$form = $this->createForm(CaisseAnomalieType::class, $caisse);
$errors = "";
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
//$this->container->get('dtc.commande')->creerHistorique($commande, 'dtc_gestioncomercialebundle_commande_transporteur');
//$rendu = $this->renderView('layout-errors.html.twig');
$rendu = '';
$url = '';
$this->addFlash('notice', $translator->trans('Anomalie ajoutée avec succès !'));
if ( ! empty($get["type"]) && $get["type"] == "emballage") {
//$statutCommande = $em->getRepository(StatutCommande::class)->find(33);
//$commande->setStatutCommande($statutCommande);
//$commandeService->creerHistorique($commande, 31, ["message" => " : ".$commande->getRaisonAnomalie()->getLibelle()]);
} elseif ( ! empty($get["type"]) && $get["type"] == "etiquette") {
// $commandeService->creerHistorique($commande, 32, ["message" => " : ".$commande->getRaisonAnomalie()->getLibelle()]);
}
$em->persist($caisse);
$em->flush();
$url = "";
if ( ! empty($get["type"]) && $get["type"] == "emballage" && ! empty($get["lp"]) && $get["lp"] != "") {
$url = $this->generateUrl('dtc_liste_preparation_simplifie_scan_produit_action', ["id" => $get["lp"]]);
} elseif ( ! empty($get["type"]) && $get["type"] == "etiquette" && ! empty($get["lp"]) && $get["lp"] != "") {
$url = $this->generateUrl('dtc_liste_preparation_simplifie_scan_produit_action', ["id" => $get["lp"]]);
} else {
$url = $this->generateUrl('dtcfo_homepage', ["ms" => "1"]);
}
return new JsonResponse(['rendu' => '', 'valide' => '1', 'url' => $url], 200, ['Content-Type' => 'application/json']);
} else {
$errors = $validator->validate($caisse);
$rendu = $this->renderView('GestionComerciale/Anomalies/ajouter.html.twig', ['form' => $form->createView(), 'errors' => $errors]);
return new JsonResponse(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]);
}
}
$rendu = $this->renderView('GestionComerciale/Anomalies/ajouter.html.twig', ['form' => $form->createView(), 'errors' => $errors, 'caisse' => $caisse]);
return new JsonResponse(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]);
}
/**
* @Route("/old/vente/commande-anomaliesccccc/{id}", name="dtc_commande_anomalies_ajouter_old")
*/
public function ___ajouterAction(Request $request, Commande $commande, EntityManagerInterface $em, TranslatorInterface $translator, ValidatorInterface $validator, CommandeService $commandeService)
{
$titre_modal = $translator->trans("Indiquer une anomalie sur la commande");
$form = $this->createForm(CommandeAnomalieType::class, $commande);
$errors = "";
$form->handleRequest($request);
if ( ! $form->isSubmitted()) {
if(is_object($commande->getCommande()) && count($commande->getCommande()->getArticleCommande())>0) {
foreach($commande->getCommande()->getArticleCommande() as $acBloquer) {
if($acBloquer->isBloquer()) {
$this->addFlash('warning', $translator->trans('L\'élement '.$acBloquer->getArticle()->getReference().' est bloqué !'));
}
}
}
}
if ( ! $form->isSubmitted() && is_object($commande->getCommande()) && $commande->getCommande()->getAbloquer()) {
$this->addFlash('warning', $translator->trans('La commande est bloquée !'));
}
if ($form->isSubmitted()) {
if ($form->isValid()) {
//$this->container->get('dtc.commande')->creerHistorique($commande, 'dtc_gestioncomercialebundle_commande_transporteur');
//$rendu = $this->renderView('layout-errors.html.twig');
$rendu = '';
$url = '';
$get = $request->query->all();
$this->addFlash('notice', $translator->trans('Anomalie ajoutée avec succès !'));
if ( ! empty($get["type"]) && $get["type"] == "emballage") {
$statutCommande = $em->getRepository(StatutCommande::class)->find(33);
$commande->setStatutCommande($statutCommande);
$commandeService->creerHistorique($commande, 31, ["message" => " : ".$commande->getRaisonAnomalie()->getLibelle()]);
} elseif ( ! empty($get["type"]) && $get["type"] == "etiquette") {
$commandeService->creerHistorique($commande, 32, ["message" => " : ".$commande->getRaisonAnomalie()->getLibelle()]);
}
$em->persist($commande);
$em->flush();
$url = "";
if ( ! empty($get["type"]) && $get["type"] == "emballage" && ! empty($get["lp"]) && $get["lp"] != "") {
$url = $this->generateUrl('dtc_liste_preparation_simplifie_scan_produit_action', ["id" => $get["lp"]]);
} elseif ( ! empty($get["type"]) && $get["type"] == "etiquette" && ! empty($get["lp"]) && $get["lp"] != "") {
$url = $this->generateUrl('dtc_liste_preparation_simplifie_scan_produit_action', ["id" => $get["lp"]]);
} else {
$url = $this->generateUrl('dtcfo_homepage', ["ms" => "1"]);
}
return new JsonResponse(['rendu' => '', 'valide' => '1', 'url' => $url], 200, ['Content-Type' => 'application/json']);
} else {
$errors = $validator->validate($commande);
$rendu = $this->renderView('GestionComerciale/Anomalies/ajouter.html.twig', ['form' => $form->createView(), 'errors' => $errors]);
return new JsonResponse(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]);
}
}
$rendu = $this->renderView('GestionComerciale/Anomalies/ajouter.html.twig', ['form' => $form->createView(), 'errors' => $errors, 'commande' => $commande]);
return new JsonResponse(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]);
}
}