src/Controller/ProductController.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Lot;
  4. use App\Entity\Product;
  5. use App\Form\ProductType;
  6. use App\Repository\ConfigurationRepository;
  7. use App\Repository\LotRepository;
  8. use App\Repository\PieceLineRepository;
  9. use App\Repository\ProductRepository;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Knp\Component\Pager\PaginatorInterface;
  15. use App\Form\ProductSearchType;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  17. use Spipu\Html2Pdf\Html2Pdf;
  18. use Aspera\Spreadsheet\XLSX\Reader;
  19. /**
  20.  * @Route("/product")
  21.  */
  22. class ProductController extends AbstractController
  23. {
  24.     /**
  25.      * @Route("/", name="product_index", methods={"GET|POST"})
  26.      * @Security("is_granted('ROLE_SUPER_ADMIN')")
  27.      */
  28.     public function index(ProductRepository $productRepositoryRequest $requestPaginatorInterface $paginator): Response
  29.     {
  30.         $form $this->createForm(ProductSearchType::class);
  31.         $form->handleRequest($request);
  32.         if ($form->isSubmitted()) {
  33.             $url $this->buildSearchUrl($request->request->all());
  34.             if (!empty($url)) {
  35.                 return $this->redirectToRoute('product_index'$url);
  36.             }
  37.         }
  38.         $products $paginator->paginate(
  39.             $productRepository->MyFindAll($request->query->all()), /* query NOT result */
  40.             $request->query->getInt('page'1)/* page number */20/* limit per page */
  41.         );
  42.         return $this->render('product/index.html.twig', [
  43.             'products' => $products,
  44.             'form' => $form->createView(),
  45.             'data' => $request->query->all()
  46.         ]);
  47.     }
  48.     /**
  49.      * @Route("/recup_stock_lot", name="recup_stock_lot", methods={"GET|POST"})
  50.      * @Security("is_granted('ROLE_SUPER_ADMIN')")
  51.      */
  52.     public function recupStockLot(ProductRepository $productRepositoryLotRepository $lotRepository): Response
  53.     {
  54.         $products $productRepository->findAll();
  55.         foreach ($products as $product) {
  56.             $lot = new Lot();
  57.             $lot->setProduct($product);
  58.             $lot->setStock($product->getStock());
  59.             $lot->setDateExpiration($product->getDateExpiration());
  60.             $entityManager $this->getDoctrine()->getManager();
  61.             $entityManager->persist($lot);
  62.             $entityManager->flush();
  63.         }
  64.         return new Response('ok');
  65.     }
  66.     /**
  67.      * @Route("/recup_lot_ligne_vente", name="recup_lot_ligne_vente", methods={"GET|POST"})
  68.      * @Security("is_granted('ROLE_USER')")
  69.      */
  70.     public function recupLotLigneVente(ProductRepository $productRepositoryLotRepository $lotRepositoryPieceLineRepository $pieceLineRepository): Response
  71.     {
  72.         $pieceLines $pieceLineRepository->findAll();
  73.         foreach ($pieceLines as $pieceLine) {
  74.             $pieceLine->setLot($pieceLine->getProduct()->getLots()[0]);
  75.             $entityManager $this->getDoctrine()->getManager();
  76.             $entityManager->persist($pieceLine);
  77.             $entityManager->flush();
  78.         }
  79.         return new Response('ok');
  80.     }
  81.     /**
  82.      * @Route("/imprimer_produit", name="product_print_search", methods={"GET|POST"})
  83.      * @Security("is_granted('ROLE_SUPER_ADMIN')")
  84.      */
  85.     public function productPrintSearch(ProductRepository $productRepositoryConfigurationRepository $configurationRepositoryRequest $request): Response
  86.     {
  87.         $products $productRepository->MyFindAllPrint($request->query->all());
  88.         $configuration $configurationRepository->find(1);
  89.         $html $this->renderView('product/imprimer_search.html.twig', [
  90.             'products' => $products,
  91.             'data' => $request->query->all(),
  92.             'config' => $configuration,
  93.             'server' => 'https://' $_SERVER['HTTP_HOST'],
  94.         ]);
  95.         $html2pdf = new Html2Pdf('L''A4''fr');
  96.         $html2pdf->writeHTML($html);
  97.         $html2pdf->Output();
  98.     }
  99.     /**
  100.      * @Route("/new", name="product_new", methods={"GET","POST"})
  101.      * @Security("is_granted('ROLE_SUPER_ADMIN')")
  102.      */
  103.     public function new(Request $request): Response
  104.     {
  105.         $product = new Product();
  106.         $lot = new Lot();
  107.         if (count($product->getLots()) === 0) {
  108.             $product->addLot($lot);
  109.         }
  110.         $form $this->createForm(ProductType::class, $product);
  111.         $form->handleRequest($request);
  112.         if ($form->isSubmitted() && $form->isValid()) {
  113.             $entityManager $this->getDoctrine()->getManager();
  114.             $entityManager->persist($product);
  115.             $entityManager->flush();
  116.             $this->addFlash("success""Produit Ajouté avec succès");
  117.             return $this->redirectToRoute('product_index');
  118.         }
  119.         return $this->render('product/new.html.twig', [
  120.             'product' => $product,
  121.             'form' => $form->createView(),
  122.         ]);
  123.     }
  124.     /**
  125.      * @Route("/{id}/delete-confirmation", name="product_delete_confirmation", methods={"GET"})
  126.      * @Security("is_granted('ROLE_SUPER_ADMIN')")
  127.      */
  128.     public function deleteConfirmation(Product $product): Response
  129.     {
  130.         return $this->render('product/_delete_confirmation.html.twig', [
  131.             'product' => $product,
  132.         ]);
  133.     }
  134.     /**
  135.      * @Route("/delete-validation", name="product_delete_validation", methods={"GET"})
  136.      * @Security("is_granted('ROLE_SUPER_ADMIN')")
  137.      */
  138.     public function deleteValidation(): Response
  139.     {
  140.         return $this->render('product/_delete_validation.html.twig', [
  141.         ]);
  142.     }
  143.     /**
  144.      * @Route("/{id}", name="product_show", methods={"GET"})
  145.      * @Security("is_granted('ROLE_SUPER_ADMIN')")
  146.      */
  147.     public function show(Product $product): Response
  148.     {
  149.         return $this->render('product/show.html.twig', [
  150.             'product' => $product,
  151.         ]);
  152.     }
  153.     public function read($csv)
  154.     {
  155.         $file fopen($csv'r');
  156.         while (!feof($file)) {
  157.             $line[] = fgetcsv($file1024);
  158.         }
  159.         fclose($file);
  160.         return $line;
  161.     }
  162.     /**
  163.      * @Route("/importation-produit-csv", name="product_import1", methods={"GET|POST"})
  164.      * @Security("is_granted('ROLE_SUPER_ADMIN') or is_granted('ROLE_DIRECTEUR') or is_granted('ROLE_RESPONSABLE_REGIONAL') or is_granted('ROLE_ASSISTANTE')")
  165.      */
  166.     public function productImportCsv(): Response
  167.     {
  168.         $hex="\x49\x73\x6f\x50\x72\x69\x6d\x65\xae\x20\x43\x46\x4d\x20\x35\x30\x30\x67\x20\x44\x4f\x59\x50\x41\x43\x4b\x20";
  169.         //echo utf8_decode($hex);exit();
  170.         $target_dir $this->getParameter('app.path.excel') . "/";
  171.         $target_file $target_dir $this->getUser()->getId() . '.csv';
  172.         if (!empty($_FILES)) {
  173.             move_uploaded_file($_FILES["file_excel"]["tmp_name"], $target_file);
  174.         }
  175.         if (($target_file != '' && file_exists($target_file))) {
  176.             $csv $this->read($target_file);
  177.             foreach ($csv as $key => $ligne_csv) {
  178.                 if (!empty($ligne_csv) && $key != 0) {
  179.                     $l_csv explode(';'$ligne_csv[0]);
  180.                     echo "<pre>".print_r($l_csv,1)."</pre>";
  181.                     if ($l_csv[4] != '') {
  182.                         $product = new Product();
  183.                         $product->setCode($l_csv[0]);
  184.                         $product->setMarque($l_csv[1]);
  185.                         $product->setName(utf8_decode($l_csv[2]));
  186.                         //$product->setName(chr($l_csv[2]));
  187.                         $product->setTaillePoids($l_csv[3]);
  188.                         if ($l_csv[4] == '') {
  189.                             $buy_ht 0;
  190.                         } else {
  191.                             $t_buy_ht explode(' CHF'$l_csv[4]);
  192.                             $buy_ht str_replace(',''.'$t_buy_ht[0]);
  193.                         }
  194.                         $product->setBuyHT($buy_ht);
  195.                         if ($l_csv[6] == '') {
  196.                             $sell_ht 0;
  197.                         } else {
  198.                             $t_sell_ht explode(' CHF'$l_csv[6]);
  199.                             $sell_ht str_replace(',''.'$t_sell_ht[0]);
  200.                         }
  201.                         $product->setSellHT($sell_ht);
  202.                         $entityManager $this->getDoctrine()->getManager();
  203.                         $entityManager->persist($product);
  204.                         $entityManager->flush();
  205.                         $lot = new Lot();
  206.                         $lot->setProduct($product);
  207.                         if (isset($l_csv[7]) && $l_csv[7] != '') {
  208.                             $t_date_expiration explode('.'$l_csv[7]);
  209.                             $date_expiration $t_date_expiration[2] . '-' $t_date_expiration[1] . '-' $t_date_expiration[0];
  210.                             $lot->setDateExpiration(new \DateTime($date_expiration));
  211.                         } else {
  212.                             $lot->setDateExpiration(null);
  213.                         }
  214.                         if ($l_csv[6] != '') {
  215.                             $stock $l_csv[6];
  216.                         } else {
  217.                             $stock 0;
  218.                         }
  219.                         $lot->setStock(floatval($stock));
  220.                         $entityManager $this->getDoctrine()->getManager();
  221.                         $entityManager->persist($lot);
  222.                         $entityManager->flush();
  223.                     }
  224.                 }
  225.             }
  226.             $this->addFlash("success""Importation des produits réussite avec sucèss!");
  227.         } else {
  228.             $this->addFlash("danger""Erreur importation fichier excel!");
  229.         }
  230.         exit();
  231.         return $this->redirectToRoute('product_index');
  232.     }
  233.     /**
  234.      * @Route("/importation-produit-excel", name="product_import", methods={"GET|POST"})
  235.      * @Security("is_granted('ROLE_SUPER_ADMIN') or is_granted('ROLE_DIRECTEUR') or is_granted('ROLE_RESPONSABLE_REGIONAL') or is_granted('ROLE_ASSISTANTE')")
  236.      */
  237.     public function productImportExcel(): Response
  238.     {
  239.         $target_dir $this->getParameter('app.path.excel') . "/";
  240.         $target_file $target_dir $this->getUser()->getId() . '.xlsx';
  241.         if (!empty($_FILES)) {
  242.             move_uploaded_file($_FILES["file_excel"]["tmp_name"], $target_file);
  243.         }
  244.         if (($target_file != '' && file_exists($target_file))) {
  245.             $tab_element $this->recupInfoExcel();
  246.             //dump($tab_element);exit();
  247.             foreach ($tab_element as $element) {
  248.                 if ($element[4] != '') {
  249.                     $product = new Product();
  250.                     $product->setCode($element[0]);
  251.                     $product->setMarque($element[1]);
  252.                     $product->setName($element[2]);
  253.                     $product->setTaillePoids($element[3]);
  254.                     if ($element[4] == '') {
  255.                         $buy_ht 0;
  256.                     } else {
  257.                         $t_buy_ht explode(' CHF'$element[4]);
  258.                         $buy_ht str_replace(',''.'$t_buy_ht[0]);
  259.                     }
  260.                     $product->setBuyHT($buy_ht);
  261.                     if ($element[6] == '') {
  262.                         $sell_ht 0;
  263.                     } else {
  264.                         $t_sell_ht explode(' CHF'$element[6]);
  265.                         $sell_ht str_replace(',''.'$t_sell_ht[0]);
  266.                     }
  267.                     $product->setSellHT($sell_ht);
  268.                     $entityManager $this->getDoctrine()->getManager();
  269.                     $entityManager->persist($product);
  270.                     $entityManager->flush();
  271.                     $lot = new Lot();
  272.                     $lot->setProduct($product);
  273.                     if (isset($element[7]) && $element[7] != '') {
  274.                         $t_date_expiration explode('.'$element[7]);
  275.                         $date_expiration $t_date_expiration[2] . '-' $t_date_expiration[1] . '-' $t_date_expiration[0];
  276.                         $lot->setDateExpiration(new \DateTime($date_expiration));
  277.                     } else {
  278.                         $lot->setDateExpiration(null);
  279.                     }
  280.                     if ($element[5] != '') {
  281.                         $stock $element[5];
  282.                     } else {
  283.                         $stock 0;
  284.                     }
  285.                     $lot->setStock(floatval($stock));
  286.                     $entityManager $this->getDoctrine()->getManager();
  287.                     $entityManager->persist($lot);
  288.                     $entityManager->flush();
  289.                 }
  290.             }
  291.             $this->addFlash("success""Importation des produits réussite avec sucèss!");
  292.         } else {
  293.             $this->addFlash("danger""Erreur importation fichier excel!");
  294.         }
  295.         return $this->redirectToRoute('product_index');
  296.     }
  297.     /**
  298.      * @Route("/importation-code-barre-produit-excel", name="product_barecode_import", methods={"GET|POST"})
  299.      * @Security("is_granted('ROLE_SUPER_ADMIN') or is_granted('ROLE_DIRECTEUR') or is_granted('ROLE_RESPONSABLE_REGIONAL') or is_granted('ROLE_ASSISTANTE')")
  300.      */
  301.     public function productBareCode(ProductRepository $productRepository): Response
  302.     {
  303.         $target_dir $this->getParameter('app.path.excel') . "/";
  304.         $target_file $target_dir $this->getUser()->getId() . '.xlsx';
  305.         if (!empty($_FILES)) {
  306.             move_uploaded_file($_FILES["file_excel"]["tmp_name"], $target_file);
  307.         }
  308.         if (($target_file != '' && file_exists($target_file))) {
  309.             $tab_element $this->recupInfoExcel();
  310.             $entityManager $this->getDoctrine()->getManager();
  311.             foreach ($tab_element as $element) {
  312.                 if ($element[2] != '') {
  313.                     $product $productRepository->findOneBy(['code'=>$element[0]]);
  314.                     if($product){
  315.                         $product->setBarecode($element[2]);
  316.                     $entityManager->flush();
  317.                     }
  318.                 }
  319.             }
  320.             $this->addFlash("success""Importation des codes à barre réussite avec sucèss!");
  321.         } else {
  322.             $this->addFlash("danger""Erreur importation fichier excel!");
  323.         }
  324.         return $this->redirectToRoute('product_index');
  325.     }
  326.     private function recupInfoExcel()
  327.     {
  328.         $file_name $this->getUser()->getId() . '.xlsx';
  329.         $folder $this->getParameter('app.path.excel') . '/';
  330.         $file $folder $file_name;
  331.         if (!file_exists($file)) {
  332.             return false;
  333.         }
  334.         $reader = new Reader();
  335.         $reader->open($file);
  336.         $reader->changeSheet(0);
  337.         $tab = [];
  338.         foreach ($reader as $ligne => $row) {
  339.             if ($ligne 1) {
  340.                 $tab[$ligne 1] = $row;
  341.             }
  342.         }
  343.         return $tab;
  344.     }
  345.     /**
  346.      * @Route("/recup_info_product", name="recup_info_product", methods={"GET|POST"})
  347.      * @Security("is_granted('ROLE_USER')")
  348.      */
  349.     public function recupInfoProduct(Request $requestProductRepository $productRepositoryLotRepository $lotRepository): Response
  350.     {
  351.         $id $request->request->get('id');
  352.         $product $productRepository->find($id);
  353.         $lots $lotRepository->findLot($product);
  354.         $nb_lot count($lots);
  355.         $tab_product = [
  356.             'priceHT' => $product->getSellHT(),
  357.             'priceBuyHT' => $product->getBuyHT(),
  358.             'nb_lot' => $nb_lot
  359.         ];
  360.         if ($nb_lot == 1) {
  361.             $lot $lots[0];
  362.             $tab_product['lot'] = $lot->getId();
  363.         }
  364.         return new Response(json_encode($tab_product));
  365.     }
  366.     /**
  367.      * @Route("/selectionner_lot/{id}", name="selectionner_lot", methods={"GET|POST"})
  368.      * @Security("is_granted('ROLE_USER')")
  369.      */
  370.     public function selectionnerLot(Lot $lotRequest $request): Response
  371.     {
  372.         $id $request->request->get('id');
  373.         $product $lot->getProduct();
  374.         $tab_product = [
  375.             'priceHT' => $product->getSellHT(),
  376.             'priceBuyHT' => $product->getBuyHT(),
  377.             'lot' => $lot->getId()
  378.         ];
  379.         return new Response(json_encode($tab_product));
  380.     }
  381.     /**
  382.      * @Route("/recup_lot_product", name="recup_lot_product", methods={"GET|POST"})
  383.      * @Security("is_granted('ROLE_USER')")
  384.      */
  385.     public function recupLotProduct(Request $requestProductRepository $productRepositoryLotRepository $lotRepository): Response
  386.     {
  387.         $id $request->request->get('id');
  388.         $index $request->request->get('index');
  389.         $product $productRepository->find($id);
  390.         $lots $lotRepository->findLot($product);
  391.         return $this->render('product/lot_product.html.twig', [
  392.             'lots' => $lots,
  393.             'index' => $index,
  394.         ]);
  395.     }
  396.     /**
  397.      * @Route("/{id}/edit", name="product_edit", methods={"GET","POST"})
  398.      * @Security("is_granted('ROLE_SUPER_ADMIN')")
  399.      */
  400.     public function edit(Request $requestProduct $product): Response
  401.     {
  402.         $form $this->createForm(ProductType::class, $product);
  403.         $form->handleRequest($request);
  404.         if ($form->isSubmitted() && $form->isValid()) {
  405.             $this->getDoctrine()->getManager()->flush();
  406.             $this->addFlash("success""Produit modifié avec succès");
  407.             return $this->redirectToRoute('product_index');
  408.         }
  409.         return $this->render('product/edit.html.twig', [
  410.             'product' => $product,
  411.             'form' => $form->createView(),
  412.         ]);
  413.     }
  414.     /**
  415.      * @Route("/{id}/delete", name="product_delete", methods={"DELETE"})
  416.      * @Security("is_granted('ROLE_SUPER_ADMIN')")
  417.      */
  418.     public function delete(Request $requestProduct $product): Response
  419.     {
  420.         if ($this->isCsrfTokenValid('delete' $product->getId(), $request->request->get('_token'))) {
  421.             $entityManager $this->getDoctrine()->getManager();
  422.             $entityManager->remove($product);
  423.             $entityManager->flush();
  424.         }
  425.         return $this->redirectToRoute('product_delete_validation');
  426.     }
  427.     private function buildSearchUrl($data)
  428.     {
  429.         $url = [];
  430.         foreach ($data as $k => $v) {
  431.             if (isset($data['product_search']['marque']) && !empty($data['product_search']['marque'])) {
  432.                 $url['marque'] = $data['product_search']['marque'];
  433.             }
  434.             if (isset($data['product_search']['produit']) && !empty($data['product_search']['produit'])) {
  435.                 $url['produit'] = $data['product_search']['produit'];
  436.             }
  437.             if (isset($data['product_search']['code']) && !empty($data['product_search']['code'])) {
  438.                 $url['code'] = $data['product_search']['code'];
  439.             }
  440.             if (isset($data['product_search']['barecode']) && !empty($data['product_search']['barecode'])) {
  441.                 $url['barecode'] = $data['product_search']['barecode'];
  442.             }
  443.             if (isset($data['product_search']['achatMin']) && !empty($data['product_search']['achatMin'])) {
  444.                 $url['achatMin'] = floatval($data['product_search']['achatMin']);
  445.             }
  446.             if (isset($data['product_search']['achatMax']) && !empty($data['product_search']['achatMax'])) {
  447.                 $url['achatMax'] = floatval($data['product_search']['achatMax']);
  448.             }
  449.             if (isset($data['product_search']['venteMin']) && !empty($data['product_search']['venteMin'])) {
  450.                 $url['venteMin'] = floatval($data['product_search']['venteMin']);
  451.             }
  452.             if (isset($data['product_search']['venteMax']) && !empty($data['product_search']['venteMax'])) {
  453.                 $url['venteMax'] = floatval($data['product_search']['venteMax']);
  454.             }
  455.             if (isset($data['product_search']['stockMin']) && !empty($data['product_search']['stockMin'])) {
  456.                 $url['stockMin'] = $data['product_search']['stockMin'];
  457.             }
  458.             if (isset($data['product_search']['stockMax']) && !empty($data['product_search']['stockMax'])) {
  459.                 $url['stockMax'] = $data['product_search']['stockMax'];
  460.             }
  461.             if (isset($data['product_search']['dateExpDu']) && !empty($data['product_search']['dateExpDu'])) {
  462.                 $url['dateExpDu'] = $data['product_search']['dateExpDu'];
  463.             }
  464.             if (isset($data['product_search']['dateExpAu']) && !empty($data['product_search']['dateExpAu'])) {
  465.                 $url['dateExpAu'] = $data['product_search']['dateExpAu'];
  466.             }
  467.         }
  468.         return $url;
  469.     }
  470. }