using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GZTW.AyaNova.BLL;
using System.Windows.Forms;
using FileHelpers;
using System.Collections;
namespace AyaNova.PlugIn.ImportExportCSV
{
public class ExportCSV
{
///
/// Export the indicated objects to csv file
///
public static void ExportList(RootObjectTypes objectType, List objectIDList)
{
string objectName = EnumDescConverter.GetEnumDescription(objectType);
SaveFileDialog sf = new SaveFileDialog();
sf.DefaultExt = ".csv";
sf.FileName = objectType.ToString() + ".csv";
if (sf.ShowDialog() != DialogResult.OK) return;
if (objectIDList == null) objectIDList = new List();
FileHelperEngine engine = null;
System.Collections.ArrayList al = new ArrayList();
//Got the file name now to export
switch (objectType)
{
case RootObjectTypes.Client:
{
#region export clients
//zero selected? Then export all.
if (objectIDList.Count == 0)
{
ClientPickList cpl = ClientPickList.GetList();
foreach (ClientPickList.ClientPickListInfo i in cpl)
objectIDList.Add(i.ID);
}
FHClient fhc;
Client c;
engine = new FileHelperEngine(typeof(FHClient));
Waiting w = new Waiting();
w.Show();
w.Ops = "Exporting " + objectName;
foreach (Guid id in objectIDList)
{
c = Client.GetItem(id);
ClientList cl = ClientList.GetListForSingleItem(id);
fhc = new FHClient();
w.Step = c.Name;
fhc.Name = c.Name;
fhc.AccountNumber = c.AccountNumber;
//contact fields
fhc.Contact = c.Contact;
fhc.ContactNotes = c.ContactNotes;
fhc.Phone1 = c.Phone1;
fhc.Phone2 = c.Phone2;
fhc.Phone3 = c.Phone3;
fhc.Phone4 = c.Phone4;
fhc.Phone5 = c.Phone5;
fhc.EmailAddress = c.Email;
fhc.DeliveryAddress = c.GoToAddress.DeliveryAddress;
fhc.DeliveryCity = c.GoToAddress.City;
fhc.DeliveryCountry = c.GoToAddress.Country;
fhc.DeliveryPostal = c.GoToAddress.Postal;
fhc.DeliveryStateProvince = c.GoToAddress.StateProv;
fhc.GeneralNotes = c.Notes;
fhc.MailAddress = c.MailToAddress.DeliveryAddress;
fhc.MailCity = c.MailToAddress.City;
fhc.MailCountry = c.MailToAddress.Country;
fhc.MailPostal = c.MailToAddress.Postal;
fhc.MailStateProvince = c.MailToAddress.StateProv;
fhc.PopupNotes = c.PopUpNotes;
fhc.TechNotes = c.TechNotes;
fhc.WebSite = c.WebAddress;
//case 1737
fhc.ClientGroup = cl[0].LT_O_ClientGroup.Display;
fhc.DispatchZone = cl[0].LT_O_DispatchZone.Display;
fhc.Region = cl[0].LT_O_Region.Display;
al.Add(fhc);
}
w.Close();
try
{
engine.WriteFile(sf.FileName, (object[])al.ToArray(typeof(FHClient)));
}
catch (Exception ex)
{
MessageBox.Show("Error writing the export file:\r\n\r\n" + ex.Message);
return;
}
MessageBox.Show("Selected clients have been exported to file:\r\n" +
sf.FileName);
#endregion
}
break;
case RootObjectTypes.Part:
{
#region Export parts
#region cached lists
//Load cached lists
htManufacturers.Clear();
htWholesalers.Clear();
VendorPickList vp = VendorPickList.GetList();
foreach (VendorPickList.VendorPickListInfo i in vp)
{
switch (i.VendorType)
{
case VendorTypes.Manufacturer:
//case 3606
if (!htManufacturers.ContainsKey(i.Name))
htManufacturers.Add(i.Name, i.ID);
break;
case VendorTypes.Wholesaler:
//case 3606
if (!htWholesalers.ContainsKey(i.Name))
htWholesalers.Add(i.Name, i.ID);
break;
default:
break;
}
}
Measures = UnitOfMeasures.GetItems();
Categories = PartCategories.GetItems();
#endregion
engine = new FileHelperEngine(typeof(FHPart));
FHPart fhc;
Part p;
//zero selected? Then export all.
if (objectIDList.Count == 0)
{
PartPickList ppl = PartPickList.GetAllParts();
foreach (PartPickList.PartPickListInfo i in ppl)
objectIDList.Add(i.ID);
}
Waiting w = new Waiting();
w.Show();
w.Ops = "Exporting " + objectName;
foreach (Guid id in objectIDList)
{
p = Part.GetItem(id);
fhc = new FHPart();
w.Step = p.Name + " " + p.PartNumber;
fhc.Number = p.PartNumber;
fhc.Name = p.Name;
fhc.AlternativeWholeSaler = GetWholesalerByID(p.AlternativeWholesalerID);
fhc.AlternativeWholeSalerNumber = p.AlternativeWholesalerNumber;
fhc.Category = "";
for (int x = 0; x < Categories.Count; x++)
{
if (Categories[x].ID == p.PartCategoryID)
{
fhc.Category = Categories[x].Name;
break;
}
}
fhc.Cost = p.Cost.ToString();
fhc.Manufacturer = GetManufacturerByID(p.ManufacturerID);
fhc.ManufacturerNumber = p.ManufacturerNumber;
fhc.Notes = p.Notes;
fhc.Retail = p.Retail.ToString();
fhc.TrackSerialNumber = (p.TrackSerialNumber ? "TRUE" : "FALSE");
fhc.UnitOfMeasure = "";
for (int x = 0; x < Measures.Count; x++)
{
if (Measures[x].ID == p.UnitOfMeasureID)
{
fhc.UnitOfMeasure = Measures[x].Name;
break;
}
}
fhc.UPC = p.UPC;
fhc.WholeSaler = GetWholesalerByID(p.WholesalerID);
fhc.WholeSalerNumber = p.WholesalerNumber;
al.Add(fhc);
}
w.Close();
try
{
engine.WriteFile(sf.FileName, (object[])al.ToArray(typeof(FHPart)));
}
catch (Exception ex)
{
MessageBox.Show("Error writing the export file:\r\n\r\n" + ex.Message);
return;
}
MessageBox.Show("Selected items have been exported to file:\r\n" +
sf.FileName);
#endregion
}
break;
case RootObjectTypes.Unit:
{
#region Export Units
#region cached lists
//Load cached lists
try
{
htManufacturers.Clear();
htWholesalers.Clear();
htClients.Clear();
htModels.Clear();
htUnits.Clear();
VendorPickList vp = VendorPickList.GetList();
foreach (VendorPickList.VendorPickListInfo i in vp)
{
switch (i.VendorType)
{
case VendorTypes.Manufacturer:
//case 3606
if (!htManufacturers.ContainsKey(i.Name))
htManufacturers.Add(i.Name, i.ID);
break;
case VendorTypes.Wholesaler:
//case 3606
if (!htWholesalers.ContainsKey(i.Name))
htWholesalers.Add(i.Name, i.ID);
break;
default:
break;
}
}
ClientPickList p = ClientPickList.GetList();
foreach (ClientPickList.ClientPickListInfo i in p)
{
//case 3606
if (!htClients.ContainsKey(i.Name))
htClients.Add(i.Name, i.ID);
}
UnitModelPickList upl = UnitModelPickList.GetList();
foreach (UnitModelPickList.UnitModelPickListInfo i in upl)
{
//case 3606
if (!htModels.ContainsKey(i.Name))
htModels.Add(i.Name, i.ID);
}
UnitPickList unpl = UnitPickList.GetListOfAll();
foreach (UnitPickList.UnitPickListInfo i in unpl)
{
//case 3606
if (!htUnits.ContainsKey(i.Serial))
htUnits.Add(i.Serial, i.ID);
}
}
catch (Exception ex)
{
if (ex is System.ArgumentException)
{
MessageBox.Show(
"An error has occurred while attempting to retrieve the lists of objects\r\n" +
"already stored in AyaNova which are used to match to the data being imported.\r\n" +
"(Clients, Unit Models, Vendors Units etc)\r\n\r\n" +
"A duplicate record was found with the same name and must be removed or changed\r\n" +
"before you can proceed with this utility. Details below:\r\n\r\n" +
ex.Message, "Duplicate object name in AyaNova");
}
else
MessageBox.Show("An error has occurred while attempting to retrieve the lists of objects\r\n" +
"already stored in AyaNova which are used to match to the data being imported.\r\n" +
"The import utility can not proceed, details below:\r\n\r\n", ex.Message);
return;
}
#endregion
//zero selected? Then export all.
if (objectIDList.Count == 0)
{
UnitPickList ppl = UnitPickList.GetListOfAll();
foreach (UnitPickList.UnitPickListInfo i in ppl)
objectIDList.Add(i.ID);
}
FHUnit fhc;
Unit u;
engine = new FileHelperEngine(typeof(FHUnit));
Waiting w = new Waiting();
w.Show();
w.Ops = "Exporting " + objectName;
foreach (Guid id in objectIDList)
{
u = Unit.GetItem(id);
fhc = new FHUnit();
w.Step = u.Serial;
fhc.Serial = u.Serial;
fhc.Client = GetClientByID(u.ClientID);
fhc.Description = u.Description;
fhc.Metered = (u.Metered ? "TRUE" : "FALSE");
fhc.Notes = u.Notes;
if (u.PurchasedDate == System.DBNull.Value)
fhc.PurchasedDate = "";
else
{
DateTime dtPurchased = System.Convert.ToDateTime(u.PurchasedDate);
fhc.PurchasedDate = dtPurchased.ToString();
}
fhc.PurchasedFromVendor = GetPurchasedFromByID(u.PurchasedFromID);
fhc.SalesReceiptNumber = u.Receipt;
fhc.UnitModel = GetModelByID(u.UnitModelID);
fhc.WarrantyLength = u.WarrantyLength.ToString();//case 1739
al.Add(fhc);
}
w.Close();
try
{
engine.WriteFile(sf.FileName, (object[])al.ToArray(typeof(FHUnit)));
}
catch (Exception ex)
{
MessageBox.Show("Error writing the export file:\r\n\r\n" + ex.Message);
return;
}
MessageBox.Show("Selected items have been exported to file:\r\n" +
sf.FileName);
#endregion
}
break;
}//end of object type switch
}//end of method
//Part and unit helpers
static Hashtable htManufacturers = new Hashtable();
static Hashtable htWholesalers = new Hashtable();
#region part helpers
//These are name value lists used to
//keep track of the id's of existing items
//that are either pre-existing or have been created during import
//this supports the code that matches text names of objects to their id's
//i.e. "IBM" as a manufacturer to the IBM record in AyaNova already
//case is not taken into account during comparison to ensure no little
//mismatches due to a small typo in the import file
static UnitOfMeasures Measures;//=UnitOfMeasures.GetItems();
static PartCategories Categories;//=PartCategories.GetItems();
private static string GetWholesalerByID(Guid ID)
{
foreach (DictionaryEntry de in htWholesalers)
{
if ((Guid)de.Value == ID)
return de.Key.ToString();
}
return "";
}
private static string GetManufacturerByID(Guid ID)
{
foreach (DictionaryEntry de in htManufacturers)
{
if ((Guid)de.Value == ID)
return de.Key.ToString();
}
return "";
}
#endregion
#region Unit helpers
static Hashtable htClients = new Hashtable();
static Hashtable htModels = new Hashtable();
static Hashtable htUnits = new Hashtable();
static private string GetPurchasedFromByID(Guid ID)
{
foreach (DictionaryEntry de in htWholesalers)
{
if ((Guid)de.Value == ID)
return de.Key.ToString();
}
return "";
}
static private string GetClientByID(Guid ID)
{
foreach (DictionaryEntry de in htClients)
{
if ((Guid)de.Value == ID)
return de.Key.ToString();
}
return "";
}
static private string GetModelByID(Guid ID)
{
foreach (DictionaryEntry de in htModels)
{
if ((Guid)de.Value == ID)
return de.Key.ToString();
}
return "";
}
#endregion
}//end of class
}//end of namespace