LogikalCommon.cs 18 KB

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