142 lines
5.2 KiB
C#
142 lines
5.2 KiB
C#
using MySql.Data.MySqlClient;
|
|
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 NeshinaPolina2111
|
|
{
|
|
public partial class MaterialPurchaseCalculatorForm : Form
|
|
{
|
|
public MaterialPurchaseCalculatorForm()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void MaterialPurchaseCalculatorForm_Load(object sender, EventArgs e)
|
|
{
|
|
LoadProductTypes();
|
|
LoadMaterialTypes();
|
|
lblResult.Text = "";
|
|
}
|
|
|
|
private void LoadProductTypes()
|
|
{
|
|
string query = "SELECT idProduct_type_import, TypeProduction, Coefficient FROM Product_type_import ORDER BY TypeProduction";
|
|
LoadComboBox(cmbProductType, query, "TypeProduction", "idProduct_type_import");
|
|
}
|
|
|
|
private void LoadMaterialTypes()
|
|
{
|
|
string query = "SELECT idMaterial_type_import, Name, Procent FROM Material_type_import ORDER BY Name";
|
|
LoadComboBox(cmbMaterialType, query, "Name", "idMaterial_type_import");
|
|
}
|
|
|
|
private void LoadComboBox(ComboBox comboBox, string query, string displayMember, string valueMember)
|
|
{
|
|
var conn = DB.GetInstance().GetConnection();
|
|
if (conn.State == ConnectionState.Closed)
|
|
conn.Open();
|
|
|
|
using (var cmd = new MySqlCommand(query, conn))
|
|
{
|
|
var adapter = new MySqlDataAdapter(cmd);
|
|
var dt = new DataTable();
|
|
adapter.Fill(dt);
|
|
|
|
comboBox.DisplayMember = displayMember;
|
|
comboBox.ValueMember = valueMember;
|
|
comboBox.DataSource = dt;
|
|
}
|
|
}
|
|
|
|
private bool TryParsePositiveDecimal(string input, out decimal result)
|
|
{
|
|
result = 0;
|
|
if (string.IsNullOrWhiteSpace(input))
|
|
return false;
|
|
if (!decimal.TryParse(input.Replace('.', ','), out result))
|
|
return false;
|
|
return result > 0;
|
|
}
|
|
|
|
private bool TryParseNonNegativeDecimal(string input, out decimal result)
|
|
{
|
|
result = 0;
|
|
if (string.IsNullOrWhiteSpace(input))
|
|
return false;
|
|
if (!decimal.TryParse(input.Replace('.', ','), out result))
|
|
return false;
|
|
return result >= 0;
|
|
}
|
|
|
|
private void btnCalculate_Click(object sender, EventArgs e)
|
|
{
|
|
// 1. Проверка выбора типов
|
|
if (cmbProductType.SelectedValue == null || cmbMaterialType.SelectedValue == null)
|
|
{
|
|
MessageBox.Show("Выберите тип продукции и тип материала.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
|
|
int productTypeId = Convert.ToInt32(cmbProductType.SelectedValue);
|
|
int materialTypeId = Convert.ToInt32(cmbMaterialType.SelectedValue);
|
|
int quantityProduced = (int)numQuantityProduced.Value;
|
|
|
|
// 2. Проверка параметров
|
|
decimal param1, param2, stock;
|
|
if (!TryParsePositiveDecimal(txtParam1.Text, out param1))
|
|
{
|
|
MessageBox.Show("Параметр 1 должен быть положительным числом.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
|
|
if (!TryParsePositiveDecimal(txtParam2.Text, out param2))
|
|
{
|
|
MessageBox.Show("Параметр 2 должен быть положительным числом.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
|
|
if (!TryParseNonNegativeDecimal(txtStock.Text, out stock))
|
|
{
|
|
MessageBox.Show("Количество на складе не может быть отрицательным.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
return;
|
|
}
|
|
|
|
// 3. Вызов метода расчёта
|
|
int result = DB.CalculateMaterialToPurchase(
|
|
productTypeId,
|
|
materialTypeId,
|
|
quantityProduced,
|
|
param1,
|
|
param2,
|
|
stock
|
|
);
|
|
|
|
// 4. Обработка результата
|
|
if (result == -1)
|
|
{
|
|
lblResult.Text = "❌ Ошибка: не найдены типы продукции или материала.";
|
|
lblResult.ForeColor = Color.Red;
|
|
}
|
|
else if (result == 0)
|
|
{
|
|
lblResult.Text = "✅ Материал на складе достаточен — ничего докупать не нужно.";
|
|
lblResult.ForeColor = Color.Green;
|
|
}
|
|
else
|
|
{
|
|
lblResult.Text = $"🔹 Требуется докупить материала: {result} ед.";
|
|
lblResult.ForeColor = Color.Blue;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|