143 lines
5.4 KiB
C#
143 lines
5.4 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 NeshinaPolina07_11
|
|
{
|
|
public partial class AddOrder : Form
|
|
{
|
|
private readonly MySqlConnection _connection;
|
|
|
|
public AddOrder()
|
|
{
|
|
InitializeComponent();
|
|
_connection = DB.GetInstance().GetConnection();
|
|
LoadPickupPoints();
|
|
LoadUsers();
|
|
LoadStatuses();
|
|
dtpOrderDate.Value = DateTime.Now;
|
|
dtpDeliveryDate.Value = DateTime.Now.AddDays(3);
|
|
}
|
|
|
|
private void LoadPickupPoints()
|
|
{
|
|
LoadComboBox("pickup_points", "point_id", "address", cmbPickupPoint);
|
|
}
|
|
|
|
private void LoadUsers()
|
|
{
|
|
LoadComboBox("users", "user_id", "full_name", cmbUser);
|
|
}
|
|
|
|
private void LoadStatuses()
|
|
{
|
|
LoadComboBox("order_statuses", "status_id", "status_name", cmbStatus);
|
|
}
|
|
|
|
private void LoadComboBox(string table, string idCol, string nameCol, ComboBox combo)
|
|
{
|
|
string query = $"SELECT {idCol}, {nameCol} FROM {table} ORDER BY {nameCol}";
|
|
using (var cmd = new MySqlCommand(query, _connection))
|
|
using (var adapter = new MySqlDataAdapter(cmd))
|
|
{
|
|
var dt = new DataTable();
|
|
adapter.Fill(dt);
|
|
combo.DataSource = dt;
|
|
combo.DisplayMember = nameCol;
|
|
combo.ValueMember = idCol;
|
|
combo.SelectedIndex = -1;
|
|
}
|
|
}
|
|
|
|
private void label1_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void btnSave_Click(object sender, EventArgs e)
|
|
{
|
|
if (cmbPickupPoint.SelectedValue == null)
|
|
{
|
|
MessageBox.Show("Выберите пункт выдачи.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
if (cmbUser.SelectedValue == null)
|
|
{
|
|
MessageBox.Show("Выберите пользователя.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
if (cmbStatus.SelectedValue == null)
|
|
{
|
|
MessageBox.Show("Выберите статус заказа.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
if (string.IsNullOrWhiteSpace(txtReceiveCode.Text))
|
|
{
|
|
MessageBox.Show("Введите код получения.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
if (!string.IsNullOrWhiteSpace(txtReceiveCode.Text))
|
|
{
|
|
if (!int.TryParse(txtReceiveCode.Text, out int quantity) || quantity < 0)
|
|
{
|
|
MessageBox.Show("Введите код получения цифрами", "Некорректный код", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
txtReceiveCode.Focus();
|
|
return;
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
string getMaxIdQuery = "SELECT MAX(order_id) FROM orders";
|
|
using (var cmd = new MySqlCommand(getMaxIdQuery, _connection))
|
|
{
|
|
object result = cmd.ExecuteScalar();
|
|
int nextOrderId = result == DBNull.Value ? 1 : Convert.ToInt32(result) + 1;
|
|
|
|
string query = @"
|
|
INSERT INTO orders (order_id, order_date, delivery_date, pickup_point_id, user_id, receive_code, status_id)
|
|
VALUES (@order_id, @order_date, @delivery_date, @pickup_point_id, @user_id, @receive_code, @status_id)";
|
|
|
|
using (var insertCmd = new MySqlCommand(query, _connection))
|
|
{
|
|
insertCmd.Parameters.AddWithValue("@order_id", nextOrderId);
|
|
insertCmd.Parameters.AddWithValue("@order_date", dtpOrderDate.Value);
|
|
insertCmd.Parameters.AddWithValue("@delivery_date", dtpDeliveryDate.Value);
|
|
insertCmd.Parameters.AddWithValue("@pickup_point_id", cmbPickupPoint.SelectedValue);
|
|
insertCmd.Parameters.AddWithValue("@user_id", cmbUser.SelectedValue);
|
|
insertCmd.Parameters.AddWithValue("@receive_code", txtReceiveCode.Text.Trim());
|
|
insertCmd.Parameters.AddWithValue("@status_id", cmbStatus.SelectedValue);
|
|
|
|
insertCmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
|
|
MessageBox.Show("Заказ успешно добавлен.", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
DialogResult = DialogResult.OK;
|
|
Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show($"Ошибка при добавлении заказа: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private void label2_Click(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void cmbStatus_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|