177 lines
6.2 KiB
C#
177 lines
6.2 KiB
C#
using AbstractFoodOrdersContracts.BindingModels;
|
|
using AbstractFoodOrdersContracts.BusinessLogicsContracts;
|
|
using AbstractFoodOrdersContracts.SearchModels;
|
|
using AbstractFoodOrdersContracts.ViewModels;
|
|
using AbstractFoodOrdersDataModels.Models;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace FoodOrders
|
|
{
|
|
public partial class FormCreateOrder : Form
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly IDishLogic _logicD;
|
|
private readonly IOrderLogic _logicO;
|
|
private readonly IClientLogic _logicC;
|
|
private List<DishViewModel>? _listD;
|
|
public int Id
|
|
{
|
|
get
|
|
{
|
|
return
|
|
Convert.ToInt32(comboBoxDish.SelectedValue);
|
|
}
|
|
set
|
|
{
|
|
comboBoxDish.SelectedValue = value;
|
|
}
|
|
}
|
|
public IDishModel? DishModel
|
|
{
|
|
get
|
|
{
|
|
if (_listD == null)
|
|
{
|
|
return null;
|
|
}
|
|
foreach (var elem in _listD)
|
|
{
|
|
if (elem.Id == Id)
|
|
{
|
|
return elem;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
public int Count { get { return Convert.ToInt32(textBoxCount.Text); } set { textBoxCount.Text = value.ToString(); } }
|
|
public FormCreateOrder(ILogger<FormCreateOrder> logger, IDishLogic
|
|
logicD, IOrderLogic logicO, IClientLogic logicC)
|
|
{
|
|
InitializeComponent();
|
|
_logger = logger;
|
|
_logicD = logicD;
|
|
_logicO = logicO;
|
|
_logicC = logicC;
|
|
}
|
|
private void FormCreateOrder_Load(object sender, EventArgs e)
|
|
{
|
|
_logger.LogInformation("Загрузка изделий для заказа");
|
|
_listD = _logicD.ReadList(null);
|
|
|
|
if (_listD != null)
|
|
{
|
|
comboBoxDish.DisplayMember = "DishName";
|
|
comboBoxDish.ValueMember = "Id";
|
|
comboBoxDish.DataSource = _listD;
|
|
comboBoxDish.SelectedItem = null;
|
|
}
|
|
_logger.LogInformation("Загрузка клиентов для заказа");
|
|
var _listC = _logicC.ReadList(null);
|
|
|
|
if (_listC != null)
|
|
{
|
|
comboBoxClient.DisplayMember = "ClientFio";
|
|
comboBoxClient.ValueMember = "Id";
|
|
comboBoxClient.DataSource = _listC;
|
|
comboBoxClient.SelectedItem = null;
|
|
}
|
|
}
|
|
private void CalcSum()
|
|
{
|
|
if (comboBoxDish.SelectedValue != null &&
|
|
!string.IsNullOrEmpty(textBoxCount.Text))
|
|
{
|
|
try
|
|
{
|
|
int id = Convert.ToInt32(comboBoxDish.SelectedValue);
|
|
var dish = _logicD.ReadElement(new DishSearchModel
|
|
{
|
|
Id = id
|
|
});
|
|
int count = Convert.ToInt32(textBoxCount.Text);
|
|
textBoxSum.Text = Math.Round(count * (dish?.Price ?? 0),
|
|
2).ToString();
|
|
_logger.LogInformation("Расчет суммы заказа");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка расчета суммы заказа");
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
|
|
MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
}
|
|
private void TextBoxCount_TextChanged(object sender, EventArgs e)
|
|
{
|
|
CalcSum();
|
|
}
|
|
private void ComboBoxProduct_SelectedIndexChanged(object sender,
|
|
EventArgs e)
|
|
{
|
|
CalcSum();
|
|
}
|
|
private void ButtonSave_Click(object sender, EventArgs e)
|
|
{
|
|
if (string.IsNullOrEmpty(textBoxCount.Text))
|
|
{
|
|
MessageBox.Show("Заполните поле Количество", "Ошибка",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
if (comboBoxDish.SelectedValue == null)
|
|
{
|
|
MessageBox.Show("Выберите изделие", "Ошибка",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
if (comboBoxClient.SelectedValue == null)
|
|
{
|
|
MessageBox.Show("Выберите клиента", "Ошибка",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
_logger.LogInformation("Создание заказа");
|
|
try
|
|
{
|
|
var operationResult = _logicO.CreateOrder(new OrderBindingModel
|
|
{
|
|
DishId = Convert.ToInt32(comboBoxDish.SelectedValue),
|
|
ClientId = Convert.ToInt32(comboBoxClient.SelectedValue),
|
|
Count = Convert.ToInt32(textBoxCount.Text),
|
|
Sum = Convert.ToDouble(textBoxSum.Text)
|
|
});
|
|
if (!operationResult)
|
|
{
|
|
throw new Exception("Ошибка при создании заказа. Дополнительная информация в логах.");
|
|
}
|
|
MessageBox.Show("Сохранение прошло успешно", "Сообщение",
|
|
MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
DialogResult = DialogResult.OK;
|
|
Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка создания заказа");
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
|
|
MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
private void ButtonCancel_Click(object sender, EventArgs e)
|
|
{
|
|
DialogResult = DialogResult.Cancel;
|
|
Close();
|
|
}
|
|
|
|
}
|
|
}
|