279 lines
9.6 KiB
C#
279 lines
9.6 KiB
C#
using Avalonia.Controls;
|
||
using Avalonia.Interactivity;
|
||
using Avalonia.Media.Imaging;
|
||
using Demka_Snova_1.Hardik.Conect;
|
||
using Demka_Snova_1.Hardik.Conect.Dao;
|
||
using PdfSharpCore.Drawing;
|
||
using PdfSharpCore.Pdf;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Demka_Snova_1.OknaFunciy;
|
||
|
||
public partial class AddZakazWindow : Window
|
||
{
|
||
private List<KlientDao> _allClients;
|
||
private List<uslugiDao> _allServices;
|
||
private List<SelectedService> _selectedServices = new();
|
||
private KlientDao _selectedClient;
|
||
private int _nextOrderId;
|
||
private string _generatedBarcodeData;
|
||
|
||
//private TextBox OrderNumberTextBox => this.FindControl<TextBox>("OrderNumberTextBox");
|
||
//private TextBox ClientSearchTextBoxTextBox => this.FindControl<TextBox>("ClientSearchTextBoxTextBox");
|
||
private TextBox ServiceSearchTextBox => this.FindControl<TextBox>("ServiceSearchTextBox");
|
||
//private TextBox TotalPriceTextBlock => this.FindControl<TextBox>("TotalPriceTextBlock");
|
||
//private DataGrid ClientsDataGrid => this.FindControl<DataGrid>("ClientsDataGrid");
|
||
//private DataGrid ServicesDataGrid => this.FindControl<DataGrid>("ServicesDataGrid");
|
||
//private DataGrid SelectedServicesDataGrid => this.FindControl<DataGrid>("SelectedServicesDataGrid");
|
||
|
||
|
||
|
||
|
||
public AddZakazWindow()
|
||
{
|
||
InitializeComponent();
|
||
LoadData();
|
||
}
|
||
|
||
|
||
|
||
private async void LoadData()
|
||
{
|
||
try
|
||
{
|
||
using (var db = new DatabaseConnection())
|
||
{
|
||
_allClients = db.GetAllKlienti();
|
||
_allServices = db.GetAllUslugi();
|
||
|
||
var orders = db.GetAllOrders();
|
||
_nextOrderId = orders.Count > 0 ? orders.Max(o => o.ID) + 1 : 1;
|
||
|
||
OrderNumberTextBox.Text = _nextOrderId.ToString();
|
||
OrderNumberTextBox.Watermark = $"Рекомендуемый номер: {_nextOrderId}";
|
||
|
||
ClientsDataGrid.ItemsSource = _allClients;
|
||
ServicesDataGrid.ItemsSource = _allServices;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
await ShowError($"Ошибка загрузки данных: {ex.Message}");
|
||
}
|
||
}
|
||
|
||
private void OrderNumberTextBox_KeyDown(object sender, Avalonia.Input.KeyEventArgs e)
|
||
{
|
||
if (e.Key == Avalonia.Input.Key.Enter)
|
||
{
|
||
if (int.TryParse(OrderNumberTextBox.Text, out int orderNum))
|
||
{
|
||
_nextOrderId = orderNum;
|
||
}
|
||
else
|
||
{
|
||
OrderNumberTextBox.Text = _nextOrderId.ToString();
|
||
}
|
||
}
|
||
}
|
||
|
||
private void ClientSearchTextBox_TextChanged(object sender, TextChangedEventArgs e)
|
||
{
|
||
var searchText = this.FindControl<TextBox>("ClientSearchTextBox").Text.ToLower();
|
||
ClientsDataGrid.ItemsSource = _allClients
|
||
.Where(c => c.Fio.ToLower().Contains(searchText) ||
|
||
c.Code.ToString().Contains(searchText))
|
||
.ToList();
|
||
}
|
||
|
||
private void ServiceSearchTextBox_TextChanged(object sender, TextChangedEventArgs e)
|
||
{
|
||
var searchText = ServiceSearchTextBox.Text.ToLower();
|
||
ServicesDataGrid.ItemsSource = _allServices
|
||
.Where(s => s.Name.ToLower().Contains(searchText) ||
|
||
s.Code.Contains(searchText))
|
||
.ToList();
|
||
}
|
||
|
||
private void ClientsDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||
{
|
||
_selectedClient = ClientsDataGrid.SelectedItem as KlientDao;
|
||
UpdateButtonsState();
|
||
}
|
||
|
||
private async void AddClientButton_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
var addClientWindow = new AddClientWindow();
|
||
var result = await addClientWindow.ShowDialog<KlientDao>(this);
|
||
|
||
if (result != null)
|
||
{
|
||
try
|
||
{
|
||
using (var db = new DatabaseConnection())
|
||
{
|
||
_allClients.Add(result);
|
||
ClientsDataGrid.ItemsSource = _allClients;
|
||
ClientsDataGrid.SelectedItem = result;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
await ShowError($"Ошибка при добавлении клиента: {ex.Message}");
|
||
}
|
||
}
|
||
}
|
||
|
||
private async void AddServiceButton_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
var selectedService = ServicesDataGrid.SelectedItem as uslugiDao;
|
||
if (selectedService == null)
|
||
{
|
||
await ShowError("Выберите услугу из списка");
|
||
return;
|
||
}
|
||
|
||
/*var hoursWindow = new InputHoursWindow(selectedService.Name);
|
||
var hours = await hoursWindow.ShowDialog<int?>(this);
|
||
|
||
if (hours.HasValue && hours > 0)
|
||
{
|
||
_selectedServices.Add(new SelectedService
|
||
{
|
||
Id = selectedService.id.ToString(),
|
||
Name = selectedService.Name,
|
||
PrisePerH = selectedService.PrisePerH,
|
||
Hours = hours.Value
|
||
});
|
||
|
||
UpdateSelectedServices();
|
||
}*/
|
||
}
|
||
|
||
private void RemoveServiceButton_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
var selected = SelectedServicesDataGrid.SelectedItem as SelectedService;
|
||
if (selected != null)
|
||
{
|
||
_selectedServices.Remove(selected);
|
||
UpdateSelectedServices();
|
||
}
|
||
}
|
||
|
||
private void UpdateSelectedServices()
|
||
{
|
||
SelectedServicesDataGrid.ItemsSource = _selectedServices.ToList();
|
||
TotalPriceTextBlock.Text = $"Итого: {_selectedServices.Sum(s => s.Total)} руб.";
|
||
UpdateButtonsState();
|
||
}
|
||
|
||
private void SaveBarcodeToPdf(Bitmap barcodeBitmap, int orderId)
|
||
{
|
||
var pdfPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
|
||
$"Штрих-код_{orderId}.pdf");
|
||
|
||
using (var stream = new MemoryStream())
|
||
{
|
||
barcodeBitmap.Save(stream);
|
||
stream.Position = 0;
|
||
|
||
var document = new PdfDocument();
|
||
var page = document.AddPage();
|
||
page.Width = XUnit.FromMillimeter(80);
|
||
page.Height = XUnit.FromMillimeter(40);
|
||
|
||
using (var xGraphics = XGraphics.FromPdfPage(page))
|
||
{
|
||
var image = XImage.FromStream(() => stream);
|
||
xGraphics.DrawImage(image, 0, 0, page.Width, page.Height);
|
||
|
||
var font = new XFont("Arial", 10, XFontStyle.Bold);
|
||
xGraphics.DrawString($"Заказ №{orderId}", font, XBrushes.Black,
|
||
new XRect(0, page.Height - 15, page.Width, 15),
|
||
XStringFormats.Center);
|
||
}
|
||
|
||
document.Save(pdfPath);
|
||
}
|
||
}
|
||
|
||
private async void SaveOrderButton_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
if (_selectedClient == null || !_selectedServices.Any())
|
||
{
|
||
await ShowError("Выберите клиента и добавьте хотя бы одну услугу");
|
||
return;
|
||
}
|
||
|
||
try
|
||
{
|
||
using (var db = new DatabaseConnection())
|
||
{
|
||
var order = new ordersDao
|
||
{
|
||
ID = _nextOrderId,
|
||
CodeZakaz = _generatedBarcodeData,
|
||
Date = DateOnly.FromDateTime(DateTime.Now),
|
||
Time = TimeOnly.FromDateTime(DateTime.Now),
|
||
CodeClient = _selectedClient.Code.ToString(),
|
||
Usluga = string.Join(", ", _selectedServices.Select(s => $"{s.Name} ({s.Hours}ч)")),
|
||
Status = "Активен",
|
||
Prokat = _selectedServices.Sum(s => s.Hours)
|
||
};
|
||
|
||
db.AddOrder(order);
|
||
|
||
var orderData = $"дата_заказа={DateTime.Now:yyyy-MM-ddTHH:mm:ss}&номер_заказа={_nextOrderId}";
|
||
var base64Data = Convert.ToBase64String(Encoding.UTF8.GetBytes(orderData));
|
||
var orderLink = $"C:/Users/PC/source/Dopolnenia/Fails/Doky";
|
||
|
||
|
||
var linkPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
|
||
$"Ссылка_заказа_{_nextOrderId}.txt");
|
||
File.WriteAllText(linkPath, orderLink);
|
||
|
||
var pdfPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
|
||
$"Штрих-код_{_nextOrderId}.pdf");
|
||
|
||
await ShowError($"Заказ №{_nextOrderId} успешно сохранен!\n" +
|
||
$"Штрих-код сохранен в: {pdfPath}\n" +
|
||
$"Ссылка сохранена в: {linkPath}");
|
||
|
||
this.Close();
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
await ShowError($"Ошибка при сохранении заказа: {ex.Message}");
|
||
}
|
||
}
|
||
|
||
private void CancelButton_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
this.Close();
|
||
}
|
||
|
||
private void UpdateButtonsState()
|
||
{
|
||
SaveOrderButton.IsEnabled = _selectedClient != null && _selectedServices.Any();
|
||
}
|
||
|
||
private async Task ShowError(string message)
|
||
{
|
||
var dialog = new Window
|
||
{
|
||
Title = "Информация",
|
||
Content = new TextBlock { Text = message },
|
||
Width = 400,
|
||
Height = 200,
|
||
WindowStartupLocation = WindowStartupLocation.CenterOwner
|
||
};
|
||
|
||
await dialog.ShowDialog(this);
|
||
}
|
||
} |