197 lines
8.3 KiB
C#
197 lines
8.3 KiB
C#
using System;
|
|
using System.Text;
|
|
using System.Windows.Data;
|
|
using Gremlin.GremlinData.EntityClasses;
|
|
using Caliburn.Micro;
|
|
using System.Threading.Tasks;
|
|
using System.Threading;
|
|
|
|
namespace Gremlin.MVVM
|
|
{
|
|
public class ContactViewModel : PropertyChangedBase, IHandle<ShellViewModel>
|
|
{
|
|
private byte _gender;
|
|
private string _firstName;
|
|
private string _lastName;
|
|
private string _eMail;
|
|
private string _accountName;
|
|
private string _accountStreet;
|
|
private uint _accountZIP;
|
|
private string _accountCity;
|
|
private IEventAggregator _eventAggregator;
|
|
|
|
public byte Gender { get => _gender; internal set { _gender = value; NotifyOfPropertyChange(() => Gender); } }
|
|
public string FirstName { get => _firstName; internal set { _firstName = value; NotifyOfPropertyChange(() => FirstName); } }
|
|
public string LastName { get => _lastName; internal set { _lastName = value; NotifyOfPropertyChange(() => LastName); } }
|
|
public string EMail { get => _eMail; internal set { _eMail = value; NotifyOfPropertyChange(() => EMail); } }
|
|
public string AccountName { get => _accountName; internal set { _accountName = value; NotifyOfPropertyChange(() => AccountName); } }
|
|
public string AccountStreet { get => _accountStreet; internal set { _accountStreet = value; NotifyOfPropertyChange(() => AccountStreet); } }
|
|
public uint AccountZIP { get => _accountZIP; internal set { _accountZIP = value; NotifyOfPropertyChange(() => AccountZIP); } }
|
|
public string AccountCity { get => _accountCity; internal set { _accountCity = value; NotifyOfPropertyChange(() => AccountCity); } }
|
|
|
|
public ContactViewModel(IEventAggregator eventAggregator)
|
|
{
|
|
_eventAggregator = eventAggregator;
|
|
}
|
|
|
|
public ContactViewModel(byte gender, string firstName, string lastName, string eMail, string accountName, string accountStreet, uint accountZIP, string accountCity)
|
|
{
|
|
Gender = gender;
|
|
FirstName = firstName ?? throw new ArgumentNullException(nameof(firstName));
|
|
LastName = lastName ?? throw new ArgumentNullException(nameof(lastName));
|
|
EMail = eMail ?? throw new ArgumentNullException(nameof(eMail));
|
|
AccountName = accountName ?? throw new ArgumentNullException(nameof(accountName));
|
|
AccountStreet = accountStreet ?? throw new ArgumentNullException(nameof(accountStreet));
|
|
AccountZIP = accountZIP;
|
|
AccountCity = accountCity ?? throw new ArgumentNullException(nameof(accountCity));
|
|
}
|
|
|
|
public ContactViewModel()
|
|
{
|
|
}
|
|
|
|
internal static ContactViewModel ConvertObjectToContactVM(object selectedItem)
|
|
{
|
|
ContactViewModel selectedContact = new();
|
|
|
|
if (selectedItem != CollectionView.NewItemPlaceholder)
|
|
{
|
|
//TryCast
|
|
selectedContact = selectedItem as ContactViewModel;
|
|
|
|
//if failed == null
|
|
if (selectedContact == null)
|
|
{
|
|
return default;
|
|
}
|
|
}
|
|
|
|
return selectedContact;
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return Gender == (byte)Enums.Gender.Male
|
|
? $"Herr {FirstName} {LastName}; {AccountName}, {AccountStreet}, {AccountZIP} {AccountCity}"
|
|
: $"Frau {FirstName} {LastName}; {AccountName}, {AccountStreet}, {AccountZIP} {AccountCity}";
|
|
}
|
|
|
|
internal static Predicate<object> SearchContact(ListCollectionView listCollectionContacts, string search)
|
|
{
|
|
return listCollectionContacts.Filter = (c) =>
|
|
{
|
|
ContactViewModel contact = c as ContactViewModel;
|
|
return contact.LastName.ToLower().Contains(search.ToLower())
|
|
|| contact.FirstName.ToLower().Contains(search.ToLower())
|
|
|| contact.AccountName.ToLower().Contains(search.ToLower())
|
|
|| contact.AccountCity.ToLower().Contains(search.ToLower());
|
|
};
|
|
}
|
|
|
|
internal static string FilterContacts(object selectedItem)
|
|
{
|
|
return selectedItem != null
|
|
? selectedItem == CollectionView.NewItemPlaceholder
|
|
? "Bitte neuen Kontakt erstellen"
|
|
: CreateBriefkopf(selectedItem as ContactViewModel).ToString()
|
|
: "Kein Kontakt gefunden!";
|
|
}
|
|
|
|
internal static ContactViewModel GetSalesRepAsContact(int SalesRepCode)
|
|
{
|
|
switch (SalesRepCode)
|
|
{
|
|
case 1:
|
|
{
|
|
ContactViewModel salesRepWoitschetzki = new((byte)Enums.Gender.Male,
|
|
"Sascha",
|
|
"Woitschetzki",
|
|
"sascha.woitschetzki@non.agilent.com",
|
|
"Handelsvertretung Sascha Woitschetzki",
|
|
"Parsevalstr. 58",
|
|
45470,
|
|
"Mülheim an der Ruhr");
|
|
return salesRepWoitschetzki;
|
|
}
|
|
|
|
case 2:
|
|
{
|
|
ContactViewModel salesRepWelsch = new((byte)Enums.Gender.Male,
|
|
"Sebastian",
|
|
"Welsch",
|
|
"sebastian.welsch@non.agilent.com",
|
|
"Handelsvertretung Sebastian Welsch",
|
|
"",
|
|
0,
|
|
"");
|
|
return salesRepWelsch;
|
|
}
|
|
|
|
default:
|
|
{
|
|
ContactViewModel salesRep = new();
|
|
return salesRep;
|
|
}
|
|
}
|
|
}
|
|
|
|
internal static ContactViewModel ConvertContactToVM(Contact contact)
|
|
{
|
|
ContactViewModel contactVM = new();
|
|
contactVM.Gender = contact.Gender;
|
|
contactVM.LastName = contact.LastName;
|
|
contactVM.FirstName = contact.FirstName;
|
|
contactVM.EMail = contact.EMail;
|
|
if (contact.Account != null)
|
|
{
|
|
contactVM.AccountName = contact.Account.AccountName;
|
|
contactVM.AccountStreet = contact.Account.Street;
|
|
contactVM.AccountZIP = contact.Account.ZIP;
|
|
contactVM.AccountCity = contact.Account.City;
|
|
}
|
|
return contactVM;
|
|
}
|
|
|
|
internal static StringBuilder CreateBriefkopf(ContactViewModel contactVM, bool tex = false)
|
|
{
|
|
StringBuilder briefkopf = new();
|
|
|
|
_ = contactVM.Gender == (byte)Enums.Gender.Male
|
|
? briefkopf.AppendLine($"Herr {contactVM.FirstName} {contactVM.LastName}")
|
|
: briefkopf.AppendLine($"Frau {contactVM.FirstName} {contactVM.LastName}");
|
|
if (tex) _ = briefkopf.AppendLine($"\\\\");
|
|
|
|
string AccountName = contactVM.AccountName;
|
|
|
|
//AccountNamen mit "&" im Namen abfangen
|
|
if (tex && AccountName.Contains("&"))
|
|
{
|
|
string[] accountNameSplit = AccountName.Split("&"); //.Replace?
|
|
AccountName = "";
|
|
for (int i = 0; i < accountNameSplit.Length; i++)
|
|
{
|
|
AccountName += i < accountNameSplit.Length - 1
|
|
? accountNameSplit[i] + "\\&"
|
|
: accountNameSplit[i];
|
|
}
|
|
}
|
|
|
|
_ = briefkopf.AppendLine($"{AccountName}");
|
|
if (tex) { briefkopf.AppendLine($"\\\\"); }
|
|
|
|
_ = briefkopf.AppendLine($"{contactVM.AccountStreet}");
|
|
if (tex) { briefkopf.AppendLine($"\\\\"); }
|
|
|
|
_ = briefkopf.AppendLine($"{contactVM.AccountZIP} {contactVM.AccountCity}");
|
|
if (tex) { briefkopf.AppendLine($"\\\\"); }
|
|
|
|
return briefkopf;
|
|
}
|
|
|
|
public Task HandleAsync(ShellViewModel message, CancellationToken cancellationToken)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
}
|