120 lines
3.9 KiB
C#
120 lines
3.9 KiB
C#
using ProjectCompRepair.Entities;
|
|
using ProjectCompRepair.Repositories;
|
|
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 ProjectCompRepair.Forms;
|
|
|
|
public partial class FormOrder : Form
|
|
{
|
|
private readonly IOrderRepository _orderRepository;
|
|
|
|
private int? _orderId;
|
|
|
|
public int Id
|
|
{
|
|
set
|
|
{
|
|
try
|
|
{
|
|
var order = _orderRepository.ReadOrderById(value);
|
|
if (order == null)
|
|
{
|
|
throw new InvalidOperationException(nameof(order));
|
|
}
|
|
|
|
textBoxName.Text = order.Name;
|
|
textBoxComent.Text = order.Coment;
|
|
dateTimePicker.Value = order.Date;
|
|
_orderId = value;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
public FormOrder(IOrderRepository orderRepository, IMasterRepository masterRepository, IAccessoriesRepository accessoriesRepository, IServiceRepository serviceRepository)
|
|
{
|
|
InitializeComponent();
|
|
_orderRepository = orderRepository ??
|
|
throw new ArgumentNullException(nameof(orderRepository));
|
|
|
|
comboBoxMaster.DataSource = masterRepository.ReadMaster();
|
|
comboBoxMaster.ValueMember = "Id";
|
|
comboBoxMaster.DisplayMember = "Name";
|
|
|
|
ColumnAccessories.DataSource = accessoriesRepository.ReadAccessories();
|
|
ColumnAccessories.DisplayMember = "AccessoriesType";
|
|
ColumnAccessories.ValueMember = "Id";
|
|
|
|
ColumnService.DataSource = serviceRepository.ReadService();
|
|
ColumnService.DisplayMember = "ServiceType";
|
|
ColumnService.ValueMember = "Id";
|
|
}
|
|
|
|
private void ButtonSave_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if(dataGridViewAccessories.RowCount < 1 || comboBoxMaster.SelectedIndex < 0 || dataGridViewServices.RowCount < 1)
|
|
{
|
|
throw new Exception("Имеются незаполненые поля");
|
|
}
|
|
_orderRepository.CreateOrder(Order.CreateElement(0, textBoxName.Text, textBoxComent.Text, (int)comboBoxMaster.SelectedValue!));
|
|
Close();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
Close();
|
|
}
|
|
|
|
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
|
|
|
|
private List<AccessoiresOrder> CreateListAccessoiresOrderFromDataGrid()
|
|
{
|
|
var list = new List<AccessoiresOrder>();
|
|
foreach (DataGridViewRow row in dataGridViewAccessories.Rows)
|
|
{
|
|
if (row.Cells["ColumnAccessoires"].Value == null ||
|
|
row.Cells["ColumnCount"].Value == null)
|
|
{
|
|
continue;
|
|
}
|
|
list.Add(AccessoiresOrder.CreateElement(0,
|
|
Convert.ToInt32(row.Cells["ColumnAccessoires"].Value),
|
|
Convert.ToInt32(row.Cells["ColumnCount"].Value)));
|
|
}
|
|
return list;
|
|
}
|
|
|
|
private List<ServicesOrder> CreateListServicesOrderFromDataGrid()
|
|
{
|
|
var list = new List<ServicesOrder>();
|
|
foreach (DataGridViewRow row in dataGridViewAccessories.Rows)
|
|
{
|
|
if (row.Cells["ColumnServices"].Value == null ||
|
|
row.Cells["ColumnCountServices"].Value == null)
|
|
{
|
|
continue;
|
|
}
|
|
list.Add(ServicesOrder.CreateElement(0,
|
|
Convert.ToInt32(row.Cells["ColumnAccessoires"].Value),
|
|
Convert.ToInt32(row.Cells["ColumnCount"].Value)));
|
|
}
|
|
return list;
|
|
}
|
|
|
|
}
|