191 lines
7.7 KiB
C#

using DressAtelierContracts.BindingModels;
using DressAtelierContracts.BusinessLogicContracts;
using DressAtelierDataModels.Enums;
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;
namespace SewingDresses
{
public partial class FormMain : Form
{
private readonly ILogger _logger;
private readonly IOrderLogic _orderLogic;
public FormMain(ILogger<FormMain> logger, IOrderLogic orderLogic)
{
InitializeComponent();
_logger = logger;
_orderLogic = orderLogic;
}
private void FormMain_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
_logger.LogInformation("Downloading orders");
try
{
var list = _orderLogic.ReadList(null);
if (list != null)
{
dataGridView.DataSource = list;
dataGridView.Columns["DressID"].Visible = false;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error downloading orders");
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void materialsToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormMaterials));
if (service is FormMaterials form)
{
form.ShowDialog();
}
}
private void dressesToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormDresses));
if (service is FormDresses form)
{
form.ShowDialog();
}
}
private void ButtonCreateOrder_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormOrderCreation));
if (service is FormOrderCreation form)
{
form.ShowDialog();
LoadData();
}
}
private void ButtonTakeOrderInWork_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["DressID"].Value);
_logger.LogInformation("Order №{id}. Changing status to 'In process'", id);
try
{
var operationResult = _orderLogic.TakeOrderInWork(new OrderBindingModel
{
ID = id,
DressID = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["DressID"].Value),
Status = Enum.Parse<OrderStatus>(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()),
Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value),
Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()),
DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()),
});
if (!operationResult)
{
MessageBox.Show("Order must be in 'Accepted' state.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw new Exception("Saving Error. Extra information in logs.");
}
LoadData();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error transferring order into process");
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
private void ButtonOrderReady_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["DressID"].Value);
_logger.LogInformation("Order №{id}. Changing status to 'Ready'", id);
try
{
var operationResult = _orderLogic.ReadyOrder(new OrderBindingModel
{
ID = id,
DressID = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["DressID"].Value),
Status = Enum.Parse<OrderStatus>(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()),
Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value),
Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()),
DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()),
});
if (!operationResult)
{
MessageBox.Show("Order must be in 'InProcess' state.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw new Exception("Saving error. Extra information in logs.");
}
LoadData();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in giving ready status to order");
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ButtonIssuedOrder_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["DressID"].Value);
_logger.LogInformation("Order №{id}. Changing status to 'Given'",id);
try
{
var operationResult = _orderLogic.GivenOrder(new OrderBindingModel
{
ID = id,
DressID = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["DressID"].Value),
Status = Enum.Parse<OrderStatus>(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()),
Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value),
Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()),
DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()),
});
if (!operationResult)
{
MessageBox.Show("Order must be in 'Ready' state.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw new Exception("Saving error. Extra information in logs.");
}
_logger.LogInformation("Order №{id} is given", id);
LoadData();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in giving 'Is given' status to order");
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
private void ButtonRef_Click(object sender, EventArgs e)
{
LoadData();
}
}
}