2024-05-24 01:56:34 +04:00

166 lines
5.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using SushiBarBusinessLogic;
using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.ViewModels;
namespace SushiBarView.Forms
{
public partial class FormCreateCheque : Form
{
private readonly ChequeLogic _chequeLogic;
private readonly CustomerLogic _customerLogic;
private List<ChequeItemViewModel> ChequeItems;
private int? _id;
public int Id { set { _id = value; } }
public FormCreateCheque(ChequeLogic ChequeLogic, CustomerLogic customerLogic)
{
InitializeComponent();
_chequeLogic = ChequeLogic;
_customerLogic = customerLogic;
ChequeItems = new();
}
private void FormCreateCheque_Load(object sender, EventArgs e)
{
var List = _customerLogic.ReadList(null);
if (List != null)
{
CustomerComboBox.DisplayMember = "Fio";
CustomerComboBox.ValueMember = "Id";
CustomerComboBox.DataSource = List;
CustomerComboBox.SelectedItem = null;
}
if (_id.HasValue)
{
try
{
var View = _chequeLogic.ReadElement(new ChequeSearchModel
{
Id = _id.Value
});
if (View != null)
{
CustomerComboBox.SelectedItem = View.CustomerId;
ChequeItems = View.ChequeItems.Select(x => x.Value).ToList();
CustomerComboBox.Enabled = false;
AddButton.Enabled = false;
SaveButton.Enabled = false;
LoadData();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void LoadData()
{
try
{
DataGridView.Rows.Clear();
foreach (var ChequeItem in ChequeItems)
{
DataGridView.Rows.Add(new object[] { ChequeItem.DishId, ChequeItem.DishName, ChequeItem.Count, ChequeItem.CookFio });
}
PriceTextBox.Text = CalcPrice().ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void AddButton_Click(object sender, EventArgs e)
{
var Form = new FormCreateChequeItem(
new DishLogic(new SushiBarDatabaseImplement.Storages.DishStorage()),
new CookLogic(new SushiBarDatabaseImplement.Storages.CookStorage()));
if (Form.ShowDialog() == DialogResult.OK)
{
if (Form.DishModel == null || Form.CookModel == null)
{
return;
}
ChequeItems.Add(new ChequeItemViewModel
{
DishId = Form.DishId,
DishName = Form.DishModel.DishName,
DishPrice = Form.DishModel.Price,
CookId = Form.CookId,
CookFio = Form.CookModel.Fio,
Count = Form.Count,
});
LoadData();
}
}
private void SaveButton_Click(object sender, EventArgs e)
{
try
{
var Мodel = new ChequeBindingModel
{
Id = 0,
ChequeItems = ChequeItems.Select(x => new ChequeItemBindingModel
{
DishId = x.DishId,
CookId = x.CookId,
Count = x.Count,
}).ToList(),
CustomerId = null,
OrderDate = DateTime.Now,
TotalSum = CalcPrice(),
PromotionId = null,
};
var OperationResult = _chequeLogic.CreateCheque(Мodel);
if (!OperationResult)
{
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
}
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private double CalcPrice()
{
double Price = 0;
foreach (var Elem in ChequeItems)
{
Price += Elem.DishPrice * Elem.Count;
}
return Math.Round(Price * 1.1, 2);
}
}
}