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

211 lines
6.5 KiB
C#

using BeautySalonContracts.BindingModels;
using BeautySalonContracts.BusinessLogicsContracts;
using BeautySalonContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using Unity;
namespace BeautySalonViewEmployee
{
/// <summary>
/// Логика взаимодействия для WindowReceipts.xaml
/// </summary>
public partial class WindowReceipts : Window
{
public int Id { set { id = value; } }
private int? id;
private readonly IReceiptLogic _logic;
public WindowReceipts(IReceiptLogic logic)
{
InitializeComponent();
this._logic = logic;
}
private void WindowReceipts_Loaded(object sender, RoutedEventArgs e)
{
LoadData();
}
private void LoadData()
{
try
{
var list = _logic.Read(new ReceiptBindingModel { EmployeeId = id });
if (list != null)
{
dataGrid.ItemsSource = list;
ReorderColumns();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void ReorderColumns()
{
foreach (var column in dataGrid.Columns)
{
switch (column.Header.ToString())
{
case "Номер чека":
column.DisplayIndex = 0;
break;
case "Дата чека":
column.DisplayIndex = 1;
break;
case "Дата покупки":
column.DisplayIndex = 2;
break;
case "Общая стоимость":
column.DisplayIndex = 3;
break;
}
}
}
private void buttonAdd_Click(object sender, RoutedEventArgs e)
{
var window = App.Container.Resolve<WindowReceipt>();
window.EmployeeId = (int)id;
if (window.ShowDialog().Value)
{
LoadData();
}
}
private void buttonUpd_Click(object sender, RoutedEventArgs e)
{
if (dataGrid.SelectedCells.Count != 0)
{
var window = App.Container.Resolve<WindowReceipt>();
window.Id = ((ReceiptViewModel)dataGrid.SelectedCells[0].Item).Id;
window.EmployeeId = (int)id;
if (window.ShowDialog().Value)
{
LoadData();
}
}
}
private void buttonDel_Click(object sender, RoutedEventArgs e)
{
if (dataGrid.SelectedCells.Count != 0)
{
if (MessageBox.Show("Удалить запись", "Вопрос", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
int id = ((ReceiptViewModel)dataGrid.SelectedCells[0].Item).Id;
try
{
_logic.Delete(new ReceiptBindingModel { Id = id });
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
}
LoadData();
}
}
}
private void buttonRef_Click(object sender, RoutedEventArgs e)
{
LoadData();
}
private void buttonLinking_Click(object sender, RoutedEventArgs e)
{
if (dataGrid.SelectedCells.Count != 0)
{
// Получаем модель выбранного чека
var selectedReceipt = (ReceiptViewModel)dataGrid.SelectedCells[0].Item;
// Создаём окно связи
var window = App.Container.Resolve<WindowLinkingPurchases>();
window.Owner = this;
// Передаём туда ID чека
window.EmployeeId = (int)id;
window.ReceiptId = selectedReceipt.Id;
// Открываем диалог
if (window.ShowDialog() == true)
{
// Если привязка прошла успешно, обновляем грид
LoadData();
}
//var window = App.Container.Resolve<WindowLinkingPurchases>();
//window.EmployeeId = (int)id;
//window.DistributionId = ((DistributionViewModel)dataGrid.SelectedCells[0].Item).Id;
//if (window.ShowDialog().Value)
//{
// LoadData();
//}
}
}
/// <summary>
/// Данные для привязки DisplayName к названиям столбцов
/// </summary>
private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyName == "ReceiptCosmetics" ||
e.PropertyName == "ReceiptPurchases" ||
e.PropertyName == "EmployeeId" ||
e.PropertyName == "PurchaseId")
{
e.Cancel = true;
return;
}
string displayName = GetPropertyDisplayName(e.PropertyDescriptor);
if (!string.IsNullOrEmpty(displayName))
e.Column.Header = displayName;
// Больше ничего — DisplayIndex убираем отсюда
}
/// <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;
}
}
}