V6ProjectImport.xaml.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Media.Imaging;
  8. using Comal.Classes;
  9. using InABox.Clients;
  10. using InABox.Core;
  11. using InABox.DynamicGrid;
  12. using InABox.Wpf;
  13. using InABox.WPF;
  14. using NPOI.OpenXmlFormats.Wordprocessing;
  15. using PRSDesktop.Integations.V6;
  16. namespace PRSDesktop;
  17. public class V6ProjectImportGrid : DynamicItemsListGrid<V6Quote>
  18. {
  19. private static BitmapImage Quotation => PRSDesktop.Resources.quotation.AsBitmapImage();
  20. private static BitmapImage Revision => PRSDesktop.Resources.revision.AsBitmapImage();
  21. public V6ProjectImportGrid()
  22. {
  23. ActionColumns.Add(new DynamicImageColumn(StatusImage) { Position = DynamicActionColumnPosition.Start});
  24. }
  25. private BitmapImage? StatusImage(CoreRow? row)
  26. {
  27. return row == null
  28. ? Quotation
  29. : string.IsNullOrWhiteSpace(row.Get<V6Quote, string>(x => x.Variation))
  30. ? Quotation
  31. : Revision;
  32. }
  33. protected override void DoReconfigure(DynamicGridOptions options)
  34. {
  35. base.DoReconfigure(options);
  36. options.FilterRows = true;
  37. options.HideDatabaseFilters = true;
  38. }
  39. }
  40. public partial class V6ProjectImport : Window
  41. {
  42. private readonly V6Client _client;
  43. public V6ProjectImport()
  44. {
  45. InitializeComponent();
  46. _client = new V6Client();
  47. ImportCosts.SelectedValue = _client.Settings.ImportCosts;
  48. ImportDesigns.SelectedValue = _client.Settings.ImportDesigns;
  49. if (_client.Connect())
  50. {
  51. Task<List<V6Quote>> v6Task = Task.Run(() =>
  52. {
  53. var _quotes = _client.IsConnected
  54. ? _client?.GetQuotes()?.ToList()
  55. : null;
  56. return _quotes ?? new List<V6Quote>();
  57. });
  58. Task<List<JobScope>> prsTask = Task.Run(() => Client.Query<JobScope>(null,
  59. Columns.None<JobScope>().Add(x => x.Job.ID).Add(x => x.Job.JobNumber).Add(x=>x.Job.SourceRef).Add(x=>x.Job.DefaultScope.ID).Add(x=>x.ID).Add(x=>x.Number).Add(x=>x.SourceRef)
  60. ).ToObjects<JobScope>().ToList());
  61. Task.WaitAll(v6Task,prsTask);
  62. var quotes = v6Task.Result;
  63. var scopes = prsTask.Result;
  64. Projects.Items = quotes
  65. .Where(q => string.IsNullOrWhiteSpace(q.Variation)
  66. ? !scopes.Any(x=>string.Equals(x.Job.DefaultScope.SourceRef, $"{q.Number}"))
  67. : !scopes.Any(x=> string.Equals(x.SourceRef,$"{q.Number}")))
  68. .ToList();
  69. }
  70. else
  71. {
  72. MessageWindow.ShowMessage("Cannot connect to V6!","Error");
  73. Projects.Items = new List<V6Quote>();
  74. }
  75. Projects.Refresh(true,true);
  76. }
  77. private void OK_Click(object sender, RoutedEventArgs e)
  78. {
  79. var _project = Projects.LoadItem(Projects.SelectedRows.First());
  80. var _importCosts = (V6ImportCosts)ImportCosts.SelectedValue;
  81. var _importDesigns = (V6ImportDesigns)ImportDesigns.SelectedValue;
  82. JobStatus? _statusCode = new();
  83. TaxCode? _taxCode = new();
  84. ProductDimensionUnit? _profileUom = new();
  85. ProductDimensionUnit? _componentUom = new();
  86. ProductDimensionUnit? _glassUom = new();
  87. ManufacturingTemplate? _template = new ManufacturingTemplate();
  88. ManufacturingTemplateStage[] _stages = [];
  89. MultiQuery query = new MultiQuery();
  90. query.Add(
  91. new Filter<JobStatus>(x=>x.Code).IsEqualTo(_client.Settings.JobStatus),
  92. Columns.All<JobStatus>().Add(x=>x.ID)
  93. );
  94. query.Add(
  95. new Filter<TaxCode>(x=>x.Code).IsEqualTo(_client.Settings.TaxCode),
  96. Columns.All<TaxCode>()
  97. );
  98. if (_importCosts != V6ImportCosts.None)
  99. {
  100. query.Add(
  101. new Filter<ProductDimensionUnit>(x => x.Code).InList(new string[]
  102. { _client.Settings.ProfileUom, _client.Settings.ComponentUom, _client.Settings.GlassUom }),
  103. Columns.All<ProductDimensionUnit>()
  104. );
  105. }
  106. if (_importDesigns == V6ImportDesigns.ToManufacturing)
  107. {
  108. query.Add(new Filter<ManufacturingTemplate>(x => x.Code).IsEqualTo(_client.Settings.PacketTemplate));
  109. query.Add(new Filter<ManufacturingTemplateStage>(x => x.Template.Code).IsEqualTo(_client.Settings.PacketTemplate));
  110. }
  111. query.Query();
  112. _statusCode = query.Get<JobStatus>().Rows.FirstOrDefault()?.ToObject<JobStatus>();
  113. if (_statusCode == null)
  114. {
  115. MessageWindow.ShowMessage(
  116. "Job Status setting has not been configured correctly!\nPlease correct this and try again.", "Error");
  117. return;
  118. }
  119. _taxCode = query.Get<TaxCode>().Rows.FirstOrDefault()?.ToObject<TaxCode>();
  120. if (_taxCode == null)
  121. {
  122. MessageWindow.ShowMessage(
  123. "Tax Code setting has not been configured correctly!\nPlease correct this and try again.", "Error");
  124. return;
  125. }
  126. if (_importDesigns == V6ImportDesigns.ToManufacturing)
  127. {
  128. _template = query.Get<ManufacturingTemplate>().Rows.FirstOrDefault()?.ToObject<ManufacturingTemplate>();
  129. _stages = query.Get<ManufacturingTemplateStage>().ToObjects<ManufacturingTemplateStage>().ToArray();
  130. }
  131. if (_importCosts != V6ImportCosts.None)
  132. {
  133. var _uoms = query.Get<ProductDimensionUnit>().ToObjects<ProductDimensionUnit>().ToList();
  134. _profileUom = _uoms.FirstOrDefault(x => string.Equals(x.Code, _client.Settings.ProfileUom));
  135. _componentUom = _uoms.FirstOrDefault(x => string.Equals(x.Code, _client.Settings.ComponentUom));
  136. _glassUom = _uoms.FirstOrDefault(x => string.Equals(x.Code, _client.Settings.GlassUom));
  137. }
  138. if (_profileUom == null)
  139. {
  140. MessageWindow.ShowMessage(
  141. "Profile UOM setting has not been configured correctly!\nPlease correct this and try again.", "Error");
  142. return;
  143. }
  144. if (_componentUom == null)
  145. {
  146. MessageWindow.ShowMessage(
  147. "Component UOM settings has not been configured correctly!\nPlease correct this and try again.", "Error");
  148. return;
  149. }
  150. if (_glassUom == null)
  151. {
  152. MessageWindow.ShowMessage(
  153. "Glass UOM setting has not been configured correctly!\nPlease correct this and try again.", "Error");
  154. return;
  155. }
  156. if (_template == null)
  157. {
  158. MessageWindow.ShowMessage(
  159. "Packet Template setting has not been configured correctly!\nPlease correct this and try again.", "Error");
  160. return;
  161. }
  162. List<string> _finishes = new();
  163. List<V6Profile> _profiles = new();
  164. List<V6Component> _components = new();
  165. List<V6Glass> _glass = new();
  166. List<V6Labour> _labour = new();
  167. List<ProductStyle> _styles = new();
  168. List<Product> _products = new();
  169. List<Activity> _activities = new();
  170. Dictionary<V6Elevation, V6Drawings> _designs = new();
  171. List<V6Profile> _missingProfiles = new();
  172. List<V6Component> _missingComponents = new();
  173. List<V6Glass> _missingGlass = new();
  174. List<V6Labour> _missingLabour = new();
  175. List<string> _missingFinishes = new();
  176. string exception = null;
  177. if (_importCosts != V6ImportCosts.None)
  178. {
  179. Progress.ShowModal("Checking Products", progress =>
  180. {
  181. try
  182. {
  183. progress.Report("Loading Profiles");
  184. _profiles = _client.GetProfiles(_project.Number, _project.Variation);
  185. }
  186. catch (Exception _exception)
  187. {
  188. exception = $"Error retrieving Profiles : {_exception.Message}";
  189. return;
  190. }
  191. try
  192. {
  193. progress.Report("Loading Components");
  194. _components = _client.GetComponents(_project.Number, _project.Variation);
  195. }
  196. catch (Exception _exception)
  197. {
  198. exception = $"Error retrieving Components : {_exception.Message}";
  199. return;
  200. }
  201. try
  202. {
  203. progress.Report("Loading Glass");
  204. _glass = _client.GetGlass(_project.Number, _project.Variation);
  205. }
  206. catch (Exception _exception)
  207. {
  208. exception = $"Error retrieving Glass : {_exception.Message}";
  209. return;
  210. }
  211. progress.Report("Checking PRS Codes");
  212. MultiQuery query = new MultiQuery();
  213. var _productStyles = _profiles.Select(x => x.Finish)
  214. .Union(_glass.Select(x => x.Treatment))
  215. .Distinct()
  216. .ToArray();
  217. query.Add(
  218. new Filter<ProductStyle>(x => x.Code).InList(_productStyles),
  219. Columns.None<ProductStyle>().Add(x => x.ID).Add(x => x.Code)
  220. );
  221. var _productcodes = _profiles.Select(x => x.Code)
  222. .Union(_components.Select(x => x.Code))
  223. .Union(_glass.Select(x => x.Code))
  224. .Distinct()
  225. .ToArray();
  226. query.Add(
  227. new Filter<Product>(x => x.Code).InList(_productcodes),
  228. Columns.None<Product>()
  229. .Add(x => x.ID)
  230. .Add(x => x.Code)
  231. .Add(x => x.Name)
  232. .Add(x => x.UnitOfMeasure.ID)
  233. .Add(x => x.UnitOfMeasure.Code)
  234. .Add(x => x.UnitOfMeasure.Description)
  235. .Add(x => x.UnitOfMeasure.HasQuantity)
  236. .Add(x => x.UnitOfMeasure.HasLength)
  237. .Add(x => x.UnitOfMeasure.HasWidth)
  238. .Add(x => x.UnitOfMeasure.HasHeight)
  239. .Add(x => x.UnitOfMeasure.Format)
  240. .Add(x => x.UnitOfMeasure.Formula)
  241. .Add(x => x.TaxCode.ID)
  242. .Add(x => x.TaxCode.Code)
  243. );
  244. query.Query();
  245. _styles = query.Get<ProductStyle>().ToObjects<ProductStyle>().ToList();
  246. _products = query.Get<Product>().ToObjects<Product>().ToList();
  247. _missingFinishes.AddRange(_productStyles.Where(c => !string.IsNullOrWhiteSpace(c) && !_styles.Any(p => string.Equals(p.Code, c))));
  248. var _missingCodes = _productcodes.Where(c => !_products.Any(p => string.Equals(p.Code, c))).ToArray();
  249. _missingProfiles.AddRange(_profiles.Where(x => _missingCodes.Contains(x.Code)));
  250. _missingComponents.AddRange(_components.Where(x => _missingCodes.Contains(x.Code)));
  251. _missingGlass.AddRange(_glass.Where(x => _missingCodes.Contains(x.Code)));
  252. });
  253. if (!string.IsNullOrWhiteSpace(exception))
  254. {
  255. MessageWindow.ShowMessage(exception,"V6 Error",PRSDesktop.Resources.warning.AsBitmapImage());
  256. return;
  257. }
  258. if (_missingProfiles.Any() || _missingComponents.Any() || _missingGlass.Any())
  259. {
  260. if (!MessageWindow.ShowYesNo(
  261. $"The following products do not exist in PRS\n" +
  262. $"- {string.Join("\n- ", _missingProfiles.Select(x => x.Code)
  263. .Union(_missingComponents.Select(x => x.Code))
  264. .Union(_missingGlass.Select(x => x.Code))
  265. .Distinct().OrderBy(x => x))}\n\n" +
  266. $"Do you wish to create them now?",
  267. "Create Missing Products"))
  268. return;
  269. }
  270. if (_missingFinishes.Any())
  271. {
  272. if (!MessageWindow.ShowYesNo(
  273. $"The following styles do not exist in PRS\n" +
  274. $"- {string.Join("\n- ", _missingFinishes
  275. .Distinct().OrderBy(x => x))}\n\n" +
  276. $"Do you wish to create them now?",
  277. "Create Missing Styles"))
  278. return;
  279. }
  280. Progress.ShowModal("Checking Labour", progress =>
  281. {
  282. progress.Report("Loading Labour");
  283. _labour = _client.GetLabour(_project.Number, _project.Variation);
  284. var _labourcodes = _labour.Select(x => x.Code).Distinct().ToArray();
  285. _activities = Client.Query(
  286. new Filter<Activity>(x => x.Code).InList(_labourcodes),
  287. Columns.None<Activity>()
  288. .Add(x => x.ID)
  289. .Add(x => x.Code)
  290. .Add(x => x.Description)
  291. ).ToObjects<Activity>().ToList();
  292. var _missingCodes = _labourcodes.Where(l => !_activities.Any(a => string.Equals(a.Code, l))).ToArray();
  293. _missingLabour.AddRange(_labour.Where(x => _missingCodes.Contains(x.Code)));
  294. });
  295. if (_missingLabour.Any())
  296. {
  297. if (!MessageWindow.ShowOKCancel(
  298. $"The following products do not exist in PRS\n" +
  299. $"- {string.Join("\n- ", _missingLabour.Select(x => x.Code).Distinct().OrderBy(x => x))}\n\n" +
  300. $"Do you wish to create them?",
  301. "Create Missing Activities"))
  302. return;
  303. }
  304. }
  305. List<String> designExceptions = new();
  306. if (_importDesigns != V6ImportDesigns.None)
  307. {
  308. List<V6Elevation> _designlist = new();
  309. Progress.ShowModal("Checking Designs", progress =>
  310. {
  311. try
  312. {
  313. _designlist = _client.GetItems(_project.Number, _project.Variation);
  314. }
  315. catch (Exception _exception)
  316. {
  317. designExceptions.Add($"Error retrieving designs : {_exception.Message}");
  318. return;
  319. }
  320. foreach (var _design in _designlist)
  321. {
  322. try
  323. {
  324. _designs[_design] = new V6Drawings();
  325. }
  326. catch (Exception _exception)
  327. {
  328. designExceptions.Add($"Error retrieving design [{_design.Description}]: {_exception.Message}");
  329. }
  330. }
  331. });
  332. }
  333. if (designExceptions.Any())
  334. {
  335. MessageWindow.ShowMessage(string.Join("\n",designExceptions),"V6 Error",PRSDesktop.Resources.warning.AsBitmapImage());
  336. return;
  337. }
  338. string createException = "";
  339. Progress.ShowModal("Creating Job", progress =>
  340. {
  341. try
  342. {
  343. var _scope = CreateJob(_project, _statusCode, _taxCode);
  344. if (_importCosts != V6ImportCosts.None)
  345. {
  346. CreateMissingStyles(_missingFinishes, _styles);
  347. CreateMissingProducts<V6Profile>(_profileUom, _taxCode, _missingProfiles, _products);
  348. CreateMissingProducts<V6Component>(_componentUom, _taxCode, _missingComponents, _products);
  349. CreateMissingProducts<V6Glass>(_glassUom, _taxCode, _missingGlass, _products);
  350. CreateMissingLabour(_missingLabour, _activities);
  351. progress.Report("Creating Bill of Materials");
  352. var bom = CreateBillofMaterials(_project, _scope,
  353. _profiles, _profileUom,
  354. _components, _componentUom,
  355. _glass, _glassUom,
  356. _products, _styles);
  357. if (_importCosts == V6ImportCosts.Requisitions)
  358. {
  359. // Convert BOM to Requisition
  360. }
  361. progress.Report("Creating Labour Budget");
  362. CreateActivities(_project, _scope, _labour, _activities);
  363. }
  364. if (_importDesigns != V6ImportDesigns.None)
  365. {
  366. progress.Report("Loading Drawings");
  367. foreach (var _key in _designs.Keys)
  368. {
  369. progress.Report($"Loading Drawing: {_key.Description}");
  370. _designs[_key] = _client.GetDrawings(_key.ID);
  371. }
  372. if (_importDesigns == V6ImportDesigns.ForApproval)
  373. CreateStagedSetouts(_project, _scope, _designs, _template, _stages);
  374. else
  375. CreateManufacturingPackets(_project, _scope, _designs, _template, _stages);
  376. }
  377. }
  378. catch (Exception _exception)
  379. {
  380. createException = $"Error Creating Job: {_exception.Message}\n{_exception.StackTrace}";
  381. }
  382. });
  383. if (!string.IsNullOrWhiteSpace(createException))
  384. {
  385. MessageWindow.ShowMessage(createException,"PRS Error",PRSDesktop.Resources.warning.AsBitmapImage());
  386. return;
  387. }
  388. List<String> _missing = new();
  389. _missing.AddRange(_missingLabour.Select(x => $"- Activity {x.Code}: {x.Description}").Distinct().OrderBy(x => x));
  390. _missing.AddRange(_missingFinishes.Select(x => $"- Style {x}").Distinct().OrderBy(x => x));
  391. _missing.AddRange(_missingProfiles.Select(x => $"- Product {x.Code}: {x.Description}")
  392. .Union(_missingComponents.Select(x => $"- Product {x.Code}: {x.Description}"))
  393. .Union(_missingGlass.Select(x => $"- Product {x.Code}: {x.Description}"))
  394. .Distinct()
  395. .OrderBy(x => x));
  396. if (_missing.Any())
  397. MessageWindow.ShowMessage($"The following items were auto-created and should be manually checked:\n{String.Join("\n",_missing)}","Results");
  398. DialogResult = true;
  399. }
  400. private static void CreateMissingLabour(List<V6Labour> missinglabour, List<Activity> activitylist)
  401. {
  402. List<Activity> _updates = new();
  403. foreach (var _missing in missinglabour)
  404. {
  405. if (!_updates.Any(x => String.Equals(x.Code, _missing.Code)))
  406. {
  407. var _activity = new Activity();
  408. _activity.Code = _missing.Code;
  409. _activity.Description = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(_missing.Code.ToLower());
  410. _activity.Problem.Notes = new string[] { "Created by V6 Import" };
  411. _updates.Add(_activity);
  412. }
  413. }
  414. Client.Save(_updates, "Created by V6 Import");
  415. activitylist.AddRange(_updates);
  416. }
  417. private static void CreateMissingStyles( List<string> missingitems, List<ProductStyle> styleList)
  418. {
  419. List<ProductStyle> _updates = new();
  420. foreach (var _missingitem in missingitems)
  421. {
  422. if (!_updates.Any(x => String.Equals(x.Code, _missingitem)))
  423. {
  424. var _productstyle = new ProductStyle();
  425. _productstyle.Code = _missingitem;
  426. _productstyle.Description = _missingitem;
  427. _productstyle.Problem.Notes = new string[] { "Created by V6 Import" };
  428. _updates.Add(_productstyle);
  429. }
  430. }
  431. Client.Save(_updates, "Created by V6 Import");
  432. styleList.AddRange(_updates);
  433. }
  434. private static void CreateMissingProducts<T>(ProductDimensionUnit uom, TaxCode tax, List<T> missingitems, List<Product> productlist) where T : V6BOMItem
  435. {
  436. List<Product> _updates = new();
  437. foreach (var _missingitem in missingitems)
  438. {
  439. if (!_updates.Any(x => String.Equals(x.Code, _missingitem.Code)))
  440. {
  441. var _product = new Product();
  442. _product.UnitOfMeasure.CopyFrom(uom);
  443. _product.Code = _missingitem.Code;
  444. _product.Name = _missingitem.Description;
  445. _product.Problem.Notes = new string[] { "Created by V6 Import" };
  446. _product.TaxCode.CopyFrom(tax);
  447. _updates.Add(_product);
  448. }
  449. }
  450. Client.Save(_updates, "Created by V6 Import");
  451. productlist.AddRange(_updates);
  452. }
  453. private static JobScope CreateJob(V6Quote quote, JobStatus status, TaxCode tax)
  454. {
  455. var _jobno = $"V{quote.Number}";
  456. MultiQuery query = new MultiQuery();
  457. query.Add(
  458. new Filter<Job>(x=>x.JobNumber).IsEqualTo(_jobno),
  459. Columns.Required<Job>().Add(x=>x.DefaultScope.ID)
  460. );
  461. if (!string.IsNullOrWhiteSpace(quote.ClientID))
  462. {
  463. query.Add(
  464. new Filter<Customer>(x => x.Code).IsEqualTo(quote.ClientID),
  465. Columns.Required<Customer>()
  466. );
  467. }
  468. query.Query();
  469. var _scope = new JobScope();
  470. var _job = query.Get<Job>().ToObjects<Job>().FirstOrDefault();
  471. var _customer = !string.IsNullOrWhiteSpace(quote.ClientID)
  472. ? query.Get<Customer>().ToObjects<Customer>().FirstOrDefault()
  473. : null;
  474. if (_job == null)
  475. {
  476. if (!string.IsNullOrWhiteSpace(quote.ClientID) && _customer == null)
  477. {
  478. _customer = new Customer();
  479. _customer.Code = quote.ClientID;
  480. _customer.Name = quote.ClientName;
  481. _customer.Delivery.Street = quote.Street;
  482. _customer.Delivery.City = quote.City;
  483. _customer.Delivery.State = quote.State;
  484. _customer.Delivery.PostCode = quote.PostCode;
  485. Client.Save(_customer,"Imported From V6");
  486. }
  487. _job = new Job();
  488. _job.JobNumber = _jobno;
  489. _job.Name = quote.Title;
  490. _job.JobStatus.CopyFrom(status);
  491. if (_customer != null)
  492. {
  493. _job.Customer.CopyFrom(_customer);
  494. _job.Account.CopyFrom(_customer);
  495. }
  496. _job.SiteAddress.Street = quote.Street;
  497. _job.SiteAddress.City = quote.City;
  498. _job.SiteAddress.State = quote.State;
  499. _job.SiteAddress.PostCode = quote.PostCode;
  500. Client.Save(_job,"Imported From V6");
  501. _scope.ID = _job.DefaultScope.ID;
  502. _scope.CommitChanges();
  503. }
  504. _scope.Job.ID = _job.ID;
  505. _scope.Number = quote.Variation;
  506. _scope.SourceRef = $"{quote.ID}.{quote.Revision}";
  507. _scope.Description = string.IsNullOrWhiteSpace(quote.Variation)
  508. ? "Main Job"
  509. : quote.Title;
  510. _scope.ExTax = quote.SellPrice;
  511. _scope.TaxCode.CopyFrom(tax);
  512. _scope.Type = string.IsNullOrWhiteSpace(quote.Variation)
  513. ? JobScopeType.Contract
  514. : JobScopeType.Variation;
  515. Client.Save(_scope, "Imported From V6");
  516. return _scope;
  517. }
  518. private JobBillOfMaterials CreateBillofMaterials(V6Quote quote,
  519. JobScope scope,
  520. List<V6Profile> profiles, ProductDimensionUnit profileUOM,
  521. List<V6Component> components, ProductDimensionUnit componentUOM,
  522. List<V6Glass> glass, ProductDimensionUnit glassUOM,
  523. List<Product> _products, List<ProductStyle> _styles)
  524. {
  525. var _bom = new JobBillOfMaterials();
  526. _bom.Job.ID = scope.Job.ID;
  527. _bom.Description = string.IsNullOrWhiteSpace(quote.Variation)
  528. ? "Main Job"
  529. : $"{quote.Variation}";
  530. Client.Save(_bom,"Imported From V6");
  531. List<JobBillOfMaterialsItem> _bomitems = new();
  532. foreach (var _profile in profiles)
  533. {
  534. var _bomitem = new JobBillOfMaterialsItem();
  535. _bomitem.BillOfMaterials.ID = _bom.ID;
  536. _bomitem.Job.ID = scope.Job.ID;
  537. _bomitem.Scope.ID = scope.ID;
  538. if (_products.FirstOrDefault(x => x.Code == _profile.Code) is { } _p)
  539. {
  540. _bomitem.Product.CopyFrom(_p);
  541. _bomitem.Dimensions.Unit.CopyFrom(profileUOM);
  542. _bomitem.Dimensions.Length = _profile.Length;
  543. }
  544. else
  545. _bomitem.Problem.Notes = new string[] { $"Unable to Locate Product: {_profile.Quantity} x {_profile.Code}: {_profile.Description} ({_profile.Length})" };
  546. if (!string.IsNullOrWhiteSpace(_profile.Finish))
  547. {
  548. if (_styles.FirstOrDefault(x => x.Code == _profile.Finish) is { } _s)
  549. _bomitem.Style.CopyFrom(_s);
  550. else
  551. _bomitem.Problem.Notes = new string[] { $"Unable to Locate Style: {_profile.Finish}" };
  552. }
  553. _bomitem.Quantity = _profile.Quantity;
  554. _bomitem.UnitCost = _profile.Cost;
  555. _bomitems.Add(_bomitem);
  556. }
  557. foreach (var _component in components)
  558. {
  559. var _bomitem = new JobBillOfMaterialsItem();
  560. _bomitem.BillOfMaterials.ID = _bom.ID;
  561. _bomitem.Job.ID = scope.Job.ID;
  562. _bomitem.Scope.ID = scope.ID;
  563. if (_products.FirstOrDefault(x => x.Code == _component.Code) is { } _s)
  564. {
  565. _bomitem.Product.CopyFrom(_s);
  566. _bomitem.Dimensions.Unit.CopyFrom(componentUOM);
  567. _bomitem.Dimensions.Quantity = 1;
  568. _bomitems.Add(_bomitem);
  569. }
  570. else
  571. _bomitem.Problem.Notes = new string[] { $"Unable to Locate Product: {_component.Quantity} x {_component.Code}: {_component.Description} ({_component.PackSize})" };
  572. _bomitem.Quantity = _component.Quantity * _component.PackSize;
  573. _bomitem.UnitCost = _component.Cost;
  574. }
  575. foreach (var _glass in glass)
  576. {
  577. var _bomitem = new JobBillOfMaterialsItem();
  578. _bomitem.BillOfMaterials.ID = _bom.ID;
  579. _bomitem.Job.ID = scope.Job.ID;
  580. _bomitem.Scope.ID = scope.ID;
  581. if (_products.FirstOrDefault(x => x.Code == _glass.Code) is { } _p)
  582. {
  583. _bomitem.Product.CopyFrom(_p);
  584. _bomitem.Dimensions.Unit.CopyFrom(glassUOM);
  585. _bomitem.Dimensions.Height = _glass.Height * 25.4;
  586. _bomitem.Dimensions.Width = _glass.Width * 25.4;
  587. _bomitems.Add(_bomitem);
  588. }
  589. else
  590. _bomitem.Problem.Notes = new string[] {
  591. $"Unable to Locate Product: {_glass.Code}: {_glass.Description} ({_glass.Height} x {_glass.Width})" };
  592. if (!string.IsNullOrWhiteSpace(_glass.Treatment))
  593. {
  594. if (_styles.FirstOrDefault(x => x.Code == _glass.Treatment) is { } _s)
  595. _bomitem.Style.CopyFrom(_s);
  596. else
  597. _bomitem.Problem.Notes = new string[] { $"Unable to Locate Style: {_glass.Treatment})" };
  598. }
  599. _bomitem.Quantity = _glass.Quantity;
  600. _bomitem.UnitCost = _glass.Cost;
  601. }
  602. Client.Save(_bomitems,"Imported From V6");
  603. return _bom;
  604. }
  605. private void CreateActivities(V6Quote project, JobScope scope, List<V6Labour> labour, List<Activity> activities)
  606. {
  607. var _jobactivities = Client.Query(
  608. new Filter<JobActivity>(x => x.JobLink.ID).IsEqualTo(scope.Job.ID),
  609. Columns.Required<JobActivity>()
  610. .Add(x => x.JobLink.ID)
  611. .Add(x => x.ActivityLink.ID)
  612. .Add(x => x.Budget)
  613. ).ToObjects<JobActivity>().ToList();
  614. List<JobActivity> _updates = new List<JobActivity>();
  615. foreach (var _labour in labour)
  616. {
  617. var _activity = activities.FirstOrDefault(x => string.Equals(x.Code, _labour.Code)) ?? new Activity();
  618. var _jobactivity = _jobactivities.FirstOrDefault(x => x.ActivityLink.ID == _activity.ID) ?? new JobActivity();
  619. _jobactivity.JobLink.ID = scope.Job.ID;
  620. _jobactivity.ActivityLink.CopyFrom(_activity);
  621. _jobactivity.Budget += TimeSpan.FromMinutes(_labour.Minutes);
  622. _updates.Add(_jobactivity);
  623. }
  624. Client.Save(_updates,"Imported From V6");
  625. }
  626. private void CreateManufacturingPackets(V6Quote project, JobScope scope, Dictionary<V6Elevation, V6Drawings> designs, ManufacturingTemplate template, ManufacturingTemplateStage[] stages)
  627. {
  628. foreach (var _design in designs)
  629. {
  630. var _setout = new Setout();
  631. _setout.JobLink.ID = scope.Job.ID;
  632. _setout.Description = _design.Key.Description;
  633. _setout.Number = _design.Key.Description;
  634. Client.Save(_setout,"Imported From V6");
  635. var _drawings = _client.DecodeDrawings(_design.Value.Drawings, new string[] { "FrameDrawing" });
  636. List<Document> _documents = new();
  637. foreach (var _drawing in _drawings)
  638. {
  639. var _document = new Document();
  640. _document.FileName = System.IO.Path.ChangeExtension(_drawing.FileName, ".pdf");
  641. _document.Data = ImageUtils.BitmapToPdf(_drawing.Data);
  642. _documents.Add(_document);
  643. }
  644. if (_documents.Any())
  645. Client.Save(_documents, "Imported From V6");
  646. List<SetoutDocument> _setoutdocuments = new();
  647. foreach (var _document in _documents)
  648. {
  649. var _setoutdocument = new SetoutDocument();
  650. _setoutdocument.EntityLink.CopyFrom(_setout);
  651. _setoutdocument.DocumentLink.CopyFrom(_document);
  652. _setoutdocument.Thumbnail = ImageUtils.GetPDFThumbnail(_document.Data, 256, 256);
  653. _setoutdocuments.Add(_setoutdocument);
  654. }
  655. if (_setoutdocuments.Any())
  656. Client.Save(_setoutdocuments, "Imported From V6");
  657. var _packet = new ManufacturingPacket();
  658. _packet.SetoutLink.ID = _setout.ID;
  659. _packet.ManufacturingTemplateLink.CopyFrom(template);
  660. _packet.Title = _setout.Description;
  661. _packet.Quantity = _design.Key.Quantity;
  662. Client.Save(_packet,"Imported From V6");
  663. List<ManufacturingPacketStage> _packetstages = new();
  664. foreach (var _templatestage in stages)
  665. {
  666. var _packetstage = new ManufacturingPacketStage
  667. {
  668. Time = _templatestage.Time,
  669. SequenceType = _templatestage.SequenceType,
  670. Sequence = _templatestage.Sequence
  671. };
  672. _packetstage.Parent.ID = _packet.ID;
  673. _packetstage.ManufacturingSectionLink.ID = _templatestage.Section.ID;
  674. _packetstage.ManufacturingSectionLink.Name = _templatestage.Section.Name;
  675. _packetstages.Add(_packetstage);
  676. }
  677. if (_packetstages.Any())
  678. Client.Save(_packetstages,"Imported from V6");
  679. }
  680. }
  681. private void CreateStagedSetouts(V6Quote project, JobScope scope, Dictionary<V6Elevation, V6Drawings> designs, ManufacturingTemplate template, ManufacturingTemplateStage[] stages)
  682. {
  683. foreach (var _design in designs)
  684. {
  685. var _setout = new StagingSetout();
  686. _setout.JobLink.ID = scope.Job.ID;
  687. _setout.Number = _design.Key.Description;
  688. Client.Save(_setout,"Imported From V6");
  689. var _drawings = _client.DecodeDrawings(_design.Value.Drawings, new string[] { "FrameDrawing" });
  690. List<Document> _documents = new();
  691. foreach (var _drawing in _drawings)
  692. {
  693. var _document = new Document();
  694. _document.FileName = System.IO.Path.ChangeExtension(_drawing.FileName, ".pdf");
  695. _document.Data = ImageUtils.BitmapToPdf(_drawing.Data);
  696. _documents.Add(_document);
  697. }
  698. if (_documents.Any())
  699. Client.Save(_documents, "Imported From V6");
  700. List<StagingSetoutDocument> _setoutdocuments = new();
  701. foreach (var _document in _documents)
  702. {
  703. var _setoutdocument = new StagingSetoutDocument();
  704. _setoutdocument.EntityLink.CopyFrom(_setout);
  705. _setoutdocument.DocumentLink.CopyFrom(_document);
  706. _setoutdocument.Thumbnail = ImageUtils.GetPDFThumbnail(_document.Data, 256, 256);
  707. _setoutdocuments.Add(_setoutdocument);
  708. }
  709. if (_setoutdocuments.Any())
  710. Client.Save(_setoutdocuments, "Imported From V6");
  711. var _packet = new StagingManufacturingPacket();
  712. _packet.StagingSetout.ID = _setout.ID;
  713. _packet.Template.CopyFrom(template);
  714. _packet.Title = _setout.Number;
  715. _packet.Quantity = _design.Key.Quantity;
  716. Client.Save(_packet,"Imported From V6");
  717. List<StagingManufacturingPacketStage> _packetstages = new();
  718. foreach (var _templatestage in stages)
  719. {
  720. var _packetstage = new StagingManufacturingPacketStage
  721. {
  722. Time = _templatestage.Time,
  723. SequenceType = _templatestage.SequenceType,
  724. Sequence = _templatestage.Sequence
  725. };
  726. _packetstage.Packet.ID = _packet.ID;
  727. _packetstage.Section.ID = _templatestage.Section.ID;
  728. _packetstage.Section.Name = _templatestage.Section.Name;
  729. _packetstages.Add(_packetstage);
  730. }
  731. if (_packetstages.Any())
  732. Client.Save(_packetstages,"Imported from V6");
  733. }
  734. }
  735. private void Cancel_Click(object sender, RoutedEventArgs e)
  736. {
  737. DialogResult = false;
  738. }
  739. private void Projects_OnOnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  740. {
  741. Ok.IsEnabled = Projects.SelectedRows.Any();
  742. }
  743. }