SUBD_PIbd-21_Balberova_D.N./BeautySaloon/BeautySaloonView/FormCreateOrder.cs
2023-03-29 20:00:53 +04:00

222 lines
8.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using BeautySaloonContracts.BindingModels;
using BeautySaloonContracts.BusinessLogicsContracts;
using BeautySaloonContracts.SearchModels;
using BeautySaloonDataModels;
namespace BeautySaloonView
{
public partial class FormCreateOrder : Form
{
private readonly IOrderLogic _logicO;
private readonly IEmployeeLogic _logicE;
private int? _idClient;
private int? _id;
private Dictionary<int, (TimeOnly, IServiceModel, int)> _orderServices;
public int IdClient { set { _idClient = value; } }
public int Id { set { _id = value; } }
public FormCreateOrder(IOrderLogic logic, IEmployeeLogic logicE)
{
InitializeComponent();
_logicO = logic;
_logicE = logicE;
_orderServices = new();
}
private void FormCreateOrder_Load(object sender, EventArgs e)
{
try
{
// продавцы
var list = _logicE.ReadList(new EmployeeSearchModel
{
Id = 1
});
if (list != null)
{
comboBoxEmployee.DisplayMember = "FIO";
comboBoxEmployee.ValueMember = "Id";
comboBoxEmployee.DataSource = list;
comboBoxEmployee.SelectedItem = null;
}
// загружаем заказ для изменения
if (_id.HasValue)
{
var view = _logicO.ReadElement(new OrderSearchModel
{
Id = _idClient.Value
});
if (view != null)
{
textBox.Text = view.Sum.ToString();
dateTimePicker.Text = view.Date.ToString();
comboBoxEmployee.SelectedValue = view.EmployeeId;
_orderServices = view.OrderServices ?? new();
LoadData();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка загрузки", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void LoadData()
{
// заполняем datagridview
try
{
if (_orderServices != null)
{
dataGridView.Rows.Clear();
foreach (var os in _orderServices)
{
var empl = _logicE.ReadElement(new EmployeeSearchModel
{
Id = os.Value.Item3
});
if (empl == null)
{
throw new ArgumentNullException("Не найден указанный сотрудник");
}
dataGridView.Rows.Add(new object[]
{
os.Key,
os.Value.Item2.Name,
os.Value.Item1,
empl.FIO,
});
}
textBox.Text = CalcPrice().ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonAdd_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormOrderServices));
if (service is FormOrderServices form)
{
if (form.ShowDialog() == DialogResult.OK)
{
if (form.ServiceModel == null)
{
return;
}
if (_orderServices.ContainsKey(form.IdService))
{
_orderServices[form.IdService] = (form.Date,
form.ServiceModel, form.IdEmployee);
}
else
{
_orderServices.Add(form.IdService,
(form.Date, form.ServiceModel, form.IdEmployee));
}
LoadData();
}
}
}
private void ButtonUpd_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
var service = Program.ServiceProvider?.GetService(typeof(FormOrderServices));
if (service is FormOrderServices form)
{
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value);
form.IdService = id;
form.Date = _orderServices[id].Item1;
form.IdEmployee = _orderServices[id].Item3;
if (form.ShowDialog() == DialogResult.OK)
{
if (form.ServiceModel == null)
{
return;
}
_orderServices[form.IdService] = (form.Date,
form.ServiceModel, form.IdEmployee);
LoadData();
}
}
}
}
private void ButtonDel_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
try
{
_orderServices?.Remove(Convert.ToInt32(dataGridView
.SelectedRows[0].Cells[0].Value));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
LoadData();
}
}
}
private void ButtonRef_Click(object sender, EventArgs e)
{
LoadData();
}
private void ButtonSave_Click(object sender, EventArgs e)
{
if (comboBoxEmployee.SelectedValue == null)
{
MessageBox.Show("Выберите продавца", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (_orderServices == null || _orderServices.Count == 0)
{
MessageBox.Show("Заполните услуги", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var model = new OrderBindingModel
{
Id = _id ?? 0,
Date = DateOnly.FromDateTime(dateTimePicker.Value),
Sum = Convert.ToDecimal(textBox.Text),
ClientId = _idClient.Value,
EmployeeId = Convert.ToInt32(comboBoxEmployee.SelectedValue),
OrderServices = _orderServices
};
var operationResult = _id.HasValue ? _logicO.Update(model) : _logicO.CreateOrder(model);
if (!operationResult)
{
throw new Exception("Ошибка при сохранении.");
}
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private decimal CalcPrice()
{
decimal price = 0;
foreach (var elem in _orderServices)
{
price += elem.Value.Item2?.Price ?? 0;
}
return Math.Round(price, 2);
}
}
}