EquipmentGrid.cs 6.6 KB

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