Demka_kur/OknaFunciy/AddOtchotWindow.axaml.cs
2025-03-05 12:12:58 +03:00

132 lines
4.2 KiB
C#

using Avalonia.Controls;
using Avalonia.Interactivity;
using Demka_Snova_1.Hardik.Conect.Dao;
using Demka_Snova_1.OknaRoley;
using System;
using System.Collections.Generic;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout.Element;
using Document = iText.Layout.Document;
using iText.Kernel.Font;
using iText.IO.Font;
namespace Demka_Snova_1;
public partial class AddOtchotWindow : Window
{
private List<ordersDao> ordersList;
private Random random;
private int nextId = 1;
public AddOtchotWindow()
{
InitializeComponent();
random = new Random();
ordersList = new List<ordersDao>();
}
private void TestOrders()
{
ordersList = new List<ordersDao>
{
new ordersDao { ID = 1, CodeZakaz = "Èâàíîâ Èâàí", Date = DateOnly.FromDayNumber(8), Time = TimeOnly.FromDateTime(DateTime.Now), CodeClient = "123", Usluga = "2023-10-01", Status = "Admin", DateClose = null, Prokat = 23 }
};
}
private void FormatOtchot_Click(object sender, RoutedEventArgs e)
{
string client = this.FindControl<TextBox>("ClientTextBox").Text;
string usluga = this.FindControl<TextBox>("UslugaTextBox").Text;
string prokatText = this.FindControl<TextBox>("ProkatTextBox").Text;
if (!decimal.TryParse(prokatText, out decimal prokat))
{
ShowError("Íåêîððåêòíîå çíà÷åíèå äëÿ ïðîêàòà.");
return;
}
string codeZakaz = $"{random.Next(10000, 99999)}.{random.Next(10000, 99999)}";
int id = nextId++;
DateOnly date = DateOnly.FromDateTime(DateTime.Now);
TimeOnly time = TimeOnly.FromDateTime(DateTime.Now);
ordersDao Order = new ordersDao
{
ID = id,
CodeZakaz = codeZakaz,
Date = date,
Time = time,
CodeClient = client,
Usluga = usluga,
Status = "Íîâûé",
DateClose = null,
Prokat = prokat
};
ordersList.Add(Order);
SaveOrderToPdf(Order);
this.FindControl<TextBox>("ClientTextBox").Text = string.Empty;
this.FindControl<TextBox>("UslugaTextBox").Text = string.Empty;
this.FindControl<TextBox>("ProkatTextBox").Text = string.Empty;
ShowError("Çàêàç óñïåøíî ñîçäàí è ñîõðàíåí â PDF!");
}
private void SaveOrderToPdf(ordersDao order)
{
string directoryPath = "C:/Users/PC/source/Dopolnenia/Fails/Doky";
string pdfPath = Path.Combine(directoryPath, $"Order_{order.CodeZakaz}.pdf");
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
using (PdfWriter writer = new PdfWriter(pdfPath))
using (PdfDocument pdf = new PdfDocument(writer))
{
Document document = new Document(pdf);
PdfFont font = PdfFontFactory.CreateFont("C:/Windows/Fonts/arial.ttf", PdfEncodings.IDENTITY_H);
document.Add(new Paragraph($"Îò÷åò î çàêàçå îò {DateOnly.FromDateTime(DateTime.Now)}")
.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER)
.SetFontSize(20)
.SetFont(font));
document.Add(new Paragraph($"Êîä çàêàçà: {order.CodeZakaz}").SetFont(font));
document.Add(new Paragraph($"Êîä êëèåíòà: {order.CodeClient}").SetFont(font));
document.Add(new Paragraph($"Óñëóãà: {order.Usluga}").SetFont(font));
document.Add(new Paragraph($"Ïðîêàò: {order.Prokat}").SetFont(font));
document.Add(new Paragraph($"Äàòà: {order.Date}").SetFont(font));
document.Add(new Paragraph($"Âðåìÿ: {order.Time}").SetFont(font));
document.Add(new Paragraph($"Ñòàòóñ: {order.Status}").SetFont(font));
document.Close();
}
}
private void Exitka(object sender, RoutedEventArgs e)
{
var login = new AdminWindow();
login.Show();
this.Close();
}
async void ShowError(string mes)
{
var dialog = new Window
{
Title = "Îøèáêà",
Content = mes,
Width = 300,
Height = 200
};
await dialog.ShowDialog(this);
}
}