EquipmentGrid.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Media.Imaging;
  9. using Comal.Classes;
  10. using InABox.Clients;
  11. using InABox.Core;
  12. using InABox.DynamicGrid;
  13. using InABox.WPF;
  14. namespace PRSDesktop
  15. {
  16. public class EquipmentGrid : DynamicDataGrid<Equipment>
  17. {
  18. private readonly BitmapImage docs = PRSDesktop.Resources.doc_misc.AsBitmapImage();
  19. private readonly BitmapImage specification = PRSDesktop.Resources.doc_pdf.AsBitmapImage();
  20. public Guid CustomerID { get; set; }
  21. public Guid GroupID { get; set; }
  22. public EquipmentGrid()
  23. {
  24. CustomerID = Guid.Empty;
  25. GroupID = CoreUtils.FullGuid;
  26. }
  27. protected override void Init()
  28. {
  29. base.Init();
  30. HiddenColumns.Add(x => x.Customer.ID);
  31. HiddenColumns.Add(x => x.GroupLink.ID);
  32. ActionColumns.Add(new DynamicImageColumn(SpecificationImage, ShowSpecificationSheet) { Position = DynamicActionColumnPosition.Start });
  33. //HiddenColumns.Add(x => x.Specification.FileName);
  34. HiddenColumns.Add(x => x.SpecificationSheet.ID);
  35. ActionColumns.Add(new DynamicScheduleEditorColumn<Equipment>());
  36. HiddenColumns.Add(x => x.ActiveSchedules);
  37. ActionColumns.Add(new DynamicImageColumn(DocumentsImage, DocumentsClick));
  38. HiddenColumns.Add(x => x.Documents);
  39. ActionColumns.Add(new DynamicMapColumn<Equipment>(this, x => x.TrackerLink.Location));
  40. AddButton("Duplicate", PRSDesktop.Resources.copy.AsBitmapImage(), DuplicateEquipment);
  41. }
  42. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  43. {
  44. base.DoReconfigure(options);
  45. options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.SelectColumns, DynamicGridOption.FilterRows);
  46. }
  47. private bool DocumentsClick(CoreRow arg)
  48. {
  49. if (arg == null)
  50. return false;
  51. var docs = new List<IEntityDocument>();
  52. using (new WaitCursor())
  53. {
  54. var deliveryid = arg.Get<Delivery, Guid>(x => x.ID);
  55. var table = new Client<EquipmentDocument>().Query(
  56. new Filter<EquipmentDocument>(x => x.EntityLink.ID).IsEqualTo(deliveryid)
  57. );
  58. foreach (var row in table.Rows)
  59. docs.Add(row.ToObject<EquipmentDocument>());
  60. }
  61. if (docs.Any())
  62. {
  63. var editor = new DocumentEditor(docs.ToArray());
  64. //editor.PrintAllowed = true;
  65. editor.SaveAllowed = true;
  66. editor.ShowDialog();
  67. }
  68. else
  69. {
  70. MessageBox.Show("No Documents Available!");
  71. }
  72. return false;
  73. }
  74. private BitmapImage DocumentsImage(CoreRow arg)
  75. {
  76. if (arg == null)
  77. return docs;
  78. return arg.Get<Equipment, int>(x => x.Documents) > 0 ? docs : null;
  79. }
  80. private bool DuplicateEquipment(Button sender, CoreRow[] rows)
  81. {
  82. if (rows.Length != 1)
  83. {
  84. MessageBox.Show("Please select one equipment item to duplicate!");
  85. return false;
  86. }
  87. var row = rows.First();
  88. var res = MessageBox.Show("Do you wish to copy the attached documents along with this item?", "Copy Documents",
  89. MessageBoxButton.YesNoCancel);
  90. if (res == MessageBoxResult.Cancel)
  91. return false;
  92. Progress.Show("");
  93. var equipment = new Client<Equipment>().Load(new Filter<Equipment>(x => x.ID).IsEqualTo(row.Get<Equipment, Guid>(x => x.ID)))
  94. .FirstOrDefault();
  95. Progress.SetMessage("Duplicating Equipment");
  96. var id = equipment.ID;
  97. equipment.ID = Guid.Empty;
  98. equipment.Code = equipment.Code + " - COPY";
  99. equipment.ActiveSchedules = 0;
  100. equipment.CommitChanges();
  101. new Client<Equipment>().Save(equipment, "Equipment Item Duplicated");
  102. Progress.SetMessage("Duplicating Schedules");
  103. var schedules = new Client<Schedule>().Load(new Filter<Schedule>(x => x.DocumentID).IsEqualTo(id));
  104. foreach (var schedule in schedules)
  105. {
  106. schedule.ID = Guid.Empty;
  107. schedule.DocumentID = equipment.ID;
  108. schedule.CommitChanges();
  109. }
  110. new Client<Schedule>().Save(schedules, "Equipment Item Duplicated");
  111. if (res == MessageBoxResult.Yes)
  112. {
  113. Progress.SetMessage("Duplicating Document References");
  114. var docs = new Client<EquipmentDocument>().Load(new Filter<EquipmentDocument>(x => x.EntityLink.ID).IsEqualTo(id));
  115. foreach (var doc in docs)
  116. {
  117. doc.ID = Guid.Empty;
  118. doc.EntityLink.ID = equipment.ID;
  119. doc.CommitChanges();
  120. }
  121. new Client<EquipmentDocument>().Save(docs, "Equipment Item Duplicated");
  122. }
  123. Progress.Close();
  124. MessageBox.Show(string.Format("{0} created!", equipment.Code));
  125. return true;
  126. }
  127. protected override void Reload(Filters<Equipment> criteria, Columns<Equipment> columns, ref SortOrder<Equipment>? sort, Action<CoreTable?, Exception?> action)
  128. {
  129. if (GroupID != CoreUtils.FullGuid)
  130. criteria.Add(new Filter<Equipment>(x => x.GroupLink.ID).IsEqualTo(GroupID));
  131. if (CustomerID != CoreUtils.FullGuid)
  132. criteria.Add(new Filter<Equipment>(x => x.Customer.ID).IsEqualTo(CustomerID));
  133. base.Reload(criteria, columns, ref sort, action);
  134. }
  135. private bool ShowSpecificationSheet(CoreRow arg)
  136. {
  137. var id = Entity.EntityLinkID<Equipment, ImageDocumentLink>(x => x.SpecificationSheet, arg);
  138. //String file = arg.Get<Equipment, String>(x => x.Specification.FileName);
  139. if (id != null)
  140. return false;
  141. var doc = new Client<Document>().Load(new Filter<Document>(x => x.ID).IsEqualTo(id)).FirstOrDefault();
  142. if (doc == null)
  143. {
  144. MessageBox.Show("Unable to Load Document");
  145. return false;
  146. }
  147. var ext = Path.GetExtension(doc.FileName);
  148. var tmpfile = Path.ChangeExtension(Path.GetTempFileName(), ext);
  149. File.WriteAllBytes(tmpfile, doc.Data);
  150. ProcessStartInfo gsProcessInfo = new ProcessStartInfo();
  151. gsProcessInfo.Verb = "open";
  152. gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal;
  153. gsProcessInfo.FileName = tmpfile;
  154. gsProcessInfo.UseShellExecute = true;
  155. Process.Start(gsProcessInfo);
  156. return false;
  157. }
  158. private BitmapImage? SpecificationImage(CoreRow arg)
  159. {
  160. if (arg == null)
  161. return specification;
  162. var id = Entity.EntityLinkID<Equipment, ImageDocumentLink>(x => x.SpecificationSheet, arg);
  163. return id == null ? null : specification;
  164. }
  165. public override bool EditItems(Equipment[] items, Func<Type, CoreTable>? PageDataHandler = null, bool PreloadPages = false)
  166. {
  167. var result = base.EditItems(items, PageDataHandler, PreloadPages);
  168. if (result)
  169. {
  170. var changedcustomers = items.Any(x => x.Customer.ID != CustomerID);
  171. var changedgroups = GroupID != CoreUtils.FullGuid
  172. ? items.Cast<Equipment>().Any(x => x.GroupLink.ID != GroupID)
  173. : false;
  174. if (changedcustomers || changedgroups)
  175. {
  176. Refresh(false, true);
  177. return false;
  178. }
  179. }
  180. return result;
  181. }
  182. protected override BaseEditor? GetEditor(object item, DynamicGridColumn column)
  183. {
  184. if (String.Equals(column.ColumnName, CoreUtils.GetFullPropertyName<Equipment, Guid>(x => x.Customer.ID, ".")))
  185. {
  186. if (CustomerID == CoreUtils.FullGuid)
  187. return new NullEditor();
  188. }
  189. return base.GetEditor(item, column);
  190. }
  191. }
  192. }