JobScopeGrid.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Media.Imaging;
  6. using Comal.Classes;
  7. using InABox.Clients;
  8. using InABox.Core;
  9. using InABox.DynamicGrid;
  10. using InABox.WPF;
  11. using InABox.Wpf;
  12. using System.Threading;
  13. using System.Windows.Controls;
  14. using Comal.Classes.SecurityDescriptors;
  15. using InABox.Configuration;
  16. using PRSDesktop.Integrations.V6;
  17. namespace PRSDesktop
  18. {
  19. internal class JobScopeGrid : DynamicDataGrid<JobScope>, IMasterDetailControl<Job,JobScope>
  20. {
  21. private readonly BitmapImage tick = PRSDesktop.Resources.tick.AsBitmapImage();
  22. public Job? Master { get; set; }
  23. public Filter<JobScope> MasterDetailFilter => (Master?.ID ?? Guid.Empty) != Guid.Empty
  24. ? new Filter<JobScope>(x => x.Job.ID).IsEqualTo(Master.ID)
  25. : new Filter<JobScope>().None();
  26. public JobScopeGrid()
  27. {
  28. HiddenColumns.Add(x=>x.Job.DefaultScope.ID);
  29. HiddenColumns.Add(x=>x.Job.ID);
  30. HiddenColumns.Add(x=>x.SourceRef);
  31. ActionColumns.Add(new DynamicImageColumn(IsDefaultImage, SelectDefaultAction) { Position = DynamicActionColumnPosition.End });
  32. }
  33. protected override void DoReconfigure(DynamicGridOptions options)
  34. {
  35. base.DoReconfigure(options);
  36. options.RecordCount = true;
  37. options.SelectColumns = true;
  38. options.FilterRows = true;
  39. }
  40. public override void DeleteItems(params CoreRow[] rows)
  41. {
  42. base.DeleteItems(rows);
  43. var row = rows.FirstOrDefault(x => x.Get<JobScope, Guid>(x => x.Job.DefaultScope.ID) == x.Get<JobScope, Guid>(x => x.ID));
  44. if (row is not null)
  45. {
  46. var job = Client.Query(
  47. new Filter<Job>(x => x.ID).IsEqualTo(row.Get<JobScope, Guid>(x => x.Job.ID)),
  48. Columns.Required<Job>().Add(x => x.ID).Add(x => x.DefaultScope.ID)
  49. ).Rows.FirstOrDefault()?.ToObject<Job>();
  50. if (job != null)
  51. {
  52. job.DefaultScope.ID = row.Get<JobScope, Guid>(x => x.ID);
  53. Client.Save(job, "Updated Default Scope");
  54. }
  55. }
  56. }
  57. private BitmapImage? IsDefaultImage(CoreRow? row)
  58. {
  59. return row == null
  60. ? tick
  61. : row.Get<JobScope, Guid>(x => x.Job.DefaultScope.ID) == row.Get<JobScope, Guid>(x => x.ID)
  62. ? tick
  63. : null;
  64. }
  65. private bool SelectDefaultAction(CoreRow? row)
  66. {
  67. if ((row == null) ||row.Get<JobScope, Guid>(x => x.Job.DefaultScope.ID) == row.Get<JobScope, Guid>(x => x.ID))
  68. return false;
  69. using (new WaitCursor())
  70. {
  71. var job = new Client<Job>().Query(
  72. new Filter<Job>(x => x.ID).IsEqualTo(row.Get<JobScope, Guid>(x => x.Job.ID)),
  73. Columns.Required<Job>().Add(x => x.ID).Add(x => x.DefaultScope.ID)
  74. ).Rows.FirstOrDefault()?.ToObject<Job>();
  75. if (job != null)
  76. {
  77. job.DefaultScope.ID = row.Get<JobScope, Guid>(x => x.ID);
  78. new Client<Job>().Save(job, "Updated Default Scope");
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84. protected override bool CanCreateItems()
  85. {
  86. return base.CanCreateItems() && (Master?.ID ?? Guid.Empty) != Guid.Empty;
  87. }
  88. public override JobScope CreateItem()
  89. {
  90. var result = base.CreateItem();
  91. result.Job.ID = Master?.ID ?? Guid.Empty;
  92. result.Job.Synchronise(Master ?? new Job());
  93. result.Type = Data.Rows.Any() ? JobScopeType.Variation : JobScopeType.Contract;
  94. return result;
  95. }
  96. private V6Settings? _v6Settings;
  97. protected override void DoAdd(bool openEditorOnDirectEdit = false)
  98. {
  99. ContextMenu? menu = null;
  100. if (Security.IsAllowed<ImportV6Projects>())
  101. {
  102. _v6Settings ??= new GlobalConfiguration<V6Settings>().Load();
  103. if (_v6Settings.ImportProjects == V6ProjectType.Project)
  104. {
  105. menu ??= new ContextMenu();
  106. var item = new MenuItem()
  107. {
  108. Header = "Import from V6",
  109. Icon = new Image() { Source = PRSDesktop.Resources.v6.AsBitmapImage() }
  110. };
  111. item.Click += ImportFromV6;
  112. menu.Items.Add(item);
  113. }
  114. }
  115. if (menu != null)
  116. {
  117. menu.Items.Insert(0,new Separator());
  118. var item = new MenuItem()
  119. {
  120. Header = "New Scope"
  121. };
  122. item.Click += (o, e) => base.DoAdd(openEditorOnDirectEdit);
  123. menu.Items.Insert(0,item);
  124. menu.IsOpen = true;
  125. }
  126. else
  127. base.DoAdd(openEditorOnDirectEdit);
  128. }
  129. private void ImportFromV6(object sender, RoutedEventArgs e)
  130. {
  131. var _client = new V6Client();
  132. if (!_client.Connect())
  133. {
  134. MessageBox.Show("Unable to connect to V6!");
  135. return;
  136. }
  137. var master = _client.GetProject(Master?.JobNumber,Master?.SourceRef);
  138. if (master == null)
  139. {
  140. MessageBox.Show("This is not a V6 project!");
  141. return;
  142. }
  143. var import = new V6VariationSelection(
  144. _client,
  145. master,
  146. (v) => FilterV6Variations(master,v),
  147. CreateV6Variation
  148. );
  149. if (import.ShowDialog() == true)
  150. Refresh(false,true);
  151. }
  152. private bool FilterV6Variations(V6Project master, V6Variation variation)
  153. {
  154. // Exclude previously imported variations
  155. return !Data.Rows.Any(r => string.Equals(variation.ID, r.Get<JobScope, string>(x => x.SourceRef)));
  156. }
  157. private bool CreateV6Variation(V6Variation variation)
  158. {
  159. _v6Settings ??= new GlobalConfiguration<V6Settings>().Load();
  160. JobScope scope = new JobScope();
  161. scope.Job.ID = Master?.ID ?? Guid.Empty;
  162. if (_v6Settings.UseV6QuoteNumber)
  163. scope.Number = variation.ID;
  164. scope.Description = variation.Description;
  165. scope.Type = JobScopeType.Variation;
  166. scope.SourceRef = variation.GetReference();
  167. Client.Save(scope, "Imported From V6");
  168. return true;
  169. }
  170. protected override void Reload(
  171. Filters<JobScope> criteria, Columns<JobScope> columns, ref SortOrder<JobScope>? sort,
  172. CancellationToken token, Action<CoreTable?, Exception?> action)
  173. {
  174. criteria.Add(MasterDetailFilter);
  175. base.Reload(criteria, columns, ref sort, token, action);
  176. }
  177. private DynamicGridTreeUIComponent<JobScope, Guid>? _uiComponent;
  178. private DynamicGridTreeUIComponent<JobScope, Guid> UIComponent
  179. {
  180. get
  181. {
  182. if(_uiComponent is null)
  183. {
  184. _uiComponent = new DynamicGridTreeUIComponent<JobScope, Guid>(
  185. x => x.ID,
  186. x => x.Parent.ID,
  187. Guid.Empty)
  188. {
  189. Parent = this,
  190. MaxRowHeight = 30,
  191. };
  192. //_uiComponent.OnContextMenuOpening += JobDocumentSetFolderTree_OnContextMenuOpening;
  193. }
  194. return _uiComponent;
  195. }
  196. }
  197. protected override IDynamicGridUIComponent<JobScope> CreateUIComponent()
  198. {
  199. return UIComponent;
  200. }
  201. }
  202. }