<?php
namespace App\Controller\gmao;
use App\Entity\gmao\MouvementStock;
use App\Entity\gmao\FamilleArticle;
use App\Entity\gmao\Article;
use App\Entity\gmao\Marque;
use Symfony\Component\Validator\Constraints\Range;
use App\Entity\gmao\TypeStock;
use App\Form\gmao\MouvementStockType;
use Doctrine\ORM\EntityRepository;
use App\Repository\gmao\MouvementStockRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use App\Traits\gmao\EtatElementTrait;
use App\Repository\gmao\ArticleRepository;
use App\ControllerModel\user\paramUtilTrait;
/**
* @Route("/mouvement/stock")
*/
class MouvementStockController extends AbstractController
{
use EtatElementTrait;use paramUtilTrait;
private $response;
public function __construct() {
$this->response = new Response();
$this->response->headers->addCacheControlDirective('no-cache', true);
$this->response->headers->addCacheControlDirective('max-age', 0);
$this->response->headers->addCacheControlDirective('must-revalidate', true);
$this->response->headers->addCacheControlDirective('no-store', true);
}
/**
* @Route("/liste", name="mvtstock_liste", methods="GET")
*/
public function index(MouvementStockRepository $mouvementStockRepository): Response
{
return $this->render($this->gmaoBundleSlash($sessionData['typePresentation']). 'mouvement_stock/index.html.twig', ['mouvement_stocks' => $mouvementStockRepository->findAll()]);
}
/**
* @Route("/operation/{cas}", name="mvstock_new", methods="GET|POST" , requirements={"cas" = "\d+"})
*/
public function mgstock_operation(Request $request, int $cas = 0, ArticleRepository $repoArt, MouvementStockRepository $repoMvt ): Response
{
$this->data = array();
//var_dump( $repoArt->findArticleAjax(4,16,13)); exit;;
$form = $this->createFormStock($cas);
$form->handleRequest($request);
$this->data['form'] = $form->createView();
$this->data['cas'] = $cas;
if ($form->isSubmitted()) {
if ($form->isValid()){
$em = $this->getDoctrine()->getManager();
$data = $form->getData();
$article = ($data)? $data['article']: NULL ;
$nbre = (int) $data['nbre'];
if ($cas>0){
$livr = $data['refliv'];
$cmde = $data['refcmde'];
}
$mrq = $data['marque'];
if ($article instanceof Article){
$mvt = new MouvementStock();
$d = new \DateTime();
$mvt->setHorodatage($d);
($cas > 0)? $mvt->setQteEntree($nbre): $mvt->setQteSortie($nbre);
$mvt->setRefMouvement($repoMvt->setMouvement());
($cas > 0)? $lib =' Approvisionnement '.$article->getNomArticle() : $lib =' Retrait '.$article->getNomArticle();
$mvt->setLibelle($lib);
if ($cas>0){
$mvt->setRefCmde($repoMvt->setCommande());
$mvt->setRefLivr($repoMvt->setLivraison());
}
$mvt->setEtat(1);
$mvt->setArticle($article);
$em->persist($mvt);
$em->flush();
}
}else{
}
return $this->redirectToRoute('mvtstock_liste');
}
return $this->render($this->gmaoBundleSlash($sessionData['typePresentation']). 'mouvement_stock/new.html.twig', $this->data,$this->response);
}
/**
* @Route("details/{id}", name="mvtstock_details", methods="GET|POST" , requirements={"cas" = "\d+"})
*/
public function mgstock_details(Request $request): Response //, MouvementStock $mouvementStock
{
return $this->render($this->gmaoBundleSlash($sessionData['typePresentation']). 'mouvement_stock/show.html.twig', ['mouvement_stock' => $mouvementStock]);
}
/**
* @Route("annule/{id}", name="mvtstock_annule", methods="GET|POST" , requirements={"cas" = "\d+"})
*/
public function mgstock_annule(Request $request): Response //, MouvementStock $mouvementStock
{
$form = $this->createForm(MouvementStockType::class, $mouvementStock);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('mouvement_stock_edit', ['id' => $mouvementStock->getId()]);
}
return $this->render($this->gmaoBundleSlash($sessionData['typePresentation']). 'mouvement_stock/edit.html.twig', [
'mouvement_stock' => $mouvementStock,
'form' => $form->createView(),
]);
}
protected function createFormStock($cas = 0){
($cas > 0)? $tab = ['Approvisionnement Equipements' => 1] : $tab = ['Sortie Equipements' => 0] ;
$builder = $this->createFormBuilder();
$builder->add( 'typestock', EntityType::class, array(
'class' => TypeStock::class,
'choice_label' => 'nomTypeStock',
'multiple' => false,
'expanded' => false,
'query_builder' => function (EntityRepository $rep) {
return $rep->createQueryBuilder('t')
->AndWhere(' t.etat = 1');
},
'required' => FALSE,
'attr' => array('required' => FALSE,'class' => 'form-control custom-select mouv typstock' ,'id'=>'client'))
)
->add( 'famille', EntityType::class, array(
'class' => FamilleArticle::class,
'choice_label' => 'nomFamille',
'multiple' => false,
'expanded' => false,
'query_builder' => function (EntityRepository $rep) {
return $rep->createQueryBuilder('f')
->AndWhere(' f.etat = 1');
},
'empty_data'=>'Veuillez sélectionner la famille',
'required' => FALSE,
'attr' => array('required' => TRUE, 'class' => 'form-control custom-select mouv famille' ,'id'=>'technic'))
)
->add( 'marque', EntityType::class, array(
'class' => Marque::class,
'choice_label' => 'marqueLib',
'multiple' => false,
'expanded' => false,
'empty_data'=>'Veuillez sélectionner la marque',
'query_builder' => function (EntityRepository $rep) {
return $rep->createQueryBuilder('m')
->AndWhere(' m.etat = 1');
},
'required' => FALSE,
'attr' => array('required' => TRUE,'class' => 'form-control custom-select mouv marque' ,'id'=>'technic'))
)
// ->add( 'article', EntityType::class, array(
// 'class' => Article::class,
// 'choice_label' => 'nomArticle',
// 'multiple' => false,
// 'expanded' => false,
// 'empty_data'=>"Veuillez sélectionner l'article",
// 'required' => FALSE,
// 'attr' => array('required' => TRUE, 'class' => 'form-control custom-select mouv marque' ,'id'=>'article'))
// )
//
->add( 'article', ChoiceType::class, array(
'label' => 'Type Operation',
'attr' => array('class' => 'form-control',
'name' => 'typeopertion',
'disabled' => false,
'placeholder' => '', 'autocomplete'=>"off"),
)
)
->add( 'sens', ChoiceType::class, array(
'constraints' => new Range(array('min' => 0)),
'label' => 'Type Operation',
'attr' => array('class' => 'form-control',
'name' => 'typeopertion',
'disabled' => true,
'placeholder' => '', 'autocomplete'=>"off"),
'choices' => $tab)
)
->add( 'reel', IntegerType::class,
array(
'constraints' => new Range(array('min' => 0, 'minMessage'=>'Nombre équipements du mouvement de stock ne peut être inférieur à zéro!')),
'label' => 'reel',
'attr' => array(
'required' => FALSE,
'min'=>0,
'class' => 'form-control',
'name' => 'reel',
'placeholder' => 'Nbre en stock',
'autocomplete'=>"off"))
)
->add( 'nbre', IntegerType::class, array(
'label' => 'nbre',
'attr' => array('required' => TRUE,'min'=>0,'class' => 'form-control', 'name' => 'nbre', 'placeholder' => 'Nbre mouvements', 'autocomplete'=>"off")))
;
if ($cas > 0){
$builder->add( 'refcmde', TextType::class, array('label' => 'cmde', 'attr' => array('required' => FALSE,'class' => 'form-control', 'name' => 'nbre', 'placeholder' => 'Référence commande', 'autocomplete'=>"off")))
->add( 'refliv', TextType::class, array('label' => 'liv', 'attr' => array('required' => FALSE,'class' => 'form-control', 'name' => 'nbre', 'placeholder' => 'Référence livraison', 'autocomplete'=>"off"))) ;
}
$form = $builder->getForm();
return $form;
}
/**
* @Route("load/equipement", name="equipement_get", methods="GET")
*/
public function mgstock_loadEquipement(Request $request): String
{
// etat initial
$rep = array('liste'=>[], 'msg' => 'Erreur survenue lors du traitement');
$encoders = array(new JsonEncoder());
$normalizers = array(new ObjectNormalizer());
$serializer = new Serializer($normalizers, $encoders);
$data = $serializer->serialize($rep,'json');
$em = $this->getDoctrine()->getManager();
if (($request->isXmlHttpRequest()) && ($request->getMethod() == 'POST')) {
$tempIds = $request->request->get('sId');
$tabEl = explode('|', $tempIds);
foreach ($tabEl as $elS) {
if($elS !=""){
$tabIds = explode('_', $elS);
foreach ($tabIds as $key => $value) {
if ( lowrcase($value) === 'f'){
$idFam = $tabIds[$key+1];
}
if ( lowrcase($value) === 'm'){
$idMrq = $tabIds[$key+1];
}
}
}
}
$repository = $this->getDoctrine()->getRepository(Article::class);
$liste = $repository->findArticles(0,$idMrq,$idFam);
$rep['liste'] = $liste;
$rep['msg'] = '';
$data = $serializer->serialize($rep,'json');
}
return $data;
}
}