EditProductWindow.xaml.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 IEnumerable<ProductMaterial> productMaterialList
  26. {
  27. get
  28. {
  29. return Globals.dataProvider.getProductMaterials(currentProduct.ID);
  30. }
  31. }
  32. public EditProductWindow(Product editProduct)
  33. {
  34. InitializeComponent();
  35. DataContext = this;
  36. currentProduct = editProduct;
  37. root.Title = currentProduct.ID == 0 ? "Новый продукт" : "Редактирование продукта";
  38. productTypeList = Globals.dataProvider.getProductTypes().ToList();
  39. if (currentProduct.ID > 0)
  40. {
  41. selectedProductIndex = productTypeList.FindIndex(pt => pt.ID == currentProduct.ProductTypeID);
  42. }
  43. }
  44. public event PropertyChangedEventHandler PropertyChanged;
  45. private void Invalidate(string element)
  46. {
  47. if (PropertyChanged != null)
  48. PropertyChanged(this, new PropertyChangedEventArgs($"{element}"));
  49. }
  50. private void ChangeImageButton_Click(object sender, RoutedEventArgs e)
  51. {
  52. OpenFileDialog GetImageDialog = new OpenFileDialog();
  53. GetImageDialog.Filter = "Файлы изображений: (*.png, *.jpg)|*.png;*.jpg";
  54. GetImageDialog.InitialDirectory = Environment.CurrentDirectory;
  55. if (GetImageDialog.ShowDialog() == true)
  56. {
  57. currentProduct.Image = GetImageDialog.FileName.Substring(Environment.CurrentDirectory.Length);
  58. Invalidate(currentProduct.Image);
  59. }
  60. }
  61. private void SaveButton_Click(object sender, RoutedEventArgs e)
  62. {
  63. try
  64. {
  65. if (ProductTypeComboBox.SelectedIndex != null)
  66. {
  67. currentProduct.ProductTypeID = ((ProductType)ProductTypeComboBox.SelectedItem).ID;
  68. }
  69. else
  70. {
  71. throw new Exception("Не выбран тип продукта");
  72. }
  73. decimal minCost = decimal.Parse(MinCostForAgentTextBox.Text, CultureInfo.InvariantCulture);
  74. int decIndex = minCost.ToString().IndexOf('.');
  75. if (minCost > 0 && (decIndex == -1 || minCost.ToString().Length - decIndex <= 2))
  76. {
  77. currentProduct.MinCostForAgent = minCost;
  78. }
  79. else
  80. {
  81. throw new Exception("Цена должна быть больше 0 с двумя знаками после точки");
  82. }
  83. string newArticleNumber = ArticleNumberTextBox.Text;
  84. if (Globals.dataProvider.getArticleCheck(newArticleNumber, currentProduct.ID) == 0)
  85. {
  86. currentProduct.ArticleNumber = newArticleNumber;
  87. }
  88. else
  89. {
  90. throw new Exception("Данный артикул уже используется");
  91. }
  92. currentProduct.Title = TitleTextBox.Text;
  93. currentProduct.ProductionPersonCount = Convert.ToInt32(ProductionPersonCountTextBox.Text);
  94. currentProduct.ProductionWorkshopNumber = Convert.ToInt32(ProductionWorkshopNumberTextBox.Text);
  95. currentProduct.Description = DescriptionTextBox.Text;
  96. Globals.dataProvider.saveProduct(currentProduct);
  97. DialogResult = true;
  98. }
  99. catch (FormatException)
  100. {
  101. MessageBox.Show("Используйте только цифры");
  102. }
  103. catch (Exception ex)
  104. {
  105. MessageBox.Show(ex.Message);
  106. }
  107. }
  108. private void deleteButton_Click(object sender, RoutedEventArgs e)
  109. {
  110. try
  111. {
  112. var saleCount = Globals.dataProvider.saleCount(currentProduct.ID);
  113. if (saleCount > 0)
  114. throw new Exception("Нельзя удалять продукт с продажами");
  115. Globals.dataProvider.removeProductMaterial(currentProduct.ID);
  116. Globals.dataProvider.removeProductCostHistory(currentProduct.ID);
  117. Globals.dataProvider.removeProduct(currentProduct.ID);
  118. DialogResult = true;
  119. MessageBox.Show("Продукт удален");
  120. }
  121. catch (Exception ex)
  122. {
  123. MessageBox.Show(ex.Message);
  124. }
  125. }
  126. private void DeleteMaterialTextBox_MouseDown(object sender, MouseButtonEventArgs e)
  127. {
  128. var productMaterial = (sender as TextBlock).Tag as ProductMaterial;
  129. Globals.dataProvider.deleteProductMaterial(productMaterial.MaterialId, productMaterial.ProductId);
  130. Invalidate("productMaterialList");
  131. }
  132. private void AddMaterialButton_Click(object sender, RoutedEventArgs e)
  133. {
  134. var EditWindow = new EditingMaterialWindow(currentProduct.ID);
  135. if ((bool)EditWindow.ShowDialog())
  136. {
  137. Invalidate("productMaterialList");
  138. }
  139. }
  140. }
  141. }