Beispiel #1
0
 private void purchaseButtonClick(object sender, EventArgs e)
 {
     using (var db = new Model.BudgetModel())
     {
         var purchase = new Purchase();
         purchase.DateTime = purchaseDateTimePicker.Value;
         var selectedSource = purchaseComboBox.SelectedItem as ListBoxItem;
         if (selectedSource == null)
         {
             MessageBox.Show("Выберите источник");
             return;
         }
         purchase.Source = db.Sources.Find(selectedSource.Id);
         db.Purchases.Add(purchase);
         db.SaveChanges();
         IdPurchase = purchase.Id;
         ChekItemForm chekItemForm = new ChekItemForm(IdPurchase);
         if (chekItemForm.ShowDialog(this) == DialogResult.OK)
             UpdateDataGrid();
         else
         {
             db.Purchases.Remove(purchase);
             db.SaveChanges();
             UpdateDataGrid();
         }
     }
 }
Beispiel #2
0
 private void RenameToolStripMenuItemClick(object sender, EventArgs e)
 {
     var selectedItem = unitOfMeasureListBox.SelectedItem as ComplexListBoxItem;
     if (selectedItem == null) return;
     using (var db = new Model.BudgetModel())
     {
         var renameItem = db.UnitOfMeasures.Find(selectedItem.Id);
         AddItemForm addItemForm = new AddItemForm(selectedItem.Name);
         if (addItemForm.ShowDialog(this) == DialogResult.OK)
             if (string.IsNullOrWhiteSpace(addItemForm.Value))
             {
                 MessageBox.Show("Введите название");
                 return;
             }
             else
             {
                 renameItem.Name = addItemForm.Value;
                 addItemForm = new AddItemForm(selectedItem.ShortName);
                 if (addItemForm.ShowDialog(this) == DialogResult.OK)
                     if (string.IsNullOrWhiteSpace(addItemForm.Value))
                     {
                         MessageBox.Show("Введите название");
                         return;
                     }
                 renameItem.ShortName = addItemForm.Value;
                 db.SaveChanges();
             }
     }
     UpdateListBox();
 }
Beispiel #3
0
 private void UpdateDataGrid()
 {
     purchaseDataGrid.ColumnWidth = DataGridLength.SizeToHeader;
     using (var db = new Model.BudgetModel())
     {
         var purchases = db.Purchases.ToList();
         foreach (var purchase in purchases)
         {
             if (purchase.ChekItems.ToList().Count == 0) db.Purchases.Remove(purchase);
         }
         db.SaveChanges();
         purchaseDataGrid.ItemsSource = db.Purchases.Include("Source").ToList();
     }
 }
Beispiel #4
0
 private void DeleteToolStripMenuItemClick(object sender, EventArgs e)
 {
     var res = MessageBox.Show("Удалить?", "Удаление элемента", MessageBoxButtons.YesNo);
     if (res == DialogResult.No) return;
     else
     {
         var selectedItem = sourcesListBox.SelectedItem as ListBoxItem;
         if (selectedItem == null) return;
         using (var db = new Model.BudgetModel())
         {
             var removeItem = db.Sources.Find(selectedItem.Id);
             db.Sources.Remove(removeItem);
             db.SaveChanges();
         }
     }
     UpdateListBox();
 }
Beispiel #5
0
 private void RemoveMenuItemClick(object sender, RoutedEventArgs e)
 {
     var res = MessageBox.Show("Удалить?", "Удаление", MessageBoxButton.YesNo);
     if (res == MessageBoxResult.Yes)
     {
         using (var db = new Model.BudgetModel())
         {
             var selectedItem = sourceLstBox.SelectedItem as ListBoxItem;
             if (selectedItem == null) return;
             var removeItem = db.Sources.Find(selectedItem.Id);
             db.Sources.Remove(removeItem);
             db.SaveChanges();
         }
         UpdateListBox();
     }
     else return;
 }
Beispiel #6
0
 private void AddButtonClick(object sender, RoutedEventArgs e)
 {
     AddItemsWindow addItemsWindow = new AddItemsWindow();
     addItemsWindow.ShowDialog();
     using (var db = new Model.BudgetModel())
     {
         var source = new Source();
         if (string.IsNullOrWhiteSpace(addItemsWindow.Value)) MessageBox.Show("Введите название");
         else
         {
             source.Name = addItemsWindow.Value;
             db.Sources.Add(source);
             db.SaveChanges();
             UpdateListBox();
         }
     }
 }
Beispiel #7
0
 private void RenameMenuItemClick(object sender, RoutedEventArgs e)
 {
     using (var db = new Model.BudgetModel())
     {
         var selectedItem = sourceLstBox.SelectedItem as ListBoxItem;
         if (selectedItem == null) return;
         AddItemsWindow addItemsWindow = new AddItemsWindow(selectedItem.Name);
         addItemsWindow.ShowDialog();
         var renameItem = db.Sources.Find(selectedItem.Id);
         if (string.IsNullOrWhiteSpace(addItemsWindow.Value)) MessageBox.Show("Введите название");
         else
         {
             renameItem.Name = addItemsWindow.Value;
             db.SaveChanges();
             UpdateListBox();
         }
     }
 }
Beispiel #8
0
 private void categoryAddButtonClick(object sender, EventArgs e)
 {
     AddItemForm addItemForm = new AddItemForm();
     if(addItemForm.ShowDialog(this) == DialogResult.OK)
     {
         using (var db = new Model.BudgetModel())
         {
             var category = new Category();
             if (string.IsNullOrWhiteSpace(addItemForm.Value)) MessageBox.Show("Введите название");
             else
             {
                 category.Name = addItemForm.Value;
                 db.Categories.Add(category);
                 db.SaveChanges();
             }
         }
         UpdateListBox();
     }
 }
Beispiel #9
0
 private void AddButtonClick(object sender, RoutedEventArgs e)
 {
     using (var db = new Model.BudgetModel())
     {
         var purchase = new Purchase();
         purchase.DateTime = purchaseDataTime.SelectedDate.Value;
         var selectedSource = purchaseCmbBox.SelectedItem as ListBoxItem;
         if (selectedSource == null)
         {
             MessageBox.Show("Выберите источник");
             return;
         }
         purchase.Source = db.Sources.Find(selectedSource.Id);
         db.Purchases.Add(purchase);
         db.SaveChanges();
         ChekItemWindow chekItemWindow = new ChekItemWindow(purchase.Id);
         chekItemWindow.ShowDialog();
     }
     UpdateDataGrid();
 }
Beispiel #10
0
 private void goodsItemAddButtonClick(object sender, EventArgs e)
 {
     using (var db = new Model.BudgetModel())
     {
         var selectedCategory = goodsItemComboBox.SelectedItem as ListBoxItem;
         AddGoodsItemForm addGoodsItemForm = new AddGoodsItemForm();
         if (selectedCategory != null) addGoodsItemForm = new AddGoodsItemForm(selectedCategory);
         addGoodsItemForm.ShowDialog(this);
         var goods = new GoodsItem();
         if (string.IsNullOrWhiteSpace(addGoodsItemForm.Value)) return;
         else
         {
             goods.Name = addGoodsItemForm.Value;
             goods.Category = db.Categories.Find(addGoodsItemForm.SelectedCategory.Id);
             goods.UnitOfMeasure = db.UnitOfMeasures.Find(addGoodsItemForm.SelectedUnitOfMeasure.Id);
             db.Goods.Add(goods);
             db.SaveChanges();
         }
     }
     UpdateListBox();
 }
Beispiel #11
0
        private void sourceAddButtonClick(object sender, EventArgs e)
        {
            AddItemForm addItemForm = new AddItemForm();
            if(addItemForm.ShowDialog(this) == DialogResult.OK)
            {
                using (var db = new Model.BudgetModel())
                {
                    var source = new Source();
                    if (string.IsNullOrWhiteSpace(addItemForm.Value)) MessageBox.Show("Введите название");
                    else
                    {
                        source.Name = addItemForm.Value;
                        db.Sources.Add(source);
                        try
                        {
                            db.SaveChanges();
                        }
                        catch (DbUpdateException ex)
                        {
                            var sqlErrors = ex?.InnerException?.InnerException as SqlException;
                            // 2601 - cannot insert duplicate key.
                            if (sqlErrors.Number == 2601)
                            {
                                MessageBox.Show("Такое название уже существует.");
                            }
                            else
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }

                    }
                }
                UpdateListBox();
            }
        }
Beispiel #12
0
 private void RemoveButtonClick(object sender, RoutedEventArgs e)
 {
     var res = MessageBox.Show("Удалить?", "Удаление", MessageBoxButton.YesNo);
     if (res == MessageBoxResult.No) return;
     using (var db = new Model.BudgetModel())
     {
         Purchase purchase = purchaseDataGrid.SelectedItem as Purchase;
         db.Purchases.Remove(db.Purchases.Find(purchase.Id));
         db.SaveChanges();
         UpdateDataGrid();
     }
 }
Beispiel #13
0
 private void unitOfMeasureAddButtonClick(object sender, EventArgs e)
 {
     using (var db = new Model.BudgetModel())
     {
         var unitOfMeasure = new UnitOfMeasure();
         AddItemForm addItemForm = new AddItemForm("Введите полное название");
         if (addItemForm.ShowDialog(this) == DialogResult.OK)
             if (string.IsNullOrWhiteSpace(addItemForm.Value)
                 || addItemForm.Value == "Введите полное название")
             {
                 MessageBox.Show("Введите название");
                 return;
             }
             else
             {
                 unitOfMeasure.Name = addItemForm.Value;
                 addItemForm = new AddItemForm("Введите сокращенное название");
                 if (addItemForm.ShowDialog(this) == DialogResult.OK)
                     if (string.IsNullOrWhiteSpace(addItemForm.Value)
                         || addItemForm.Value == "Введите сокращенное название")
                     {
                         MessageBox.Show("Введите название");
                         return;
                     }
                     else
                     {
                         unitOfMeasure.ShortName = addItemForm.Value;
                         db.UnitOfMeasures.Add(unitOfMeasure);
                         db.SaveChanges();
                     }
             }
         UpdateListBox();
     }
 }
Beispiel #14
0
 private void SaveButClick(object sender, RoutedEventArgs e)
 {
     using (var db = new Model.BudgetModel())
     {
         Purchase purchase = purchaseDataGrid.SelectedItem as Purchase;
         var selectedPurchase = db.Purchases.Find(purchase.Id);
         selectedPurchase.DateTime = purchaseDataTime.SelectedDate.Value;
         var selectedSource = purchaseCmbBox.SelectedItem as ListBoxItem;
         selectedPurchase.Source = db.Sources.Find(selectedSource.Id);
         db.SaveChanges();
         UpdateDataGrid();
         saveBut.IsEnabled = false;
         addButton.IsEnabled = true;
         purchaseDataGrid.IsEnabled = true;
     }
 }
Beispiel #15
0
 private void RenameMenuItemClick(object sender, RoutedEventArgs e)
 {
     var selectedItem = goodsLstBox.SelectedValue as ListBoxItem;
     var selectedCategory = goodsComboBox.SelectedValue as ListBoxItem;
     if (selectedItem == null) return;
     using (var db = new Model.BudgetModel())
     {
         var goodsItem = db.Goods.Find(selectedItem.Id);
         AddGoodsWindow addGoodsItem = new AddGoodsWindow(
             goodsItem.Category.Id,
             goodsItem.UnitOfMeasure.Id,
             goodsItem.Name
             );
         addGoodsItem.ShowDialog();
         if (addGoodsItem.Category == null || addGoodsItem.UnitOfMeasure == null
             || string.IsNullOrWhiteSpace(addGoodsItem.Value))
             return;
         goodsItem.Name = addGoodsItem.Value;
         goodsItem.Category = db.Categories.Find(addGoodsItem.Category.Id);
         goodsItem.UnitOfMeasure = db.UnitOfMeasures.Find(addGoodsItem.UnitOfMeasure.Id);
         db.SaveChanges();
         UpdateListBox();
     }
 }
Beispiel #16
0
 private void AddButtonClick(object sender, RoutedEventArgs e)
 {
     var selectedItem = goodsComboBox.SelectedValue as ListBoxItem;
     AddGoodsWindow addGoodsWindow = new AddGoodsWindow();
     if (selectedItem != null) addGoodsWindow = new AddGoodsWindow(selectedItem.Id);
     addGoodsWindow.ShowDialog();
     if (string.IsNullOrWhiteSpace(addGoodsWindow.Value)) return;
     using (var db = new Model.BudgetModel())
     {
         var goodsItem = new GoodsItem();
         goodsItem.Name = addGoodsWindow.Value;
         goodsItem.Category = db.Categories.Find(addGoodsWindow.Category.Id);
         goodsItem.UnitOfMeasure = db.UnitOfMeasures.Find(addGoodsWindow.UnitOfMeasure.Id);
         db.Goods.Add(goodsItem);
         db.SaveChanges();
     }
     UpdateListBox();
 }
Beispiel #17
0
 private void RenameToolStripMenuItemClick(object sender, EventArgs e)
 {
     using (var db = new Model.BudgetModel())
     {
         var selectedItem = goodsItemListBox.SelectedItem as ListBoxItem;
         if (selectedItem == null) return;
         var selectedCategory = goodsItemComboBox.SelectedItem as ListBoxItem;
         var selectedGoodsItem = db.Goods.Find(selectedItem.Id);
         var selectedUnitOfMeasure = db.UnitOfMeasures.Find(selectedGoodsItem.UnitOfMeasure.Id);
         AddGoodsItemForm addGoodsForm = new AddGoodsItemForm(
             selectedCategory,
             selectedUnitOfMeasure,
             selectedGoodsItem.Name
             );
         addGoodsForm.ShowDialog(this);
         if (string.IsNullOrWhiteSpace(addGoodsForm.Value)) return;
         else
         {
             selectedGoodsItem.Name = addGoodsForm.Value;
             selectedGoodsItem.Category = db.Categories.Find(addGoodsForm.SelectedCategory.Id);
             selectedGoodsItem.UnitOfMeasure = db.UnitOfMeasures.Find(addGoodsForm.SelectedUnitOfMeasure.Id);
             db.SaveChanges();
             UpdateListBox();
         }
     }
 }
Beispiel #18
0
 private void purchaseDataGridViewCellContentClick(object sender, DataGridViewCellEventArgs e)
 {
     if (e.RowIndex == -1) return;
     if (e.ColumnIndex == 3)
     {
         IdPurchase = (int)(purchaseDataGridView[0, e.RowIndex].Value);
         ChekItemForm chekItemForm = new ChekItemForm(IdPurchase);
         chekItemForm.ShowDialog(this);
     }
     else if(e.ColumnIndex == 4)
     {
         var res = MessageBox.Show("Удалить?", "Удаление элемента", MessageBoxButtons.YesNo);
         if (res == DialogResult.No) return;
         else
         {
             IdPurchase = (int)(purchaseDataGridView[0, e.RowIndex].Value);
             using (var db = new Model.BudgetModel())
             {
                 var removePurchase = db.Purchases.Find(IdPurchase);
                 var removeChekItems = db.Purchases.Find(IdPurchase).ChekItems.ToList();
                 foreach (var removeChekItem in removeChekItems)
                     db.ChekItems.Remove(removeChekItem);
                 db.Purchases.Remove(removePurchase);
                 db.SaveChanges();
             }
         }
     }
     UpdateDataGrid();
 }
Beispiel #19
0
 private void UpdateDataGrid()
 {
     purchaseDataGridView.Rows.Clear();
     using (var db = new Model.BudgetModel())
     {
         var purchases = db.Purchases.ToList();
         var renameButton = new DataGridViewButtonCell();
         var removeButton = new DataGridViewButtonCell();
         foreach (var purchase in purchases)
         {
             var chekItems = db.Purchases.Find(purchase.Id).ChekItems.ToList();
             if (chekItems.Count == 0)
             {
                 db.Purchases.Remove(purchase);
                 db.SaveChanges();
             }
             else
                 purchaseDataGridView.Rows.Add(
                     purchase.Id,
                     purchase.DateTime,
                     purchase.Source.Name,
                     renameButton,
                     removeButton
                 );
         }
     }
 }