Sales.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.SqlClient;
  11. namespace proba
  12. {
  13. public partial class Sales : Form
  14. {
  15. string id_product;
  16. string product;
  17. string description;
  18. string price;
  19. string year_publishing;
  20. string country;
  21. string edition;
  22. string genre;
  23. string performer;
  24. string supplier;
  25. public static int countBuyProducts = 0;
  26. public static double summaBuyProducts = 0;
  27. DataBase dataBase = new DataBase();
  28. public Sales()
  29. {
  30. InitializeComponent();
  31. }
  32. private void CreateColumns()
  33. {
  34. dataGridView1.Columns.Add("product", "Номенклатура");
  35. dataGridView1.Columns.Add("description", "Описание");
  36. dataGridView1.Columns.Add("price", "Цена");
  37. dataGridView1.Columns.Add("year_publishing", "Год издания");
  38. dataGridView1.Columns.Add("country", "Страна производства");
  39. dataGridView1.Columns.Add("edition", "Издательство");
  40. dataGridView1.Columns.Add("genre", "Жанр музыки");
  41. dataGridView1.Columns.Add("performer", "Исполнитель");
  42. dataGridView1.Columns.Add("supplier", "Поставщик");
  43. }
  44. private void ReadSingleRow(DataGridView dgw, IDataRecord record)
  45. {
  46. dgw.Rows.Add(record.GetString(0), record.GetString(1), record.GetDouble(2), record.GetString(3), record.GetString(4), record.GetString(5), record.GetString(6), record.GetString(7), record.GetString(8));
  47. }
  48. private void RefresDataGird(DataGridView dgw)
  49. {
  50. dgw.Rows.Clear();
  51. string queryString = $"Select P.product, P.description, P.price, P.year_publishing, C.country, E.edition, G.genre, Per.performer, S.name_organization From Products P inner join Country C ON P.id_country = C.id_country inner join Edition E ON P.id_edition = E.id_edition inner join Genre G ON P.id_genre = G.id_genre inner join Performer Per ON P.id_performer = Per.id_performer inner join Supplier S ON P.id_supplier = S.id_supplier LEFT JOIN Sales Sales ON P.id_product = Sales.id_product WHERE Sales.id_product IS NULL";
  52. SqlCommand command = new SqlCommand(queryString, dataBase.GetConnection());
  53. dataBase.openConnection();
  54. SqlDataReader reader = command.ExecuteReader();
  55. while (reader.Read())
  56. {
  57. ReadSingleRow(dgw, reader);
  58. }
  59. reader.Close();
  60. }
  61. private void Sales_Load(object sender, EventArgs e)
  62. {
  63. CreateColumns();
  64. RefresDataGird(dataGridView1);
  65. }
  66. private void button_back_Click_1(object sender, EventArgs e)
  67. {
  68. DialogResult dialogResult = MessageBox.Show("Продано товаров: " + countBuyProducts +"\nСовершена продажа на сумму: " + summaBuyProducts, "Информация", MessageBoxButtons.OK);
  69. if (dialogResult == DialogResult.OK)
  70. {
  71. this.Hide();
  72. MainMenu mainMenu = new MainMenu();
  73. mainMenu.ShowDialog();
  74. }
  75. }
  76. public void global_FormClosed(object sender, EventArgs e)
  77. {
  78. Application.Exit();
  79. }
  80. private void button_buy_Click(object sender, EventArgs e)
  81. {
  82. if (id_product == null)
  83. {
  84. MessageBox.Show("Не выделена номенклатура для покупки");
  85. }
  86. else
  87. {
  88. this.Hide();
  89. BuyTools buyTools = new BuyTools(id_product, product, description, price, year_publishing, country, edition, genre, performer, supplier);
  90. buyTools.ShowDialog();
  91. }
  92. }
  93. private void pictureBox2_Click(object sender, EventArgs e)
  94. {
  95. RefresDataGird(dataGridView1);
  96. }
  97. private void text_id_view_tools_SelectedIndexChanged(object sender, EventArgs e)
  98. {
  99. Search(dataGridView1);
  100. }
  101. private void Search(DataGridView dgw)
  102. {
  103. dgw.Rows.Clear();
  104. string searchString = "";
  105. if (text_name.Text.Replace(" ", "") != "")
  106. {
  107. searchString = $"Select P.product, P.description, P.price, P.year_publishing, C.country, E.edition, G.genre, Per.performer, S.name_organization From Products P inner join Country C ON P.id_country = C.id_country inner join Edition E ON P.id_edition = E.id_edition inner join Genre G ON P.id_genre = G.id_genre inner join Performer Per ON P.id_performer = Per.id_performer inner join Supplier S ON P.id_supplier = S.id_supplier LEFT JOIN Sales Sales ON P.id_product = Sales.id_product WHERE Sales.id_product IS NULL and product like '%" + text_name.Text + "%'";
  108. }
  109. else
  110. if (text_price1.Text.Replace(" ", "") != "")
  111. {
  112. searchString = $"Select P.product, P.description, P.price, P.year_publishing, C.country, E.edition, G.genre, Per.performer, S.name_organization From Products P inner join Country C ON P.id_country = C.id_country inner join Edition E ON P.id_edition = E.id_edition inner join Genre G ON P.id_genre = G.id_genre inner join Performer Per ON P.id_performer = Per.id_performer inner join Supplier S ON P.id_supplier = S.id_supplier LEFT JOIN Sales Sales ON P.id_product = Sales.id_product WHERE Sales.id_product IS NULL and price > '" + text_price1.Text + "'";
  113. }
  114. else
  115. if (text_price2.Text.Replace(" ", "") != "")
  116. {
  117. searchString = $"Select P.product, P.description, P.price, P.year_publishing, C.country, E.edition, G.genre, Per.performer, S.name_organization From Products P inner join Country C ON P.id_country = C.id_country inner join Edition E ON P.id_edition = E.id_edition inner join Genre G ON P.id_genre = G.id_genre inner join Performer Per ON P.id_performer = Per.id_performer inner join Supplier S ON P.id_supplier = S.id_supplier LEFT JOIN Sales Sales ON P.id_product = Sales.id_product WHERE Sales.id_product IS NULL and price < '" + text_price2.Text + "'";
  118. }
  119. else
  120. {
  121. searchString = $"Select P.product, P.description, P.price, P.year_publishing, C.country, E.edition, G.genre, Per.performer, S.name_organization From Products P inner join Country C ON P.id_country = C.id_country inner join Edition E ON P.id_edition = E.id_edition inner join Genre G ON P.id_genre = G.id_genre inner join Performer Per ON P.id_performer = Per.id_performer inner join Supplier S ON P.id_supplier = S.id_supplier LEFT JOIN Sales Sales ON P.id_product = Sales.id_product WHERE Sales.id_product IS NULL";
  122. }
  123. SqlCommand com = new SqlCommand(searchString, dataBase.GetConnection());
  124. dataBase.openConnection();
  125. SqlDataReader read = com.ExecuteReader();
  126. while (read.Read())
  127. {
  128. ReadSingleRow(dgw, read);
  129. }
  130. read.Close();
  131. }
  132. private void text_name_TextChanged(object sender, EventArgs e)
  133. {
  134. Search(dataGridView1);
  135. }
  136. private void text_price1_TextChanged(object sender, EventArgs e)
  137. {
  138. Search(dataGridView1);
  139. }
  140. private void text_price2_TextChanged(object sender, EventArgs e)
  141. {
  142. Search(dataGridView1);
  143. }
  144. private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
  145. {
  146. var selectedRow = e.RowIndex;
  147. if (e.RowIndex >= 0)
  148. {
  149. DataGridViewRow row = dataGridView1.Rows[selectedRow];
  150. dataBase.openConnection();
  151. product = row.Cells[0].Value.ToString();
  152. description = row.Cells[1].Value.ToString();
  153. price = row.Cells[2].Value.ToString();
  154. year_publishing = row.Cells[3].Value.ToString();
  155. country = row.Cells[4].Value.ToString();
  156. edition = row.Cells[5].Value.ToString();
  157. genre = row.Cells[6].Value.ToString();
  158. performer = row.Cells[7].Value.ToString();
  159. supplier = row.Cells[8].Value.ToString();
  160. SqlCommand sqlCommand_product = new SqlCommand($"SELECT id_product From Products WHERE product = '{product}'", dataBase.GetConnection());
  161. id_product = sqlCommand_product.ExecuteScalar().ToString();
  162. dataBase.closeConnection();
  163. }
  164. }
  165. }
  166. }