ProjectsGrid.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using Comal.Classes;
  8. using Comal.Classes.SecurityDescriptors;
  9. using InABox.Clients;
  10. using InABox.Configuration;
  11. using InABox.Core;
  12. using InABox.DynamicGrid;
  13. using InABox.WPF;
  14. namespace PRSDesktop;
  15. public class JobGridSettings : IUserConfigurationSettings
  16. {
  17. [Obsolete]
  18. private CoreFilterDefinition? _currentFilter;
  19. [Obsolete]
  20. public CoreFilterDefinition? CurrentFilter
  21. {
  22. get => _currentFilter;
  23. set
  24. {
  25. if (value is not null)
  26. {
  27. Filters = new DynamicGridSelectedFilterSettings(new List<ICoreFilterDefinition> { value }, false, null);
  28. }
  29. }
  30. }
  31. public DynamicGridSelectedFilterSettings Filters { get; set; } = new();
  32. }
  33. public class ProjectsGrid : DynamicDataGrid<Job>
  34. {
  35. private Guid _statusid = Guid.Empty;
  36. private JobGridSettings _settings;
  37. public ProjectsGrid()
  38. {
  39. _settings = new UserConfiguration<JobGridSettings>().Load();
  40. FilterComponent.SetSettings(_settings.Filters, false);
  41. FilterComponent.OnFiltersSelected += FilterComponent_OnFilterSelected;
  42. HiddenColumns.Add(x => x.ID);
  43. HiddenColumns.Add(x => x.JobNumber);
  44. HiddenColumns.Add(x => x.Name);
  45. HiddenColumns.Add(x => x.Customer.ID);
  46. HiddenColumns.Add(x => x.Customer.Code);
  47. HiddenColumns.Add(x => x.Customer.Name);
  48. HiddenColumns.Add(x => x.Customer.Terms.ID);
  49. HiddenColumns.Add(x => x.Customer.Terms.Code);
  50. HiddenColumns.Add(x => x.Customer.Terms.Description);
  51. HiddenColumns.Add(x => x.Customer.Terms.Calculation);
  52. HiddenColumns.Add(x => x.SiteAddress.Street);
  53. HiddenColumns.Add(x => x.SiteAddress.City);
  54. HiddenColumns.Add(x => x.SiteAddress.State);
  55. HiddenColumns.Add(x => x.SiteAddress.PostCode);
  56. HiddenColumns.Add(x => x.Account.ID);
  57. HiddenColumns.Add(x => x.Account.Code);
  58. HiddenColumns.Add(x => x.Account.Name);
  59. HiddenColumns.Add(x => x.Account.Terms.ID);
  60. HiddenColumns.Add(x => x.Account.Terms.Code);
  61. HiddenColumns.Add(x => x.Account.Terms.Description);
  62. HiddenColumns.Add(x => x.Account.Terms.Calculation);
  63. HiddenColumns.Add(x => x.Notes);
  64. HiddenColumns.Add(x => x.JobStatus.ID);
  65. HiddenColumns.Add(x => x.JobStatus.Active);
  66. HiddenColumns.Add(x => x.DefaultScope.ID);
  67. HiddenColumns.Add(x => x.SourceRef);
  68. ActionColumns.Add(new DynamicMapColumn<Job>(this, x => x.SiteAddress.Location));
  69. //ActionColumns.Add(new DynamicMenuColumn(BuildMenu));
  70. }
  71. protected override void DoReconfigure(DynamicGridOptions options)
  72. {
  73. base.DoReconfigure(options);
  74. options.RecordCount = true;
  75. options.SelectColumns = true;
  76. options.FilterRows = true;
  77. }
  78. // private void BuildMenu(DynamicMenuColumn column, CoreRow? row)
  79. // {
  80. // if (Security.IsAllowed<CanCancelAllJobRequisitions>())
  81. // {
  82. // column.AddItem("Cancel all active requisitions", null, CancelJobRequisitions_Click);
  83. // }
  84. // if (Security.IsAllowed<CanReleaseJobReserves>())
  85. // {
  86. // column.AddItem("Release Job Reserves", null, ReleaseJobReserves_Click);
  87. // }
  88. // }
  89. //
  90. // private void CancelJobRequisitions_Click(CoreRow? obj)
  91. // {
  92. // var job = obj?.ToObject<Job>();
  93. // if (job is null)
  94. // {
  95. // MessageWindow.ShowMessage("Please select a job.", "No job selected");
  96. // return;
  97. // }
  98. //
  99. // if(MessageWindow.ShowYesNoCancel("Are you sure you wish to do this? This will cancel all requisitions for this job.", "Confirm") != MessageWindowResult.Yes)
  100. // {
  101. // MessageWindow.ShowMessage("No action taken.", "Process aborted");
  102. // return;
  103. // }
  104. //
  105. // var requisitionItems = Client.Query(
  106. // new Filter<JobRequisitionItem>(x => x.Requisition.Job.ID).IsEqualTo(job.ID)
  107. // .And(x => x.Status).IsNotEqualTo(JobRequisitionItemStatus.Cancelled)
  108. // .And(x => x.Status).IsNotEqualTo(JobRequisitionItemStatus.Issued)
  109. // .And(x => x.Status).IsNotEqualTo(JobRequisitionItemStatus.Archived),
  110. // new Columns<JobRequisitionItem>(x => x.ID)
  111. // .Add(x => x.Cancelled))
  112. // .ToList<JobRequisitionItem>();
  113. // foreach(var jri in requisitionItems)
  114. // {
  115. // jri.Cancelled = DateTime.Now;
  116. // }
  117. // Client.Save(requisitionItems, "Cancelled all job requisitions for job");
  118. // MessageWindow.ShowMessage("All job requisitions cancelled.", "Success");
  119. // }
  120. //
  121. // private void ReleaseJobReserves_Click(CoreRow? obj)
  122. // {
  123. // var job = obj?.ToObject<Job>();
  124. // if (job is null)
  125. // {
  126. // MessageWindow.ShowMessage("Please select a job.", "No job selected");
  127. // return;
  128. // }
  129. //
  130. // if(MessageWindow.ShowYesNoCancel("Are you sure you wish to do this? This will release all reserves for this job.", "Confirm") != MessageWindowResult.Yes)
  131. // {
  132. // MessageWindow.ShowMessage("No action taken.", "Process aborted");
  133. // return;
  134. // }
  135. //
  136. // var movements = Client.Query<StockMovement>(
  137. // new Filter<StockMovement>(x => x.Job.ID).IsEqualTo(job.ID),
  138. // new Columns<StockMovement>(x => x.Units)
  139. // .Add(x => x.Location.ID)
  140. // .Add(x => x.Product.ID)
  141. // .Add(x => x.Style.ID)
  142. // .AddDimensionsColumns(x => x.Dimensions)
  143. // .Add(x => x.Cost)
  144. // .Add(x => x.OrderItem.ID)
  145. // .Add(x => x.JobRequisitionItem.ID))
  146. // .ToObjects<StockMovement>()
  147. // .GroupBy(x => new
  148. // {
  149. // Location = x.Location.ID,
  150. // Product = x.Product.ID,
  151. // Style = x.Style.ID,
  152. // x.Dimensions,
  153. // x.Cost,
  154. // OrderItem = x.OrderItem.ID,
  155. // JobRequisitionItem = x.JobRequisitionItem.ID
  156. // })
  157. // .Select(x => new { x.Key, Units = x.Sum(x => x.Units) });
  158. //
  159. // var toSave = new List<StockMovement>();
  160. // foreach(var group in movements)
  161. // {
  162. // var from = new StockMovement();
  163. // from.Location.ID = group.Key.Location;
  164. // from.Style.ID = group.Key.Style;
  165. // from.Product.ID = group.Key.Product;
  166. // from.Dimensions.CopyFrom(group.Key.Dimensions);
  167. //
  168. // var to = from.CreateMovement();
  169. //
  170. // from.Job.ID = job.ID;
  171. //
  172. // from.Cost = group.Key.Cost;
  173. // to.Cost = group.Key.Cost;
  174. // from.OrderItem.ID = group.Key.OrderItem;
  175. // to.OrderItem.ID = group.Key.OrderItem;
  176. // from.JobRequisitionItem.ID = group.Key.JobRequisitionItem;
  177. // to.JobRequisitionItem.ID = group.Key.JobRequisitionItem;
  178. //
  179. // from.Type = StockMovementType.TransferOut;
  180. // to.Type = StockMovementType.TransferIn;
  181. // to.Transaction = from.Transaction;
  182. //
  183. // from.Units = -group.Units;
  184. // to.Units = group.Units;
  185. //
  186. // toSave.Add(from);
  187. // toSave.Add(to);
  188. // }
  189. // Client.Save(toSave, "Released all job stock.");
  190. // MessageWindow.ShowMessage("All job stock reserves released.", "Success");
  191. // }
  192. public Guid StatusID
  193. {
  194. get => _statusid;
  195. set
  196. {
  197. _statusid = value;
  198. }
  199. }
  200. protected override void Reload(
  201. Filters<Job> criteria, Columns<Job> columns, ref SortOrder<Job>? sort,
  202. CancellationToken token, Action<CoreTable?, Exception?> action)
  203. {
  204. if (_statusid != Guid.Empty)
  205. criteria.Add(new Filter<Job>(x => x.JobStatus.ID).IsEqualTo(_statusid));
  206. //criteria.Add(new Filter<Job>(x => x.JobType).IsEqualTo(JobType.Project));
  207. sort = new SortOrder<Job>(x => x.JobNumber, SortDirection.Descending);
  208. base.Reload(criteria, columns, ref sort, token, action);
  209. }
  210. public override DynamicEditorPages LoadEditorPages(Job item)
  211. {
  212. var pages = new DynamicEditorPages(new IDynamicEditorPage[]
  213. {
  214. new DynamicManyToManyGrid<JobDocument,Job>(),
  215. //new DynamicManyToManyGrid<JobStyle,Job>(),
  216. new DynamicOneToManyGrid<Job,JobDocumentSetTag>(),
  217. new JobFormDefinitionGrid(),
  218. //new DynamicOneToManyGrid<Job, JobProductMapping>(),
  219. });
  220. return pages;
  221. }
  222. private LogikalSettings? _logikalSettings;
  223. protected override void DoAdd(bool openEditorOnDirectEdit = false)
  224. {
  225. ContextMenu? menu = null;
  226. if (Security.IsAllowed<ImportV6Projects>())
  227. {
  228. menu ??= new ContextMenu();
  229. var item = new MenuItem()
  230. {
  231. Header = "Import from V6",
  232. Icon = new Image() { Source = PRSDesktop.Resources.v6.AsBitmapImage() }
  233. };
  234. item.Click += ImportFromV6;
  235. menu.Items.Add(item);
  236. }
  237. _logikalSettings ??= new GlobalConfiguration<LogikalSettings>().Load();
  238. if (_logikalSettings.ImportJobs && Security.IsAllowed<ImportLogikalProjects>())
  239. {
  240. menu ??= new ContextMenu();
  241. var item = new MenuItem()
  242. {
  243. Header = "Import from Logikal",
  244. Icon = new Image() { Source = PRSDesktop.Resources.logikal.AsBitmapImage() }
  245. };
  246. item.Click += ImportFromLogikal;
  247. menu.Items.Add(item);
  248. }
  249. if (menu != null)
  250. {
  251. menu.Items.Insert(0,new Separator());
  252. var item = new MenuItem()
  253. {
  254. Header = "New Project"
  255. };
  256. item.Click += (o, e) => base.DoAdd(openEditorOnDirectEdit);
  257. menu.Items.Insert(0,item);
  258. menu.IsOpen = true;
  259. }
  260. else
  261. base.DoAdd(openEditorOnDirectEdit);
  262. }
  263. private void ImportFromLogikal(object sender, RoutedEventArgs e)
  264. {
  265. var import = new LogikalProjectImport();
  266. if (import.ShowDialog() == true)
  267. Refresh(false,true);
  268. }
  269. private void ImportFromV6(object sender, RoutedEventArgs e)
  270. {
  271. var import = new V6ProjectImport();
  272. if (import.ShowDialog() == true)
  273. Refresh(false,true);
  274. }
  275. public override Job CreateItem()
  276. {
  277. var result = base.CreateItem();
  278. var defstatus = new Client<JobStatus>().Query(new Filter<JobStatus>(x => x.Default).IsEqualTo(true));
  279. if (defstatus.Rows.Any())
  280. result.JobStatus.ID = defstatus.Rows.First().Get<JobStatus, Guid>(x => x.ID);
  281. result.JobType = JobType.Project;
  282. return result;
  283. }
  284. protected override void CustomiseEditor(Job[] items, DynamicGridColumn column, BaseEditor editor)
  285. {
  286. base.CustomiseEditor(items, column, editor);
  287. if(new Column<Job>(x => x.Customer.ID).IsEqualTo(column.ColumnName) && editor is CodePopupEditor popup)
  288. {
  289. popup.CanAdd = Security.CanEdit<Customer>();
  290. }
  291. }
  292. protected override Dictionary<string, object?> EditorValueChanged(IDynamicEditorForm editor, Job[] items, string name, object value)
  293. {
  294. var result = base.EditorValueChanged(editor, items, name, value);
  295. if (name.Equals("Customer.ID"))
  296. {
  297. // false here because a customer has a defaultcontact
  298. // and we need to load the lookups before we set the default value
  299. var contact = editor.FindEditor("Contact.ID") as ILookupEditorControl;
  300. if (contact != null)
  301. DefineLookups(contact,items,false);
  302. }
  303. return result;
  304. }
  305. private void FilterComponent_OnFilterSelected(DynamicGridSelectedFilterSettings settings)
  306. {
  307. _settings.Filters = settings;
  308. new UserConfiguration<JobGridSettings>().Save(_settings);
  309. }
  310. }