AccountingWarehouseProducts.../AccountingWarehouseProducts/AccountingWarehouseProductsView/FormCreateOrder.cs

170 lines
6.0 KiB
C#
Raw Normal View History

2024-05-01 22:21:11 +04:00
using AccountingWarehouseProductsBusinessLogic.BusinessLogic;
using AccountingWarehouseProductsContracts.BindingModels;
using AccountingWarehouseProductsContracts.BusinessLogicsContracts;
using AccountingWarehouseProductsContracts.SearchModels;
using AccountingWarehouseProductsDatabaseImplement.Models;
using AccountingWarehouseProductsDataModels.Enums;
using AccountingWarehouseProductsDataModels.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;
using System.Windows.Forms.VisualStyles;
namespace AccountingWarehouseProductsView
{
public partial class FormCreateOrder : Form
{
private readonly ISupplierLogic _logicS;
private readonly IProductLogic _logicP;
private readonly IOrderLogic _logicO;
private int? _id;
private Dictionary<int, (IProductModel, int)> _orderProduct;
public int Id { set { _id = value; } }
public FormCreateOrder(ISupplierLogic logicS, IOrderLogic logicO, IProductLogic logicP)
{
InitializeComponent();
_logicS = logicS;
_logicP = logicP;
_logicO = logicO;
_orderProduct = new Dictionary<int, (IProductModel, int)>();
LoadData();
}
private void FormCreateOrder_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
try
{
var list = _logicP.ReadList(null);
if (list != null)
{
comboBoxProduct.DisplayMember = "ProductName";
comboBoxProduct.ValueMember = "Id";
comboBoxProduct.DataSource = list;
comboBoxProduct.SelectedItem = null;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
try
{
var list = _logicS.ReadList(null);
if (list != null)
{
comboBoxSupplier.DisplayMember = "SupplierName";
comboBoxSupplier.ValueMember = "Id";
comboBoxSupplier.DataSource = list;
comboBoxSupplier.SelectedItem = null;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void CalcSum()
{
if (comboBoxProduct.SelectedValue != null && !string.IsNullOrEmpty(textBoxCount.Text))
{
try
{
int id = Convert.ToInt32(comboBoxProduct.SelectedValue);
var product = _logicP.ReadElement(new ProductSearchModel { Id = id });
int count = Convert.ToInt32(textBoxCount.Text);
textBoxSum.Text = Math.Round(count * (product?.Cost ?? 0), 2).ToString();
}
catch (Exception 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 (comboBoxProduct.SelectedValue == null)
{
MessageBox.Show("Выберите продукт", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxSupplier.SelectedValue == null)
{
MessageBox.Show("Выберите поставщика", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
var model = new OrderBindingModel
{
Id = _id ?? 0,
ProductId = Convert.ToInt32(comboBoxProduct.SelectedValue),
SupplierId = Convert.ToInt32(comboBoxSupplier.SelectedValue),
DateofOrder = DateTime.Now,
Status = OrderStatus.Заказан,
ProductName = comboBoxProduct.Text,
Count = Convert.ToInt32(textBoxCount.Text),
SupplierName = comboBoxSupplier.Text,
Sum = Convert.ToDouble(textBoxSum.Text),
OrderProduct = _orderProduct
};
var operationResult = _id.HasValue ? _logicO.Update(model) : _logicO.Create(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();
}
}
}