LogikalPartsManager.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using Comal.Classes;
  6. using InABox.Clients;
  7. using InABox.Core;
  8. using InABox.Integration.Logikal;
  9. using InABox.Wpf;
  10. using InABox.WPF;
  11. using javax.print.attribute.standard;
  12. using net.sf.mpxj;
  13. using PRSDesktop.Integrations.Common;
  14. namespace PRSDesktop.Integrations.Logikal;
  15. public class LogikalPartsManager
  16. {
  17. public List<Product> Products { get; private set; } = new();
  18. public List<ProductStyle> ProductStyles { get; private set; } = new();
  19. public List<Comal.Classes.Activity> Activities { get; private set; } = new();
  20. //
  21. // public LogikalProductMapping[] ProcessComponents(IEnumerable<LogikalComponent> components)
  22. // {
  23. // List<LogikalProductMapping> results = new();
  24. //
  25. // var finishes = components
  26. // .Select(x => x.Finish.ToUpper())
  27. // .Distinct()
  28. // .OrderBy(x=>x)
  29. // .ToArray();
  30. // MultiQuery query = new MultiQuery();
  31. //
  32. // query.Add<ProductStyle>(
  33. // new Filter<ProductStyle>(x=>x.Code).InList(finishes),
  34. // Columns.None<ProductStyle>()
  35. // .Add(x=>x.ID)
  36. // .Add(x=>x.Code)
  37. // .Add(x=>x.Description)
  38. // );
  39. //
  40. // query.Add<ProductStyleIntegration>(
  41. // new Filter<ProductStyleIntegration>(x=>x.Integration).IsEqualTo(IntegrationType.Logikal)
  42. // .And(x=>x.Code).InList(finishes),
  43. // Columns.None<ProductStyleIntegration>()
  44. // .Add(x=>x.Code)
  45. // .Add(x=>x.Style.ID)
  46. // .Add(x=>x.Style.Code)
  47. // .Add(x=>x.Style.Description)
  48. // );
  49. //
  50. // query.Query();
  51. //
  52. // var styles = query.Get<ProductStyle>()
  53. // .ToObjects<ProductStyle>()
  54. // .ToArray();
  55. // var mappings = query.Get<ProductStyleIntegration>()
  56. // .ToObjects<ProductStyleIntegration>()
  57. // .ToArray();
  58. //
  59. //
  60. // foreach (var finish in finishes)
  61. // {
  62. // var style = styles.FirstOrDefault(x => string.Equals(x.Code, finish.Code));
  63. // if (style != null)
  64. // {
  65. // var result = new LogikalProductStyleMapping();
  66. // result.Code = finish;
  67. // result.Product.ID = style.ID;
  68. // result.Add(new LogikalProductMapping() { Code= finish, });
  69. // }
  70. // }
  71. //
  72. // return [];
  73. // }
  74. //
  75. //
  76. // public LogikalProductMapping[] ProcessComponents(IEnumerable<LogikalGlass> glass)
  77. // {
  78. // return [];
  79. // }
  80. public bool CheckMissingItems(ILogikalPartsResponse<LogikalFinish, LogikalProfile, LogikalComponent, LogikalGlass, LogikalLabour> bom)
  81. {
  82. List<String> errors = new();
  83. if (LogikalCommon.ProfileUOM == null)
  84. errors.Add("Profile UOM is incorrect");
  85. if (LogikalCommon.ComponentUOM == null)
  86. errors.Add("Component UOM is incorrect");
  87. if (LogikalCommon.GlassUOM == null)
  88. errors.Add("Glass UOM is incorrect");
  89. if (LogikalCommon.TaxCode == null)
  90. errors.Add("Tax Code is incorrect");
  91. if (errors.Any())
  92. {
  93. MessageWindow.ShowMessage($"Configuration is invalid\n\n- {string.Join("\n- ",errors)}", "Error");
  94. return false;
  95. }
  96. string[] _requiredProfiles = bom.Profiles
  97. .Select(x => x.Code ?? "")
  98. .Where(x => !string.IsNullOrWhiteSpace(x))
  99. .Distinct()
  100. .ToArray();
  101. string[] _requiredFinishes = bom.Finishes
  102. .Select(x => x.Code ?? "")
  103. .Where(x => !string.IsNullOrWhiteSpace(x))
  104. .Distinct()
  105. .ToArray();
  106. string[] _requiredComponents = bom.Components
  107. .Select(x => x.Code ?? "")
  108. .Where(x => !string.IsNullOrWhiteSpace(x))
  109. .Distinct()
  110. .ToArray();
  111. string[] _requiredGlass = bom.Glass
  112. .Select(x => x.Code ?? "")
  113. .Where(x => !string.IsNullOrWhiteSpace(x))
  114. .Distinct()
  115. .ToArray();
  116. string[] _requiredProducts = _requiredProfiles
  117. .Union(_requiredComponents)
  118. .Union(_requiredGlass)
  119. .ToArray();
  120. string[] _requiredActivities = bom.Labour
  121. .Select(x => x.Code ?? "")
  122. .Where(x => !string.IsNullOrWhiteSpace(x))
  123. .Distinct()
  124. .ToArray();
  125. MultiQuery query = new MultiQuery();
  126. Progress.ShowModal("Checking Missing Items", progress =>
  127. {
  128. query.Add(
  129. new Filter<ProductStyle>(x => x.Code).InList(_requiredFinishes),
  130. Columns.All<ProductStyle>()
  131. );
  132. query.Add(
  133. new Filter<Product>(x => x.Code).InList(_requiredProducts),
  134. Columns.None<Product>()
  135. .Add(x => x.ID)
  136. .Add(x => x.Code)
  137. .Add(x => x.Name)
  138. .Add(x => x.UnitOfMeasure.ID)
  139. .Add(x => x.UnitOfMeasure.Code)
  140. .Add(x => x.UnitOfMeasure.Description)
  141. .Add(x => x.UnitOfMeasure.HasQuantity)
  142. .Add(x => x.UnitOfMeasure.HasLength)
  143. .Add(x => x.UnitOfMeasure.HasWidth)
  144. .Add(x => x.UnitOfMeasure.HasHeight)
  145. .Add(x => x.UnitOfMeasure.Format)
  146. .Add(x => x.UnitOfMeasure.Formula)
  147. .Add(x => x.TaxCode.ID)
  148. .Add(x => x.TaxCode.Code)
  149. );
  150. query.Add(
  151. new Filter<Comal.Classes.Activity>(x => x.Code).InList(_requiredActivities),
  152. Columns.All<Comal.Classes.Activity>()
  153. );
  154. query.Query();
  155. });
  156. ProductStyles = query.Get<ProductStyle>().ToObjects<ProductStyle>().ToList();
  157. var _missingFinishes = bom.Finishes.Where(l => !ProductStyles.Any(s => string.Equals(l.Code, s.Code))).ToArray();
  158. if (_missingFinishes.Any())
  159. {
  160. var missing = string.Join("\n- ", _missingFinishes.Select(x => $"{x.Code}: {x.Description}"));
  161. if (!MessageWindow.ShowYesNo(
  162. $"The following Product Styles are missing\n- {missing}\n\nDo you wish to create them now?",
  163. "Missing Finishes"))
  164. return false;
  165. }
  166. Products = query.Get<Product>().ToObjects<Product>().ToList();
  167. var _missingProducts = _requiredProducts.Where(x => !Products.Any(p => string.Equals(p.Code, x))).ToArray();
  168. var _missingProfiles = bom.Profiles.Where(x => _missingProducts.Contains(x.Code)).ToArray();
  169. if (_missingProfiles.Any())
  170. {
  171. var missing = string.Join("\n- ", _missingProfiles.Select(x => $"{x.Code}: {x.Description}"));
  172. if (!MessageWindow.ShowYesNo(
  173. $"The following Profiles are missing\n- {missing}\n\nDo you wish to create them now?",
  174. "Missing Profiles"))
  175. return false;
  176. }
  177. var _missingComponents = bom.Components.Where(x => _missingProducts.Contains(x.Code)).ToArray();
  178. if (_missingComponents.Any())
  179. {
  180. var missing = string.Join("\n- ", _missingComponents.Select(x => $"{x.Code}: {x.Description}"));
  181. if (!MessageWindow.ShowYesNo(
  182. $"The following Components are missing\n- {missing}\n\nDo you wish to create them now?",
  183. "Missing Components"))
  184. return false;
  185. }
  186. var _missingGlasses = bom.Glass.Where(x => _missingProducts.Contains(x.Code)).ToArray();
  187. if (_missingGlasses.Any())
  188. {
  189. var missing = string.Join("\n- ", _missingGlasses.Select(x => $"{x.Code}: {x.Description}"));
  190. if (!MessageWindow.ShowYesNo(
  191. $"The following Glass Codes are missing\n- {missing}\n\nDo you wish to create them now?",
  192. "Missing Glass"))
  193. return false;
  194. }
  195. Activities = query.Get<Activity>().ToObjects<Activity>().ToList();
  196. var _missingActivities = bom.Labour.Where(l => !Activities.Any(a => string.Equals(l.Code,a.Code))).ToArray();
  197. if (_missingActivities.Any())
  198. {
  199. var missing = string.Join("\n- ", _missingActivities.Select(x => $"{x.Code}: {x.Description}"));
  200. if (!MessageWindow.ShowYesNo(
  201. $"The following Activities are missing\n- {missing}\n\nDo you wish to create them now?",
  202. "Missing Products"))
  203. return false;
  204. }
  205. if (_missingFinishes.Any() || _missingProducts.Any() || _missingActivities.Any())
  206. {
  207. Progress.ShowModal("Creating Missing Items", progress =>
  208. {
  209. if (_missingFinishes.Any())
  210. {
  211. progress.Report("Creating Missing Finishes");
  212. List<ProductStyle> _newFinishes = new();
  213. foreach (var _missingFinish in _missingFinishes)
  214. {
  215. _newFinishes.Add(
  216. new ProductStyle()
  217. {
  218. Code = _missingFinish.Code,
  219. Description = _missingFinish.Description
  220. }
  221. );
  222. }
  223. Client.Save(_newFinishes, "Created By LogikalImport");
  224. ProductStyles.AddRange(_newFinishes);
  225. }
  226. if (_missingProfiles.Any())
  227. {
  228. progress.Report("Creating Missing Profiles");
  229. List<Product> _newProfiles = new();
  230. foreach (var _missingProfile in _missingProfiles)
  231. {
  232. var _newprofile = new Product()
  233. {
  234. Code = _missingProfile.Code ?? "",
  235. Name = _missingProfile.Description ?? ""
  236. };
  237. _newprofile.Problem.Notes = new string[] { "Created by Logikal Import" };
  238. _newprofile.UnitOfMeasure.CopyFrom(LogikalCommon.ProfileUOM);
  239. _newProfiles.Add(_newprofile);
  240. }
  241. Client.Save(_newProfiles, "Created By LogikalImport");
  242. Products.AddRange(_newProfiles);
  243. }
  244. if (_missingComponents.Any())
  245. {
  246. progress.Report("Creating Missing Components");
  247. List<Product> _newComponents = new();
  248. foreach (var _missingComponent in _missingComponents)
  249. {
  250. var _newComponent = new Product()
  251. {
  252. Code = _missingComponent.Code ?? "",
  253. Name = _missingComponent.Description ?? "",
  254. };
  255. _newComponent.Problem.Notes = new string[] { "Created by Logikal Import" };
  256. _newComponent.UnitOfMeasure.CopyFrom(LogikalCommon.ComponentUOM);
  257. _newComponents.Add(_newComponent);
  258. }
  259. Client.Save(_newComponents, "Created By LogikalImport");
  260. Products.AddRange(_newComponents);
  261. }
  262. if (_missingGlasses.Any())
  263. {
  264. progress.Report("Creating Missing Glass");
  265. List<Product> _newGlasses = new();
  266. foreach (var _missingGlass in _missingGlasses)
  267. {
  268. var _newGlass = new Product()
  269. {
  270. Code = _missingGlass.Code ?? "",
  271. Name = _missingGlass.Description ?? "",
  272. };
  273. _newGlass.Problem.Notes = new string[] { "Created by Logikal Import" };
  274. _newGlass.UnitOfMeasure.CopyFrom(LogikalCommon.GlassUOM);
  275. _newGlasses.Add(_newGlass);
  276. }
  277. Client.Save(_newGlasses, "Created By LogikalImport");
  278. Products.AddRange(_newGlasses);
  279. }
  280. if (_missingActivities.Any())
  281. {
  282. progress.Report("Creating Missing Activities");
  283. List<Comal.Classes.Activity> _newActivities = new();
  284. foreach (var _missingActivity in _missingActivities)
  285. {
  286. var _newActivity = new Comal.Classes.Activity()
  287. {
  288. Code = _missingActivity.Code,
  289. Description = _missingActivity.Description,
  290. };
  291. _newActivity.Problem.Notes = new string[] { "Created by Logikal Import" };
  292. _newActivities.Add(_newActivity);
  293. }
  294. Client.Save(_newActivities, "Created By LogikalImport");
  295. Activities.AddRange(_newActivities);
  296. }
  297. });
  298. List<String> _missing = new();
  299. _missing.AddRange(_missingFinishes.Select(x => $"- Style {x.Code}: {x.Description}").Distinct().OrderBy(x => x));
  300. _missing.AddRange(_missingProfiles.Select(x => $"- Profile {x.Code}: {x.Description}").Distinct().OrderBy(x => x));
  301. _missing.AddRange(_missingComponents.Select(x => $"- Component {x.Code}: {x.Description}").Distinct().OrderBy(x => x));
  302. _missing.AddRange(_missingGlasses.Select(x => $"- Glass {x.Code}: {x.Description}").Distinct().OrderBy(x => x));
  303. _missing.AddRange(_missingActivities.Select(x => $"- Activity {x.Code}: {x.Description}").Distinct().OrderBy(x => x));
  304. if (_missing.Any())
  305. MessageWindow.ShowMessage($"The following items were auto-created and should be manually checked:\n{String.Join("\n", _missing)}", "Results");
  306. }
  307. return true;
  308. }
  309. public T[] CreateItems<T>(JobBillOfMaterials jobbom, LogikalBOMResponse<LogikalFinish, LogikalProfile, LogikalComponent, LogikalGlass, LogikalLabour> bom, Action<LogikalBOMItem,T> customise) where T : StockEntity, IJobMaterial, new()
  310. {
  311. List<T> results = new List<T>();
  312. foreach (var profile in bom.Profiles)
  313. {
  314. var product = Products.FirstOrDefault(x => string.Equals(x.Code, profile.Code)) ?? new Product();
  315. var finish = ProductStyles.FirstOrDefault(x => string.Equals(x.Code, profile.Finish)) ?? new ProductStyle();
  316. if (product != null)
  317. {
  318. T newitem = new T();
  319. newitem.Job.ID = jobbom.Job.ID;
  320. newitem.Product.CopyFrom(product);
  321. newitem.Dimensions.Unit.CopyFrom(product.UnitOfMeasure);
  322. newitem.Dimensions.Length = profile.Length;
  323. newitem.Style.CopyFrom(finish);
  324. customise(profile, newitem);
  325. results.Add(newitem);
  326. }
  327. }
  328. foreach (var component in bom.Components)
  329. {
  330. var product = Products.FirstOrDefault(x => string.Equals(x.Code, component.Code)) ?? new Product();
  331. if (product != null)
  332. {
  333. T newitem = new T();
  334. newitem.Job.ID = jobbom.Job.ID;
  335. newitem.Product.CopyFrom(product);
  336. newitem.Dimensions.Unit.CopyFrom(product.UnitOfMeasure);
  337. newitem.Dimensions.Quantity = component.PackSize;
  338. customise(component, newitem);
  339. results.Add(newitem);
  340. }
  341. }
  342. foreach (var glass in bom.Glass)
  343. {
  344. var product = Products.FirstOrDefault(x => string.Equals(x.Code, glass.Code)) ?? new Product();
  345. if (product != null)
  346. {
  347. T newitem = new T();
  348. newitem.Job.ID = jobbom.Job.ID;
  349. newitem.Product.CopyFrom(product);
  350. newitem.Dimensions.Unit.CopyFrom(product.UnitOfMeasure);
  351. newitem.Dimensions.Height = glass.Height;
  352. newitem.Dimensions.Height = glass.Width;
  353. customise(glass, newitem);
  354. results.Add(newitem);
  355. }
  356. }
  357. return results.ToArray();
  358. }
  359. }