670 lines
No EOL
31 KiB
C#
670 lines
No EOL
31 KiB
C#
using ClosedXML.Excel;
|
|
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 System.Data;
|
|
using static CuentasCobrar.UI.Helpers.Enum;
|
|
|
|
namespace CuentasCobrar.UI.Controllers
|
|
{
|
|
public class FacturaController : BaseController
|
|
{
|
|
private readonly IFacturaAPI _facturaAPI;
|
|
private readonly IMonedaAPI _monedaAPI;
|
|
private readonly IClienteAPI _clienteAPI;
|
|
private readonly ITipoDocumentoAPI _tipoDocumentoAPI;
|
|
private readonly IUsuarioAPI _usuarioAPI;
|
|
|
|
public FacturaController ( IUsuarioAPI usuarioAPI, IFacturaAPI facturaAPI, IMonedaAPI monedaAPI,
|
|
IClienteAPI clienteAPI, ITipoDocumentoAPI tipoDocumentoAPI )
|
|
{
|
|
_facturaAPI = facturaAPI;
|
|
_monedaAPI = monedaAPI;
|
|
_clienteAPI = clienteAPI;
|
|
_tipoDocumentoAPI = tipoDocumentoAPI;
|
|
_usuarioAPI = usuarioAPI;
|
|
}
|
|
|
|
public async Task<ActionResult> Index ( FacturaFilterModel filtros )
|
|
{
|
|
List<FacturaViewModel> facturas = new List<FacturaViewModel> ();
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
var rolesCookie = Request.Cookies["CookieRoles"];
|
|
facturas = await _facturaAPI.ObtenerFacturasFiltradas ( token, filtros );
|
|
ViewBag.Monedas = await _monedaAPI.ObtenerMonedas ( token );
|
|
ViewBag.Clientes = await _clienteAPI.ObtenerClientes ( token );
|
|
ViewBag.TiposDocumento = await _tipoDocumentoAPI.ObtenerTiposDocumento ( token );
|
|
ViewBag.Filtros = filtros;
|
|
ViewBag.Roles = new List<string> ( rolesCookie.Split ( "," ) );
|
|
}
|
|
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, "Factura" );
|
|
return RedirectToAction ( "Index", "Home" );
|
|
}
|
|
}
|
|
return View ( facturas );
|
|
}
|
|
|
|
public async Task<ActionResult> ActualizarFactura ( FacturaViewModel factura, FacturaFilterModel filtros )
|
|
{
|
|
|
|
bool respuestaFactura = false;
|
|
var filtrosDevuelta = new
|
|
{
|
|
consecutivo = factura.Consecutivo,
|
|
NumeroDePagina = filtros.NumeroDePagina,
|
|
RegistrosPorPagina = filtros.RegistrosPorPagina,
|
|
IdTipoDocumentoFiltro = filtros.IdTipoDocumentoFiltro,
|
|
IdMonedaFiltro = filtros.IdMonedaFiltro,
|
|
EstadoFactura = filtros.EstadoFactura,
|
|
FechaInicioFiltro = filtros.FechaInicioFiltro,
|
|
FechaFinFiltro = filtros.FechaFinFiltro,
|
|
BusquedaFactura = filtros.BusquedaFactura
|
|
};
|
|
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
if (factura.FechaVencimiento < factura.FechaEmision)
|
|
{
|
|
ModelState.AddModelError ( $"FechaVencimiento",
|
|
$"La fecha de vencimiento no puede ser menor a la fecha de emisión" );
|
|
}
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
respuestaFactura = await _facturaAPI.ActualizarFactura ( token, factura );
|
|
|
|
if (respuestaFactura)
|
|
{
|
|
CustomNotification ( "Se modificó el factura de manera exitosa", TipoNotificacion.success, NotificationPosition.Center, "Factura" );
|
|
return RedirectToAction ( "Detalles", new RouteValueDictionary ( filtrosDevuelta ) );
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Factura" );
|
|
return RedirectToAction ( "Detalles", new RouteValueDictionary ( filtrosDevuelta ) );
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ViewBag.Clientes = await _clienteAPI.ObtenerClientes ( token );
|
|
ViewBag.TiposDocumento = await _tipoDocumentoAPI.ObtenerTiposDocumento ( token );
|
|
ViewBag.Monedas = await _monedaAPI.ObtenerMonedas ( token );
|
|
ViewBag.Filtros = filtros;
|
|
return View ( "Editar", factura );
|
|
}
|
|
}
|
|
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, "Factura" );
|
|
return RedirectToAction ( "Detalles", new RouteValueDictionary ( filtrosDevuelta ) );
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public async Task<ActionResult> AgregarFactura ( FacturaViewModel factura, FacturaFilterModel filtros )
|
|
{
|
|
bool respuesta = false;
|
|
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
if (factura.FechaVencimiento < factura.FechaEmision)
|
|
{
|
|
ModelState.AddModelError ( $"FechaVencimiento",
|
|
$"La fecha de vencimiento no puede ser menor a la fecha de emisión" );
|
|
}
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
|
|
factura.Saldo = factura.TotalFactura;
|
|
respuesta = await _facturaAPI.AgregarFactura ( token, factura );
|
|
|
|
if (respuesta)
|
|
{
|
|
CustomNotification ( "Se agregó el factura de manera exitosa", TipoNotificacion.success, NotificationPosition.Center, "Factura" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Factura" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
{
|
|
ViewBag.Clientes = await _clienteAPI.ObtenerClientes ( token );
|
|
ViewBag.TiposDocumento = await _tipoDocumentoAPI.ObtenerTiposDocumento ( token );
|
|
ViewBag.Monedas = await _monedaAPI.ObtenerMonedas ( token );
|
|
ViewBag.Filtros = filtros;
|
|
return View ( "Agregar", factura );
|
|
|
|
}
|
|
}
|
|
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, "Factura" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
}
|
|
}
|
|
|
|
public async Task EliminarFactura ( string consecutivo )
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
await _facturaAPI.EliminarFactura ( token, consecutivo );
|
|
}
|
|
public async Task<ActionResult> AgregarNotaCredito ( FacturaViewModel factura, FacturaFilterModel filtros )
|
|
{
|
|
bool respuesta = false;
|
|
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
if (factura.FechaVencimiento < factura.FechaEmision)
|
|
{
|
|
ModelState.AddModelError ( $"FechaVencimiento",
|
|
$"La fecha de vencimiento no puede ser menor a la fecha de emisión" );
|
|
}
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
|
|
factura.Saldo = 0;
|
|
respuesta = await _facturaAPI.GenerarNotaCredito ( token, factura );
|
|
|
|
if (respuesta)
|
|
{
|
|
CustomNotification ( "Se agregó la nota de crédito de manera exitosa", TipoNotificacion.success, NotificationPosition.Center, "Factura" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Factura" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
{
|
|
ViewBag.Clientes = await _clienteAPI.ObtenerClientes ( token );
|
|
ViewBag.TiposDocumento = await _tipoDocumentoAPI.ObtenerTiposDocumento ( token );
|
|
ViewBag.Monedas = await _monedaAPI.ObtenerMonedas ( token );
|
|
ViewBag.Filtros = filtros;
|
|
return View ( "AgregarNotaCredito", factura );
|
|
|
|
}
|
|
}
|
|
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, "Factura" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
}
|
|
}
|
|
|
|
public async Task<ActionResult> GenerarNotaCredito ( string consecutivo, FacturaFilterModel filtros )
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
FacturaViewModel factura = await _facturaAPI.ObtenerFactura ( token, consecutivo );
|
|
var tiposDocumento = await _tipoDocumentoAPI.ObtenerTiposDocumento ( token );
|
|
var clientes = await _clienteAPI.ObtenerClientes ( token );
|
|
|
|
factura.ConsecutivoNotaReferencia = factura.Consecutivo;
|
|
factura.Consecutivo = "";
|
|
factura.SubTotal = factura.TotalFactura * -1;
|
|
factura.TotalImpuestos = 0;
|
|
factura.TotalFactura = factura.SubTotal;
|
|
factura.IdTipoDocumento = tiposDocumento.FirstOrDefault ( x => x.Descripcion == "Nota Crédito" ).IdTipoDocumento;
|
|
factura.Estado = "CANCELADA";
|
|
|
|
ViewBag.Monedas = await _monedaAPI.ObtenerMonedas ( token );
|
|
ViewBag.Clientes = await _clienteAPI.ObtenerClientes ( token );
|
|
ViewBag.TiposDocumento = await _tipoDocumentoAPI.ObtenerTiposDocumento ( token );
|
|
ViewBag.Filtros = filtros;
|
|
return View ( factura );
|
|
}
|
|
|
|
|
|
public async Task<ActionResult> Detalles ( string consecutivo, FacturaFilterModel filtros )
|
|
{
|
|
FacturaViewModel factura = new FacturaViewModel ();
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
var rolesCookie = Request.Cookies["CookieRoles"];
|
|
factura = await _facturaAPI.ObtenerFactura ( token, consecutivo );
|
|
ViewBag.Cliente = await _clienteAPI.ObtenerCliente ( token, factura.NoIdentificacionCliente );
|
|
ViewBag.TipoDocumento = await _tipoDocumentoAPI.ObtenerTipoDocumento ( token, factura.IdTipoDocumento );
|
|
ViewBag.Moneda = await _monedaAPI.ObtenerMoneda ( token, factura.IdMoneda );
|
|
ViewBag.Filtros = filtros;
|
|
ViewBag.Roles = new List<string> ( rolesCookie.Split ( "," ) );
|
|
}
|
|
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, "Factura" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
}
|
|
return View ( factura );
|
|
}
|
|
|
|
public async Task<ActionResult> Editar ( string consecutivo, FacturaFilterModel filtros )
|
|
{
|
|
FacturaViewModel factura = new FacturaViewModel ();
|
|
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" );
|
|
}
|
|
factura = await _facturaAPI.ObtenerFactura ( token, consecutivo );
|
|
|
|
ViewBag.Monedas = await _monedaAPI.ObtenerMonedas ( token );
|
|
ViewBag.Clientes = await _clienteAPI.ObtenerClientes ( token );
|
|
ViewBag.TiposDocumento = await _tipoDocumentoAPI.ObtenerTiposDocumento ( 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, "Factura" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
}
|
|
return View ( factura );
|
|
}
|
|
|
|
public async Task<ActionResult> Agregar ( FacturaFilterModel 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.Clientes = await _clienteAPI.ObtenerClientes ( token );
|
|
ViewBag.TiposDocumento = await _tipoDocumentoAPI.ObtenerTiposDocumento ( token );
|
|
ViewBag.Monedas = await _monedaAPI.ObtenerMonedas ( token );
|
|
ViewBag.NumeroDePagina = 1;
|
|
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, "Factura" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
}
|
|
return View ();
|
|
}
|
|
|
|
public ActionResult CargarExcel ( FacturaFilterModel filtros )
|
|
{
|
|
var listaFacturas = new List<FacturaViewModel> ();
|
|
ViewBag.Filtros = filtros;
|
|
return View ( listaFacturas );
|
|
}
|
|
|
|
public async Task<ActionResult> CargarParcialmenteExcel ( IFormFile archivo, FacturaFilterModel filtros )
|
|
{
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
if (archivo == null)
|
|
{
|
|
CustomNotification ( "Seleccione un archivo", TipoNotificacion.error, NotificationPosition.Center, "Factura" );
|
|
return RedirectToAction ( "CargarExcel", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
|
|
if (ValidarTipoArchivo ( archivo.ContentType ) == false)
|
|
{
|
|
CustomNotification ( "Tipo de archivo incorrecto", TipoNotificacion.error, NotificationPosition.Center, "Factura" );
|
|
return RedirectToAction ( "CargarExcel", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
|
|
var libroDeExcel = new XLWorkbook ( archivo.OpenReadStream () );
|
|
var hoja = libroDeExcel.Worksheet ( 1 );
|
|
var primeraFila = hoja.FirstRowUsed ().RangeAddress.FirstAddress.RowNumber;
|
|
var ultimaFila = hoja.LastRowUsed ().RangeAddress.FirstAddress.RowNumber;
|
|
|
|
if (hoja.LastColumnUsed ().RangeAddress.FirstAddress.ColumnNumber != 30)
|
|
{
|
|
CustomNotification ( "Cantidad de informacion insuficiente", TipoNotificacion.error, NotificationPosition.Center, "Factura" );
|
|
return RedirectToAction ( "CargarExcel", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
|
|
List<FacturaViewModel> facturas = new List<FacturaViewModel> ();
|
|
List<MonedaViewModel>? listaMonedas = await _monedaAPI.ObtenerMonedas ( token );
|
|
if (listaMonedas.Count == 0)
|
|
{
|
|
CustomNotification ( "No existen monedas", TipoNotificacion.error, NotificationPosition.Center, "Factura" );
|
|
return RedirectToAction ( "CargarExcel", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
List<TipoDocumentoViewModel>? listaTiposDocumentos = await _tipoDocumentoAPI.ObtenerTiposDocumento ( token );
|
|
if (listaTiposDocumentos.Count == 0)
|
|
{
|
|
CustomNotification ( "No existen tipos de documentos", TipoNotificacion.error, NotificationPosition.Center, "Factura" );
|
|
return RedirectToAction ( "CargarExcel", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
|
|
|
|
for (int i = primeraFila + 1; i <= ultimaFila; i++)
|
|
{
|
|
var fila = hoja.Row ( i );
|
|
|
|
var consecutivo = fila.Cell ( 1 ).GetString ();
|
|
var noIdentificacionCliente = fila.Cell ( 8 ).GetString ();
|
|
var moneda = fila.Cell ( 13 ).GetString ();
|
|
var tipoDocumento = fila.Cell ( 2 ).GetString ();
|
|
var fechaEmision = fila.Cell ( 5 ).GetString ();
|
|
var fechaVencimiento = fila.Cell ( 27 ).GetString ();
|
|
var consecutivoNotaReferencia = fila.Cell ( 3 ).GetString ();
|
|
var nombreReceptor = fila.Cell ( 6 ).GetString ();
|
|
var subTotal = fila.Cell ( 20 ).GetValue<decimal> ();
|
|
var totalImpuestos = fila.Cell ( 21 ).GetValue<decimal> ();
|
|
var totalFactura = fila.Cell ( 24 ).GetValue<decimal> ();
|
|
var diasCredito = fila.Cell ( 26 ).GetValue<int> ();
|
|
|
|
MonedaViewModel? MonedaVM = listaMonedas.Find ( x => x.Descripcion == moneda );
|
|
if (MonedaVM == null)
|
|
{
|
|
CustomNotification ( "No existe la moneda", TipoNotificacion.error, NotificationPosition.Center, "Factura" );
|
|
return RedirectToAction ( "CargarExcel", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
TipoDocumentoViewModel? TipoDocumentoVM = listaTiposDocumentos.Find ( x => x.Descripcion == tipoDocumento );
|
|
|
|
if (TipoDocumentoVM == null)
|
|
{
|
|
CustomNotification ( "No existe el tipo de documento", TipoNotificacion.error, NotificationPosition.Center, "Factura" );
|
|
return RedirectToAction ( "CargarExcel", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
|
|
FacturaViewModel factura = new FacturaViewModel
|
|
{
|
|
Consecutivo = consecutivo,
|
|
NoIdentificacionCliente = noIdentificacionCliente,
|
|
IdMoneda = MonedaVM.IdMoneda,
|
|
IdTipoDocumento = TipoDocumentoVM.IdTipoDocumento,
|
|
FechaEmision = DateTime.ParseExact ( fechaEmision, "dd/MM/yyyy", null ),
|
|
FechaVencimiento = DateTime.ParseExact ( fechaVencimiento, "dd/MM/yyyy", null ),
|
|
ConsecutivoNotaReferencia = consecutivoNotaReferencia,
|
|
NombreReceptor = nombreReceptor,
|
|
SubTotal = subTotal,
|
|
TotalImpuestos = totalImpuestos,
|
|
TotalFactura = totalFactura,
|
|
DiasCredito = diasCredito,
|
|
Saldo = totalFactura,
|
|
Estado = "PENDIENTE",
|
|
isActive = false,
|
|
MontoPagado = 0,
|
|
};
|
|
facturas.Add ( factura );
|
|
}
|
|
|
|
ViewBag.Monedas = listaMonedas;
|
|
ViewBag.Filtros = filtros;
|
|
ViewBag.TiposDocumento = listaTiposDocumentos;
|
|
ViewBag.NombreArchivo = archivo.FileName;
|
|
return View ( "CargarExcel", facturas );
|
|
}
|
|
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, "Factura" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<ActionResult> GuardarFacturasExcel ( List<FacturaViewModel> facturas, FacturaFilterModel filtros )
|
|
{
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
bool respuesta = false;
|
|
List<ClienteViewModel> clientes = await _clienteAPI.ObtenerClientes ( token );
|
|
foreach (FacturaViewModel factura in facturas)
|
|
{
|
|
factura.NombreReceptor = clientes.FirstOrDefault ( x => x.NoIdentificacion == factura.NoIdentificacionCliente ).Nombre;
|
|
}
|
|
if (ModelState.IsValid)
|
|
{
|
|
foreach (var factura in facturas)
|
|
{
|
|
factura.Estado = factura.Estado.ToUpper ();
|
|
respuesta = await _facturaAPI.AgregarFactura ( token, factura );
|
|
}
|
|
|
|
if (respuesta)
|
|
{
|
|
CustomNotification ( "Se agregó el factura de manera exitosa", TipoNotificacion.success, NotificationPosition.Center, "Factura" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
|
|
}
|
|
else
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Factura" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ViewBag.Filtros = filtros;
|
|
ViewBag.Monedas = await _monedaAPI.ObtenerMonedas ( token );
|
|
ViewBag.TiposDocumento = await _tipoDocumentoAPI.ObtenerTiposDocumento ( token );
|
|
return View ( "CargarExcel", facturas );
|
|
}
|
|
}
|
|
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, "Factura" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
}
|
|
}
|
|
|
|
[HttpGet ( "download" )]
|
|
public async Task<ActionResult> ExportarExcel ( FacturaFilterModel filtros )
|
|
{
|
|
try
|
|
{
|
|
var token = Request.Cookies["CookieToken"];
|
|
if (token == null)
|
|
{
|
|
string tokenUsuario = await CrearToken ( _usuarioAPI );
|
|
token = tokenUsuario;
|
|
CrearCookies ( tokenUsuario );
|
|
}
|
|
List<FacturaViewModel> facturas = await _facturaAPI.ObtenerFacturasFiltradas ( token, filtros );
|
|
List<MonedaViewModel>? listaMonedas = await _monedaAPI.ObtenerMonedas ( token );
|
|
List<TipoDocumentoViewModel>? listaTiposDocumentos = await _tipoDocumentoAPI.ObtenerTiposDocumento ( token );
|
|
|
|
DataTable dataTable = ExportarExcelFacturas.GenerarDataTable ( facturas, listaMonedas, listaTiposDocumentos );
|
|
MemoryStream stream = ExportarExcelFacturas.GenerarExcel ( dataTable );
|
|
var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
|
var fileName = "report.xlsx";
|
|
return File ( stream.ToArray (), contentType, fileName );
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
CustomNotification ( "Se produjo un error!", TipoNotificacion.error, NotificationPosition.Center, "Factura" );
|
|
return RedirectToAction ( "Index", new RouteValueDictionary ( filtros ) );
|
|
}
|
|
}
|
|
|
|
#region Methods
|
|
private bool ValidarTipoArchivo ( string tipoArchivo )
|
|
{
|
|
bool valido = false;
|
|
if (tipoArchivo == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") valido = true;
|
|
if (tipoArchivo == "application/vnd.ms-excel") valido = true;
|
|
|
|
return valido;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |