Ejemplo n.º 1
0
 private void BtnBuyProduct_Click(object sender, RoutedEventArgs e)
 {
     if (TableProductOnStorage.HasItems)
     {
         Coming coming = new Coming();
         coming.IDСounteragent = IDCounteragent;
         coming.DateComing     = DateTime.Now;
         db.Coming.Add(coming);
         db.SaveChanges();
         int MaxComing = db.Coming.OrderByDescending(x => x.IDComing).FirstOrDefault().IDComing;
         foreach (ContainerItem item in TableProductOnStorage.Items)
         {
             ProductComing productComing = new ProductComing {
                 IDComing = MaxComing, IDProduct = item.Id, Price = Convert.ToDouble(item.Price), Quantity = Convert.ToInt32(item.Quantity)
             };
             db.ProductComing.Add(productComing);
             db.SaveChanges();
         }
         foreach (ContainerItem item in TableProductOnStorage.Items)
         {
             if (db.ProductOnStorage.Where(p => p.IdProduct == item.Id).FirstOrDefault() != null)
             {
                 double NewPrice          = Convert.ToDouble(item.Price);
                 int    NewQuantity       = Convert.ToInt32(item.Quantity);
                 double PriceInStorage    = db.ProductOnStorage.Where(p => p.IdProduct == item.Id).FirstOrDefault().Price;
                 int    QuantityInStorage = db.ProductOnStorage.Where(p => p.IdProduct == item.Id).FirstOrDefault().Quantity;
                 var    Product           = db.ProductOnStorage.Where(p => p.IdProduct == item.Id).FirstOrDefault();
                 Product.Price    = Math.Round((PriceInStorage * QuantityInStorage + NewPrice * NewQuantity) / (NewQuantity + QuantityInStorage), 2);
                 Product.Quantity = NewQuantity + QuantityInStorage;
                 db.SaveChanges();
             }
             else
             {
                 ProductOnStorage productOnStorage = new ProductOnStorage {
                     IdProduct = item.Id, Quantity = Convert.ToInt32(item.Quantity), Price = Convert.ToDouble(item.Price)
                 };
                 db.ProductOnStorage.Add(productOnStorage);
                 db.SaveChanges();
             }
         }
         OpenClass.RefreshTable.Load();
         MessageBox.Show("Товар успешно закуплен!");
         this.Close();
     }
     else
     {
         MessageBox.Show("Вы не выбрали ни один товар!");
     }
 }
Ejemplo n.º 2
0
 private void BtnSellProduct_Click(object sender, RoutedEventArgs e)
 {
     if (TableProductOnStorage.HasItems)
     {
         Purchase purchase = new Purchase();
         purchase.IDCounteragent = IDCounteragent;
         purchase.DatePurchase   = DateTime.Now;
         db.Purchase.Add(purchase);
         db.SaveChanges();
         int MaxPurchase = db.Purchase.OrderByDescending(x => x.IDPurchase).FirstOrDefault().IDPurchase;
         foreach (ContainerItem item in TableProductOnStorage.Items)
         {
             ProductPurchase productPurchase = new ProductPurchase {
                 IDPurchase = MaxPurchase, IDProduct = item.Id, Price = Convert.ToDecimal(item.Price), Quantity = Convert.ToInt32(item.Quantity)
             };
             db.ProductPurchase.Add(productPurchase);
             db.SaveChanges();
         }
         foreach (ContainerItem item in TableProductOnStorage.Items)
         {
             int QuantityInTable   = item.Quantity;
             int QuantityInStorage = db.ProductOnStorage.Where(p => p.IdProduct == item.Id).FirstOrDefault().Quantity;
             int NewQuantity       = QuantityInStorage - QuantityInTable;
             if (NewQuantity != 0)
             {
                 ProductOnStorage product = db.ProductOnStorage.Where(p => p.IdProduct == item.Id).FirstOrDefault();
                 product.Quantity = NewQuantity;
                 db.SaveChanges();
             }
             else
             {
                 ProductOnStorage product = db.ProductOnStorage.Where(p => p.IdProduct == item.Id).FirstOrDefault();
                 db.ProductOnStorage.Remove(product);
                 db.SaveChanges();
             }
         }
         OpenClass.RefreshTable.Load();
         MessageBox.Show("Товар успешно продан!");
         this.Close();
     }
     else
     {
         MessageBox.Show("Вы не выбрали ни один товар!");
     }
 }