<?php
namespace Noahtech\Sistemas\InterjamaBundle\Handler;
use Noahtech\Sistemas\InterjamaBundle\Entity\McCliente;
use Noahtech\Sistemas\InterjamaBundle\Utils\Constants;
use Noahtech\Sistemas\InterjamaBundle\Utils\EmailsMessages;
use Noahtech\Sistemas\InterjamaBundle\Utils\Encrypt;
use DateInterval;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpFoundation\Request;
class McClienteHandler extends BaseHandler {
public function __construct(ContainerInterface $container, EntityManagerInterface $entityManager) {
$this->container = $container;
$this->entityManager = $entityManager;
$this->repository = $entityManager->getRepository(McCliente::class);
}
public function search($offset, $limit, $sortField, $sortDirection, $searchParam) {
$lp = $this->repository->search($offset, $limit, $sortField, $sortDirection, $searchParam);
$clientes = $this->toarray($lp->getListado(), 'cliente');
$lp->setListado($clientes);
return $lp;
}
public function getClienteByRazonSocial($razonSocial) {
return $this->repository->findOneByRazonSocial($razonSocial);
}
public function getClienteByCuit($cuit) {
$cliente = $this->repository->findOneByCuit($cuit);
$aux = null;
if (!is_null($cliente)) {
$aux = $this->toarray($cliente, "cliente");
}
return $aux;
}
public function saveImageFile(&$archivo){
$data['result'] = false;
$path = $this->getPathFiles();
//Crea la carpeta si no existe
if (!file_exists($path)) {
mkdir($path, 0777, true);
}
$info = pathinfo($archivo['name']);
$ext = $info['extension']; // get the extension of the file
$name = $info['filename'];
$nameMd5 = md5($name.time());
$nameFileMd5 = "$nameMd5.$ext";
$nameFileMd5File = "$nameMd5.$ext";
$pathFile = $path.$nameFileMd5;
$archivo['result'] = true;
move_uploaded_file($archivo['tmp_name'], $pathFile);
$archivo['name_md5']=$nameMd5;
$archivo['name_file_Md5'] = $nameFileMd5;
$archivo['path_file']=$pathFile;
$archivo['path_only']=$path;
return $pathFile;
}
private function getPathFiles(){
$basePath = $this->container->getParameter('kernel.root_dir');
$basePath = str_replace('/app', '/web', $basePath);
$basePath = $basePath . $this->container->getParameter('path_files_archivos_clientes');
$path = "$basePath/";
return $path;
}
private function deleteFile($pathFile) {
try {
unlink($pathFile);
} catch (Exception $e) {
}
return $pathFile;
}
public function comprobarArchivo($archivo) {
//Sección que almadena el archivo relacionado
if (!is_null($archivo) && $archivo['name'] !== '') {
$pathFile = $this->saveImageFile($archivo);
if ($archivo['result']) {
$archivo = $archivo['name_file_Md5'];
} else {
$this->deleteFile($pathFile);
}
} else {
$archivo = null;
}
return $archivo;
}
public function getClienteFromRequest(Request $request, $archivo_poder, $archivo_presentacion, int $id = null):McCliente {
$cuit = $request->request->get('cuit');
$razon_social = $request->request->get('razon_social');
$celular = $request->request->get('celular');
$mail = $request->request->get('mail');
$mailAlternativo = $request->request->get('mail_alternativo');
$fechaAlta = date('Y-m-d', strtotime($request->request->get('fecha_alta')));
$condicion_iva = $request->request->get('condicion_iva');
$celular2 = $request->request->get('celular2');
$direccion = $request->request->get('direccion');
$representanteLegal = $request->request->get('representanteLegal');
if (is_null($id)) {
$cliente = new McCliente();
$cliente->setFechaCreacion(new DateTime());
//Calculo del número
$clientes = $this->repository->findAll();
$clientes = $this->toarray($clientes, "cliente");
if (count($clientes) == 0) {
$numero = 1;
} else {
$numero = count($clientes) + 1;
}
$cliente->setCuit($cuit);
$cliente->setNumero($numero);
$archivoPoderAux = $this->comprobarArchivo($archivo_poder);
$archivoPresentacionAux = $this->comprobarArchivo($archivo_presentacion);
$cliente->setArchivoPoder($archivoPoderAux);
$cliente->setArchivoPresentacion($archivoPresentacionAux);
} else {
// Se valida que no exista el CUIT
$cuitValido = $this->getClienteByCuit($cuit);
if (!is_null($cuitValido) && (int)$cuitValido['id'] != (int)$id) {
throw new HttpException(409, "El CUIT ingresado " . $cuit. " ya se encuentra registrado.");
}
$cliente = $this->repository->findOneById($id);
$cliente->setFechaActualizacion(new DateTime());
$cliente->setCuit($cuit);
$archivoPoderAux = $this->comprobarArchivo($archivo_poder);
if (!is_null($archivoPoderAux)) {
$cliente->setArchivoPoder($archivoPoderAux);
}
$archivoPresentacionAux = $this->comprobarArchivo($archivo_presentacion);
if (!is_null($archivoPresentacionAux)) {
$cliente->setArchivoPresentacion($archivoPresentacionAux);
}
}
$cliente->setRazonSocial($razon_social);
$cliente->setCelular($celular);
$cliente->setMail($mail);
$cliente->setMailAlternativo($mailAlternativo);
$cliente->setCondicionIva($condicion_iva);
$cliente->setCelular2($celular2);
$cliente->setDireccion($direccion);
$cliente->setRepresentanteLegal($representanteLegal);
$cliente->setFechaAlta(new DateTime($fechaAlta));
return $cliente;
}
public function save(McCliente $cliente) {
$cliente = $this->repository->save($cliente);
$cliente = $this->toarray($cliente, "cliente");
return $cliente;
}
public function getById($id) {
$cliente = $this->repository->findOneById($id);
$cliente = $this->toarray($cliente, "cliente");
return $cliente;
}
public function update(McCliente $cliente, int $id) {
$cliente = $this->repository->update($cliente);
$cliente = $this->toarray($cliente, "cliente");
return $cliente;
}
public function getClientes() {
$clientes = $this->repository->findAll();
$clientes = $this->toarray($clientes, 'cliente');
return $clientes;
}
public function getClienteById($id) {
return $this->repository->findOneById($id);
}
public function saveOrGetCliente ($arrayCliente) {
if (!is_null($arrayCliente['id'])) { // Si el cliente ya existe en la base de datos
return $this->repository->findOneById((int) $arrayCliente['id']);
} else { //Si el cliente no existe en la base de datos
$cliente = new McCliente();
$cliente->setFechaCreacion(new DateTime());
//Calculo del número
$clientes = $this->repository->findAll();
$clientes = $this->toarray($clientes, "cliente");
if (count($clientes) == 0) {
$numero = 1;
} else {
$numero = count($clientes) + 1;
}
$cliente->setNumero($numero);
$cliente->setCuit($arrayCliente['cuit']);
$cliente->setRazonSocial($arrayCliente['razon_social']);
$cliente->setCelular($arrayCliente['celular']);
$cliente->setMail($arrayCliente['mail']);
$cliente->setCondicionIva($arrayCliente['condicion_iva']);
return $this->repository->save($cliente);
}
}
public function searchClientesByTermino($searchParam) {
$clientes = $this->repository->searchClientesByTermino($searchParam);
$clientes = $this->toarray($clientes, 'cliente');
return $clientes;
}
}