Files
Final_Das/Modelo/RepositorioLote.cs

84 lines
2.3 KiB
C#

using Entidades;
using System.Runtime.Intrinsics.Arm;
namespace Modelo
{
public sealed class RepositorioLote : Repositorio<Lote>
{
public RepositorioLote(Context context)
{
this.context = context;
}
public override List<Lote> Listar()
{
return context.Lotes.ToList();
}
public Lote ObtenerPorId(int Tid)
{
Lote lot = context.Lotes.First(x => x.Id == Tid);
return lot ?? new Lote();
}
public void Add(Remito rem)
{
if (rem.MostrarLotes().Count <= 0) return;
foreach (var detalle in rem.MostrarLotes())
{
Add(detalle);
}
}
public override void Add(Lote t)
{
context.Lotes.Add(t);
}
public override void Del(Lote t)
{
Lote lot = context.Lotes.First(x => x.Id == t.Id);
if (lot == null) return;
context.Lotes.Remove(lot);
}
public override void Mod(Lote t)
{
context.Lotes.Update(t);
}
public bool DisminuirStock(DetalleFactura detalleFactura)
{
var detalle = context.DetalleFacturas.First(x => x.Id == detalleFactura.Id);
bool ret = false; int cantidad = detalle.Cantidad;
while (cantidad > 0)
{
var elementoAdisminuir = context.Lotes.Where(x => x.Habilitado == true)
.First(x => x.Producto.Id == detalle.Producto.Id);
cantidad -= elementoAdisminuir.Cantidad;
if (cantidad > 0)
{
elementoAdisminuir.Cantidad = 0;
elementoAdisminuir.Habilitado = false;
}
else if (cantidad == 0)
{
elementoAdisminuir.Cantidad = 0;
elementoAdisminuir.Habilitado = false;
ret = true;
}
else
{
elementoAdisminuir.Cantidad = -cantidad;
ret = true;
}
context.Lotes.Update(elementoAdisminuir);
}
context.DetalleFacturas.Update(detalle);
return ret;
}
}
}