<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Products;
use App\Entity\Clientes;
use App\Form\ClientesType;
use App\Entity\Ventas;
use App\Entity\VentaProducts;
/**
* Description of TPVController
*
* @author joseangelparra
*/
class TPVController extends AbstractController{
private $params;
public function __construct(ParameterBagInterface $params){
$this->session = new Session();
$this->params = $params;
}
public function index(Request $request){
$em = $this->getDoctrine()->getManager();
$cliente = new Clientes();
$form = $this->createForm(ClientesType::class,$cliente,[
'save_lbl'=>'Guardar'
]);
$start=false;
$fech = new \DateTime();
$fech->setTime(0, 0);
$cj = $em->getRepository(\App\Entity\CierreCaja::class)->findOneBy(array("fecha"=>$fech));
if(!$cj){
$start=true;
}
$form->handleRequest($request);
if($form->isSubmitted()){
$cliente->setDatecreated(new \DateTime());
$cliente->setIdWp(null);
$cliente->setStatus(1);
$em->persist($cliente);
$f = $em->flush();
if($f == null){
$type = 1;
$status = "Cliente añadido correctamente";
}else{
$type = 0;
$status = "Error al añadir el cliente";
}
$this->session->getFlashBag()->add('type', $type);
$this->session->getFlashBag()->add('status', $status);
return $this->redirectToRoute('tpv');
}
$clientes = $em->getRepository(Clientes::class)->findBy(array("status"=>1));
return $this->render('tpv/index.html.twig',array(
'form'=>$form->createView(),
'clientes'=>$clientes,
'start'=>$start
));
}
public function getVentaTable(){
return $this->render('tpv/ventaTable.html.twig',array(
));
}
public function getProductByCode(Request $request){
$em = $this->getDoctrine()->getManager();
$search = ($request->request->get("search")) ? $request->request->get("search") : '';
$product = $em->createQueryBuilder()->select('p')
->from('App\Entity\Products','p')
->where('(p.sku = :search or p.ean = :search) and p.status > 0')
->setParameter('search',$search)
->getQuery()
->getResult();
if(count($product)>0){
return new JsonResponse(array('empty_data'=>false,'product'=>$product[0]->to_Array()));
}else{
return new JsonResponse(array('empty_data'=>true));
}
}
public function getProductById(Request $request){
$em = $this->getDoctrine()->getManager();
$search = ($request->request->get("search")) ? $request->request->get("search") : '';
$product = $em->createQueryBuilder()->select('p')
->from('App\Entity\Products','p')
->where('p.id = :search')
->setParameter('search',$search)
->getQuery()
->getResult();
if(count($product)>0){
return new JsonResponse(array('empty_data'=>false,'product'=>$product[0]->to_Array()));
}else{
return new JsonResponse(array('empty_data'=>true));
}
}
public function renderLineTableTicket(Request $request){
$prod = $request->request->get("prod");
return $this->render('tpv/ticketLineTable.html.twig',array(
'prod'=>$prod
));
}
public function saveTransaction(Request $request){
$em = $this->getDoctrine()->getManager();
$info = $request->request->get("venta");
$venta = json_decode($info);
$type = 1;
$message = "Transacción guardada";
$vid = false;
if($venta){
if($venta->ticket_id == null){
$trans = new Ventas();
$trans->setDate(new \DateTime());
}else{
$trans = $em->getRepository(Ventas::class)->find($venta->ticket_id);
}
$trans->setClientId($venta->client_id);
$trans->setUserId($this->getUser()->getId());
if($venta->tipo == 1){
if(floatval($venta->pagado) >= floatval($venta->total_ticket)){
$trans->setStatus(1); //total
}else{
$trans->setStatus(2); //parcial
}
}else{
$trans->setStatus(3); //aparcado
}
$trans->setSubtotal($venta->subtotal_ticket);
$iva_value = floatval($venta->total_ticket) - floatval($venta->subtotal_ticket);
$trans->setIva(21);
$trans->setIvaValue($iva_value);
$trans->setTotal($venta->total_ticket);
$trans->setDateModif(new \DateTime());
$trans->setPaymentMethod(intval($venta->metodo_pago));
$trans->setOrderWpId(null);
$trans->setExtraInfo($info);
$trans->setGlobalDiscount(0);
$trans->setPayed($venta->pagado);
$em->persist($trans);
$f = $em->flush();
if($f == null){
$vid = $trans->getId();
//eliminamos los productos anteriores
$prods = $em->getRepository(VentaProducts::class)->findBy(array("ventaId"=>$trans->getId()));
if($prods){
foreach($prods as $p){
$em->remove($p);
$em->flush();
}
}
//añadimos los productos
$error=0;
foreach($venta->products as $prod){
$vp = new VentaProducts();
$vp->setVentaId($trans->getId());
$vp->setProductId($prod->id);
$vp->setQty($prod->qty);
$vp->setUnitPrice($prod->unit_price);
$vp->setIva($prod->iva);
$vp->setDiscount($prod->discount);
$em->persist($vp);
$f_p = $em->flush();
if($f_p != null){
$type = 0;
$message = "Error al añadir el producto ".$prod->id;
break;
}
//descontamos el stock si es totalizado
if($trans->getStatus() != 3 ){
$this->discountLocalStock($prod);
}
}
}else{
$type=0;
$message = "Error al guardar la transacción";
}
}else{
$type=0;
$message = "Error al leer la venta";
}
$this->session->getFlashBag()->add('type', $type);
$this->session->getFlashBag()->add('status', $message);
return $this->redirectToRoute('tpv', array("id"=>$vid));
}
public function generarDevolucion(Request $request){
$em = $this->getDoctrine()->getManager();
$venta_id = $request->get("id");
$type = 1;
$message = "Devolución realizada";
$venta = $em->getRepository(Ventas::class)->find($venta_id);
//sets
$trans = new Ventas();
$trans->setDate(new \DateTime());
$trans->setClientId($venta->getclientId());
$trans->setUserId($this->getUser()->getId());
$trans->setStatus(4); //devolución
$trans->setSubtotal(($venta->getSubtotal() * -1));
$trans->setIva(21);
$trans->setIvaValue($venta->getIvaValue());
$trans->setTotal($venta->getTotal()*-1);
$trans->setDateModif(new \DateTime());
$trans->setPaymentMethod($venta->getPaymentMethod());
$trans->setOrderWpId($venta->getOrderWpId());
$trans->setExtraInfo($venta->getExtraInfo());
$trans->setGlobalDiscount(0);
$trans->setPayed($venta->getPayed()*-1);
$em->persist($trans);
$f = $em->flush();
if($f == null){
//eliminamos los productos anteriores
$prods = $em->getRepository(VentaProducts::class)->findBy(array("ventaId"=>$venta_id));
if($prods){
//añadimos los productos
$error=0;
foreach($prods as $prod){
$vp = new VentaProducts();
$vp->setVentaId($trans->getId());
$vp->setProductId($prod->getProductId());
$vp->setQty($prod->getQty());
$vp->setUnitPrice($prod->getUnitPrice());
$vp->setIva($prod->getIva());
$vp->setDiscount($prod->getDiscount());
$em->persist($vp);
$f_p = $em->flush();
if($f_p != null){
$type = 0;
$message = "Error al añadir el producto ".$prod->getId();
break;
}
//descontamos el stock si es totalizado
if($trans->getStatus() != 3 ){
$this->restoreLocalStock($prod);
}
}
}
}else{
$type=0;
$message = "Error al guardar la transacción";
}
$this->session->getFlashBag()->add('type', $type);
$this->session->getFlashBag()->add('status', $message);
return $this->redirectToRoute('ventas');
}
public function discountLocalStock($prod){
$em = $this->getDoctrine()->getManager();
$producto = $em->getRepository(Products::class)->find($prod->id);
$c_stock = $producto->getStoreStock();
$stock = $c_stock - $prod->qty;
$g_stock = $producto->getGlobalStock();
$stock_g = $g_stock - $prod->qty;
$producto->setStoreStock($stock);
$producto->setGlobalStock($stock_g);
$em->persist($producto);
$f = $em->flush();
if($f == null){
return true;
}else{
return false;
}
}
public function restoreLocalStock($prod){
$em = $this->getDoctrine()->getManager();
$producto = $em->getRepository(Products::class)->find($prod->getProductId());
$c_stock = $producto->getStoreStock();
$stock = $c_stock + $prod->getQty();
$g_stock = $producto->getGlobalStock();
$stock_g = $g_stock + $prod->getQty();
$producto->setStoreStock($stock);
$producto->setGlobalStock($stock_g);
$em->persist($producto);
$f = $em->flush();
if($f == null){
return true;
}else{
return false;
}
}
public function printTicket(Request $request){
$em = $this->getDoctrine()->getManager();
$ticket_id = $request->request->get("id");
$venta = $em->getRepository(Ventas::class)->find($ticket_id);
$prods = $em->getRepository(VentaProducts::class)->findBy(array('ventaId'=>$ticket_id));
$client = $em->getRepository(Clientes::class)->find($venta->getClientId());
return $this->render('tpv/ticket.html.twig',array(
'venta'=>$venta,
'prods'=>$prods,
'client'=>$client,
'info'=>json_decode($venta->getExtraInfo())
));
}
public function printTicketRegalo(Request $request){
$em = $this->getDoctrine()->getManager();
$ticket_id = $request->request->get("id");
$venta = $em->getRepository(Ventas::class)->find($ticket_id);
$prods = $em->getRepository(VentaProducts::class)->findBy(array('ventaId'=>$ticket_id));
$client = $em->getRepository(Clientes::class)->find($venta->getClientId());
return $this->render('tpv/ticketRegalo.html.twig',array(
'venta'=>$venta,
'prods'=>$prods,
'client'=>$client,
'info'=>json_decode($venta->getExtraInfo())
));
}
public function loadAparquets(Request $request){
$em = $this->getDoctrine()->getManager();
$aparcados = $em->getRepository(Ventas::class)->findBy(array('status'=>3));
return $this->render('tpv/aparcados.html.twig',array(
'ventas'=>$aparcados
));
}
public function loadPartials(Request $request){
$em = $this->getDoctrine()->getManager();
$aparcados = $em->getRepository(Ventas::class)->findBy(array('status'=>2));
return $this->render('tpv/parciales.html.twig',array(
'ventas'=>$aparcados
));
}
public function deleteVenta(Request $request){
$id = $request->request->get("id");
$em = $this->getDoctrine()->getManager();
$venta = $em->getRepository(Ventas::class)->find($id);
$ok=false;
if($venta){
$em->remove($venta);
$f = $em->flush();
if($f==null){
$ok=true;
}
}
return new JsonResponse(array("ok"=>$ok));
}
public function newProductManual(){
return $this->render('products/new_product_ajax.html.twig',array(
));
}
}