src/Controller/MainController.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Bodega;
  4. use App\Entity\Categoria;
  5. use App\Entity\Checkout;
  6. use App\Entity\DocSiiVenta;
  7. use App\Entity\DocSiiCompra;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use App\Entity\InputCall;
  14. use App\Entity\SiiLog;
  15. use App\Entity\User;
  16. use App\Entity\Empresa;
  17. use App\Entity\ListaPrecio;
  18. use App\Entity\Producto;
  19. use App\Entity\Stock;
  20. use App\Entity\VersionProducto;
  21. use App\Entity\WebhookMultivende;
  22. /**
  23. * Main controller.
  24. * @Route("/main", name="mainController_")
  25. */
  26. class MainController extends AbstractController
  27. {
  28.     /**
  29.      * @Route("/dashboard", name="dashboard")
  30.      */
  31.     public function index(): Response
  32.     {
  33.         $em $this->getDoctrine()->getManager();
  34.         $user $this->getUser();
  35.         $empresaId $user->getEmpresaId();
  36.         $webhookRepository $em->getRepository(WebhookMultivende::class);
  37.         $inputDaysCalls $webhookRepository->countByDays($em1$empresaId);
  38.         $parameters['inputDaysCalls'] = count($inputDaysCalls);
  39.         return $this->render('main/index.html.twig'$parameters);
  40.     }
  41.     /**
  42.      * @Route("/report/getWeekData", name="getWeekData", options={"expose"=true})
  43.      */
  44.     public function getWeekData(){
  45.         // Set up required variables
  46.         $em $this->getDoctrine()->getManager();
  47.         $user $this->getUser();
  48.         $empresaId $user->getEmpresaId();
  49.         $countMonthRepository $em->getRepository(WebhookMultivende::class);
  50.         
  51.         $labelA '';
  52.         $labelB '';
  53.         $results $countMonthRepository->countWeekData($em$empresaId);
  54.         $x 0;
  55.         if(!$results){
  56.             $arrayLabel = [];
  57.             $arrayCount = [];
  58.         }
  59.         foreach($results as $data){
  60.             $arrayLabel[] = $data['monthDay'];
  61.             
  62.             $arrayCount[] = $data['dayLb'];
  63.             $x++;
  64.         }
  65.         $labels array_unique($arrayLabel);
  66.         $valores array_count_values($arrayCount);
  67.        
  68.         $arrayResponse['sucess'] =  true;
  69.         $arrayResponse['labelA'] =  array_values($labels);
  70.         $arrayResponse['labelB'] =  array_values($valores);
  71.         $returnResponse json_encode($arrayResponse);
  72.     
  73.         return new JsonResponse($returnResponse);
  74.         // return $arrayResponse;
  75.     }
  76.     /**
  77.      * @Route("/report/getMonthData", name="getMonthData", options={"expose"=true})
  78.      */
  79.     public function getMonthData(){
  80.         $em $this->getDoctrine()->getManager();
  81.         $user $this->getUser();
  82.         $empresaId $user->getEmpresaId();
  83.         $countMonthRepository $em->getRepository(WebhookMultivende::class);
  84.         
  85.         $labelA '';
  86.         $labelB '';
  87.         $arrayLabel = array();
  88.         $arrayCount = array();
  89.         
  90.         $results $countMonthRepository->countMonthData($em$empresaId);
  91.         // dump($results);die;
  92.         $x 0;
  93.         foreach($results as $data){
  94.             $arrayLabel[] = $data['FechaStr'];
  95.             
  96.             $arrayCount[] = $data['month'];
  97.             $x++;
  98.         }
  99.         // dump($arrayLabel);die;
  100.         $labels array_unique($arrayLabel);
  101.         $valores array_count_values($arrayCount);
  102.         // dump($valores);die;
  103.         $arrayResponse['sucess'] =  true;
  104.         $arrayResponse['labelA'] =  array_values($labels);
  105.         $arrayResponse['labelB'] =  array_values($valores);
  106.         $returnResponse json_encode($arrayResponse);
  107.     
  108.         return new JsonResponse($returnResponse);
  109.         // return $arrayResponse;
  110.     }
  111.     
  112.     public function getDTDashboardAdmin(Request $request$origin){
  113.         $em $this->getDoctrine()->getManager();
  114.         $user $this->getUser();
  115.         $empresaId $user->getEmpresaId();
  116.         $draw 0;
  117.         $start '';
  118.         $lenght '';
  119.         $search '';
  120.         $orders = [];
  121.         $columns = [];
  122.         if ($request->getMethod() == 'POST')
  123.         {
  124.             $draw intval($request->request->get('draw'));
  125.             $start $request->request->get('start'); //used for pagination
  126.             $length $request->request->get('length'); // used for pagination
  127.             $search $request->request->get('search'); //global search
  128.             $orders $request->request->get('order'); //contains the required order column, as well as the sorting direction (ascending or descending)
  129.             $columns $request->request->get('columns'); //columns as defined in the columnDefs
  130.             
  131.         }
  132.         else // If the request is not a POST one, die hard
  133.         {
  134.             die;
  135.         }
  136.         
  137.         foreach ($orders as $key => $order)
  138.         {
  139.             // Orders does not contain the name of the column, but its number,
  140.             // so add the name so we can handle it just like the $columns array
  141.             $orders[$key]['name'] = $columns[$order['column']]['name'];
  142.         }
  143.         $schemaManager $em->getConnection()->getSchemaManager();
  144.         switch($origin){
  145.             case 'main':
  146.                 $DBColumns $schemaManager->listTableColumns('webhook_multivende');
  147.                 break;
  148.             case 'product':
  149.                 $DBColumns $schemaManager->listTableColumns('version_producto');
  150.                 break;
  151.             case 'priceList':
  152.                 $DBColumns $schemaManager->listTableColumns('lista_precio');
  153.                 break;
  154.             case 'warehouse':
  155.                 $DBColumns $schemaManager->listTableColumns('bodega');
  156.                 break;
  157.             case 'category':
  158.                 $DBColumns $schemaManager->listTableColumns('categoria');
  159.                 break;
  160.             case 'stock':
  161.                 $DBColumns $schemaManager->listTableColumns('stock');
  162.                 break;
  163.             case 'requestUser':
  164.                 $DBColumns $schemaManager->listTableColumns('user');
  165.                 break;
  166.             case 'checkout':
  167.                 $DBColumns $schemaManager->listTableColumns('checkout');
  168.                 break;
  169.         }
  170.         $DBColumnsName = [];
  171.         foreach($DBColumns as $column){
  172.             $DBColumnsName[] = $column->getName();
  173.         }
  174.         $useCondition true;
  175.         switch($origin){
  176.             case 'main':
  177.                 $inputCallRepository $em->getRepository(WebhookMultivende::class);
  178.                 $tableName ='webhook_multivende';
  179.                 break;
  180.             case 'product':
  181.                 $inputCallRepository $em->getRepository(VersionProducto::class);
  182.                 $tableName ='version_producto';
  183.                 break;
  184.             case 'priceList':
  185.                 $inputCallRepository $em->getRepository(ListaPrecio::class);
  186.                 $tableName ='lista_precio';
  187.                 break;
  188.             case 'warehouse':
  189.                 $inputCallRepository $em->getRepository(Bodega::class);
  190.                 $tableName ='bodega';
  191.                 break;
  192.             case 'category':
  193.                 $inputCallRepository $em->getRepository(Categoria::class);
  194.                 $tableName ='categoria';
  195.                 break;
  196.             case 'stock':
  197.                 $inputCallRepository $em->getRepository(Stock::class);
  198.                 $tableName ='stock';
  199.                 break;
  200.             case 'requestUser':
  201.                 $inputCallRepository $em->getRepository(User::class);
  202.                 $tableName ='user';
  203.                 $useCondition false;
  204.                 break;
  205.             case 'checkout':
  206.                 $inputCallRepository $em->getRepository(Checkout::class);
  207.                 $tableName ='checkout';
  208.                 break;
  209.         }
  210.         if($useCondition){
  211.             switch($origin){
  212.                 case 'main':
  213.                 case 'product':
  214.                 case 'priceList':
  215.                 case 'warehouse':
  216.                 case 'category':
  217.                 case 'stock':
  218.                 case 'checkout':
  219.                     $otherConditions $tableName.".empresa_id =".$empresaId ;
  220.                     break;
  221.                 
  222.             }
  223.         }
  224.         else{
  225.             switch($origin){
  226.                 default:
  227.                     $otherConditions '1 = 1';
  228.                     break;
  229.             }
  230.         }
  231.         $results $inputCallRepository->getRequiredDTData($tableName$DBColumnsName$start$length$orders$search$columns$otherConditions);
  232.         // Returned objects are of type Town
  233.         $objects $results["results"];
  234.         // Get total number of objects
  235.         $total_objects_count $inputCallRepository->count2($tableName$otherConditions);
  236.         // Get total number of results
  237.         $selected_objects_count count($objects);
  238.         // Get total number of filtered data
  239.         $filtered_objects_count $results["countResult"];
  240.         // Construct response
  241.         $response '{
  242.             "draw": '.$draw.',
  243.             "recordsTotal": '.$total_objects_count.',
  244.             "recordsFiltered": '.$filtered_objects_count.',
  245.             "data": [';
  246.             
  247.         $i 0;
  248.         foreach ($objects as $key => $sucessPostData){
  249.             $response .= '["';
  250.             $j 0
  251.             $nbColumn count($columns);
  252.             foreach ($columns as $key => $column){
  253.                 // In all cases where something does not exist or went wrong, return -
  254.                 $responseTemp "-";
  255.                 if(in_array($column['name'] , $DBColumnsName)){
  256.                     $valSearch array_search($column['name'], $DBColumnsName);
  257.                     $DBColumnNameArray explode('_'$DBColumnsName[$valSearch]);
  258.                     if(count($DBColumnNameArray) <= 0){
  259.                         $methodVal ucfirst($DBColumnNameArray[0]);
  260.                     }
  261.                     else{
  262.                         $upersArray array_map('ucfirst'$DBColumnNameArray);
  263.                         $methodVal '';
  264.                         foreach($upersArray as $uperArray){
  265.                             $methodVal $methodVal $uperArray;
  266.                         }
  267.                     }
  268.                     $methodVal 'get'.$methodVal;
  269.                     $temp $sucessPostData->$methodVal();
  270.                     // if(in_array('at', $DBColumnNameArray)){
  271.                     //     $responseTemp = $temp->format('Y-m-d H:i:s');
  272.                     // }
  273.                     if($temp instanceof \DateTime ){
  274.                         $responseTemp $temp->format('Y-m-d H:i:s');
  275.                     }
  276.                     else{
  277.                         
  278.                         if(is_int($temp)){
  279.                             $responseTemp $temp;
  280.                         }
  281.                         else{
  282.                             if(is_array($temp)){
  283.                                 $responseTemp implode(' - ',$temp);
  284.                             }
  285.                             else{
  286.                                 if($temp == null){
  287.                                     $temp '';
  288.                                 }
  289.                                 $firstChar mb_substr($temp,  01);
  290.                                 if($firstChar == '{'){
  291.                                     $resultTemp json_decode($temptrue);
  292.                                     $responseTemp implode(',',$resultTemp);
  293.                                     // dump(implode(',',$resultTemp));
  294.                                 }
  295.                                 else{
  296.                                     $responseTemp $temp;
  297.                                 }
  298.                             }
  299.                         }
  300.                     }
  301.                 }
  302.                 switch($origin){
  303.                     case 'product':
  304.                         switch($column['name']){
  305.                             case 'producto_nombre':
  306.                             case 'impuesto':
  307.                             case 'concesionario':
  308.                                 $multivendeId $sucessPostData->getProductoPadreMultivendeId();
  309.                                 $productoEntity $em->getRepository(Producto::class)->findOneBy(array('productoMultivendeId' => $multivendeId));
  310.                                 switch($column['name']){
  311.                                     case 'producto_nombre':
  312.                                         $responseTemp $productoEntity->getProductoNombre();
  313.                                         break;
  314.                                     case 'impuesto':
  315.                                         $responseTemp $productoEntity->getImpuesto();
  316.                                         break;
  317.                                     case 'concesionario':
  318.                                         $responseTemp $productoEntity->getConcesionario();
  319.                                         break;
  320.                                 }
  321.                                 break;
  322.                             
  323.                         }
  324.                         if($column['name'] == 'producto_short_description'){
  325.                             // dump($responseTemp);
  326.                             $nonSalt htmlentities(str_replace(array("\r\n""\n""\r"), ' '$responseTemp));
  327.                             // $nonSalt = str_replace(["\n", "\r", "\t"], ' ', $responseTemp);
  328.                             // dump($nonSalt);die;
  329.                             $trimmed_description strlen($responseTemp) > 20 substr($responseTemp020) . "..." $responseTemp;
  330.                             $responseTemp "<div title='".$nonSalt."'>"$trimmed_description."</div>";
  331.                         }
  332.                         break;
  333.                     case 'checkout':
  334.                         if($column['name'] == 'pdf_url'){
  335.                             // $nonSalt = htmlentities(str_replace(array("\r\n", "\n", "\r"), ' ', $responseTemp));
  336.                             $responseTemp "<a href='".$responseTemp."' target='_blank'>PDF URL</a>";
  337.                         }
  338.                         break;
  339.                     // case 'requestSii':
  340.                     //     if($column['name'] == 'empresa_id'){
  341.                     //         $empresaEntity = $em->getRepository(Empresa::class)->find($responseTemp);
  342.                     //         $responseTemp = "<a href='{{ path('assocEmpView')}}'>".$empresaEntity->getEmpresaRut()."</a>";
  343.                     //     }
  344.                     //     if($column['name'] == 'operacion_id'){
  345.                     //         switch($responseTemp){
  346.                     //             case 1:
  347.                     //                 $responseTemp = 'VENTA';
  348.                     //                 break;
  349.                     //             case 2:
  350.                     //                 $responseTemp = 'COMPRA';
  351.                     //                 break;
  352.                     //         }
  353.                     //     }
  354.                     //     break;
  355.                     // case 'docCompra':
  356.                     // case 'docVenta':
  357.                     //     if($column['name'] == 'razon_social'){
  358.                     //         $responseTemp = str_replace(' ', '&nbsp;', $responseTemp);
  359.                     //     }
  360.                     //     break;
  361.                 }
  362.                 $response .= $responseTemp;
  363.                 if(++$j !== $nbColumn)
  364.                     $response .='","';
  365.             }
  366.             $response .= '"]';
  367.     
  368.             // Not on the last item
  369.             if(++$i !== $selected_objects_count)
  370.                 $response .= ',';
  371.         }
  372.         $response .= ']}';
  373.         // dump($response);die;
  374.         $returnResponse = new JsonResponse();
  375.         $returnResponse->setJson($response);
  376.     
  377.         return $returnResponse;
  378.         
  379.     }
  380.     public function deleteElement(Request $request){
  381.         $responseJson = new JsonResponse();
  382.         $dataSend['empresa_rut'] = $request->get('rutEmpresa');
  383.         $requestForward = new Request();
  384.         $requestForward->initialize(
  385.             $requestForward->query->all(),
  386.             $requestForward->request->all(),
  387.             $requestForward->attributes->all(),
  388.             $requestForward->cookies->all(),
  389.             $requestForward->files->all(),
  390.             $requestForward->server->all(),
  391.             json_encode($dataSend)
  392.         );
  393.         $response $this->forward('App\Controller\ApiSiiController::deleteEmpAssoc', [
  394.             'request' => $requestForward
  395.         ]);
  396.         $responseJson->setJson($response->getContent());
  397.         return $responseJson;
  398.     }
  399. }