V6ProjectImport.xaml.cs 28 KB

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