160 lines
5.3 KiB
C#
160 lines
5.3 KiB
C#
using Contracts.BindingModels;
|
|
using Contracts.BusinessLogicsContracts;
|
|
using Contracts.SearchModels;
|
|
using DocumentFormat.OpenXml.Office2010.Excel;
|
|
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 WinForms
|
|
{
|
|
/// <summary>
|
|
/// Форма для создания или редактирования счета
|
|
/// </summary>
|
|
public partial class FormOrder : Form
|
|
{
|
|
/// <summary>
|
|
/// Бизнес-логика для сущности "Счет"
|
|
/// </summary>
|
|
private readonly IOrderLogic _orderLogic;
|
|
|
|
/// <summary>
|
|
/// Бизнес-логика для сущности "Тип заказа"
|
|
/// </summary>
|
|
private readonly IOrderTypeLogic _orderTypeLogic;
|
|
|
|
/// <summary>
|
|
/// Идентификатор счета
|
|
/// </summary>
|
|
private int? _id;
|
|
|
|
/// <summary>
|
|
/// Идентификатор счета
|
|
/// </summary>
|
|
public int Id { set { _id = value; } }
|
|
|
|
/// <summary>
|
|
/// Конструктор
|
|
/// </summary>
|
|
/// <param name="orderLogic"></param>
|
|
/// <param name="orderTypeLogic"></param>
|
|
public FormOrder(IOrderLogic orderLogic, IOrderTypeLogic orderTypeLogic)
|
|
{
|
|
InitializeComponent();
|
|
|
|
_orderLogic = orderLogic;
|
|
_orderTypeLogic = orderTypeLogic;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Загрузка формы
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void FormOrder_Load(object sender, EventArgs e)
|
|
{
|
|
var listOrderTypes = _orderTypeLogic.ReadList(null);
|
|
if (listOrderTypes != null)
|
|
{
|
|
foreach (var type in listOrderTypes)
|
|
{
|
|
customComboBox.AddItem(type.Name);
|
|
}
|
|
}
|
|
|
|
if (!_id.HasValue)
|
|
{
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
var order = _orderLogic.ReadElement(new OrderSearchModel { Id = _id.Value });
|
|
if (order == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
textBoxWaiterFullName.Text = order.WaiterFullName;
|
|
textBoxInfo.Text = order.Info;
|
|
customComboBox.SelectedItem = order.Type;
|
|
if (string.IsNullOrEmpty(order.Sum))
|
|
{
|
|
controlInputNullableDouble.Value = double.Parse(order.Sum!);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Кнопка "Сохранить"
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void buttonSave_Click(object sender, EventArgs e)
|
|
{
|
|
if (string.IsNullOrEmpty(textBoxWaiterFullName.Text))
|
|
{
|
|
MessageBox.Show("Заполните ФИО официанта!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
if (string.IsNullOrEmpty(textBoxInfo.Text))
|
|
{
|
|
MessageBox.Show("Заполните информацию по счету!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
if (string.IsNullOrEmpty(customComboBox.SelectedItem))
|
|
{
|
|
MessageBox.Show("Выберите тип заказа!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
var model = new OrderBindingModel
|
|
{
|
|
Id = _id ?? 0,
|
|
WaiterFullName = textBoxWaiterFullName.Text,
|
|
Info = textBoxInfo.Text,
|
|
Type = customComboBox.SelectedItem,
|
|
Sum = controlInputNullableDouble.Value,
|
|
};
|
|
|
|
var operatingResult = _id.HasValue ? _orderLogic.Update(model) : _orderLogic.Create(model);
|
|
if (!operatingResult)
|
|
{
|
|
throw new Exception("Ошибка при создании сущности 'Счет'!");
|
|
}
|
|
|
|
MessageBox.Show("Создание сущности 'Счет' прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
DialogResult = DialogResult.OK;
|
|
Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Кнопка "Отмена"
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void buttonCancel_Click(object sender, EventArgs e)
|
|
{
|
|
DialogResult = DialogResult.Cancel;
|
|
Close();
|
|
}
|
|
}
|
|
}
|