WorkerWindow.xaml.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Shapes;
  14. using System.Data.SqlClient;
  15. using System.Data;
  16. using System.Windows.Threading;
  17. namespace SkladProject
  18. {
  19. /// <summary>
  20. /// Логика взаимодействия для WorkerWindow.xaml
  21. /// </summary>
  22. public partial class WorkerWindow : Window
  23. {
  24. public WorkerWindow()
  25. {
  26. InitializeComponent();
  27. gridprivet.Visibility = Visibility.Visible;
  28. DispatcherTimer timer = new DispatcherTimer();
  29. timer.Tick += new EventHandler(Update_Timer_Tick);
  30. timer.Interval = new TimeSpan(0, 0, 1);
  31. timer.Start();
  32. dateproduct.DisplayDateStart = DateTime.Now;
  33. }
  34. SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=praktika;Integrated Security=True");
  35. private void Update_Timer_Tick(object sender, EventArgs e)
  36. {
  37. timetxt.Text = DateTime.Now.ToString();
  38. }
  39. private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
  40. {
  41. DragMove();
  42. }
  43. private void Back(object sender, RoutedEventArgs e)
  44. {
  45. MainWindow mainWindow = new MainWindow();
  46. mainWindow.Show();
  47. this.Close();
  48. }
  49. private void WinMin_Click(object sender, RoutedEventArgs e)
  50. {
  51. this.WindowState = WindowState.Minimized;
  52. }
  53. private void Exit_Click(object sender, RoutedEventArgs e)
  54. {
  55. Application.Current.Shutdown();
  56. }
  57. private void Window_Loaded(object sender, RoutedEventArgs e)
  58. {
  59. fillcomboproduct();
  60. filllichcab();
  61. showdataproduct();
  62. showdocument();
  63. showtraffic();
  64. fillvsego();
  65. }
  66. private void UserInform(object sender, RoutedEventArgs e)
  67. {
  68. if (gridlich.Visibility == Visibility.Visible)
  69. {
  70. gridlich.Visibility = Visibility.Hidden;
  71. gridprivet.Visibility = Visibility.Visible;
  72. }
  73. else
  74. {
  75. gridlich.Visibility = Visibility.Visible;
  76. gridproduct.Visibility = Visibility.Hidden;
  77. gridprivet.Visibility = Visibility.Hidden;
  78. gridskladinfo.Visibility = Visibility.Hidden;
  79. griddocument.Visibility = Visibility.Hidden;
  80. gridtraffic.Visibility = Visibility.Hidden;
  81. }
  82. }
  83. private void Production(object sender, RoutedEventArgs e)
  84. {
  85. if (gridproduct.Visibility == Visibility.Visible)
  86. {
  87. gridproduct.Visibility = Visibility.Hidden;
  88. gridprivet.Visibility = Visibility.Visible;
  89. }
  90. else
  91. {
  92. gridproduct.Visibility = Visibility.Visible;
  93. gridlich.Visibility = Visibility.Hidden;
  94. gridprivet.Visibility = Visibility.Hidden;
  95. gridskladinfo.Visibility = Visibility.Hidden;
  96. griddocument.Visibility = Visibility.Hidden;
  97. gridtraffic.Visibility = Visibility.Hidden;
  98. }
  99. }
  100. private void Document(object sender, RoutedEventArgs e)
  101. {
  102. if (griddocument.Visibility == Visibility.Visible)
  103. {
  104. griddocument.Visibility = Visibility.Hidden;
  105. gridprivet.Visibility = Visibility.Visible;
  106. }
  107. else
  108. {
  109. griddocument.Visibility = Visibility.Visible;
  110. gridproduct.Visibility = Visibility.Hidden;
  111. gridlich.Visibility = Visibility.Hidden;
  112. gridprivet.Visibility = Visibility.Hidden;
  113. gridskladinfo.Visibility = Visibility.Hidden;
  114. gridtraffic.Visibility = Visibility.Hidden;
  115. }
  116. }
  117. private void Traffic(object sender, RoutedEventArgs e)
  118. {
  119. if (gridtraffic.Visibility == Visibility.Visible)
  120. {
  121. gridtraffic.Visibility = Visibility.Hidden;
  122. gridprivet.Visibility = Visibility.Visible;
  123. }
  124. else
  125. {
  126. gridtraffic.Visibility = Visibility.Visible;
  127. griddocument.Visibility = Visibility.Hidden;
  128. gridproduct.Visibility = Visibility.Hidden;
  129. gridlich.Visibility = Visibility.Hidden;
  130. gridprivet.Visibility = Visibility.Hidden;
  131. gridskladinfo.Visibility = Visibility.Hidden;
  132. }
  133. }
  134. private void SkladInfo(object sender, RoutedEventArgs e)
  135. {
  136. if (gridskladinfo.Visibility == Visibility.Visible)
  137. {
  138. gridskladinfo.Visibility = Visibility.Hidden;
  139. gridprivet.Visibility = Visibility.Visible;
  140. }
  141. else
  142. {
  143. gridskladinfo.Visibility = Visibility.Visible;
  144. gridproduct.Visibility = Visibility.Hidden;
  145. gridlich.Visibility = Visibility.Hidden;
  146. gridprivet.Visibility = Visibility.Hidden;
  147. griddocument.Visibility = Visibility.Hidden;
  148. gridtraffic.Visibility = Visibility.Hidden;
  149. }
  150. }
  151. void fillvsego()
  152. {
  153. try
  154. {
  155. con.Open();
  156. SqlCommand cmd = new SqlCommand("SELECT SUM(Quantity) as 'Sum' FROM Documents", con);
  157. cmd.CommandType = CommandType.Text;
  158. SqlDataAdapter adapter = new SqlDataAdapter();
  159. adapter.SelectCommand = cmd;
  160. DataSet dataSet = new DataSet();
  161. adapter.Fill(dataSet);
  162. if (dataSet.Tables[0].Rows.Count > 0)
  163. {
  164. vsegotxt.Text = "Количество продукции на складе: " + dataSet.Tables[0].Rows[0]["Sum"].ToString();
  165. }
  166. con.Close();
  167. }
  168. catch (Exception ex)
  169. {
  170. con.Close();
  171. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  172. }
  173. }
  174. void filllichcab()
  175. {
  176. try
  177. {
  178. con.Open();
  179. SqlCommand cmd = new SqlCommand("Select * from Users where ID_User ='" + idusertxt.Text + "'", con);
  180. cmd.CommandType = CommandType.Text;
  181. SqlDataAdapter adapter = new SqlDataAdapter();
  182. adapter.SelectCommand = cmd;
  183. DataSet dataSet = new DataSet();
  184. adapter.Fill(dataSet);
  185. if (dataSet.Tables[0].Rows.Count > 0)
  186. {
  187. firstnametxt.Text = "Имя: " + dataSet.Tables[0].Rows[0]["FirstName"].ToString();
  188. lastnametxt.Text = "Фамилия: " + dataSet.Tables[0].Rows[0]["LastName"].ToString();
  189. middlename.Text = "Отчество: " + dataSet.Tables[0].Rows[0]["MiddleName"].ToString();
  190. logintxt.Text = "Логин: " + dataSet.Tables[0].Rows[0]["Login"].ToString();
  191. }
  192. con.Close();
  193. }
  194. catch (Exception ex)
  195. {
  196. con.Close();
  197. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  198. }
  199. }
  200. void showdataproduct()
  201. {
  202. try
  203. {
  204. con.Open();
  205. string sql = "SELECT ID_Product,NameProduct,DateProduct From Production";
  206. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  207. DataTable data = new DataTable("Production");
  208. dataAdapter.Fill(data);
  209. dataproduct.ItemsSource = data.DefaultView;
  210. dataAdapter.Update(data);
  211. con.Close();
  212. dataproduct.Columns[0].Header = "ID";
  213. dataproduct.Columns[1].Header = "Название продукта";
  214. dataproduct.Columns[2].Header = "Срок хранения";
  215. dataproduct.Columns[0].Visibility = Visibility.Collapsed;
  216. (dataproduct.Columns[2] as DataGridTextColumn).Binding.StringFormat = "dd/MM/yyyy";
  217. }
  218. catch (Exception ex)
  219. {
  220. con.Close();
  221. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  222. }
  223. }
  224. private void dataproduct_SelectionChanged(object sender, SelectionChangedEventArgs e)
  225. {
  226. try
  227. {
  228. DataGrid gd = (DataGrid)sender;
  229. DataRowView rowView = gd.SelectedItem as DataRowView;
  230. if (rowView != null)
  231. {
  232. idproducttxt.Text = rowView["ID_Product"].ToString();
  233. productnametxt.Text = rowView["NameProduct"].ToString();
  234. dateproduct.Text = rowView["DateProduct"].ToString();
  235. }
  236. }
  237. catch (Exception ex)
  238. {
  239. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  240. }
  241. }
  242. private void productnametxt_TextChanged(object sender, TextChangedEventArgs e)
  243. {
  244. if (sender is TextBox textBox)
  245. {
  246. textBox.Text = new string
  247. (textBox.Text.Where(ch => (ch >= 'А' && ch <= 'Я') || (ch >= 'а' && ch <= 'я')).ToArray());
  248. }
  249. }
  250. private void Refresh_Click(object sender, RoutedEventArgs e)
  251. {
  252. idproducttxt.Text = "";
  253. productnametxt.Text = "";
  254. dateproduct.SelectedDate = null;
  255. showdataproduct();
  256. }
  257. private void Add_Click(object sender, RoutedEventArgs e)
  258. {
  259. if (productnametxt.Text == "" || dateproduct.SelectedDate == null)
  260. {
  261. MessageBox.Show("Заполните все поля!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  262. }
  263. else
  264. {
  265. try
  266. {
  267. con.Open();
  268. string sql = "INSERT INTO Production (NameProduct,DateProduct) VALUES('" + productnametxt.Text + "','" + dateproduct.SelectedDate + "')";
  269. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  270. dataAdapter.SelectCommand.ExecuteNonQuery();
  271. con.Close();
  272. idproducttxt.Text = "";
  273. productnametxt.Text = "";
  274. dateproduct.SelectedDate = null;
  275. showdataproduct();
  276. fillcomboproduct();
  277. }
  278. catch (Exception ex)
  279. {
  280. con.Close();
  281. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  282. }
  283. }
  284. }
  285. private void Update_Click(object sender, RoutedEventArgs e)
  286. {
  287. if (idproducttxt.Text == "")
  288. {
  289. MessageBox.Show("Поле не выбрано! Выберите нужное поле!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  290. }
  291. else
  292. {
  293. try
  294. {
  295. con.Open();
  296. string sql = "Update Production set NameProduct ='" + productnametxt.Text + "', DateProduct = '" + dateproduct.SelectedDate + "' where ID_Product = '" + idproducttxt.Text + "'";
  297. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  298. dataAdapter.SelectCommand.ExecuteNonQuery();
  299. con.Close();
  300. idproducttxt.Text = "";
  301. productnametxt.Text = "";
  302. dateproduct.SelectedDate = null;
  303. showdataproduct();
  304. fillcomboproduct();
  305. }
  306. catch (Exception ex)
  307. {
  308. con.Close();
  309. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  310. }
  311. }
  312. }
  313. private void Delete_Click(object sender, RoutedEventArgs e)
  314. {
  315. if (idproducttxt.Text == "")
  316. {
  317. MessageBox.Show("Поле не выбрано! Выберите нужное поле!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  318. }
  319. else
  320. {
  321. try
  322. {
  323. con.Open();
  324. string sql = "DELETE FROM Production WHERE ID_Product = '" + idproducttxt.Text + "'";
  325. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  326. dataAdapter.SelectCommand.ExecuteNonQuery();
  327. con.Close();
  328. idproducttxt.Text = "";
  329. productnametxt.Text = "";
  330. dateproduct.SelectedDate = null;
  331. showdataproduct();
  332. fillcomboproduct();
  333. }
  334. catch (Exception ex)
  335. {
  336. con.Close();
  337. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  338. }
  339. }
  340. }
  341. private void lastnamepost_TextChanged(object sender, TextChangedEventArgs e)
  342. {
  343. if (sender is TextBox textBox)
  344. {
  345. textBox.Text = new string
  346. (textBox.Text.Where(ch => (ch >= 'А' && ch <= 'Я') || (ch >= 'а' && ch <= 'я')).ToArray());
  347. }
  348. }
  349. private void firstnamepost_TextChanged(object sender, TextChangedEventArgs e)
  350. {
  351. if (sender is TextBox textBox)
  352. {
  353. textBox.Text = new string
  354. (textBox.Text.Where(ch => (ch >= 'А' && ch <= 'Я') || (ch >= 'а' && ch <= 'я')).ToArray());
  355. }
  356. }
  357. private void middlenamepost_TextChanged(object sender, TextChangedEventArgs e)
  358. {
  359. if (sender is TextBox textBox)
  360. {
  361. textBox.Text = new string
  362. (textBox.Text.Where(ch => (ch >= 'А' && ch <= 'Я') || (ch >= 'а' && ch <= 'я')).ToArray());
  363. }
  364. }
  365. private void quantitydocument_TextChanged(object sender, TextChangedEventArgs e)
  366. {
  367. if (sender is TextBox textBox)
  368. {
  369. textBox.Text = new string
  370. (textBox.Text.Where(ch => (ch >= '0' && ch <= '9')).ToArray());
  371. }
  372. }
  373. private void datadocument_SelectionChanged(object sender, SelectionChangedEventArgs e)
  374. {
  375. try
  376. {
  377. DataGrid gd = (DataGrid)sender;
  378. DataRowView rowView = gd.SelectedItem as DataRowView;
  379. if (rowView != null)
  380. {
  381. iddocumenttxt.Text = rowView["ID_Document"].ToString();
  382. firstnamepost.Text = rowView["FirstName"].ToString();
  383. lastnamepost.Text = rowView["LastName"].ToString();
  384. middlenamepost.Text = rowView["MiddleName"].ToString();
  385. datedocument.Text = rowView["DateDocument"].ToString();
  386. quantitydocument.Text = rowView["Quantity"].ToString();
  387. productcombo.Text = rowView["NameProduct"].ToString();
  388. productcombo.IsEnabled = false;
  389. }
  390. }
  391. catch (Exception ex)
  392. {
  393. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  394. }
  395. }
  396. void fillcomboproduct()
  397. {
  398. try
  399. {
  400. productcombo.Items.Clear();
  401. con.Open();
  402. SqlCommand sql = con.CreateCommand();
  403. sql.CommandType = CommandType.Text;
  404. sql.CommandText = "Select NameProduct from Production";
  405. sql.ExecuteNonQuery();
  406. DataTable dt = new DataTable();
  407. SqlDataAdapter da = new SqlDataAdapter(sql);
  408. da.Fill(dt);
  409. foreach (DataRow dr in dt.Rows)
  410. {
  411. productcombo.Items.Add(dr["NameProduct"].ToString());
  412. }
  413. con.Close();
  414. }
  415. catch (Exception ex)
  416. {
  417. con.Close();
  418. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  419. }
  420. }
  421. void showdocument()
  422. {
  423. try
  424. {
  425. con.Open();
  426. string sql = "SELECT ID_Document,LastName,FirstName,MiddleName,DateDocument,Quantity,Production.NameProduct FROM Documents inner join Production on Documents.ID_Product = Production.ID_Product";
  427. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  428. DataTable data = new DataTable("Documents");
  429. dataAdapter.Fill(data);
  430. datadocument.ItemsSource = data.DefaultView;
  431. dataAdapter.Update(data);
  432. con.Close();
  433. datadocument.Columns[0].Header = "ID";
  434. datadocument.Columns[1].Header = "Фамилия поставщика";
  435. datadocument.Columns[2].Header = "Имя поставщика";
  436. datadocument.Columns[3].Header = "Отчество поставщика";
  437. datadocument.Columns[4].Header = "Дата документа";
  438. datadocument.Columns[5].Header = "Количество";
  439. datadocument.Columns[6].Header = "Продукция";
  440. datadocument.Columns[0].Visibility = Visibility.Collapsed;
  441. (datadocument.Columns[4] as DataGridTextColumn).Binding.StringFormat = "dd/MM/yyyy";
  442. }
  443. catch (Exception ex)
  444. {
  445. con.Close();
  446. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  447. }
  448. }
  449. private void RefreshDocument_Click(object sender, RoutedEventArgs e)
  450. {
  451. iddocumenttxt.Text = "";
  452. firstnamepost.Text = "";
  453. lastnamepost.Text = "";
  454. middlenamepost.Text = "";
  455. datedocument.SelectedDate = null;
  456. quantitydocument.Text = "";
  457. productcombo.Text = "";
  458. productcombo.IsEnabled = true;
  459. showdocument();
  460. fillvsego();
  461. }
  462. private void AddDocument_Click(object sender, RoutedEventArgs e)
  463. {
  464. if (firstnamepost.Text == "" || lastnamepost.Text == "" || middlenamepost.Text == "" || productcombo.Text == "" || quantitydocument.Text == "" || datedocument.SelectedDate == null)
  465. {
  466. MessageBox.Show("Заполните все поля!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  467. }
  468. else
  469. {
  470. try
  471. {
  472. con.Open();
  473. SqlCommand cmd = new SqlCommand("Select ID_Product from Production where NameProduct ='" + productcombo.Text + "'", con);
  474. cmd.CommandType = CommandType.Text;
  475. SqlDataAdapter adapter = new SqlDataAdapter();
  476. adapter.SelectCommand = cmd;
  477. DataSet dataSet = new DataSet();
  478. adapter.Fill(dataSet);
  479. if (dataSet.Tables[0].Rows.Count > 0)
  480. {
  481. string idproduct = dataSet.Tables[0].Rows[0]["ID_Product"].ToString();
  482. string sql = "INSERT INTO Documents (LastName,FirstName,MiddleName,DateDocument,Quantity,ID_Product) VALUES('" + lastnamepost.Text + "','" + firstnamepost.Text + "','" + middlenamepost.Text + "','" + datedocument.SelectedDate + "','" + quantitydocument.Text + "','" + idproduct.ToString() + "'); INSERT INTO Traffics (ID_TrafficType,TrafficDate,Quantity,ID_Product) VALUES('1','"+datedocument.SelectedDate+ "','" + quantitydocument.Text + "','" + idproduct.ToString() + "')";
  483. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  484. dataAdapter.SelectCommand.ExecuteNonQuery();
  485. con.Close();
  486. iddocumenttxt.Text = "";
  487. firstnamepost.Text = "";
  488. lastnamepost.Text = "";
  489. middlenamepost.Text = "";
  490. datedocument.SelectedDate = null;
  491. quantitydocument.Text = "";
  492. productcombo.Text = "";
  493. showdocument();
  494. showtraffic();
  495. fillvsego();
  496. productcombo.IsEnabled = true;
  497. }
  498. }
  499. catch (Exception ex)
  500. {
  501. con.Close();
  502. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  503. }
  504. }
  505. }
  506. private void DeleteDocument_Click(object sender, RoutedEventArgs e)
  507. {
  508. if (iddocumenttxt.Text == "")
  509. {
  510. MessageBox.Show("Поле не выбрано! Выберите нужное поле!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  511. }
  512. else
  513. {
  514. try
  515. {
  516. con.Open();
  517. SqlCommand cmd = new SqlCommand("Select ID_Product from Production where NameProduct ='" + productcombo.Text + "'", con);
  518. cmd.CommandType = CommandType.Text;
  519. SqlDataAdapter adapter = new SqlDataAdapter();
  520. adapter.SelectCommand = cmd;
  521. DataSet dataSet = new DataSet();
  522. adapter.Fill(dataSet);
  523. if (dataSet.Tables[0].Rows.Count > 0)
  524. {
  525. string idproduct = dataSet.Tables[0].Rows[0]["ID_Product"].ToString();
  526. string sql = "DELETE FROM Documents WHERE ID_Document = '" + iddocumenttxt.Text + "'; INSERT INTO Traffics (ID_TrafficType,TrafficDate,Quantity,ID_Product) VALUES('2',GETDATE(),'" + quantitydocument.Text + "','" + idproduct.ToString() + "')";
  527. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  528. dataAdapter.SelectCommand.ExecuteNonQuery();
  529. con.Close();
  530. iddocumenttxt.Text = "";
  531. firstnamepost.Text = "";
  532. lastnamepost.Text = "";
  533. middlenamepost.Text = "";
  534. datedocument.SelectedDate = null;
  535. quantitydocument.Text = "";
  536. productcombo.Text = "";
  537. productcombo.IsEnabled = true;
  538. showdocument();
  539. showtraffic();
  540. fillvsego();
  541. }
  542. }
  543. catch (Exception ex)
  544. {
  545. con.Close();
  546. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  547. }
  548. }
  549. }
  550. private void datatraffic_SelectionChanged(object sender, SelectionChangedEventArgs e)
  551. {
  552. try
  553. {
  554. DataGrid gd = (DataGrid)sender;
  555. DataRowView rowView = gd.SelectedItem as DataRowView;
  556. if (rowView != null)
  557. {
  558. idtraffictxt.Text = rowView["ID_Traffic"].ToString();
  559. }
  560. }
  561. catch (Exception ex)
  562. {
  563. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  564. }
  565. }
  566. private void RefreshTraffic_Click(object sender, RoutedEventArgs e)
  567. {
  568. idtraffictxt.Text = "";
  569. showtraffic();
  570. }
  571. private void DeleteTraffic_Click(object sender, RoutedEventArgs e)
  572. {
  573. if (idtraffictxt.Text == "")
  574. {
  575. MessageBox.Show("Поле не выбрано! Выберите нужное поле!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  576. }
  577. else
  578. {
  579. try
  580. {
  581. con.Open();
  582. string sql = "DELETE FROM Traffics WHERE ID_Traffic = '" + idtraffictxt.Text + "'";
  583. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  584. dataAdapter.SelectCommand.ExecuteNonQuery();
  585. con.Close();
  586. idtraffictxt.Text = "";
  587. showtraffic();
  588. }
  589. catch (Exception ex)
  590. {
  591. con.Close();
  592. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  593. }
  594. }
  595. }
  596. void showtraffic()
  597. {
  598. try
  599. {
  600. con.Open();
  601. string sql = "SELECT ID_Traffic,TrafficTypes.TrafficType,TrafficDate,Quantity,Production.NameProduct FROM Traffics inner join Production on Traffics.ID_Product = Production.ID_Product inner join TrafficTypes on Traffics.ID_TrafficType = TrafficTypes.ID_TrafficType";
  602. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  603. DataTable data = new DataTable("Documents");
  604. dataAdapter.Fill(data);
  605. datatraffic.ItemsSource = data.DefaultView;
  606. dataAdapter.Update(data);
  607. con.Close();
  608. datatraffic.Columns[0].Header = "ID";
  609. datatraffic.Columns[1].Header = "Тип движения";
  610. datatraffic.Columns[2].Header = "Дата движения";
  611. datatraffic.Columns[3].Header = "Количество";
  612. datatraffic.Columns[4].Header = "Продукция";
  613. datatraffic.Columns[0].Visibility = Visibility.Collapsed;
  614. (datatraffic.Columns[2] as DataGridTextColumn).Binding.StringFormat = "dd/MM/yyyy";
  615. }
  616. catch (Exception ex)
  617. {
  618. con.Close();
  619. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  620. }
  621. }
  622. }
  623. }