389 lines
No EOL
19 KiB
C#
389 lines
No EOL
19 KiB
C#
using CuentasCobrar.CORE.Exceptions;
|
|
using CuentasCobrar.UI.Helpers;
|
|
using CuentasCobrar.UI.Models;
|
|
using CuentasCobrar.UI.Models.Filters;
|
|
using CuentasCobrar.UI.Services.Interfaces;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using static CuentasCobrar.UI.Helpers.Enum;
|
|
|
|
namespace CuentasCobrar.UI.Controllers
|
|
{
|
|
public class ClienteController : BaseController
|
|
{
|
|
private readonly IClienteAPI _clienteAPI;
|
|
private readonly ITelefonoAPI _telefonoAPI;
|
|
private readonly IPaisAPI _paisAPI;
|
|
private readonly ITipoIdentificacionAPI _tipoIdentificacionAPI;
|
|
private readonly ITipoTelefonoAPI _tipoTelefonoAPI;
|
|
private readonly IUsuarioAPI _usuarioAPI;
|
|
private readonly IFacturaAPI _facturasAPI;
|
|
|
|
|
|
public ClienteController ( IFacturaAPI facturasAPI, IUsuarioAPI usuarioAPI, IClienteAPI clienteAPI, ITelefonoAPI telefonoAPI,
|
|
IPaisAPI paisAPI, ITipoIdentificacionAPI tipoIdentificacionAPI, ITipoTelefonoAPI tipoTelefonoAPI )
|
|
{
|
|
_clienteAPI = clienteAPI;
|
|
_telefonoAPI = telefonoAPI;
|
|
_paisAPI = paisAPI;
|
|
_tipoIdentificacionAPI = tipoIdentificacionAPI;
|
|
_tipoTelefonoAPI = tipoTelefonoAPI;
|
|
_usuarioAPI = usuarioAPI;
|
|
_facturasAPI = facturasAPI;
|
|
}
|
|
|
|
public async Task<ActionResult> Index ( ClienteFilterModel filtros )
|
|
{
|
|
List<ClienteViewModel> clientes = new List<ClienteViewModel> ();
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
var rolesCookie = Request.Cookies["CookieRoles"];
|
|
clientes = await _clienteAPI.ObtenerClientesFiltrados ( token, filtros );
|
|
ViewBag.Roles = new List<string> ( rolesCookie.Split ( "," ) );
|
|
ViewBag.Telefonos = await _telefonoAPI.ObtenerTelefonos ( token );
|
|
ViewBag.Paises = await _paisAPI.ObtenerPaises ( token );
|
|
ViewBag.TiposIdentificacion = await _tipoIdentificacionAPI.ObtenerTiposIdentificacion ( token );
|
|
ViewBag.Filtros = filtros;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (ex.GetType () == typeof ( AuthorizeException ))
|
|
{
|
|
AuthorizeException excepcion = ex as AuthorizeException;
|
|
CustomNotification ( $"Error {excepcion.number}: {excepcion.Message}", TipoNotificacion.error, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index", "Home" );
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Banco" );
|
|
return RedirectToAction ( "Index", "Home" );
|
|
}
|
|
}
|
|
return View ( clientes );
|
|
}
|
|
|
|
public async Task<ActionResult> ActualizarCliente ( ClienteViewModel cliente, ClienteFilterModel filtros, string noIdentificacionVieja )
|
|
{
|
|
bool respuestaCliente = false;
|
|
bool respuestaTelefonos = true;
|
|
var filtrosDevuelta = new
|
|
{
|
|
NoIdentificacion = cliente.NoIdentificacion,
|
|
NumeroDePagina = filtros.NumeroDePagina,
|
|
RegistrosPorPagina = filtros.RegistrosPorPagina,
|
|
BusquedaCliente = filtros.BusquedaCliente
|
|
};
|
|
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
if (ModelState.IsValid)
|
|
{
|
|
respuestaCliente = await _clienteAPI.ActualizarCliente ( token, noIdentificacionVieja, cliente );
|
|
if (respuestaCliente)
|
|
{
|
|
if (cliente.Telefonos != null)
|
|
{
|
|
foreach (var telefono in cliente.Telefonos)
|
|
{
|
|
if (telefono.Numero == "ELIMINAR")
|
|
{
|
|
respuestaTelefonos = await _telefonoAPI.EliminarTelefono ( token, telefono.IdTelefono );
|
|
}
|
|
}
|
|
|
|
cliente.Telefonos.RemoveAll ( x => x.Numero == "ELIMINAR" );
|
|
|
|
foreach (var telefono in cliente.Telefonos)
|
|
{
|
|
if (telefono.Numero != "IGNORAR")
|
|
{
|
|
if (telefono.IdTelefono != 0)
|
|
{
|
|
telefono.Habilitado = cliente.Habilitado;
|
|
respuestaTelefonos = await _telefonoAPI.ActualizarTelefono ( token, telefono );
|
|
}
|
|
else
|
|
{
|
|
telefono.Habilitado = cliente.Habilitado;
|
|
respuestaTelefonos = await _telefonoAPI.AgregarTelefono ( token, telefono );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (respuestaCliente && respuestaTelefonos)
|
|
{
|
|
CustomNotification ( "Se modificó el cliente de manera exitosa", TipoNotificacion.success, NotificationPosition.Center, "Cliente" );
|
|
return RedirectToAction ( "Detalles", new RouteValueDictionary ( filtrosDevuelta ) );
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Cliente" );
|
|
return RedirectToAction ( "Detalles", new RouteValueDictionary ( filtrosDevuelta ) );
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ViewBag.Paises = await _paisAPI.ObtenerPaises ( token );
|
|
ViewBag.TiposIdentificacion = await _tipoIdentificacionAPI.ObtenerTiposIdentificacion ( token );
|
|
ViewBag.TiposTelefono = await _tipoTelefonoAPI.ObtenerTiposTelefono ( token );
|
|
ViewBag.Filtros = filtros;
|
|
|
|
return View ( "Editar", cliente );
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
if (ex.GetType () == typeof ( AuthorizeException ))
|
|
{
|
|
AuthorizeException excepcion = ex as AuthorizeException;
|
|
CustomNotification ( $"Error {excepcion.number}: {excepcion.Message}", TipoNotificacion.error, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Detalles", new RouteValueDictionary ( filtrosDevuelta ) );
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Banco" );
|
|
return RedirectToAction ( "Detalles", new RouteValueDictionary ( filtrosDevuelta ) );
|
|
}
|
|
}
|
|
}
|
|
|
|
public async Task<ActionResult> AgregarCliente ( ClienteViewModel cliente, ClienteFilterModel filtros )
|
|
{
|
|
bool respuesta = false;
|
|
List<TelefonoViewModel?>? telefonos = cliente.Telefonos;
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
if (ModelState.IsValid)
|
|
{
|
|
respuesta = await _clienteAPI.AgregarCliente ( token, cliente );
|
|
foreach (var telefono in telefonos)
|
|
{
|
|
if (telefono.Numero != "IGNORAR")
|
|
{
|
|
telefono.NoIdentificacionCliente = cliente.NoIdentificacion.ToUpper ();
|
|
await _telefonoAPI.AgregarTelefono ( token, telefono );
|
|
}
|
|
}
|
|
|
|
if (respuesta)
|
|
{
|
|
CustomNotification ( "Se agregó el cliente de manera exitosa", TipoNotificacion.success, NotificationPosition.Center, "Cliente" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Cliente" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ViewBag.Clientes = await _clienteAPI.ObtenerClientesFiltrados ( token, filtros );
|
|
ViewBag.Filtros = filtros;
|
|
ViewBag.Paises = await _paisAPI.ObtenerPaises ( token );
|
|
ViewBag.TiposIdentificacion = await _tipoIdentificacionAPI.ObtenerTiposIdentificacion ( token );
|
|
ViewBag.TiposTelefono = await _tipoTelefonoAPI.ObtenerTiposTelefono ( token );
|
|
|
|
return View ( "Agregar", cliente );
|
|
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (ex.GetType () == typeof ( AuthorizeException ))
|
|
{
|
|
AuthorizeException excepcion = ex as AuthorizeException;
|
|
CustomNotification ( $"Error {excepcion.number}: {excepcion.Message}", TipoNotificacion.error, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Cliente" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
}
|
|
}
|
|
|
|
public async Task EliminarCliente ( string noIdentificacion )
|
|
{
|
|
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
List<FacturaViewModel>? facturas = await _facturasAPI.ObtenerFacturas ( token );
|
|
if (facturas.FirstOrDefault ( x => x.NoIdentificacionCliente == noIdentificacion ) != null)
|
|
{
|
|
throw new BadRequestException ( (int)System.Net.HttpStatusCode.BadRequest, "No se puede eliminar el cliente porque se encuentra asociado a facturas" );
|
|
}
|
|
List<TelefonoViewModel>? telefonos = await _telefonoAPI.ObtenerTelefonos ( token );
|
|
List<TelefonoViewModel>? telefonosCliente = telefonos.FindAll ( x => x.NoIdentificacionCliente.ToUpper () == noIdentificacion.ToUpper () );
|
|
|
|
if (telefonosCliente.Count >= 1)
|
|
{
|
|
foreach (var telefono in telefonosCliente)
|
|
{
|
|
await _telefonoAPI.EliminarTelefono ( token, telefono.IdTelefono );
|
|
}
|
|
}
|
|
await _clienteAPI.EliminarCliente ( token, noIdentificacion );
|
|
}
|
|
|
|
public async Task<ActionResult> Detalles ( string noIdentificacion, ClienteFilterModel filtros )
|
|
{
|
|
ClienteViewModel cliente = new ClienteViewModel ();
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
var rolesCookie = Request.Cookies["CookieRoles"];
|
|
cliente = await _clienteAPI.ObtenerCliente ( token, noIdentificacion );
|
|
var telefonos = await _telefonoAPI.ObtenerTelefonos ( token );
|
|
var telefonosCliente = telefonos.FindAll ( x => x.NoIdentificacionCliente.ToUpper () == cliente.NoIdentificacion.ToUpper () );
|
|
cliente.Telefonos = telefonosCliente;
|
|
ViewBag.Roles = new List<string> ( rolesCookie.Split ( "," ) );
|
|
ViewBag.Paises = await _paisAPI.ObtenerPaises ( token );
|
|
ViewBag.PaisCliente = await _paisAPI.ObtenerPais ( token, cliente.IdPaisNacionalidad );
|
|
ViewBag.TiposIdentificacion = await _tipoIdentificacionAPI.ObtenerTiposIdentificacion ( token );
|
|
ViewBag.TipoIdentificacionCliente = await _tipoIdentificacionAPI.ObtenerTipoIdentificacion ( token, cliente.IdTipoIdentificacion );
|
|
ViewBag.TiposTelefono = await _tipoTelefonoAPI.ObtenerTiposTelefono ( token );
|
|
ViewBag.Filtros = filtros;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (ex.GetType () == typeof ( AuthorizeException ))
|
|
{
|
|
AuthorizeException excepcion = ex as AuthorizeException;
|
|
CustomNotification ( $"Error {excepcion.number}: {excepcion.Message}", TipoNotificacion.error, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Cliente" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
}
|
|
|
|
return View ( cliente );
|
|
}
|
|
|
|
public async Task<ActionResult> Editar ( string noIdentificacion, ClienteFilterModel filtros )
|
|
{
|
|
ClienteViewModel cliente = new ClienteViewModel ();
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
var rolesCookie = Request.Cookies["CookieRoles"];
|
|
var listaRoles = new List<string> ( rolesCookie.Split ( "," ) );
|
|
if (!listaRoles.Exists ( r => r == "ADMINISTRADOR" || r == "OPERATIVO" ))
|
|
{
|
|
throw new AuthorizeException ( 403, "Usuario no autorizado" );
|
|
}
|
|
cliente = await _clienteAPI.ObtenerCliente ( token, noIdentificacion );
|
|
var telefonos = await _telefonoAPI.ObtenerTelefonos ( token );
|
|
var telefonosCliente = telefonos.FindAll ( x => x.NoIdentificacionCliente.ToUpper () == cliente.NoIdentificacion.ToUpper () );
|
|
cliente.Telefonos = telefonosCliente;
|
|
ViewBag.Paises = await _paisAPI.ObtenerPaises ( token );
|
|
ViewBag.TiposIdentificacion = await _tipoIdentificacionAPI.ObtenerTiposIdentificacion ( token );
|
|
ViewBag.TiposTelefono = await _tipoTelefonoAPI.ObtenerTiposTelefono ( token );
|
|
ViewBag.Filtros = filtros;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (ex.GetType () == typeof ( AuthorizeException ))
|
|
{
|
|
AuthorizeException excepcion = ex as AuthorizeException;
|
|
CustomNotification ( $"Error {excepcion.number}: {excepcion.Message}", TipoNotificacion.error, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Cliente" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
}
|
|
|
|
return View ( cliente );
|
|
}
|
|
|
|
public async Task<ActionResult> Agregar ( ClienteFilterModel filtros )
|
|
{
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
var rolesCookie = Request.Cookies["CookieRoles"];
|
|
var listaRoles = new List<string> ( rolesCookie.Split ( "," ) );
|
|
if (!listaRoles.Exists ( r => r == "ADMINISTRADOR" || r == "OPERATIVO" ))
|
|
{
|
|
throw new AuthorizeException ( 403, "Usuario no autorizado" );
|
|
}
|
|
ViewBag.Paises = await _paisAPI.ObtenerPaises ( token );
|
|
ViewBag.TiposIdentificacion = await _tipoIdentificacionAPI.ObtenerTiposIdentificacion ( token );
|
|
ViewBag.TiposTelefono = await _tipoTelefonoAPI.ObtenerTiposTelefono ( token );
|
|
ViewBag.Filtros = filtros;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (ex.GetType () == typeof ( AuthorizeException ))
|
|
{
|
|
AuthorizeException excepcion = ex as AuthorizeException;
|
|
CustomNotification ( $"Error {excepcion.number}: {excepcion.Message}", TipoNotificacion.error, NotificationPosition.Center, "Usuario" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Cliente" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
}
|
|
|
|
return View ();
|
|
}
|
|
|
|
}
|
|
} |