- <?php
- namespace App\Controller;
- use App\Entity\CompanyAssociation;
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
- use Symfony\Component\Routing\Annotation\Route;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpFoundation\JsonResponse;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Entity;
- use Doctrine\ORM\EntityManagerInterface;
- use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
- use Symfony\Component\HttpFoundation\RedirectResponse;
- use Doctrine\Common\Collections\ArrayCollection;
- use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
- use Symfony\Component\Form\Extension\Core\Type\SubmitType;
- use Knp\Component\Pager\PaginatorInterface;
- use App\Entity\Category;
- use App\Entity\Cart;
- use App\Entity\Certification;
- use App\Entity\Company;
- use App\Entity\Conservation;
- use App\Entity\DeliveryMethod;
- use App\Entity\Ingredient;
- use App\Entity\Pricing;
- use App\Entity\Product;
- use App\Entity\ProductSponsored;
- use App\Entity\ProductSponsoredCategory;
- use App\Entity\ProductReplenishment;
- use App\Entity\Region;
- use App\Entity\Storage;
- use App\Entity\Tag;
- use App\Entity\UserSearch;
- use App\Entity\User;
- use App\Entity\ProductInBox;
- use App\Service\DistributorService;
- use App\Service\PaymentService;
- use App\Service\ProductService;
- use App\Service\UserService;
- use App\Service\ImageService;
- use App\Service\CompanyService;
- use App\Form\ProductType;
- use App\Form\ProductReplenishmentType;
- use App\Form\StorageType;
- use Symfony\Component\Form\Extension\Core\Type\CollectionType;
- class ProductController extends AbstractController
- {
-     private $entityMana;
-     private $productServ;
-     private $userServ;
-     private $companyServ;
-     private $request;
-     public function __construct(ProductService $prodServ, EntityManagerInterface $em, UserService $user, CompanyService $companyServ){
-         $this->entityMana = $em;
-         $this->productServ = $prodServ;
-         $this->userServ = $user;
-         $this->companyServ = $companyServ;
-     }
-     /**
-      * @route("/core/convert/productBoxes",
-      *      name="convertBoxesOfProduct")
-      */
-     public function convertCart(EntityManagerInterface $em){
-         return new Response('Disabled');
-         $products = $em->getRepository(Product::class)->findProductsWithOldBundles();
-         foreach($products as $box){
-             $box->setIsBoxOfProducts(true);
-             foreach($box->getProductsInBundle() as $p){
-                 $pb = new ProductInBox();
-                 $pb->setProductRepresentingBox($box);
-                 $pb->setProduct($p->getProduct());
-                 $pb->setQuantity(1);
-                 $pb->setPriceToCompany($p->getPriceToCompany());
-                 $em->persist($pb);
-                 $box->addProductInBox($pb);
-             }
-             $em->persist($box);
-         }
-         $em->flush();
-         return new Response();
-     }
-     /**
-      * @route({
-      *          "fr": "/soldes",
-      *          "en": "/sponsored/{url}"
-      *      },
-      *      options = { "expose" = true },
-      *      name="viewOnSalesProduct")
-      * @Template("frontend/category.html.twig")
-      */
-     public function viewOnSaleProducts(Request $request){
-         $twigData = array();
-         $twigData['link'] = $request->getPathInfo();
-         $twigData['category'] =  new Category();
-         $twigData['category']->setTitle('En solde');
-         $twigData['noLoader'] = true;
-         $twigData['noPagination'] = true;
-         $twigData['forcedBanner'] = '/assets/frontend/images/Banner-02-2021-commealaferme.png';
-         $order = 'Desc';
-         if ($request->get("order")) {
-           $order = $request->get("order");
-         }
-         $twigData['order']= $order;
-         $sorting = "date";
-         // $sorting = null;
-         if ($request->get("sorting")) {
-           $sorting = $request->get("sorting");
-         }
-         $twigData['sorting']= $sorting;
-         $sales = $this->entityMana->getRepository(Pricing::class)->findOnSale($sorting,$order);
-         // dd($sales);
-         $twigData['products'] = array();
-         foreach( $sales as $s ){
-         if($s->getDiscountProduct()->isSalable())
-             $twigData['products'][]= $s->getDiscountProduct();
-         }
-         $twigData['productsAmount'] = count($twigData['products']);
-         //$twigData['products']['getTotalItemCount'] = $category->getProductsSponsored()->count();
-         $twigData['regions'] = $this->entityMana->getRepository(Region::class)->findBy(['province'=>'Qc']);
-         $twigData['conservations'] = $this->entityMana->getRepository(Conservation::class)->findAll();
-         return $twigData;
-     }
-     /**
-      * @route({
-      *          "fr": "/vedette/{url}",
-      *          "en": "/sponsored/{url}"
-      *      },
-      *      defaults={
-      *          "url" = false
-      *      },
-      *      options = { "expose" = true },
-      *      name="viewSponsoredCategory")
-      * @Template("frontend/category.html.twig")
-      */
-     public function viewSponsoredCategory(ProductSponsoredCategory $category=null, Request $request){
-         $twigData = array();
-         if(empty($category) || $category->getUrl() == 'soldes'){
-             return $this->redirectToRoute('viewOnSalesProduct');
-         }
-         if($category->getCategory()){
-             $fullUrl = $category->getCategory()->getFullUrlPath();
-             return new RedirectResponse('/categorie'.$fullUrl);
-         }
-         $order = "desc";
-         if ($request->get("order")) {
-           $order = $request->get("order");
-         }
-         $twigData['order']= $order;
-         $sorting = "date";
-         if ($request->get("sorting")) {
-           $sorting = $request->get("sorting");
-         }
-         $twigData['sorting']= $sorting;
-         $twigData['category'] =  new Category();
-         $twigData['category']->setTitle($category->getTitle());
-         $twigData['noLoader'] = true;
-         $twigData['noPagination'] = true;
-         $twigData['forcedBanner'] = '/assets/frontend/images/Banner-02-2021-commealaferme.png';
-         $twigData['selected'] = array(
-             'main' => $twigData['category']->getId(),
-             'sub1' => false,
-             'sub2' => false,
-             'sub3' => false
-         );
-         $Products = $this->entityMana->getRepository(Product::class)->getProductsSponsored($category, $sorting, $order);
-         $twigData['products'] = $Products;
-         // $now = new \DateTime('now');
-         // foreach( $Products as $p ) {
-         //   if (
-         //     $now >= $p->getFromDate() &&
-         //     $now <= $p->getToDate() &&
-         //     $p->getActive()
-         //   ) {
-         //     $twigData['products'][]= $p->getProduct();
-         //   }
-         // }
-         $twigData['productsAmount'] = count($twigData['products']);
-         //$twigData['products']['getTotalItemCount'] = $category->getProductsSponsored()->count();
-         $twigData['regions'] = $this->entityMana->getRepository(Region::class)->findBy(['province'=>'Qc']);
-         $twigData['conservations'] = $this->entityMana->getRepository(Conservation::class)->findAll();
-         return $twigData;
-     }
-     /**
-      * @route({
-      *          "fr": "/solex/{id}",
-      *          "en": "/solex/{id}"
-      *      },
-      *      options = { "expose" = true },
-      *      name="testing")
-      */
-     public function testing(DistributorService $d, User $user){
-         //$form = $this->createForm(ProductReplenishmentType::class, $replenishment);
-         $twig = $this->get('twig')->render('emails/welcomeNewAccount.html.twig', [
-             'user' => $user,
-         ]);
-         var_dump($twig);
-         die();
-     }
-     /**
-      * @route({
-      *          "fr": "/vendeur/inventaire/{id}",
-      *          "en": "/seller/inventory/{id}"
-      *      },
-      *      defaults={
-      *          "id" = false
-      *      },
-      *      name="replenishment")
-      * @Security("has_role('ROLE_ADMIN')")
-      * @Template("admin/product/replenishmentCalendar.html.twig")
-      */
-     public function replenishment( Company $company=null, DistributorService $distributor,Request $request, UserService $userServ){
-         if(empty($company)){
-             $user = $userServ->getUser();
-             $companies = $user->getCompanies();
-             if(count($companies) > 1){
-                 $this->addFlash('error', "Vous avez plusieur boutiques, pour accéder a l'inventaire via le menu");
-                 return $this->redirectToRoute('dashboard');
-             }else{
-                 $company = $companies->get(0);
-             }
-         }
-         //@SECuritY
-         if(!$this->companyServ->allowedToModify($company) || empty($company)){
-             $this->addFlash('error', "Vous n'avez pas d'accès Ã  cette boutique, veuillez utiliser le menu");
-             return $this->redirectToRoute('dashboard');
-         }
-         if($_ENV['APP_ENV'] != 'dev'){
-             if(!$company->hasPayment()){
-                 $this->addFlash('error', "Avant de planifier votre mise en inventaire, SVP veuillez compléter votre mode de paiement. Ce mode de paiement servira Ã  payer vos frais mensuels tel l’abonnement de votre boutique et l’entreposage de vos produits.");
-                 return $this->redirectToRoute('companyPayment');
-             }
-         }
-         if(empty($company->getProducts()) || count($company->getProducts()) < 1){
-             $this->addFlash('error', "Vous devez avoir des produits pour prendre un rendez-vous.");
-             return $this->redirectToRoute('adminNewProduct', array('id' => $company->getId()));
-         }
-         $twigData = array();
-         $twigData['company'] = $company;
-         return $twigData;
-     }
-     /**
-      * @route({
-      *          "fr": "/vendeur/inventaire/dimensions/{id}",
-      *          "en": "/vendeur/inventory/dimensions/{id}"
-      *      },
-      *      options = { "expose" = true },
-      *      name="replenishmentDimensionsForm")
-      * @Security("has_role('ROLE_ADMIN')")
-      */
-     public function confirmDimensionStorage(Product $product, ProductService $productServ, Request $request){
-         if(!$productServ->allowedToModify($product)){
-             $this->addFlash('error', "Vous n'avez pas d'accès Ã  ce produit.");
-             return $this->redirectToRoute('dashboard');
-         }
-         $response = new JsonResponse();
-         $twigData = array();
-         $twigData['product'] = $product;
-         $form = $this->createFormBuilder($product)
-             ->add('storages', CollectionType::class, array(
-                 'entry_type' => StorageType::class,
-                 'allow_delete' => false,
-                 'data' => $product->getStorages()
-             ))
-             ->add('confirm', CheckboxType::class, array(
-                 'label' => 'Je confirme les informations et la mise-en-inventaire de ce produit.',
-                 'required' => true,
-                 'mapped' => false,
-                 'attr' => array(
-                     'class' => 'checkbox-only confirm-checkbox'
-                 )
-             ))
-             ->getForm();
-             ;
-         $twigData['form'] = $form->createView();
-         $form->handleRequest($request);
-         if($request->isXmlHttpRequest() && $request->get('form') !== null ){
-             $minimum = $request->get('minimum');
-             if(!empty($minimum) && is_numeric($minimum))
-                 $product->setMinimumInStorage($minimum);
-             $em = $this->getDoctrine()->getManager();
-             $em->persist($product);
-             $em->flush();
-             $response->setData(array('success' => true));
-             return $response;
-         }else{
-             $response->setData(array(
-                 'html' => $this->get('twig')->render('admin/product/replenishmentConfirmDimensions.html.twig', $twigData),
-                 'minimum' => $product->getMinimumInStorage()
-             ));
-             return $response;
-         }
-     }
-     /**
-      * @route({
-      *          "fr": "/vendeur/inventaire/rendezvous/{replenishment}/{sendEmail}",
-      *          "en": "/seller/inventory/appointment/{replenishment}/{sendEmail}"
-      *      },
-      *      options = { "expose" = true },
-      *      defaults={
-      *          "sendEmail" = 1,
-      *      },
-      *      name="editReplenishmentAppointment")
-      * @Security("has_role('ROLE_ADMIN')")
-      * @Template("admin/product/replenishmentSchedule.html.twig")
-      */
-     public function editReplenishmentAppointment(ProductReplenishment $replenishment, Request $request, CompanyService $companyServ, DistributorService $distributor, UserService $userServ, $sendEmail=true){
-         $company = $replenishment->getCompany();
-         if(!$companyServ->allowedToModify($company)){
-             $this->addFlash('error', "Vous n'avez pas d'accès Ã  cette boutique.");
-             return $this->redirectToRoute('dashboard');
-         }
-         $twigData = array();
-         $twigData['company'] = $company;
-         $twigData['editing'] = true;
-         $twigData['scheduleDate'] = $replenishment->getAppointmentDate();
-         $twigData['replenishment'] = $replenishment;
-         $form = $this->createForm(ProductReplenishmentType::class, $replenishment);
-         $form
-             ->add('erase', SubmitType::Class,
-                 array(
-                     'label' => 'Annuler le rendez-vous',
-                     'attr' => array(
-                         'class' => 'btn btn-warning'
-                     )
-                 )
-             );
-         $form->handleRequest($request);
-         if($form->isSubmitted() && $form->isValid()){
-             $em = $this->getDoctrine()->getManager();
-             //We erase
-             if($form->get('submit')->isClicked()){
-                 //We edit only
-                 $em->persist($replenishment);
-                 $em->flush();
-                 if($distributor->setStockingSchedule($replenishment)){
-                    $this->addFlash('success', "Votre rendez-vous Ã  Ã©té modifié.");
-                    if($sendEmail){
-                         $email =  $this->get('twig')->render('emails/confirmReplenishment.html.twig', array('replenishment' => $replenishment, 'user' => $userServ->getUser()));
-                        //For now we send a email to some people to warn them
-                        if($replenishment->hasProductsNeedingRefrigeration()){
-                             $userServ->sendEmail('Maturin: Un rdv pour produits réfrigéré le '.$replenishment->getAppointmentDate()->format('d-m-Y').' Ã  Ã©té pris', $email, false, 'support@maturin.ca');
-                             $userServ->sendEmail('Maturin: Un rdv pour produits réfrigéré le '.$replenishment->getAppointmentDate()->format('d-m-Y').' Ã  Ã©té pris', $email, false, 'philippe.vandelac@solexdocument.com');
-                        }
-                        $userServ->sendEmail(' Confirmation de votre rendez-vous chez Maturin le '.$replenishment->getAppointmentDate()->format('d-m-Y'), $email, false, $userServ->getUser()->getEmail());
-                     }
-                 }else{
-                     $this->addFlash('error', "Nous n'avons pas réussi a confirmer votre rendez-vous.");
-                     $replenishment->setIsValid(false);
-                     $em->persist($replenishment);
-                     $em->flush();
-                 }
-             }else{
-                 if($distributor->cancelStockingSchedule($replenishment)){
-                     $this->addFlash('success', "Votre rendez-vous Ã  Ã©té annulé.");
-                     $replenishment->setIsValid(false);
-                     //$em->remove($replenishment);
-                     $em->persist($replenishment);
-                 }
-             }
-             $em->flush();
-             return $this->redirectToRoute('replenishment', array('id' => $company->getId()));
-         }
-         $twigData['form'] = $form->createView();
-         return $twigData;
-     }
-     /**
-      * @route({
-      *          "fr": "/vendeur/inventaire/rendezvous/{year}/{month}/{day}/{hour}/{min}/{company}",
-      *          "en": "/seller/inventory/appointment/{year}/{month}/{day}/{hour}/{min}/{company}"
-      *      },
-      *      options = { "expose" = true },
-      *      name="replenishmentAppointment")
-      * @Security("has_role('ROLE_ADMIN')")
-      * @Template("admin/product/replenishmentSchedule.html.twig")
-      */
-     public function replenishementAppointment(Company $company=null, $year=null, $month=null, $day=null, $hour=null, $min=null, CompanyService $companyServ, Request $request, DistributorService $distributor, UserService $userServ){
-         if(!$companyServ->allowedToModify($company)){
-             $this->addFlash('error', "Vous n'avez pas d'accès Ã  cette boutique.");
-             return $this->redirectToRoute('dashboard');
-         }
-         $twigData = array();
-         $twigData['scheduleDate'] = \DateTime::createFromFormat('d-m-Y H:i', $day.'-'.$month.'-'.$year.' '.$hour.':'.$min);
-         $twigData['company'] = $company;
-         $twigData['loader'] = true;
-         //If they already have a appointment at that time, let's edit that one instead of creating a new one
-         if($test = $company->hasReplenishmentOn($twigData['scheduleDate'])){
-             $this->addFlash('error', 'Utiliser le bouton "Ajouter un produit" pour ajouter un produit a votre commande');
-             $this->redirectToRoute('editReplenishmentAppointment', array('replenishment' => $test->getId()));
-         }
-         $replenishment = new ProductReplenishment();
-         $replenishment->setAppointmentDate($twigData['scheduleDate']);
-         $replenishment->setCompany($company);
-         $twigData['editing'] = false;
-         $twigData['replenishment'] = $replenishment;
-         $form = $this->createForm(ProductReplenishmentType::class, $replenishment);
-         $form->handleRequest($request);
-         if($form->isSubmitted() && $form->isValid()){
-             try {
-                 $em = $this->getDoctrine()->getManager();
-                 $em->persist($replenishment);
-                 $em->flush();
-                 if($distributor->setStockingSchedule($replenishment)){
-                    $email =  $this->get('twig')->render('emails/confirmReplenishment.html.twig', array('replenishment' => $replenishment, 'user' => $userServ->getUser()));
-                    $userServ->sendEmail(' Confirmation de votre rendez-vous chez Maturin le '.$replenishment->getAppointmentDate()->format('d-m-Y'), $email, false, $userServ->getUser()->getEmail());
-                     $this->addFlash('success', 'Votre rendez-vous Ã©té confirmé.');
-                    //For now we send a email to some people to warn them
-                    if($replenishment->hasProductsNeedingRefrigeration()){
-                         $userServ->sendEmail('Maturin: Un rdv pour produits réfrigéré le '.$replenishment->getAppointmentDate()->format('d-m-Y').' Ã  Ã©té pris', $email, false, 'support@maturin.ca');
-                         $userServ->sendEmail('Maturin: Un rdv pour produits réfrigéré le '.$replenishment->getAppointmentDate()->format('d-m-Y').' Ã  Ã©té pris', $email, false, 'philippe.vandelac@solexdocument.com');
-                    }
-                 }else{
-                     $this->addFlash('error', "Nous n'avons pas réussi a confirmer votre rendez-vous.");
-                     $replenishment->setIsValid(false);
-                     $em->persist($replenishment);
-                     $em->flush();
-                 }
-             } catch (\Exception $e ){
-                 $this->addFlash('error', 'Une erreur est survenu, veuillez contacter notre support.');
-                 $this->addFlash('error', $e);
-                 $em->remove($replenishment);
-                 $em->flush();
-                 return $this->redirectToRoute('replenishment', array('id' => $company->getId()));
-             }
-             return $this->redirectToRoute('replenishment', array('id' => $company->getId()));
-         }
-         $twigData['form'] = $form->createView();
-         return $twigData;
-     }
-     /**
-      * @route({
-      *          "fr": "/admin/inventaire/horraire/{id}/{action}",
-      *          "en": "/admin/inventory/schedule/{id}/{action}"
-      *      },
-      *      options = { "expose" = true },
-      *      name="replenishmentGetSchedule")
-      * @Security("has_role('ROLE_ADMIN')")
-      */
-     public function replenishmentGetSchedule(DistributorService $distributor, Company $company, $action){
-         $response = new JsonResponse();
-         $dateFrom = new \DateTime(urldecode($_GET['start']));
-         $dateTo = new \DateTime(urldecode($_GET['end']));
-         $data = array();
-         $now = new \DateTime();
-         foreach($company->getProductReplenishments() as $r){
-             $tmpDateFrom = $r->getAppointmentDate();
-             if($tmpDateFrom > $now){
-                 $data[]=array(
-                     'id' => $r->getId(),
-                     'title' => 'Votre rendez-vous',
-                     'allDay' => 'false',
-                     'className' => 'calendar-mine',
-                     'editable' => false,
-                     'start' => $tmpDateFrom->format('c'),
-                     'end' => $tmpDateFrom->modify('+59 minutes 60 seconds')->format('c')
-                 );
-             }
-         }
-         $minSchedule = new \DateTime();
-         $minSchedule->add(new \DateInterval('P1D'));
-         /*
-         if($minSchedule > $dateFrom){
-             $dateFrom = $minSchedule;
-         }
-          */
-         $schedules = $distributor->getStockingSchedule($dateFrom->format('c'), $dateTo->format('c'));
-         $cmtp = 0;
-         foreach($schedules as $s){
-             $dateFrom = new \DateTime($s->startTime);
-             $dateTo = new \DateTime($s->endTime);
-             $nbSpot = $s->nbSpotsLeft;
-             if($nbSpot > 0 && $dateFrom->format('Ymd') >= $minSchedule->format('Ymd') ){
-                 $data[]=array(
-                     'id' => $cmtp,
-                     'title' => '',
-                     'allDay' => 'false',
-                     'className' => 'calendar-available',
-                     'editable' => false,
-                     'start' => $dateFrom->format('c'),
-                     'end' => $dateTo->format('c')
-                 );
-                 $cmtp++;
-             }
-         }
-         $response->setData($data);
-         return $response;
-     }
-     /**
-      * @route({
-      *          "fr": "/groupe/{groupUrlName}/produit/{producerUrl}/{productName}/{id}/",
-      *          "en": "/groupe/{groupUrlName}/product/{producerUrl}/{productName}/{id}/"
-      *      },
-      *      options = { "expose" = true },
-      *      name="viewProductInAssociation")
-      * @Template("frontend/product.html.twig")
-      */
-     public function viewProductInAssociation( $groupUrlName, $producerUrl, $productName, $id,    UserService $UserService, ProductService $ProductService, EntityManagerInterface $em, Request $request, PaginatorInterface $paginator, UrlGeneratorInterface $router){
-         $product = $em->getRepository(Product::class)->find((int)$id);
-         $assocation=$em->getRepository(CompanyAssociation::class)
-             ->findOneBy([
-                 'urlName' => $groupUrlName
-             ]);
-         if ($assocation && $assocation != $UserService->getAssociationUserIsBrowsing()){
-             $UserService->setAssociationUserIsBrowsing($assocation->getId());
-         }
-         return $this->viewProduct($UserService, $ProductService, $em, $product, $request, $paginator, $router);
-     }
-     /**
-      * @route({
-      *          "fr": "/produit/{producerUrl}/{productName}/{id}/",
-      *          "en": "/product/{producerUrl}/{productName}/{id}/"
-      *      },
-      *      options = { "expose" = true },
-      *      name="viewProduct")
-      * @Template("frontend/product.html.twig")
-      */
-     public function viewProduct(UserService $UserService, ProductService $ProductService, EntityManagerInterface $em, Product $product, Request $request, PaginatorInterface $paginator, UrlGeneratorInterface $router){
-         $twigData = array();
-         $UserService->addViewedProduct($product);
-         if ($this->userServ->getAssociationUserIsBrowsing()){
-             if ($request->get('_route') == 'viewProduct'){
-                 return $this->redirectToRoute('viewProductInAssociation', [
-                     'groupUrlName' => $this->userServ->getAssociationUserIsBrowsing()->getUrlName(),
-                     'producerUrl' => $product->getCompany()->getUrlName(),
-                     'productName' => $product->getUrlName(),
-                     'id' => $product->getId(),
-                 ]);
-             }
-             $userAllowed = false;
-             foreach($product->getCompany()->getAssociations() as $assocation){
-                 if ($assocation->getId() == $this->userServ->getAssociationUserIsBrowsing()->getId()){
-                     $userAllowed  =true;
-                 }
-             }
-             if (!$userAllowed){
-                 $this->addFlash('error', 'The product you tried to see is not allowed in '.$this->userServ->getAssociationUserIsBrowsing()->getName());
-                 $referer = $request->headers->get('referer');
-                 return $this->redirect($referer != null ? $referer : '/', 307);
-             }
-         } else if ($product->getIsDisplayedInAssociationOnly() || !$product->getCompany()->getShowPublicly()
-             || !$product->getIsConsumer()){
-             $this->addFlash('error', 'The product you tried to see is not available.');
-             $referer = $request->headers->get('referer');
-             return $this->redirect($referer != null ? $referer : '/', 307);
-         }
-         $referer = $request->headers->get('referer');
-         if(stripos($referer, 'maturin.ca') !== false){
-             $baseUrl = $request->getSchemeAndHttpHost();
-             $lastPath = current(explode('?', substr($referer, strpos($referer, $baseUrl) + strlen($baseUrl))));
-             $params = $this->get('router')->getMatcher()->match($lastPath);
-             if(empty($params['sub1'])){
-                 $params['sub1']= false;
-                 $params['sub2']= false;
-                 $params['sub3']= false;
-             }
-             if($params['_route'] != 'allCategory' && strpos($referer, 'page') !== false){
-                 $raw = explode('?', $referer);
-                 $page = str_replace('page=', '', end($raw));
-                 if(is_numeric($page)){
-                     $request->attributes->set('page', $page);
-                 }
-             }
-         }else{
-             $params['sub1']= false;
-             $params['sub2']= false;
-             $params['sub3']= false;
-         }
-         /*
-          * Depending of how much results there is in this sub-Category
-          * We are either displaying the sub-Category if results are > 12 (Filling a least 3 rows in the Desktop UI)
-          */
-         if($product->getSubCategory() && $product->getSubCategory()->getProducts()->count() > 4 )
-             $categoryToUse = $product->getSubCategory();
-         else
-             $categoryToUse = $product->getCategory();
-         $twigData = $this->viewCategory($categoryToUse, $params['sub1'], $params['sub2'], $params['sub3'], $paginator, $request, true);
-         $twigData['categoryToUse'] = $categoryToUse;
-         $twigData['product'] = $product;
-         $twigData['showProductsRightAway']= true;
-         $twigData['productsToBuyToo'] = $ProductService->getSimilarProduct($product);
-         //Suggestion Algo Products 
-         $twigData['productsUserMayLike'] = false;
-         $produtSuggestion = $product->getComplementedByGroups();
-         if(count($produtSuggestion) > 0){
-             $twigData['productsUserMayLike'] = $product->getAllComplementaryProducts();
-             if(count($product->getAllComplementaryProducts())< 4){
-                 //return an array of 4 elements for suggestion
-                 $suggestedProductsLength=(4- count($product->getAllComplementaryProducts()));
-                 $suggestionArray = $ProductService->getProductsSuggestion($suggestedProductsLength,$product);
-                 foreach ($product->getAllComplementaryProducts()as $p) {
-                    array_push($suggestionArray,$p);
-                 }
-                 $twigData['productsUserMayLike'] = $suggestionArray;
-             }
-         }else{
-                 $suggestedProductsLength=4;
-                 $suggestionArray = $ProductService->getProductsSuggestion($suggestedProductsLength,$product);
-                 $twigData['productsUserMayLike'] = $suggestionArray;
-         }
-         // remove independante delivery for cartes-cadeauxCarte-cadeau
-         $twigData['independentDelivery'] = true;
-         if($categoryToUse->getId()== 341 && strpos($product->getName(), 'Carte-cadeau') !== false){
-             $twigData['independentDelivery'] = false;
-         };
-         return $twigData;
-     }
-     /**
-      * @route({
-      *          "fr": "/categorie",
-      *          "en": "/category"
-      *      },
-      *      options = { "expose" = true },
-      *      name="allCategory")
-      * @Template("frontend/category.html.twig")
-      */
-     public function allCategory(ProductService $ProductService, EntityManagerInterface $em, PaginatorInterface $paginator, Request $request){
-         $twigData = array();
-         $order = "desc";
-         if ($request->get("order")) {
-           $order = $request->get("order");
-         }
-         $twigData['order']= $order;
-         $sorting = "popularity";
-         if ($request->get("sorting")) {
-           $sorting = $request->get("sorting");
-         }
-         $twigData['sorting']= $sorting;
-         $productsQuery  = $this->entityMana->getRepository(Product::class)->getProductsForAllCategory($sorting, $order);
-         $page = 1;
-         if(!empty($request->get('page')))
-             $page = $request->get('page');
-         // Paginate the results of the query
-         $twigData['products'] = $paginator->paginate(
-             $productsQuery,
-             $request->query->getInt('page', $page),
-             24
-         );
-         $twigData['productsAmount'] = count($productsQuery);
-         $twigData['regions'] = $this->entityMana->getRepository(Region::class)->findBy(['province'=>'Qc']);
-         $twigData['conservations'] = $this->entityMana->getRepository(Conservation::class)->findAll();
-         $twigData['category'] = new Category();
-         $twigData['category']->setTitle('NOTRE RICHESSE…');
-         $twigData['forcedBanner'] = '/assets/frontend/images/categorie-banner.png';
-         $twigData['noLoader'] = true;
-         $twigData['filters']= array();
-         $twigData['selected'] = array(
-             'main' => false,
-             'sub1' => false,
-             'sub2' => false,
-             'sub3' => false
-         );
-         return $twigData;
-     }
-     /**
-      * @route({
-      *          "fr": "/produits/recherche/{token}",
-      *          "en": "/products/search/{token}}"
-      *      },
-      *      options = { "expose" = true },
-      *      name="searchProducts")
-      * @Template("frontend/category.html.twig")
-      */
-     public function searchProducts($token, Request $request, ProductService $productServ, PaginatorInterface $paginator, UserService $userServ, $forcedValues = false){
-         $twigData = array();
-         $search = $this->entityMana->getRepository(UserSearch::class)->findOneBy(['token' => $token]);
-         $page = 1;
-         if(!empty($request->get('page')))
-             $page = $request->query->getInt('page');
-         $forcedValues = array(
-             'filters' => $search->getRawFilters(),
-             'token' => $token,
-             'page' => $page,
-         );
-         $products = $this->getProductByActionFromAPI($search->getSearch(), $request, $productServ, $paginator, $userServ, $forcedValues, false);
-         //Template values
-         // Paginate the results of the query
-         $twigData['products'] = $paginator->paginate(
-             $products,
-             $page,
-             24
-         );
-         $rawFilters = json_decode($search->getRawFilters());
-         $filters = array();
-         foreach( $rawFilters as $f){
-             $filters[$f->type][] = $f->id;
-         }
-         if(!empty($filters['category'])){
-             $category = $this->entityMana->getRepository(Category::class)->findOneById(current($filters['category']));
-             $twigData['category'] = $category;
-             $twigData['selected'] = array(
-                 'main' => $category->getId(),
-                 'sub1' => false,
-                 'sub2' => false,
-                 'sub3' => false
-             );
-         }else{
-             $twigData['category'] = new Category();
-             $twigData['selected'] = array(
-                 'main' => false,
-                 'sub1' => false,
-                 'sub2' => false,
-                 'sub3' => false
-             );
-         }
-         $twigData['filters'] = $filters;
-         $twigData['productsAmount'] = $twigData['products']->getTotalItemCount();
-         $twigData['regions'] = $this->entityMana->getRepository(Region::class)->findBy(['province'=>'Qc']);
-         $twigData['conservations'] = $this->entityMana->getRepository(Conservation::class)->findAll();
-         return $twigData;
-     }
-     /**
-      * @route({
-      *          "fr": "/categorie/{name}/{sub1}/{sub2}/{sub3}",
-      *          "en": "/category/{name}/{sub1}/{sub2}/{sub3}"
-      *      },
-      *      options = { "expose" = true },
-      *      defaults={
-      *          "sub1" = false,
-      *          "sub2" = false,
-      *          "sub3" = false
-      *      },
-      *      name="viewCategory")
-      * @Entity("category", expr="repository.findByUrl(name)")
-      * @Template("frontend/category.html.twig")
-      */
-     public function viewCategory(Category $category, $sub1, $sub2, $sub3, PaginatorInterface $paginator, Request $request, $forceRoute=false){
-         $twigData = array();
-         $twigData['link'] = $request->getPathInfo();
-         // $order = "desc";
-         $order = null;
-         if ($request->get("order")) {
-           $order = $request->get("order");
-         }
-         $twigData['order']= $order;
-         // $sorting = "popularity";
-         $sorting = null;
-         if ($request->get("sorting")) {
-           $sorting = $request->get("sorting");
-         }
-         $twigData['sorting']= $sorting;
-         $twigData['selected'] = array(
-             'main' => $category->getId(),
-             'sub1' => $sub1,
-             'sub2' => $sub2,
-             'sub3' => $sub3
-         );
-         $twigData['noLoader'] = true;
-         $findCategory = false;
-         //Let's find the final category the product should be in
-         //We go 4 levels here
-         if($sub1){
-             foreach($category->getCategories() as $cat){
-                 if(strtolower($cat->getUrlName()) == strtolower(urldecode($sub1))){
-                     $twigData['selected']['sub1'] = $cat->getId();
-                     if($sub2){
-                         foreach($cat->getCategories() as $subCat){
-                             if(strtolower($subCat->getUrlName()) == strtolower(urldecode($sub2))){
-                                 $twigData['selected']['sub2'] = $subCat->getId();
-                                 if($sub3){
-                                     foreach($subCat->getCategories() as $subSubCat){
-                                         if(strtolower($subSubCat->getUrlName()) == strtolower(urldecode($sub3))){
-                                             $twigData['selected']['sub3'] = $subSubCat->getId();
-                                             $findCategory = $subSubCat;
-                                             break;
-                                         }
-                                     }
-                                 }else{
-                                     $findCategory = $subCat;
-                                     break;
-                                 }
-                             }
-                         }
-                     }else{
-                         $findCategory = $cat;
-                         break;
-                     }
-                 }
-             }
-         }
-         $filter = null;
-         if ($request->get('filter')){
-             $filter = $request->get('filter');
-         }
-        
-         //If all else fail let's go back to original cat
-         if(!$findCategory)
-             $findCategory = $category;
-        
-         $categoryPlacementQuery = $this->entityMana->getRepository(Category::class)->getMyCategoryforplacemnt($findCategory);
-         $productsQuery=[];
-         $allproductsSubSubcategories = [];
-         if (!$filter){
-         if ($sorting == null){
-             if (count($categoryPlacementQuery) > 0){
-                 $resproductsQueryultat = array();
-                     foreach ($categoryPlacementQuery as $orderPlacement) {
-                         $categories = $this->entityMana->getRepository(Category::class)->find($orderPlacement['cateId']);
-                         $findsubCategories = $categories->getSubCategory();
-                             $QueryTimeTwo = $this->entityMana->getRepository(Category::class)->getMyCategoryforplacemnt($categories);
-                             if(count($QueryTimeTwo) > 0){
-                                     foreach ($QueryTimeTwo as $findproduct) {
-                                         $catefindproduct = $this->entityMana->getRepository(Category::class)->find($findproduct['cateId']);
-                                         $prods = $this->entityMana->getRepository(Product::class)->getcategoriesByPlacement($catefindproduct, $sorting, $order);
-                                                 if(count($prods) > 0){
-                                                     foreach ($prods as $prod) {
-                                                         array_push($productsQuery, $prod);  
-                                                     }
-                                                 }else{
-                                                     $findsubsubCategories = $catefindproduct->getSubCategory();
-                                                     $allproductsSubSubcategories = $this->entityMana->getRepository(Product::class)->getcategoriesByPlacement($findsubsubCategories, $sorting, $order);
-                                                 }
-                                     }
-                             }
-                             // else{
-                             //     $allProductsifNoSubCategories = $this->entityMana->getRepository(Product::class)->getcategoriesByPlacement($findsubCategories, $sorting, $order);
-                                 // if($allProductsifNoSubCategories){
-                                 //     foreach ($allProductsifNoSubCategories as $allProductsifNoSubCategory) {
-                                 //         if (!in_array($allProductsifNoSubCategory,$productsQuery)){
-                                 //             array_push($productsQuery, $allProductsifNoSubCategory);
-                                 //         }   
-                                 //     }    
-                                 // }
-                             // }
-                             if($allproductsSubSubcategories){
-                                 foreach ($allproductsSubSubcategories as $allproductsSubSubcategory) {
-                                     
-                                     if (!in_array($allproductsSubSubcategory,$productsQuery)){
-                                         array_push($productsQuery, $allproductsSubSubcategory);
-                                     }   
-                                 }    
-                             }
-                     }
-             }else{
-                 $testcategories = $this->entityMana->getRepository(Product::class)->getcategoriesByPlacement($findCategory, $sorting, $order);
-                
-                 if($testcategories){
-                     foreach ($testcategories as $testcategorie) {
-                         array_push($productsQuery, $testcategorie);  
-                     }
-                 }
-             }
-             $categoryforPages = $this->entityMana->getRepository(Product::class)->getcategoriesByPlacement($findCategory, $sorting, $order);
-             if($categoryforPages){
-                 foreach ($categoryforPages as $categoryforPage) {
-                     
-                     if (!in_array($categoryforPage,$productsQuery)){
-                         array_push($productsQuery, $categoryforPage);
-                     }   
-                 }    
-             }
-         }else{
-             $productsQuery = $this->entityMana->getRepository(Product::class)->getcategoriesByPlacement($findCategory, $sorting, $order);
-         }
-     }else{
-         // $productsQuery = $this->entityMana->getRepository(Product::class)->getcategoriesByPlacement($findCategory, $sorting, $order);
-         
-             $region = explode('region_',$filter);
-             if (count($region) > 1){
-                 $regionName= $region[1];
-                 $region = $this->entityMana->getRepository(Region::class)->findOneBy([ 'name' => $regionName ])->getId();
-             }
-             $productsQuery = $this->entityMana->getRepository(Product::class)->getcategoriesByPlacementWithFilter($findCategory, $sorting, $order, $filter, $region);
-     }
-         $page = 1;
-         if(!empty($request->get('page')))
-             $page = $request->get('page');
-         if(is_numeric($page)){
-             // Paginate the results of the query
-             $twigData['products'] = $paginator->paginate(
-             $productsQuery,
-             $request->query->getInt('page', $page),
-             24
-         );
-         if($forceRoute){
-             $twigData['products']->setUsedRoute('viewCategory');
-             $twigData['products']->setParam('name', $category->getUrlName());
-             $twigData['products']->setParam('sub1', $sub1);
-             $twigData['products']->setParam('sub2', $sub2);
-             $twigData['products']->setParam('sub3', $sub3);
-         }
-         $twigData['productsAmount'] = $twigData['products']->getTotalItemCount();
-         }else{
-             $this->addFlash('error', "le numéro de la page n'est pas valide");
-             return $this->redirectToRoute('viewCategory',[
-                 'name' => $category->getUrlName(),
-             ]);
-         }
-       
-       
-         $twigData['regions'] = $this->entityMana->getRepository(Region::class)->findBy(['province'=>'Qc']);
-         $twigData['conservations'] = $this->entityMana->getRepository(Conservation::class)->findAll();
-         $twigData['category'] = $findCategory;
-         // dd($twigData);
-         return $twigData;
-     }
-     /**
-      * @route({
-      *          "fr": "/vendeur/produit/editer/{id}",
-      *          "en": "/seller/product/edit/{id}"
-      *      },
-      *      options = {
-      *          "expose" = true
-      *      },
-      *      name="adminEditProduct")
-      * @Template("admin/product/product.html.twig")
-      * @Security("has_role('ROLE_ADMIN')")
-      */
-     public function editProduct(Request $request, UserService $userServ, ImageService $imageServ, ProductService $prodServ, Product $product=null, DistributorService $distributorServ){
-         //Check if user is allowed to modify first
-         if(!$prodServ->allowedToModify($product))
-             die('Not allowed');
-         if(empty($product)){
-             $productId = $request->get('productId');
-             $product = $prodServ->getProductById($productId);
-             if(!$prodServ->allowedToModify($product))
-                 die('Hummm.... No product access, hacking?');
-         }
-         if ($request->get("action") == "edit_product_variation") {
-           return $this->redirect("/vendeur/produit/editer/".$request->get("product_id"));
-         }
-         if ($request->get("action") == "deactivate_product_variation") {
-           $productVariation = $this->entityMana->getRepository(Product::class)->findOneBy([
-             'id' => $request->get("product_id"),
-           ]);
-           if ($productVariation) {
-             $productVariation->setAvailable(0);
-             $this->entityMana->persist($productVariation);
-             $this->entityMana->flush();
-           }
-           return $this->redirect("/vendeur/produit/editer/".$product->getId());
-         }
-         if ($request->get("action") == "deassign_variation") {
-           $productVariation = $this->entityMana->getRepository(Product::class)->findOneBy([
-             'id' => $request->get("product_id"),
-           ]);
-           if ($productVariation) {
-             $productVariation->setParentProductId(null);
-             $this->entityMana->persist($productVariation);
-             $this->entityMana->flush();
-           }
-           return $this->redirect("/vendeur/produit/editer/".$product->getId());
-         }
-         if ($request->get("action") == "assign_this_product_to_parent") {
-           $product->setParentProductId($request->get("product_id"));
-           $this->entityMana->persist($product);
-           $this->entityMana->flush();
-           return $this->redirect("/vendeur/produit/editer/".$request->get("product_id"));
-         }
-         if ($request->get("action") == "generate_product_variation") {
-           $company = $product->getCompany();
-           $productVariation = new Product();
-           $productVariation->setParentProductId($product->getId());
-           $productVariation->setOrigName($product->getOrigName());
-           $productVariation->setDeliveryType($product->getDeliveryType());
-           $productVariation->setActionWhenExpired($product->getActionWhenExpired());
-           $productVariation->setCategories($product->getCategories());
-           $productVariation->setConservation($product->getConservation());
-           $productVariation->setSubCategory($product->getSubCategory());
-           $productVariation->setRegion($product->getRegion());
-           $productVariation->setOrigBrandName($product->getOrigBrandName());
-           $productVariation->setDescription($product->getDescription());
-           $productVariation->setIngredients($product->getIngredients());
-           $productVariation->setOrigin($product->getOrigin());
-           $productVariation->setAvailable(1);
-           $tags = $product->getTags();
-           foreach($tags as $tag) {
-             $productVariation->addTag($tag);
-           }
-           $sp = new Storage();
-           $sp->addProduct($productVariation);
-           $sp->setType("product");
-           $this->entityMana->persist($sp);
-           $sb = new Storage();
-           $sb->addProduct($productVariation);
-           $sb->setType("box");
-           $this->entityMana->persist($sb);
-           $ss = new Storage();
-           $ss->addProduct($productVariation);
-           $ss->setType("skid");
-           $this->entityMana->persist($ss);
-           $p = new Pricing();
-           $p->setPrice("0.00");
-           $p->setCurrency("CAD");
-           $p->setStorage($sp);
-           $p->setQuantity(1);
-           $productVariation->addPricing($p);
-           $productVariation->setCompany($company);
-           $productVariation->setIsConsumer(true);
-         //   $productVariation->setIsDisplayedInAssociationOnly(count($company->getAssociations())>0);
-           $productVariation->setIsDisplayedInAssociationOnly(0);
-           $this->entityMana->persist($productVariation);
-           $this->entityMana->flush();
-           return $this->redirect("/vendeur/produit/editer/".$product->getId());
-         }
-         $twigData = array();
-         $twigData['productEntry']= true;
-         $twigData['company'] = $product->getCompany();
-         $twigData['product'] = $product;
-         $companyProducts = $this->entityMana->getRepository(Product::class)->findBy([
-           'company' => $product->getCompany(),
-           'parentProductId' => null,
-         ]);
-         $twigData['companyProducts'] = $companyProducts;
-         $twigData['parent'] = false;
-         if ($product->getParentProductId()) {
-           $productParent = $this->entityMana->getRepository(Product::class)->findOneBy([
-             'id' => $product->getParentProductId(),
-           ]);
-           if ($productParent) {
-             $twigData['parent'] = $productParent;
-           }
-         }
-         $twigData['variations'] = [];
-         $productVariations = $this->entityMana->getRepository(Product::class)->findBy([
-           'parentProductId' => $product->getId(),
-           'available' => 1
-         ]);
-         if ($productVariations) {
-           $twigData['variations'] = $productVariations;
-         }
-         $twigData['editProduct'] = $product->getId();
-         if(!$request->isXmlHttpRequest() && $product->getPricings()->count() < 1){
-             $product->addPricing(new Pricing());
-         }
-         $twigData['pageTitle'] = 'Edition du produit '.$product->getName();
-        
-         $form = $this->createForm(ProductType::class, $product);
-         $form->handleRequest($request);
-         $twigData['addNewForm'] = $form->createView();
-         if($request->isXmlHttpRequest()){
-             //Ok let's save that new product
-             $newProduct = $form->getData();
-             // if ($newProduct->getAlimentsDuQuebec() == true){
-             //     $findAlimentDuQuebec = $this->entityMana->getRepository(Certification::class)->findBy(['name' => 'Aliments du Québec']);
-             //     $newProduct->setAlimentsDuQuebecCertification($findAlimentDuQuebec[0]->getId());
-             // }
-             $this->entityMana->persist($newProduct);
-             $this->entityMana->flush();
-             $this->saveDataFromForm($newProduct, $request, $imageServ, $userServ, true);
-             //Let's update Solex if the product is already with them
-             //if($_ENV['APP_ENV'] != 'dev'){
-                 if($newProduct->isShippedByMaturin() && !empty($newProduct->getSolexId())){
-                     $distributorServ->setProduct($newProduct);
-                 }elseif($newProduct->isShippedByMaturin() && $newProduct->getIsJustInTime()){
-                     $distributorServ->setProduct($newProduct);
-                 }
-             //}
-             $response = new JsonResponse();
-             $response->setData(array(
-                 'success' => 1
-             ));
-             return $response;
-         }
-         return $twigData;
-     }
-     /**
-      * @Route({
-      *      "fr": "/livraison/priceDetail/ajax_search/{isFrozen}/{retailPrice}",
-      *      "en": "/delivery/priceDetail/ajax_search/{isFrozen}/{retailPrice}"
-      *      },
-      *      name="priceDetailAJAXSearch"
-      * )
-      *@Security("has_role('ROLE_ADMIN')")
-      */
-     public function priceDetailAJAXSearch($isFrozen, $retailPrice,PaymentService $paymentService)
-     {
-         $result = [];
-         $CommandPreparationfees = 1.10;
-         $wrapingFees = 0.55;
-         if ($isFrozen == 1) {
-             $wrapingFees = 1.15;
-         }
-         $result['CommandPreparationfees'] = $CommandPreparationfees;
-         $result['wrapingFees'] = $wrapingFees;
-         if($retailPrice <= 6)
-             $maturinPercentFees = round($retailPrice * 0.085, 2); //8.5%
-         else
-             $maturinPercentFees = round($retailPrice * 0.115, 2); //11.5%
-         $result['maturinPercentFees'] = $maturinPercentFees;
-         $creditPercentFees = round($retailPrice * 0.035, 2); //3.5%
-         $result['creditPercentFees'] = $creditPercentFees;
-         $fees = round($maturinPercentFees +  $creditPercentFees + $CommandPreparationfees + $wrapingFees, 2);
-         $result['feesTotal'] = $fees;
-         $totalTaxable = $fees;
-         $totaltaxes = $paymentService->calculateTaxes($totalTaxable, "QC");
-         $taxesToPay = round($totaltaxes['totalTax'], 2);
-         $result['taxesToPay'] = $taxesToPay;
-         $feesTotal = $fees + $taxesToPay;
-         $amountReceived = round($retailPrice - $feesTotal, 2);
-         $result['amountReceived'] = $amountReceived;
-         $amountAfterTaxes = round($amountReceived +  $taxesToPay, 2);
-         $result['amountAfterTaxes'] =  $retailPrice - $fees;
-         $totalFees=($retailPrice - $amountAfterTaxes)/$retailPrice;
-         $result['totalFees'] =$totalFees*100;
-         $beneficeMarge = 1 - $totalFees;
-         $result['beneficeMarge'] =  $beneficeMarge*100;
-         $buyerCost = round($retailPrice / 0.92, 2); // 8% fees which the buyer see;
-         $result['buyerCost'] = $buyerCost;
-         die(json_encode(["results" => $result]));
-     }
-     /**
-      * @route({
-      *          "fr": "/vendeur/produit/nouveau/{id}/{cloneProductId}",
-      *          "en": "/seller/product/new/{id}/{cloneProductId}"
-      *      },
-      *      options = {
-      *          "expose" = true
-      *      },
-      *      defaults={
-      *          "cloneProductId" = false,
-      *          "id" = false
-      *      },
-      *      name="adminNewProduct")
-      * @Security("has_role('ROLE_ADMIN')")
-      * @Template("admin/product/product.html.twig")
-      */
-     public function newProduct(Request $request, UserService $userServ, ImageService $imageServ, Company $company=null, ProductService $ProductService, $cloneProductId, UrlGeneratorInterface $router, DistributorService $distributorServ){
-         $twigData = array();
-         $twigData['productEntry']= true;
-         $user = $userServ->getUser();
-         if(empty($company)){
-             $companies = $user->getCompanies();
-             if(count($companies) > 1){
-                 $this->addFlash('error', "Vous avez plusieur boutiques, pour accéder au paiement utiliser le lien Info & Dépot dans le menu sous l'onglet 'Paramètre'");
-                 return $this->redirectToRoute('dashboard');
-             }else{
-                 $company = $companies->get(0);
-                 return $this->redirectToRoute('adminNewProduct', array('id' => $company->getId()));
-             }
-         }
-         if(empty($company)){
-             $this->addFlash('error', "Nous n'arrivons pas a trouver votre boutique, utiliser le lien Info & Dépot dans le menu sous l'onglet 'Paramètre'");
-             return $this->redirectToRoute('dashboard');
-         }
-         if($cloneProductId){
-             $cloneProduct = $ProductService->getProductById($cloneProductId);
-             $cloneProduct->setMainImage(null);
-             $product = clone $cloneProduct;
-             //$product->purgeImages();
-             //$product->purgeMainImage();
-             $product->setNutritionFact(NULL);
-             $product->setValidatedByDistributor(false);
-             $product->setSolexId(null);
-             $product->setQtyReadyToShip(0);
-             $product->setId(NULL);
-             // $product->setIsDisplayedInAssociationOnly($cloneProduct->getIsDisplayedInAssociationOnly());
-             $product->setIsDisplayedInAssociationOnly(0);
-             $product->purgeImages();
-             //Clone subscriptions too
-             if($cloneProduct->getIsSubscribable()){
-                 $product->setIsSubscribable($cloneProduct->getIsSubscribable());
-                 $product->setIsOnlySubscribable($cloneProduct->getIsOnlySubscribable());
-                 $product->setIsOnlySubscribableToType($cloneProduct->getIsOnlySubscribableToType());
-             }
-         }else{
-             $product = new Product();
-             if(!$request->isXmlHttpRequest()){
-                 $p = new Pricing();
-                 $p->setQuantity(1);
-                 $product->addPricing($p);
-             }
-             $product->setCompany($company);
-             $product->setIsConsumer(true);
-             // $product->setIsDisplayedInAssociationOnly(count($company->getAssociations())>0);
-             $product->setIsDisplayedInAssociationOnly(0);
-         }
-         $twigData['product'] = $product;
-         $form = $this->createForm(ProductType::class, $product);
-         //Add the company if the user is part of that company
-         if(!$userServ->getUser()->getCompanies()->contains($company) && !$userServ->isGod())
-             die('Error, you arent suppose to see this. Reported');
-         $twigData['company'] = $company;
-         $form->handleRequest($request);
-         $twigData['addNewForm'] = $form->createView();
-         if($request->isXmlHttpRequest()){
-             //Ok let's save that new product
-             //$newProduct = $form->getData();
-             $newProduct = $product;
-             //Set the company
-             $newProduct->setCompany($company);
-             if(!empty($request->get('draft')))
-                 $newProduct->setDraft(true);
-             $this->entityMana->persist($newProduct);
-             $this->entityMana->flush();
-             $this->saveDataFromForm($newProduct, $request, $imageServ, $userServ);
-             if(empty($request->get('draft'))){
-                 if($newProduct->isShippedByMaturin() && !$newProduct->getIsJustInTime()){
-                     //Product saved, let's send a notification
-                     $urlInventory = $router->generate( 'replenishment', array ('_locale' => $userServ->getUser()->getLocale(), 'id' => $newProduct->getCompany()->getId()));
-                     $userServ->addNotification('info', 'Mise en inventaire', 'Veuillez planifier un rendez-vous pour la mise en inventaire du produit '.$newProduct->getName().' <a href="'.$urlInventory.'">ici</a>');
-                 }
-                 if($newProduct->isShippedByMaturin() && $newProduct->getIsJustInTime()){
-                     $distributorServ->setProduct($newProduct);
-                 }
-                 $response = new JsonResponse();
-                 $response->setData(array(
-                     'success' => 1
-                 ));
-                 return $response;
-             }else{
-                 //It's a draft
-                 $response = new JsonResponse();
-                 $response->setData(array(
-                     'success' => 1,
-                     'preview' => $this->generateUrl('viewProduct', array('producerUrl' => $newProduct->getCompany()->getUrlName(), 'productName' => $newProduct->getUrlName(), 'id' => $newProduct->getId()))
-                 ));
-                 return $response;
-             }
-         }
-         return $twigData;
-     }
-     /*
-      * @TODO move all this in the form instead
-      */
-     private function saveDataFromForm($newProduct, $request, $imageServ, $userServ, $edit=false){
-         //Let's start by deleting the temporary images not needed
-         $delImages = json_decode($request->get('imagesToDelete'));
-         foreach($delImages as $delImage){
-                 $imageServ->deleteImage($delImage, false, $newProduct);
-         }
-         //Let's build the Images for the new product
-         $Images = json_decode($request->get('imagesAndDescriptions'));
-         $order = 1;
-         foreach($Images as $newImage){
-             $image = $imageServ->getImage($newImage->id);
-             if($image){
-                 $image->setDescription($newImage->description);
-                 $image->setDisplayOrder($order);
-                 $image->setCategory('product');
-                 $image->addProduct($newProduct);
-                 $this->entityMana->persist($image);
-                 if($order == 1)
-                     $newProduct->setMainImage($image);
-                 $order++;
-             }
-         }
-         $this->entityMana->flush();
-         //Erasing the pricing before saving them
-         //Easier for editing right now
-         if(!empty($request->get('prices'))){
-             $pricings = $newProduct->getPricings();
-             foreach($pricings as $pricing){
-                 $newProduct->removePricing($pricing);
-                 $this->entityMana->remove($pricing);
-             }
-             $this->entityMana->flush();
-             //Building the pricing
-             //Need to be incorporated in the Forms at some point
-             $pricings = json_decode($request->get('prices'));
-             foreach($pricings as $pricing){
-                 $price = new Pricing();
-                 $price->setCurrency('CAD');
-                 $price->setPrice($pricing->price);
-                 $price->addProduct($newProduct);
-                 if(!empty($pricing->qty) && $this->userServ->isGod())
-                     $price->setQuantity($pricing->qty);
-                 else
-                     $price->setQuantity(1);
-                 /*
-                  * Old code put at some point to bypass the code above
-                  * forgot why it was removed or forced to 1
-                  */
-                 //$price->setQuantity(1);
-                 $storage_found = false;
-                 foreach($newProduct->getStorages() as $storage){
-                     if($storage->getType() == $pricing->unit){
-                         $storage_found = true;
-                         $price->setStorage($storage);
-                         break;
-                     }
-                 }
-                 if (!$storage_found) {
-                   $s = new Storage();
-                   $s->setType($pricing->unit);
-                   $s->setQuantityProduct(0);
-                   $newProduct->addStorage($s);
-                   $price->setStorage($s);
-                 }
-                 /*
-                 if($pricing->unit == 'vrac' && !$vrac){
-                     $s = new Storage();
-                     $s->setType('vrac');
-                     $s->setQuantityProduct(0);
-                     $newProduct->addStorage($s);
-                     $price->setStorage($s);
-                 }
-                  */
-                 $this->entityMana->persist($price);
-                 $newProduct->addPricing($price);
-             }
-         }
-         //Building the tags
-         $formTags = json_decode($request->get('tags'));
-         $newTags = new ArrayCollection();
-         foreach($formTags as $tag){
-             $newTag = $this->entityMana->getRepository(Tag::class)->findOneByName($tag);
-             //One with this name already exist in the database, let's link to that one
-             if(empty($newTag)){
-                 //This tag don't exist let's create
-                 $newTag = new Tag();
-                 $newTag->setName(trim($tag));
-                 $newTag->setLocale($userServ->getUser()->getLocale());
-                 $this->entityMana->persist($newTag);
-             }
-             $newProduct->addTag($newTag);
-             $newTags[]=$newTag;
-         }
-         //For when editing we need to remove those not good too
-         if(!empty($formTags)){
-             foreach($newProduct->getTags() as $t){
-                 if(!$newTags->contains($t))
-                     $newProduct->removeTag($t);
-             }
-         }
-         //Fix a weird bug with the Ajax category refresh
-         if(!empty($_POST['product']['subCategory']) && is_numeric($_POST['product']['subCategory'])){
-             $category = $this->entityMana->getRepository(category::class)->find($_POST['product']['subCategory']);
-             $newProduct->setSubCategory($category);
-         }
-         //Fix a bug when switching from Independant shipping back to Maturin
-         if($newProduct->isShippedByMaturin()){
-             if($newProduct->getQtyReadyToShip() == 1)
-                 $newProduct->setQtyReadyToShip(0);
-         } else {
-             $newProduct->setQtyReadyToShip(1);
-         }
-         // ugly fix
-         if (!empty($_POST['product']['qtyReadyToShip']) && is_numeric($_POST['product']['qtyReadyToShip'])
-             && ((int)$_POST['product']['qtyReadyToShip']) > 1){
-             // product[qtyReadyToShip]
-             $newProduct->setQtyReadyToShip((int)$_POST['product']['qtyReadyToShip']);
-         }
-         //End of saving the Ingredients
-         $this->entityMana->persist($newProduct);
-         $this->entityMana->flush();
-     }
-     /**
-      * @route( "/vendeur/produits/update/{id}",
-      *      name="adminUpdateProducts")
-      */
-     public function updateProducts(Company $company, DistributorService $distributorServ, Request $request){
-         if($company->hasInventoryWithMaturin()){
-             $distributorServ->updateProductsOf($company);
-         }
-         $this->addFlash('success', 'Mise a jour fait de '.$company->getName());
-         $url = $_SERVER['HTTP_REFERER'];
-         return $this->redirect($url, 301);
-     }
-     /**
-      * @route({
-      *          "fr": "/vendeur/produits/liste/{id}",
-      *          "en": "/seller/product/list/{id}"
-      *      },
-      *      defaults = {
-      *          "id" = false
-      *      },
-      *      options = { "expose" = true },
-      *      name="adminListProducts")
-      * @Template("admin/product/list.html.twig")
-      */
-     public function listProduct(ProductService $ProductService, EntityManagerInterface $em, Request $request, Company $company=null, DistributorService $distributorServ, UserService $userServ){
-         $twigData = array();
-         if(empty($company)){
-             $user = $userServ->getUser();
-             $companies = $user->getCompanies();
-             if(count($companies) > 1){
-                 $this->addFlash('error', "Vous avez plusieur boutiques, pour accéder a l'inventaire via le menu");
-                 return $this->redirectToRoute('dashboard');
-             }else{
-                 $company = $companies->get(0);
-             }
-         }
-         //@SECuritY
-         if(!$this->companyServ->allowedToModify($company) || empty($company)){
-             $this->addFlash('error', "Vous n'avez pas d'accès Ã  cette boutique, veuillez utiliser le menu");
-             return $this->redirectToRoute('dashboard');
-         }
-         /*
-          * Conflict with the cart, when a sale occur the quantity change
-          * If the producer check the list of product when a order was done but not shipped yet
-          * The quantity will reset to the one in Inventory which can make a "sale" happen
-          * as quantity is available again
-          */
-         if($company->hasInventoryWithMaturin()) 
-             $distributorServ->updateProductsOf($company);
-     $twigData['products'] = $em->getRepository(Product::class)
-             ->findBy([
-                 'company' => $company,
-                 'draft' => false
-             ] );
-         $twigData['company']=$company;
-                 // $dateExpirations = [];
-                 // // dump($twigData['products']);
-                 
-                 //     for($i=0; $i < count($twigData['products']);$i++){
-                 //         // dump($dateExpirations);
-                 //         //if (in_array($twigData['products'][$i]->getOrigName(), $dateExpirations)){
-                 //             // dump('nom existe');
-                 //         //}else{
-                 //             if(empty($dateExpirations)){
-                 //                 $dateExpirations[] = [ 
-                 //                     'id' => $twigData['products'][$i]->getId(),
-                 //                     'origName' => $twigData['products'][$i]->getOrigName(),
-                 //                     'expirationDate' =>$twigData['products'][$i]->getExpirationDate()
-                 //                 ];
-                 //             }else{
-                 //                 if ($twigData['products'][$i]->getExpirationDate() < $twigData['products'][$i-1]->getExpirationDate()){
-                 //                     $dateExpirations[] = [ 
-                 //                         'id' => $twigData['products'][$i]->getId(),
-                 //                         'origName' => $twigData['products'][$i]->getOrigName(),
-                 //                         'expirationDate' =>$twigData['products'][$i]->getExpirationDate()
-                 //                     ];
-                 //                 }
-                 //             //     dump($twigData['products'][$i]->getOrigName()) ;
-                 //             //    dump($twigData['products'][$i]->getExpirationDate()) ;
-                 //             }
-                            
-                 //         }
-                 //     //}
-                 //     $twigData['expirationDates'] = $dateExpirations;
-          
-                 
-             
-          
-         return $twigData;
-         
-     }
-     /**
-      * @Route("/API/setProduct/{action}",
-      *      options = { "expose" = true },
-      *      name = "setProductByAction"
-      * )
-      *
-      * @Security("has_role('ROLE_ADMIN')")
-      *
-      */
-     public function setProductByActionFromAPI($action, Request $request, ProductService $productServ){
-         $action = strtolower($action);
-         $success = false;
-         switch($action){
-             case 'available':
-                 $id = $request->get('id');
-                 $available = $request->get('available');
-                 $product = $productServ->getProductById($id);
-                 if($productServ->allowedToModify($product)){
-                     $product->setAvailable($available);
-                     $this->entityMana->persist($product);
-                     $this->entityMana->flush();
-                     $success = true;
-                 }
-                 break;
-             case 'delete':
-                 $id = $request->get('id');
-                 if($productServ->deleteProductById($id))
-                     $success = true;
-                 break;
-         }
-         $response = new jsonresponse();
-         $response->setdata(array(
-             'success' => $success
-         ));
-             return $response;
-     }
-     /**
-      * @Route("/API/getProduct/{action}",
-      *      options = { "expose" = true },
-      *      name = "getProductByAction"
-      * )
-      *
-      */
-     public function getProductByActionFromAPI($action, Request $request, ProductService $productServ, PaginatorInterface $paginator, UserService $userServ, $forcedValues = false, $returnJSON = true){
-         $action = strtolower($action);
-         $success = false;
-         $data = array();
-         switch($action){
-             case 'getcategories':
-                 $id = $request->get('id');
-                 $category = $this->entityMana->getRepository(Category::class)->findOneBy(['id' => $id]);
-                 $subCategory = $this->entityMana->getRepository(Category::class)->getSubCategories($category, true);
-                 if($subCategory){
-                     $group = array();
-                     foreach($subCategory as $sub){
-                         $group[$sub->getSubcategory()->getName()][] = "<option value='".$sub->getId()."'>".$sub->getName()."</option>";
-                     }
-                     $data = '';
-                     foreach($group as $key => $choice){
-                         $data .= '<optgroup label="'.$key.'">'.implode('', $choice).'</optgroup>';
-                     }
-                     $success = true;
-                 }
-                 break;
-             case 'getproductsbytokenpagination':
-                 /*
-                 $page = $request->get('page');
-                 $token = $request->get('token');
-                 $search = $this->entityMana->getRepository(UserSearch::class)->findOneBy(['token' => $token]);
-                  */
-                 if($search){
-                     $forcedValues = array(
-                         'page' => $page,
-                         'filters' => $search->getRawFilters(),
-                         'token' => $token
-                     );
-                     return $this->getProductByActionFromAPI($search->getSearch(), $request, $productServ, $paginator, $userServ, $forcedValues);
-                 }
-                 break;
-             case 'getproductsbyfiltering':
-                 $data = array(
-                     'html' => '',
-                     'amount' => '0'
-                 );
-                 $page = 1;
-                 if($forcedValues){
-                     $filters = json_decode($forcedValues['filters']);
-                     $page = $forcedValues['page'];
-                 }else
-                     $filters = json_decode($request->get('filters'));
-                 $filter['pricings']=array();
-                 $filter['conservation']=array();
-                 $filter['region']=array();
-                 $filter['deliveryType']=array();
-                 foreach($filters as $k => $f){
-                     switch(strtolower($f->type)){
-                         case 'aliment_quebec':
-                             $filter['alimentsDuQuebec']=1;
-                             break;
-                         case 'prix_laureat':
-                             $filter['isPrixLaureat']=1;
-                             break;
-                         case 'certification':
-                             $filter['alimentsDuQuebecCertification']=$f->id;
-                             break;
-                         case "category":
-                             $cat = $this->entityMana->getRepository(Category::class)->findOneBy(array('id' => $f->id));
-                             if($cat){
-                                 $products = $this->productServ->getProductsFromCategory($cat, false);
-                             }
-                             break;
-                         case 'shipping':
-                             $filter['DeliveryType'][] = $f->id;
-                             break;
-                         case "price":
-                             $filter['pricings'][]=$f->id;
-                             break;
-                         case "region":
-                             $filter['region'][]=$this->entityMana->getRepository(Region::class)->findOneBy(array('id' => $f->id));
-                             break;
-                         case "conservation":
-                             $filter['conservation'][]=$this->entityMana->getRepository(Conservation::class)->findOneBy(array('id' => $f->id));
-                             break;
-                         case "page":
-                             if(empty($forcedValues))
-                                 $page = $f->id;
-                             break;
-                     }
-                 }
-                 //If no category and selecting at large
-                 if(empty($products))
-                     $products = $this->entityMana->getRepository(Product::class)->getBaseProductsForSearching();
-                 $join = false;
-                 foreach($filter as $k => $v){
-                     if(!empty($v)){
-                         if($k == 'pricings'){
-                             $pricing = array();
-                             foreach($v as $p){
-                                 //Because of the multiple here, we need to do a query build
-                                 //Let's not forget that Maturin has a 8% increase in price so this is why there is a double
-                                 //query here
-                                 switch($p){
-                                     case '0';
-                                         $now = new \DateTime();
-                                         $products->setParameter('now', $now);
-                                         $products->leftJoin('p.discountPricings', 'discount');
-                                         $products->andWhere('discount.startDate <= :now');
-                                         $products->andWhere('discount.endDate >= :now');
-                                         $pricing[] = '(discount.isADiscount = 1)';
-                                         break;
-                                     case '10';
-                                         $mPricing = 10 - (10 * 0.08);
-                                         $pricing[] = '(pricing.price < 10 and p.DeliveryType = 1)';
-                                         $pricing[] = '(pricing.price < '.$mPricing.' and p.DeliveryType = 0)';
-                                         break;
-                                     case '10-25';
-                                         $minPricing = 10 - (10 * 0.08);
-                                         $maxPricing = 25 - (25 * 0.08);
-                                         $pricing[] = '(pricing.price >= 10 and pricing.price < 25 and p.DeliveryType=1)';
-                                         $pricing[] = '(pricing.price >= '.$minPricing.' and pricing.price < '.$maxPricing.' and p.DeliveryType=0)';
-                                         break;
-                                     case '25-50';
-                                         $minPricing = 25 - (25 * 0.08);
-                                         $maxPricing = 50 - (50 * 0.08);
-                                         $pricing[] = '(pricing.price >= 25 and pricing.price < 50 and p.DeliveryType=1)';
-                                         $pricing[] = '(pricing.price >= '.$minPricing.' and pricing.price < '.$maxPricing.' and p.DeliveryType=0)';
-                                         break;
-                                     case '50';
-                                         $maxPricing = 50 - (50 * 0.08);
-                                         $pricing[] = '(pricing.price >= 50 and p.DeliveryType=1)';
-                                         $pricing[] = '(pricing.price >= '.$maxPricing.' and p.DeliveryType=0)';
-                                         break;
-                                 }
-                             }
-                             if(!empty($pricing) && count($pricing) > 0){
-                                 if(!$join){
-                                     $products->leftJoin('p.pricings', 'pricing');
-                                     $join = true;
-                                 }
-                                 if(count($pricing) == 1){
-                                     $products->andWhere(current($pricing));
-                                 }else{
-                                     $query = implode(' OR ', $pricing);
-                                     $products->andWhere($query);
-                                 }
-                             }
-                         }else{
-                             //If it's a array
-                             if(is_array($v)){
-                                 $products->andWhere("p.$k IN ( :filter_".$k.")");
-                                 $products->setParameter('filter_'.$k, $v);
-                             }else{
-                                 $products->andWhere("p.$k = :filter_".$k);
-                                 $products->setParameter('filter_'.$k, $v);
-                             }
-                         }
-                     }
-                 }
-                 $paginatorProducts = false;
-                 if(!empty($products)){
-                     $products = $products->getQuery();
-                     if(!$returnJSON)
-                         return $products;
-                     // Paginate the results of the query
-                     $twigData['products'] = $paginator->paginate(
-                         $products,
-                         $request->query->getInt('page', $page),
-                         24
-                     );
-                     $data['amount'] = $twigData['products']->getTotalItemCount();
-                     $products = $products->getResult();
-                     //Let's save the search for pagination and others
-                     $twigData['ajax'] = true;
-                     $twigData['masonryProducts'] = $products;
-                     if(!$forcedValues){
-                         $twigData['searchToken'] = uniqid();
-                         $userServ->saveSearch($action, $data['amount'], 'adv', $twigData['searchToken'], $request->get('filters'));
-                     }else{
-                         $twigData['searchToken'] = $forcedValues['token'];
-                     }
-                     $data['html'] = $this
-                         ->get('twig')
-                         ->render('frontend/productMasonry.html.twig', $twigData);
-                 }else{
-                     $products = $this->entityMana->getRepository(Product::class)->getSalableProducts();
-                     $data['html'] = $this
-                         ->get('twig')
-                         ->render('frontend/productMasonry.html.twig', array(
-                             'masonryProducts' => $products,
-                             'ajax' => true
-                     ));
-                 }
-                 $success = true;
-                 break;
-         }
-         $response = new jsonresponse();
-         if($returnJSON){
-             if(!empty($twigData['searchToken'])){
-                 $response->setdata(array(
-                     'success' => $success,
-                     'data' => $data,
-                     'token' => $twigData['searchToken']
-                 ));
-             }else{
-                 $response->setdata(array(
-                     'success' => $success,
-                     'data' => $data,
-                     'token' => ''
-                 ));
-             }
-             return $response;
-         }else{
-             return $rawResults;
-         }
-     }
- }
-