src/Controller/LicenseController.php line 400

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\License;
  4. use App\Repository\LicenseRepository;
  5. use App\Form\LicenseType;
  6. use App\Form\LicenseSearchType;
  7. use App\Entity\Company;
  8. use App\Entity\LicenseCompany;
  9. use App\Repository\LicenseCompanyRepository;
  10. use App\Form\LicenseCompanyType;
  11. use App\Form\LicenseCompanySearchType;
  12. use App\Service\UtilService;
  13. use Doctrine\DBAL\Types\TextType;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Knp\Component\Pager\PaginatorInterface;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  22. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  23. use Symfony\Contracts\Translation\TranslatorInterface;
  24. use Doctrine\ORM\Query\Expr\Join;
  25. use Doctrine\Persistence\ManagerRegistry;
  26. /**
  27.  * @Route({"en": "/license",
  28.  *          "es": "/licencia"})
  29.  */
  30. class LicenseController extends AbstractController
  31. {
  32.     public const PAGE_ELEMENTS 25;
  33.     private $translator;
  34.     private $util;
  35.     private $em;
  36.     private $method "AES-256-CBC";
  37.     private $key "AD2#s3cret0";
  38.     private $options 0;
  39.     private $iv '2234547891511121';
  40.     /**
  41.      * LicenseController constructor.
  42.      */
  43.     public function __construct(
  44.         TranslatorInterface $translator,
  45.         ManagerRegistry $doctrine,
  46.         UtilService $utilService)
  47.     {
  48.         $this->translator $translator;
  49.         $this->util $utilService;
  50.         $this->em $doctrine->getManager();
  51.     }
  52.     /**
  53.      * @Route({"en": "/list",
  54.      *         "es": "/listado"}, name="license_index", methods={"GET","POST"})
  55.      */
  56.     public function index(Request $requestLicenseCompanyRepository $licenseCompanyRepositoryPaginatorInterface $paginator): Response
  57.     {
  58.         $em $this->em;
  59.         $license = [];
  60.         $form $this->createForm(licenseCompanySearchType::class, $license, ['method' => 'GET']);
  61.         $form->handleRequest($request);
  62.         $orderBy null;
  63.         $filters = [
  64.             ['field'=>'license','title'=>'Licencia'],
  65.             ['field'=>'company','title'=>'Empresa'],
  66.             ['field'=>'date_added','title'=>'Alta'],
  67.             ['field'=>'date_start','title'=>'Inicio'],
  68.             ['field'=>'date_end','title'=>'Fin'],
  69.             ['field'=>'state','title'=>'Estado']
  70.         ];
  71.         if ($form->isSubmitted() && $form->isValid())
  72.         {
  73.             $orderBy $request->query->get('order_by');
  74.             $licenses $licenseCompanyRepository->findByName($form->getData(), $orderBy);
  75.         } else {
  76.             $licenses $licenseCompanyRepository->buscarTodos(2);
  77.         }
  78.         // Creating pagnination
  79.         $pagination $paginator->paginate(
  80.             $licenses,
  81.             $request->query->getInt('page'1),
  82.             self::PAGE_ELEMENTS
  83.         );
  84.         $lc = new LicenseCompany();
  85.         $formLc $this->createForm(licenseCompanyType::class, $lc);
  86.         $formLc->handleRequest($request);
  87.         if ($formLc->isSubmitted() && $formLc->isValid())
  88.         {
  89.             $today = new \DateTime();
  90.             $today->setTime(0,0,0);
  91.             $company $formLc->get('company')->getData();
  92.             $license $formLc->get('license')->getData();
  93.             $renewal $formLc->get('renewal')->getData();
  94.             $renewalType $formLc->get('renewal_type')->getData();
  95.             $start $formLc->get('date_start')->getData();
  96.             $end $formLc->get('date_end')->getData();
  97.             if(empty($company) || empty($license)){
  98.                 $this->addFlash('danger'$this->translator->trans('Debe seleccionar empresa y licencia para poder efectuar la asignación'));
  99.                 return $this->redirectToRoute('license_index');
  100.             }
  101.             elseif(empty($start) || empty($end)){
  102.                 $this->addFlash('danger'$this->translator->trans('Debe seleccionar inicio y fin de la licencia'));
  103.                 return $this->redirectToRoute('license_index');
  104.             }
  105.             elseif($today>$end || $start>=$end){
  106.                 $this->addFlash('danger'$this->translator->trans('Revise las fechas de inicio y fin de la licencia'));
  107.                 return $this->redirectToRoute('license_index');
  108.             }
  109.             elseif($company->getType()==&& !in_array($license->getType(),[1,2])){
  110.                 $this->addFlash('danger'$this->translator->trans('A las empresas tipo service bureaus solo se pueden asignar licencias de tipo service bureau'));
  111.                 return $this->redirectToRoute('license_index');
  112.             }
  113.             elseif($company->getType()==&& in_array($license->getType(),[1,2])){
  114.                 $this->addFlash('danger'$this->translator->trans('No se pueden asignar licencias service bureau a un OEM'));
  115.                 return $this->redirectToRoute('license_index');
  116.             }
  117.             $lc->setDateAdded(new \DateTime());
  118.             $lc->setState($start>$today 2);
  119.             if(empty($renewal)){
  120.                 $lc->setRenewal(1);
  121.             }
  122.             $lc->setRenewalType($renewalType);
  123.             $em->persist($lc);
  124.             //Si estamos renovando una licencia vencida la convertimos a renovada
  125.             $lcOld $company->getLicenseCompany();
  126.             if($lcOld && $lcOld->getState()==3){
  127.                 $lcOld->setState(4);
  128.             }
  129.             //Asignamos nueva licencia a la empresa
  130.             $company->setLicenseCompany($lc);
  131.             $em->flush();
  132.             $this->addFlash('success'$this->translator->trans('Asignación realizada correctamente'));
  133.             return $this->redirectToRoute('license_index');
  134.         }
  135.         return $this->render('license/index.html.twig', [
  136.             'pagination' => $pagination,
  137.             'search_form' => $form->createView(),
  138.             'assign_form' => $formLc->createView(),
  139.             'filters' => $filters,
  140.             'order_by' => $orderBy,
  141.             'navbarTitle' => $this->translator->trans("Licencias contratadas")
  142.         ]);
  143.     }
  144.     /**
  145.      * @Route({"en": "/assigned/edit/{id}",
  146.      *         "es": "/asignada/editar/{id}"}, name="license_assigned_edit", methods={"GET","POST"})
  147.      */
  148.     public function assigned_edit(Request $requestLicenseCompany $lc): Response
  149.     {
  150.         $em $this->em;
  151.         $form $this->createForm(licenseCompanyType::class, $lc);
  152.         $form->handleRequest($request);
  153.         if ($form->isSubmitted() && $form->isValid())
  154.         {
  155.             $today = new \DateTime();
  156.             $today->setTime(0,0,0);
  157.             $company $lc->getCompany();
  158.             $license $form->get('license')->getData();
  159.             $start $form->get('date_start')->getData();
  160.             $end $form->get('date_end')->getData();
  161.             if(empty($license)){
  162.                 $this->addFlash('danger'$this->translator->trans('Debe seleccionar licencia para poder efectuar la asignación'));
  163.                 return $this->redirectToRoute('license_index');
  164.             }
  165.             elseif(empty($start) || empty($end)){
  166.                 $this->addFlash('danger'$this->translator->trans('Debe seleccionar inicio y fin de la licencia'));
  167.                 return $this->redirectToRoute('license_index');
  168.             }
  169.             elseif($today>$end || $start>=$end){
  170.                 $this->addFlash('danger'$this->translator->trans('Revise las fechas de inicio y fin de la licencia'));
  171.                 return $this->redirectToRoute('license_index');
  172.             }
  173.             elseif($company->getType()==&& !in_array($license->getType(),[1,2])){
  174.                 $this->addFlash('danger'$this->translator->trans('A las empresas tipo service bureaus solo se pueden asignar licencias de tipo service bureau'));
  175.                 return $this->redirectToRoute('license_index');
  176.             }
  177.             elseif($company->getType()==&& in_array($license->getType(),[1,2])){
  178.                 $this->addFlash('danger'$this->translator->trans('No se pueden asignar licencias service bureau a un OEM'));
  179.                 return $this->redirectToRoute('license_index');
  180.             }
  181.             $lc->setState($start>$today 2);
  182.             $lc->setDateUpdated(new \DateTime());
  183.             $em->flush();
  184.             $this->addFlash('success'$this->translator->trans('Asignación editada correctamente'));
  185.             return $this->redirectToRoute('license_index');
  186.         }
  187.         return $this->render('license/assigned_edit.html.twig', [
  188.             'form' => $form->createView(),
  189.             'lc' => $lc,
  190.             'navbarTitle' => $this->translator->trans("Editar licencia asignada")
  191.         ]);
  192.     }
  193.     /**
  194.      * @Route({"en": "/enterprise/new",
  195.      *         "es": "/enterprise/nueva"}, name="license_enterprise_new", methods={"GET","POST"})
  196.      */
  197.     public function enterprise_new(Request $requestLicenseRepository $licenseRepository): Response
  198.     {
  199.         $em $this->em;
  200.         $license = new License();
  201.         $companies $em->createQueryBuilder()
  202.             ->select('c')
  203.             ->from(Company::class,'c')
  204.             ->leftJoin('c.licenseCompany','lc')
  205.             ->where('lc.state IS NULL OR lc.state IN (3,5)')
  206.             ->getQuery()->getResult();
  207.         if(empty($companies)){
  208.             $this->addFlash('danger'$this->translator->trans('Todas las empresas tienen una licencia asignada'));
  209.             return $this->redirectToRoute('license_index');
  210.         }
  211.         $form $this->createForm(LicenseType::class, $license, ['lc'=>null]);
  212.         $form->handleRequest($request);
  213.         if ($form->isSubmitted() && $form->isValid()) {
  214.             $today = new \DateTime();
  215.             $today->setTime(0,0,0);
  216.             $company $form->get('company')->getData();
  217.             $start $form->get('date_start')->getData();
  218.             $end $form->get('date_end')->getData();
  219.             $renewal $form->get('renewal')->getData();
  220.             $renewal_type $form->get('renewal_type')->getData();
  221.             if(empty($company)){
  222.                 $this->addFlash('danger'$this->translator->trans('Debe seleccionar empresa a la que se asignará la licencia Enterprise'));
  223.                 return $this->redirectToRoute('license_enterprise_new');
  224.             }
  225.             elseif(empty($start) || empty($end)){
  226.                 $this->addFlash('danger'$this->translator->trans('Debe seleccionar inicio y fin de la licencia Enterprise'));
  227.                 return $this->redirectToRoute('license_enterprise_new');
  228.             }
  229.             elseif($today>$end || $start>=$end){
  230.                 $this->addFlash('danger'$this->translator->trans('Revise las fechas de inicio y fin de la licencia'));
  231.                 return $this->redirectToRoute('license_enterprise_new');
  232.             }
  233.             $license->setType(6);
  234.             $license->setUserAdmin(false);
  235.             $em->persist($license);
  236.             $lc = new LicenseCompany();
  237.             $lc->setLicense($license);
  238.             $lc->setCompany($company);
  239.             $lc->setDateAdded(new \DateTime());
  240.             $lc->setDateStart($start);
  241.             $lc->setDateEnd($end);
  242.             $lc->setRenewal($renewal);
  243.             $lc->setRenewalType($renewal_type);
  244.             $lc->setState($start>$today 2);
  245.             $em->persist($lc);
  246.             //Si estamos renovando una licencia vencida la convertimos a renovada
  247.             $lcOld $company->getLicenseCompany();
  248.             if($lcOld && $lcOld->getState()==3){
  249.                 $lcOld->setState(4);
  250.             }
  251.             //Asignamos nueva licencia a la empresa
  252.             $company->setLicenseCompany($lc);
  253.             $em->flush();
  254.             $this->addFlash('success'$this->translator->trans('Licencia creada correctamente'));
  255.             return $this->redirectToRoute('license_index');
  256.         }
  257.         return $this->render('license/enterprise_new.html.twig', [
  258.             'license' => $license,
  259.             'form' => $form->createView(),
  260.             'navbarTitle' => $this->translator->trans("Crear licencia Enterprise")
  261.         ]);
  262.     }
  263.     /**
  264.      * @Route({"en": "/enterprise/edit/{id}",
  265.      *         "es": "/enterprise/editar/{id}"}, name="license_enterprise_edit", methods={"GET","POST"})
  266.      */
  267.     public function enterprise_edit(Request $requestLicenseCompany $lc): Response
  268.     {
  269.         $em $this->em;
  270.         $company $lc->getCompany();
  271.         $license $lc->getLicense();
  272.         $form $this->createForm(LicenseType::class, $license, ['lc'=>$lc]);
  273.         $form->handleRequest($request);
  274.         if ($form->isSubmitted() && $form->isValid()) {
  275.             $today = new \DateTime();
  276.             $today->setTime(0,0,0);
  277.             $start $form->get('date_start')->getData();
  278.             $end $form->get('date_end')->getData();
  279.             $renewal $form->get('renewal')->getData();
  280.             $renewal_type $form->get('renewal_type')->getData();
  281.             if(empty($start) || empty($end)){
  282.                 $this->addFlash('danger'$this->translator->trans('Debe seleccionar inicio y fin de la licencia Enterprise'));
  283.                 return $this->redirectToRoute('license_enterprise_edit',['id'=>$lc->getId()]);
  284.             }
  285.             elseif($today>$end || $start>=$end){
  286.                 $this->addFlash('danger'$this->translator->trans('Revise las fechas de inicio y fin de la licencia'));
  287.                 return $this->redirectToRoute('license_enterprise_edit',['id'=>$lc->getId()]);
  288.             }
  289.             $license $form->getData();
  290.             $lc->setDateUpdated(new \DateTime());
  291.             $lc->setDateStart($start);
  292.             $lc->setDateEnd($end);
  293.             $lc->setRenewal($renewal);
  294.             $lc->setRenewalType($renewal_type);
  295.             $lc->setState($start>$today 2);
  296.             $em->flush();
  297.             $this->addFlash('success'$this->translator->trans('Licencia editada correctamente'));
  298.             return $this->redirectToRoute('license_index');
  299.         }
  300.         return $this->render('license/enterprise_edit.html.twig', [
  301.             'license' => $license,
  302.             'lc' => $lc,
  303.             'form' => $form->createView(),
  304.             'navbarTitle' => $this->translator->trans("Editar licencia Enterprise").' - '.$license->getName()
  305.         ]);
  306.     }
  307.     /**
  308.      * @Route({"en": "/delete/{id}",
  309.      *         "es": "/eliminar/{id}"}, name="license_delete", methods={"POST"})
  310.      */
  311.     public function delete(Request $requestlicenseCompany $lcLicenseRepository $licenseRepository): Response
  312.     {
  313.         $em $this->em;
  314.         $rmData $request->request->get('license_remove_data');
  315.         if(empty($lc)){
  316.             $this->addFlash('danger'$this->translator->trans('Error inesperado'));
  317.         }
  318.         elseif($this->getUser()->getRole()!='ROLE_ADMIN'){
  319.             $this->addFlash('danger'$this->translator->trans('Sólo un usuario Superadministrador puede gestionar licencias'));
  320.         }
  321.         else{
  322.             if($rmData){
  323.                 $rmAccount $this->util->rmAccount($lc->getCompany());
  324.                 if($rmAccount['error']){
  325.                     $this->addFlash('danger'$rmAccount['msg']);
  326.                 }
  327.                 else{
  328.                     $this->addFlash('success'$this->translator->trans('Datos y archivos eliminados satisfactoriamente'));
  329.                 }
  330.             }
  331.             $lc->setState(5);
  332.             $lc->setDateUpdated(new \DateTime());
  333.             $em->flush();
  334.             $this->addFlash('success'$this->translator->trans('Licencia dada de baja satisfactoriamente'));
  335.         }
  336.         return $this->redirectToRoute('license_index');
  337.     }
  338.     /**
  339.      * @Route({"en": "/code",
  340.      *         "es": "/codigo"}, name="license_code")
  341.      */
  342.     public function licenseCode(Request $request) {
  343.         $form $this->createFormBuilder()
  344.             ->add('code'\Symfony\Component\Form\Extension\Core\Type\TextType::class,['label' => 'Codigo'])
  345.             ->add('save'SubmitType::class, ['label' => 'Canjear'])
  346.             ->getForm();
  347.         $form->handleRequest($request);
  348.         if ($form->isSubmitted() && $form->isValid()) {
  349.             $em $this->em;
  350.             $decryptedData openssl_decrypt(  $start $form->get('code')->getData(), $this->method$this->key$this->options$this->iv);
  351.             $data explode(';',$decryptedData);
  352.             if( count($data) != 2) {
  353.                 $this->addFlash('danger'$this->translator->trans('codigo no valido'));
  354.                 return $this->redirectToRoute('license_code');
  355.             }
  356.             $company $em->getRepository(Company::class)->findOneBy(array('id' => $data[0]));
  357.             if(!$company) {
  358.                 $this->addFlash('danger'$this->translator->trans('codigo no valido'));
  359.                 return $this->redirectToRoute('license_code');
  360.             }
  361.             $lc $company->getLicenseCompany();
  362.             $dateEnd $lc->getDateEnd();
  363.             $renewal $lc->getRenewal();
  364.             $renewalType $lc->getRenewalType();
  365.             $license $lc->getLicense();
  366.             $company $lc->getCompany();
  367.             $lc->setState(LicenseCompany::RENEWED);
  368.             $dt = new \DateTime();
  369.             $dt->setTimestamp($data[1]);
  370.             $now =  new \DateTime();
  371.             if($now $dt)  {
  372.                 $this->addFlash('success'$this->translator->trans('codigo caducado el '.$dt->format('d-m-Y')));
  373.                 return $this->render('license/code.html.twig', [
  374.                     'form' => $form->createView(),
  375.                     'navbarTitle' => $this->translator->trans("Canjear código")
  376.                 ]);
  377.             }
  378.             $lc = new LicenseCompany();
  379.             $lc->setLicense($license);
  380.             $lc->setCompany($company);
  381.             $lc->setDateAdded(new \DateTime());
  382.             $lc->setDateStart($dateEnd);
  383.             $lc->setDateEnd($dt);
  384.             $lc->setRenewal($renewal);
  385.             $lc->setRenewalType($renewalType);
  386.             $lc->setState(2);
  387.             $em->persist($lc);
  388.             //Si estamos renovando una licencia vencida la convertimos a renovada
  389.             $lcOld $company->getLicenseCompany();
  390.             if($lcOld && $lcOld->getState()==LicenseCompany::EXPIRED){
  391.                 $lcOld->setState(LicenseCompany::RENEWED);
  392.             }
  393.             //Asignamos nueva licencia a la empresa
  394.             $company->setLicenseCompany($lc);
  395.             $em->persist($lc);
  396.             $em->flush();
  397.             $this->addFlash('success'$this->translator->trans('codigo canjeado con exito, Licencia renovada hasta el '.$dt->format('d-m-Y')));
  398.         }
  399.         return $this->render('license/code.html.twig', [
  400.             'form' => $form->createView(),
  401.             'navbarTitle' => $this->translator->trans("Canjear código")
  402.         ]);
  403.     }
  404.     /**
  405.      * @Route({"en": "/createLicenseCode/{company}/{endDate}",
  406.      *         "es": "/createLicenseCode/{company}/{endDate}"}, name="create_license_code", methods={"GET"})
  407.      */
  408.     public function createLicenseCode(Request $request,$company,$endDate) {
  409.         $data $company.';'.$endDate;
  410.         $encryptedData openssl_encrypt($data$this->method$this->key$this->options$this->iv);
  411.         return new JsonResponse(
  412.             array(
  413.                 'company' => $company,
  414.                 'endate' => $endDate,
  415.                 'encryptedData' => $encryptedData,
  416.             )
  417.         );
  418.     }
  419. }