Rezerv.xaml.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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;
  15. using System.Data.SqlClient;
  16. using System.Windows.Threading;
  17. namespace HotelCalifornia
  18. {
  19. /// <summary>
  20. /// Логика взаимодействия для Rezerv.xaml
  21. /// </summary>
  22. public partial class Rezerv : Window
  23. {
  24. public Rezerv()
  25. {
  26. InitializeComponent();
  27. //Таймер на обновление времени
  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. //Начало даты
  33. datein.DisplayDateStart = DateTime.Now;
  34. dateout.DisplayDateStart = DateTime.Now;
  35. }
  36. //Строка подключения
  37. SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=kursah;Integrated Security=True");
  38. //Вывод даты и время в textblock
  39. private void Update_Timer_Tick(object sender, EventArgs e)
  40. {
  41. timetxt.Text = DateTime.Now.ToString();
  42. }
  43. //Перетаскивание окна
  44. private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
  45. {
  46. DragMove();
  47. }
  48. //Возврат к окну выбора функции
  49. private void Back(object sender, RoutedEventArgs e)
  50. {
  51. Variant variant = new Variant();
  52. variant.idadmintxt.Text = idadmintxt.Text;
  53. this.Close();
  54. variant.Show();
  55. }
  56. //Свернуть окно
  57. private void WindMin_Click(object sender, RoutedEventArgs e)
  58. {
  59. this.WindowState = WindowState.Minimized;
  60. }
  61. //Выход из приложения
  62. private void Close(object sender, RoutedEventArgs e)
  63. {
  64. Application.Current.Shutdown();
  65. }
  66. //Выбор строки из БД
  67. private void datarezerv_SelectionChanged(object sender, SelectionChangedEventArgs e)
  68. {
  69. try
  70. {
  71. DataGrid gd = (DataGrid)sender;
  72. DataRowView rowView = gd.SelectedItem as DataRowView;
  73. if (rowView != null)
  74. {
  75. numbroomtxt.Text = rowView["Number_Room"].ToString();
  76. datein.Text = rowView["Date_in"].ToString();
  77. dateout.Text = rowView["Date_out"].ToString();
  78. clientcombo.Text = rowView["LastName_Client"].ToString();
  79. roomcombo.Text = rowView["Number_Room"].ToString();
  80. roomcombo.IsEnabled = false;
  81. clientcombo.IsEnabled = false;
  82. }
  83. }
  84. catch (Exception ex)
  85. {
  86. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  87. }
  88. }
  89. //Запуск в самом начале
  90. private void Window_Loaded(object sender, RoutedEventArgs e)
  91. {
  92. fillcomboroom();
  93. fillcomboclient();
  94. fill1comboroom();
  95. showgrid();
  96. }
  97. //Заполнение combo
  98. #region Combo
  99. void fillcomboroom()
  100. {
  101. try
  102. {
  103. roomcombo.Items.Clear();
  104. con.Open();
  105. SqlCommand sql = con.CreateCommand();
  106. sql.CommandType = CommandType.Text;
  107. sql.CommandText = "Select Number_Room from Room WHERE Status_Room = 1";
  108. sql.ExecuteNonQuery();
  109. DataTable dt = new DataTable();
  110. SqlDataAdapter da = new SqlDataAdapter(sql);
  111. da.Fill(dt);
  112. foreach (DataRow dr in dt.Rows)
  113. {
  114. roomcombo.Items.Add(dr["Number_Room"].ToString());
  115. }
  116. con.Close();
  117. }
  118. catch (Exception ex)
  119. {
  120. con.Close();
  121. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  122. }
  123. }
  124. void fill1comboroom()
  125. {
  126. try
  127. {
  128. room1combo.Items.Clear();
  129. con.Open();
  130. SqlCommand sql = con.CreateCommand();
  131. sql.CommandType = CommandType.Text;
  132. sql.CommandText = "Select Number_Room from Room WHERE Status_Room = 2";
  133. sql.ExecuteNonQuery();
  134. DataTable dt = new DataTable();
  135. SqlDataAdapter da = new SqlDataAdapter(sql);
  136. da.Fill(dt);
  137. foreach (DataRow dr in dt.Rows)
  138. {
  139. room1combo.Items.Add(dr["Number_Room"].ToString());
  140. }
  141. con.Close();
  142. }
  143. catch (Exception ex)
  144. {
  145. con.Close();
  146. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  147. }
  148. }
  149. void fillcomboclient()
  150. {
  151. try
  152. {
  153. clientcombo.Items.Clear();
  154. con.Open();
  155. SqlCommand sql = con.CreateCommand();
  156. sql.CommandType = CommandType.Text;
  157. sql.CommandText = "Select LastName_Client from Client";
  158. sql.ExecuteNonQuery();
  159. DataTable dt = new DataTable();
  160. SqlDataAdapter da = new SqlDataAdapter(sql);
  161. da.Fill(dt);
  162. foreach (DataRow dr in dt.Rows)
  163. {
  164. clientcombo.Items.Add(dr["LastName_Client"].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. #endregion
  175. //Добавление резервирования
  176. private void Add_Click(object sender, RoutedEventArgs e)
  177. {
  178. if (clientcombo.Text == "" || roomcombo.Text == "" || dateout.SelectedDate == null || datein.SelectedDate == null)
  179. {
  180. MessageBox.Show("Заполните все поля!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  181. }
  182. else if (datein.SelectedDate >= dateout.SelectedDate)
  183. {
  184. MessageBox.Show("Дата указана неверно!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  185. }
  186. else
  187. {
  188. try
  189. {
  190. con.Open();
  191. SqlCommand cmd = new SqlCommand("Select * from Client where LastName_Client = '" + clientcombo.Text + "'", con);
  192. cmd.CommandType = CommandType.Text;
  193. SqlDataAdapter adapter = new SqlDataAdapter();
  194. adapter.SelectCommand = cmd;
  195. DataSet dataSet = new DataSet();
  196. adapter.Fill(dataSet);
  197. if (dataSet.Tables[0].Rows.Count > 0)
  198. {
  199. string stridclient = dataSet.Tables[0].Rows[0]["ID_Client"].ToString();
  200. SqlCommand cmd2 = new SqlCommand("Select * from Rezervirovanie where ID_Client = '" + stridclient.ToString() + "'; Select * from RoomClient where ID_Client = '" + stridclient.ToString() + "'", con);
  201. cmd2.CommandType = CommandType.Text;
  202. SqlDataAdapter adapter2 = new SqlDataAdapter();
  203. adapter2.SelectCommand = cmd2;
  204. DataSet dataSet2 = new DataSet();
  205. adapter2.Fill(dataSet2);
  206. if (dataSet2.Tables[0].Rows.Count > 0)
  207. {
  208. con.Close();
  209. MessageBox.Show("Клиент уже забронировал комнату!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  210. }
  211. else if (dataSet2.Tables[1].Rows.Count > 0)
  212. {
  213. con.Close();
  214. MessageBox.Show("Клиент уже заселён!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  215. }
  216. else
  217. {
  218. SqlCommand cmd1 = new SqlCommand("Select ID_Room from Room where Number_Room = '" + roomcombo.Text + "'; Select ID_Client from Client where LastName_Client = '" + clientcombo.Text + "'", con);
  219. cmd1.CommandType = CommandType.Text;
  220. SqlDataAdapter adapter1 = new SqlDataAdapter();
  221. adapter1.SelectCommand = cmd1;
  222. DataSet dataSet1 = new DataSet();
  223. adapter1.Fill(dataSet1);
  224. if (dataSet1.Tables[0].Rows.Count > 0)
  225. {
  226. string stridroom = dataSet1.Tables[0].Rows[0]["ID_Room"].ToString();
  227. string stridclient1 = dataSet1.Tables[1].Rows[0]["ID_Client"].ToString();
  228. string sql = "INSERT INTO Rezervirovanie (ID_Room,ID_Client,Date_in,Date_out,ID_Administrator) VALUES('" + stridroom.ToString() + "','" + stridclient1.ToString() + "','" + datein.SelectedDate + "','" + dateout.SelectedDate + "','" + idadmintxt.Text.ToString() + "'); Update Room set Status_Room ='" + 2 + "' Where ID_Room = '" + stridroom.ToString() + "'; INSERT INTO RoomClient (ID_Room,ID_Client) VALUES('" + stridroom.ToString() + "','" + stridclient1.ToString() + "')";
  229. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  230. dataAdapter.SelectCommand.ExecuteNonQuery();
  231. con.Close();
  232. MessageBox.Show("Бронирование успешно создано!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  233. }
  234. }
  235. }
  236. fillcomboroom();
  237. fill1comboroom();
  238. roomcombo.Text = "";
  239. clientcombo.Text = "";
  240. datein.SelectedDate = null;
  241. dateout.SelectedDate = null;
  242. showgrid();
  243. }
  244. catch (Exception ex)
  245. {
  246. con.Close();
  247. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  248. }
  249. }
  250. }
  251. //Обновление резервирования
  252. private void Update_Click(object sender, RoutedEventArgs e)
  253. {
  254. if (numbroomtxt.Text == "")
  255. {
  256. MessageBox.Show("Поле не выбрано! Выберите нужное поле!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  257. }
  258. else if (datein.SelectedDate == null || dateout.SelectedDate == null)
  259. {
  260. MessageBox.Show("Заполните все поля!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  261. }
  262. else if (datein.SelectedDate >= dateout.SelectedDate)
  263. {
  264. MessageBox.Show("Дата указана неверно!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  265. }
  266. else
  267. {
  268. try
  269. {
  270. con.Open();
  271. SqlCommand cmd1 = new SqlCommand("Select ID_Room from Room where Number_Room = '" + numbroomtxt.Text + "'", con);
  272. cmd1.CommandType = CommandType.Text;
  273. SqlDataAdapter adapter1 = new SqlDataAdapter();
  274. adapter1.SelectCommand = cmd1;
  275. DataSet dataSet1 = new DataSet();
  276. adapter1.Fill(dataSet1);
  277. if (dataSet1.Tables[0].Rows.Count > 0)
  278. {
  279. string stridroom = dataSet1.Tables[0].Rows[0]["ID_Room"].ToString();
  280. string sql = "Update Rezervirovanie set Date_in ='" + datein.SelectedDate + "', Date_out = '" + dateout.SelectedDate + "', ID_Administrator = '"+idadmintxt.Text+"' where ID_Room = '" + stridroom.ToString() + "'";
  281. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  282. dataAdapter.SelectCommand.ExecuteNonQuery();
  283. con.Close();
  284. showgrid();
  285. roomcombo.Text = "";
  286. room1combo.Text = "";
  287. datein.SelectedDate = null;
  288. dateout.SelectedDate = null;
  289. roomcombo.IsEnabled = true;
  290. clientcombo.IsEnabled = true;
  291. numbroomtxt.Text = "";
  292. MessageBox.Show("Дата резервироания изменена!", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
  293. }
  294. }
  295. catch (Exception ex)
  296. {
  297. con.Close();
  298. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  299. }
  300. }
  301. }
  302. //Удаление резервирования
  303. private void Delete_Click(object sender, RoutedEventArgs e)
  304. {
  305. if (numbroomtxt.Text == "")
  306. {
  307. MessageBox.Show("Поле не выбрано! Выберите нужное поле!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  308. }
  309. else
  310. {
  311. try
  312. {
  313. con.Open();
  314. SqlCommand cmd1 = new SqlCommand("Select ID_Room from Room where Number_Room = '" + numbroomtxt.Text + "'; Select ID_Client From Client where LastName_Client = '" + clientcombo.Text + "'", con);
  315. cmd1.CommandType = CommandType.Text;
  316. SqlDataAdapter adapter1 = new SqlDataAdapter();
  317. adapter1.SelectCommand = cmd1;
  318. DataSet dataSet1 = new DataSet();
  319. adapter1.Fill(dataSet1);
  320. if (dataSet1.Tables[0].Rows.Count > 0)
  321. {
  322. string stridroom = dataSet1.Tables[0].Rows[0]["ID_Room"].ToString();
  323. string stridclient = dataSet1.Tables[1].Rows[0]["ID_Client"].ToString();
  324. string sql = "DELETE FROM Rezervirovanie WHERE ID_Room = '" + stridroom.ToString() + "'; Update Room set Status_Room ='" + 1 + "' Where ID_Room = '" + stridroom.ToString() + "'; DELETE FROM RoomClient WHERE ID_Client = '" + stridclient.ToString() + "';";
  325. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  326. dataAdapter.SelectCommand.ExecuteNonQuery();
  327. con.Close();
  328. showgrid();
  329. fillcomboroom();
  330. fill1comboroom();
  331. roomcombo.Text = "";
  332. room1combo.Text = "";
  333. datein.SelectedDate = null;
  334. dateout.SelectedDate = null;
  335. roomcombo.IsEnabled = true;
  336. clientcombo.IsEnabled = true;
  337. numbroomtxt.Text = "";
  338. MessageBox.Show("Резервирование удалено!", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
  339. }
  340. }
  341. catch (Exception ex)
  342. {
  343. con.Close();
  344. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  345. }
  346. }
  347. }
  348. //Фомировка данных из БД
  349. void showgrid()
  350. {
  351. try
  352. {
  353. con.Open();
  354. string sql = "SELECT Number_Room, Client.LastName_Client, Rezervirovanie.Date_in,Rezervirovanie.Date_out From Room inner join Rezervirovanie on Room.ID_Room = Rezervirovanie.ID_Room inner join Client on Rezervirovanie.[ID_Client] = Client.ID_Client";
  355. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  356. DataTable data = new DataTable("Rezervirovanie");
  357. dataAdapter.Fill(data);
  358. datarezerv.ItemsSource = data.DefaultView;
  359. dataAdapter.Update(data);
  360. con.Close();
  361. datarezerv.Columns[0].Header = "Номер комнаты";
  362. datarezerv.Columns[1].Header = "Фамилия";
  363. datarezerv.Columns[2].Header = "Дата прибытия";
  364. datarezerv.Columns[3].Header = "Дата отбытия";
  365. (datarezerv.Columns[2] as DataGridTextColumn).Binding.StringFormat = "dd/MM/yyyy";
  366. (datarezerv.Columns[3] as DataGridTextColumn).Binding.StringFormat = "dd/MM/yyyy";
  367. }
  368. catch (Exception ex)
  369. {
  370. con.Close();
  371. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  372. }
  373. }
  374. //Поиск
  375. private void Search_Click(object sender, RoutedEventArgs e)
  376. {
  377. if (room1combo.Text == "")
  378. {
  379. MessageBox.Show("Выберите комнату для поиска!", "Предупреждение", MessageBoxButton.OK, MessageBoxImage.Information);
  380. }
  381. else
  382. {
  383. try
  384. {
  385. con.Open();
  386. string sql = "SELECT Number_Room, Client.LastName_Client, Rezervirovanie.Date_in,Rezervirovanie.Date_out From Room inner join Rezervirovanie on Room.ID_Room = Rezervirovanie.ID_Room inner join Client on Rezervirovanie.[ID_Client] = Client.ID_Client group by Number_Room, Client.LastName_Client, Rezervirovanie.Date_in,Rezervirovanie.Date_out having Number_Room = '"+room1combo.Text+"'";
  387. SqlDataAdapter dataAdapter = new SqlDataAdapter(sql, con);
  388. DataTable data = new DataTable("Rezervirovanie");
  389. dataAdapter.Fill(data);
  390. datarezerv.ItemsSource = data.DefaultView;
  391. dataAdapter.Update(data);
  392. con.Close();
  393. datarezerv.Columns[0].Header = "Номер комнаты";
  394. datarezerv.Columns[1].Header = "Фамилия";
  395. datarezerv.Columns[2].Header = "Дата прибытия";
  396. datarezerv.Columns[3].Header = "Дата отбытия";
  397. (datarezerv.Columns[2] as DataGridTextColumn).Binding.StringFormat = "dd/MM/yyyy";
  398. (datarezerv.Columns[3] as DataGridTextColumn).Binding.StringFormat = "dd/MM/yyyy";
  399. }
  400. catch (Exception ex)
  401. {
  402. con.Close();
  403. MessageBox.Show("Возникла ошибка! " + ex.ToString(), "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
  404. }
  405. }
  406. }
  407. //Обновление
  408. private void Refresh_Click(object sender, RoutedEventArgs e)
  409. {
  410. showgrid();
  411. roomcombo.Text = "";
  412. room1combo.Text = "";
  413. datein.SelectedDate = null;
  414. dateout.SelectedDate = null;
  415. roomcombo.IsEnabled = true;
  416. numbroomtxt.Text = "";
  417. clientcombo.IsEnabled = true;
  418. clientcombo.Text = "";
  419. }
  420. }
  421. }