LogikalBillOfMaterialsImport.xaml.cs 4.4 KB

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