src/Controller/TPVController.php line 429

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.                 }                
  202.             }else{
  203.                 $type=0;
  204.                 $message "Error al guardar la transacción";
  205.             }
  206.         }else{
  207.             $type=0;
  208.             $message "Error al leer la venta";
  209.         }
  210.         
  211.         $this->session->getFlashBag()->add('type'$type);
  212.         $this->session->getFlashBag()->add('status'$message);
  213.         return $this->redirectToRoute('tpv', array("id"=>$vid));
  214.         
  215.     }
  216.     
  217.     public function generarDevolucion(Request $request){
  218.         $em $this->getDoctrine()->getManager();
  219.         $venta_id $request->get("id");
  220.         $type 1;
  221.         $message "Devolución realizada";
  222.         
  223.         $venta $em->getRepository(Ventas::class)->find($venta_id);
  224.         //sets
  225.         $trans = new Ventas();
  226.         $trans->setDate(new \DateTime());
  227.         
  228.         $trans->setClientId($venta->getclientId());
  229.         $trans->setUserId($this->getUser()->getId());
  230.         $trans->setStatus(4); //devolución
  231.         
  232.         $trans->setSubtotal(($venta->getSubtotal() * -1));
  233.         
  234.         $trans->setIva(21);
  235.         $trans->setIvaValue($venta->getIvaValue());
  236.         $trans->setTotal($venta->getTotal()*-1);
  237.         $trans->setDateModif(new \DateTime());
  238.         $trans->setPaymentMethod($venta->getPaymentMethod());
  239.         $trans->setOrderWpId($venta->getOrderWpId());
  240.         $trans->setExtraInfo($venta->getExtraInfo());
  241.         $trans->setGlobalDiscount(0); 
  242.         $trans->setPayed($venta->getPayed()*-1);
  243.         $em->persist($trans);
  244.         $f $em->flush();
  245.         if($f == null){            
  246.             //eliminamos los productos anteriores
  247.             $prods $em->getRepository(VentaProducts::class)->findBy(array("ventaId"=>$venta_id));
  248.             if($prods){
  249.                 
  250.                 //añadimos los productos   
  251.                 $error=0;
  252.                 foreach($prods as $prod){
  253.                     $vp = new VentaProducts();
  254.                     $vp->setVentaId($trans->getId());
  255.                     $vp->setProductId($prod->getProductId());
  256.                     $vp->setQty($prod->getQty());
  257.                     $vp->setUnitPrice($prod->getUnitPrice());
  258.                     $vp->setIva($prod->getIva());
  259.                     $vp->setDiscount($prod->getDiscount());
  260.                     $em->persist($vp);
  261.                     $f_p $em->flush();
  262.                     if($f_p != null){
  263.                         $type 0;
  264.                         $message "Error al añadir el producto ".$prod->getId();
  265.                         break;
  266.                     }
  267.                     //descontamos el stock si es totalizado
  268.                     if($trans->getStatus() != ){
  269.                         $this->restoreLocalStock($prod);
  270.                     }
  271.                 }         
  272.             }
  273.         }else{
  274.             $type=0;
  275.             $message "Error al guardar la transacción";
  276.         }
  277.         
  278.         $this->session->getFlashBag()->add('type'$type);
  279.         $this->session->getFlashBag()->add('status'$message);
  280.         return $this->redirectToRoute('ventas');
  281.         
  282.     }
  283.     
  284.     public function discountLocalStock($prod){
  285.         $em $this->getDoctrine()->getManager();
  286.         
  287.         
  288.         $producto $em->getRepository(Products::class)->find($prod->id);
  289.         
  290.         $c_stock $producto->getStoreStock();
  291.         $stock $c_stock $prod->qty;
  292.         
  293.         $g_stock $producto->getGlobalStock();
  294.         $stock_g $g_stock $prod->qty;
  295.         
  296.         $producto->setStoreStock($stock);
  297.         $producto->setGlobalStock($stock_g);
  298.         
  299.         
  300.         $em->persist($producto);
  301.         $f $em->flush();
  302.         if($f == null){
  303.             if($producto->getWpId()!= null){
  304.                 $wc = new WCController();
  305.                 $wc->updateProductToWP($producto$em);
  306.             }
  307.             return true;
  308.         }else{
  309.             return false;
  310.         }
  311.     }
  312.     public function restoreLocalStock($prod){
  313.         $em $this->getDoctrine()->getManager();
  314.         
  315.         $producto $em->getRepository(Products::class)->find($prod->getProductId());
  316.         $c_stock $producto->getStoreStock();
  317.         $stock $c_stock $prod->getQty();
  318.         
  319.         $g_stock $producto->getGlobalStock();
  320.         $stock_g $g_stock $prod->getQty();
  321.         
  322.         $producto->setStoreStock($stock);
  323.         $producto->setGlobalStock($stock_g);
  324.         
  325.         $em->persist($producto);
  326.         $f $em->flush();
  327.         if($f == null){
  328.             if($producto->getWpId()!= null){
  329.                 $wc = new WCController();
  330.                 $wc->updateProductToWP($producto$em);
  331.             }
  332.             return true;
  333.         }else{
  334.             return false;
  335.         }
  336.     }
  337.     public function printTicket(Request $request){
  338.         $em $this->getDoctrine()->getManager();
  339.         
  340.         $ticket_id $request->request->get("id");
  341.         $venta $em->getRepository(Ventas::class)->find($ticket_id);
  342.         $prods $em->getRepository(VentaProducts::class)->findBy(array('ventaId'=>$ticket_id));
  343.         $client $em->getRepository(Clientes::class)->find($venta->getClientId());
  344.         
  345.         return $this->render('tpv/ticket.html.twig',array(
  346.             'venta'=>$venta,
  347.             'prods'=>$prods,
  348.             'client'=>$client,
  349.             'info'=>json_decode($venta->getExtraInfo())
  350.         ));
  351.     }
  352.     
  353.     public function printTicketRegalo(Request $request){
  354.         $em $this->getDoctrine()->getManager();
  355.         
  356.         $ticket_id $request->request->get("id");
  357.         $venta $em->getRepository(Ventas::class)->find($ticket_id);
  358.         $prods $em->getRepository(VentaProducts::class)->findBy(array('ventaId'=>$ticket_id));
  359.         $client $em->getRepository(Clientes::class)->find($venta->getClientId());
  360.         
  361.         return $this->render('tpv/ticketRegalo.html.twig',array(
  362.             'venta'=>$venta,
  363.             'prods'=>$prods,
  364.             'client'=>$client,
  365.             'info'=>json_decode($venta->getExtraInfo())
  366.         ));
  367.     }
  368.     public function loadAparquets(Request $request){
  369.         $em $this->getDoctrine()->getManager();
  370.         
  371.         $aparcados $em->getRepository(Ventas::class)->findBy(array('status'=>3));
  372.         
  373.         return $this->render('tpv/aparcados.html.twig',array(
  374.             'ventas'=>$aparcados
  375.         ));
  376.     }
  377.     
  378.     public function loadPartials(Request $request){
  379.         $em $this->getDoctrine()->getManager();
  380.         
  381.         $aparcados $em->getRepository(Ventas::class)->findBy(array('status'=>2));
  382.         
  383.         return $this->render('tpv/parciales.html.twig',array(
  384.             'ventas'=>$aparcados
  385.         ));
  386.     }
  387.     
  388.     public function deleteVenta(Request $request){
  389.         $id $request->request->get("id");
  390.         $em $this->getDoctrine()->getManager();
  391.         $venta $em->getRepository(Ventas::class)->find($id);
  392.         $ok=false;
  393.         if($venta){
  394.             $em->remove($venta);
  395.             $f $em->flush();
  396.             if($f==null){
  397.                 $ok=true;
  398.             }
  399.         }
  400.         return new JsonResponse(array("ok"=>$ok));
  401.     }
  402.     
  403.     public function newProductManual(){
  404.         return $this->render('products/new_product_ajax.html.twig',array(
  405.             
  406.         ));
  407.     }
  408.     
  409.     
  410.     public function getTodayWpSells(Request $request){
  411.         $em $this->getDoctrine()->getManager();
  412.         $wc = new WCController();
  413.         
  414.         $orders $wc->getVentas();
  415.         
  416.         foreach($orders as $order){
  417.             //dd($order);
  418.             //1. miramos si ya existe la venta:
  419.             $exists $em->getRepository(Ventas::class)->findOneBy(array("orderWpId"=>$order->id));
  420.             if(!$exists){
  421.                 $trans = new Ventas();
  422.                 $trans->setDate(new \DateTime($order->date_created));
  423.                 $trans->setOrderWpId($order->id);
  424.                 $trans->setDateModif(new \DateTime($order->date_modified));
  425.                 $trans->setSubtotal(floatval($order->total) - floatval($order->total_tax));
  426.                 $trans->setPaymentMethod(2);
  427.                 $trans->setExtraInfo(json_encode($order));
  428.                 //buscamos/creamos el cliente 
  429.                 if($order->billing->email){
  430.                     $cust $em->getRepository(Clientes::class)->findOneBy(array("email"=>$order->billing->email));
  431.                     if(!$cust){
  432.                         $cust = new Clientes();
  433.                         $cust->setName($order->billing->first_name);
  434.                         $cust->setSurname($order->billing->last_name);
  435.                         $cust->setEmail($order->billing->email);
  436.                         $cust->setAddress($order->billing->address_1);
  437.                         $cust->setCp($order->billing->postcode);
  438.                         $cust->setCity($order->billing->city);
  439.                         $cust->setCountry($order->billing->country);
  440.                         $cust->setState($order->billing->state);
  441.                         $cust->setNif('');
  442.                         $cust->setPhone($order->billing->phone);
  443.                         $cust->setDatecreated(new \DateTime());
  444.                         $cust->setStatus(1);
  445.                         $em->persist($cust);
  446.                         $em->flush();
  447.                         
  448.                     }
  449.                     
  450.                 }
  451.                 $trans->setClientId($cust->getId());
  452.                 $trans->setGlobalDiscount(0);
  453.                 $trans->setIva(21);
  454.                 $trans->setStatus(1);
  455.                 $trans->setIvaValue(floatval($order->total_tax));
  456.                 $trans->setPayed(floatval($order->total));
  457.                 $trans->setTotal(floatval($order->total));
  458.                 $trans->setUserId(-1);
  459.                 
  460.                 $em->persist($trans);
  461.                 $f $em->flush();
  462.                 if($f == null){
  463.                     //venta prods 
  464.                    
  465.                     foreach($order->line_items as $item){
  466.                         $prod $em->getRepository(Products::class)->findOneBy(array("sku"=>$item->sku));
  467.                         if($prod){
  468.                             $vp = new VentaProducts();
  469.                             $vp->setVentaId($trans->getId());
  470.                             $vp->setProductId($prod->getId());
  471.                             $vp->setQty($item->quantity);
  472.                             $vp->setUnitPrice($item->price);
  473.                             $vp->setIva($item->total_tax);
  474.                             $vp->setDiscount(0);
  475.                             if(floatval($item->subtotal)>floatval($item->total)){
  476.                                 $resta =  floatval($item->subtotal) - floatval($item->total);
  477.                                 $percent round(($resta floatval($item->subtotal)) * 100);
  478.                                 $unit_price floatval($item->total) / $item->quantity;
  479.                                 $vp->setUnitPrice($unit_price);
  480.                                 $vp->setDiscount($percent);
  481.                             }
  482.                             
  483.                             if($trans->getStatus() != ){
  484.                                 $this->discountLocalStock(json_decode(json_encode(array(
  485.                                     "id"=>$prod->getId(),
  486.                                     "qty"=>$item->quantity,
  487.                                     
  488.                                 ))));
  489.                             }
  490.                             $em->persist($vp);
  491.                             $f_p $em->flush();
  492.                             if($f_p != null){
  493.                                 $type 0;
  494.                                 $message "Error al añadir el producto ".$prod->getId();
  495.                                 break;
  496.                             }
  497.                             
  498.                         }
  499.                     }
  500.                     
  501.                     
  502.                 }
  503.                 
  504.             }/*else{
  505.                  $trans = $exists;
  506.                     $vp = $em->getRepository(VentaProducts::class)->findBy(array("ventaId"=>$trans->getId()));
  507.                     foreach($vp as $v){
  508.                         $em->remove($v);
  509.                         $em->flush();
  510.                     }
  511.                 foreach($order->line_items as $item){
  512.                     $prod = $em->getRepository(Products::class)->findOneBy(array("sku"=>$item->sku));
  513.                     if($prod){
  514.                         $vp = new VentaProducts();
  515.                         $vp->setVentaId($trans->getId());
  516.                         $vp->setProductId($prod->getId());
  517.                         $vp->setQty($item->quantity);
  518.                         $vp->setUnitPrice($item->price);
  519.                         $vp->setIva($item->total_tax);
  520.                         $vp->setDiscount(0);
  521.                         if(floatval($item->subtotal)>floatval($item->total)){
  522.                             $resta =  floatval($item->subtotal) - floatval($item->total);
  523.                             $percent = round(($resta / floatval($item->subtotal)) * 100);
  524.                             
  525.                             $unit_price = floatval($item->total) / $item->quantity;
  526.                             
  527.                             $vp->setUnitPrice($unit_price);
  528.                             $vp->setDiscount($percent);
  529.                         }
  530.                         if($trans->getStatus() != 3 ){
  531.                             $this->discountLocalStock(json_decode(json_encode(array(
  532.                                 "id"=>$prod->getId(),
  533.                                 "qty"=>$item->quantity,
  534.                             ))));
  535.                         }
  536.                         $em->persist($vp);
  537.                         $f_p = $em->flush();
  538.                         if($f_p != null){
  539.                             $type = 0;
  540.                             $message = "Error al añadir el producto ".$prod->getId();
  541.                             break;
  542.                         }
  543.                     }
  544.                 }
  545.                 
  546.             }*/
  547.         }
  548.         
  549.         dd($orders);
  550.     }
  551. }