EquipmentGrid.cs 8.2 KB

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