src/Controller/TPVController.php line 124

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\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Session\Session;
  10. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use App\Entity\Products;
  14. use App\Entity\Clientes;
  15. use App\Form\ClientesType;
  16. use App\Entity\Ventas;
  17. use App\Entity\VentaProducts;
  18. /**
  19.  * Description of TPVController
  20.  *
  21.  * @author joseangelparra
  22.  */
  23. class TPVController extends AbstractController{
  24.     
  25.     private $params;
  26.     public function __construct(ParameterBagInterface $params){
  27.          
  28.          $this->session = new Session();
  29.          $this->params $params;
  30.     }
  31.     
  32.     public function index(Request $request){
  33.         $em $this->getDoctrine()->getManager();
  34.         
  35.         $cliente = new Clientes();
  36.         $form $this->createForm(ClientesType::class,$cliente,[
  37.              'save_lbl'=>'Guardar'
  38.         ]);
  39.         $start=false;
  40.         
  41.         $fech = new \DateTime();
  42.         $fech->setTime(00);
  43.         
  44.         $cj $em->getRepository(\App\Entity\CierreCaja::class)->findOneBy(array("fecha"=>$fech));
  45.         if(!$cj){
  46.             $start=true;
  47.         }
  48.         $form->handleRequest($request);
  49.         if($form->isSubmitted()){
  50.             $cliente->setDatecreated(new \DateTime());
  51.             $cliente->setIdWp(null);
  52.             $cliente->setStatus(1);
  53.             
  54.           
  55.             $em->persist($cliente);
  56.             $f $em->flush();
  57.             if($f == null){
  58.                 $type 1;
  59.                 $status "Cliente añadido correctamente";
  60.             }else{
  61.                 $type 0;
  62.                 $status "Error al añadir el cliente";
  63.             }
  64.             $this->session->getFlashBag()->add('type'$type);
  65.             $this->session->getFlashBag()->add('status'$status);
  66.             return $this->redirectToRoute('tpv');
  67.             
  68.         }
  69.         
  70.         $clientes $em->getRepository(Clientes::class)->findBy(array("status"=>1));
  71.         return $this->render('tpv/index.html.twig',array(
  72.             'form'=>$form->createView(),
  73.             'clientes'=>$clientes,
  74.             'start'=>$start
  75.         ));
  76.     }
  77.     
  78.     public function getVentaTable(){
  79.         return $this->render('tpv/ventaTable.html.twig',array(
  80.             
  81.         ));
  82.     }
  83.     
  84.     public function getProductByCode(Request $request){
  85.         $em $this->getDoctrine()->getManager();
  86.         $search = ($request->request->get("search")) ? $request->request->get("search") : '';
  87.         
  88.         $product $em->createQueryBuilder()->select('p')
  89.                 ->from('App\Entity\Products','p')
  90.                 ->where('(p.sku = :search or p.ean = :search) and p.status > 0')
  91.                 ->setParameter('search',$search)
  92.                 ->getQuery()
  93.                 ->getResult();
  94.         
  95.         if(count($product)>0){
  96.             return new JsonResponse(array('empty_data'=>false,'product'=>$product[0]->to_Array()));
  97.         }else{
  98.              return new JsonResponse(array('empty_data'=>true));
  99.         }
  100.     }
  101.     
  102.     public function getProductById(Request $request){
  103.         $em $this->getDoctrine()->getManager();
  104.         $search = ($request->request->get("search")) ? $request->request->get("search") : '';
  105.         
  106.         $product $em->createQueryBuilder()->select('p')
  107.                 ->from('App\Entity\Products','p')
  108.                 ->where('p.id = :search')
  109.                 ->setParameter('search',$search)
  110.                 ->getQuery()
  111.                 ->getResult();
  112.         
  113.         if(count($product)>0){
  114.             return new JsonResponse(array('empty_data'=>false,'product'=>$product[0]->to_Array()));
  115.         }else{
  116.              return new JsonResponse(array('empty_data'=>true));
  117.         }
  118.     }
  119.     
  120.     public function renderLineTableTicket(Request $request){
  121.         $prod $request->request->get("prod");
  122.         return $this->render('tpv/ticketLineTable.html.twig',array(
  123.             'prod'=>$prod
  124.         ));
  125.     }
  126.     
  127.     public function saveTransaction(Request $request){
  128.         $em $this->getDoctrine()->getManager();
  129.         
  130.         $info $request->request->get("venta");
  131.         $venta json_decode($info);
  132.         $type 1;
  133.         $message "Transacción guardada";
  134.         $vid false;
  135.         if($venta){
  136.             if($venta->ticket_id == null){
  137.                 $trans = new Ventas(); 
  138.                 $trans->setDate(new \DateTime());
  139.             }else{
  140.                 $trans $em->getRepository(Ventas::class)->find($venta->ticket_id);
  141.             }
  142.             $trans->setClientId($venta->client_id);
  143.             $trans->setUserId($this->getUser()->getId());
  144.             if($venta->tipo == 1){
  145.                  if(floatval($venta->pagado) >= floatval($venta->total_ticket)){
  146.                      $trans->setStatus(1); //total
  147.                  }else{
  148.                      $trans->setStatus(2); //parcial
  149.                  }
  150.             }else{
  151.              $trans->setStatus(3); //aparcado
  152.             }
  153.             $trans->setSubtotal($venta->subtotal_ticket);
  154.             $iva_value floatval($venta->total_ticket) - floatval($venta->subtotal_ticket);
  155.             $trans->setIva(21);
  156.             $trans->setIvaValue($iva_value);
  157.             $trans->setTotal($venta->total_ticket);
  158.             $trans->setDateModif(new \DateTime());
  159.             $trans->setPaymentMethod(intval($venta->metodo_pago));
  160.             $trans->setOrderWpId(null);
  161.             $trans->setExtraInfo($info);
  162.             $trans->setGlobalDiscount(0); 
  163.             $trans->setPayed($venta->pagado);
  164.             
  165.             $em->persist($trans);
  166.             $f $em->flush();
  167.             if($f == null){
  168.                 $vid $trans->getId();
  169.                 //eliminamos los productos anteriores
  170.                 $prods $em->getRepository(VentaProducts::class)->findBy(array("ventaId"=>$trans->getId()));
  171.                 if($prods){
  172.                     foreach($prods as $p){
  173.                         $em->remove($p);
  174.                         $em->flush();
  175.                     }
  176.                 }
  177.                 //añadimos los productos   
  178.                 $error=0;
  179.                 foreach($venta->products as $prod){
  180.                     $vp = new VentaProducts();
  181.                     $vp->setVentaId($trans->getId());
  182.                     $vp->setProductId($prod->id);
  183.                     $vp->setQty($prod->qty);
  184.                     $vp->setUnitPrice($prod->unit_price);
  185.                     $vp->setIva($prod->iva);
  186.                     $vp->setDiscount($prod->discount);
  187.                     
  188.                     $em->persist($vp);
  189.                     $f_p $em->flush();
  190.                     if($f_p != null){
  191.                         $type 0;
  192.                         $message "Error al añadir el producto ".$prod->id;
  193.                         break;
  194.                     }
  195.                     
  196.                     //descontamos el stock si es totalizado
  197.                     if($trans->getStatus() != ){
  198.                         $this->discountLocalStock($prod);
  199.                     }
  200.                 }                
  201.             }else{
  202.                 $type=0;
  203.                 $message "Error al guardar la transacción";
  204.             }
  205.         }else{
  206.             $type=0;
  207.             $message "Error al leer la venta";
  208.         }
  209.         
  210.         $this->session->getFlashBag()->add('type'$type);
  211.         $this->session->getFlashBag()->add('status'$message);
  212.         return $this->redirectToRoute('tpv', array("id"=>$vid));
  213.         
  214.     }
  215.     
  216.     public function generarDevolucion(Request $request){
  217.         $em $this->getDoctrine()->getManager();
  218.         $venta_id $request->get("id");
  219.         $type 1;
  220.         $message "Devolución realizada";
  221.         
  222.         $venta $em->getRepository(Ventas::class)->find($venta_id);
  223.         //sets
  224.         $trans = new Ventas();
  225.         $trans->setDate(new \DateTime());
  226.         
  227.         $trans->setClientId($venta->getclientId());
  228.         $trans->setUserId($this->getUser()->getId());
  229.         $trans->setStatus(4); //devolución
  230.         
  231.         $trans->setSubtotal(($venta->getSubtotal() * -1));
  232.         
  233.         $trans->setIva(21);
  234.         $trans->setIvaValue($venta->getIvaValue());
  235.         $trans->setTotal($venta->getTotal()*-1);
  236.         $trans->setDateModif(new \DateTime());
  237.         $trans->setPaymentMethod($venta->getPaymentMethod());
  238.         $trans->setOrderWpId($venta->getOrderWpId());
  239.         $trans->setExtraInfo($venta->getExtraInfo());
  240.         $trans->setGlobalDiscount(0); 
  241.         $trans->setPayed($venta->getPayed()*-1);
  242.         $em->persist($trans);
  243.         $f $em->flush();
  244.         if($f == null){            
  245.             //eliminamos los productos anteriores
  246.             $prods $em->getRepository(VentaProducts::class)->findBy(array("ventaId"=>$venta_id));
  247.             if($prods){
  248.                 
  249.                 //añadimos los productos   
  250.                 $error=0;
  251.                 foreach($prods as $prod){
  252.                     $vp = new VentaProducts();
  253.                     $vp->setVentaId($trans->getId());
  254.                     $vp->setProductId($prod->getProductId());
  255.                     $vp->setQty($prod->getQty());
  256.                     $vp->setUnitPrice($prod->getUnitPrice());
  257.                     $vp->setIva($prod->getIva());
  258.                     $vp->setDiscount($prod->getDiscount());
  259.                     $em->persist($vp);
  260.                     $f_p $em->flush();
  261.                     if($f_p != null){
  262.                         $type 0;
  263.                         $message "Error al añadir el producto ".$prod->getId();
  264.                         break;
  265.                     }
  266.                     //descontamos el stock si es totalizado
  267.                     if($trans->getStatus() != ){
  268.                         $this->restoreLocalStock($prod);
  269.                     }
  270.                 }         
  271.             }
  272.         }else{
  273.             $type=0;
  274.             $message "Error al guardar la transacción";
  275.         }
  276.         
  277.         $this->session->getFlashBag()->add('type'$type);
  278.         $this->session->getFlashBag()->add('status'$message);
  279.         return $this->redirectToRoute('ventas');
  280.         
  281.     }
  282.     
  283.     public function discountLocalStock($prod){
  284.         $em $this->getDoctrine()->getManager();
  285.         
  286.         $producto $em->getRepository(Products::class)->find($prod->id);
  287.         $c_stock $producto->getStoreStock();
  288.         $stock $c_stock $prod->qty;
  289.         
  290.         $g_stock $producto->getGlobalStock();
  291.         $stock_g $g_stock $prod->qty;
  292.         
  293.         $producto->setStoreStock($stock);
  294.         $producto->setGlobalStock($stock_g);
  295.         
  296.         $em->persist($producto);
  297.         $f $em->flush();
  298.         if($f == null){
  299.             return true;
  300.         }else{
  301.             return false;
  302.         }
  303.     }
  304.     public function restoreLocalStock($prod){
  305.         $em $this->getDoctrine()->getManager();
  306.         
  307.         $producto $em->getRepository(Products::class)->find($prod->getProductId());
  308.         $c_stock $producto->getStoreStock();
  309.         $stock $c_stock $prod->getQty();
  310.         
  311.         $g_stock $producto->getGlobalStock();
  312.         $stock_g $g_stock $prod->getQty();
  313.         
  314.         $producto->setStoreStock($stock);
  315.         $producto->setGlobalStock($stock_g);
  316.         
  317.         $em->persist($producto);
  318.         $f $em->flush();
  319.         if($f == null){
  320.             return true;
  321.         }else{
  322.             return false;
  323.         }
  324.     }
  325.     public function printTicket(Request $request){
  326.         $em $this->getDoctrine()->getManager();
  327.         
  328.         $ticket_id $request->request->get("id");
  329.         $venta $em->getRepository(Ventas::class)->find($ticket_id);
  330.         $prods $em->getRepository(VentaProducts::class)->findBy(array('ventaId'=>$ticket_id));
  331.         $client $em->getRepository(Clientes::class)->find($venta->getClientId());
  332.         
  333.         return $this->render('tpv/ticket.html.twig',array(
  334.             'venta'=>$venta,
  335.             'prods'=>$prods,
  336.             'client'=>$client,
  337.             'info'=>json_decode($venta->getExtraInfo())
  338.         ));
  339.     }
  340.     
  341.     public function printTicketRegalo(Request $request){
  342.         $em $this->getDoctrine()->getManager();
  343.         
  344.         $ticket_id $request->request->get("id");
  345.         $venta $em->getRepository(Ventas::class)->find($ticket_id);
  346.         $prods $em->getRepository(VentaProducts::class)->findBy(array('ventaId'=>$ticket_id));
  347.         $client $em->getRepository(Clientes::class)->find($venta->getClientId());
  348.         
  349.         return $this->render('tpv/ticketRegalo.html.twig',array(
  350.             'venta'=>$venta,
  351.             'prods'=>$prods,
  352.             'client'=>$client,
  353.             'info'=>json_decode($venta->getExtraInfo())
  354.         ));
  355.     }
  356.     public function loadAparquets(Request $request){
  357.         $em $this->getDoctrine()->getManager();
  358.         
  359.         $aparcados $em->getRepository(Ventas::class)->findBy(array('status'=>3));
  360.         
  361.         return $this->render('tpv/aparcados.html.twig',array(
  362.             'ventas'=>$aparcados
  363.         ));
  364.     }
  365.     
  366.     public function loadPartials(Request $request){
  367.         $em $this->getDoctrine()->getManager();
  368.         
  369.         $aparcados $em->getRepository(Ventas::class)->findBy(array('status'=>2));
  370.         
  371.         return $this->render('tpv/parciales.html.twig',array(
  372.             'ventas'=>$aparcados
  373.         ));
  374.     }
  375.     
  376.     public function deleteVenta(Request $request){
  377.         $id $request->request->get("id");
  378.         $em $this->getDoctrine()->getManager();
  379.         $venta $em->getRepository(Ventas::class)->find($id);
  380.         $ok=false;
  381.         if($venta){
  382.             $em->remove($venta);
  383.             $f $em->flush();
  384.             if($f==null){
  385.                 $ok=true;
  386.             }
  387.         }
  388.         return new JsonResponse(array("ok"=>$ok));
  389.     }
  390.     
  391.     public function newProductManual(){
  392.         return $this->render('products/new_product_ajax.html.twig',array(
  393.             
  394.         ));
  395.     }
  396. }