public void AgregarItem(long mesaId, decimal cantidad, ProductoMesaDto dto) { using (var context = new ModeloXCommerceContainer()) { var comprobante = context.Comprobantes .OfType <ComprobanteSalon>() .FirstOrDefault(x => x.MesaId == mesaId && x.EstadoComprobanteSalon == EstadoComprobanteSalon.EnProceso); if (comprobante == null) { throw new Exception("Ocurrio un error al obtener el comprobante"); } var item = comprobante.DetalleComprobantes .FirstOrDefault(x => x.Codigo == dto.Codigo); if (item == null) { comprobante.DetalleComprobantes.Add(new DetalleComprobante { ArticuloId = (int)dto.Id, Cantidad = cantidad, Codigo = dto.Codigo, Descripcion = dto.Descripcion, PrecioUnitario = dto.Precio, SubTotal = dto.Precio * cantidad, }); } else { item.Cantidad += cantidad; item.SubTotal = item.Cantidad * item.PrecioUnitario; } if (dto.DescuentaStock) { var producto = context.Articulos.FirstOrDefault(x => x.Id == dto.Id); if (producto == null) { throw new Exception("Ocurrio un error al descontar stock"); } if (producto.Stock >= cantidad) { producto.Stock -= cantidad; cantidad -= 1; } } context.SaveChanges(); } }
public void QuitarItem(long mesaId, decimal cantidad, ProductoMesaDto dto) { using (var context = new ModeloXCommerceContainer()) { var comprobante = context.Comprobantes .OfType <ComprobanteSalon>() .FirstOrDefault(x => x.MesaId == mesaId && x.EstadoComprobanteSalon == EstadoComprobanteSalon.EnProceso); if (comprobante == null) { throw new Exception("Ocurrio un error al obtener el comprobante"); } var item = comprobante.DetalleComprobantes .FirstOrDefault(x => x.Codigo == dto.Codigo); if (item == null) { return; } else { if (item.Cantidad > 1) { item.Cantidad -= cantidad; item.SubTotal = item.Cantidad * item.PrecioUnitario; } else if (item.Cantidad == 1) { comprobante.DetalleComprobantes.Remove(item); context.DetalleComprobantes.Remove(item); } } context.SaveChanges(); } }