2024-11-15 18:27:06 +04:00
|
|
|
|
using GasStation.Entities;
|
|
|
|
|
using GasStation.Repositories;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace GasStation.Forms
|
|
|
|
|
{
|
|
|
|
|
public partial class FormSelling : Form
|
|
|
|
|
{
|
|
|
|
|
private readonly ISellingRepository _sellingRepository;
|
|
|
|
|
|
|
|
|
|
public FormSelling(ISellingRepository sellingRepository, IGasmanRepository gasmanRepository, IProductRepository productRepository)
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
_sellingRepository = sellingRepository ??
|
|
|
|
|
throw new ArgumentNullException(nameof(sellingRepository));
|
|
|
|
|
|
|
|
|
|
comboBoxGasman.DataSource = gasmanRepository.ReadGasman();
|
|
|
|
|
comboBoxGasman.DisplayMember = "GasmanName";
|
|
|
|
|
comboBoxGasman.ValueMember = "ID";
|
|
|
|
|
|
2024-12-02 14:38:24 +04:00
|
|
|
|
ColumnProduct.DataSource = productRepository.ReadProduct();
|
2024-12-21 15:34:40 +04:00
|
|
|
|
ColumnProduct.DisplayMember = "ProductName";
|
2024-12-02 14:38:24 +04:00
|
|
|
|
ColumnProduct.ValueMember = "ID";
|
2024-11-15 18:27:06 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ButtonSave_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (comboBoxGasman.SelectedIndex < 0 ||
|
2024-12-02 14:38:24 +04:00
|
|
|
|
dataGridViewSelling.RowCount < 1)
|
2024-11-15 18:27:06 +04:00
|
|
|
|
{
|
|
|
|
|
throw new Exception("Имеются незаполненые поля");
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-21 10:43:19 +04:00
|
|
|
|
_sellingRepository.CreateSelling(Selling.CreateSelling(0, (int)comboBoxGasman.SelectedValue!, CreateProductSellingsFromDataGrid()));
|
2024-11-15 18:27:06 +04:00
|
|
|
|
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
|
2024-12-02 14:38:24 +04:00
|
|
|
|
|
|
|
|
|
private List<ProductSelling> CreateProductSellingsFromDataGrid()
|
|
|
|
|
{
|
|
|
|
|
var list = new List<ProductSelling>();
|
|
|
|
|
foreach (DataGridViewRow row in dataGridViewSelling.Rows)
|
|
|
|
|
{
|
|
|
|
|
if (row.Cells["ColumnProduct"].Value == null || row.Cells["ColumnCount"].Value == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list.Add(ProductSelling.CreateSelling(0, Convert.ToInt32(row.Cells["ColumnProduct"].Value),
|
|
|
|
|
Convert.ToInt32(row.Cells["ColumnCount"].Value)));
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-21 15:34:40 +04:00
|
|
|
|
return list.GroupBy(x => x.ProductID, x => x.Count, (id, counts) => ProductSelling.CreateSelling(0, id, counts.Sum())).ToList();
|
2024-12-11 18:13:28 +04:00
|
|
|
|
;
|
2024-12-02 14:38:24 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-15 18:27:06 +04:00
|
|
|
|
}
|
|
|
|
|
}
|