888 lines
32 KiB
C#
888 lines
32 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
using System.Drawing;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Windows.Forms;
|
||
//******* SAMPLE ********************
|
||
//The AyaNova business object library:
|
||
using GZTW.AyaNova.BLL;
|
||
|
||
//The security library required for
|
||
//processing the login
|
||
using CSLA.Security;
|
||
|
||
//Used for login confirmation
|
||
using System.Threading;
|
||
//*************************************
|
||
|
||
namespace UniversalImports
|
||
{
|
||
public partial class Form1 : Form
|
||
{
|
||
public Form1()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
|
||
private void Form1_Load(object sender, EventArgs e)
|
||
{
|
||
//Initialize AyaNova biz object library
|
||
try
|
||
{
|
||
GZTW.AyaNova.BLL.AyaBizUtils.Initialize();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
//"crack" the exception to get to the innermost exception
|
||
while (ex.InnerException != null)
|
||
ex = ex.InnerException;
|
||
|
||
MessageBox.Show(ex.Message);
|
||
return;
|
||
}
|
||
|
||
AyaBizUtils.Login("manager", "letmein");
|
||
|
||
//confirm the user is logged in:
|
||
if (Thread.CurrentPrincipal.Identity.IsAuthenticated == false)
|
||
{
|
||
//Nope, they are not authenticated so
|
||
//show an error and bail out
|
||
MessageBox.Show("Login failed");
|
||
return;
|
||
}
|
||
|
||
|
||
|
||
|
||
}//eoFormLoad
|
||
|
||
|
||
private void viewFileToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
DataTable dt = new DataTable();
|
||
ReadCSV(dataGridView1, ref dt, @"C:\temp\morrow\wo1.csv");
|
||
}
|
||
|
||
System.Text.StringBuilder sbErrors = new StringBuilder();
|
||
private void importToolStripMenuItem_Click(object sender, EventArgs e)
|
||
{
|
||
System.Media.SystemSounds.Beep.Play();
|
||
//erase database
|
||
DBManager.EraseDatabase("I_UNDERSTAND_ALL_DATA_WILL_BE_ERASED_AND_I_ACCEPT_RESPONSIBILITY");
|
||
|
||
this.pb.Maximum = 9;
|
||
this.pb.Value = 1;
|
||
|
||
UserPickList up = UserPickList.GetList(false);
|
||
System.Diagnostics.Debug.WriteLine("Can retrieve client list, db ready for import proceeding to first item now...");
|
||
|
||
//Headoffice
|
||
ImportHeadOffice();
|
||
this.pb.Value = 2;
|
||
|
||
//client
|
||
ImportClient();
|
||
this.pb.Value = 3;
|
||
|
||
//vendor
|
||
ImportVendor();
|
||
this.pb.Value = 4;
|
||
|
||
//unit
|
||
ImportUnit();
|
||
this.pb.Value = 5;
|
||
|
||
//pmtemplate
|
||
ImportPMTemplate();
|
||
this.pb.Value = 6;
|
||
|
||
//wo1
|
||
ImportWorkorder("WORKORDERS-OPEN-2011-0406.csv");
|
||
this.pb.Value = 7;
|
||
|
||
//wo2
|
||
ImportWorkorder("WORKORDERS-CLOSED-2011-0405.csv");
|
||
this.pb.Value = 8;
|
||
|
||
//PM
|
||
ImportPMWorkorders();
|
||
this.pb.Value = 9;
|
||
|
||
System.Media.SystemSounds.Beep.Play();
|
||
|
||
if (sbErrors.Length > 0)
|
||
{
|
||
MessageBox.Show("DONE with errors written to errors.txt in csv folder");
|
||
System.IO.File.WriteAllText(@"C:\temp\morrow\errors.txt", sbErrors.ToString());
|
||
}
|
||
else
|
||
MessageBox.Show("Done, no errors");
|
||
|
||
}
|
||
|
||
#region Headoffice
|
||
Dictionary<string, Guid> HeadOffices = new Dictionary<string, Guid>();
|
||
private bool ImportHeadOffice()
|
||
{
|
||
DataTable dt = new DataTable();
|
||
ReadCSV(dataGridView1, ref dt, @"C:\temp\morrow\HEADOFFICE-ROLLUP.csv");
|
||
Dictionary<string, string> d = new Dictionary<string, string>();
|
||
bool first = true;
|
||
pbr.ForeColor = Color.Black;
|
||
this.pbr.Maximum = dt.Rows.Count;
|
||
this.pbr.Value = 0;
|
||
foreach (DataRow dr in dt.Rows)
|
||
{
|
||
if (first)
|
||
{
|
||
first = false;
|
||
continue;
|
||
}
|
||
string name = v(dr, 0, 255);
|
||
string contact = v(dr, 1, 500);
|
||
if (!d.ContainsKey(name))
|
||
{
|
||
d.Add(name, contact);
|
||
}
|
||
else
|
||
{
|
||
//do we have more info than the existing one?
|
||
if (d[name].Length < contact.Length)
|
||
d[name] = contact;
|
||
}
|
||
pbr.Value++;
|
||
}
|
||
|
||
pbr.Maximum = d.Count;
|
||
pbr.Value = 0;
|
||
pbr.ForeColor = Color.Green;
|
||
foreach (KeyValuePair<string, string> k in d)
|
||
{
|
||
HeadOffice h = HeadOffice.NewItem();
|
||
h.Name = k.Key;
|
||
h.Contact = k.Value;
|
||
HeadOffices.Add(k.Key, h.ID);
|
||
System.Diagnostics.Debug.WriteLine("About to save headoffice: " + h.Name + " Which is import number:" + pbr.Value.ToString());
|
||
h.Save();
|
||
pbr.Value++;
|
||
}
|
||
|
||
pbr.Value = 0;
|
||
//MessageBox.Show("Done head office");
|
||
return true;
|
||
}
|
||
#endregion
|
||
|
||
#region Client
|
||
Dictionary<string, Guid> ClientList = new Dictionary<string, Guid>();
|
||
private bool ImportClient()
|
||
{
|
||
//0="Client Name"
|
||
//1="Account Number"
|
||
//2="Bill Head Office"
|
||
//3="Head Office"
|
||
//4="Contract (Name/Type)"
|
||
//5="Contract Expires"
|
||
//6="Customer Postal Address"
|
||
//7="Client Contact"
|
||
//8="CLIENT General Notes"
|
||
|
||
DataTable dt = new DataTable();
|
||
ReadCSV(dataGridView1, ref dt, @"C:\temp\morrow\CLIENT-UpdateContract-2011-0406.csv");
|
||
Dictionary<string, Guid> Contracts = new Dictionary<string, Guid>();
|
||
bool first = true;
|
||
pbr.Value = 0;
|
||
pbr.Maximum = dt.Rows.Count;
|
||
pbr.ForeColor = Color.Red;
|
||
foreach (DataRow dr in dt.Rows)
|
||
{
|
||
pbr.Value++;
|
||
if (first)
|
||
{
|
||
first = false;
|
||
continue;
|
||
}
|
||
string name = v(dr, 0, 255);
|
||
string AccountNumber = v(dr, 1, 255);
|
||
bool BillHeadOffice = v(dr, 2, 10).Equals("YES", StringComparison.InvariantCultureIgnoreCase);
|
||
string HeadOfficeName = v(dr, 3, 255);
|
||
string contractName = v(dr, 4, 255);
|
||
if (contractName.Contains("None"))
|
||
contractName = string.Empty;
|
||
Guid ContractID = Guid.Empty;
|
||
|
||
string sContractExpires = v(dr, 5, 255);
|
||
//parse
|
||
DateTime dtContractExpires = new DateTime(2010, 12, 31);
|
||
DateTime.TryParse(sContractExpires, out dtContractExpires);
|
||
|
||
string sAddress = v(dr, 6, 255);
|
||
|
||
string sContact = v(dr, 7, 500);
|
||
string sNotes = v(dr, 8, 32767);
|
||
|
||
//Unique clients only
|
||
if (!ClientList.ContainsKey(name))
|
||
{
|
||
|
||
//add contract?
|
||
#region contract
|
||
if (contractName != string.Empty)
|
||
{
|
||
if (!Contracts.ContainsKey(contractName))
|
||
{
|
||
//add it
|
||
Contract ct = Contract.NewItem();
|
||
ct.Active = true;
|
||
ct.ContractRatesOnly = false;
|
||
ct.Name = contractName;
|
||
ct.RegionID = GZTW.AyaNova.BLL.Region.DefaultRegionID;
|
||
ContractID = ct.ID;
|
||
ct.Save();
|
||
Contracts.Add(contractName, ContractID);
|
||
}
|
||
else
|
||
ContractID = Contracts[contractName];
|
||
|
||
}
|
||
#endregion contract
|
||
|
||
//Import client
|
||
Client c = Client.NewItem();
|
||
c.Name = name;
|
||
c.AccountNumber = AccountNumber;
|
||
c.HeadOfficeID = HeadOffices[HeadOfficeName];
|
||
c.BillHeadOffice = BillHeadOffice;
|
||
if (ContractID != Guid.Empty)
|
||
{
|
||
c.ContractID = ContractID;
|
||
c.ContractExpires = dtContractExpires;
|
||
}
|
||
|
||
|
||
c.Active = true;
|
||
c.MailToAddress.DeliveryAddress = sAddress;
|
||
c.Contact = sContact;
|
||
c.Notes = sNotes;
|
||
ClientList.Add(name, c.ID);
|
||
c.Save();
|
||
|
||
}
|
||
|
||
}
|
||
pbr.Value = 0;
|
||
// MessageBox.Show("Done Client");
|
||
return true;
|
||
}
|
||
#endregion
|
||
|
||
#region Vendor
|
||
Dictionary<string, Guid> ImportedManufacturers = new Dictionary<string, Guid>();
|
||
private bool ImportVendor()
|
||
{
|
||
DataTable dt = new DataTable();
|
||
ReadCSV(dataGridView1, ref dt, @"C:\temp\morrow\VENDOR-ROLLUP-UNIQUE-2.csv");
|
||
|
||
List<string> lVendors = new List<string>();
|
||
bool first = true;
|
||
pbr.Value = 0;
|
||
pbr.Maximum = dt.Rows.Count;
|
||
pbr.ForeColor = Color.Orange;
|
||
foreach (DataRow dr in dt.Rows)
|
||
{
|
||
pbr.Value++;
|
||
if (first)
|
||
{
|
||
first = false;
|
||
continue;
|
||
}
|
||
string name = v(dr, 0, 255);
|
||
|
||
|
||
//Unique Vendors only
|
||
if (!lVendors.Contains(name))
|
||
{
|
||
lVendors.Add(name);
|
||
|
||
//Import Vendor
|
||
Vendor c = Vendor.NewItem();
|
||
c.Name = name;
|
||
c.VendorType = VendorTypes.Manufacturer;
|
||
c.Save();
|
||
ImportedManufacturers.Add(name, c.ID);
|
||
}
|
||
|
||
}
|
||
pbr.Value = 0;
|
||
// MessageBox.Show("Done Vendor");
|
||
return true;
|
||
}
|
||
#endregion
|
||
|
||
#region Unit
|
||
Dictionary<string, Guid> UnitList = new Dictionary<string, Guid>();
|
||
Dictionary<Guid, Guid> UnitClientList = new Dictionary<Guid, Guid>();
|
||
private bool ImportUnit()
|
||
{
|
||
//New
|
||
//0="Unit Serial #"
|
||
//1="Client"
|
||
//2="Description"
|
||
//3="Type"
|
||
//4="Manufacturer/Vendor"
|
||
//5="Model"
|
||
//6="Unit Has Own Address"
|
||
//7="Unit Address"
|
||
//8="Unit Notes"
|
||
//9="Unit Text1 (Contract: Option)"
|
||
//10="Unit Text2 (Expiration)"
|
||
//11="Unit Text3 (Principle Inv)"
|
||
//12="Unit Text4 (Value)"
|
||
|
||
|
||
DataTable dt = new DataTable();
|
||
ReadCSV(dataGridView1, ref dt, @"C:\temp\morrow\UNIT-ROLLUP-2011-0405.csv");
|
||
Dictionary<string, Guid> UnitModelList = new Dictionary<string, Guid>();
|
||
Dictionary<string, Guid> UnitModelCategoryList = new Dictionary<string, Guid>();
|
||
UnitModelCategories umcList = UnitModelCategories.GetItems();
|
||
bool first = true;
|
||
pbr.Value = 0;
|
||
pbr.Maximum = dt.Rows.Count;
|
||
pbr.ForeColor = Color.Goldenrod;
|
||
int nUnique = 0;
|
||
foreach (DataRow dr in dt.Rows)
|
||
{
|
||
pbr.Value++;
|
||
if (first)
|
||
{
|
||
first = false;
|
||
continue;
|
||
}
|
||
string serial = v(dr, 0, 255);
|
||
//source docs have empty serials as well as duplicate serials
|
||
if (string.IsNullOrWhiteSpace(serial))
|
||
{
|
||
serial = "BLANK IN CSV(" + nUnique++.ToString() + ")";
|
||
|
||
}
|
||
if (UnitList.ContainsKey(serial))
|
||
{
|
||
serial = serial + " DUPLICATE IN CSV(" + nUnique++.ToString() + ")";
|
||
if (serial.Length > 255) throw new System.ApplicationException("TOO LONG");
|
||
}
|
||
string client = v(dr, 1, 255);
|
||
if (string.IsNullOrWhiteSpace(client))
|
||
{
|
||
client = "CLIENT BLANK IN UNITS CSV(" + nUnique++.ToString() + ")";
|
||
}
|
||
|
||
if (!ClientList.ContainsKey(client))
|
||
{
|
||
client = client + " (from unitcsv)";
|
||
if (!ClientList.ContainsKey(client))
|
||
{
|
||
Client c = Client.NewItem();
|
||
c.Name = client;
|
||
c.Notes = "This client created during Unit import because it didn't exist in Client csv file";
|
||
ClientList.Add(client, c.ID);
|
||
c.Save();
|
||
}
|
||
}
|
||
Guid ClientID = ClientList[client];
|
||
string description = v(dr, 2, 255);
|
||
string unitmodelcategory = v(dr, 3, 255);
|
||
Guid UnitModelCategoryID = Guid.Empty;
|
||
|
||
string ManufacturerName = v(dr, 4, 255);
|
||
Guid VendorID = Guid.Empty;
|
||
|
||
if (string.IsNullOrWhiteSpace(ManufacturerName))
|
||
{
|
||
ManufacturerName = "VENDOR BLANK IN UNITS CSV(" + nUnique++.ToString() + ")";
|
||
}
|
||
|
||
if (!ImportedManufacturers.ContainsKey(ManufacturerName))
|
||
{
|
||
ManufacturerName = ManufacturerName + " (from unitcsv)";
|
||
if (!ImportedManufacturers.ContainsKey(ManufacturerName))
|
||
{
|
||
Vendor c = Vendor.NewItem();
|
||
c.Name = ManufacturerName;
|
||
c.Notes = "This vendor created during Unit import because it didn't exist in Vendor csv file";
|
||
ImportedManufacturers.Add(ManufacturerName, c.ID);
|
||
c.Save();
|
||
}
|
||
}
|
||
|
||
VendorID = ImportedManufacturers[ManufacturerName];
|
||
|
||
string modelnumber = v(dr, 5, 255);
|
||
Guid UnitModelID = Guid.Empty;
|
||
|
||
string unitaddress = v(dr, 7, 255);
|
||
string unitnotes = v(dr, 8, 32767);
|
||
string text1 = v(dr, 9, 255);
|
||
string text2 = v(dr, 10, 255);
|
||
string text3 = v(dr, 11, 255);
|
||
string text4 = v(dr, 12, 255);
|
||
|
||
|
||
//Unit model category
|
||
if (!string.IsNullOrWhiteSpace(unitmodelcategory))
|
||
{
|
||
if (!UnitModelCategoryList.ContainsKey(unitmodelcategory))
|
||
{
|
||
UnitModelCategory umc = umcList.Add();
|
||
umc.Active = true;
|
||
umc.Name = unitmodelcategory;
|
||
UnitModelCategoryList.Add(unitmodelcategory, umc.ID);
|
||
umcList.Save();
|
||
}
|
||
|
||
UnitModelCategoryID = UnitModelCategoryList[unitmodelcategory];
|
||
}
|
||
|
||
//Unit model
|
||
if (!string.IsNullOrWhiteSpace(modelnumber))
|
||
{
|
||
if (!UnitModelList.ContainsKey(modelnumber))
|
||
{
|
||
UnitModel um = UnitModel.NewItem();
|
||
um.Active = true;
|
||
um.ModelNumber = modelnumber;
|
||
um.VendorID = VendorID;
|
||
um.UnitModelCategoryID = UnitModelCategoryID;
|
||
UnitModelList.Add(modelnumber, um.ID);
|
||
um.Save();
|
||
}
|
||
|
||
UnitModelID = UnitModelList[modelnumber];
|
||
}
|
||
|
||
//Unique Units only
|
||
if (!UnitList.ContainsKey(serial))
|
||
{
|
||
//Import Unit
|
||
Unit u = Unit.NewItem();
|
||
u.Active = true;
|
||
u.Serial = serial;
|
||
u.ClientID = ClientID;
|
||
u.Description = description;
|
||
u.UnitModelID = UnitModelID;
|
||
u.GoToAddress.DeliveryAddress = unitaddress;
|
||
u.UnitHasOwnAddress = true;
|
||
u.Notes = unitnotes;
|
||
u.Text1 = text1;
|
||
u.Text2 = text2;
|
||
u.Text3 = text3;
|
||
u.Text4 = text4;
|
||
UnitList.Add(serial, u.ID);
|
||
UnitClientList.Add(u.ID, ClientID);
|
||
u.Save();
|
||
}
|
||
|
||
}
|
||
pbr.Value = 0;
|
||
return true;
|
||
}
|
||
#endregion
|
||
|
||
#region PMTemplate
|
||
private bool ImportPMTemplate()
|
||
{
|
||
//0-TemplateDescription
|
||
//1-Service notes
|
||
DataTable dt = new DataTable();
|
||
ReadCSV(dataGridView1, ref dt, @"C:\temp\morrow\PM-TEMPLATE-2.csv");
|
||
|
||
bool first = true;
|
||
pbr.Value = 0;
|
||
pbr.Maximum = dt.Rows.Count;
|
||
pbr.ForeColor = Color.Green;
|
||
foreach (DataRow dr in dt.Rows)
|
||
{
|
||
pbr.Value++;
|
||
if (first)
|
||
{
|
||
first = false;
|
||
continue;
|
||
}
|
||
string description = v(dr, 0, 255);
|
||
string notes = v(dr, 1, 32767);
|
||
|
||
Workorder w = Workorder.NewItem(WorkorderTypes.TemplatePreventiveMaintenance);
|
||
w.TemplateDescription = description;
|
||
w.WorkorderItems[0].TechNotes = notes;
|
||
w.Save();
|
||
|
||
}
|
||
pbr.Value = 0;
|
||
// MessageBox.Show("Done Workorder");
|
||
return true;
|
||
}
|
||
#endregion
|
||
|
||
#region Workorder
|
||
private bool ImportWorkorder(string csv)
|
||
{
|
||
//0-serial#
|
||
//1-Date m/d/y
|
||
//2-ServiceNotes
|
||
//3-workorder summary
|
||
//4-Travel time
|
||
//5-Labor time
|
||
//6-Part
|
||
//7-User
|
||
//8-ClientContact
|
||
|
||
//NEW FIELDS
|
||
//9-Closed "YES" or "NO"
|
||
//10-Category - ad hoc text, need to track and generate new ones as required and set wo to them
|
||
|
||
DataTable dt = new DataTable();
|
||
ReadCSV(dataGridView1, ref dt, @"C:\temp\morrow\" + csv);
|
||
System.Text.StringBuilder sbErrors = new StringBuilder();
|
||
bool first = true;
|
||
pbr.Value = 0;
|
||
pbr.Maximum = dt.Rows.Count;
|
||
if (csv.Contains("CLOSED"))
|
||
pbr.ForeColor = Color.Blue;
|
||
else
|
||
pbr.ForeColor = Color.DarkViolet;
|
||
|
||
WorkorderCategories Categories = WorkorderCategories.GetItems();
|
||
|
||
|
||
foreach (DataRow dr in dt.Rows)
|
||
{
|
||
pbr.Value++;
|
||
if (first)
|
||
{
|
||
first = false;
|
||
continue;
|
||
}
|
||
|
||
string wodate = v(dr, 1, 255);
|
||
DateTime dtWoDate;
|
||
if (!DateTime.TryParse(wodate, out dtWoDate))
|
||
dtWoDate = DateTime.Now;
|
||
string servicenotes = v(dr, 2, 32767);
|
||
string wosummary = v(dr, 3, 255);
|
||
string traveltime = v(dr, 4, 255);
|
||
string labortime = v(dr, 5, 255);
|
||
string part = v(dr, 6, 1000);
|
||
string username = v(dr, 7, 255);
|
||
string clientcontact = v(dr, 8, 1000);
|
||
|
||
bool Closed = v(dr, 9, 10).Equals("YES", StringComparison.InvariantCultureIgnoreCase);
|
||
//Add category or get id of existing if necessary
|
||
string Category = v(dr, 10, 255);
|
||
Guid CategoryID = Guid.Empty;
|
||
if (!string.IsNullOrWhiteSpace(Category))
|
||
CategoryID = FetchSertCategory(Categories, Category);
|
||
|
||
string unitserial = v(dr, 0, 255);
|
||
if (string.IsNullOrWhiteSpace(unitserial))
|
||
{
|
||
sbErrors.AppendLine("Unit serial is blank can't import this workorder. (wosummary: " + v(dr, 3, 255) + " wodate: " + wodate + ")");
|
||
continue;
|
||
}
|
||
//get unit and then get client
|
||
if (!UnitList.ContainsKey(unitserial))
|
||
{
|
||
sbErrors.AppendLine("Unit serial not imported in units: " + unitserial + " can't import this workorder. (wosummary: " + v(dr, 3, 255) + " wodate: " + wodate + ")");
|
||
continue;
|
||
}
|
||
Guid unitID = UnitList[unitserial];
|
||
Guid clientID = UnitClientList[unitID];
|
||
|
||
Workorder w = Workorder.NewItem(WorkorderTypes.Service);
|
||
w.ClientID = clientID;
|
||
WorkorderItem wi = w.WorkorderItems[0];
|
||
wi.UnitID = unitID;
|
||
w.WorkorderService.ServiceDate = dtWoDate;
|
||
System.Text.StringBuilder sb = new StringBuilder();
|
||
|
||
if (!string.IsNullOrWhiteSpace(traveltime))
|
||
sb.AppendLine("Travel: " + traveltime);
|
||
if (!string.IsNullOrWhiteSpace(labortime))
|
||
sb.AppendLine("Labor: " + labortime);
|
||
if (!string.IsNullOrWhiteSpace(part))
|
||
sb.AppendLine("Part: " + part);
|
||
if (!string.IsNullOrWhiteSpace(username))
|
||
sb.AppendLine("User: " + username);
|
||
|
||
if (sb.Length > 0)
|
||
wi.TechNotes = servicenotes + "\r\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-\r\n" + sb.ToString();
|
||
else
|
||
wi.TechNotes = servicenotes;
|
||
|
||
w.CustomerContactName = clientcontact;
|
||
w.Summary = wosummary;
|
||
|
||
w.WorkorderCategoryID = CategoryID;
|
||
|
||
if (Closed)
|
||
{
|
||
w.ServiceCompleted = true;
|
||
w.Closed = true;
|
||
}
|
||
if (!w.IsSavable)
|
||
{
|
||
System.Diagnostics.Debugger.Break();
|
||
}
|
||
w.Save();
|
||
|
||
}
|
||
pbr.Value = 0;
|
||
|
||
return true;
|
||
}
|
||
|
||
|
||
|
||
|
||
#endregion
|
||
|
||
#region PM workorders
|
||
private bool ImportPMWorkorders()
|
||
{
|
||
//0="PM Unit (Make: Model – Serial #)"
|
||
//1="Client"
|
||
//2="Next Service Date"
|
||
//3="Stop Generating Date"
|
||
//4="Active"
|
||
//5="WorkOrder Category"
|
||
//6="Time Span (Months)"
|
||
//7="Service Notes"
|
||
//8="WorkOrder Number"
|
||
//9="Item Summary"
|
||
//10="Preventative Maintenance Contact"
|
||
|
||
DataTable dt = new DataTable();
|
||
ReadCSV(dataGridView1, ref dt, @"C:\temp\morrow\PM-2011-0405.csv");
|
||
WorkorderCategories Categories = WorkorderCategories.GetItems();
|
||
|
||
bool first = true;
|
||
pbr.Value = 0;
|
||
pbr.Maximum = dt.Rows.Count;
|
||
pbr.ForeColor = Color.Maroon;
|
||
foreach (DataRow dr in dt.Rows)
|
||
{
|
||
pbr.Value++;
|
||
if (first)
|
||
{
|
||
first = false;
|
||
continue;
|
||
}
|
||
|
||
string unitserial = v(dr, 0, 255);
|
||
|
||
|
||
if (string.IsNullOrWhiteSpace(unitserial))
|
||
{
|
||
sbErrors.AppendLine("Unit serial is blank can't import this PM. (Summary: " + v(dr, 9, 255) + " WO# " + v(dr, 8, 255) + ")");
|
||
continue;
|
||
}
|
||
|
||
|
||
int StartPositionOfSerialNumber = unitserial.LastIndexOf(" - ");
|
||
if (StartPositionOfSerialNumber != -1)
|
||
{
|
||
//extract the serial number portion
|
||
unitserial = unitserial.Substring(StartPositionOfSerialNumber+3).Trim();
|
||
|
||
}
|
||
|
||
//get unit and then get client
|
||
Guid unitID;
|
||
if (!UnitList.TryGetValue(unitserial, out unitID))
|
||
{
|
||
sbErrors.AppendLine("PM IMPORT ERROR: Unit serial CSV value: [" + v(dr, 0, 255) + "] parsed to this: ["+ unitserial+"] not found in imported units, skipping this PM. (Summary: " + v(dr, 9, 255) + " WO# " + v(dr, 8, 255) + ")");
|
||
continue;
|
||
}
|
||
|
||
Guid clientID = UnitClientList[unitID];
|
||
|
||
string sdate = v(dr, 2, 255);
|
||
DateTime dtNextServiceDate = DateTime.MinValue;
|
||
if (!DateTime.TryParse(sdate, out dtNextServiceDate))
|
||
{
|
||
sbErrors.AppendLine("Next service date not parseable: \"" + sdate + "\" can't import this PM. (Summary: " + v(dr, 9, 255) + " WO# " + v(dr, 8, 255) + ")");
|
||
continue;
|
||
}
|
||
|
||
sdate = v(dr, 3, 255);
|
||
DateTime dtStopGeneratingDate = DateTime.MinValue;
|
||
if (!DateTime.TryParse(sdate, out dtStopGeneratingDate))
|
||
{
|
||
sbErrors.AppendLine("Stop generating date not parseable: \"" + sdate + "\" can't import this PM. (Summary: " + v(dr, 9, 255) + " WO# " + v(dr, 8, 255) + ")");
|
||
continue;
|
||
}
|
||
|
||
bool Active = v(dr, 4, 255).Equals("ACTIVE", StringComparison.InvariantCultureIgnoreCase);
|
||
|
||
//Add category or get id of existing if necessary
|
||
string Category = v(dr, 5, 255);
|
||
Guid CategoryID = Guid.Empty;
|
||
if (!string.IsNullOrWhiteSpace(Category))
|
||
CategoryID = FetchSertCategory(Categories, Category);
|
||
|
||
int TimeSpanMonths = 999;
|
||
if (!int.TryParse(v(dr, 6, 255), out TimeSpanMonths))
|
||
TimeSpanMonths = 999;//failure to parse code
|
||
if (TimeSpanMonths < 1)
|
||
TimeSpanMonths = 999;//less than zero is in-valid
|
||
|
||
string servicenotes = v(dr, 7, 32767);
|
||
string WorkorderNumberWTF = v(dr, 8, 255);
|
||
string ItemSummary = v(dr, 9, 255);
|
||
string Contact = v(dr, 10, 255);
|
||
|
||
Workorder w = Workorder.NewItem(WorkorderTypes.PreventiveMaintenance);
|
||
w.ClientID = clientID;
|
||
w.CustomerContactName = Contact;
|
||
w.InternalReferenceNumber = WorkorderNumberWTF;
|
||
w.WorkorderCategoryID = CategoryID;
|
||
WorkorderItem wi = w.WorkorderItems[0];
|
||
wi.UnitID = unitID;
|
||
wi.TechNotes = servicenotes;
|
||
wi.Summary = ItemSummary;
|
||
|
||
w.WorkorderPreventiveMaintenance.Active = Active;
|
||
w.WorkorderPreventiveMaintenance.NextServiceDate = dtNextServiceDate;
|
||
w.WorkorderPreventiveMaintenance.StopGeneratingDate = dtStopGeneratingDate;
|
||
w.WorkorderPreventiveMaintenance.GenerateSpanUnit = AyaUnitsOfTime.Months;
|
||
w.WorkorderPreventiveMaintenance.GenerateSpan = TimeSpanMonths;
|
||
//Note defaulted this as not specified
|
||
w.WorkorderPreventiveMaintenance.ThresholdSpanUnit = AyaUnitsOfTime.Days;
|
||
w.WorkorderPreventiveMaintenance.ThresholdSpan = 7;
|
||
w.Save();
|
||
|
||
}
|
||
pbr.Value = 0;
|
||
// MessageBox.Show("Done Workorder");
|
||
return true;
|
||
}
|
||
#endregion
|
||
|
||
|
||
|
||
private Guid FetchSertCategory(WorkorderCategories cats, string catname)
|
||
{
|
||
|
||
foreach (WorkorderCategory cat in cats)
|
||
{
|
||
if (cat.Name == catname)
|
||
return cat.ID;
|
||
}
|
||
//Not there, need to add it
|
||
WorkorderCategory newcat = cats.Add();
|
||
newcat.Name = catname;
|
||
newcat.Active = true;
|
||
cats = (WorkorderCategories)cats.Save();
|
||
return newcat.ID;
|
||
|
||
|
||
}
|
||
|
||
|
||
private string v(DataRow dr, int column, int maxlength)
|
||
{
|
||
string s = dr[column].ToString().Trim();
|
||
if (s.Length > maxlength)
|
||
s = s.Substring(0, maxlength);
|
||
return s;
|
||
}
|
||
|
||
#region ReadCSV
|
||
public static void ReadCSV(DataGridView grid, ref System.Data.DataTable dt, string csvPath)
|
||
{
|
||
if (dt != null)
|
||
{
|
||
dt.Clear();
|
||
dt = null;
|
||
}
|
||
|
||
|
||
bool bHasErrors = false;
|
||
System.Text.StringBuilder sbErrors = new System.Text.StringBuilder();
|
||
long lRecordsRead = 0;
|
||
long lRecordsBad = 0;
|
||
|
||
Microsoft.VisualBasic.FileIO.TextFieldParser rdr = new Microsoft.VisualBasic.FileIO.TextFieldParser(csvPath);
|
||
rdr.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
|
||
rdr.SetDelimiters(",");
|
||
rdr.TrimWhiteSpace = true;
|
||
rdr.HasFieldsEnclosedInQuotes = true;
|
||
|
||
|
||
|
||
List<string[]> lsRows = new List<string[]>();
|
||
int nMaxRowFields = 0;
|
||
|
||
string[] sCurrentRow;
|
||
while (!rdr.EndOfData)
|
||
{
|
||
try
|
||
{
|
||
sCurrentRow = rdr.ReadFields();
|
||
if (sCurrentRow.Length > nMaxRowFields)
|
||
nMaxRowFields = sCurrentRow.Length;
|
||
lsRows.Add(sCurrentRow);
|
||
lRecordsRead++;
|
||
}
|
||
catch (Microsoft.VisualBasic.FileIO.MalformedLineException ex)
|
||
{
|
||
bHasErrors = true;
|
||
sbErrors.Append("Line ");
|
||
sbErrors.Append(ex.Message);
|
||
sbErrors.Append(" is not valid and will be skipped.\r\n");
|
||
lRecordsBad++;
|
||
}
|
||
}
|
||
|
||
|
||
if (bHasErrors)
|
||
{
|
||
MessageBox.Show(lRecordsBad.ToString() + " records were malformed and not able to be read:\r\n\r\n" + sbErrors.ToString());
|
||
}
|
||
|
||
|
||
//MessageBox.Show(lRecordsRead.ToString() +
|
||
//" records were successfully read from the .csv file.");
|
||
|
||
dt = new System.Data.DataTable("CSVData");
|
||
for (int x = 0; x < nMaxRowFields; x++)
|
||
{
|
||
dt.Columns.Add("Column" + (x + 1).ToString(), typeof(string));
|
||
}
|
||
|
||
foreach (string[] sRow in lsRows)
|
||
{
|
||
System.Data.DataRow dr = dt.NewRow();
|
||
int nField = 0;
|
||
foreach (string sField in sRow)
|
||
{
|
||
dr[nField] = sField;
|
||
nField++;
|
||
}
|
||
dt.Rows.Add(dr);
|
||
}
|
||
|
||
|
||
grid.DataSource = dt;
|
||
|
||
|
||
|
||
}
|
||
|
||
#endregion read csv
|
||
|
||
}//eoc
|
||
}
|