OrderEditForm.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.Windows.Forms;
  2. namespace TradeApp.WinForms.V08;
  3. public sealed partial class OrderEditForm : Form
  4. {
  5. private readonly DatabaseClient _db;
  6. private readonly OrderView _order;
  7. private readonly Dictionary<int, string> _pickupPoints;
  8. public OrderEditForm(DatabaseClient db, OrderView? order)
  9. {
  10. _db = db;
  11. _order = order ?? new OrderView();
  12. _pickupPoints = db.LoadPickupPoints();
  13. InitializeComponent();
  14. Text = order is null ? "Добавление заказа" : "Редактирование заказа";
  15. LoadLookups();
  16. Fill();
  17. }
  18. private void SaveButton_Click(object? sender, EventArgs e)
  19. {
  20. if (articleComboBox.SelectedItem is null || pickupPointComboBox.SelectedItem is not PickupPointItem point || string.IsNullOrWhiteSpace(statusComboBox.Text) || customerComboBox.SelectedItem is null)
  21. {
  22. MessageBox.Show("Заполните артикул, клиента, статус и пункт выдачи.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  23. return;
  24. }
  25. _order.Status = statusComboBox.Text.Trim();
  26. _order.Customer = customerComboBox.Text.Trim();
  27. _order.ReceiveCode = receiveCodeTextBox.Text.Trim();
  28. _order.OrderDate = orderDatePicker.Checked ? orderDatePicker.Value.Date : null;
  29. _order.DeliveryDate = deliveryDatePicker.Checked ? deliveryDatePicker.Value.Date : null;
  30. _db.SaveOrder(_order, point.Id, articleComboBox.Text, (int)quantityNumeric.Value, customerComboBox.Text);
  31. DialogResult = DialogResult.OK;
  32. }
  33. private void LoadLookups()
  34. {
  35. articleComboBox.Items.AddRange(_db.LoadProductArticles().Cast<object>().ToArray());
  36. statusComboBox.Items.AddRange(_db.LoadStatuses().Cast<object>().ToArray());
  37. customerComboBox.Items.AddRange(_db.LoadUsers().Cast<object>().ToArray());
  38. foreach (var item in _pickupPoints)
  39. {
  40. pickupPointComboBox.Items.Add(new PickupPointItem(item.Key, item.Value));
  41. }
  42. }
  43. private void Fill()
  44. {
  45. var firstItem = _order.OrderId == 0 ? null : _db.LoadFirstOrderItem(_order.OrderId);
  46. if (firstItem is not null)
  47. {
  48. articleComboBox.Text = firstItem.Value.Article;
  49. quantityNumeric.Value = firstItem.Value.Quantity;
  50. }
  51. else if (articleComboBox.Items.Count > 0)
  52. {
  53. articleComboBox.SelectedIndex = 0;
  54. }
  55. statusComboBox.Text = string.IsNullOrWhiteSpace(_order.Status) ? FirstText(statusComboBox) : _order.Status;
  56. customerComboBox.Text = string.IsNullOrWhiteSpace(_order.Customer) ? FirstText(customerComboBox) : _order.Customer;
  57. receiveCodeTextBox.Text = string.IsNullOrWhiteSpace(_order.ReceiveCode) ? "000" : _order.ReceiveCode;
  58. SelectPickupPoint();
  59. SetDate(orderDatePicker, _order.OrderDate);
  60. SetDate(deliveryDatePicker, _order.DeliveryDate);
  61. }
  62. private void SelectPickupPoint()
  63. {
  64. for (var i = 0; i < pickupPointComboBox.Items.Count; i++)
  65. {
  66. if (pickupPointComboBox.Items[i] is PickupPointItem item && item.Address == _order.PickupPoint)
  67. {
  68. pickupPointComboBox.SelectedIndex = i;
  69. return;
  70. }
  71. }
  72. if (pickupPointComboBox.Items.Count > 0)
  73. {
  74. pickupPointComboBox.SelectedIndex = 0;
  75. }
  76. }
  77. private static string FirstText(ComboBox comboBox)
  78. {
  79. return comboBox.Items.Count > 0 ? comboBox.Items[0]?.ToString() ?? "" : "";
  80. }
  81. private static void SetDate(DateTimePicker picker, DateTime? value)
  82. {
  83. picker.Checked = value.HasValue;
  84. if (value.HasValue)
  85. {
  86. picker.Value = value.Value;
  87. }
  88. }
  89. private sealed record PickupPointItem(int Id, string Address)
  90. {
  91. public override string ToString() => Address;
  92. }
  93. }