public void AddProductSizes(List <ProductsSize> ps) { using (var ctx = new BirovAmContext()) { foreach (ProductsSize s in ps) { ProductsSize prs = ctx.ProductsSizes.Where(i => i.ProductID == s.ProductID && i.SizeID == s.SizeID).FirstOrDefault(); if (prs == null) { if (s.Included) { ctx.ProductsSizes.Add(s); } } else { if (s.Included) { if (prs.Stock != s.Stock) { prs.Stock = s.Stock; } } else { ctx.Entry(prs).State = EntityState.Deleted; } } ctx.SaveChanges(); } } }
public bool OutOfStock(int pId, int sId, int qty) { using (var ctx = new BirovAmContext()) { ProductsSize ps = ctx.ProductsSizes.Where(p => p.ProductID == pId && p.SizeID == sId).FirstOrDefault(); return(ps.Stock - qty <= 5); } }
public bool OutOfStock(int odId, int qty, int sId) { using (var ctx = new BirovAmContext()) { OrderDetail od = ctx.OrderDetails.Include(d => d.Order).Include(d => d.Product).Where(d => d.OrderDetailID == odId).FirstOrDefault(); ProductsSize ps = ctx.ProductsSizes.Where(p => p.ProductID == od.ProductID && (sId == 0 ? p.SizeID == od.SizeID : p.SizeID == sId)).FirstOrDefault(); return(qty > 0 ? (ps.Stock + (od.Quantity.Value - qty)) <= 5 : (ps.Stock - od.Quantity.Value) <= 5); } }
public void AddSizeToOrderDetail(int orderDetailId, int sizeId) { using (var ctx = new BirovAmContext()) { OrderDetail od = ctx.OrderDetails.Where(o => o.OrderDetailID == orderDetailId).FirstOrDefault(); od.SizeID = sizeId; ProductsSize ps = ctx.ProductsSizes.Where(p => p.ProductID == od.ProductID && p.SizeID == sizeId).FirstOrDefault(); ps.Stock -= 1; ctx.SaveChanges(); } }
public void DeleteOrderDetail(int odId) { using (var ctx = new BirovAmContext()) { var od = ctx.OrderDetails.Where(d => d.OrderDetailID == odId).FirstOrDefault(); od.DeleteFlag = true; ProductsSize ps = ctx.ProductsSizes.Where(p => p.ProductID == od.ProductID && p.SizeID == od.SizeID).FirstOrDefault(); ps.Stock += od.Quantity.Value; ctx.SaveChanges(); od.Order.TotalCost = ctx.OrderDetails.Where(x => x.OrderID == od.Order.OrderID && x.DeleteFlag != true).Sum(x => x.Price); od.Order.TotalQuantity = ctx.OrderDetails.Where(x => x.OrderID == od.Order.OrderID && x.DeleteFlag != true).Sum(x => x.Quantity); ctx.SaveChanges(); } }
public void UpdateSize(int sId, int odId) { using (var ctx = new BirovAmContext()) { OrderDetail od = ctx.OrderDetails.Where(d => d.OrderDetailID == odId).FirstOrDefault(); var origSid = od.SizeID; od.SizeID = sId; ProductsSize ps1 = ctx.ProductsSizes.Where(p => p.ProductID == od.ProductID && p.SizeID == origSid).FirstOrDefault(); ProductsSize ps2 = ctx.ProductsSizes.Where(p => p.ProductID == od.ProductID && p.SizeID == od.SizeID).FirstOrDefault(); ps1.Stock += od.Quantity.Value; ps2.Stock -= od.Quantity.Value; ctx.SaveChanges(); } }
public void AddQuantityToOrderDetail(int quantity, int orderDetailId, int orderId) { using (var ctx = new BirovAmContext()) { OrderDetail od = ctx.OrderDetails.Include(or => or.Product).Include(or => or.Order).Where(or => or.OrderDetailID == orderDetailId).FirstOrDefault(); od.Quantity = quantity; od.Price = od.Product.Price * od.Quantity; ProductsSize ps = ctx.ProductsSizes.Where(p => p.ProductID == od.ProductID && p.SizeID == od.SizeID).FirstOrDefault(); ps.Stock -= (quantity - 1); ctx.SaveChanges(); od.Order.TotalCost = ctx.OrderDetails.Where(x => x.OrderID == orderId && x.DeleteFlag != true).Sum(x => x.Price); od.Order.TotalQuantity = ctx.OrderDetails.Where(x => x.OrderID == orderId && x.DeleteFlag != true).Sum(x => x.Quantity); ctx.SaveChanges(); } }
public void AddOrderDetail(OrderDetail od) { using (var ctx = new BirovAmContext()) { var product = ctx.Products.Where(p => p.ProductID == od.ProductID).FirstOrDefault(); od.Price = product.Price * od.Quantity; ctx.OrderDetails.Add(od); ProductsSize ps = ctx.ProductsSizes.Where(p => p.ProductID == od.ProductID && p.SizeID == od.SizeID).FirstOrDefault(); ps.Stock -= od.Quantity.Value; ctx.SaveChanges(); var order = ctx.Orders.Where(o => o.OrderID == od.OrderID).FirstOrDefault(); order.TotalCost = ctx.OrderDetails.Where(x => x.OrderID == order.OrderID && x.DeleteFlag != true).Sum(x => x.Price); order.TotalQuantity = ctx.OrderDetails.Where(x => x.OrderID == order.OrderID && x.DeleteFlag != true).Sum(x => x.Quantity); ctx.SaveChanges(); } }
public void UpdateQuantity(int qty, int odId) { using (var ctx = new BirovAmContext()) { OrderDetail od = ctx.OrderDetails.Include(d => d.Order).Include(d => d.Product).Where(d => d.OrderDetailID == odId).FirstOrDefault(); var origQty = od.Quantity.Value; od.Quantity = qty; od.Price = od.Product.Price * od.Quantity; ProductsSize ps = ctx.ProductsSizes.Where(p => p.ProductID == od.ProductID && p.SizeID == od.SizeID).FirstOrDefault(); ps.Stock += (origQty - qty); ctx.SaveChanges(); od.Order.TotalCost = ctx.OrderDetails.Where(x => x.OrderID == od.Order.OrderID && x.DeleteFlag != true).Sum(x => x.Price); od.Order.TotalQuantity = ctx.OrderDetails.Where(x => x.OrderID == od.Order.OrderID && x.DeleteFlag != true).Sum(x => x.Quantity); ctx.SaveChanges(); } }
public void DeleteOrderDetail(int odId, int oId) { using (var ctx = new BirovAmContext()) { OrderDetail od = ctx.OrderDetails.Where(d => d.OrderDetailID == odId).FirstOrDefault(); ProductsSize ps = ctx.ProductsSizes.Where(p => p.ProductID == od.ProductID && p.SizeID == od.SizeID).FirstOrDefault(); if (ps != null) { ps.Stock += od.Quantity.Value; } ctx.Entry(od).State = EntityState.Deleted; ctx.SaveChanges(); Order order = ctx.Orders.Where(o => o.OrderID == oId).FirstOrDefault(); order.TotalCost = ctx.OrderDetails.Where(x => x.OrderID == oId && x.DeleteFlag != true).Sum(x => x.Price); order.TotalQuantity = ctx.OrderDetails.Where(x => x.OrderID == oId && x.DeleteFlag != true).Sum(x => x.Quantity); ctx.SaveChanges(); } }