EquipmentGrid.cs 8.5 KB

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