EditProductWindow.xaml.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Shapes;
  17. using wpf_connection3.model;
  18. namespace wpf_connection3.Windows
  19. {
  20. public partial class EditProductWindow : Window, INotifyPropertyChanged
  21. {
  22. public int selectedProductIndex { get; set; } = -1;
  23. public List<ProductType> productTypeList { get; set; }
  24. public Product currentProduct { get; set; }
  25. public EditProductWindow(Product editProduct)
  26. {
  27. InitializeComponent();
  28. DataContext = this;
  29. currentProduct = editProduct;
  30. root.Title = currentProduct.ID == 0 ? "Новый продукт" : "Редактирование продукта";
  31. productTypeList = Globals.dataProvider.getProductTypes().ToList();
  32. if (currentProduct.ID > 0)
  33. {
  34. selectedProductIndex = productTypeList.FindIndex(pt => pt.ID == currentProduct.ProductTypeID);
  35. }
  36. }
  37. public event PropertyChangedEventHandler PropertyChanged;
  38. private void Invalidate(string element)
  39. {
  40. if (PropertyChanged != null)
  41. PropertyChanged(this, new PropertyChangedEventArgs($"{element}"));
  42. }
  43. private void ChangeImageButton_Click(object sender, RoutedEventArgs e)
  44. {
  45. OpenFileDialog GetImageDialog = new OpenFileDialog();
  46. GetImageDialog.Filter = "Файлы изображений: (*.png, *.jpg)|*.png;*.jpg";
  47. GetImageDialog.InitialDirectory = Environment.CurrentDirectory;
  48. if (GetImageDialog.ShowDialog() == true)
  49. {
  50. currentProduct.Image = GetImageDialog.FileName.Substring(Environment.CurrentDirectory.Length);
  51. Invalidate(currentProduct.Image);
  52. }
  53. }
  54. private void SaveButton_Click(object sender, RoutedEventArgs e)
  55. {
  56. try
  57. {
  58. if (ProductTypeComboBox.SelectedIndex != null)
  59. {
  60. currentProduct.ProductTypeID = ((ProductType)ProductTypeComboBox.SelectedItem).ID;
  61. }
  62. else
  63. {
  64. throw new Exception("Не выбран тип продукта");
  65. }
  66. decimal minCost = decimal.Parse(MinCostForAgentTextBox.Text, CultureInfo.InvariantCulture);
  67. int decIndex = minCost.ToString().IndexOf('.');
  68. if (minCost > 0 && (decIndex == -1 || minCost.ToString().Length - decIndex <= 2))
  69. {
  70. currentProduct.MinCostForAgent = minCost;
  71. }
  72. else
  73. {
  74. throw new Exception("Цена должна быть больше 0 с двумя знаками после точки");
  75. }
  76. string newArticleNumber = ArticleNumberTextBox.Text;
  77. if (Globals.dataProvider.getArticleCheck(newArticleNumber, currentProduct.ID) == 0)
  78. {
  79. currentProduct.ArticleNumber = newArticleNumber;
  80. }
  81. else
  82. {
  83. throw new Exception("Данный артикул уже используется");
  84. }
  85. currentProduct.Title = TitleTextBox.Text;
  86. currentProduct.ProductionPersonCount = Convert.ToInt32(ProductionPersonCountTextBox.Text);
  87. currentProduct.ProductionWorkshopNumber = Convert.ToInt32(ProductionWorkshopNumberTextBox.Text);
  88. currentProduct.Description = DescriptionTextBox.Text;
  89. Globals.dataProvider.saveProduct(currentProduct);
  90. DialogResult = true;
  91. }
  92. catch (FormatException)
  93. {
  94. MessageBox.Show("Используйте только цифры");
  95. }
  96. catch (Exception ex)
  97. {
  98. MessageBox.Show(ex.Message);
  99. }
  100. }
  101. private void deleteButton_Click(object sender, RoutedEventArgs e)
  102. {
  103. try
  104. {
  105. var saleCount = Globals.dataProvider.saleCount(currentProduct.ID);
  106. if (saleCount > 0)
  107. throw new Exception("Нельзя удалять продукт с продажами");
  108. Globals.dataProvider.removeProductMaterial(currentProduct.ID);
  109. Globals.dataProvider.removeProductCostHistory(currentProduct.ID);
  110. Globals.dataProvider.removeProduct(currentProduct.ID);
  111. DialogResult = true;
  112. MessageBox.Show("Продукт удален");
  113. }
  114. catch (Exception ex)
  115. {
  116. MessageBox.Show(ex.Message);
  117. }
  118. }
  119. }
  120. }