Gremlin/Gremlin/MVVM/ViewModels/QuoteViewModel.cs

220 lines
8.1 KiB
C#

using Caliburn.Micro;
using Microsoft.Win32;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace Gremlin.MVVM
{
public class QuoteViewModel : PropertyChangedBase
{
private string _quoteType = "ein Analysegerät";
private string _quotePath = AppDomain.CurrentDomain.BaseDirectory;
private ObservableCollection<LineItemViewModel> _lineItemsVM = new();
private ContactViewModel _recipient = new();
private ContactViewModel _salesRep = new();
private string _quoteNumber;
private int _warranty = 12;
private int _validity = 60;
private decimal _vAT = 19;
private decimal _freight = 1;
private bool? _showBrutto = true;
private bool? _singlePrices = true;
private bool? _attachBrochures;
private bool? _attachDataSheets;
private bool? _useMailTemplate;
private bool? _showDiscounts = true;
private int _oppID;
private bool _preisinformation = false;
public string QuoteType { get => _quoteType; set { _quoteType = value; NotifyOfPropertyChange(() => QuoteType); } }
public string QuotePath { get => _quotePath; set { _quotePath = CorrectQuotePath(value); NotifyOfPropertyChange(() => QuotePath); } }
public bool? ShowBrutto { get => _showBrutto; set { _showBrutto = value; NotifyOfPropertyChange(() => ShowBrutto); } }
public string QuoteNumber { get => _quoteNumber; set { _quoteNumber = value; NotifyOfPropertyChange(() => QuoteNumber); } }
public int Warranty { get => _warranty; set { _warranty = value; NotifyOfPropertyChange(() => Warranty); } }
public int Validity { get => _validity; set { _validity = value; NotifyOfPropertyChange(() => Validity); } }
public bool? SinglePrices { get => _singlePrices; set { _singlePrices = value; NotifyOfPropertyChange(() => SinglePrices); } }
public bool? AttachBrochures { get => _attachBrochures; set { _attachBrochures = value; NotifyOfPropertyChange(() => AttachBrochures); } }
public bool? AttachDataSheets { get => _attachDataSheets; set { _attachDataSheets = value; NotifyOfPropertyChange(() => AttachDataSheets); } }
public bool? UseMailTemplate { get => _useMailTemplate; set { _useMailTemplate = value; NotifyOfPropertyChange(() => UseMailTemplate); } }
public bool? ShowDiscounts { get => _showDiscounts; set { _showDiscounts = value; NotifyOfPropertyChange(() => ShowDiscounts); } }
public decimal VAT { get => _vAT; set { _vAT = value; NotifyOfPropertyChange(() => VAT); } }
public int OppID { get => _oppID; set { _oppID = value; NotifyOfPropertyChange(() => OppID); } }
public bool Preisinformation { get => _preisinformation; set { _preisinformation = value; NotifyOfPropertyChange(() => Preisinformation); } }
public decimal Freight { get => _freight; set { _freight = value; NotifyOfPropertyChange(() => Freight); } }
public ObservableCollection<LineItemViewModel> LineItemsViewModel { get => _lineItemsVM; set { _lineItemsVM = value; NotifyOfPropertyChange(() => LineItemsViewModel); } }
public ContactViewModel Recipient { get => _recipient; set { _recipient = value; NotifyOfPropertyChange(() => Recipient); } }
public ContactViewModel SalesRep { get => _salesRep; set { _salesRep = value; NotifyOfPropertyChange(() => SalesRep); } }
public decimal TotalListprice => GetTotal("TotalListprice");
public decimal AverageDiscount => GetTotal("AverageDiscount");
public decimal TotalNet => GetTotal("TotalNet");
public decimal TotalFreightOnly => TotalNet * Freight / 100;
public decimal TotalFreight => TotalNet * (1 + VAT) / 100;
public decimal TotalVAT => TotalFreight * VAT / 100;
public decimal TotalGross => TotalNet * (1 + VAT) / 100;
public bool QuoteContains3PP => DoesContains("3PP");
public bool QuoteContainsRB => DoesContains("RB");
public QuoteViewModel(ContactViewModel salesRep)
{
Random random = new();
SalesRep = salesRep;
QuoteNumber = SalesRep.LastName switch
{
"Woitschetzki" => $"DE-83PE89-{DateTime.Now:My}-{random.Next(999999)}",
"Welsch" => $"DE-83RE32-{DateTime.Now:My}-{random.Next(999999)}",
_ => $"DE-XXYYXX-{DateTime.Now:My}-{random.Next(999999)}",
};
}
public QuoteViewModel()
{
GenerateQuoteNumber();
GenerateOppID();
}
public void GenerateOppID()
{
Random random = new();
OppID = random.Next(111111111, 999999999);
}
private QuoteViewModel(string quoteNumber, string quoteType, ContactViewModel recipient, ContactViewModel salesRep, bool? brutto, decimal vAT, int warranty, int validity, string quotePath, bool? singlePrices, bool? brochures, bool? dataSheets, bool? mailTemplate, bool? showDiscounts)
{
QuoteNumber = quoteNumber;
QuoteType = quoteType;
Recipient = recipient;
SalesRep = salesRep;
ShowBrutto = brutto;
VAT = vAT;
Warranty = warranty;
Validity = validity;
QuotePath = quotePath;
SinglePrices = singlePrices;
AttachBrochures = brochures;
AttachDataSheets = dataSheets;
UseMailTemplate = mailTemplate;
ShowDiscounts = showDiscounts;
}
internal static QuoteViewModel CreateQuote(string quoteNumber, ContactViewModel recipient, ContactViewModel salesRep, decimal vAT = 19, string quoteType = "ein Analysegerät", bool? brutto = true, int warranty = 12, bool? singlePrices = true, bool? brochures = true, bool? dataSheets = true, bool? mailTemplate = true, string quotePath = "", int validity = 60, bool? showDiscounts = true)
{
QuoteViewModel quoteVM = new(quoteNumber, quoteType, recipient, salesRep, brutto, vAT, warranty, validity, quotePath, singlePrices, brochures, dataSheets, mailTemplate, showDiscounts);
quoteVM.LineItemsViewModel = new();
ObservableCollection<LineItemViewModel> lineItemsVM = LineItemViewModel.ReadLineItemsFromClipboard();
if (lineItemsVM == null) return null;
foreach (LineItemViewModel lineItemVM in lineItemsVM)
{
if (lineItemVM.OptionNumber.StartsWith("8D"))
{
quoteVM.Warranty = int.Parse(lineItemVM.OptionNumber.Last().ToString()) * 12;
}
quoteVM.LineItemsViewModel.Add(lineItemVM);
}
return quoteVM;
}
public void GenerateQuoteNumber()
{
Random random = new();
QuoteNumber = SalesRep.LastName switch
{
"Woitschetzki" => $"DE-83PE89-{DateTime.Now:My}-{random.Next(999999)}",
"Welsch" => $"DE-83RE32-{DateTime.Now:My}-{random.Next(999999)}",
_ => $"DE-XXYYXX-{DateTime.Now:My}-{random.Next(999999)}",
};
}
public void ReadLineItems()
{
LineItemsViewModel = LineItemViewModel.ReadLineItemsFromClipboard();
}
private static string CorrectQuotePath(string quotePath)
{
if (quotePath.EndsWith("\\"))
return quotePath;
else
return $"{quotePath}\\";
}
public void ChooseQuotePath()
{
OpenFileDialog openFileDialog = new()
{
Multiselect = false,
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
};
if (openFileDialog.ShowDialog() == true)
{
FileInfo fileInfo = new(openFileDialog.FileName);
QuotePath = fileInfo.DirectoryName;
}
}
private decimal GetTotal(string type)
{
decimal total = 0;
foreach (LineItemViewModel lineItemVM in LineItemsViewModel)
{
switch (type)
{
case "TotalListprice":
total += lineItemVM.ListPrice;
break;
case "TotalNet":
total += lineItemVM.CalcTotalNet;
break;
case "AverageDiscount":
total += lineItemVM.TotalDiscount;
break;
}
}
if (type == "AverageDiscount" & LineItemsViewModel.Count != 0) { total /= LineItemsViewModel.Count; }
Debug.WriteLine($"{type} = {total}");
return total;
}
private bool DoesContains(string type)
{
foreach (LineItemViewModel lineItemVM in LineItemsViewModel)
{
switch (type)
{
case "3PP":
if (lineItemVM.ProductLine == "3PP")
{
Debug.WriteLine($"Quote containts 3PP with ProductNumber {lineItemVM.ProductNumber}");
NotifyOfPropertyChange(() => QuoteContains3PP);
return true;
}
break;
case "RB":
if (lineItemVM.ProductLine == "RB" & lineItemVM.ProductNumber != "R2005A")
{
Debug.WriteLine($"Quote containts RB with ProductNumber {lineItemVM.ProductNumber}");
NotifyOfPropertyChange(() => QuoteContainsRB);
return true;
}
break;
}
}
return false;
}
}
}