src/Controller/ModComercio/Ventas/UsuariosController.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Controller\ModComercio\Ventas;
  3. use App\Entity\Comercio\{Cuenta};
  4. use App\Entity\Seguridades\Usuario;
  5. use App\Service\CacheService;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  10. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  11. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  12. class UsuariosController extends AbstractController {
  13.   private $em;
  14.   private $cache;
  15.   public function __construct(EntityManagerInterface $emCacheService $cache) {
  16.     date_default_timezone_set('America/Guayaquil');
  17.     $this->em $em;
  18.     $this->cache $cache;
  19.   }
  20.   // * READ FUNCTIONS *
  21.     /**
  22.      * @Route("/getInitialUsuarios", name="getInitialUsuarios")
  23.      */
  24.     public function getInitialUsuarios() {
  25.       $this->cache->delete('excelListUsuariosVentas');
  26.       $list = [];
  27.       $usuarios $this->em->getRepository(Usuario::class)->findAll();
  28.       foreach ($usuarios as $indice => $usuario) {
  29.         $list $this->setArrayUsuarios($list$usuario$indice);
  30.       }
  31.       $this->cache->add('excelListUsuariosVentas'$list);
  32.       return $this->json(['data' => $list], 200);
  33.     }
  34.     private function setArrayUsuarios($list$item$indice) {
  35.       $cuenta $this->em->getRepository(Cuenta::class)->findOneBy(['idUsuario' => $item->getId(), 'activa' => true]);
  36.       array_push($list, [
  37.         'indice' => $indice 1,
  38.         'id' => $item->getId(),
  39.         'nombres' => $item->getIdPersona()->getNombres().' '.$item->getIdPersona()->getApellidos(),
  40.         'numeroIdentificacion' => $item->getIdPersona()->getNumeroIdentificacion(),
  41.         'mailPersonal' => $item->getIdPersona()->getMailPersonal(),
  42.         'idUsuario' => $item->getId(),
  43.         'cuentaActiva' => $cuenta true false
  44.       ]);
  45.       return $list;
  46.     }
  47.   // * EXPORTS *
  48.     /**
  49.      * @Route("/getExcelUsuariosVentas", name="getExcelUsuariosVentas")
  50.      */
  51.     public function getExcelUsuariosVentas(){
  52.       $spreadsheet = new Spreadsheet();
  53.       $sheet $spreadsheet->getActiveSheet();
  54.       $sheet->setTitle("Productos");
  55.       $headers = ['id''nombres apellidos''nro. identificacion''correo'];
  56.       $sheet->fromArray$headersNULL'A1' );
  57.       $lista $this->cache->get('excelListUsuariosVentas');
  58.       $lista = !$lista ? [] : $lista;
  59.       if(count($lista) != 0):
  60.         $data array_map(function ($menu) {
  61.           return [
  62.             $menu['id'],
  63.             $menu['nombres'],
  64.             $menu['numeroIdentificacion'],
  65.             $menu['mailPersonal']
  66.           ];
  67.         }, $lista);
  68.       endif;
  69.       $sheet->fromArray($datanull'A2');
  70.       $writer = new Xlsx($spreadsheet);
  71.       $fileName 'usuarios.xlsx';
  72.       $temp_file tempnam(sys_get_temp_dir(), $fileName);
  73.       $writer->save($temp_file);
  74.       return $this->file($temp_file$fileNameResponseHeaderBag::DISPOSITION_INLINE);
  75.     }
  76. }