LogikalBillOfMaterialsImport.xaml.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Windows;
  5. using Comal.Classes;
  6. using InABox.Clients;
  7. using InABox.Core;
  8. using InABox.DynamicGrid;
  9. using InABox.Logikal;
  10. using InABox.Wpf;
  11. using InABox.WPF;
  12. using Microsoft.CodeAnalysis;
  13. using PRSDesktop.Utils.LogikalUtils;
  14. namespace PRSDesktop;
  15. public partial class LogikalBillOfMaterialsImport : Window
  16. {
  17. private void UpdateStatus(LogikalResponse response)
  18. {
  19. Status.Content = response.ToString();
  20. }
  21. private LogikalClient GetClient(object sender) => client;
  22. private LogikalClient client;
  23. private Job _job;
  24. public LogikalBillOfMaterialsImport(Job job)
  25. {
  26. client = new LogikalClient();
  27. InitializeComponent();
  28. _job = job;
  29. Projects.Refresh(true, false);
  30. Phases.Refresh(true, false);
  31. Elevations.Refresh(true, false);
  32. }
  33. private void Window_Loaded(object sender, RoutedEventArgs e)
  34. {
  35. if (LogikalCommon.CheckSettings(client.Settings))
  36. {
  37. if (client.Settings.ImportJobs && Guid.TryParse(_job.SourceRef, out Guid _projectid))
  38. {
  39. Phases.ProjectID = _projectid;
  40. Elevations.ProjectID = _projectid;
  41. Phases.Get();
  42. }
  43. else
  44. {
  45. Projects.JobNumber = _job.JobNumber;
  46. Projects.Get();
  47. }
  48. }
  49. else
  50. Close();
  51. }
  52. private void Projects_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  53. {
  54. var id = e.Rows?.FirstOrDefault()?.Get<LogikalProject, Guid>(x => x.ID) ?? Guid.Empty;
  55. GetPhases(id);
  56. }
  57. private void GetPhases(Guid id)
  58. {
  59. Phases.ProjectID = id;
  60. Elevations.ProjectID = id;
  61. Phases.Get();
  62. }
  63. private void Phases_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  64. {
  65. var phase = e.Rows?.FirstOrDefault()?.Get<LogikalPhase, string>(x => x.ID) ?? "";
  66. GetElevations(phase);
  67. }
  68. private void GetElevations(string phase)
  69. {
  70. Elevations.Phase = phase;
  71. Elevations.Get();
  72. }
  73. private void Elevations_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  74. {
  75. var id = e.Rows?.FirstOrDefault()?.Get<LogikalElevation, Guid>(x => x.ID) ?? Guid.Empty;
  76. OK.IsEnabled = id != Guid.Empty;
  77. }
  78. private void Projects_AfterRefresh(object sender, AfterRefreshEventArgs args)
  79. {
  80. Projects.Visibility = Projects.Items.Count > 1
  81. ? Visibility.Visible
  82. : Visibility.Collapsed;
  83. GetPhases(Projects.Items.FirstOrDefault()?.ID ?? Guid.Empty);
  84. }
  85. private void Phases_AfterRefresh(object sender, AfterRefreshEventArgs args)
  86. {
  87. Phases.Visibility = Phases.Items.Count > 1
  88. ? Visibility.Visible
  89. : Visibility.Collapsed;
  90. GetElevations(Phases.Items.FirstOrDefault()?.ID ?? "");
  91. }
  92. private void Cancel_Click(object sender, RoutedEventArgs e)
  93. {
  94. DialogResult = false;
  95. }
  96. private void OK_Click(object sender, RoutedEventArgs e)
  97. {
  98. bool close = false;
  99. // Create a BOM
  100. var ids = Elevations.SelectedRows.Select(r => r.Get<LogikalElevation, Guid>(c => c.ID)).ToArray();
  101. client.GetBillOfMaterials(Elevations.ProjectID, ids, true)
  102. .Always(UpdateStatus)
  103. .Error(error =>
  104. {
  105. MessageWindow.ShowMessage(error.Message,"Error");
  106. })
  107. .Success<LogikalBOMResponse<LogikalFinish,LogikalProfile,LogikalComponent,LogikalGlass,LogikalLabour>>(bom =>
  108. {
  109. var _fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "BillOfMaterials.xlsx");
  110. File.WriteAllBytes(_fileName, bom.ExcelData);
  111. if (CreateBOM(bom))
  112. close = true;
  113. });
  114. if (close)
  115. DialogResult = true;
  116. }
  117. private bool CreateBOM(LogikalBOMResponse<LogikalFinish,LogikalProfile, LogikalComponent, LogikalGlass, LogikalLabour> bom)
  118. {
  119. LogikalPartsManager manager = new LogikalPartsManager();
  120. if (!manager.CheckMissingItems(bom))
  121. return false;
  122. var _jobbom = new JobBillOfMaterials();
  123. _jobbom.Job.ID = _job.ID;
  124. _jobbom.Description = $"BOM Imported {DateTime.Now}";
  125. Client.Save(_jobbom, "Imported From Logikal");
  126. var _bomitems = manager.CreateItems<JobBillOfMaterialsItem>(
  127. _jobbom,
  128. bom,
  129. (logikalitem, bomitem) =>
  130. {
  131. bomitem.BillOfMaterials.ID = _jobbom.ID;
  132. bomitem.Quantity = logikalitem.Quantity;
  133. }
  134. );
  135. Client.Save(_bomitems, "Imported From Logikal");
  136. return true;
  137. }
  138. }