コード例 #1
0
 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();
         }
     }
 }
コード例 #2
0
 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);
     }
 }
コード例 #3
0
 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);
     }
 }
コード例 #4
0
 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();
     }
 }
コード例 #5
0
 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();
     }
 }
コード例 #6
0
 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();
     }
 }
コード例 #7
0
 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();
     }
 }
コード例 #8
0
 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();
     }
 }
コード例 #9
0
 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();
     }
 }
コード例 #10
0
 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();
     }
 }