PaymentForm.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Data;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5. using RestaurantApp.DAL;
  6. using RestaurantApp.Models;
  7. namespace RestaurantApp
  8. {
  9. public partial class PaymentForm : Form
  10. {
  11. private OrderRepository repo = new OrderRepository();
  12. private ClientRepository clientRepo = new ClientRepository();
  13. private int orderId;
  14. private decimal totalAmount;
  15. private int? clientId;
  16. private int availableBonus = 0;
  17. public PaymentForm(int orderId, decimal totalAmount)
  18. {
  19. InitializeComponent();
  20. this.orderId = orderId;
  21. this.totalAmount = totalAmount;
  22. LoadClientInfo();
  23. LoadData();
  24. cmbMethod.SelectedIndex = 0;
  25. cmbMethod.SelectedIndexChanged += (s, e) => UpdateFinalAmount();
  26. numBonus.ValueChanged += (s, e) => UpdateFinalAmount();
  27. btnPay.Click += BtnPay_Click;
  28. btnCancel.Click += (s, e) => DialogResult = DialogResult.Cancel;
  29. }
  30. private void LoadClientInfo()
  31. {
  32. // Получаем клиента из заказа
  33. var orders = repo.GetAll("", "");
  34. var order = orders.FirstOrDefault(o => o.OrderId == orderId);
  35. if (order != null && order.ClientId.HasValue)
  36. {
  37. clientId = order.ClientId;
  38. var client = clientRepo.GetById(clientId.Value);
  39. if (client != null)
  40. {
  41. availableBonus = client.BonusPoints;
  42. lblBonusInfo.Text = $"(доступно: {availableBonus})";
  43. numBonus.Maximum = Math.Min(availableBonus, (int)(totalAmount / 10)); // 1 бонус = 10 руб
  44. }
  45. else
  46. {
  47. lblBonusInfo.Text = "(нет бонусов)";
  48. numBonus.Enabled = false;
  49. }
  50. }
  51. else
  52. {
  53. lblBonusInfo.Text = "(без клиента)";
  54. numBonus.Enabled = false;
  55. }
  56. }
  57. private void LoadData()
  58. {
  59. lblOrderIdValue.Text = orderId.ToString();
  60. lblAmountValue.Text = $"{totalAmount:N2} ₽";
  61. UpdateFinalAmount();
  62. }
  63. private void UpdateFinalAmount()
  64. {
  65. decimal bonusDiscount = numBonus.Value * 10; // 1 бонус = 10 рублей
  66. decimal finalAmount = totalAmount - bonusDiscount;
  67. if (finalAmount < 0) finalAmount = 0;
  68. lblFinalAmountValue.Text = $"{finalAmount:N2} ₽";
  69. }
  70. private void BtnPay_Click(object sender, EventArgs e)
  71. {
  72. decimal bonusDiscount = numBonus.Value * 10;
  73. decimal finalAmount = totalAmount - bonusDiscount;
  74. if (finalAmount < 0) finalAmount = 0;
  75. if (finalAmount <= 0 && cmbMethod.Text != "bonus")
  76. {
  77. MessageBox.Show("Сумма к оплате 0. Выберите оплату бонусами");
  78. return;
  79. }
  80. try
  81. {
  82. repo.AddPayment(orderId, finalAmount, cmbMethod.Text);
  83. // Обновляем статус заказа
  84. repo.UpdateOrderStatus(orderId, "closed");
  85. DialogResult = DialogResult.OK;
  86. Close();
  87. }
  88. catch (Exception ex)
  89. {
  90. MessageBox.Show($"Ошибка оплаты: {ex.Message}");
  91. }
  92. }
  93. }
  94. }