| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- using System;
- using System.Windows.Forms;
- using RestaurantApp.DAL;
- using RestaurantApp.Models;
- namespace RestaurantApp
- {
- public partial class OrderItemEditForm : Form
- {
- private OrderRepository repo = new OrderRepository();
- private OrderItem item;
- public OrderItemEditForm(OrderItem orderItem)
- {
- InitializeComponent();
- this.item = orderItem;
- LoadData();
- btnSave.Click += BtnSave_Click;
- btnCancel.Click += (s, e) => DialogResult = DialogResult.Cancel;
- }
- private void LoadData()
- {
- lblDishName.Text = item.DishName;
- numQuantity.Value = item.Quantity;
- txtNotes.Text = item.Notes;
- // Выбираем статус в комбобоксе
- int index = cmbStatus.FindStringExact(item.Status);
- if (index >= 0) cmbStatus.SelectedIndex = index;
- // Блокируем изменение статуса для закрытых заказов
- // (можно добавить логику)
- }
- private void BtnSave_Click(object sender, EventArgs e)
- {
- try
- {
- // Обновляем количество
- if (numQuantity.Value != item.Quantity)
- {
- // Тут нужно обновить количество через SQL
- using (var conn = Database.GetConnection())
- using (var cmd = new Npgsql.NpgsqlCommand(
- "UPDATE order_item SET quantity = @q WHERE order_item_id = @id", conn))
- {
- cmd.Parameters.AddWithValue("q", (int)numQuantity.Value);
- cmd.Parameters.AddWithValue("id", item.OrderItemId);
- cmd.ExecuteNonQuery();
- }
- }
- // Обновляем статус
- if (cmbStatus.Text != item.Status)
- {
- repo.UpdateItemStatus(item.OrderItemId, cmbStatus.Text);
- }
- // Обновляем примечание
- if (txtNotes.Text != item.Notes)
- {
- using (var conn = Database.GetConnection())
- using (var cmd = new Npgsql.NpgsqlCommand(
- "UPDATE order_item SET notes = @n WHERE order_item_id = @id", conn))
- {
- cmd.Parameters.AddWithValue("n", string.IsNullOrEmpty(txtNotes.Text) ? (object)DBNull.Value : txtNotes.Text);
- cmd.Parameters.AddWithValue("id", item.OrderItemId);
- cmd.ExecuteNonQuery();
- }
- }
- DialogResult = DialogResult.OK;
- Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show($"Ошибка: {ex.Message}");
- }
- }
- }
- }
|