PIbd-22_Petrushin_E.A._LawFirm/LawFirm/LawFirmView/FormCreateOrder.cs

167 lines
6.3 KiB
C#
Raw Normal View History

2024-02-27 19:44:44 +04:00
using AbstractLawFirmContracts.BusinessLogicsContracts;
2024-02-11 18:24:54 +04:00
using Microsoft.Extensions.Logging;
2024-02-27 19:44:44 +04:00
using AbstractLawFirmContracts.SearchModels;
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;
using AbstractLawFirmContracts.BindingModels;
2024-02-11 18:24:54 +04:00
2024-02-14 00:11:21 +04:00
namespace LawFirmView
2024-02-11 18:24:54 +04:00
{
public partial class FormCreateOrder : Form
{
private readonly ILogger _logger;
2024-02-27 19:44:44 +04:00
private readonly IDocumentLogic _logicD;
2024-02-11 18:24:54 +04:00
private readonly IOrderLogic _logicO;
private readonly IClientLogic _logicC;
public FormCreateOrder(ILogger<FormCreateOrder> logger, IDocumentLogic logicD, IOrderLogic logicO, IClientLogic logicC)
2024-02-11 18:24:54 +04:00
{
InitializeComponent();
_logger = logger;
2024-02-27 19:44:44 +04:00
_logicD = logicD;
2024-02-11 18:24:54 +04:00
_logicO = logicO;
_logicC = logicC;
2024-02-11 18:24:54 +04:00
}
private void FormCreateOrder_Load(object sender, EventArgs e)
{
2024-02-27 19:44:44 +04:00
_logger.LogInformation("Загрузка пакетов документов для заказа");
try
{
var list = _logicD.ReadList(null);
if (list != null)
{
comboBoxDocument.DisplayMember = "DocumentName";
comboBoxDocument.ValueMember = "Id";
comboBoxDocument.DataSource = list;
comboBoxDocument.SelectedItem = null;
}
_logger.LogInformation("Пакеты документов загружены");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки пакетов документов");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
_logger.LogInformation("Загрузка клиентов для заказа");
try
{
var list = _logicC.ReadList(null);
if (list != null)
{
comboBoxClient.DisplayMember = "ClientFIO";
comboBoxClient.ValueMember = "Id";
comboBoxClient.DataSource = list;
comboBoxClient.SelectedItem = null;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки списка клиентов");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
2024-02-11 18:24:54 +04:00
}
private void CalcSum()
{
2024-02-27 19:44:44 +04:00
if (comboBoxDocument.SelectedValue != null &&
!string.IsNullOrEmpty(textBoxCount.Text))
2024-02-11 18:24:54 +04:00
{
try
{
2024-02-27 19:44:44 +04:00
int id = Convert.ToInt32(comboBoxDocument.SelectedValue);
var product = _logicD.ReadElement(new DocumentSearchModel
{
Id
= id
});
2024-02-11 18:24:54 +04:00
int count = Convert.ToInt32(textBoxCount.Text);
2024-02-27 19:44:44 +04:00
textBoxSum.Text = Math.Round(count * (product?.Price ?? 0),
2).ToString();
2024-02-11 18:24:54 +04:00
_logger.LogInformation("Расчет суммы заказа");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка расчета суммы заказа");
2024-02-27 19:44:44 +04:00
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
2024-02-11 18:24:54 +04:00
}
}
}
2024-02-27 19:44:44 +04:00
private void textBoxCount_TextChanged(object sender, EventArgs e)
{
CalcSum();
}
private void comboBoxDocument_SelectedIndexChanged(object sender, EventArgs e)
2024-02-11 18:24:54 +04:00
{
CalcSum();
}
2024-02-27 19:44:44 +04:00
private void textBoxSum_TextChanged(object sender, EventArgs e)
2024-02-11 18:24:54 +04:00
{
CalcSum();
}
2024-02-27 19:44:44 +04:00
private void buttonSave_Click(object sender, EventArgs e)
2024-02-11 18:24:54 +04:00
{
if (string.IsNullOrEmpty(textBoxCount.Text))
{
2024-02-27 19:44:44 +04:00
MessageBox.Show("Заполните поле Количество", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
2024-02-11 18:24:54 +04:00
return;
}
2024-02-27 19:44:44 +04:00
if (comboBoxDocument.SelectedValue == null)
2024-02-11 18:24:54 +04:00
{
2024-02-27 19:44:44 +04:00
MessageBox.Show("Выберите пакет документов", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
2024-02-11 18:24:54 +04:00
return;
}
if (comboBoxClient.SelectedValue == null)
{
MessageBox.Show("Выберите клиента", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
2024-02-11 18:24:54 +04:00
_logger.LogInformation("Создание заказа");
try
{
var operationResult = _logicO.CreateOrder(new OrderBindingModel
{
2024-02-27 19:44:44 +04:00
DocumentId = Convert.ToInt32(comboBoxDocument.SelectedValue),
ClientId = Convert.ToInt32(comboBoxClient.SelectedValue),
2024-02-11 18:24:54 +04:00
Count = Convert.ToInt32(textBoxCount.Text),
Sum = Convert.ToDouble(textBoxSum.Text)
});
if (!operationResult)
{
throw new Exception("Ошибка при создании заказа. Дополнительная информация в логах.");
}
2024-02-27 19:44:44 +04:00
MessageBox.Show("Сохранение прошло успешно", "Сообщение",
MessageBoxButtons.OK, MessageBoxIcon.Information);
2024-02-11 18:24:54 +04:00
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка создания заказа");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
2024-02-27 19:44:44 +04:00
2024-02-11 18:24:54 +04:00
}
2024-02-27 19:44:44 +04:00
private void buttonCancel_Click(object sender, EventArgs e)
2024-02-11 18:24:54 +04:00
{
DialogResult = DialogResult.Cancel;
Close();
}
}
2024-02-27 19:44:44 +04:00
}