338 lines
16 KiB
C#
338 lines
16 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 PagoController : BaseController
|
|
{
|
|
private readonly IPagoAPI _pagoAPI;
|
|
private readonly IBancoAPI _bancoAPI;
|
|
private readonly IMonedaAPI _monedaAPI;
|
|
private readonly IFacturaAPI _facturaAPI;
|
|
private readonly IClienteAPI _clienteAPI;
|
|
private readonly ITipoPagoAPI _tipoPagoAPI;
|
|
private readonly IUsuarioAPI _usuarioAPI;
|
|
private readonly ITipoDocumentoAPI _tipoDocumentoAPI;
|
|
private readonly IPagoContieneFacturaAPI _pagoContieneFacturaAPI;
|
|
|
|
public PagoController ( IUsuarioAPI usuarioAPI, IPagoAPI pagoAPI, IClienteAPI clienteAPI,
|
|
ITipoDocumentoAPI tipoDocumentoAPI, ITipoPagoAPI tipoPagoAPI, IFacturaAPI facturaAPI,
|
|
IMonedaAPI monedaAPI, IBancoAPI bancoAPI, IPagoContieneFacturaAPI pagoContieneFacturaAPI )
|
|
{
|
|
_pagoAPI = pagoAPI;
|
|
_usuarioAPI = usuarioAPI;
|
|
_bancoAPI = bancoAPI;
|
|
_monedaAPI = monedaAPI;
|
|
_facturaAPI = facturaAPI;
|
|
_clienteAPI = clienteAPI;
|
|
_tipoPagoAPI = tipoPagoAPI;
|
|
_tipoDocumentoAPI = tipoDocumentoAPI;
|
|
_pagoContieneFacturaAPI = pagoContieneFacturaAPI;
|
|
}
|
|
public async Task<ActionResult> Index ( PagoFilterModel filtros )
|
|
{
|
|
List<PagoViewModel> pagos = new List<PagoViewModel> ();
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
var rolesCookie = Request.Cookies["CookieRoles"];
|
|
pagos = await _pagoAPI.ObtenerPagosFiltrados ( token, filtros );
|
|
|
|
ViewBag.Roles = new List<string> ( rolesCookie.Split ( "," ) );
|
|
ViewBag.Filtros = filtros;
|
|
ViewBag.Monedas = await _monedaAPI.ObtenerMonedas ( token );
|
|
ViewBag.TiposPago = await _tipoPagoAPI.ObtenerTiposPago ( token );
|
|
ViewBag.Bancos = await _bancoAPI.ObtenerBancos ( token );
|
|
}
|
|
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, "Pago" );
|
|
return RedirectToAction ( "Index", "Home" );
|
|
}
|
|
}
|
|
return View ( pagos );
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<ActionResult> AgregarPago ( PagoViewModel pago, PagoFilterModel filtrosPago, FacturaFilterModel filtrosFactura )
|
|
{
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
bool respuesta = false;
|
|
|
|
#region Validaciones del monto pagado
|
|
var monedas = await _monedaAPI.ObtenerMonedas ( token );
|
|
var moneda = monedas.FirstOrDefault ( x => x.IdMoneda == pago.IdMoneda );
|
|
if (moneda != null && moneda.Descripcion.ToUpper () != "COLONES")
|
|
{
|
|
if (pago.TipoCambio == null)
|
|
{
|
|
ModelState.AddModelError ( $"TipoCambio",
|
|
$"Este campo es requerido" );
|
|
}
|
|
}
|
|
if (pago.Facturas != null)
|
|
{
|
|
for (int i = 0; i < pago.Facturas.Count; i++)
|
|
{
|
|
if (pago.Facturas[i].isActive == true)
|
|
{
|
|
if (pago.Facturas[i].MontoPagado > pago.Facturas[i].Saldo || pago.Facturas[i].MontoPagado <= 0)
|
|
{
|
|
ModelState.AddModelError ( $"Facturas[{i}].MontoPagado",
|
|
$"El monto ingresado debe ser mayor que 0.00 y menor que {pago.Facturas[i].Saldo}" );
|
|
}
|
|
if (pago.Facturas[i].MontoPagado == null)
|
|
{
|
|
ModelState.AddModelError ( $"Facturas[{i}].MontoPagado",
|
|
$"Este campo es requerido" );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
List<FacturaViewModel> Facturas = new List<FacturaViewModel> ();
|
|
foreach (FacturaViewModel factura in pago.Facturas)
|
|
{
|
|
if (factura.isActive == true)
|
|
{
|
|
if (factura.Saldo != null) factura.Saldo = factura.Saldo - (decimal)factura.MontoPagado;
|
|
Facturas.Add ( factura );
|
|
}
|
|
}
|
|
pago.Facturas = Facturas;
|
|
respuesta = await _pagoAPI.AgregarPago ( token, pago );
|
|
|
|
if (respuesta)
|
|
{
|
|
CustomNotification ( "Se agregó el pago de manera exitosa", TipoNotificacion.success, NotificationPosition.Center, "Pago" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtrosPago ) );
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Pago" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtrosPago ) );
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
filtrosFactura.EstadoFactura = "PENDIENTE";
|
|
filtrosFactura.ObtenerTodas = true;
|
|
if (filtrosFactura.IdCliente == null)
|
|
{
|
|
filtrosFactura.IdCliente = "0";
|
|
}
|
|
|
|
#region Datos necesarios al ViewBag
|
|
var TiposDocumento = await _tipoDocumentoAPI.ObtenerTiposDocumento ( token );
|
|
ViewBag.TiposDocumento = TiposDocumento;
|
|
ViewBag.FiltrosPago = filtrosPago;
|
|
ViewBag.FiltrosFactura = filtrosFactura;
|
|
ViewBag.TiposPago = await _tipoPagoAPI.ObtenerTiposPago ( token );
|
|
ViewBag.Monedas = await _monedaAPI.ObtenerMonedas ( token );
|
|
ViewBag.Bancos = await _bancoAPI.ObtenerBancos ( token );
|
|
ViewBag.Clientes = await _clienteAPI.ObtenerClientes ( token );
|
|
ViewBag.TipoPago = await _tipoPagoAPI.ObtenerTipoPago ( token, pago.IdTipoPago );
|
|
ViewBag.Moneda = await _monedaAPI.ObtenerMoneda ( token, pago.IdMoneda );
|
|
ViewBag.Banco = await _bancoAPI.ObtenerBanco ( token, pago.IdBanco );
|
|
#endregion
|
|
|
|
List<FacturaViewModel> facturasPendientes = await _facturaAPI.ObtenerFacturasFiltradas ( token, filtrosFactura );
|
|
ViewBag.Facturas = facturasPendientes.FindAll ( x => TiposDocumento.FirstOrDefault ( t => t.Descripcion == "Tiquete" ).IdTipoDocumento == x.IdTipoDocumento || TiposDocumento.FirstOrDefault ( t => t.Descripcion == "Factura" ).IdTipoDocumento == x.IdTipoDocumento ).ToList ();
|
|
|
|
return View ( "RealizarPago", pago );
|
|
}
|
|
}
|
|
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 ( filtrosPago ) );
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Pago" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtrosPago ) );
|
|
}
|
|
}
|
|
}
|
|
|
|
public async Task<ActionResult> Detalles ( int noRecibo, PagoFilterModel filtros )
|
|
{
|
|
PagoViewModel pago = new PagoViewModel ();
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
pago = await _pagoAPI.ObtenerPago ( token, noRecibo );
|
|
|
|
#region Datos necesarios al ViewBag
|
|
ViewBag.Filtros = filtros;
|
|
ViewBag.TiposDocumento = await _tipoDocumentoAPI.ObtenerTiposDocumento ( token );
|
|
ViewBag.Monedas = await _monedaAPI.ObtenerMonedas ( token );
|
|
ViewBag.Facturas = await _pagoContieneFacturaAPI.ObtenerPagoContieneFacturas ( token, noRecibo );
|
|
ViewBag.Banco = await _bancoAPI.ObtenerBanco ( token, pago.IdBanco );
|
|
ViewBag.TipoPago = await _tipoPagoAPI.ObtenerTipoPago ( token, pago.IdTipoPago );
|
|
#endregion
|
|
|
|
}
|
|
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, "Pago" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
}
|
|
return View ( pago );
|
|
}
|
|
|
|
public async Task<ActionResult> RealizarPago ( PagoFilterModel filtros, FacturaFilterModel filtrosFactura, PagoViewModel pago )
|
|
{
|
|
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" );
|
|
}
|
|
filtrosFactura.EstadoFactura = "PENDIENTE";
|
|
if (!ModelState.IsValid)
|
|
{
|
|
ModelState.Remove ( "IdBanco" );
|
|
ModelState.Remove ( "IdTipoPago" );
|
|
ModelState.Remove ( "IdMoneda" );
|
|
ModelState.Remove ( "FechaPago" );
|
|
ModelState.Remove ( "TipoCambio" );
|
|
}
|
|
filtrosFactura.ObtenerTodas = true;
|
|
if (filtrosFactura.IdCliente == null)
|
|
{
|
|
filtrosFactura.IdCliente = "0";
|
|
}
|
|
|
|
#region Datos necesarios al ViewBag
|
|
var TiposDocumento = await _tipoDocumentoAPI.ObtenerTiposDocumento ( token );
|
|
ViewBag.TiposDocumento = TiposDocumento;
|
|
ViewBag.FiltrosPago = filtros;
|
|
ViewBag.FiltrosFactura = filtrosFactura;
|
|
ViewBag.TiposPago = await _tipoPagoAPI.ObtenerTiposPago ( token );
|
|
ViewBag.Monedas = await _monedaAPI.ObtenerMonedas ( token );
|
|
ViewBag.Bancos = await _bancoAPI.ObtenerBancos ( token );
|
|
ViewBag.Clientes = await _clienteAPI.ObtenerClientes ( token );
|
|
ViewBag.TipoPago = await _tipoPagoAPI.ObtenerTipoPago ( token, pago.IdTipoPago );
|
|
ViewBag.Moneda = await _monedaAPI.ObtenerMoneda ( token, pago.IdMoneda );
|
|
ViewBag.Banco = await _bancoAPI.ObtenerBanco ( token, pago.IdBanco );
|
|
#endregion
|
|
|
|
List<FacturaViewModel> facturasPendientes = await _facturaAPI.ObtenerFacturasFiltradas ( token, filtrosFactura );
|
|
ViewBag.Facturas = facturasPendientes.FindAll ( x => TiposDocumento.FirstOrDefault ( t => t.Descripcion == "Tiquete" ).IdTipoDocumento == x.IdTipoDocumento || TiposDocumento.FirstOrDefault ( t => t.Descripcion == "Factura" ).IdTipoDocumento == x.IdTipoDocumento ).ToList ();
|
|
|
|
|
|
}
|
|
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, "Pago" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
}
|
|
return View ();
|
|
}
|
|
|
|
public async Task<ActionResult> DetallesFactura ( int noRecibo, string consecutivo, PagoFilterModel filtros )
|
|
{
|
|
FacturaViewModel factura = new FacturaViewModel ();
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
factura = await _facturaAPI.ObtenerFactura ( token, consecutivo );
|
|
ViewBag.Clientes = await _clienteAPI.ObtenerClientes ( token );
|
|
ViewBag.TiposDocumento = await _tipoDocumentoAPI.ObtenerTiposDocumento ( token );
|
|
ViewBag.Monedas = await _monedaAPI.ObtenerMonedas ( token );
|
|
ViewBag.NoRecibo = noRecibo;
|
|
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, "Pago" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
}
|
|
return View ( factura );
|
|
}
|
|
|
|
}
|
|
}
|