private void AddProduct() { ModelName = Trim(ModelName); FirstName = Trim(FirstName); LastName = Trim(LastName); SerialNumber = Trim(SerialNumber); Notes = Trim(Notes); Product product = new Product {SerialNumber = SerialNumber, SellingPrice = SellingPrice, Notes = Notes}; if (Models.Contains(ModelName)) product.Model = GetModelIDByName(ModelName); if (!Models.Contains(ModelName) || product.Model == null) { Model model = new Model {Name = ModelName, CategoryID = SelectedCategory.ID}; _mainViewModel.Context.Models.Add(model); product.Model = model; } _mainViewModel.Context.Products.Add(product); var client = _mainViewModel.Context.Clients.FirstOrDefault( x => x.Contragent.FirstName == FirstName && x.Contragent.LastName == LastName); if (client == null) { var contragent = new Contragent { LastName = LastName, FirstName = FirstName }; client = new Client { Contragent = contragent }; _mainViewModel.Context.Clients.Add(client); } Transaction transaction = new Transaction { Product = product, Date = DateTime.Now, TypeID = (int) TranType.ToRepair, OperatorID = OperatorManager.Instance.CurrentUserID, Contragent = client.Contragent, Price = 0 }; _mainViewModel.Context.Transactions.Add(transaction); try { _mainViewModel.Context.SaveChanges(); } catch (Exception e) { MessageBox.Show(("Не удалось добавить новый товар"), "Error", MessageBoxButton.OK, MessageBoxImage.Error); Logging.WriteToLog("Failed add new bought transaction. " + e.Message); } _mainViewModel.ToRepairProductsViewModel.Update(); _mainViewModel.ToRepairProductsViewModel.AddDialogViewModel = null; }
private void EditProduct() { ModelName = Trim(ModelName); SerialNumber = Trim(SerialNumber); Notes = Trim(Notes); Model model = new Model(); if (Models.Contains(ModelName)) model = GetModelByName(ModelName); if (!Models.Contains(ModelName) || model == null) { model = new Model {Name = ModelName, CategoryID = SelectedCategory.ID}; _context.Models.Add(model); } try { var productRows = _context.Products.Where(x => _selectedProductIDs.Contains(x.ID)).ToList(); foreach (var productRow in productRows) { productRow.Notes = Notes; productRow.Model = model; productRow.SerialNumber = SerialNumber; productRow.SellingPrice = SellingPrice; productRow.DateSellTo = DateSellTo; } _context.SaveChanges(); } catch (Exception e) { MessageBox.Show(("Не удалось изменить данные товара"), "Error", MessageBoxButton.OK, MessageBoxImage.Error); Logging.WriteToLog("Failed edit product. " + e.InnerException.Message); } _productsViewModel.Update(); _productsViewModel.EditProductDialogViewModel = null; }
private void AddProduct() { ModelName = Trim(ModelName); SerialNumber = Trim(SerialNumber); Notes = Trim(Notes); List<Product> products = new List<Product>(); for (int i = 0; i < Number; i++) products.Add(new Product { SerialNumber = SerialNumber, Notes = Notes, SellingPrice = SellingPrice }); if (Models.Contains(ModelName)) foreach (var product in products) { product.Model = GetModelIDByName(ModelName); } if (!Models.Contains(ModelName) || products.FirstOrDefault().Model == null) { Model model = new Model {Name = ModelName, CategoryID = SelectedCategory.ID}; _mainViewModel.Context.Models.Add(model); foreach (var product in products) { product.Model = model; } } _mainViewModel.Context.Products.AddRange(products); List<Transaction> transactions = new List<Transaction>(); foreach (var product in products) { transactions.Add(new Transaction { Product = product, Date = DateTime.Now, TypeID = (int)TranType.Bought, OperatorID = OperatorManager.Instance.CurrentUserID, Price = Price }); } _mainViewModel.Context.Transactions.AddRange(transactions); try { _mainViewModel.Context.SaveChanges(); _mainViewModel.CashInHand -= Price * products.Count; _mainViewModel.ProductsValue += Price * products.Count; } catch (Exception e) { MessageBox.Show(("Не удалось добавить новый товар"), "Error", MessageBoxButton.OK, MessageBoxImage.Error); Logging.WriteToLog("Failed add new bought transaction. " + e.Message); } _mainViewModel.OnStockProductsViewModel.Update(); _mainViewModel.OnStockProductsViewModel.AddDialogViewModel = null; }