Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

536 lignes
24 KiB

  1. using System.Security.Principal;
  2. using System;
  3. using System.Text;
  4. using System.Linq;
  5. using System.Collections.Generic;
  6. using importer.RestObjects;
  7. using importer.ImportObjects;
  8. using System.IO;
  9. namespace importer
  10. {
  11. public class JDISImport
  12. {
  13. private static string _serviceUrl;
  14. private static RESTService _rservice;
  15. private static List<Customer> _customers;
  16. private static List<Make> _makes;
  17. private static List<Product> _products;
  18. private static List<Model> _models;
  19. private static List<InspectionType> _inspectionTypes;
  20. private static List<Template> _templates;
  21. private static List<User> _users;
  22. private static Status _status;
  23. private static Priority _priority;
  24. private static Reason _reason;
  25. public static string ServiceUrl { get { return _serviceUrl; } set { _serviceUrl = value; } }
  26. public static Dictionary<string, int> InspectionType;
  27. private static List<string> WriteFileToText(string file)
  28. {
  29. List<string> list = new List<string>();
  30. string txt = string.Empty;
  31. using (StreamReader sr = new StreamReader(file))
  32. {
  33. txt = sr.ReadToEnd().Trim();
  34. txt = txt.Replace("\r\r\r", "");
  35. txt = txt.Replace("\r\r", "");
  36. txt = txt.Replace("\r", "");
  37. sr.Close();
  38. }
  39. string[] array = txt.Trim().Split('\n');
  40. list.AddRange(array);
  41. return list;
  42. }
  43. private static string[] RemoveWhiteSpace(ref List<string> filelines)
  44. {
  45. for (int i = filelines.Count - 1; i >= 0; i--)
  46. {
  47. if (string.IsNullOrEmpty(filelines[i])) filelines.RemoveAt(i);
  48. }
  49. return filelines.ToArray();
  50. }
  51. private static void Line1(string currentline, ref ToDo todo)
  52. {
  53. string strDate = currentline.Substring(5, currentline.IndexOf("*") - 6);
  54. strDate = strDate.Trim();
  55. strDate.Replace(" ", " ");
  56. todo.WorkOrder.CreateDate = Global.DateFormatter(strDate);
  57. }
  58. private static void Line2(string currentline, ref ToDo todo)
  59. {
  60. string strWONum = currentline.Substring(currentline.IndexOf("Work Order") + 10, 12);
  61. strWONum = strWONum.Trim();
  62. todo.WorkOrder.Number = strWONum;
  63. todo.WorkOrderNumber = strWONum;
  64. string strSeg = currentline.Substring(currentline.IndexOf("Seg.") + 4, 25);
  65. strSeg = strSeg.Trim();
  66. todo.WorkOrder.Segment = strSeg;
  67. string strODate = currentline.Substring(currentline.IndexOf("Opn Dt") + 6);
  68. strODate = strODate.Trim();
  69. todo.WorkOrder.OpnDt = Global.DateFormatter(strODate, false);
  70. }
  71. private static void Line5678(string currentline, ref ToDo todo, int lineNumber)
  72. {
  73. if (lineNumber == 5)
  74. {
  75. todo.Customer.Name = currentline.Substring(3, 25);
  76. todo.Customer.Number = currentline.Substring(38, 10);
  77. }
  78. if (lineNumber == 6)
  79. {
  80. todo.Customer.Addr1 = currentline.Substring(3, 25);
  81. }
  82. if (lineNumber == 7)
  83. {
  84. todo.Customer.Addr2 = currentline.Substring(3, 25);
  85. }
  86. if (lineNumber == 8)
  87. {
  88. int comma = currentline.IndexOf(",");
  89. int length = currentline.Length - 3;
  90. todo.Customer.City = (comma == -1) ? "" : currentline.Substring(3, comma - 3);
  91. todo.Customer.State = currentline.Substring(currentline.IndexOf(",") + 2, 2);
  92. todo.Customer.Zip = currentline.Substring(comma + 5, 7);
  93. }
  94. }
  95. private static void Line11(string currentline, ref ToDo todo)
  96. {
  97. todo.Equipment.Make = currentline.Substring(0, 8).Trim();
  98. todo.Equipment.Model = currentline.Substring(8, 14).Trim();
  99. todo.Equipment.SerialNumber = currentline.Substring(22, 22).Trim();
  100. todo.Equipment.EquipmentNumber = currentline.Substring(42, 16).Trim();
  101. todo.Equipment.MeterReading = currentline.Substring(60, 7).Trim();
  102. todo.Equipment.MID = EquipmentImport.MachineExists(todo.Equipment.SerialNumber);
  103. if (todo.Equipment.MID == 0) todo.Equipment.IsNew = true;
  104. else todo.Equipment.IsNew = false;
  105. }
  106. private static void CreateAPart(string currentline, ref ToDo todo)
  107. {
  108. PartImport pt = new PartImport();
  109. string sline = currentline.Substring(19).Trim();
  110. //pt.Description = currentline.Substring(34, 14);
  111. pt.Number = sline.Substring(0, sline.IndexOf(" "));
  112. sline = sline.Substring(sline.IndexOf(" ")).Trim();
  113. pt.Description = sline.Substring(0, sline.IndexOf(" ")).Trim();
  114. pt.Qty = currentline.Substring(0, currentline.IndexOf(" ")).Trim();
  115. todo.Parts.Add(pt);
  116. }
  117. private static void PMInterval(string currentline, ref ToDo todo, ref bool isSpecial)
  118. {
  119. isSpecial = false;
  120. string pminterval = currentline.Substring(currentline.IndexOf(":") + 2);
  121. pminterval = pminterval.Substring(0, pminterval.IndexOf(" ")).Trim();
  122. if (XMLData.IntervalLookup.ContainsKey(pminterval)) todo.WorkOrder.InspectionLevel = XMLData.IntervalLookup[pminterval].ToString();
  123. if (!string.IsNullOrEmpty(todo.WorkOrder.InspectionLevel)) todo.WorkOrder.InspectionInterval = pminterval;
  124. string strService = currentline.Substring(currentline.IndexOf(":") + 2);
  125. strService = strService.Substring(strService.IndexOf(" ") + 1).Trim();
  126. strService = strService.Substring(0, strService.IndexOf(" ") + 1).Trim();
  127. if (strService.IndexOf(" ") > 0)
  128. strService = strService.Substring(0, strService.IndexOf(" ")).Trim();
  129. if (InspectionType.ContainsKey(strService))
  130. todo.WorkOrder.InspectionType = InspectionType[strService].ToString();
  131. else todo.WorkOrder.InspectionType = InspectionType["PM"].ToString();
  132. string codeanddescriptionoriginal = currentline.Substring(currentline.IndexOf(":") + 2);
  133. codeanddescriptionoriginal = codeanddescriptionoriginal.Substring(0, codeanddescriptionoriginal.IndexOf(" T") - 3).Trim();
  134. todo.WorkOrder.CodeAndDescriptionOriginal = codeanddescriptionoriginal;
  135. }
  136. private static void EmpID(string currentline, ref ToDo todo, ref bool isSpecial)
  137. {
  138. isSpecial = false;
  139. string empid = currentline.Substring(currentline.IndexOf(":") + 2);
  140. empid = empid.Substring(0, empid.IndexOf(" ")).Trim();
  141. todo.WorkOrder.TechnicianUserID = empid;
  142. }
  143. private static void PromiseDate(string currentline, ref ToDo todo, ref bool isSpecial)
  144. {
  145. isSpecial = false;
  146. string promisedate = currentline.Substring(currentline.IndexOf(":") + 2);
  147. promisedate = promisedate.Substring(0, promisedate.IndexOf(" ")).Trim();
  148. todo.WorkOrder.ScheduledStartDate = Global.DateFormatter(promisedate, false);
  149. }
  150. private static void SpecialInstructions(string currentline, ref ToDo todo, ref bool isSpecial, ref StringBuilder sb)
  151. {
  152. isSpecial = true;
  153. string special = currentline.Substring(currentline.IndexOf(":") + 2);
  154. special = special.Substring(0, special.IndexOf(" T") - 1).Trim();
  155. sb.Append(special);
  156. }
  157. private static void Line14AndUp(string currentline, ref ToDo todo, ref bool isSpecial, ref StringBuilder sb)
  158. {
  159. if (currentline.EndsWith(" T"))
  160. {
  161. if (currentline.Contains("PM LEVEL")) isSpecial = false;
  162. else if (currentline.Contains("PM INTERVAL")) PMInterval(currentline, ref todo, ref isSpecial);
  163. else if (currentline.Contains("EMPID")) EmpID(currentline, ref todo, ref isSpecial);
  164. else if (currentline.Contains("PROMISE DATE")) PromiseDate(currentline, ref todo, ref isSpecial);
  165. else if (currentline.Contains("SPECIAL INSTRUCTIONS")) SpecialInstructions(currentline, ref todo, ref isSpecial, ref sb);
  166. else
  167. {
  168. if (isSpecial)
  169. {
  170. string addspecial = currentline.Substring(0, currentline.IndexOf(" T") - 1).Trim();
  171. sb.Append(string.Format(" {0}", addspecial));
  172. }
  173. }
  174. }
  175. else //Decide if it is a part
  176. {
  177. isSpecial = false;
  178. int qty = 0;
  179. if (currentline.Length > 0)
  180. {
  181. string input = currentline.Substring(0, (currentline.IndexOf(" ") == -1) ? 1 : currentline.IndexOf(" ")).Trim();
  182. if (int.TryParse(input, out qty))
  183. {
  184. if (qty < 100 && !currentline.Contains("HR"))
  185. {
  186. CreateAPart(currentline, ref todo);
  187. }
  188. }
  189. }
  190. }
  191. }
  192. private static Customer IsNewCustomer(string name)
  193. {
  194. List<Customer> list = _customers.FindAll(x => x.Name.Trim() == name.Trim());
  195. if (list.Count > 1)
  196. return list[list.Count - 1];
  197. else if (list.Count == 1)
  198. return list[0];
  199. else return null;
  200. }
  201. private static Customer CreateNewCustomer(ToDo todo)
  202. {
  203. Customer cust = new Customer();
  204. cust.Name = todo.Customer.Name;
  205. cust.Address = todo.Customer.Addr1;
  206. cust.Address2 = todo.Customer.Addr2;
  207. cust.City = todo.Customer.City;
  208. cust.State = todo.Customer.State;
  209. cust.CreateDate = DateTime.Now;
  210. cust.CreateUserID = -1;
  211. cust.UpdateDate = DateTime.Now;
  212. cust.UpdateUserID = -1;
  213. cust.IsActive = true;
  214. cust.Number = todo.Customer.Number;
  215. cust.ZipCode = todo.Customer.Zip;
  216. string json = Serialize.ToJson<Customer>(cust);
  217. cust = _rservice.PostRest<Customer>(json, "customers.svc");
  218. return cust;
  219. }
  220. private static Model CreateNewModel(Product defaultP, ToDo todo)
  221. {
  222. Model mdl = new Model();
  223. //Find the Make and assign the default Product
  224. mdl.Name = todo.Equipment.Model.Trim();
  225. mdl.IsActive = true;
  226. mdl.CreateDate = DateTime.Now;
  227. mdl.CreateUserID = -1;
  228. mdl.UpdateDate = DateTime.Now; ;
  229. mdl.UpdateUserID = -1;
  230. mdl.Product = defaultP;
  231. Make mk = (from x in _makes where x.Name.Trim() == todo.Equipment.Make.Trim() select x).FirstOrDefault();
  232. if (mk == null)
  233. {
  234. //Create new Make
  235. mk.Name = todo.Equipment.Make.Trim();
  236. mk.CreateDate = DateTime.Now;
  237. mk.CreateUserID = -1;
  238. mk.UpdateDate = DateTime.Now;
  239. mk.UpdateUserID = -1;
  240. mk.IsActive = true;
  241. mk.Description = todo.Equipment.Make.Trim();
  242. string mkJson = Serialize.ToJson<Make>(mk);
  243. mk = _rservice.PostRest<Make>(mkJson, "makes.svc");
  244. }
  245. mdl.Make = mk;
  246. string json = Serialize.ToJson<Model>(mdl);
  247. mdl = _rservice.PostRest<Model>(json, "models.svc");
  248. return mdl;
  249. }
  250. private static Equipment CreateNewMachine(Model mdl, Customer cust, ToDo todo)
  251. {
  252. Equipment newMachine = new Equipment();
  253. newMachine.Model = mdl;
  254. newMachine.Make = mdl.Make;
  255. newMachine.Product = mdl.Product;
  256. newMachine.BarcodeID = null;
  257. newMachine.CreateDate = DateTime.Now;
  258. newMachine.UpdateDate = DateTime.Now;
  259. newMachine.UpateUserID = -1;
  260. newMachine.CreateUserID = -1;
  261. newMachine.Description = todo.Equipment.SerialNumber;
  262. newMachine.IsActive = true;
  263. newMachine.LastServiceDate = null;
  264. newMachine.Location = null;
  265. newMachine.ManufacturerYear = "2000";
  266. newMachine.MeterReading = todo.Equipment.MeterReading;
  267. newMachine.Number = todo.Equipment.EquipmentNumber;
  268. newMachine.Odometer = null;
  269. newMachine.SerialNumber = todo.Equipment.SerialNumber;
  270. newMachine.Customer = cust;
  271. string eqJson = Serialize.ToJson<Equipment>(newMachine);
  272. newMachine = _rservice.PostRest<Equipment>(eqJson, "Equipments.svc");
  273. return newMachine;
  274. }
  275. private static List<Equipment> IsNewMachine(string serial)
  276. {
  277. Dictionary<string, string> ps = new Dictionary<string, string>();
  278. ps.Add("search", serial);
  279. List<Equipment> machines = _rservice.RestGet<Equipment>("equipments.svc", ps);
  280. return machines;
  281. }
  282. private static void APIPush(ref ToDo todo)
  283. {
  284. Equipment machine = new Equipment();
  285. string strModel = todo.Equipment.Model.Trim();
  286. Model mdl = _models.Find(x => x.Name.Trim() == strModel);
  287. Product defaultP = _products.Find(x => x.ID == 14);
  288. //Customer cust = new Customer();
  289. Customer cust = IsNewCustomer(todo.Customer.Name);
  290. if (cust == null)
  291. cust = CreateNewCustomer(todo);
  292. List<Equipment> equipment = IsNewMachine(todo.Equipment.SerialNumber);
  293. todo.Equipment.IsNew = equipment.Count == 0;
  294. if (todo.Equipment.IsNew)
  295. {
  296. bool isNewModel = mdl == null;
  297. if (isNewModel)
  298. {
  299. string sModel = todo.Equipment.Model.Trim().Substring(0, todo.Equipment.Model.Length - 1);
  300. mdl = (from x in _models where x.Name.Contains(sModel) orderby x.ID select x).LastOrDefault();
  301. if (mdl == null)
  302. mdl = CreateNewModel(defaultP, todo);
  303. else
  304. machine = CreateNewMachine(mdl, cust, todo);
  305. }
  306. else
  307. {
  308. machine = CreateNewMachine(mdl, cust, todo);
  309. }
  310. }
  311. else
  312. {
  313. string eqSerial = todo.Equipment.SerialNumber.Trim();
  314. List<Equipment> eqlist = equipment.FindAll(x => x.SerialNumber == eqSerial);
  315. machine = eqlist[eqlist.Count - 1];
  316. machine.Customer = cust;
  317. }
  318. //Find the Template
  319. string il = todo.WorkOrder.InspectionLevel.Trim();
  320. string it = todo.WorkOrder.InspectionType.Trim();
  321. string eqmdl = todo.Equipment.Model.Trim();
  322. Template template = _templates.Find(x => x.InspectionLevel.ID == Convert.ToInt32(il) && x.InspectionType.ID == Convert.ToInt32(it) && x.Name == eqmdl);
  323. if (template == null)
  324. {
  325. //Search by model prefix and get the last one
  326. string prefix = todo.Equipment.Model.Trim();
  327. prefix = prefix.Substring(0, prefix.Length - 1);
  328. 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();
  329. if (template == null || template.ID == 0)
  330. {
  331. todo.Success = false;
  332. todo.Validation = todo.Validation + " No template for this model.";
  333. }
  334. }
  335. string tuid = todo.WorkOrder.TechnicianUserID.Trim();
  336. User user = _users.Find(x => x.Number == tuid);
  337. WorkOrder workorder = new WorkOrder();
  338. workorder.Number = todo.WorkOrder.Number;// wo.Number;
  339. workorder.Technician = user;
  340. workorder.Reason = _reason;
  341. workorder.Priority = _priority;
  342. workorder.Status = _status;
  343. workorder.CreateDate = DateTime.Now;
  344. workorder.CreateUserID = -1;
  345. workorder.Equipment = machine;
  346. workorder.Customer = cust;
  347. string mtrReading = todo.Equipment.MeterReading;
  348. workorder.MeterReading = Convert.ToDecimal(mtrReading);
  349. workorder.ScheduledStartDate = todo.WorkOrder.OpnDt;// wo.OpnDt;
  350. workorder.ScheduledEndDate = todo.WorkOrder.OpnDt.AddDays(1d);
  351. workorder.UpdateDate = DateTime.Now;
  352. workorder.UpdateUserID = -1;
  353. string woJson = Serialize.ToJson<WorkOrder>(workorder);
  354. //Console.WriteLine("WorkOrder Object");
  355. //Console.WriteLine();
  356. //Console.WriteLine(woJson);
  357. //JDISLog.WriteToLog(_logFile, string.Format("Creating Work Order: {0}", workorder.Number));
  358. workorder = _rservice.PostRest<WorkOrder>(woJson, "workorders.svc");
  359. if (workorder.ID != 0) todo.Success = true;
  360. if (template == null || template.ID == 0)
  361. {
  362. todo.Validation = string.Format("Work Order {0} was created but there is no template matching model # {1}", workorder.Number, mdl.Name);
  363. todo.Success = false;
  364. }
  365. else
  366. {
  367. //JDISLog.WriteToLog(_logFile, String.Format("Creating Inspections for Workorder {0}", workorder.Number));
  368. Dictionary<string, string> tps = new Dictionary<string, string>();
  369. tps.Add("create", string.Format("{0}/{1}/{2}", template.ID, workorder.ID, -1));
  370. Inspection i = _rservice.RestGet<Inspection>(true, "inspections.svc", tps);
  371. tps["create"] = string.Format("{0}/{1}/{2}", 1691, workorder.ID, -1);
  372. Inspection sta = _rservice.RestGet<Inspection>(true, "inspections.svc", tps);
  373. if (i != null && i.ID != 0)
  374. {
  375. todo.Success = true;
  376. }
  377. }
  378. }
  379. private static void ReadFile(ref ToDo todo)
  380. {
  381. todo.PathToFile = Path.Combine(Global.StorageDir, todo.FileName);
  382. todo.Customer = new CustomerImport();
  383. todo.WorkOrder = new WorkOrderImport();
  384. todo.Equipment = new EquipmentImport();
  385. todo.Parts = new List<PartImport>();
  386. string filemove = string.Empty;
  387. string filetxt = string.Empty;
  388. string inspectionLevel = string.Empty;
  389. bool isSpecial = false;
  390. StringBuilder specialInstructions = new StringBuilder();
  391. int line = 1;
  392. try
  393. {
  394. List<string> filelines = WriteFileToText(todo.PathToFile);
  395. string[] lines = RemoveWhiteSpace(ref filelines);
  396. foreach (string s in lines)
  397. {
  398. string currentline = s.Trim().Replace("'", "''");
  399. if (currentline.Contains("Page") && currentline.Substring(78).Trim() != "1")
  400. line = 18;
  401. if (currentline.Contains("Page") && currentline.Substring(78).Trim() == "1")
  402. line = 1;
  403. switch (line)
  404. {
  405. case 1:
  406. Line1(currentline, ref todo);
  407. break;
  408. case 2:
  409. Line2(currentline, ref todo);
  410. break;
  411. case 5:
  412. case 6:
  413. case 7:
  414. case 8:
  415. Line5678(currentline, ref todo, line);
  416. break;
  417. case 11:
  418. Line11(currentline, ref todo);
  419. break;
  420. default:
  421. break;
  422. }
  423. if (line > 14)
  424. {
  425. Line14AndUp(currentline, ref todo, ref isSpecial, ref specialInstructions);
  426. }
  427. line++;
  428. }
  429. if (todo.WorkOrder.ScheduledStartDate == DateTime.MinValue) todo.WorkOrder.ScheduledStartDate = DateTime.Now;
  430. // List<CSVObject> list = new List<CSVObject>();
  431. // List<CSVObject> exceptions = new List<CSVObject>();
  432. if (todo.Parts.Count == 0)
  433. {
  434. PartImport p = new PartImport();
  435. p.Description = string.Empty;
  436. p.Qty = string.Empty;
  437. p.Number = string.Empty;
  438. todo.Parts.Add(p);
  439. }
  440. APIPush(ref todo);
  441. }
  442. catch (Exception ex)
  443. {
  444. Console.WriteLine(ex.Message);
  445. }
  446. }
  447. public static void BeginImport(List<ToDo> todos)
  448. {
  449. for (int i = 0; i < todos.Count; i++)
  450. {
  451. ToDo todo = todos[i];
  452. XMLData.BuildIntervalLookup();
  453. GetInspectionTypes();
  454. ReadFile(ref todo);
  455. todos[i] = todo;
  456. }
  457. }
  458. public static void GetInspectionTypes()
  459. {
  460. List<InspectionType> list = _rservice.RestGet<InspectionType>("inspectiontypes.svc");
  461. InspectionType = new Dictionary<string, int>();
  462. foreach (InspectionType t in list)
  463. {
  464. InspectionType.Add(t.Name, t.ID);
  465. }
  466. }
  467. public static void BuildDefaults()
  468. {
  469. if (Global.Settings.Remote)
  470. _serviceUrl = Global.Settings.Prelub.RemoteService;
  471. else _serviceUrl = Global.Settings.Prelub.LocalService;
  472. _rservice = new RESTService();
  473. _customers = _rservice.RestGet<Customer>("customers.svc");
  474. _makes = _rservice.RestGet<Make>("makes.svc");
  475. _products = _rservice.RestGet<Product>("products.svc");
  476. _models = _rservice.RestGet<Model>("models.svc");
  477. _inspectionTypes = _rservice.RestGet<InspectionType>("inspectionTypes.svc");
  478. _templates = _rservice.RestGet<Template>("templates.svc");
  479. _users = _rservice.RestGet<User>("users.svc");
  480. _status = new Status();
  481. _status.ID = 1;
  482. _status.Name = "Open";
  483. _priority = new Priority();
  484. _priority.ID = 3;
  485. _priority.Name = "Medium";
  486. _reason = new Reason();
  487. _reason.ID = 9;
  488. _reason.Name = "Cust. Request";
  489. _reason.IsActive = true;
  490. _reason.CreateDate = new DateTime(2013, 10, 1, 9, 23, 7);
  491. _reason.CreateUserID = -1;
  492. _reason.UpdateDate = new DateTime(2013, 10, 1, 9, 23, 7);
  493. _reason.UpdateUserID = -1;
  494. }
  495. }
  496. public class JDISLog
  497. {
  498. public static void WriteToLog(string logFile, string msg)
  499. {
  500. using (StreamWriter sw = File.AppendText(logFile))
  501. {
  502. sw.WriteLine(msg);
  503. }
  504. }
  505. public static void Begin(string logFile)
  506. {
  507. using (StreamWriter sw = File.AppendText(logFile))
  508. {
  509. sw.WriteLine();
  510. sw.WriteLine("###########################################################################");
  511. sw.WriteLine(string.Format("Service Started @ {0}", DateTime.Now.ToShortDateString(), DateTime.Now.ToLongTimeString()));
  512. }
  513. }
  514. }
  515. }