120 lines
4.7 KiB
C#
120 lines
4.7 KiB
C#
|
using Avalonia.Controls;
|
|||
|
using demo_trade.Data.RemoteData.Entity;
|
|||
|
using demo_trade.Domain;
|
|||
|
using demo_trade.Models;
|
|||
|
using demo_trade.UI.Presenters;
|
|||
|
using DynamicData;
|
|||
|
using ReactiveUI;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Collections.ObjectModel;
|
|||
|
using System.Linq;
|
|||
|
using System.Reactive;
|
|||
|
using System.Reflection.Emit;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace demo_trade.ViewModels
|
|||
|
{
|
|||
|
public class OrderShowDialogViewModel : ViewModelBase
|
|||
|
{
|
|||
|
|
|||
|
private OrderClientUseCase _orderClientUseCase;
|
|||
|
|
|||
|
private PickupPoint _pickupPointSelected = new();
|
|||
|
public PickupPoint PickupPointSelected { get => _pickupPointSelected; set => this.RaiseAndSetIfChanged(ref _pickupPointSelected, value); }
|
|||
|
|
|||
|
private Models.Order? orderComplete { get; set; } = null;
|
|||
|
|
|||
|
private string _userName = String.Empty;
|
|||
|
public string UserName{ get => _userName; set => this.RaiseAndSetIfChanged(ref _userName, value); }
|
|||
|
|
|||
|
private int _numberOrder = 0;
|
|||
|
public int NumberOrder { get => _numberOrder; set => this.RaiseAndSetIfChanged(ref _numberOrder, value); }
|
|||
|
|
|||
|
private int _selectedPointId = 0;
|
|||
|
public int SelectedPointId { get => _selectedPointId; set => this.RaiseAndSetIfChanged(ref _selectedPointId, value); }
|
|||
|
|
|||
|
private List<PickupPoint> _pickupPoints = new();
|
|||
|
public List<PickupPoint> PickupPoints { get => _pickupPoints; set => this.RaiseAndSetIfChanged(ref _pickupPoints, value); }
|
|||
|
|
|||
|
private Decimal _orderSumValue = 0;
|
|||
|
public Decimal OrderSumValue { get => _orderSumValue; set => this.RaiseAndSetIfChanged(ref _orderSumValue, value); }
|
|||
|
|
|||
|
public ReactiveCommand<Unit, Unit> GenerateOrderCommand { get; }
|
|||
|
|
|||
|
|
|||
|
private ObservableCollection<OrderProductPresenter> _productOrderList = new();
|
|||
|
public ObservableCollection<OrderProductPresenter> ProductOrderList
|
|||
|
{
|
|||
|
get => _productOrderList;
|
|||
|
set => this.RaiseAndSetIfChanged(ref _productOrderList, value);
|
|||
|
}
|
|||
|
|
|||
|
public OrderShowDialogViewModel( Dictionary<string, (ProductPresenter, int)> orderProducts, OrderClientUseCase orderClientUseCase, User? user = null) {
|
|||
|
|
|||
|
_orderClientUseCase = orderClientUseCase;
|
|||
|
_numberOrder = _orderClientUseCase.GetNewOrderNumber();
|
|||
|
_pickupPoints = _orderClientUseCase.GetPickupPoints();
|
|||
|
if (user != null)
|
|||
|
{
|
|||
|
UserName = String.Format("{0} {1} {2}", user.Usersurname, user.Username, user.Userpatronymic);
|
|||
|
}
|
|||
|
GenerateOrderCommand = ReactiveCommand.Create(() => {
|
|||
|
string? clientName = null;
|
|||
|
if(!String.IsNullOrEmpty(UserName)) clientName = UserName;
|
|||
|
var resultOrder = _orderClientUseCase.GenerateNewOrder(PickupPointSelected, ProductOrderList.ToList(),OrderSumValue, clientName);
|
|||
|
if (resultOrder != null) {
|
|||
|
NumberOrder = resultOrder.Orderid + 1;
|
|||
|
orderComplete = resultOrder;
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
foreach (var item in orderProducts.Values)
|
|||
|
{
|
|||
|
ProductOrderList.Add(new OrderProductPresenter(item.Item1, item.Item2));
|
|||
|
}
|
|||
|
|
|||
|
this.WhenAnyValue(it => it.ProductOrderList).Subscribe(it => CalculateOrderSum());
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public void NumericChange(string ArticleProduct, int value) {
|
|||
|
ProductOrderList.First(it => it.ArticleNumber == ArticleProduct).ProductCount = value;
|
|||
|
CalculateOrderSum();
|
|||
|
if (value == 0)
|
|||
|
{
|
|||
|
ProductOrderList.Remove(ProductOrderList.First(it => it.ArticleNumber == ArticleProduct));
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public void SaveFile(string path) {
|
|||
|
if (orderComplete == null) return;
|
|||
|
Ticket ticket = new Ticket()
|
|||
|
{
|
|||
|
ClientName = orderComplete.OrderClient,
|
|||
|
OrderID = orderComplete.Orderid,
|
|||
|
OrderDate = DateOnly.FromDateTime(DateTime.Now),
|
|||
|
OrderDelivery = orderComplete.Orderdeliverydate,
|
|||
|
Products = orderComplete.ProductList,
|
|||
|
PickupPointName = orderComplete.PickupPointName,
|
|||
|
OrderSumCost = orderComplete.OrderSumCost
|
|||
|
}
|
|||
|
;
|
|||
|
ticket.GeneratePDF(path);
|
|||
|
|
|||
|
}
|
|||
|
private void CalculateOrderSum() {
|
|||
|
var orderSum = ProductOrderList.Sum(product => {
|
|||
|
if (product.CostWithDiscount != null) return product.CostWithDiscount * product.ProductCount;
|
|||
|
return product.ProductCount * product.Cost;
|
|||
|
});
|
|||
|
if (orderSum != null) OrderSumValue = orderSum.Value;
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|