70 lines
2.6 KiB
C#
70 lines
2.6 KiB
C#
using System.Collections.ObjectModel;
|
|
using Entidades;
|
|
using Modelo;
|
|
|
|
namespace Controladora
|
|
{
|
|
public class ControladoraFacturas : Singleton<ControladoraFacturas>
|
|
{
|
|
public string Añadir(Factura t)
|
|
{
|
|
if (t == null) return "La Factura es nula, fallo la carga";
|
|
|
|
if (RepositorioFactura.Instance.ExistePorId(t.Id)) return $"La Factura con el ID {t.Id} ya existe";
|
|
if (t.Cliente == null || t.Cliente.Cuit == 0) return "Debe seleccionar un cliente antes de agregar la factura";
|
|
|
|
string checkstock = "";
|
|
foreach (var detalle in t.MostrarDetalles())
|
|
{
|
|
if (detalle.Cantidad > ControladoraLotes.Instance.MostrarStockDeProducto(detalle.Producto))
|
|
{
|
|
checkstock += $"No hay existencias en stock para realizar la venta de {detalle.Producto.Nombre}\n";
|
|
}
|
|
}
|
|
if (checkstock != "") return checkstock;
|
|
|
|
try
|
|
{
|
|
bool resultado = RepositorioFactura.Instance.Add(t);
|
|
string resultadolote = ControladoraLotes.Instance.DisminuirStock(t.MostrarDetalles());
|
|
return (resultado && (resultadolote == "Se Descontaron los productos")) ?
|
|
$"La Factura con el ID {t.Id} se cargó correctamente" :
|
|
$"Falló la carga de la Factura con el ID {t.Id}";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Captura cualquier excepción no prevista
|
|
return $"Ocurrió un error inesperado: {ex.Message}";
|
|
}
|
|
}
|
|
|
|
public ReadOnlyCollection<Factura> Listar()
|
|
{
|
|
return RepositorioFactura.Instance.Listar();
|
|
}
|
|
|
|
public ReadOnlyCollection<DetalleFactura> ListarDetallesFactura(Factura factura)
|
|
{
|
|
Factura facturaalistar = ControladoraFacturas.Instance.Listar().First(x => x.Id == factura.Id);
|
|
if (facturaalistar == null) return new ReadOnlyCollection<DetalleFactura>(new List<DetalleFactura>());
|
|
return facturaalistar.MostrarDetalles();
|
|
|
|
}
|
|
|
|
public ReadOnlyCollection<Producto> ListarProductos()
|
|
{
|
|
return RepositorioProductos.Instance.Listar()
|
|
.Where(x => x.Habilitado == true)
|
|
.ToList()
|
|
.AsReadOnly();
|
|
}
|
|
|
|
public ReadOnlyCollection<Cliente> ListarClientes()
|
|
{
|
|
return RepositorioClientes.Instance.Listar().Where(x => x.Habilitado == true)
|
|
.ToList()
|
|
.AsReadOnly();
|
|
}
|
|
}
|
|
}
|
|
|