117 lines
3.8 KiB
C#
117 lines
3.8 KiB
C#
using Entidades;
|
|
using Modelo;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Controladora
|
|
{
|
|
public class ControladoraLotes : Singleton<ControladoraLotes>
|
|
{
|
|
/*
|
|
public string Añadir(Lote t)
|
|
{
|
|
if (t == null) return "El Lote es nulo, falló la carga";
|
|
|
|
try
|
|
{
|
|
bool resultado = RepositorioLote.Instance.Add(t);
|
|
return resultado ?
|
|
$"El Lote con el ID {t.Id} se cargó correctamente" :
|
|
$"Falló la carga del Lote con el ID {t.Id}";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Captura cualquier excepción no prevista
|
|
return $"Ocurrió un error inesperado: {ex.Message}";
|
|
}
|
|
}
|
|
|
|
public string Modificar(Lote t)
|
|
{
|
|
if (t == null) return "El Lote es nulo, falló la modificación";
|
|
|
|
try
|
|
{
|
|
bool resultado = RepositorioLote.Instance.Mod(t);
|
|
return resultado ?
|
|
$"El Lote con el ID {t.Id} se modificó correctamente" :
|
|
$"Falló la modificación del Lote con el ID {t.Id}";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Captura cualquier excepción no prevista
|
|
return $"Ocurrió un error inesperado: {ex.Message}";
|
|
}
|
|
}
|
|
|
|
public string Eliminar(Lote t)
|
|
{
|
|
if (t == null) return "El Lote es nulo, falló la eliminación";
|
|
|
|
try
|
|
{
|
|
bool resultado = RepositorioLote.Instance.Del(t);
|
|
return resultado ?
|
|
$"El Lote con el ID {t.Id} se eliminó correctamente" :
|
|
$"Falló la eliminación del Lote con el ID {t.Id}";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Captura cualquier excepción no prevista
|
|
return $"Ocurrió un error inesperado: {ex.Message}";
|
|
}
|
|
}
|
|
*/
|
|
|
|
public string DisminuirStock(ReadOnlyCollection<DetalleFactura> detalleFactura)
|
|
{
|
|
var ret = false;
|
|
foreach (var detalle in detalleFactura)
|
|
{
|
|
if (detalle == null) return "El detalle es nulo";
|
|
if (detalle.Producto.Id < 0) return "Esta mal cargada la Id de producto";
|
|
var productos = RepositorioProductos.Instance.Listar();
|
|
if (productos.Any(x => x.Id != detalle.Producto.Id)) return "No hay Productos con esa id";
|
|
ret = RepositorioLote.Instance.DisminuirStock(detalle);
|
|
|
|
}
|
|
|
|
return (ret) ?
|
|
"Se Descontaron los productos":
|
|
"Se fallo al diminuir el stock";
|
|
|
|
}
|
|
|
|
public int MostrarStockDeProducto(Producto producto)
|
|
{
|
|
if (producto == null) return 0;
|
|
if (producto.Id < 0) return 0;
|
|
var lotes = RepositorioLote.Instance.Listar();
|
|
if (lotes.Any(x => x.Producto.Id != producto.Id)) return 0; // basicamente no hay productos con esa id
|
|
|
|
var CantidadStock = lotes
|
|
.Where(x => x.Producto.Id == producto.Id)
|
|
.Sum(x=> x.Cantidad);
|
|
return CantidadStock;
|
|
}
|
|
|
|
public ReadOnlyCollection<Lote> Listar()
|
|
{
|
|
try
|
|
{
|
|
return RepositorioLote.Instance.Listar().Where(x=> x.Habilitado == true)
|
|
.ToList().AsReadOnly();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Captura cualquier excepción no prevista
|
|
throw new InvalidOperationException($"Ocurrió un error inesperado: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
}
|