<?php
namespace App\Controller\ModComercio\Ventas;
use App\Entity\Comercio\{Cuenta};
use App\Entity\Seguridades\Usuario;
use App\Service\CacheService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
class UsuariosController extends AbstractController {
private $em;
private $cache;
public function __construct(EntityManagerInterface $em, CacheService $cache) {
date_default_timezone_set('America/Guayaquil');
$this->em = $em;
$this->cache = $cache;
}
// * READ FUNCTIONS *
/**
* @Route("/getInitialUsuarios", name="getInitialUsuarios")
*/
public function getInitialUsuarios() {
$this->cache->delete('excelListUsuariosVentas');
$list = [];
$usuarios = $this->em->getRepository(Usuario::class)->findAll();
foreach ($usuarios as $indice => $usuario) {
$list = $this->setArrayUsuarios($list, $usuario, $indice);
}
$this->cache->add('excelListUsuariosVentas', $list);
return $this->json(['data' => $list], 200);
}
private function setArrayUsuarios($list, $item, $indice) {
$cuenta = $this->em->getRepository(Cuenta::class)->findOneBy(['idUsuario' => $item->getId(), 'activa' => true]);
array_push($list, [
'indice' => $indice + 1,
'id' => $item->getId(),
'nombres' => $item->getIdPersona()->getNombres().' '.$item->getIdPersona()->getApellidos(),
'numeroIdentificacion' => $item->getIdPersona()->getNumeroIdentificacion(),
'mailPersonal' => $item->getIdPersona()->getMailPersonal(),
'idUsuario' => $item->getId(),
'cuentaActiva' => $cuenta ? true : false
]);
return $list;
}
// * EXPORTS *
/**
* @Route("/getExcelUsuariosVentas", name="getExcelUsuariosVentas")
*/
public function getExcelUsuariosVentas(){
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle("Productos");
$headers = ['id', 'nombres apellidos', 'nro. identificacion', 'correo'];
$sheet->fromArray( $headers, NULL, 'A1' );
$lista = $this->cache->get('excelListUsuariosVentas');
$lista = !$lista ? [] : $lista;
if(count($lista) != 0):
$data = array_map(function ($menu) {
return [
$menu['id'],
$menu['nombres'],
$menu['numeroIdentificacion'],
$menu['mailPersonal']
];
}, $lista);
endif;
$sheet->fromArray($data, null, 'A2');
$writer = new Xlsx($spreadsheet);
$fileName = 'usuarios.xlsx';
$temp_file = tempnam(sys_get_temp_dir(), $fileName);
$writer->save($temp_file);
return $this->file($temp_file, $fileName, ResponseHeaderBag::DISPOSITION_INLINE);
}
}