demo_hard/demo_hard/Order.axaml.cs
2025-04-22 11:40:40 +03:00

68 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Generic;
using Avalonia.Controls;
using Avalonia.Interactivity;
using demo_hard.Model;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace demo_hard;
public partial class Order : Window
{
private readonly Model.Order _order;
private readonly User15Context _db = new();
public Order(Model.Order order)
{
InitializeComponent();
_order = order;
LoadOrderData();
}
private void LoadOrderData()
{
var client = _db.Clients.FirstOrDefault(c => c.ClientId == _order.ClientId);
var orderServices = _db.OrderServices
.Include(os => os.Service)
.Where(os => os.OrderId == _order.OrderId)
.ToList();
OrderNumber.Text = _order.OrderId.ToString();
ClientName.Text = client?.Fio ?? "Не указан";
OrderDate.Text = $"{_order.StartDate} {_order.Time}";
var servicesInfo = CalculateServicesInfo(orderServices);
TotalCost.Text = servicesInfo.totalCost.ToString("C");
StatusText.Text = _order.Status ?? "Новая";
}
private (decimal totalCost, string servicesText) CalculateServicesInfo(List<OrderService> orderServices)
{
decimal total = 0;
string info = "";
foreach (var os in orderServices)
{
if (os.Service != null)
{
decimal cost = (os.Service.CostPerHour ?? 0) * (os.RentTime / 60m);
total += cost;
info += $"{os.Service.ServiceName} - {os.RentTime} мин ({cost:C})\n";
}
}
return (total, info);
}
private void PrintBarcode_Click(object sender, RoutedEventArgs e)
{
int rentTime = _db.OrderServices
.Where(os => os.OrderId == _order.OrderId)
.Max(os => os.RentTime);
new BarcodeWindow(_order.OrderId, rentTime).Show();
Close();
}
private void Close_Click(object sender, RoutedEventArgs e) => Close();
}