BuyTools.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 BuyTools : Form
  14. {
  15. bool discount = false;
  16. public int razmer = 0;
  17. public static string[] buy = new string[1];
  18. string id_product;
  19. DataBase dataBase = new DataBase();
  20. public BuyTools(string id_product, string product, string description, string price, string year_publishing, string country, string edition, string genre, string performer, string supplier)
  21. {
  22. InitializeComponent();
  23. text_name.Text = product;
  24. text_description.Text = description;
  25. text_price.Text = Convert.ToString(Math.Round(Convert.ToDouble(price), 2));
  26. text_output.Text = text_price.Text;
  27. textBox_year_publishing.Text = year_publishing;
  28. text_country.Text = country;
  29. text_edition.Text = edition;
  30. text_genre.Text = genre;
  31. text_performer.Text = performer;
  32. text_id_suplier.Text = supplier;
  33. this.id_product = id_product;
  34. }
  35. private void button_back_Click(object sender, EventArgs e)
  36. {
  37. this.Hide();
  38. Sales sales = new Sales();
  39. sales.ShowDialog();
  40. }
  41. private void text_input_TextChanged(object sender, EventArgs e)
  42. {
  43. if(text_input.Text == "")
  44. {
  45. text_input.Text = "0";
  46. }
  47. text_output.Text = Convert.ToString(GetSurrender(Convert.ToDouble(text_input.Text), Convert.ToDouble(text_price.Text)));
  48. text_output.Text = Convert.ToString(Math.Round(Convert.ToDouble(text_output.Text), 2));
  49. if (Convert.ToDouble(text_input.Text) <= Convert.ToDouble(text_price.Text))
  50. {
  51. label_output1.Visible = true;
  52. label_output2.Visible = false;
  53. }
  54. else
  55. {
  56. label_output2.Visible = true;
  57. label_output1.Visible = false;
  58. }
  59. }
  60. private void button_buy_Click(object sender, EventArgs e)
  61. {
  62. dataBase.openConnection();
  63. string str = DateTime.Now.ToString("d MMMM yyyy");
  64. string str1;
  65. SqlCommand sqlCommand1 = new SqlCommand($"SELECT id_employee From Employee WHERE login = '{PosleVHODA.login}'", dataBase.GetConnection());
  66. str1 = sqlCommand1.ExecuteScalar().ToString();
  67. var addQuery = $"insert into Sales (id_product, date_sale, id_employee, summa) values ('{id_product}','{str}','{str1}', '{Convert.ToDouble(text_price.Text)}')";
  68. var command = new SqlCommand(addQuery, dataBase.GetConnection());
  69. command.ExecuteNonQuery();
  70. MessageBox.Show("Покупка совершена");
  71. Sales.countBuyProducts = GetCountBuyProducts(Sales.countBuyProducts);
  72. Sales.summaBuyProducts = GetSummaBuyProducts(Sales.summaBuyProducts, Convert.ToDouble(text_price.Text));
  73. buy[razmer] = text_name.Text;
  74. Array.Resize(ref buy, buy.Length + 1);
  75. razmer++;
  76. dataBase.closeConnection();
  77. this.Hide();
  78. Sales sales = new Sales();
  79. sales.ShowDialog();
  80. }
  81. public void global_FormClosed(object sender, EventArgs e)
  82. {
  83. Application.Exit();
  84. }
  85. private void text_input_KeyPress(object sender, KeyPressEventArgs e)
  86. {
  87. char number = e.KeyChar;
  88. if (!Char.IsDigit(number) && number != 8 && !(e.KeyChar == ',')) // цифры и клавиша BackSpace
  89. {
  90. e.Handled = true;
  91. }
  92. }
  93. public static int GetCountBuyProducts(int countBuyProducts)
  94. {
  95. countBuyProducts++;
  96. return countBuyProducts;
  97. }
  98. public static double GetSummaBuyProducts(double summaBuyProducts, double summa)
  99. {
  100. summaBuyProducts = summaBuyProducts + summa;
  101. return summaBuyProducts;
  102. }
  103. public static double ShareCount(double summa, double percent)
  104. {
  105. summa = summa - summa *(double)(percent/100);
  106. return summa;
  107. }
  108. public static double GetSurrender(double summaCustomer, double summaProduct)
  109. {
  110. summaProduct = summaProduct - summaCustomer;
  111. return summaProduct;
  112. }
  113. private void button_discount_Click(object sender, EventArgs e)
  114. {
  115. if (text_discount.Text == "")
  116. {
  117. MessageBox.Show("Поле скидка не заполнено");
  118. }
  119. else if (text_discount.Text == "0")
  120. {
  121. MessageBox.Show("Скидка не может быть равна 0");
  122. }
  123. else if (Convert.ToDouble(text_discount.Text) > 100)
  124. {
  125. MessageBox.Show("Скидка не может быть больше 100%");
  126. }
  127. else if (discount == true)
  128. {
  129. var confirmResult = MessageBox.Show("Вы точно хотите применить скидку?\nСкидка будет применена к уже уценённому товару", "Подтвердите применение скидки!", MessageBoxButtons.YesNo);
  130. if (confirmResult == DialogResult.Yes)
  131. {
  132. text_price.Text = Convert.ToString(ShareCount(Convert.ToDouble(text_price.Text), Convert.ToDouble(text_discount.Text)));
  133. text_price.Text = Convert.ToString(Math.Round(Convert.ToDouble(text_price.Text), 2));
  134. showDiscount.Visible = true;
  135. showDiscount.Text = showDiscount.Text + "+" + text_discount.Text + "%";
  136. text_discount.Text = "";
  137. }
  138. }
  139. else
  140. {
  141. text_price.Text = Convert.ToString(ShareCount(Convert.ToDouble(text_price.Text), Convert.ToDouble(text_discount.Text)));
  142. text_price.Text = Convert.ToString(Math.Round(Convert.ToDouble(text_price.Text), 2));
  143. showDiscount.Visible = true;
  144. showDiscount.Text = showDiscount.Text + text_discount.Text + "%";
  145. text_discount.Text = "";
  146. discount = true;
  147. }
  148. text_input_TextChanged(sender, e);
  149. }
  150. private void text_discount_KeyPress(object sender, KeyPressEventArgs e)
  151. {
  152. char number = e.KeyChar;
  153. if (!Char.IsDigit(number) && number != 8 && !(e.KeyChar == ',')) // цифры и клавиша BackSpace
  154. {
  155. e.Handled = true;
  156. }
  157. }
  158. }
  159. }