using ConstructionCompanyContracts.BusinessLogicContracts; using ConstructionCompanyContracts.ViewModels; using ConstructionCompanyContracts.BindingModels; using ConstructionCompanyContracts.SearchModels; using Microsoft.Extensions.Logging; using System; using System.Collections; 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 ConstructionCompanyView { public partial class FormMaterialOrder : Form { private readonly ILogger _logger; private readonly IOrderLogic _order; private readonly IMaterialLogic _material; private readonly IMaterialOrderLogic _materialOrder; private List? _listM; private List? _listO; private int _quantity = -1; public FormMaterialOrder(ILogger logger, IOrderLogic order, IMaterialLogic material, IMaterialOrderLogic materialOrder) { InitializeComponent(); _logger = logger; _order = order; _material = material; _materialOrder = materialOrder; } private void buttonCancel_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } private void buttonCreate_Click(object sender, EventArgs e) { if (comboBoxOrder.SelectedValue == null) { MessageBox.Show("Выберите заказ", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (comboBoxMaterial.SelectedValue == null) { MessageBox.Show("Выберите материал", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(textBoxQuantity.Text)) { MessageBox.Show("Заполните количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (Convert.ToInt32(textBoxQuantity.Text) > _quantity) { MessageBox.Show("На складе столько нет!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { var model = new MaterialOrderBindingModel { MaterialId = Convert.ToInt32(comboBoxMaterial.SelectedValue), OrderId = Convert.ToInt32(comboBoxOrder.SelectedValue), Quantity = Convert.ToInt32(textBoxQuantity.Text) }; var operationResult = _materialOrder.Create(model); if (!operationResult) { throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); } MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); var material = _material.ReadElement(new MaterialSearchModel { Id = Convert.ToInt32(comboBoxMaterial.SelectedValue) }); material.Quantity -= Convert.ToInt32(textBoxQuantity.Text); _material.Update(new MaterialBindingModel { Id = material.Id, MaterialName = material.MaterialName, Quantity = material.Quantity }); DialogResult = DialogResult.OK; Close(); } catch (Exception ex) { _logger.LogError(ex, "Ошибка сохранения компонента"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void FormMaterialOrder_Load(object sender, EventArgs e) { _logger.LogInformation("Загрузка материалов для заказа"); _listM = _material.ReadList(null); if (_listM != null) { comboBoxMaterial.DisplayMember = "MaterialName"; comboBoxMaterial.ValueMember = "Id"; comboBoxMaterial.DataSource = _listM; comboBoxMaterial.SelectedItem = null; } _listO = _order.ReadList(null); if (_listO != null) { comboBoxOrder.DisplayMember = "Adress"; comboBoxOrder.ValueMember = "Id"; comboBoxOrder.DataSource = _listO; comboBoxOrder.SelectedItem = null; } } private void comboBoxMaterial_SelectedValueChanged(object sender, EventArgs e) { if (comboBoxMaterial.SelectedValue == null) { labelQuantity.Text = "Единиц на складе: "; _quantity = -1; } else { var quantity = _material.ReadElement(new MaterialSearchModel { Id = Convert.ToInt32(comboBoxMaterial.SelectedValue) })?.Quantity; labelQuantity.Text = $"Единиц на складе: {(quantity.HasValue ? quantity.Value : "")}"; _quantity = quantity.HasValue ? quantity.Value : -1; } } } }