src/Controller/ClientesController.php line 187

Open in your IDE?
  1. <?php
  2. /*
  3.  * To change this license header, choose License Headers in Project Properties.
  4.  * To change this template file, choose Tools | Templates
  5.  * and open the template in the editor.
  6.  */
  7. namespace App\Controller;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Session\Session;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  12. use Knp\Component\Pager\PaginatorInterface;
  13. use App\Entity\Clientes;
  14. use App\Form\ClientesType;
  15. /**
  16.  * Description of ClientesController
  17.  *
  18.  * @author joseangelparra
  19.  */
  20. class ClientesController extends AbstractController{
  21.     private $params;
  22.     public function __construct(ParameterBagInterface $params){
  23.          
  24.          $this->session = new Session();
  25.          $this->params $params;
  26.     }
  27.     
  28.     public function index(Request $requestPaginatorInterface $paginator){
  29.         
  30.         if (!$this->getUser() || !is_object($this->getUser())) {         
  31.             return $this->redirectToRoute('logout');
  32.         }
  33.         
  34.         $em $this->getDoctrine()->getManager();
  35.         
  36.         $s = ($request->get("s") )? $request->get("s") : "";
  37.         if($s){
  38.             $s_str " AND c.name LIKE '%".$s."%' OR c.surname LIKE '%".$s."%'";
  39.         }else{
  40.             $s_str "";
  41.         }
  42.         
  43.         $orden = ($request->get("orden")) ? $request->get("orden") : 'DESC';
  44.         
  45.         $filter = ($request->get("filter")) ? $request->get("filter") : 'id';
  46.         $client = new Clientes();
  47.         $form $this->createForm(ClientesType::class,$client,[
  48.             'save_lbl'=>'Guardar'
  49.         ]);
  50.         
  51.         $form->handleRequest($request);
  52.         
  53.         if($form->isSubmitted()){
  54.             $client->setDatecreated(new \DateTime());
  55.             $client->setIdWp(null);
  56.             $client->setStatus(1);
  57.             
  58.             $em->persist($client);
  59.             $f $em->flush();
  60.             if($f == null){
  61.                 $type 1;
  62.                 $status "Cliente añadido correctamente";
  63.             }else{
  64.                 $type 0;
  65.                 $status "Error al añadir el cliente";
  66.             }
  67.             $this->session->getFlashBag()->add('type'$type);
  68.             $this->session->getFlashBag()->add('status'$status);
  69.             return $this->redirectToRoute('clientes');
  70.             
  71.         }
  72.         
  73.         $clientes $em->createQueryBuilder()->select("c")
  74.                 ->from("App\Entity\Clientes","c")
  75.                  ->where("c.id <> '' AND c.status=1 ".$s_str)
  76.                 ->orderBy("c.".$filter,$orden)
  77.                 ->getQuery()->getResult();
  78.         
  79.         $clis $paginator->paginate(
  80.                 $clientes$request->query->getInt('page'1), 20);
  81.         
  82.         return $this->render('clients/list.html.twig',array(
  83.             'clients'=>$clis,
  84.             's'=>$s,
  85.             "orden"=>$orden,
  86.             "form"=>$form->createView()
  87.             
  88.         ));
  89.     }
  90.     
  91.     public function delete(Request $request){
  92.         $em $this->getDoctrine()->getManager();
  93.         
  94.         $id=  $request->get("id");
  95.         if($id){
  96.             $client $em->getRepository(Clientes::class)->find($id);
  97.             if($client){
  98.                 $client->setStatus(0);
  99.                 $client->setEmail('');
  100.                 $client->setNif('');
  101.                 $client->setAddress('');
  102.                 $client->setCp('');
  103.                 $client->setCity('');
  104.                 $client->setCountry('');
  105.                 $client->setPhone('');
  106.                 $client->setState('');
  107.                 $em->persist($client);
  108.                 $f $em->flush();
  109.                 if($f == null){
  110.                     $type 1;
  111.                     $status "Cliente eliminado correctamente";
  112.                 }else{
  113.                     $type 0;
  114.                     $status "Error al eliminar el cliente";
  115.                 }
  116.                 $this->session->getFlashBag()->add('type'$type);
  117.                 $this->session->getFlashBag()->add('status'$status);
  118.                 return $this->redirectToRoute('clientes');
  119.             }else{
  120.                 $type 0;
  121.                 $status "Cliente no existente";
  122.                 $this->session->getFlashBag()->add('type'$type);
  123.                 $this->session->getFlashBag()->add('status'$status);
  124.                 return $this->redirectToRoute('clientes');
  125.             }
  126.         }else{
  127.             $type 0;
  128.             $status "Error en la petición";
  129.             $this->session->getFlashBag()->add('type'$type);
  130.             $this->session->getFlashBag()->add('status'$status);
  131.             return $this->redirectToRoute('clientes');
  132.         }
  133.         
  134.     }
  135.     
  136.     public function detailClient(Request $requestPaginatorInterface $paginator){
  137.         $em $this->getDoctrine()->getManager();
  138.         $id $request->get("id");
  139.         
  140.         $cliente $em->getRepository(Clientes::class)->find($id);
  141.         
  142.         
  143.         $ventas $em->createQueryBuilder()->select("v")
  144.                 ->from("App\Entity\Ventas","v")
  145.                 ->where("v.clientId = :id")
  146.                 ->setParameter("id",$id)
  147.                 ->orderBy("v.date""DESC")
  148.                 ->getQuery()
  149.                 ->getResult();
  150.         $v $paginator->paginate(
  151.                 $ventas$request->query->getInt('page'1), 10);
  152.         
  153.         $prods_cant 0;
  154.         $prods_res 0;
  155.         foreach($ventas as $vent){
  156.             
  157.                 $prods_in $em->getRepository(\App\Entity\VentaProducts::class)->findBy(array("ventaId"=>$vent->getId()));
  158.                 foreach($prods_in as $p){
  159.                     if($vent->getStatus()<3){
  160.                         $prods_cant $prods_cant $p->getQty();
  161.                     }else{
  162.                         $prods_res $prods_res $p->getQty();
  163.                     }
  164.                 }
  165.             
  166.         }
  167.         
  168.         $last_buy $em->createQueryBuilder()->select('b')
  169.                 ->from('App\Entity\Ventas','b')
  170.                 ->where("b.clientId = :id ")
  171.                 ->setParameter("id",$id)
  172.                 ->orderBy("b.date""DESC")
  173.                 ->setMaxResults(1)
  174.                 ->getQuery()
  175.                 ->getResult();
  176.         
  177.         return $this->render('clients/detail.html.twig',array(
  178.             'client'=>$cliente,
  179.             'ventas'=>$v,
  180.             'prods_cant'=>$prods_cant,
  181.             'prods_res'=>$prods_res,
  182.             'last_buy'=>(count($last_buy)>0) ? $last_buy[0] : []
  183.         ));
  184.     }
  185.     
  186.     public function modifyClientData(Request $request){
  187.         $em $this->getDoctrine()->getManager();
  188.         $id $request->get("id");
  189.         
  190.         $datos $request->request->all();
  191.         $client $em->getRepository(Clientes::class)->find($id);
  192.         
  193.         if($datos && $client){
  194.             $client->setName($datos["name"]);
  195.             $client->setSurname($datos["surname"]);
  196.             $client->setEmail($datos["email"]);
  197.             $client->setNif($datos["nif"]);
  198.             $client->setAddress($datos["address"]);
  199.             $client->setCp($datos["cp"]);
  200.             $client->setCity($datos["city"]);
  201.             $client->setState($datos["state"]);
  202.             $client->setCountry($datos["country"]);
  203.             $client->setPhone($datos["phone"]);
  204.             
  205.             $em->persist($client);
  206.             $f $em->flush();
  207.             
  208.             if($f == null){
  209.                 $type 1;
  210.                 $status "Cliente editado correctamente";
  211.             }else{
  212.                 $type 0;
  213.                 $status "Error al editar el cliente";
  214.             } 
  215.         }else{
  216.             $type 0;
  217.             $status "Datos incorrectos o no se encontro cliente";
  218.         }
  219.         $this->session->getFlashBag()->add('type'$type);
  220.         $this->session->getFlashBag()->add('status'$status);
  221.         return $this->redirectToRoute('detail_client',array("id"=>$id));
  222.     }
  223. }