src/Controller/Admin/ProductUploadController.php line 188

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpFoundation\Session\Session;
  7. use Symfony\Component\Process\Process;
  8. use App\DependencyInjection\ToolsUsers;
  9. class ProductUploadController extends AbstractController {
  10.     use \App\Traits\Database;
  11.     use \App\Traits\DateUtils;
  12.     use \App\Traits\Rights;
  13.     public function index(Request $request) {
  14.         $session=new Session();
  15.         $translate=$this->container->get('site.translate')->get();
  16.         $conn=$this->getDoctrine()->getConnection();
  17.         if(!$session->get('admin')) return $this->redirect($this->generateUrl('admin_homepage'));
  18.         $is_admin=$this->checkAdmin($session$conn);
  19.         if(!$is_admin) return $this->redirect($this->generateUrl('admin_homepage'));
  20.         return $this->render('Admin/ProductUpload/index.html.twig', array('is_admin'=>$is_admin'translate'=>$translate));
  21.     }
  22.     public function controlFile(Request $request$id) {
  23.         $session=new Session();
  24.         $translate=$this->container->get('site.translate')->get();
  25.         $conn=$this->getDoctrine()->getConnection();
  26.         if(!$session->get('admin')) return $this->redirect($this->generateUrl('admin_homepage'));
  27.         $is_admin=$this->checkAdmin($session$conn);
  28.         if(!$is_admin) return $this->redirect($this->generateUrl('admin_homepage'));
  29.         return $this->render('Admin/ProductUpload/controlFile.html.twig', array('id'=>$id'is_admin'=>$is_admin'translate'=>$translate));
  30.     }
  31.     public function controlFileList(Request $request$id) {
  32.         $session=$this->get('session');
  33.         $translate=$this->container->get('site.translate')->get();
  34.         $conn=$this->getDoctrine()->getConnection();
  35.         if(!$this->checkConnected($session)) return $this->render('Admin/Default/not-connected-single.html.twig', array('translate'=>$translate));
  36.         $is_admin=$this->checkAdmin($session$conn);
  37.         if(!$is_admin) return $this->render('Admin/Default/no-rights-single.html.twig', array('translate'=>$translate));
  38.         $where='';$page=1;$limit=10;
  39.         if(isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page']) $page=$_GET['page'];
  40.         if(isset($_GET['limit']) && is_numeric($_GET['limit']) && $_GET['limit']) $limit=$_GET['limit'];
  41.         $start=($page-1)*$limit;
  42.         $dateNow=(new \DateTime())->format('Y-m-d H:i:s');
  43.         $count=$this->fetchOne($conn"SELECT COUNT(DISTINCT pud.ref)
  44.             FROM product_upload_data pud
  45.             INNER JOIN product_upload_file puf ON puf.id=pud.product_upload_file_id
  46.             WHERE puf.product_upload_id=$id $where");
  47.         $items=$this->fetchAll($conn"SELECT pud.ref, 0 qty, MAX(pud.price) price
  48.             FROM product_upload_data pud
  49.             INNER JOIN product_upload_file puf ON puf.id=pud.product_upload_file_id
  50.             WHERE puf.product_upload_id=$id $where
  51.             GROUP BY pud.ref
  52.             ORDER BY pud.ref ASC
  53.             LIMIT $start,$limit");
  54.         foreach($items as $k=>$v) {
  55.             $qte=$this->fetch($conn'SELECT SUM(qty) qty FROM product_upload_data WHERE LOWER(ref)="'.strtolower($v['ref']).'" AND qty>0 AND product_upload_id='.$id);
  56.             if($qte$items[$k]['qty']=$qte['qty'];
  57.         }
  58.         return $this->render('Admin/ProductUpload/controlFileList.html.twig', array('items'=>$items'count'=>$count'page'=>$page'limit'=>$limit'is_admin'=>$is_admin'translate'=>$translate));
  59.     }
  60.     public function controlFileExport(Request $request$id) {
  61.         $session=$this->get('session');
  62.         $translate=$this->container->get('site.translate')->get();
  63.         $conn=$this->getDoctrine()->getConnection();
  64.         if(!$this->checkConnected($session)) return $this->render('Admin/Default/not-connected-single.html.twig', array('translate'=>$translate));
  65.         $is_admin=$this->checkAdmin($session$conn);
  66.         if(!$is_admin) return $this->render('Admin/Default/no-rights-single.html.twig', array('translate'=>$translate));
  67.         $dateNow=(new \DateTime())->format('Y-m-d H:i:s');
  68.         $items=$this->fetchAll($conn"SELECT LOWER(pud.ref) ref, 0 qty, MAX(pud.price) price
  69.             FROM product_upload_data pud
  70.             WHERE pud.product_upload_id=$id
  71.                 AND pud.product_upload_file_id IN (SELECT puf.id FROM product_upload_file puf WHERE puf.status=2 AND puf.id=pud.product_upload_file_id)
  72.             GROUP BY pud.ref
  73.             ORDER BY pud.ref ASC");
  74.         $now gmdate("D, d M Y H:i:s");
  75.         header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
  76.         header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
  77.         header("Last-Modified: {$now} GMT");
  78.         header("Content-Type: application/force-download");
  79.         header("Content-Type: application/octet-stream");
  80.         header("Content-Type: application/download");            
  81.         header('Content-Disposition: attachment; filename="Controle-'.$id.'.csv";');
  82.         header("Content-Transfer-Encoding: binary");
  83.         $output fopen("php://output""w");
  84.         fputcsv($output, array('Ref''Qte''Prix'), ';');
  85.         foreach($items as $k=>$v) {
  86.             $qte=$this->fetch($conn'SELECT SUM(pud.qty) qty
  87.                 FROM product_upload_data pud
  88.                 WHERE pud.product_upload_id='.$id.' AND LOWER(pud.ref)="'.strtolower($v['ref']).'" AND pud.qty>0
  89.                     AND pud.product_upload_file_id IN (SELECT puf.id FROM product_upload_file puf WHERE puf.status=2 AND puf.id=pud.product_upload_file_id)');
  90.             if($qte$v['qty']=$qte['qty'];
  91.             fputcsv($output$v';');
  92.         }
  93.         fclose($output);
  94.         die;
  95.     }
  96.     public function checkCSV(Request $request$id$company_id=0) {
  97.         $session=new Session();
  98.         $translate=$this->container->get('site.translate')->get();
  99.         $conn=$this->getDoctrine()->getConnection();
  100.         if(!$session->get('admin')) return $this->redirect($this->generateUrl('admin_homepage'));
  101.         $is_admin=$this->checkAdmin($session$conn);
  102.         if(!$is_admin) return $this->redirect($this->generateUrl('admin_homepage'));
  103.         return $this->render('Admin/ProductUpload/checkCSV.html.twig', array('id'=>$id'company_id'=>$company_id'is_admin'=>$is_admin'translate'=>$translate));
  104.     }
  105.     public function checkCSVList(Request $request$id$company_id=0) {
  106.         $session=$this->get('session');
  107.         $translate=$this->container->get('site.translate')->get();
  108.         $conn=$this->getDoctrine()->getConnection();
  109.         if(!$this->checkConnected($session)) return $this->render('Admin/Default/not-connected-single.html.twig', array('translate'=>$translate));
  110.         $is_admin=$this->checkAdmin($session$conn);
  111.         if(!$is_admin) return $this->render('Admin/Default/no-rights-single.html.twig', array('translate'=>$translate));
  112.         $where='';$page=1;$limit=10;
  113.         if(isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page']) $page=$_GET['page'];
  114.         if(isset($_GET['limit']) && is_numeric($_GET['limit']) && $_GET['limit']) $limit=$_GET['limit'];
  115.         if($company_id$where.=' AND puf.company_id='.$company_id;
  116.         $start=($page-1)*$limit;
  117.         $dateNow=(new \DateTime())->format('Y-m-d H:i:s');
  118.         $count=$this->fetchOne($conn"SELECT COUNT(pud.product_upload_file_id)
  119.             FROM product_upload_data pud
  120.             INNER JOIN product_upload_file puf ON puf.id=pud.product_upload_file_id
  121.             INNER JOIN company c ON c.id=puf.company_id
  122.             WHERE puf.product_upload_id=$id $where");
  123.         $items=$this->fetchAll($conn"SELECT puf.*, pud.ref, pud.qty, pud.price, c.name company_name
  124.             FROM product_upload_data pud
  125.             INNER JOIN product_upload_file puf ON puf.id=pud.product_upload_file_id
  126.             INNER JOIN company c ON c.id=puf.company_id
  127.             WHERE puf.product_upload_id=$id $where
  128.             ORDER BY pud.ref ASC
  129.             LIMIT $start,$limit");
  130.         return $this->render('Admin/ProductUpload/checkCSVList.html.twig', array('items'=>$items'count'=>$count'page'=>$page'limit'=>$limit'is_admin'=>$is_admin'translate'=>$translate));
  131.     }
  132.     public function checkCSVExport(Request $request$id$company_id=0) {
  133.         $session=$this->get('session');
  134.         $translate=$this->container->get('site.translate')->get();
  135.         $conn=$this->getDoctrine()->getConnection();
  136.         if(!$this->checkConnected($session)) return $this->render('Admin/Default/not-connected-single.html.twig', array('translate'=>$translate));
  137.         $is_admin=$this->checkAdmin($session$conn);
  138.         if(!$is_admin) return $this->render('Admin/Default/no-rights-single.html.twig', array('translate'=>$translate));
  139.         $where='';
  140.         if($company_id$where.=' AND puf.company_id='.$company_id;
  141.         $dateNow=(new \DateTime())->format('Y-m-d H:i:s');
  142.         $items=$this->fetchAll($conn"SELECT c.name company_name, pud.ref, pud.qty, pud.price
  143.             FROM product_upload_data pud
  144.             INNER JOIN product_upload_file puf ON puf.id=pud.product_upload_file_id
  145.             INNER JOIN company c ON c.id=puf.company_id
  146.             WHERE product_upload_id=$id $where
  147.             ORDER BY pud.ref ASC");
  148.         $now gmdate("D, d M Y H:i:s");
  149.         header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
  150.         header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
  151.         header("Last-Modified: {$now} GMT");
  152.         header("Content-Type: application/force-download");
  153.         header("Content-Type: application/octet-stream");
  154.         header("Content-Type: application/download");            
  155.         if(!$company_idheader('Content-Disposition: attachment; filename="'.$id.'.csv";'); 
  156.         else header('Content-Disposition: attachment; filename="'.$id.'-'.$company_id.'.csv";');
  157.         header("Content-Transfer-Encoding: binary");
  158.         $output fopen("php://output""w");  
  159.         fputcsv($output, array('Libraire''Ref''Qte''Prix'), ';');  
  160.         foreach($items as $v) {
  161.             fputcsv($output$v';');
  162.         }
  163.         fclose($output);  
  164.         die;
  165.     }
  166.     public function one(Request $request$id) {
  167.         $session=$this->get('session');
  168.         $translate=$this->container->get('site.translate')->get();
  169.         $conn=$this->getDoctrine()->getConnection();
  170.         if(!$this->checkConnected($session)) return $this->render('Admin/Default/not-connected-single.html.twig', array('translate'=>$translate));
  171.         $is_admin=$this->checkAdmin($session$conn);
  172.         if($is_admin!=2) return $this->render('Admin/Default/no-rights-single.html.twig', array('translate'=>$translate));
  173.         $item=$this->fetch($conn"SELECT pu.*
  174.             FROM product_upload pu
  175.             WHERE pu.id=$id");
  176.         //$item['companies']=$this->fetchAll($conn, "SELECT * FROM company WHERE id IN (SELECT company_id FROM staff WHERE id=$id)");
  177.         return $this->render('Admin/ProductUpload/one.html.twig', array('item'=>$item'translate'=>$translate));
  178.     }
  179.     public function list(Request $request) {
  180.         $session=$this->get('session');
  181.         $translate=$this->container->get('site.translate')->get();
  182.         $conn=$this->getDoctrine()->getConnection();
  183.         if(!$this->checkConnected($session)) return $this->render('Admin/Default/not-connected-single.html.twig', array('translate'=>$translate));
  184.         $is_admin=$this->checkAdmin($session$conn);
  185.         if(!$is_admin) return $this->render('Admin/Default/no-rights-single.html.twig', array('translate'=>$translate));
  186.         $where='';$page=1;$limit=10;
  187.         if(isset($_GET['status']) && is_numeric($_GET['status']) && $_GET['status']) $where.=' AND pu.status="'.$_GET['status'].'"';
  188.         if(isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page']) $page=$_GET['page'];
  189.         if(isset($_GET['limit']) && is_numeric($_GET['limit']) && $_GET['limit']) $limit=$_GET['limit'];
  190.         $start=($page-1)*$limit;
  191.         $dateNow=(new \DateTime())->format('Y-m-d H:i:s');
  192.         if($is_admin==2) {
  193.             $count=$this->fetchOne($conn"SELECT COUNT(DISTINCT pu.id)
  194.                 FROM product_upload pu
  195.                 WHERE 1=1 $where");
  196.             $items=$this->fetchAll($conn"SELECT pu.*
  197.                 FROM product_upload pu
  198.                 WHERE 1=1 $where
  199.                 ORDER BY pu.creation_date DESC
  200.             LIMIT $start,$limit"); //pu.start_date DESC, pu.end_date DESC, 
  201.         }
  202.         elseif($is_admin==1) {
  203.             $count=$this->fetchOne($conn"SELECT COUNT(DISTINCT pu.id)
  204.                 FROM product_upload pu
  205.                 WHERE pu.start_date<='$dateNow' AND (pu.end_date IS NULL OR pu.end_date>='$dateNow') $where");
  206.             $items=$this->fetchAll($conn"SELECT pu.*
  207.                 FROM product_upload pu
  208.                 WHERE pu.start_date<='$dateNow' AND (pu.end_date IS NULL OR pu.end_date>='$dateNow') $where
  209.                 ORDER BY pu.creation_date DESC
  210.             LIMIT $start,$limit");//pu.start_date DESC, pu.end_date DESC
  211.         }
  212.         $companies=$this->getCompanies($session$conn);
  213.         foreach($items as $k=>$v) {
  214.             foreach($companies as $k2=>$v2) {
  215.                 $exists=$this->fetchOne($conn'SELECT count(id) FROM product_upload_file WHERE status=2 AND product_upload_id="'.$v['id'].'" AND company_id="'.$v2['id'].'"');
  216.                 if($exists) {
  217.                     $companies[$k2]['msg']=1;
  218.                     $exists=$this->fetchOne($conn'SELECT count(id) FROM product_upload_file WHERE status=2 AND end_script_date IS NOT NULL AND product_upload_id="'.$v['id'].'" AND company_id="'.$v2['id'].'"');
  219.                     if($exists$companies[$k2]['msg']=2;
  220.                 }
  221.                 else $companies[$k2]['msg']=0;
  222.             }
  223.             if(!$v['end_date'] || (new \DateTime($v['end_date'])>(new \DateTime()))) $items[$k]['showCSV']=1;
  224.             else $items[$k]['showCSV']="0";
  225.             $items[$k]['companies']=$companies;
  226.         }
  227.         return $this->render('Admin/ProductUpload/list.html.twig', array('items'=>$items'count'=>$count'page'=>$page'limit'=>$limit'is_admin'=>$is_admin'translate'=>$translate));
  228.     }
  229.     public function add(Request $request) {
  230.         $session=$this->get('session');
  231.         $translate=$this->container->get('site.translate')->get();
  232.         $conn=$this->getDoctrine()->getConnection();
  233.         if(!$this->checkConnected($session)) return $this->render('Admin/Default/not-connected-single.html.twig', array('translate'=>$translate));
  234.         if($this->checkAdmin($session$conn)!=2) return $this->render('Admin/Default/no-rights-single.html.twig', array('translate'=>$translate));
  235.         $genders=$this->fetchAll($conn'SELECT * FROM gender');
  236.         if(isset($_POST['ok'])) {
  237.             $errors=array();$message='';
  238.             $name=addslashes(trim($_POST['name']));
  239.             $start_date=$this->toDateHourSQL(trim($_POST['start_date']));
  240.             $end_date=$this->toDateHourSQL(trim($_POST['end_date']));
  241.             if(strlen($name)<2$errors['name']='Veuillez entrer une désignation correcte (min. 2cars)';
  242.             if(!$start_date$errors['start_date']='Veuillez entrer une date de début';
  243.             if(!$end_date$errors['end_date']='Veuillez entrer une date de fin';
  244.             if(count($errors)>|| $message) return new JsonResponse(array('s'=>0'e'=>$errors'm'=>$message));
  245.             try {
  246.                 $dateNow=(new \DateTime())->format('Y-m-d H:i:s');
  247.                 $this->query($conn'START TRANSACTION');
  248.                 $this->query($conn'INSERT INTO product_upload (name, start_date, end_date, creation_date) VALUES
  249.                     ("'.$name.'", "'.$start_date.'", "'.$end_date.'", "'.$dateNow.'")');
  250.                 $this->query($conn'COMMIT');
  251.             } catch(\Exception $e) {
  252.                 $this->query($conn'ROLLBACK');
  253.                 return new JsonResponse(array('s'=>0'e'=>$errors'm'=>'Erreur de transaction'));
  254.             }
  255.             return new JsonResponse(array('s'=>1));
  256.         }
  257.         return $this->render('Admin/ProductUpload/add.html.twig', array('genders'=>$genders'translate'=>$translate));
  258.     }
  259.     public function edit(Request $request$id) {
  260.         $session=$this->get('session');
  261.         $translate=$this->container->get('site.translate')->get();
  262.         $conn=$this->getDoctrine()->getConnection();
  263.         if(!$this->checkConnected($session)) return $this->render('Admin/Default/not-connected-single.html.twig', array('translate'=>$translate));
  264.         if($this->checkAdmin($session$conn)!=2) return $this->render('Admin/Default/no-rights-single.html.twig', array('translate'=>$translate));
  265.         $item=$this->fetch($conn'SELECT * FROM product_upload WHERE id="'.$id.'"');
  266.         if(isset($_POST['ok'])) {
  267.             $errors=array();$message='';
  268.             $name=addslashes(trim($_POST['name']));
  269.             $start_date=$this->toDateHourSQL(trim($_POST['start_date']));
  270.             $end_date=$this->toDateHourSQL(trim($_POST['end_date']));
  271.             if(strlen($name)<2$errors['name']='Veuillez entrer une désignation correcte (min. 2cars)';
  272.             if(!$start_date$errors['start_date']='Veuillez entrer une date de début';
  273.             if(!$end_date$errors['end_date']='Veuillez entrer une date de fin';
  274.             if(count($errors)>|| $message) return new JsonResponse(array('s'=>0'e'=>$errors'm'=>$message));
  275.             try {
  276.                 $this->query($conn'START TRANSACTION');
  277.                 $this->query($conn'UPDATE product_upload SET name="'.$name.'", start_date="'.$start_date.'", end_date="'.$end_date.'" WHERE id="'.$id.'"');
  278.                 $this->query($conn'COMMIT');
  279.             } catch(\Exception $e) {
  280.                 $this->query($conn'ROLLBACK');
  281.                 return new JsonResponse(array('s'=>0'e'=>$errors'm'=>'Erreur de transaction'));
  282.             }
  283.             return new JsonResponse(array('s'=>1));
  284.         }
  285.         return $this->render('Admin/ProductUpload/edit.html.twig', array('item'=>$item'translate'=>$translate));
  286.     }
  287.     private function inCompany($id$companies) {
  288.         foreach($companies as $v) {
  289.             if($v['id']==$id) return true;
  290.         }
  291.         return false;
  292.     }
  293.     public function uploadCSV(Request $request$id) {
  294.         $session=$this->get('session');
  295.         $translate=$this->container->get('site.translate')->get();
  296.         $conn=$this->getDoctrine()->getConnection();
  297.         if(!$this->checkConnected($session)) return $this->render('Admin/Default/not-connected-single.html.twig', array('translate'=>$translate));
  298.         if(!$this->checkAdmin($session$conn)) return $this->render('Admin/Default/no-rights-single.html.twig', array('translate'=>$translate));
  299.         $companies=$this->getCompanies($session$conn);
  300.         if(count($companies)==0) return $this->render('Admin/Default/no-rights-single.html.twig', array('translate'=>$translate));
  301.         $item=$this->fetch($conn'SELECT * FROM product_upload WHERE id="'.$id.'"');
  302.         foreach($companies as $k2=>$v2) {
  303.             $exists=$this->fetchOne($conn'SELECT count(id) FROM product_upload_file WHERE status=2 AND product_upload_id="'.$item['id'].'" AND company_id="'.$v2['id'].'"');
  304.             if($exists) {
  305.                 $companies[$k2]['msg']=1;
  306.                 $exists=$this->fetchOne($conn'SELECT count(id) FROM product_upload_file WHERE status=2 AND end_script_date IS NOT NULL AND product_upload_id="'.$item['id'].'" AND company_id="'.$v2['id'].'"');
  307.                 if($exists$companies[$k2]['msg']=2;
  308.             }
  309.             else $companies[$k2]['msg']=0;
  310.         }
  311.         if(isset($_POST['ok'])) {
  312.             $errors=array();$message='';
  313.             $company_id=$_POST['company_id'];
  314.             $uploadCode=$_POST['uploadCode'];
  315.             if(!is_numeric($company_id) || !$company_id || !$this->inCompany($company_id$companies)) $errors['company_id']='Vous n\'avez pas de droits suffisants pour importer ce fichier';
  316.             if(!is_numeric($uploadCode) || !$uploadCode$message='Erreur';
  317.             if(count($errors)>|| $message) return new JsonResponse(array('s'=>0'e'=>$errors'm'=>$message));
  318.             try {
  319.                 $dateNow=(new \DateTime())->format('Y-m-d H:i:s');
  320.                 $this->query($conn'START TRANSACTION');
  321.                 $this->query($conn'UPDATE product_upload_file SET status=5 WHERE product_upload_id="'.$id.'" AND company_id="'.$company_id.'"');
  322.                 $this->query($conn'UPDATE product_upload_file SET company_id="'.$company_id.'", status=2 WHERE id="'.$uploadCode.'" AND user_id="'.$session->get('admin').'"');
  323.                 $this->query($conn'COMMIT');
  324.                 exec('nohup php /var/www/stock.mylibrairie.ma/web/app/public/uploadCSV.php '.$uploadCode.' '.$session->get('admin').' > /dev/null 2>&1 &');
  325.             } catch(\Exception $e) {
  326.                 $this->query($conn'ROLLBACK');
  327.                 return new JsonResponse(array('s'=>0'e'=>$errors'm'=>'Erreur de transaction'));
  328.             }
  329.             return new JsonResponse(array('s'=>1));
  330.         }
  331.         return $this->render('Admin/ProductUpload/uploadCSV.html.twig', array('item'=>$item'user_id'=>$session->get('admin'), 'password'=>$session->get('admin'), 'companies'=>$companies'translate'=>$translate));
  332.     }
  333.     public function activate(Request $request$id$status) {
  334.         $session=$this->get('session');
  335.         $conn=$this->getDoctrine()->getConnection();
  336.         $translate=$this->container->get('site.translate')->get();
  337.         if(!$this->checkConnected($session)) return new JsonResponse(array('s'=>"0"));
  338.         if($this->checkAdmin($session$conn)!=2) return new JsonResponse(array('s'=>"0"));
  339.         $users=$this->query($conn'UPDATE product_upload SET status="'.$status.'" WHERE id="'.$id.'"');
  340.         return new JsonResponse(array('s'=>1));
  341.     }
  342. }
  343. ?>