1
0

FormPotrebRielter.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.IO;
  11. using System.Data.SqlClient;
  12. namespace СУБД_Агентство
  13. {
  14. public partial class FormPotrebRielter : Form
  15. {
  16. public FormPotrebRielter()
  17. {
  18. InitializeComponent();
  19. }
  20. // процент от продажи, процент от покупки
  21. double procprodazha, procpokupka;
  22. private void FormPotrebRielter_Load(object sender, EventArgs e)
  23. {
  24. // TODO: This line of code loads data into the 'dbAgentstvoDataSet1.predlozhenie' table. You can move, or remove it, as needed.
  25. this.predlozhenieTableAdapter.Fill(this.dbAgentstvoDataSet1.predlozhenie);
  26. // TODO: This line of code loads data into the 'dbAgentstvoDataSet1.Potrebnost' table. You can move, or remove it, as needed.
  27. this.potrebnostTableAdapter.Fill(this.dbAgentstvoDataSet1.Potrebnost);
  28. // TODO: This line of code loads data into the 'dbAgentstvoDataSet1.Users' table. You can move, or remove it, as needed.
  29. this.usersTableAdapter.Fill(this.dbAgentstvoDataSet1.Users);
  30. // чтобы не отображались предложения для пустой таблицы с потребностями
  31. if (bsPotrebnost.Count == 0)
  32. bsPredlozheniya.Filter = "'False'";
  33. // открыть файл для чтения
  34. StreamReader sr = new StreamReader("data.txt");
  35. string t = sr.ReadLine(); // прочитать первую строку с макс. кол-вом клиентов
  36. procprodazha = Convert.ToDouble(sr.ReadLine()); // процент от продажи
  37. procpokupka = Convert.ToDouble(sr.ReadLine()); // процент от покупки
  38. sr.Close(); // закрыть файл
  39. }
  40. private void btnSdelka_Click(object sender, EventArgs e)
  41. {
  42. // код потребности и предложения, участвующих в сделке
  43. string idpotreb = dgvPotreb.CurrentRow.Cells[8].Value.ToString();
  44. string idpredl = dgvPredl.CurrentRow.Cells[5].Value.ToString();
  45. string famrielterpok = lblFamRielterPok.Text; // риелтер покупателя
  46. // id покупателя
  47. string idpok = dgvPotreb.CurrentRow.Cells[10].Value.ToString();
  48. // id риелтера продавца
  49. string idrielterprod = dgvPredl.CurrentRow.Cells[7].Value.ToString();
  50. // отфильтровать пользователя - покупателя
  51. bsUsersFam.Filter = "idusers = " + idpok;
  52. string fampok = lblFam.Text; // прочитать фамилию покупателя
  53. // отфильтровать пользователя - риелтера продавца
  54. bsUsersFam.Filter = "idusers = " + idrielterprod;
  55. string famrielterprod = lblFam.Text; // прочитать фамилию риелтера продавца
  56. // подключение к базе данных
  57. SqlConnection con = new SqlConnection(Form1.txtcon);
  58. con.Open(); // открыть подключение к базе данных
  59. // SQL-запрос для добавления записи
  60. string t = String.Format(@"
  61. insert into Sdelka
  62. (idpotrebnost, idpredlozhenie, datesdelka)
  63. values ({0}, {1}, '{2}')", idpotreb, idpredl, DateTime.Now.ToString("dd.MM.yyyy"));
  64. // установить формат даты на сервере
  65. SqlCommand query2 = new SqlCommand("set dateformat dmy", con);
  66. query2.ExecuteNonQuery();
  67. // Добавить в таблицу Сделка новую запись
  68. SqlCommand query1 = new SqlCommand(t, con);
  69. query1.ExecuteNonQuery();
  70. con.Close(); // закрыть подключение к базе данных
  71. // цена проданного объекта недвижимости
  72. double sum = Convert.ToDouble(dgvPredl.CurrentRow.Cells[4].Value);
  73. // комиссия для риелетра покупателя
  74. double sumrielterpok = sum * procpokupka / 100;
  75. // комиссия для риелетра продавца
  76. double sumrielterprod = sum * procprodazha / 100;
  77. sum += sumrielterpok + sumrielterprod;
  78. // предложение и потребность, участвующие в сделке завершены
  79. dgvPredl.CurrentRow.Cells[6].Value = true;
  80. bsPredlozheniya.EndEdit();
  81. predlozhenieTableAdapter.Update(this.dbAgentstvoDataSet1.predlozhenie);
  82. // сначала обязательно сохранить изменения в таблице Предложения, а только
  83. // потом в таблице Потребности. Если сделать наоборот, то при сохранении
  84. // потребностей в таблице Предложения исчезнет выделенная строка
  85. dgvPotreb.CurrentRow.Cells[9].Value = true;
  86. bsPotrebnost.EndEdit();
  87. potrebnostTableAdapter.Update(this.dbAgentstvoDataSet1.Potrebnost);
  88. // чтобы не отображались предложения для пустой таблицы с потребностями
  89. if (bsPotrebnost.Count == 0)
  90. bsPredlozheniya.Filter = "'False'";
  91. MessageBox.Show(@"Сделка совершена.
  92. Покупатель " + fampok + " должен заплатить: " + sum +
  93. "\nРиелтер покупателя " + famrielterpok + " получает: " + sumrielterpok +
  94. "\nРиелтер продавца " + famrielterprod + " получает: " + sumrielterprod);
  95. }
  96. private void dataGridView1_SelectionChanged(object sender, EventArgs e)
  97. {
  98. // если нет выделенной потребности, то выйти из процедуры
  99. if (dgvPotreb.CurrentRow == null) return;
  100. // прочитать из DataGridView параметры выделенной потребности
  101. int minplosh = Convert.ToInt32(dgvPotreb.CurrentRow.Cells[0].Value);
  102. int maxplosh = Convert.ToInt32(dgvPotreb.CurrentRow.Cells[1].Value);
  103. int minetazh = Convert.ToInt32(dgvPotreb.CurrentRow.Cells[2].Value);
  104. int maxetazh = Convert.ToInt32(dgvPotreb.CurrentRow.Cells[3].Value);
  105. int minkomnat = Convert.ToInt32(dgvPotreb.CurrentRow.Cells[4].Value);
  106. int maxkomnat = Convert.ToInt32(dgvPotreb.CurrentRow.Cells[5].Value);
  107. double mincena = Convert.ToDouble(dgvPotreb.CurrentRow.Cells[6].Value);
  108. double maxcena = Convert.ToDouble(dgvPotreb.CurrentRow.Cells[7].Value);
  109. string t = String.Format("plosh >= {0} and plosh <= {1} and etazh >= {2} and etazh <= {3} and komnat >= {4} and komnat <= {5} and cena >= {6} and cena <= {7} and zaversheno = 'False'", minplosh, maxplosh, minetazh, maxetazh, minkomnat, maxkomnat, mincena, maxcena);
  110. // отфильтровать предложения, подходящие для реализации потребности
  111. bsPredlozheniya.Filter = t;
  112. }
  113. }
  114. }