<?php
namespace App\Controller\GestionComerciale;
use App\Entity\GestionComerciale\Fabrication;
use App\Entity\GestionComerciale\StatutCommande;
use App\Entity\GestionComerciale\StatutFabrication;
use App\Form\GestionComerciale\FabricationAnomalieType;
use App\Form\GestionComerciale\SupprimerFabricationAnomalieType;
use App\Library\Datatable\Util\Datatable;
use App\Service\Fabrication\OqtonService;
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 FabricationAnomaliesController extends AbstractController
{
/**
* @Route("/vente/fabrications-anomalies", name="dtc_fabrication_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-fabrication.html.twig';
return $this->render($template, [
'tableauClassColonne' => $tableauClassColonne,
]);
}
/**
* @Route("/vente/fabrication-anomalies/grid", name="dtc_fabrication_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 = ["statutFabrication"=>4];
$datatable
->setDatatableId('dta-anomalie-fabrication')
->setEntity(Fabrication::class, "x")
->setFields(
[
$translator->trans("ID") => 'x.id',
$translator->trans("Date") => 'x.date',
$translator->trans("Réf") => 'x.reference',
$translator->trans("Commandde") => 'c.reference',
$translator->trans("Client") => 'clt.nom',
$translator->trans("Article") => 'a.reference',
$translator->trans("Libellé") => 'a.libelle',
$translator->trans("Anomalie") => 'ra.libelle',
$translator->trans("Commentaire") => 'x.commentaireAnomalie',
$translator->trans("Actions") => 'x.id',
"_identifier_" => 'x.id',
]
)
->addJoin('x.statutFabrication', 's', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
->addJoin('x.raisonAnomalie', 'ra', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
->addJoin('x.articleCommandeCommandeClient', 'ac', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
->addJoin('ac.commande', 'c', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
->addJoin('c.client', 'clt', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
->addJoin('ac.article', 'a', \Doctrine\ORM\Query\Expr\Join::LEFT_JOIN)
->setRenderers(
[
1 => [
'view' => 'FO/DataTable/heure.html.twig',
],
9 => [
'view' => 'FO/DataTable/actions.html.twig',
'params' => [
'supprimer_route' => 'dtc_fabrication_anomalies_supprimer',
'objet' => Fabrication::class,
'deplier' => false,
'table' => "dta-anomalie-fabrication",
'page_affiche' => "anomalies",
],
],
]
)
->setSearch(true)
->setSearchFields([])
->setOrder('x.date', 'ASC');
$where = ' x.raisonAnomalie is not null and x.statutFabrication != :statutFabrication';
if ($where != '') {
$datatable->setWhere($where, $parameters);
}
return $datatable;
}
/**
* @Route("/fabrication/anomalie/ajouter/{id}", name="dtc_fabrication_anomalies_ajouter")
*/
public function ajouterAction(Request $request, Fabrication $fabrication, EntityManagerInterface $em, TranslatorInterface $translator, ValidatorInterface $validator,
CommandeService $commandeService
) {
$titre_modal = $translator->trans("Indiquer un défaut sur la fabrication");
$form = $this->createForm(FabricationAnomalieType::class, $fabrication);
$errors = "";
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$rendu = '';
$url = '';
$get = $request->query->all();
$this->addFlash('notice', $translator->trans('Défaut ajouté avec succès !'));
$donnees=[];
$donnees["message"]=" : ".$fabrication->getReference()." ".$fabrication->getArticleCommandeCommandeClient()->getReference()." (id:".$fabrication->getArticleCommandeCommandeClient()->getId().") : ".$fabrication->getRaisonAnomalie()->getLibelle();
$commandeService->creerHistorique($fabrication->getArticleCommandeCommandeClient()->getCommande(), 'anomalie_fabrication',$donnees);
$fabrication->setCaisse(null);
$em->persist($fabrication);
$em->flush();
$url = "";
if (array_key_exists('table', $get) && $get["table"] != "") {
return new Response(
json_encode(['rendu' => '', 'valide' => '1', 'url' => $url, 'type' => 'recharger_datatable', 'id_datatable' => $get["table"]]),
200,
['Content-Type' => 'application/json']
);
}
return new JsonResponse(['rendu' => '', 'valide' => '1', 'url' => $url], 200, ['Content-Type' => 'application/json']);
} else {
$errors = $validator->validate($fabrication);
$rendu = $this->renderView('GestionComerciale/Anomalies/ajouter-fabrication.html.twig', ['form' => $form->createView(), 'errors' => $errors, 'fabrication' => $fabrication]);
return new JsonResponse(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]);
}
}
$rendu = $this->renderView('GestionComerciale/Anomalies/ajouter-fabrication.html.twig', ['form' => $form->createView(), 'errors' => $errors, 'fabrication' => $fabrication]);
return new JsonResponse(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]);
}
/**
* @Route("/fabrication/anomalie/supprimer/{id}", name="dtc_fabrication_anomalies_supprimer")
*/
public function supprimerAction(Request $request, Fabrication $fabrication, EntityManagerInterface $em, TranslatorInterface $translator, ValidatorInterface $validator,CommandeService $commandeService,OqtonService $oqtonService)
{
$titre_modal = $translator->trans("Demande de confirmation");
$user = $this->getUser();
$get = $request->query->all();
$messageModal = "";
$form = $this->createForm(SupprimerFabricationAnomalieType::class, $fabrication);
$errors = "";
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$donnees=[];
$donnees["message"]=" : ".$fabrication->getReference()." ".$fabrication->getArticleCommandeCommandeClient()->getReference()." (id:".$fabrication->getArticleCommandeCommandeClient()->getId().")";
$donnees["fichier"] = $fabrication->getArticleCommandeCommandeClient()->getFichier();
$commandeService->creerHistorique($fabrication->getArticleCommandeCommandeClient()->getCommande(), 'supprimer_anomalie_fabrication',$donnees);
if($fabrication->getArticleCommandeCommandeClient()->getArticle()->getType()->getId() === Fabrication::ADDITIVE) {
$oqtonService->sendApresDefautOrder($fabrication->getArticleCommandeCommandeClient());
}
else if($fabrication->getArticleCommandeCommandeClient()->getArticle()->getType()->getId() === Fabrication::DEDUCTIVE) {
}
$fabrication->setStatutFabrication($em->getReference(StatutFabrication::class, 4));
$em->persist($fabrication);
//$fabrication->setRaisonAnomalie(NULL);
$fabrication->getArticleCommandeCommandeClient()->getCommande()->setStatutAmodifier(true);
$em->persist($fabrication->getArticleCommandeCommandeClient()->getCommande());
$fabrication->setArticleCommandeCommandeClient(NULL);
$fabrication->setCommentaireAnomalie(NULL);
$fabrication->setRaisonAnomalie(NULL);
// Annuler la fabrication
// Selon type fabrication renvoyé à Oqton
// Renommer fichier avec préfixe "SAV"
$em->persist($fabrication);
$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"]],
200,
['Content-Type' => 'application/json']
);
} else {
$this->addFlash('notice', $translator->trans('Défaut supprimé avec succès !'));
return new JsonResponse(['rendu' => '', 'valide' => '1', 'url' => $url]);
}
} else {
$errors = $validator->validate($fabrication);
$rendu = $this->renderView('FO/Supprimer/supprimer.html.twig', ['form' => $form->createView(), 'errors' => $errors]);
return new JsonResponse(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]);
}
}
else {
if($fabrication->getTypeFabrication() == Fabrication::DEDUCTIVE) {
$messageModal = "La suppresion du défaut va relancer la fabrication de l'élément : <br/> ".$fabrication->getArticle()->getReference()." <br/>Commande : ".$fabrication->getArticleCommandeCommandeClient()->getCommande()->getReference();
}
elseif($fabrication->getTypeFabrication() == Fabrication::ADDITIVE) {
$messageModal = "La suppresion du défaut va envoyer un nouveau fichier à Oqton et relancer la fabrication de l'élément : <br/> ".$fabrication->getArticle()->getReference()." <br/>Commande : ".$fabrication->getArticleCommandeCommandeClient()->getCommande()->getReference();
}
}
$rendu = $this->renderView('FO/Supprimer/supprimer.html.twig', ['form' => $form->createView(), 'errors' => $errors,'messageModal'=>$messageModal]);
return new JsonResponse(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]);
}
}