OrderItemEditForm.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Windows.Forms;
  3. using RestaurantApp.DAL;
  4. using RestaurantApp.Models;
  5. namespace RestaurantApp
  6. {
  7. public partial class OrderItemEditForm : Form
  8. {
  9. private OrderRepository repo = new OrderRepository();
  10. private OrderItem item;
  11. public OrderItemEditForm(OrderItem orderItem)
  12. {
  13. InitializeComponent();
  14. this.item = orderItem;
  15. LoadData();
  16. btnSave.Click += BtnSave_Click;
  17. btnCancel.Click += (s, e) => DialogResult = DialogResult.Cancel;
  18. }
  19. private void LoadData()
  20. {
  21. lblDishName.Text = item.DishName;
  22. numQuantity.Value = item.Quantity;
  23. txtNotes.Text = item.Notes;
  24. // Выбираем статус в комбобоксе
  25. int index = cmbStatus.FindStringExact(item.Status);
  26. if (index >= 0) cmbStatus.SelectedIndex = index;
  27. // Блокируем изменение статуса для закрытых заказов
  28. // (можно добавить логику)
  29. }
  30. private void BtnSave_Click(object sender, EventArgs e)
  31. {
  32. try
  33. {
  34. // Обновляем количество
  35. if (numQuantity.Value != item.Quantity)
  36. {
  37. // Тут нужно обновить количество через SQL
  38. using (var conn = Database.GetConnection())
  39. using (var cmd = new Npgsql.NpgsqlCommand(
  40. "UPDATE order_item SET quantity = @q WHERE order_item_id = @id", conn))
  41. {
  42. cmd.Parameters.AddWithValue("q", (int)numQuantity.Value);
  43. cmd.Parameters.AddWithValue("id", item.OrderItemId);
  44. cmd.ExecuteNonQuery();
  45. }
  46. }
  47. // Обновляем статус
  48. if (cmbStatus.Text != item.Status)
  49. {
  50. repo.UpdateItemStatus(item.OrderItemId, cmbStatus.Text);
  51. }
  52. // Обновляем примечание
  53. if (txtNotes.Text != item.Notes)
  54. {
  55. using (var conn = Database.GetConnection())
  56. using (var cmd = new Npgsql.NpgsqlCommand(
  57. "UPDATE order_item SET notes = @n WHERE order_item_id = @id", conn))
  58. {
  59. cmd.Parameters.AddWithValue("n", string.IsNullOrEmpty(txtNotes.Text) ? (object)DBNull.Value : txtNotes.Text);
  60. cmd.Parameters.AddWithValue("id", item.OrderItemId);
  61. cmd.ExecuteNonQuery();
  62. }
  63. }
  64. DialogResult = DialogResult.OK;
  65. Close();
  66. }
  67. catch (Exception ex)
  68. {
  69. MessageBox.Show($"Ошибка: {ex.Message}");
  70. }
  71. }
  72. }
  73. }