Blagodat/OknaFunciy/AddOtchotWindow.axaml.cs

148 lines
4.4 KiB
C#
Raw Normal View History

2025-03-05 09:12:58 +00:00
using Avalonia.Controls;
using Avalonia.Interactivity;
2025-04-22 07:25:24 +00:00
using Demka_Snova_1.Hardik.Conect;
2025-03-05 09:12:58 +00:00
using Demka_Snova_1.Hardik.Conect.Dao;
using Demka_Snova_1.OknaRoley;
using iText.Kernel.Pdf;
2025-04-22 07:25:24 +00:00
using iText.Layout;
2025-03-05 09:12:58 +00:00
using iText.Layout.Element;
using iText.Kernel.Font;
2025-04-22 07:25:24 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2025-03-05 09:12:58 +00:00
using iText.IO.Font;
2025-04-23 07:04:16 +00:00
using iText.IO.Font.Constants;
2025-03-05 09:12:58 +00:00
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();
2025-04-22 07:25:24 +00:00
LoadOrders();
2025-03-05 09:12:58 +00:00
}
2025-04-22 07:25:24 +00:00
private void LoadOrders()
2025-03-05 09:12:58 +00:00
{
2025-04-22 07:25:24 +00:00
using (var db = new DatabaseConnection())
2025-03-05 09:12:58 +00:00
{
2025-04-22 07:25:24 +00:00
ordersList = db.GetAllOrders();
nextId = ordersList.Count > 0 ? ordersList.Max(o => o.ID) + 1 : 1;
}
2025-03-05 09:12:58 +00:00
}
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("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.");
return;
}
string codeZakaz = $"{random.Next(10000, 99999)}.{random.Next(10000, 99999)}";
2025-04-22 07:25:24 +00:00
ordersDao order = new ordersDao
2025-03-05 09:12:58 +00:00
{
2025-04-22 07:25:24 +00:00
ID = nextId++,
2025-03-05 09:12:58 +00:00
CodeZakaz = codeZakaz,
2025-04-22 07:25:24 +00:00
Date = DateOnly.FromDateTime(DateTime.Now),
Time = TimeOnly.FromDateTime(DateTime.Now),
2025-03-05 09:12:58 +00:00
CodeClient = client,
Usluga = usluga,
Status = "<22><><EFBFBD><EFBFBD><EFBFBD>",
DateClose = null,
Prokat = prokat
};
2025-04-22 07:25:24 +00:00
SaveOrderToPdf(order);
ordersList.Add(order);
2025-03-05 09:12:58 +00:00
2025-04-22 07:25:24 +00:00
ClearFields();
2025-03-05 09:12:58 +00:00
ShowError("<22><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> PDF!");
}
private void SaveOrderToPdf(ordersDao order)
{
string directoryPath = "C:/Users/PC/source/Dopolnenia/Fails/Doky";
string pdfPath = Path.Combine(directoryPath, $"Order_{order.CodeZakaz}.pdf");
2025-04-23 07:04:16 +00:00
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;
try
{
font = PdfFontFactory.CreateFont("C:/Windows/Fonts/arial.ttf", PdfEncodings.IDENTITY_H);
}
catch
{
font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
}
document.Add(new Paragraph($"<22><><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> {order.Date}")
.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER)
.SetFontSize(20)
.SetFont(font));
var fields = new Dictionary<string, string>
{
{"<22><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", order.CodeZakaz},
{"<22><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", order.CodeClient},
{"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", order.Usluga},
{"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", order.Prokat.ToString()},
{"<22><><EFBFBD><EFBFBD>", order.Date.ToString()},
{"<22><><EFBFBD><EFBFBD><EFBFBD>", order.Time.ToString()},
{"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", order.Status}
};
foreach (var field in fields)
{
document.Add(new Paragraph($"{field.Key}: {field.Value}").SetFont(font));
}
document.Close();
}
2025-03-05 09:12:58 +00:00
}
2025-04-22 07:25:24 +00:00
private void ClearFields()
{
this.FindControl<TextBox>("ClientTextBox").Text = string.Empty;
this.FindControl<TextBox>("UslugaTextBox").Text = string.Empty;
this.FindControl<TextBox>("ProkatTextBox").Text = string.Empty;
}
2025-03-05 09:12:58 +00:00
private void Exitka(object sender, RoutedEventArgs e)
{
2025-04-22 07:25:24 +00:00
new AdminWindow().Show();
2025-03-05 09:12:58 +00:00
this.Close();
}
2025-04-22 07:25:24 +00:00
private async void ShowError(string message)
2025-03-05 09:12:58 +00:00
{
var dialog = new Window
{
Title = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>",
2025-04-22 07:25:24 +00:00
Content = message,
2025-03-05 09:12:58 +00:00
Width = 300,
Height = 200
};
await dialog.ShowDialog(this);
}
}