123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- using System;
- using System.Collections.Generic;
- using System.Data.Entity.Migrations;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using WriteErase.ClassBase;
- using WriteErase.ClassData;
- namespace WriteErase.ClassPages
- {
- /// <summary>
- /// Логика взаимодействия для PageEditProduct.xaml
- /// </summary>
- public partial class PageEditProduct : Page
- {
- Product product = new Product();
- bool edit = false;
- public PageEditProduct()
- {
- InitializeComponent();
- init();
- }
- public PageEditProduct(string article)
- {
- InitializeComponent();
- product = BaseConnection.kotBase.Product.First(tb => tb.ProductArticleNumber == article);
- edit = true;
- init();
- }
- private void init()
- {
- cbCategory.ItemsSource = BaseConnection.kotBase.ProductCategory.Select(tb => tb.CategoryName).ToList();
- cbManufactured.ItemsSource = BaseConnection.kotBase.ProductManufacturer.Select(tb => tb.ManufacturerName).ToList();
- cbSuplier.ItemsSource = BaseConnection.kotBase.ProductSuplier.Select(tb => tb.SuplierName).ToList();
- if (edit)
- {
- tbkHeader.Text = "Редактирование товара";
- tbxArticle.Text = product.ProductArticleNumber;
- tbxName.Text = product.ProductName;
- btnGenerate.IsEnabled = false;
- tbxDescription.Text = product.ProductDescription;
- tbxCost.Text = product.ProductCost.ToString();
- tbxDiscount.Text = product.ProductDiscountMax.ToString();
- tbxCount.Text = product.ProductQuantityInStock.ToString();
- cbCategory.SelectedItem = product.ProductCategory.CategoryName;
- cbManufactured.SelectedItem = product.ProductManufacturer.ManufacturerName;
- cbSuplier.SelectedItem = product.ProductSuplier.SuplierName;
- }
- else
- {
- tbkHeader.Text = "Добавление товара";
- GenerateClick(null, null);
- }
- }
-
- private void btnBackClick(object sender, RoutedEventArgs e)
- {
- switch (MessageBox.Show("При выходе ввыеденные вами данные не сохраняться, Вы действительно хотите выйти?", "Выход из приложения", MessageBoxButton.YesNo, MessageBoxImage.Question))
- {
- case MessageBoxResult.Yes:
- {
- MainFrame.mainFrame.Navigate(new PageMain());
- break;
- }
- case MessageBoxResult.No:
- {
- break;
- }
- }
- }
- private void btnSaveClick(object sender, RoutedEventArgs e)
- {
- string mess = check();
- if(mess == "")
- {
- try
- {
- if (!edit)
- {
- product.ProductArticleNumber = tbxArticle.Text;
- product.ProductStatus = "1";
- product.ProductPhoto = null;
- }
-
- product.ProductName = tbxName.Text;
- product.ProductDescription = tbxDescription.Text;
- product.ProductCost = Convert.ToDecimal(tbxCost.Text);
- product.ProductDiscountMax = Convert.ToDouble(tbxDiscount.Text);
- product.ProductQuantityInStock = int.Parse(tbxCount.Text);
- product.CategoryId = BaseConnection.kotBase.ProductCategory.
- First(tb => tb.CategoryName == cbCategory.SelectedValue.ToString()).CategoryId;
- product.ManufacturerId = BaseConnection.kotBase.ProductManufacturer.
- First(tb => tb.ManufacturerName == cbManufactured.SelectedValue.ToString()).ManufacturerID;
- product.SuplierId = BaseConnection.kotBase.ProductSuplier.
- First(tb => tb.SuplierName == cbSuplier.SelectedValue.ToString()).SuplierId;
- if(edit)
- BaseConnection.kotBase.Product.AddOrUpdate(product);
- else
- BaseConnection.kotBase.Product.Add(product);
- BaseConnection.kotBase.SaveChanges();
- MessageBox.Show("Изменения сохранены");
- MainFrame.mainFrame.Navigate(new PageMain());
- }
- catch(Exception ex)
- {
- Debug.WriteLine(ex.Message);
- }
- }
- else
- {
- MessageBox.Show(mess, "Ошибки заполнения формы");
- }
- }
- private string check()
- {
- string mess = "";
- Regex number = new Regex("^\\d{1,}$");
- Regex cost = new Regex("^\\d{1,}([\\.\\,]\\d{1,})?$");
- if (string.IsNullOrWhiteSpace(tbxName.Text))
- mess += "Поле Наименование не может быть пустым\n";
- if (string.IsNullOrWhiteSpace(tbxDescription.Text))
- mess += "Поле Описание не может быть пустым\n";
- if (!cost.IsMatch(tbxCost.Text.Trim()) || string.IsNullOrWhiteSpace(tbxCost.Text))
- mess += "Поле Цена должно бать числом\n";
- if (!number.IsMatch(tbxDiscount.Text.Trim()) || string.IsNullOrWhiteSpace(tbxDiscount.Text))
- mess += "Поле Скидка должно бать числом\n";
- if (!number.IsMatch(tbxCount.Text.Trim()) || string.IsNullOrWhiteSpace(tbxCount.Text))
- mess += "Поле Количество должно бать числом\n";
-
- if (cbCategory.SelectedIndex < 0)
- mess += "Неоходимо выбрать Категорию\n";
- if (cbManufactured.SelectedIndex < 0)
- mess += "Неоходимо выбрать Производителя\n";
- if (cbSuplier.SelectedIndex < 0)
- mess += "Неоходимо выбрать Постовщика";
- return mess;
- }
- private void GenerateClick(object sender, RoutedEventArgs e)
- {
- //A297U6
- string article = "";
- Random rnd = new Random();
- while (true)
- {
- article = ((char)rnd.Next(65, 81)).ToString() +
- rnd.Next(0, 10) + rnd.Next(0, 10) + rnd.Next(0, 10) +
- ((char)rnd.Next(65, 81)).ToString() + rnd.Next(0, 10);
- if (BaseConnection.kotBase.Product.Where(tb => tb.ProductArticleNumber == article).Count() == 0)
- break;
- article = "";
- }
- tbxArticle.Text = article;
- }
- }
- }
|