src/Controller/EsimController.php line 62

Open in your IDE?
  1. <?php
  2. // src/Controller/LuckyController.php
  3. namespace App\Controller;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Contracts\Translation\TranslatorInterface;
  9. use Symfony\Component\Validator\Validator\ValidatorInterface;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  12. use FOS\RestBundle\Request\ParamFetcherInterface;
  13. use FOS\RestBundle\Controller\Annotations\QueryParam;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  16. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  17. use PhpOffice\PhpSpreadsheet\IOFactory;
  18. use App\Annotations\IgnoreFilter;
  19. use App\Entity\Esim;
  20. use App\Entity\TugeEsim;
  21. use App\Entity\MovistarEsim;
  22. use App\Entity\AiraloEsim;
  23. use App\Entity\SimPackage;
  24. use App\Entity\Region;
  25. use App\Entity\MovistarEsimCapacity;
  26. use App\Form\EsimType;
  27. use App\Form\MovistarEsimUploadType;
  28. use App\Form\MovistarEsimType;
  29. use App\Logic\TugeLogic;
  30. use App\Logic\AiraloLogic;
  31. class EsimController extends AbstractController
  32. {
  33.     protected $em;
  34.     
  35.     public function __construct(EntityManagerInterface $em){
  36.         $this->em $em;
  37.     }
  38.     /**
  39.     * @Route({
  40.      *     "en": "/my-esims",
  41.      *     "es": "/esims",
  42.      *     "de": "/meine-esims",
  43.      *     "fr": "/mes-esims",
  44.      *     "it": "/i-miei-esims",
  45.      *     "pt": "/meus-esims",
  46.      *     "cn": "/my-esims_cn",
  47.      *     "cn2": "/my-esims_cn2",
  48.      * }, name="esims_list")
  49.     * @QueryParam(name="history", requirements="true|false", default="false", description="History.")
  50.     * @IsGranted("ROLE_USER")
  51.      */    
  52.     public function index(ParamFetcherInterface $paramFetcher)
  53.     {
  54.         // parameters
  55.         $history $paramFetcher->get('history') === "true";
  56.         $limit = !$history;
  57.         $repo $this->em->getRepository(Esim::class);
  58.         $repoTugeEsim $this->em->getRepository(TugeEsim::class);
  59.         $repoMovistarEsim $this->em->getRepository(MovistarEsim::class);
  60.         $repoAiraloEsim $this->em->getRepository(AiraloEsim::class);
  61.         $esims =  $repo->findByUser($this->getUser(), $limit);
  62.         $tugeEsims =  $repoTugeEsim->findByUser($this->getUser(), $limit);
  63.         $movistarEsims =  $repoMovistarEsim->findByUser($this->getUser(), $limit);
  64.         $airaloEsims =  $repoAiraloEsim->findByUser($this->getUser(), $limit);
  65.         // $esims = $repo->includeDeleted(fn ($repo) => $repo->findByUser($this->getUser()));
  66.         $allEsims array_merge($esims$tugeEsims$movistarEsims$airaloEsims);
  67.         usort($allEsims, function($a$b) {
  68.             return $a->getPurchaseTransaction()->getTransactedAt() < $b->getPurchaseTransaction()->getTransactedAt();
  69.         });
  70. //     * @IsGranted("ROLE_USER")
  71.         return $this->render('jelly/esims/index.html.twig', [
  72.             'esims' => $allEsims,        
  73.             'history' => $history
  74.         ]);
  75.     }
  76.     /**
  77.     * @Route("/esim/{esimId}", name="esim_detail")
  78.     * @IsGranted("ROLE_USER")
  79.      */    
  80.     public function detail(int $esimId)
  81.     {
  82.         $repoEsim $this->em->getRepository(Esim::class);
  83.         $repo $this->em->getRepository(SimPackage::class);
  84.         $esim $repoEsim->find($esimId);
  85.         if(!$esim)
  86.             throw $this->createNotFoundException('Esim not found');
  87.         if( count($esim->getTransactions()) == || $esim->getTransactions()->first()->getUser()->getId() != $this->getUser()->getId() )
  88.             throw $this->createAccessDeniedException('Esim does not belong to user');
  89.         $simPackages $repo->findByEsim($esim);
  90.         return $this->render('jelly/esims/detail.html.twig', [
  91.             'simPackages' => $simPackages,        
  92.         ]);
  93.     }
  94.     /**
  95.     * @Route({
  96.      *     "en": "/esim/instructions/{esimId}",
  97.      *     "es": "/esim/instrucciones/{esimId}",
  98.      *     "de": "/esim/anweisungen/{esimId}",
  99.      *     "fr": "/esim/instructions_fr/{esimId}",
  100.      *     "it": "/esim/istruzioni/{esimId}",
  101.      *     "pt": "/esim/instrucoes/{esimId}",
  102.      *     "cn": "/esim/instructions_cn/{esimId}",
  103.      *     "cn2": "/esim/instructions_cn2/{esimId}",
  104.      * }, name="esim_instructions")
  105.      */    
  106.     public function instructions(Request $requestint $esimId)
  107.     {
  108.         $repoEsim $this->em->getRepository(Esim::class);
  109.         $repo $this->em->getRepository(SimPackage::class);
  110.         $esim $repoEsim->find($esimId);
  111.         if(!$esim) {
  112.                 
  113.             $repoEsim $this->em->getRepository(TugeEsim::class);
  114.             $esim $repoEsim->find($esimId);
  115.             if ($esim) return $this->redirectToRoute("esim_tuge_instructions", ['esimId' => $esim->getId(), '_locale' => $request->getLocale() ]);
  116.             $repoEsim $this->em->getRepository(MovistarEsim::class);
  117.             $esim $repoEsim->find($esimId);
  118.             if ($esim) return $this->redirectToRoute("esim_movistar_instructions", ['esimId' => $esim->getId(), '_locale' => $request->getLocale()]);
  119.             $repoEsim $this->em->getRepository(AiraloEsim::class);
  120.             $esim $repoEsim->find($esimId);
  121.             if ($esim) return $this->redirectToRoute("esim_airalo_instructions", ['esimId' => $esim->getId(), '_locale' => $request->getLocale()]);
  122.             throw $this->createNotFoundException('Esim not found');
  123.         }
  124.         if( count($esim->getTransactions()) == || $esim->getTransactions()->first()->getUser()->getId() != $this->getUser()->getId() )
  125.             throw $this->createAccessDeniedException('Esim does not belong to user');
  126.         return $this->render('jelly/esims/instructions.html.twig', [
  127.             'transaction' => $esim->getPurchaseTransaction(),        
  128.         ]);
  129.     }    
  130.     /**
  131.     * @Route({
  132.      *     "en": "/esim/instructions2/{esimId}",
  133.      *     "es": "/esim/instrucciones2/{esimId}",
  134.      *     "de": "/esim/anweisungen2/{esimId}",
  135.      *     "fr": "/esim/instructions2_fr/{esimId}",
  136.      *     "it": "/esim/istruzioni2/{esimId}",
  137.      *     "pt": "/esim/instrucoes2/{esimId}",
  138.      *     "cn": "/esim/instructions2_cn/{esimId}",
  139.      *     "cn2": "/esim/instructions2_cn2/{esimId}",
  140.      * }, name="esim_tuge_instructions")
  141.      */    
  142.     public function instructions2(int $esimId)
  143.     {
  144.         $repoEsim $this->em->getRepository(TugeEsim::class);
  145.         $repo $this->em->getRepository(SimPackage::class);
  146.         $esim $repoEsim->find($esimId);
  147.         if(!$esim)
  148.             throw $this->createNotFoundException('Esim not found');
  149.         if( !$esim->getPurchaseTransaction()  )
  150.             throw $this->createAccessDeniedException('Esim does not belong to user');
  151.         return $this->render('jelly/esims/instructions.html.twig', [
  152.             'transaction' => $esim->getPurchaseTransaction(),        
  153.         ]);
  154.     }    
  155.     /**
  156.     * @Route({
  157.      *     "en": "/esim/instructions3/{esimId}",
  158.      *     "es": "/esim/instrucciones3/{esimId}",
  159.      *     "de": "/esim/anweisungen3/{esimId}",
  160.      *     "fr": "/esim/instructions3_fr/{esimId}",
  161.      *     "it": "/esim/istruzioni3/{esimId}",
  162.      *     "pt": "/esim/instrucoes3/{esimId}",
  163.      *     "cn": "/esim/instructions3_cn/{esimId}",
  164.      *     "cn2": "/esim/instructions3_cn2/{esimId}",
  165.      * }, name="esim_movistar_instructions")
  166.      */    
  167.     public function instructions3(int $esimId)
  168.     {
  169.         $repoEsim $this->em->getRepository(MovistarEsim::class);
  170.         $esim $repoEsim->find($esimId);
  171.         if(!$esim)
  172.             throw $this->createNotFoundException('Esim not found');
  173.         if( !$esim->getPurchaseTransaction() )
  174.             throw $this->createAccessDeniedException('Esim does not belong to user');
  175.         return $this->render('jelly/esims/instructions.html.twig', [
  176.             'transaction' => $esim->getPurchaseTransaction(),        
  177.         ]);
  178.     }    
  179.     /**
  180.     * @Route({
  181.      *     "en": "/esim/instructions4/{esimId}",
  182.      *     "es": "/esim/instrucciones4/{esimId}",
  183.      *     "de": "/esim/anweisungen4/{esimId}",
  184.      *     "fr": "/esim/instructions4_fr/{esimId}",
  185.      *     "it": "/esim/istruzioni4/{esimId}",
  186.      *     "pt": "/esim/instrucoes4/{esimId}",
  187.      *     "cn": "/esim/instructions4_cn/{esimId}",
  188.      *     "cn2": "/esim/instructions4_cn2/{esimId}",
  189.      * }, name="esim_airalo_instructions")
  190.      */    
  191.     public function instructions4(int $esimId)
  192.     {
  193.         $repoEsim $this->em->getRepository(AiraloEsim::class);
  194.         $esim $repoEsim->find($esimId);
  195.         if(!$esim)
  196.             throw $this->createNotFoundException('Esim not found');
  197.         if( !$esim->getPurchaseTransaction() )
  198.             throw $this->createAccessDeniedException('Esim does not belong to user');
  199.         return $this->render('jelly/esims/instructions.html.twig', [
  200.             'transaction' => $esim->getPurchaseTransaction(),        
  201.         ]);
  202.     }    
  203.     /**
  204.     * @Route("/admin/esims", name="esims_admin_list")
  205.      * @QueryParam(name="page", requirements="\d+", default=1, description="Page number.")
  206.      * @QueryParam(name="sort", requirements="e.iccid|u.lastName|r.descriptionEs|e.createdAt", description="Sort.")
  207.      * @QueryParam(name="dir", requirements="asc|desc", default="asc", description="Sort direction.")         
  208.      * @QueryParam(name="iccid", requirements=".*", default="", nullable=true, description="Iccid.")         
  209.     * @IsGranted("ROLE_ADMIN")
  210.      */    
  211.     public function adminIndex(Request $requestParamFetcherInterface $paramFetcher)
  212.     {
  213.         // parameters
  214.         $page $paramFetcher->get('page');
  215.         $sort $paramFetcher->get('sort');
  216.         $dir $paramFetcher->get('dir');
  217.         $iccid $paramFetcher->get('iccid');
  218.         $download $request->query->get('download');    
  219.         // query
  220.         $results_per_page $this->getParameter('app.results_per_page');        
  221.         // filters
  222.         $currentFilters = [];
  223.         if($sort){
  224.             $currentFilters['sort'] = $sort;
  225.             $currentFilters['dir'] = $dir;          
  226.         } 
  227.         if($iccid){
  228.             $currentFilters['iccid'] = $iccid;
  229.         }        
  230.         $repo $this->em->getRepository(Esim::class);
  231.         $form $this->createForm(EsimType::class);
  232.         $form->handleRequest($request);
  233.         if ($form->isSubmitted() && $form->isValid()) {
  234.             $region $form->get('region')->getData();
  235.             $file $form->get('file')->get('file')->getData();
  236.             if ($file) {
  237.                 $this->uploadExcel($region$file->getRealPath());
  238.             }
  239.         }
  240.         $esims $repo->getList($sort$dir$page$results_per_page$iccid$download);
  241.         if($download == '1')
  242.             return $this->downloadExcel($esims);
  243.         $totalPages ceil(count($esims) / $results_per_page);
  244.         
  245. //     * @IsGranted("ROLE_USER")
  246.         return $this->render('angle/esims/index.html.twig', [
  247.             'esims' => $esims,        
  248.             'page' => $page,
  249.             'totalPages' => $totalPages,
  250.             'totalResults' =>  count($esims),            
  251.             'currentFilters' => $currentFilters,
  252.             'results_per_page' => $results_per_page,
  253.             'iccid' => $iccid,
  254.             'form' => $form->createView(),
  255.             'regions' => $this->em->getRepository(Region::class)->findBy([], ['descriptionEs' => 'ASC']), 
  256.         ]);
  257.     }    
  258.     /**
  259.     * @Route("/admin/tuge-esims", name="esims_tuge_admin_list")
  260.      * @QueryParam(name="page", requirements="\d+", default=1, description="Page number.")
  261.      * @QueryParam(name="sort", requirements="e.iccid|u.lastName|r.descriptionEs|e.createdAt|e.bytesConsumed", description="Sort.")
  262.      * @QueryParam(name="dir", requirements="asc|desc", default="asc", description="Sort direction.")         
  263.      * @QueryParam(name="iccid", requirements=".*", default="", nullable=true, description="Iccid.")         
  264.     * @IsGranted("ROLE_AGENT")
  265.     * @IgnoreFilter(filter="softdeleteable", forClasses={"App\Entity\User"})* 
  266.      */    
  267.     public function adminTugeIndex(Request $requestParamFetcherInterface $paramFetcher)
  268.     {
  269.         // parameters
  270.         $page $paramFetcher->get('page');
  271.         $sort $paramFetcher->get('sort');
  272.         $dir $paramFetcher->get('dir');
  273.         $iccid $paramFetcher->get('iccid');
  274.         $download $request->query->get('download');    
  275.         // query
  276.         $results_per_page $this->getParameter('app.results_per_page');        
  277.         // filters
  278.         $currentFilters = [];
  279.         if($sort){
  280.             $currentFilters['sort'] = $sort;
  281.             $currentFilters['dir'] = $dir;          
  282.         } 
  283.         if($iccid){
  284.             $currentFilters['iccid'] = $iccid;
  285.         }        
  286.         $repo $this->em->getRepository(TugeEsim::class);
  287.         $esims $repo->getList($sort$dir$page$results_per_page$iccid$download);
  288.         if($download == '1')
  289.             return $this->downloadExcel($esims);
  290.         $totalPages ceil(count($esims) / $results_per_page);
  291.         
  292. //     * @IsGranted("ROLE_USER")
  293.         return $this->render('angle/esims/index_tuge.html.twig', [
  294.             'esims' => $esims,        
  295.             'page' => $page,
  296.             'totalPages' => $totalPages,
  297.             'totalResults' =>  count($esims),            
  298.             'currentFilters' => $currentFilters,
  299.             'results_per_page' => $results_per_page,
  300.             'iccid' => $iccid,
  301.         ]);
  302.     }        
  303.     /**
  304.     * @Route("/admin/movistar-esims", name="esims_movistar_admin_list")
  305.      * @QueryParam(name="page", requirements="\d+", default=1, description="Page number.")
  306.      * @QueryParam(name="sort", requirements="mec.amountGb|u.lastName|e.createdAt|t.createdAt", description="Sort.")
  307.      * @QueryParam(name="dir", requirements="asc|desc", default="asc", description="Sort direction.")         
  308.     * @IsGranted("ROLE_ADMIN")
  309.     * @IgnoreFilter(filter="softdeleteable", forClasses={"App\Entity\User"})* 
  310.      */    
  311.     public function adminMovistarIndex(Request $requestParamFetcherInterface $paramFetcher)
  312.     {
  313.         // parameters
  314.         $page $paramFetcher->get('page');
  315.         $sort $paramFetcher->get('sort');
  316.         $dir $paramFetcher->get('dir');
  317.         $download $request->query->get('download');    
  318.         // query
  319.         $results_per_page $this->getParameter('app.results_per_page');        
  320.         // filters
  321.         $currentFilters = [];
  322.         if($sort){
  323.             $currentFilters['sort'] = $sort;
  324.             $currentFilters['dir'] = $dir;          
  325.         } 
  326.         $download $request->query->get('download');    
  327.         $repo $this->em->getRepository(MovistarEsim::class);
  328.         $form $this->createForm(MovistarEsimUploadType::class);
  329.         $form->handleRequest($request);
  330.         if ($form->isSubmitted() && $form->isValid()) {
  331.             $region $form->get('region')->getData();
  332.             $file $form->get('file')->get('file')->getData();
  333.             if ($file) {
  334.                 $this->uploadExcel2($region$file->getRealPath());
  335.             }
  336.         }
  337.         $esims $repo->getList($sort$dir$page$results_per_page$download);
  338.         if($download == '1')
  339.             return $this->downloadExcel2($esims);
  340.         $totalPages ceil(count($esims) / $results_per_page);        
  341. //     * @IsGranted("ROLE_USER")
  342.         return $this->render('angle/esims/index_movistar.html.twig', [
  343.             'esims' => $esims,        
  344.             'page' => $page,
  345.             'totalPages' => $totalPages,
  346.             'totalResults' =>  count($esims),            
  347.             'currentFilters' => $currentFilters,
  348.             'results_per_page' => $results_per_page,
  349.             'form' => $form->createView(),
  350.         ]);
  351.     }        
  352.     /**
  353.     * @Route("/admin/movistar-esim/{esimId}", name="esims_movistar_admin_detail")
  354.     * @IsGranted("ROLE_ADMIN")
  355.     * @IgnoreFilter(filter="softdeleteable", forClasses={"App\Entity\User"})* 
  356.      */    
  357.     public function adminMovistarDetail(Request $requeststring $esimId)
  358.     {
  359.         $repo $this->em->getRepository(MovistarEsim::class);
  360.         $esim $repo->find($esimId);
  361.         if(!$esim)
  362.             throw $this->createNotFoundException('Esim not found');
  363.         $form $this->createForm(MovistarEsimType::class, $esim);
  364.         $form->handleRequest($request);
  365.         if ($form->isSubmitted() && $form->isValid()) {
  366.             $esim $form->getData();
  367.             $this->em->persist($esim);
  368.             $this->em->flush();
  369.             return $this->redirectToRoute('esims_movistar_admin_list');
  370.         }
  371.         return $this->render('angle/esims/detail_movistar.html.twig', [
  372.             'esim' => $esim,        
  373.             'form' => $form->createView(),
  374.         ]);
  375.     }        
  376.     /**
  377.     * @Route("/admin/airalo-esims", name="esims_airalo_admin_list")
  378.      * @QueryParam(name="page", requirements="\d+", default=1, description="Page number.")
  379.      * @QueryParam(name="sort", requirements="e.iccid|u.lastName|r.descriptionEs|e.createdAt|e.bytesConsumed", description="Sort.")
  380.      * @QueryParam(name="dir", requirements="asc|desc", default="asc", description="Sort direction.")         
  381.      * @QueryParam(name="iccid", requirements=".*", default="", nullable=true, description="Iccid.")         
  382.     * @IsGranted("ROLE_AGENT")
  383.     * @IgnoreFilter(filter="softdeleteable", forClasses={"App\Entity\User"})* 
  384.      */    
  385.     public function adminAiraloIndex(Request $requestParamFetcherInterface $paramFetcher)
  386.     {
  387.         // parameters
  388.         $page $paramFetcher->get('page');
  389.         $sort $paramFetcher->get('sort');
  390.         $dir $paramFetcher->get('dir');
  391.         $iccid $paramFetcher->get('iccid');
  392.         $download $request->query->get('download');    
  393.         // query
  394.         $results_per_page $this->getParameter('app.results_per_page');        
  395.         // filters
  396.         $currentFilters = [];
  397.         if($sort){
  398.             $currentFilters['sort'] = $sort;
  399.             $currentFilters['dir'] = $dir;          
  400.         } 
  401.         if($iccid){
  402.             $currentFilters['iccid'] = $iccid;
  403.         }        
  404.         $repo $this->em->getRepository(AiraloEsim::class);
  405.         $esims $repo->getList($sort$dir$page$results_per_page$iccid$download);
  406.         if($download == '1')
  407.             return $this->downloadExcel($esims);
  408.         $totalPages ceil(count($esims) / $results_per_page);
  409.         
  410. //     * @IsGranted("ROLE_USER")
  411.         return $this->render('angle/esims/index_airalo.html.twig', [
  412.             'esims' => $esims,        
  413.             'page' => $page,
  414.             'totalPages' => $totalPages,
  415.             'totalResults' =>  count($esims),            
  416.             'currentFilters' => $currentFilters,
  417.             'results_per_page' => $results_per_page,
  418.             'iccid' => $iccid,
  419.         ]);
  420.     }          
  421.      /**
  422.      * @IsGranted("ROLE_ADMIN")     
  423.      * @Route(
  424.      *    "/services/esim/assign_region.{_format}", 
  425.      *     name="esim_region_assign",
  426.      *     requirements={
  427.      *         "_format": "json"
  428.      *     },
  429.      *     methods={"GET"}
  430.      * )
  431.      */
  432.     public function assignRegion(Request $request)
  433.     {        
  434.      
  435.         $repo $this->getDoctrine()->getRepository(Region::class);
  436.         $repoEsims $this->getDoctrine()->getRepository(Esim::class);
  437.         $ids $request->query->get('id');
  438.         $regionId urldecode($request->query->get('region_id'));
  439.     
  440.         $region $repo->find($regionId);
  441.         $idsArray =  explode(","$ids);
  442.         
  443.         foreach($idsArray as $id) if($id){
  444.             if ($esim $repoEsims->findOneById($id) ){
  445.                 $esim->setRegion($region);    
  446.                 $this->em->flush();
  447.                 
  448.             }
  449.         }
  450.         return ['ok' => true];
  451.     }        
  452.    
  453.     private function uploadExcel($region$filename){
  454.         $em $this->getDoctrine()->getManager();
  455.         $repo $em->getRepository(Esim::class);
  456.         $spreadsheet IOFactory::load($filename);
  457.         $sheet $spreadsheet->getActiveSheet();
  458.         $rowCount 2;
  459.         while (!empty($sheet->getCell("A$rowCount")->getValue())) {
  460.             $iccid trim((string) $sheet->getCell("A$rowCount")->getValue());
  461.             $qrCode trim((string) $sheet->getCell("D$rowCount")->getValue());
  462.             $existingDevice $repo->findOneBy(['iccid' => $iccid]);
  463.             if($existingDevice){
  464.                 $rowCount++;
  465.                 continue;
  466.             }
  467.             $esim = new Esim();
  468.             $esim->setRegion($region);
  469.             $esim->setIccid($iccid);
  470.             $esim->setQrCode($qrCode);
  471.             $em->persist($esim);
  472.             $rowCount++;
  473.         }        
  474.         $em->flush();
  475.     }    
  476.     private function uploadExcel2($region$filename){
  477.         $em $this->getDoctrine()->getManager();
  478.         $repo $em->getRepository(MovistarEsim::class);
  479.         $repoCapacity $em->getRepository(MovistarEsimCapacity::class);
  480.         $spreadsheet IOFactory::load($filename);
  481.         $sheet $spreadsheet->getActiveSheet();
  482.         $rowCount 2;
  483.         while (!empty($sheet->getCell("A$rowCount")->getValue())) {
  484.             $phoneNumber trim((string) $sheet->getCell("A$rowCount")->getValue());
  485.             $amountGb trim((string) $sheet->getCell("B$rowCount")->getValue());
  486.             $qrCode trim((string) $sheet->getCell("C$rowCount")->getValue());
  487.             $existingDevice $repo->findOneBy(['phoneNumber' => $phoneNumber]);
  488.             if($existingDevice){
  489.                 $rowCount++;
  490.                 continue;
  491.             }
  492.             $capacity $repoCapacity->findOneBy(['amountGb' => $amountGb]);
  493.             if(!$capacity){
  494.                 $rowCount++;
  495.                 continue;
  496.             }
  497.             $esim = new MovistarEsim();
  498.             $esim->setRegion($region);
  499.             $esim->setPhoneNumber($phoneNumber);
  500.             $esim->setMovistarEsimCapacity($capacity);
  501.             $esim->setQrCode($qrCode);
  502.             $em->persist($esim);
  503.             $rowCount++;
  504.         }        
  505.         $em->flush();
  506.     }    
  507.     private $col = -1;
  508.     private function getCell($row$forward true) {
  509.          $cols = [ 'A' 'B''C''D''E''F''G''H''I''J''K''L''M''N''O''P''Q''R''S''T''U''V''W''X''Y''Z''AA''AB''AC''AD''AE''AF''AG''AH''AI',' AJ''AK''AL''AM''AN''AO''AP''AQ''AR''AS''AT''AU''AV''AW''AX''AY''AZ'];
  510.         if( $forward )
  511.             $this->col++;
  512.         return (String) ($cols[$this->col] . $row); 
  513.     }
  514.     private function getColumnindex($forward true) {
  515.         if( $forward )
  516.             $this->col++;
  517.         return $this->col;
  518.     }
  519.     private function resetCol() {
  520.         $this->col = -1;
  521.     }
  522.     private function downloadExcel($stats){
  523.         ini_set('memory_limit''512M');
  524.         set_time_limit(0);
  525.        
  526.        $spreadsheet = new Spreadsheet();
  527.         $spreadsheet->getProperties()
  528.             ->setCreator("Blink+")
  529.             ->setLastModifiedBy("Blink+")
  530.             ->setTitle("beconnected esims")
  531.             ->setSubject("beconnected esims")
  532.             ->setDescription(
  533.                 "beconnected esims"
  534.             );
  535.        
  536.        $sheet $spreadsheet->getActiveSheet();
  537.        $sheet->setCellValue($this->getCell(1true), 'Esim');
  538.        $sheet->setCellValue($this->getCell(1true), 'Nombre');
  539.        $sheet->setCellValue($this->getCell(1true), 'Fecha creación');
  540.         
  541.        $row 2;
  542.        foreach($stats as $stat){
  543.            $this->resetCol();
  544.            $sheet->setCellValue($this->getCell($rowtrue), $stat->getIccid());
  545.            $sheet->setCellValue($this->getCell($rowtrue), $stat->getPurchaseTransaction() ? (  $stat->getPurchaseTransaction()->getUser()->getFirstName() . " " $stat->getPurchaseTransaction()->getUser()->getLastName() ) : "" );
  546.            $sheet->setCellValue($this->getCell($rowtrue), $stat->getCreatedAt());
  547.            $row++;
  548.        }
  549.        $writer = new Xlsx($spreadsheet);
  550.        
  551.         header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  552.         header('Content-Disposition: attachment; filename="'urlencode("beconnected.xlsx").'"');
  553.         $writer->save('php://output');       
  554.         die();
  555.         return;
  556.     }
  557.     private function downloadExcel2($stats){
  558.         ini_set('memory_limit''512M');
  559.         set_time_limit(0);
  560.        
  561.        $spreadsheet = new Spreadsheet();
  562.         $spreadsheet->getProperties()
  563.             ->setCreator("Blink+")
  564.             ->setLastModifiedBy("Blink+")
  565.             ->setTitle("beconnected movistar esims")
  566.             ->setSubject("beconnected movistar esims")
  567.             ->setDescription(
  568.                 "beconnected movistar esims"
  569.             );
  570.        
  571.        $sheet $spreadsheet->getActiveSheet();
  572.        $sheet->setCellValue($this->getCell(1true), 'Número');
  573.        $sheet->setCellValue($this->getCell(1true), 'Capacidad');
  574.        $sheet->setCellValue($this->getCell(1true), 'Nombre');
  575.        $sheet->setCellValue($this->getCell(1true), 'Fecha creación');
  576.        $sheet->setCellValue($this->getCell(1true), 'Fecha compra');
  577.        $sheet->setCellValue($this->getCell(1true), 'Fecha deshabilitación');
  578.         
  579.        $row 2;
  580.        foreach($stats as $stat){
  581.            $this->resetCol();
  582.            $sheet->setCellValue($this->getCell($rowtrue), $stat->getPhoneNumber());
  583.            $sheet->setCellValue($this->getCell($rowtrue), $stat->getMovistarEsimCapacity()->getAmountGb());
  584.            $sheet->setCellValue($this->getCell($rowtrue), $stat->getPurchaseTransaction() ? (  $stat->getPurchaseTransaction()->getUser()->getFirstName() . " " $stat->getPurchaseTransaction()->getUser()->getLastName() ) : "" );
  585.            $sheet->setCellValue($this->getCell($rowtrue), $stat->getCreatedAt());
  586.            $sheet->setCellValue($this->getCell($rowtrue), $stat->getPurchaseTransaction() ? $stat->getPurchaseTransaction()->getCreatedAt()  : "" );
  587.            $sheet->setCellValue($this->getCell($rowtrue), $stat->getDeactivatedAt());
  588.            $row++;
  589.        }
  590.        $writer = new Xlsx($spreadsheet);
  591.        
  592.         header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  593.         header('Content-Disposition: attachment; filename="'urlencode("beconnected.xlsx").'"');
  594.         $writer->save('php://output');       
  595.         die();
  596.         return;
  597.     }
  598.      /**
  599.      * @IsGranted("ROLE_AGENT")     
  600.      * @Route(
  601.      *    "/services/tuge_esim.{_format}", 
  602.      *     name="service_tuge_esim",
  603.      *     requirements={
  604.      *         "_format": "json"
  605.      *     },
  606.      *     methods={"GET"}
  607.      * )
  608.      */
  609.     public function tugeEsim(Request $requestTugeLogic $tugeLogic)
  610.     {        
  611.      
  612.         $repository $this->getDoctrine()->getRepository(TugeEsim::class);
  613.         $id $request->query->get('id');
  614.         if($id){
  615.             $entity $repository->findOneById($id);
  616.                 
  617.             if($entity)
  618.                 $data $tugeLogic->queryOrder($entity->getIccid());
  619.             else
  620.                 return ['ok' => false];
  621.             return ['ok' => true'order_status' => $data[0]->orderStatus'start_date' => $data[0]->startDate 'end_date' => $data[0]->endDate'iccid' => $entity->getIccid() ]            ;
  622.         }
  623.         return ['ok' => false];
  624.     }    
  625.      /**
  626.      * @IsGranted("ROLE_ADMIN")     
  627.      * @Route(
  628.      *    "/services/tuge_esim_refund.{_format}", 
  629.      *     name="service_tuge_esim_refund",
  630.      *     requirements={
  631.      *         "_format": "json"
  632.      *     },
  633.      *     methods={"POST"}
  634.      * )
  635.      */
  636.     public function tugeEsimRefund(Request $request)
  637.     {        
  638.      
  639.         $repository $this->getDoctrine()->getRepository(TugeEsim::class);
  640.         $id $request->query->get('id');
  641.         if($id){
  642.             $entity $repository->findOneById($id);
  643.                 
  644.             if($entity){
  645.                 $entity->setIsRefunded(true);
  646.                 $this->em->flush();
  647.             }
  648.             return true;
  649.         }
  650.         return ['ok' => false];
  651.     }    
  652.      /**
  653.      * @IsGranted("ROLE_ADMIN")     
  654.      * @Route(
  655.      *    "/services/movistar_esim_deactivate.{_format}", 
  656.      *     name="service_movistar_esim_deactivate",
  657.      *     requirements={
  658.      *         "_format": "json"
  659.      *     },
  660.      *     methods={"POST"}
  661.      * )
  662.      */
  663.     public function movistarEsimDeactivate(Request $request)
  664.     {        
  665.      
  666.         $repository $this->getDoctrine()->getRepository(MovistarEsim::class);
  667.         $id $request->query->get('id');
  668.         if($id){
  669.             $entity $repository->findOneById($id);
  670.                 
  671.             if($entity){
  672.                 $entity->setDeactivatedAt(new \DateTimeImmutable);
  673.                 $this->em->flush();
  674.             }
  675.             return true;
  676.         }
  677.         return ['ok' => false];
  678.     }        
  679.      /**
  680.      * @IsGranted("ROLE_AGENT")     
  681.      * @Route(
  682.      *    "/services/airalo_esim.{_format}", 
  683.      *     name="service_airalo_esim",
  684.      *     requirements={
  685.      *         "_format": "json"
  686.      *     },
  687.      *     methods={"GET"}
  688.      * )
  689.      */
  690.     public function airaloEsim(Request $requestAiraloLogic $airaloLogic)
  691.     {        
  692.      
  693.         $repository $this->getDoctrine()->getRepository(AiraloEsim::class);
  694.         $id $request->query->get('id');
  695.         if($id){
  696.             $entity $repository->findOneById($id);
  697.                 
  698.             if($entity)
  699.                 $data $airaloLogic->fetchDataUsage($entity);
  700.             else
  701.                 return ['ok' => false];
  702.             return ['ok' => true'data' => $data['data'] ]            ;
  703.         }
  704.         return ['ok' => false];
  705.     }  
  706. }