|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536 |
- using System.Security.Principal;
- using System;
- using System.Text;
- using System.Linq;
- using System.Collections.Generic;
- using importer.RestObjects;
- using importer.ImportObjects;
- using System.IO;
-
- namespace importer
- {
- public class JDISImport
- {
- private static string _serviceUrl;
- private static RESTService _rservice;
- private static List<Customer> _customers;
- private static List<Make> _makes;
- private static List<Product> _products;
- private static List<Model> _models;
- private static List<InspectionType> _inspectionTypes;
- private static List<Template> _templates;
- private static List<User> _users;
- private static Status _status;
- private static Priority _priority;
- private static Reason _reason;
- public static string ServiceUrl { get { return _serviceUrl; } set { _serviceUrl = value; } }
- public static Dictionary<string, int> InspectionType;
- private static List<string> WriteFileToText(string file)
- {
- List<string> list = new List<string>();
- string txt = string.Empty;
- using (StreamReader sr = new StreamReader(file))
- {
- txt = sr.ReadToEnd().Trim();
- txt = txt.Replace("\r\r\r", "");
- txt = txt.Replace("\r\r", "");
- txt = txt.Replace("\r", "");
- sr.Close();
- }
- string[] array = txt.Trim().Split('\n');
- list.AddRange(array);
- return list;
- }
- private static string[] RemoveWhiteSpace(ref List<string> filelines)
- {
- for (int i = filelines.Count - 1; i >= 0; i--)
- {
- if (string.IsNullOrEmpty(filelines[i])) filelines.RemoveAt(i);
- }
- return filelines.ToArray();
-
- }
- private static void Line1(string currentline, ref ToDo todo)
- {
- string strDate = currentline.Substring(5, currentline.IndexOf("*") - 6);
- strDate = strDate.Trim();
- strDate.Replace(" ", " ");
- todo.WorkOrder.CreateDate = Global.DateFormatter(strDate);
- }
- private static void Line2(string currentline, ref ToDo todo)
- {
- string strWONum = currentline.Substring(currentline.IndexOf("Work Order") + 10, 12);
- strWONum = strWONum.Trim();
- todo.WorkOrder.Number = strWONum;
- todo.WorkOrderNumber = strWONum;
- string strSeg = currentline.Substring(currentline.IndexOf("Seg.") + 4, 25);
- strSeg = strSeg.Trim();
- todo.WorkOrder.Segment = strSeg;
- string strODate = currentline.Substring(currentline.IndexOf("Opn Dt") + 6);
- strODate = strODate.Trim();
- todo.WorkOrder.OpnDt = Global.DateFormatter(strODate, false);
- }
- private static void Line5678(string currentline, ref ToDo todo, int lineNumber)
- {
- if (lineNumber == 5)
- {
- todo.Customer.Name = currentline.Substring(3, 25);
- todo.Customer.Number = currentline.Substring(38, 10);
- }
- if (lineNumber == 6)
- {
- todo.Customer.Addr1 = currentline.Substring(3, 25);
- }
- if (lineNumber == 7)
- {
- todo.Customer.Addr2 = currentline.Substring(3, 25);
- }
- if (lineNumber == 8)
- {
- int comma = currentline.IndexOf(",");
- int length = currentline.Length - 3;
-
- todo.Customer.City = (comma == -1) ? "" : currentline.Substring(3, comma - 3);
- todo.Customer.State = currentline.Substring(currentline.IndexOf(",") + 2, 2);
- todo.Customer.Zip = currentline.Substring(comma + 5, 7);
- }
-
- }
- private static void Line11(string currentline, ref ToDo todo)
- {
- todo.Equipment.Make = currentline.Substring(0, 8).Trim();
- todo.Equipment.Model = currentline.Substring(8, 14).Trim();
- todo.Equipment.SerialNumber = currentline.Substring(22, 22).Trim();
- todo.Equipment.EquipmentNumber = currentline.Substring(42, 16).Trim();
- todo.Equipment.MeterReading = currentline.Substring(60, 7).Trim();
- todo.Equipment.MID = EquipmentImport.MachineExists(todo.Equipment.SerialNumber);
- if (todo.Equipment.MID == 0) todo.Equipment.IsNew = true;
- else todo.Equipment.IsNew = false;
- }
- private static void CreateAPart(string currentline, ref ToDo todo)
- {
- PartImport pt = new PartImport();
- string sline = currentline.Substring(19).Trim();
- //pt.Description = currentline.Substring(34, 14);
- pt.Number = sline.Substring(0, sline.IndexOf(" "));
- sline = sline.Substring(sline.IndexOf(" ")).Trim();
- pt.Description = sline.Substring(0, sline.IndexOf(" ")).Trim();
- pt.Qty = currentline.Substring(0, currentline.IndexOf(" ")).Trim();
- todo.Parts.Add(pt);
- }
- private static void PMInterval(string currentline, ref ToDo todo, ref bool isSpecial)
- {
- isSpecial = false;
- string pminterval = currentline.Substring(currentline.IndexOf(":") + 2);
- pminterval = pminterval.Substring(0, pminterval.IndexOf(" ")).Trim();
- if (XMLData.IntervalLookup.ContainsKey(pminterval)) todo.WorkOrder.InspectionLevel = XMLData.IntervalLookup[pminterval].ToString();
- if (!string.IsNullOrEmpty(todo.WorkOrder.InspectionLevel)) todo.WorkOrder.InspectionInterval = pminterval;
- string strService = currentline.Substring(currentline.IndexOf(":") + 2);
- strService = strService.Substring(strService.IndexOf(" ") + 1).Trim();
- strService = strService.Substring(0, strService.IndexOf(" ") + 1).Trim();
- if (strService.IndexOf(" ") > 0)
- strService = strService.Substring(0, strService.IndexOf(" ")).Trim();
- if (InspectionType.ContainsKey(strService))
- todo.WorkOrder.InspectionType = InspectionType[strService].ToString();
- else todo.WorkOrder.InspectionType = InspectionType["PM"].ToString();
- string codeanddescriptionoriginal = currentline.Substring(currentline.IndexOf(":") + 2);
- codeanddescriptionoriginal = codeanddescriptionoriginal.Substring(0, codeanddescriptionoriginal.IndexOf(" T") - 3).Trim();
- todo.WorkOrder.CodeAndDescriptionOriginal = codeanddescriptionoriginal;
- }
- private static void EmpID(string currentline, ref ToDo todo, ref bool isSpecial)
- {
- isSpecial = false;
- string empid = currentline.Substring(currentline.IndexOf(":") + 2);
- empid = empid.Substring(0, empid.IndexOf(" ")).Trim();
- todo.WorkOrder.TechnicianUserID = empid;
- }
- private static void PromiseDate(string currentline, ref ToDo todo, ref bool isSpecial)
- {
- isSpecial = false;
- string promisedate = currentline.Substring(currentline.IndexOf(":") + 2);
- promisedate = promisedate.Substring(0, promisedate.IndexOf(" ")).Trim();
- todo.WorkOrder.ScheduledStartDate = Global.DateFormatter(promisedate, false);
- }
- private static void SpecialInstructions(string currentline, ref ToDo todo, ref bool isSpecial, ref StringBuilder sb)
- {
- isSpecial = true;
- string special = currentline.Substring(currentline.IndexOf(":") + 2);
- special = special.Substring(0, special.IndexOf(" T") - 1).Trim();
- sb.Append(special);
- }
- private static void Line14AndUp(string currentline, ref ToDo todo, ref bool isSpecial, ref StringBuilder sb)
- {
- if (currentline.EndsWith(" T"))
- {
- if (currentline.Contains("PM LEVEL")) isSpecial = false;
- else if (currentline.Contains("PM INTERVAL")) PMInterval(currentline, ref todo, ref isSpecial);
- else if (currentline.Contains("EMPID")) EmpID(currentline, ref todo, ref isSpecial);
- else if (currentline.Contains("PROMISE DATE")) PromiseDate(currentline, ref todo, ref isSpecial);
- else if (currentline.Contains("SPECIAL INSTRUCTIONS")) SpecialInstructions(currentline, ref todo, ref isSpecial, ref sb);
- else
- {
- if (isSpecial)
- {
- string addspecial = currentline.Substring(0, currentline.IndexOf(" T") - 1).Trim();
- sb.Append(string.Format(" {0}", addspecial));
- }
- }
- }
- else //Decide if it is a part
- {
- isSpecial = false;
- int qty = 0;
-
- if (currentline.Length > 0)
- {
- string input = currentline.Substring(0, (currentline.IndexOf(" ") == -1) ? 1 : currentline.IndexOf(" ")).Trim();
- if (int.TryParse(input, out qty))
- {
- if (qty < 100 && !currentline.Contains("HR"))
- {
- CreateAPart(currentline, ref todo);
- }
- }
- }
- }
- }
- private static Customer IsNewCustomer(string name)
- {
- List<Customer> list = _customers.FindAll(x => x.Name.Trim() == name.Trim());
-
- if (list.Count > 1)
- return list[list.Count - 1];
- else if (list.Count == 1)
- return list[0];
- else return null;
-
-
- }
- private static Customer CreateNewCustomer(ToDo todo)
- {
- Customer cust = new Customer();
- cust.Name = todo.Customer.Name;
- cust.Address = todo.Customer.Addr1;
- cust.Address2 = todo.Customer.Addr2;
- cust.City = todo.Customer.City;
- cust.State = todo.Customer.State;
- cust.CreateDate = DateTime.Now;
- cust.CreateUserID = -1;
- cust.UpdateDate = DateTime.Now;
- cust.UpdateUserID = -1;
- cust.IsActive = true;
- cust.Number = todo.Customer.Number;
- cust.ZipCode = todo.Customer.Zip;
- string json = Serialize.ToJson<Customer>(cust);
- cust = _rservice.PostRest<Customer>(json, "customers.svc");
- return cust;
- }
- private static Model CreateNewModel(Product defaultP, ToDo todo)
- {
- Model mdl = new Model();
- //Find the Make and assign the default Product
- mdl.Name = todo.Equipment.Model.Trim();
- mdl.IsActive = true;
- mdl.CreateDate = DateTime.Now;
- mdl.CreateUserID = -1;
- mdl.UpdateDate = DateTime.Now; ;
- mdl.UpdateUserID = -1;
- mdl.Product = defaultP;
- Make mk = (from x in _makes where x.Name.Trim() == todo.Equipment.Make.Trim() select x).FirstOrDefault();
- if (mk == null)
- {
- //Create new Make
- mk.Name = todo.Equipment.Make.Trim();
- mk.CreateDate = DateTime.Now;
- mk.CreateUserID = -1;
- mk.UpdateDate = DateTime.Now;
- mk.UpdateUserID = -1;
- mk.IsActive = true;
- mk.Description = todo.Equipment.Make.Trim();
- string mkJson = Serialize.ToJson<Make>(mk);
- mk = _rservice.PostRest<Make>(mkJson, "makes.svc");
-
- }
- mdl.Make = mk;
- string json = Serialize.ToJson<Model>(mdl);
- mdl = _rservice.PostRest<Model>(json, "models.svc");
- return mdl;
- }
- private static Equipment CreateNewMachine(Model mdl, Customer cust, ToDo todo)
- {
- Equipment newMachine = new Equipment();
- newMachine.Model = mdl;
- newMachine.Make = mdl.Make;
- newMachine.Product = mdl.Product;
- newMachine.BarcodeID = null;
- newMachine.CreateDate = DateTime.Now;
- newMachine.UpdateDate = DateTime.Now;
- newMachine.UpateUserID = -1;
- newMachine.CreateUserID = -1;
- newMachine.Description = todo.Equipment.SerialNumber;
- newMachine.IsActive = true;
- newMachine.LastServiceDate = null;
- newMachine.Location = null;
- newMachine.ManufacturerYear = "2000";
- newMachine.MeterReading = todo.Equipment.MeterReading;
- newMachine.Number = todo.Equipment.EquipmentNumber;
- newMachine.Odometer = null;
- newMachine.SerialNumber = todo.Equipment.SerialNumber;
- newMachine.Customer = cust;
- string eqJson = Serialize.ToJson<Equipment>(newMachine);
- newMachine = _rservice.PostRest<Equipment>(eqJson, "Equipments.svc");
- return newMachine;
- }
- private static List<Equipment> IsNewMachine(string serial)
- {
- Dictionary<string, string> ps = new Dictionary<string, string>();
- ps.Add("search", serial);
-
- List<Equipment> machines = _rservice.RestGet<Equipment>("equipments.svc", ps);
-
- return machines;
- }
- private static void APIPush(ref ToDo todo)
- {
- Equipment machine = new Equipment();
- string strModel = todo.Equipment.Model.Trim();
- Model mdl = _models.Find(x => x.Name.Trim() == strModel);
- Product defaultP = _products.Find(x => x.ID == 14);
- //Customer cust = new Customer();
- Customer cust = IsNewCustomer(todo.Customer.Name);
- if (cust == null)
- cust = CreateNewCustomer(todo);
- List<Equipment> equipment = IsNewMachine(todo.Equipment.SerialNumber);
- todo.Equipment.IsNew = equipment.Count == 0;
- if (todo.Equipment.IsNew)
- {
- bool isNewModel = mdl == null;
- if (isNewModel)
- {
- string sModel = todo.Equipment.Model.Trim().Substring(0, todo.Equipment.Model.Length - 1);
- mdl = (from x in _models where x.Name.Contains(sModel) orderby x.ID select x).LastOrDefault();
- if (mdl == null)
- mdl = CreateNewModel(defaultP, todo);
- else
- machine = CreateNewMachine(mdl, cust, todo);
- }
- else
- {
- machine = CreateNewMachine(mdl, cust, todo);
- }
- }
- else
- {
- string eqSerial = todo.Equipment.SerialNumber.Trim();
- List<Equipment> eqlist = equipment.FindAll(x => x.SerialNumber == eqSerial);
- machine = eqlist[eqlist.Count - 1];
- machine.Customer = cust;
-
- }
- //Find the Template
- string il = todo.WorkOrder.InspectionLevel.Trim();
- string it = todo.WorkOrder.InspectionType.Trim();
- string eqmdl = todo.Equipment.Model.Trim();
- Template template = _templates.Find(x => x.InspectionLevel.ID == Convert.ToInt32(il) && x.InspectionType.ID == Convert.ToInt32(it) && x.Name == eqmdl);
- if (template == null)
- {
- //Search by model prefix and get the last one
- string prefix = todo.Equipment.Model.Trim();
- prefix = prefix.Substring(0, prefix.Length - 1);
- template = (from x in _templates where x.InspectionLevel.ID == Convert.ToInt32(il) && x.InspectionType.ID == Convert.ToInt32(it) && x.Name.Contains(prefix) orderby x.ID select x).LastOrDefault();
- if (template == null || template.ID == 0)
- {
- todo.Success = false;
- todo.Validation = todo.Validation + " No template for this model.";
- }
-
- }
- string tuid = todo.WorkOrder.TechnicianUserID.Trim();
- User user = _users.Find(x => x.Number == tuid);
- WorkOrder workorder = new WorkOrder();
- workorder.Number = todo.WorkOrder.Number;// wo.Number;
-
- workorder.Technician = user;
- workorder.Reason = _reason;
- workorder.Priority = _priority;
- workorder.Status = _status;
- workorder.CreateDate = DateTime.Now;
- workorder.CreateUserID = -1;
- workorder.Equipment = machine;
- workorder.Customer = cust;
- string mtrReading = todo.Equipment.MeterReading;
- workorder.MeterReading = Convert.ToDecimal(mtrReading);
- workorder.ScheduledStartDate = todo.WorkOrder.OpnDt;// wo.OpnDt;
- workorder.ScheduledEndDate = todo.WorkOrder.OpnDt.AddDays(1d);
- workorder.UpdateDate = DateTime.Now;
- workorder.UpdateUserID = -1;
- string woJson = Serialize.ToJson<WorkOrder>(workorder);
- //Console.WriteLine("WorkOrder Object");
- //Console.WriteLine();
- //Console.WriteLine(woJson);
- //JDISLog.WriteToLog(_logFile, string.Format("Creating Work Order: {0}", workorder.Number));
- workorder = _rservice.PostRest<WorkOrder>(woJson, "workorders.svc");
- if (workorder.ID != 0) todo.Success = true;
- if (template == null || template.ID == 0)
- {
- todo.Validation = string.Format("Work Order {0} was created but there is no template matching model # {1}", workorder.Number, mdl.Name);
- todo.Success = false;
- }
- else
- {
- //JDISLog.WriteToLog(_logFile, String.Format("Creating Inspections for Workorder {0}", workorder.Number));
- Dictionary<string, string> tps = new Dictionary<string, string>();
- tps.Add("create", string.Format("{0}/{1}/{2}", template.ID, workorder.ID, -1));
- Inspection i = _rservice.RestGet<Inspection>(true, "inspections.svc", tps);
- tps["create"] = string.Format("{0}/{1}/{2}", 1691, workorder.ID, -1);
- Inspection sta = _rservice.RestGet<Inspection>(true, "inspections.svc", tps);
- if (i != null && i.ID != 0)
- {
- todo.Success = true;
- }
- }
-
- }
- private static void ReadFile(ref ToDo todo)
- {
- todo.PathToFile = Path.Combine(Global.StorageDir, todo.FileName);
- todo.Customer = new CustomerImport();
- todo.WorkOrder = new WorkOrderImport();
- todo.Equipment = new EquipmentImport();
- todo.Parts = new List<PartImport>();
- string filemove = string.Empty;
- string filetxt = string.Empty;
- string inspectionLevel = string.Empty;
- bool isSpecial = false;
- StringBuilder specialInstructions = new StringBuilder();
- int line = 1;
- try
- {
- List<string> filelines = WriteFileToText(todo.PathToFile);
- string[] lines = RemoveWhiteSpace(ref filelines);
- foreach (string s in lines)
- {
- string currentline = s.Trim().Replace("'", "''");
- if (currentline.Contains("Page") && currentline.Substring(78).Trim() != "1")
- line = 18;
- if (currentline.Contains("Page") && currentline.Substring(78).Trim() == "1")
- line = 1;
- switch (line)
- {
- case 1:
- Line1(currentline, ref todo);
- break;
- case 2:
- Line2(currentline, ref todo);
- break;
- case 5:
- case 6:
- case 7:
- case 8:
- Line5678(currentline, ref todo, line);
- break;
- case 11:
- Line11(currentline, ref todo);
- break;
- default:
- break;
- }
- if (line > 14)
- {
- Line14AndUp(currentline, ref todo, ref isSpecial, ref specialInstructions);
- }
- line++;
- }
- if (todo.WorkOrder.ScheduledStartDate == DateTime.MinValue) todo.WorkOrder.ScheduledStartDate = DateTime.Now;
- // List<CSVObject> list = new List<CSVObject>();
- // List<CSVObject> exceptions = new List<CSVObject>();
- if (todo.Parts.Count == 0)
- {
- PartImport p = new PartImport();
- p.Description = string.Empty;
- p.Qty = string.Empty;
- p.Number = string.Empty;
- todo.Parts.Add(p);
-
- }
- APIPush(ref todo);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
-
- }
- public static void BeginImport(List<ToDo> todos)
- {
- for (int i = 0; i < todos.Count; i++)
- {
- ToDo todo = todos[i];
- XMLData.BuildIntervalLookup();
- GetInspectionTypes();
- ReadFile(ref todo);
- todos[i] = todo;
- }
-
- }
- public static void GetInspectionTypes()
- {
-
- List<InspectionType> list = _rservice.RestGet<InspectionType>("inspectiontypes.svc");
- InspectionType = new Dictionary<string, int>();
- foreach (InspectionType t in list)
- {
- InspectionType.Add(t.Name, t.ID);
- }
-
- }
- public static void BuildDefaults()
- {
- if (Global.Settings.Remote)
- _serviceUrl = Global.Settings.Prelub.RemoteService;
- else _serviceUrl = Global.Settings.Prelub.LocalService;
- _rservice = new RESTService();
- _customers = _rservice.RestGet<Customer>("customers.svc");
- _makes = _rservice.RestGet<Make>("makes.svc");
- _products = _rservice.RestGet<Product>("products.svc");
- _models = _rservice.RestGet<Model>("models.svc");
- _inspectionTypes = _rservice.RestGet<InspectionType>("inspectionTypes.svc");
- _templates = _rservice.RestGet<Template>("templates.svc");
- _users = _rservice.RestGet<User>("users.svc");
- _status = new Status();
- _status.ID = 1;
- _status.Name = "Open";
- _priority = new Priority();
- _priority.ID = 3;
- _priority.Name = "Medium";
- _reason = new Reason();
- _reason.ID = 9;
- _reason.Name = "Cust. Request";
- _reason.IsActive = true;
- _reason.CreateDate = new DateTime(2013, 10, 1, 9, 23, 7);
- _reason.CreateUserID = -1;
- _reason.UpdateDate = new DateTime(2013, 10, 1, 9, 23, 7);
- _reason.UpdateUserID = -1;
- }
- }
- public class JDISLog
- {
- public static void WriteToLog(string logFile, string msg)
- {
- using (StreamWriter sw = File.AppendText(logFile))
- {
- sw.WriteLine(msg);
- }
-
- }
- public static void Begin(string logFile)
- {
- using (StreamWriter sw = File.AppendText(logFile))
- {
- sw.WriteLine();
- sw.WriteLine("###########################################################################");
- sw.WriteLine(string.Format("Service Started @ {0}", DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()));
- }
- }
- }
- }
|