Files
ayanova7/source/Plugins/AyaNovaOL/ExportContactsToAyaNova.cs
2018-06-29 19:47:36 +00:00

462 lines
21 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using GZTW.AyaNova.BLL;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;
using System.Collections;
namespace AyaNovaOL
{
public class ExportContactsToAyaNova
{
private RootObjectTypes olRootObjectType;
private Dictionary<string,Guid> mIDList;
public bool DoExport(bool bSelectedOnly, Outlook.Explorer exp)
{
Cursor.Current = Cursors.WaitCursor;
if (bSelectedOnly && exp.Selection.Count < 1) return true;
olRootObjectType = (RootObjectTypes)AyaNovaOL.Properties.Settings.Default.RootObjectType;
//Because fuzzy fetch can only work with client object types we need to build a list of other types first
if (olRootObjectType == RootObjectTypes.HeadOffice)
{
if (mIDList == null) mIDList = new Dictionary<string, Guid>();
mIDList.Clear();
GenericNVList l = GenericNVList.GetList("HEADOFFICE", "ID", "NAME", true, false, true);
foreach (DictionaryEntry d in l.BindableList)
mIDList.Add(d.Value.ToString().ToLower(),new Guid(d.Key.ToString()));
}
else if (olRootObjectType == RootObjectTypes.Vendor)
{
if (mIDList == null) mIDList = new Dictionary<string, Guid>();
mIDList.Clear();
VendorPickList cpl = VendorPickList.GetList((VendorTypes)AyaNovaOL.Properties.Settings.Default.VendorType);
foreach (VendorPickList.VendorPickListInfo i in cpl)
mIDList.Add(i.Name.ToLower(), i.ID);
}
if (bSelectedOnly)
{
for (int x = 0; x < exp.Selection.Count; x++)
{
ExportItem((Outlook.ContactItem)exp.Selection[x+1]);
}
}
else
{
for (int x = 0; x < exp.CurrentFolder.Items.Count; x++)
{
ExportItem((Outlook.ContactItem)exp.CurrentFolder.Items[x + 1]);
}
}
return true;
}
private string GetPhoneNumber(RootObjectTypes obType, int nAyaPhoneSlot, Outlook.ContactItem ci)
{
//////Util.d("Get phone number top. OBTYPE: " + obType.ToString() + ", slot: " + nAyaPhoneSlot.ToString());
#region GetPhoneNumber
AYOLPhoneNumberType ptype = AYOLPhoneNumberType.BusinessTelephoneNumber;
switch (obType)
{
case RootObjectTypes.Client:
switch (nAyaPhoneSlot)
{
case 1:
ptype = (AYOLPhoneNumberType)AyaNovaOL.Properties.Settings.Default.ClientPhone1;
break;
case 2:
ptype = (AYOLPhoneNumberType)AyaNovaOL.Properties.Settings.Default.ClientPhone2;
break;
case 3:
ptype = (AYOLPhoneNumberType)AyaNovaOL.Properties.Settings.Default.ClientPhone3;
break;
case 4:
ptype = (AYOLPhoneNumberType)AyaNovaOL.Properties.Settings.Default.ClientPhone4;
break;
case 5:
ptype = (AYOLPhoneNumberType)AyaNovaOL.Properties.Settings.Default.ClientPhone5;
break;
}
break;
case RootObjectTypes.HeadOffice:
switch (nAyaPhoneSlot)
{
case 1:
ptype = (AYOLPhoneNumberType)AyaNovaOL.Properties.Settings.Default.HeadOfficePhone1;
break;
case 2:
ptype = (AYOLPhoneNumberType)AyaNovaOL.Properties.Settings.Default.HeadOfficePhone2;
break;
case 3:
ptype = (AYOLPhoneNumberType)AyaNovaOL.Properties.Settings.Default.HeadOfficePhone3;
break;
case 4:
ptype = (AYOLPhoneNumberType)AyaNovaOL.Properties.Settings.Default.HeadOfficePhone4;
break;
case 5:
ptype = (AYOLPhoneNumberType)AyaNovaOL.Properties.Settings.Default.HeadOfficePhone5;
break;
}
break;
case RootObjectTypes.Vendor:
switch (nAyaPhoneSlot)
{
case 1:
ptype = (AYOLPhoneNumberType)AyaNovaOL.Properties.Settings.Default.VendorPhone1;
break;
case 2:
ptype = (AYOLPhoneNumberType)AyaNovaOL.Properties.Settings.Default.VendorPhone2;
break;
case 3:
ptype = (AYOLPhoneNumberType)AyaNovaOL.Properties.Settings.Default.VendorPhone3;
break;
case 4:
ptype = (AYOLPhoneNumberType)AyaNovaOL.Properties.Settings.Default.VendorPhone4;
break;
case 5:
ptype = (AYOLPhoneNumberType)AyaNovaOL.Properties.Settings.Default.VendorPhone5;
break;
}
break;
}
//////Util.d("Get phone number - Got type: " + ptype.ToString());
switch (ptype)
{
case AYOLPhoneNumberType.Unused:
return "";
case AYOLPhoneNumberType.AssistantTelephoneNumber:
return ss(ci.AssistantTelephoneNumber);
case AYOLPhoneNumberType.Business2TelephoneNumber:
return ss(ci.Business2TelephoneNumber);
case AYOLPhoneNumberType.BusinessFaxNumber:
return ss(ci.BusinessFaxNumber);
case AYOLPhoneNumberType.BusinessTelephoneNumber:
return ss(ci.BusinessTelephoneNumber);
case AYOLPhoneNumberType.CallbackTelephoneNumber:
return ss(ci.CallbackTelephoneNumber);
case AYOLPhoneNumberType.CarTelephoneNumber:
return ss(ci.CarTelephoneNumber);
case AYOLPhoneNumberType.CompanyMainTelephoneNumber:
return ss(ci.CompanyMainTelephoneNumber);
case AYOLPhoneNumberType.Home2TelephoneNumber:
return ss(ci.Home2TelephoneNumber);
case AYOLPhoneNumberType.HomeFaxNumber:
return ss(ci.HomeFaxNumber);
case AYOLPhoneNumberType.HomeTelephoneNumber:
return ss(ci.HomeTelephoneNumber);
case AYOLPhoneNumberType.ISDNNumber:
return ss(ci.ISDNNumber);
case AYOLPhoneNumberType.MobileTelephoneNumber:
return ss(ci.MobileTelephoneNumber);
case AYOLPhoneNumberType.OtherFaxNumber:
return ss(ci.OtherFaxNumber);
case AYOLPhoneNumberType.OtherTelephoneNumber:
return ss(ci.OtherTelephoneNumber);
case AYOLPhoneNumberType.PagerNumber:
return ss(ci.PagerNumber);
case AYOLPhoneNumberType.PrimaryTelephoneNumber:
return ss(ci.PrimaryTelephoneNumber);
case AYOLPhoneNumberType.RadioTelephoneNumber:
return ss(ci.RadioTelephoneNumber);
case AYOLPhoneNumberType.TelexNumber:
return ss(ci.TelexNumber);
case AYOLPhoneNumberType.TTYTDDTelephoneNumber:
return ss(ci.TTYTDDTelephoneNumber);
}
return "";
#endregion getphone number
}
/// <summary>
/// Get the lowest numbered (most primary) email address
/// that actually contains information
/// </summary>
/// <param name="ci"></param>
/// <returns></returns>
private string GetEmailAddress(Outlook.ContactItem ci)
{
string sEmail = "";
if(!string.IsNullOrEmpty(ss(ci.Email3Address))) sEmail = ss(ci.Email3Address);
if (!string.IsNullOrEmpty(ss(ci.Email2Address))) sEmail = ss(ci.Email2Address);
if (!string.IsNullOrEmpty(ss(ci.Email1Address))) sEmail = ss(ci.Email1Address);
return sEmail;
}
private void SetAddress(AddressTypes adrType, GZTW.AyaNova.BLL.Address adr, Outlook.ContactItem ci)
{
//Util.d("Set Address top. adrType: " + adrType.ToString() + ", Address: " + adr.ToString());
#region is postal and use ThisIsTheMailingAddress
if (adrType == AddressTypes.Postal && !AyaNovaOL.Properties.Settings.Default.IgnoreThisIsTheMailingAddress)
{
//User has chosen to use the mailing address
//see if it's empty and use it if it's not
if (!string.IsNullOrEmpty(ss(ci.MailingAddress)))
{
//Util.d("Set address. Using ThisIsTheMailingAddress");
adr.DeliveryAddress = ss(ci.MailingAddressStreet);
if (!string.IsNullOrEmpty(ss(ci.MailingAddressPostOfficeBox)))
adr.DeliveryAddress += "\r\n" + ss(ci.MailingAddressPostOfficeBox);
adr.City = ss(ci.MailingAddressCity);
adr.StateProv = ss(ci.MailingAddressState);
adr.Country = ss(ci.MailingAddressCountry);
adr.Postal = ss(ci.MailingAddressPostalCode);
return;
}
}
#endregion
#region Forced address setting
AYOLAddressType OLAddressTypeToUse = AYOLAddressType.Unused;
if (adrType == AddressTypes.Physical)
OLAddressTypeToUse = (AYOLAddressType)AyaNovaOL.Properties.Settings.Default.PhysicalAddressEquals;
else
OLAddressTypeToUse = (AYOLAddressType)AyaNovaOL.Properties.Settings.Default.PostalAddressEquals;
switch (OLAddressTypeToUse)
{
case AYOLAddressType.Business:
{
//Util.d("Set address. Using biz");
//see if it's empty and use it if it's not
if (!string.IsNullOrEmpty(ss(ci.BusinessAddress)))
{
adr.DeliveryAddress = ss(ci.BusinessAddressStreet);
if (!string.IsNullOrEmpty(ss(ci.BusinessAddressPostOfficeBox)))
adr.DeliveryAddress += "\r\n" + ss(ci.BusinessAddressPostOfficeBox);
adr.City = ss(ci.BusinessAddressCity);
adr.StateProv = ss(ci.BusinessAddressState);
adr.Country = ss(ci.BusinessAddressCountry);
adr.Postal = ss(ci.BusinessAddressPostalCode);
}
return;
}
case AYOLAddressType.Home:
{
//Util.d("Set address. Using Home");
try
{
//see if it's empty and use it if it's not
if (!string.IsNullOrEmpty(ss(ci.HomeAddress)))
{
adr.DeliveryAddress = ss(ci.HomeAddressStreet);
if (!string.IsNullOrEmpty(ss(ci.HomeAddressPostOfficeBox)))
adr.DeliveryAddress += "\r\n" + ss(ci.HomeAddressPostOfficeBox);
adr.City = ss(ci.HomeAddressCity);
adr.StateProv = ss(ci.HomeAddressState);
adr.Country = ss(ci.HomeAddressCountry);
adr.Postal = ss(ci.HomeAddressPostalCode);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\r\nStack trace:\r\n" + ex.StackTrace);
throw (ex);
}
return;
}
case AYOLAddressType.Other:
{
//Util.d("Set address. Using Other");
//see if it's empty and use it if it's not
if (!string.IsNullOrEmpty(ss(ci.OtherAddress)))
{
adr.DeliveryAddress = ss(ci.OtherAddressStreet);
if (!string.IsNullOrEmpty(ss(ci.OtherAddressPostOfficeBox)))
adr.DeliveryAddress += "\r\n" + ss(ci.OtherAddressPostOfficeBox);
adr.City = ss(ci.OtherAddressCity);
adr.StateProv = ss(ci.OtherAddressState);
adr.Country = ss(ci.OtherAddressCountry);
adr.Postal = ss(ci.OtherAddressPostalCode);
}
return;
}
}//switch
#endregion
//Util.d("Set address done");
}
private bool ExportItem(Outlook.ContactItem ci)
{
string sName = ci.CompanyName;
if (string.IsNullOrEmpty(sName))
sName = ci.FullName;
if (string.IsNullOrEmpty(sName)) return false;
switch (olRootObjectType)
{
case RootObjectTypes.Client:
{
////Util.d("Top of export client");
Client c = FuzzyFetch.GetMeThe<Client>(sName);
c.RegionID = AyaNovaOL.Properties.Settings.Default.Region;
c.DispatchZoneID = AyaNovaOL.Properties.Settings.Default.Zone;
c.ContractID = AyaNovaOL.Properties.Settings.Default.Contract;
if (c.IsNew)
{
c.ContractExpires = DateTime.Now.AddYears(1);
c.ContactNotes = "Imported from Outlook: " + System.DateTime.Now.ToString();
}
////Util.d("export client - email");
c.Email = GetEmailAddress(ci);
c.Notes = ss(ci.Body);
////Util.d("export client - phone numbers");
c.Phone1 = GetPhoneNumber(RootObjectTypes.Client, 1,ci);
c.Phone2 = GetPhoneNumber(RootObjectTypes.Client, 2, ci);
c.Phone3 = GetPhoneNumber(RootObjectTypes.Client, 3, ci);
c.Phone4 = GetPhoneNumber(RootObjectTypes.Client, 4, ci);
////Util.d("Get slot 5 number...");
c.Phone5 = GetPhoneNumber(RootObjectTypes.Client, 5, ci);
////Util.d("export client - Address physical");
SetAddress(AddressTypes.Physical, c.GoToAddress, ci);
////Util.d("export client - Address mail to");
SetAddress(AddressTypes.Postal, c.MailToAddress, ci);
c.Contact = ss(ci.FullName);
c.WebAddress = ss(ci.WebPage);
if (c.IsSavable)
c.Save();
////Util.d("export client - saved/done");
}
break;
case RootObjectTypes.HeadOffice:
{
HeadOffice c = null;
if (mIDList.ContainsKey(sName.ToLower()))
{
c=HeadOffice.GetItem(mIDList[sName.ToLower()]);
}
else
{
c=HeadOffice.NewItem();
c.Name=sName;
}
//FuzzyFetch.GetMeThe<HeadOffice>(sName);
c.RegionID = AyaNovaOL.Properties.Settings.Default.Region;
c.ContractID = AyaNovaOL.Properties.Settings.Default.Contract;
if (c.IsNew)
{
c.ContractExpires = DateTime.Now.AddYears(1);
c.ContactNotes = "Imported from Outlook: " + System.DateTime.Now.ToString();
}
c.Email = GetEmailAddress(ci);
c.Notes = ss(ci.Body);
c.Phone1 = GetPhoneNumber(RootObjectTypes.Client, 1, ci);
c.Phone2 = GetPhoneNumber(RootObjectTypes.Client, 2, ci);
c.Phone3 = GetPhoneNumber(RootObjectTypes.Client, 3, ci);
c.Phone4 = GetPhoneNumber(RootObjectTypes.Client, 4, ci);
////Util.d("Getting phone slot 5...");
c.Phone5 = GetPhoneNumber(RootObjectTypes.Client, 5, ci);
////Util.d("Got slot 5, getting physical address");
SetAddress(AddressTypes.Physical, c.GoToAddress, ci);
SetAddress(AddressTypes.Postal, c.MailToAddress, ci);
c.Contact = ss(ci.FullName);
c.WebAddress = ss(ci.WebPage);
if (c.IsSavable)
c.Save();
}
break;
case RootObjectTypes.Vendor:
{
//Vendor c = FuzzyFetch.GetMeThe<Vendor>(sName);
Vendor c = null;
if (mIDList.ContainsKey(sName.ToLower()))
{
c = Vendor.GetItem(mIDList[sName.ToLower()]);
}
else
{
c = Vendor.NewItem();
c.Name = sName;
}
c.VendorType = (VendorTypes)AyaNovaOL.Properties.Settings.Default.VendorType;
if (c.IsNew)
{
c.ContactNotes = "Imported from Outlook: " + System.DateTime.Now.ToString();
}
c.Email = GetEmailAddress(ci);
c.Notes = ss(ci.Body);
c.Phone1 = GetPhoneNumber(RootObjectTypes.Client, 1, ci);
c.Phone2 = GetPhoneNumber(RootObjectTypes.Client, 2, ci);
c.Phone3 = GetPhoneNumber(RootObjectTypes.Client, 3, ci);
c.Phone4 = GetPhoneNumber(RootObjectTypes.Client, 4, ci);
c.Phone5 = GetPhoneNumber(RootObjectTypes.Client, 5, ci);
SetAddress(AddressTypes.Physical, c.GoToAddress, ci);
SetAddress(AddressTypes.Postal, c.MailToAddress, ci);
c.Contact = ss(ci.FullName);
c.WebAddress = ss(ci.WebPage);
if (c.IsSavable)
c.Save();
}
break;
}
return true;
}
//Note: though it's not documented anywhere it appears that outlook
//or the interop layer at least throws a null reference exception
//if you try to return an empty phone number or one that isn't selected as one of the visible
//ones in the outlook UI
//tried to find out why but could get no results however the below method works
//why setting a variable from the phone number works while returning it throws an exception is a mystery
private string ss(object ContactItemField)
{
if (ContactItemField == null) return "";
return ContactItemField.ToString();
}
}//end of class
//end of namespace
}