CalculetionForm.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 NN_TelekomPP.Forms
  12. {
  13. public partial class CalculetionForm : Form
  14. {
  15. string id_num;
  16. string MG1;
  17. string MG2;
  18. bool save;
  19. DataBase dataBase = new DataBase();
  20. public CalculetionForm()
  21. {
  22. InitializeComponent();
  23. }
  24. private void button_back_Click(object sender, EventArgs e)
  25. {
  26. this.Hide();
  27. Menu M = new Menu();
  28. M.ShowDialog();
  29. }
  30. private void CreateColums()
  31. {
  32. dataGridView1.Columns.Add("id", "id");
  33. dataGridView1.Columns.Add("Name", "Название организации (абонента)");
  34. dataGridView1.Columns.Add("Number", "МГ1");
  35. dataGridView1.Columns.Add("Number", "МГ2");
  36. dataGridView1.Columns.Add("Number", "Итого");
  37. dataGridView1.Columns.Add("IsNew", String.Empty);
  38. }
  39. private void ReadSingleRow(DataGridView dgw, IDataRecord record)
  40. {
  41. dgw.Rows.Add(record.GetInt32(0), record.GetString(1), record.GetDouble(2), record.GetDouble(3), record.GetDouble(4), RowState.ModifiedNew);
  42. }
  43. private void RefrashDataGrid(DataGridView dgw)
  44. {
  45. dgw.Rows.Clear();
  46. dataGridView1.Columns[0].Visible = false;
  47. dataGridView1.Columns[5].Visible = false;
  48. string querryString = $"select Calculation.Calculation_code,Organizations.Name, Calculation.MG_OSIPS, Calculation.MG_m200, sum(ServicesADD.Cost)+ Internet.Cost+SubscriptionFee.Cost+Calculation.MG_OSIPS+ Calculation.MG_m200 from ADD_customers join Organizations on Organizations.Organizations_code=ADD_customers.Organizations_code join ServicesADD on ServicesADD.ServicesADD_code=ADD_customers.ServicesADD_code join Calculation on Calculation.Organizations_code=Organizations.Organizations_code join Services_customers on Services_customers.Organizations_code=Organizations.Organizations_code join Internet on Internet.Internet_code=Services_customers.Internet_code join SubscriptionFee on SubscriptionFee.AP_code=Services_customers.AP_code GROUP BY Organizations.Name, Calculation.MG_OSIPS, Calculation.MG_m200,Internet.Cost,SubscriptionFee.Cost, Calculation.Calculation_code";
  49. SqlCommand command = new SqlCommand(querryString, dataBase.GetConnection());
  50. dataBase.openConnection();
  51. SqlDataReader reader = command.ExecuteReader();
  52. while (reader.Read())
  53. {
  54. ReadSingleRow(dgw, reader);
  55. }
  56. reader.Close();
  57. }
  58. private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  59. {
  60. var selectedRow = e.RowIndex;
  61. if (e.RowIndex >= 0)
  62. {
  63. DataGridViewRow row = dataGridView1.Rows[selectedRow];
  64. id_num = row.Cells[0].Value.ToString();
  65. MG1 = row.Cells[1].Value.ToString();
  66. MG2 = row.Cells[2].Value.ToString();
  67. SqlCommand sqlCommand_country = new SqlCommand($"SELECT Calculation_code from Calculation WHERE Calculation_code = '{id_num}'", dataBase.GetConnection());
  68. id_num = sqlCommand_country.ExecuteScalar().ToString();
  69. }
  70. }
  71. private void CalculetionForm_Load(object sender, EventArgs e)
  72. {
  73. CreateColums();
  74. RefrashDataGrid(dataGridView1);
  75. }
  76. private void UPD_button_Click(object sender, EventArgs e)
  77. {
  78. if (id_num == null)
  79. {
  80. MessageBox.Show("Не выделена строчка для изменения");
  81. }
  82. else
  83. {
  84. this.Hide();
  85. CalculetionFormUPD employeeUpdate = new CalculetionFormUPD(id_num, MG1, MG2);
  86. employeeUpdate.ShowDialog();
  87. }
  88. }
  89. private void InternetADD_button_Click(object sender, EventArgs e)
  90. {
  91. this.Hide();
  92. CalculetionFormADD IDD = new CalculetionFormADD();
  93. IDD.ShowDialog();
  94. }
  95. private void StrokaSearch_TextChanged(object sender, EventArgs e)
  96. {
  97. Search(dataGridView1);
  98. }
  99. private void Search(DataGridView dgw)
  100. {
  101. dgw.Rows.Clear();
  102. string searchString = $"select Calculation.Calculation_code,Organizations.Name, Calculation.MG_OSIPS, Calculation.MG_m200, sum(ServicesADD.Cost)+ Internet.Cost+SubscriptionFee.Cost+Calculation.MG_OSIPS+ Calculation.MG_m200 from ADD_customers join Organizations on Organizations.Organizations_code=ADD_customers.Organizations_code join ServicesADD on ServicesADD.ServicesADD_code=ADD_customers.ServicesADD_code join Calculation on Calculation.Organizations_code=Organizations.Organizations_code join Services_customers on Services_customers.Organizations_code=Organizations.Organizations_code join Internet on Internet.Internet_code=Services_customers.Internet_code join SubscriptionFee on SubscriptionFee.AP_code=Services_customers.AP_code Where Organizations.Name like '%" + StrokaSearch.Text + "%' GROUP BY Organizations.Name, Calculation.MG_OSIPS, Calculation.MG_m200,Internet.Cost,SubscriptionFee.Cost, Calculation.Calculation_code ";
  103. SqlCommand com = new SqlCommand(searchString, dataBase.GetConnection());
  104. dataBase.openConnection();
  105. SqlDataReader read = com.ExecuteReader();
  106. while (read.Read())
  107. {
  108. ReadSingleRow(dgw, read);
  109. }
  110. read.Close();
  111. }
  112. private void deleteRow()
  113. {
  114. int index = dataGridView1.CurrentCell.RowIndex;
  115. dataGridView1.Rows[index].Visible = false;
  116. if (dataGridView1.Rows[index].Cells[0].Value.ToString() == String.Empty)
  117. {
  118. dataGridView1.Rows[index].Cells[5].Value = RowState.Deleted;
  119. return;
  120. }
  121. dataGridView1.Rows[index].Cells[5].Value = RowState.Deleted;
  122. }
  123. private void Del_button_Click(object sender, EventArgs e)
  124. {
  125. if (id_num == null)
  126. {
  127. MessageBox.Show("Не выделена запись для удаления!!!");
  128. }
  129. else
  130. {
  131. deleteRow();
  132. save = true;
  133. }
  134. }
  135. new private void Update()
  136. {
  137. dataBase.openConnection();
  138. for (int index = 0; index < dataGridView1.Rows.Count; index++)
  139. {
  140. var rowState = (RowState)dataGridView1.Rows[index].Cells[5].Value;
  141. if (rowState == RowState.Existed)
  142. continue;
  143. if (rowState == RowState.Deleted)
  144. {
  145. var deleteQuery = $"delete from Calculation where Calculation_code = '{id_num}'";
  146. var command = new SqlCommand(deleteQuery, dataBase.GetConnection());
  147. command.ExecuteNonQuery();
  148. return;
  149. }
  150. }
  151. dataBase.closeConnection();
  152. }
  153. private void button1_Click(object sender, EventArgs e)
  154. {
  155. if (save == true)
  156. {
  157. Update();
  158. save = false;
  159. }
  160. }
  161. private void global_FormClosed(object sender, FormClosedEventArgs e)
  162. {
  163. Application.Exit();
  164. }
  165. private void button2_Click(object sender, EventArgs e)
  166. {
  167. summ();
  168. }
  169. public void summ()
  170. {
  171. string querryString = $"select Calculation.Calculation_code,Organizations.Name, Calculation.MG_OSIPS, Calculation.MG_m200, sum(ServicesADD.Cost)+ Internet.Cost+SubscriptionFee.Cost+Calculation.MG_OSIPS+ Calculation.MG_m200 from ADD_customers join Organizations on Organizations.Organizations_code=ADD_customers.Organizations_code join ServicesADD on ServicesADD.ServicesADD_code=ADD_customers.ServicesADD_code join Calculation on Calculation.Organizations_code=Organizations.Organizations_code join Services_customers on Services_customers.Organizations_code=Organizations.Organizations_code join Internet on Internet.Internet_code=Services_customers.Internet_code join SubscriptionFee on SubscriptionFee.AP_code=Services_customers.AP_code GROUP BY Organizations.Name, Calculation.MG_OSIPS, Calculation.MG_m200,Internet.Cost,SubscriptionFee.Cost, Calculation.Calculation_code";
  172. SqlCommand command = new SqlCommand(querryString, dataBase.GetConnection());
  173. SqlDataReader reader = command.ExecuteReader();
  174. double a= 0;
  175. while (reader.Read())
  176. {
  177. a=summ2(a, reader.GetDouble(4));
  178. //a += reader.GetDouble(4);
  179. }
  180. MessageBox.Show($"Итоговая сумма {a}");
  181. reader.Close();
  182. }
  183. public double summ2(double a , double b)
  184. {
  185. return a + b;
  186. }
  187. private void button3_Click(object sender, EventArgs e)
  188. {
  189. this.Hide();
  190. Date M = new Date();
  191. M.ShowDialog();
  192. }
  193. }
  194. }