Files
CourseWork_BeautySalon/BeautySalonViewEmployee/WindowReceipt.xaml.cs
revengel66 64f4cb173f the end
2025-04-30 05:08:30 +04:00

268 lines
8.6 KiB
C#

using BeautySalonContracts.BindingModels;
using BeautySalonContracts.BusinessLogicsContracts;
using BeautySalonContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using Unity;
namespace BeautySalonViewEmployee
{
/// <summary>
/// Логика взаимодействия для WindowReceipt.xaml
/// </summary>
public partial class WindowReceipt : Window
{
public int Id { set { id = value; } }
public int EmployeeId { set { employeeId = value; } }
private readonly IReceiptLogic _logicR;
private readonly ICosmeticLogic _logicC;
private int? purchaseId; // будет хранить связанный Purchase.Id
private DateTime? originalReceiptDate; // будет хранить дату чека из БД
private int? id;
private int? employeeId;
private Dictionary<int, (string, int)> receiptCosmetics;
public WindowReceipt(IReceiptLogic logicR, ICosmeticLogic logicC)
{
InitializeComponent();
this._logicR = logicR;
this._logicC = logicC;
}
private void WindowReceipt_Loaded(object sender, RoutedEventArgs e)
{
if (id.HasValue)
{
try
{
var view = _logicR.Read(new ReceiptBindingModel { Id = id.Value })?[0];
if (view != null)
{
// запоминаем дату и purchaseId из БД
originalReceiptDate = view.Date;
purchaseId = view.PurchaseId;
TextBoxTotalCost.Text = view.TotalCost.ToString("F2");
TextBoxDate.Text = originalReceiptDate.Value.ToString("g");
TextBoxPurchase.Text = view.PurchaseDate?.ToString("g") ?? "";
receiptCosmetics = view.ReceiptCosmetics;
LoadData();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
else
{
receiptCosmetics = new Dictionary<int, (string, int)>();
}
}
private void LoadData()
{
try
{
if (receiptCosmetics != null)
{
dataGrid.Columns.Clear();
var list = new List<DataGridReceiptItemViewModel>();
foreach (var rc in receiptCosmetics)
{
list.Add(new DataGridReceiptItemViewModel()
{
Id = rc.Key,
CosmeticName = rc.Value.Item1,
Price = _logicC.Read(new CosmeticBindingModel { Id = rc.Key })?[0].Price,
Count = rc.Value.Item2
});
}
dataGrid.ItemsSource = list;
dataGrid.Columns[0].Visibility = Visibility.Hidden;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void buttonAdd_Click(object sender, RoutedEventArgs e)
{
var window = App.Container.Resolve<WindowSelectionCosmetics>();
window.EmployeeId = (int)employeeId;
if (window.ShowDialog().Value)
{
if (receiptCosmetics.ContainsKey(window.Id))
{
receiptCosmetics[window.Id] = (window.CosmeticName, window.Count);
}
else
{
receiptCosmetics.Add(window.Id, (window.CosmeticName, window.Count));
}
LoadData();
CalcTotalCost();
}
}
private void CalcTotalCost()
{
try
{
int totalCost = 0;
foreach (var rc in receiptCosmetics)
{
totalCost += rc.Value.Item2 * (int)_logicC.Read(new CosmeticBindingModel { Id = rc.Key })?[0].Price;
}
TextBoxTotalCost.Text = totalCost.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void buttonUpd_Click(object sender, RoutedEventArgs e)
{
if (dataGrid.SelectedCells.Count != 0)
{
var window = App.Container.Resolve<WindowSelectionCosmetics>();
window.Id = ((DataGridReceiptItemViewModel)dataGrid.SelectedCells[0].Item).Id;
window.Count = ((DataGridReceiptItemViewModel)dataGrid.SelectedCells[0].Item).Count;
window.EmployeeId = (int)employeeId;
if (window.ShowDialog().Value)
{
receiptCosmetics[window.Id] = (window.CosmeticName, window.Count);
LoadData();
CalcTotalCost();
}
}
}
private void buttonDel_Click(object sender, RoutedEventArgs e)
{
if (dataGrid.SelectedCells.Count != 0)
{
if (MessageBox.Show("Удалить запись", "Вопрос", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
try
{
receiptCosmetics.Remove(((DataGridReceiptItemViewModel)dataGrid.SelectedCells[0].Item).Id);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
}
LoadData();
}
}
}
private void buttonRef_Click(object sender, RoutedEventArgs e)
{
LoadData();
}
private void buttonSave_Click(object sender, RoutedEventArgs e)
{
if (receiptCosmetics == null || receiptCosmetics.Count == 0)
{
MessageBox.Show("Заполните косметику", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
try
{
_logicR.CreateOrUpdate(new ReceiptBindingModel
{
Id = id,
EmployeeId = employeeId,
PurchaseId = purchaseId, // вот он, сохраняем связь
TotalCost = Convert.ToDecimal(TextBoxTotalCost.Text),
Date = originalReceiptDate ?? DateTime.Now, // не перезаписываем на Now
ReceiptCosmetics = receiptCosmetics
});
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButton.OK, MessageBoxImage.Information);
DialogResult = true;
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void buttonCancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
/// <summary>
/// Данные для привязки DisplayName к названиям столбцов
/// </summary>
private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
string displayName = GetPropertyDisplayName(e.PropertyDescriptor);
if (!string.IsNullOrEmpty(displayName))
{
e.Column.Header = displayName;
}
}
/// <summary>
/// метод привязки DisplayName к названию столбца
/// </summary>
public static string GetPropertyDisplayName(object descriptor)
{
PropertyDescriptor pd = descriptor as PropertyDescriptor;
if (pd != null)
{
// Check for DisplayName attribute and set the column header accordingly
DisplayNameAttribute displayName = pd.Attributes[typeof(DisplayNameAttribute)] as DisplayNameAttribute;
if (displayName != null && displayName != DisplayNameAttribute.Default)
{
return displayName.DisplayName;
}
}
else
{
PropertyInfo pi = descriptor as PropertyInfo;
if (pi != null)
{
// Check for DisplayName attribute and set the column header accordingly
Object[] attributes = pi.GetCustomAttributes(typeof(DisplayNameAttribute), true);
for (int i = 0; i < attributes.Length; ++i)
{
DisplayNameAttribute displayName = attributes[i] as DisplayNameAttribute;
if (displayName != null && displayName != DisplayNameAttribute.Default)
{
return displayName.DisplayName;
}
}
}
}
return null;
}
}
}