src/Noahtech/Sistemas/InterjamaBundle/Handler/McClienteHandler.php line 187

Open in your IDE?
  1. <?php
  2. namespace Noahtech\Sistemas\InterjamaBundle\Handler;
  3. use Noahtech\Sistemas\InterjamaBundle\Entity\McCliente;
  4. use Noahtech\Sistemas\InterjamaBundle\Utils\Constants;
  5. use Noahtech\Sistemas\InterjamaBundle\Utils\EmailsMessages;
  6. use Noahtech\Sistemas\InterjamaBundle\Utils\Encrypt;
  7. use DateInterval;
  8. use DateTime;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\DependencyInjection\ContainerInterface;
  11. use Symfony\Component\HttpKernel\Exception\HttpException;
  12. use Symfony\Component\HttpFoundation\Request;
  13. class McClienteHandler extends BaseHandler {
  14.     public function __construct(ContainerInterface $containerEntityManagerInterface $entityManager) {
  15.         $this->container $container;
  16.         $this->entityManager $entityManager;
  17.         $this->repository $entityManager->getRepository(McCliente::class);
  18.     }
  19.     
  20.     public function search($offset$limit$sortField$sortDirection$searchParam) {
  21.         $lp $this->repository->search($offset$limit$sortField$sortDirection$searchParam);
  22.         $clientes $this->toarray($lp->getListado(), 'cliente');
  23.         $lp->setListado($clientes);
  24.         return $lp;
  25.     }
  26.     public function getClienteByRazonSocial($razonSocial) {
  27.         return $this->repository->findOneByRazonSocial($razonSocial);
  28.     }
  29.     public function getClienteByCuit($cuit) {
  30.         $cliente $this->repository->findOneByCuit($cuit);
  31.         $aux null;
  32.         if (!is_null($cliente)) {
  33.             $aux $this->toarray($cliente"cliente");            
  34.         }
  35.         return $aux;
  36.     }
  37.     public function saveImageFile(&$archivo){
  38.         $data['result'] = false;
  39.         $path $this->getPathFiles();
  40.         //Crea la carpeta si no existe
  41.         if (!file_exists($path)) {
  42.             mkdir($path0777true);
  43.         }
  44.         $info pathinfo($archivo['name']);
  45.         $ext $info['extension']; // get the extension of the file
  46.         $name $info['filename'];
  47.         
  48.         $nameMd5 md5($name.time());
  49.         $nameFileMd5 "$nameMd5.$ext";
  50.         $nameFileMd5File "$nameMd5.$ext";
  51.         $pathFile $path.$nameFileMd5;
  52.         
  53.         $archivo['result'] = true;
  54.         move_uploaded_file($archivo['tmp_name'], $pathFile);
  55.         $archivo['name_md5']=$nameMd5;
  56.         $archivo['name_file_Md5'] = $nameFileMd5;
  57.         $archivo['path_file']=$pathFile;
  58.         $archivo['path_only']=$path;
  59.         return $pathFile;
  60.     }
  61.     private function getPathFiles(){
  62.         $basePath $this->container->getParameter('kernel.root_dir');
  63.         $basePath str_replace('/app''/web'$basePath);        
  64.         
  65.         $basePath $basePath $this->container->getParameter('path_files_archivos_clientes');
  66.         $path "$basePath/";
  67.         return $path;
  68.     }
  69.     private function deleteFile($pathFile) {
  70.         try {
  71.             unlink($pathFile);
  72.         } catch (Exception $e) {
  73.         }
  74.         return $pathFile;
  75.     }
  76.     public function comprobarArchivo($archivo) {
  77.         //Sección que almadena el archivo relacionado                
  78.         if (!is_null($archivo) && $archivo['name'] !== '') {
  79.             $pathFile $this->saveImageFile($archivo);           
  80.             if ($archivo['result']) {            
  81.                 $archivo $archivo['name_file_Md5'];            
  82.             } else {
  83.                 $this->deleteFile($pathFile);            
  84.             }              
  85.         } else {
  86.             $archivo null;
  87.         }
  88.         return $archivo;
  89.     }
  90.     public function getClienteFromRequest(Request $request,  $archivo_poder$archivo_presentacionint $id null):McCliente {                
  91.         $cuit $request->request->get('cuit');
  92.         $razon_social $request->request->get('razon_social');
  93.         $celular $request->request->get('celular');
  94.         $mail $request->request->get('mail');
  95.         $mailAlternativo $request->request->get('mail_alternativo');
  96.         $fechaAlta date('Y-m-d'strtotime($request->request->get('fecha_alta')));
  97.         $condicion_iva $request->request->get('condicion_iva');
  98.         $celular2 $request->request->get('celular2');
  99.         $direccion $request->request->get('direccion');
  100.         $representanteLegal $request->request->get('representanteLegal');
  101.         if (is_null($id)) {
  102.             $cliente = new McCliente();
  103.             $cliente->setFechaCreacion(new DateTime());
  104.             //Calculo del número
  105.             $clientes $this->repository->findAll();
  106.             $clientes $this->toarray($clientes"cliente");
  107.             if (count($clientes) == 0) {
  108.                 $numero 1;
  109.             } else {
  110.                 $numero count($clientes) + 1;
  111.             }
  112.             $cliente->setCuit($cuit);
  113.             $cliente->setNumero($numero);
  114.             $archivoPoderAux $this->comprobarArchivo($archivo_poder);
  115.             $archivoPresentacionAux $this->comprobarArchivo($archivo_presentacion);
  116.             $cliente->setArchivoPoder($archivoPoderAux);
  117.             $cliente->setArchivoPresentacion($archivoPresentacionAux);           
  118.         } else {
  119.             // Se valida que no exista el CUIT
  120.             $cuitValido $this->getClienteByCuit($cuit);
  121.             if (!is_null($cuitValido) && (int)$cuitValido['id'] != (int)$id) {
  122.                 throw new HttpException(409"El CUIT ingresado " $cuit" ya se encuentra registrado.");
  123.             }
  124.             $cliente $this->repository->findOneById($id);
  125.             $cliente->setFechaActualizacion(new DateTime());
  126.             $cliente->setCuit($cuit);
  127.             $archivoPoderAux $this->comprobarArchivo($archivo_poder);
  128.             if (!is_null($archivoPoderAux)) {
  129.                 $cliente->setArchivoPoder($archivoPoderAux);
  130.             }
  131.             $archivoPresentacionAux $this->comprobarArchivo($archivo_presentacion);
  132.             if (!is_null($archivoPresentacionAux)) {
  133.                 $cliente->setArchivoPresentacion($archivoPresentacionAux);
  134.             }          
  135.         }
  136.         $cliente->setRazonSocial($razon_social);
  137.         $cliente->setCelular($celular);
  138.         $cliente->setMail($mail);
  139.         $cliente->setMailAlternativo($mailAlternativo);
  140.         $cliente->setCondicionIva($condicion_iva);
  141.         $cliente->setCelular2($celular2);
  142.         $cliente->setDireccion($direccion);
  143.         $cliente->setRepresentanteLegal($representanteLegal);
  144.         $cliente->setFechaAlta(new DateTime($fechaAlta));
  145.         return $cliente;
  146.     }
  147.     public function save(McCliente $cliente) {
  148.         $cliente $this->repository->save($cliente);        
  149.         $cliente $this->toarray($cliente"cliente");
  150.         return $cliente;
  151.     }
  152.     public function getById($id) {
  153.         $cliente $this->repository->findOneById($id);
  154.         $cliente $this->toarray($cliente"cliente");
  155.         return $cliente;
  156.     }
  157.     public function update(McCliente $clienteint $id) {
  158.         $cliente $this->repository->update($cliente);        
  159.         $cliente $this->toarray($cliente"cliente");
  160.         return $cliente;
  161.     }
  162.     public function getClientes() {
  163.         $clientes $this->repository->findAll();
  164.         $clientes $this->toarray($clientes'cliente');
  165.         return $clientes;
  166.     }
  167.     public function getClienteById($id) {
  168.         return $this->repository->findOneById($id);
  169.     }
  170.     public function saveOrGetCliente ($arrayCliente) {
  171.         if (!is_null($arrayCliente['id'])) { // Si el cliente ya existe en la base de datos
  172.             return $this->repository->findOneById((int) $arrayCliente['id']);        
  173.         } else { //Si el cliente no existe en la base de datos                        
  174.             $cliente = new McCliente();
  175.             $cliente->setFechaCreacion(new DateTime());
  176.             //Calculo del número
  177.             $clientes $this->repository->findAll();
  178.             $clientes $this->toarray($clientes"cliente");
  179.             if (count($clientes) == 0) {
  180.                 $numero 1;
  181.             } else {
  182.                 $numero count($clientes) + 1;
  183.             } 
  184.             $cliente->setNumero($numero);
  185.             $cliente->setCuit($arrayCliente['cuit']);
  186.             $cliente->setRazonSocial($arrayCliente['razon_social']);
  187.             $cliente->setCelular($arrayCliente['celular']);
  188.             $cliente->setMail($arrayCliente['mail']);
  189.             $cliente->setCondicionIva($arrayCliente['condicion_iva']);
  190.     
  191.             return $this->repository->save($cliente);
  192.         }        
  193.     }
  194.     public function searchClientesByTermino($searchParam) {
  195.         $clientes $this->repository->searchClientesByTermino($searchParam);
  196.         $clientes $this->toarray($clientes'cliente');
  197.         return $clientes;
  198.     }
  199. }