QuoteDocuments.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.Media.Imaging;
  8. using Comal.Classes;
  9. using InABox.Clients;
  10. using InABox.Core;
  11. using InABox.DynamicGrid;
  12. using InABox.Wpf;
  13. using InABox.WPF;
  14. using Microsoft.Win32;
  15. namespace PRSDesktop
  16. {
  17. public class QuoteDocuments : DynamicDataGrid<QuoteDocument>, IMasterDetailControl<Quote,QuoteDocument>, IDataModelSource
  18. {
  19. public Quote? Master { get; set; }
  20. public Filter<QuoteDocument> MasterDetailFilter => Master != null && Master.ID != Guid.Empty
  21. ? new Filter<QuoteDocument>(x => x.EntityLink.ID).IsEqualTo(Master.ID)
  22. : new Filter<QuoteDocument>().None();
  23. public QuoteDocuments()
  24. {
  25. HiddenColumns.Add(x => x.DocumentLink.ID);
  26. HiddenColumns.Add(x => x.Superceded);
  27. HiddenColumns.Add(x => x.DocumentLink.FileName);
  28. ActionColumns.Add(new DynamicImageColumn(DocumentImage, ViewDocument) { Position = DynamicActionColumnPosition.Start });
  29. ActionColumns.Add(new DynamicImageColumn(SupercededImage, SupercedeDocument));
  30. }
  31. public event DataModelUpdateEvent? OnUpdateDataModel;
  32. public string SectionName => "Quote Documents";
  33. public DataModel DataModel(Selection selected)
  34. {
  35. var ids = ExtractValues(x => x.ID, selected).ToArray();
  36. return new BaseDataModel<QuoteDocument>(new Filter<QuoteDocument>(x => x.ID).InList(ids));
  37. }
  38. protected override void Reload(Filters<QuoteDocument> criteria, Columns<QuoteDocument> columns, ref SortOrder<QuoteDocument>? sort, Action<CoreTable?, Exception?> action)
  39. {
  40. criteria.Add(MasterDetailFilter);
  41. base.Reload(criteria, columns, ref sort, action);
  42. }
  43. private BitmapImage SupercededImage(CoreRow? row)
  44. {
  45. if (row == null)
  46. return PRSDesktop.Resources.tick.AsBitmapImage();
  47. if (row.Get<QuoteDocument, DateTime>(x => x.Superceded) != DateTime.MinValue)
  48. return PRSDesktop.Resources.warning.AsBitmapImage();
  49. return PRSDesktop.Resources.tick.AsBitmapImage();
  50. }
  51. private bool SupercedeDocument(CoreRow? row)
  52. {
  53. if (row == null)
  54. return false;
  55. var qdoc = row.ToObject<QuoteDocument>();
  56. qdoc.Superceded = qdoc.Superceded.IsEmpty() ? DateTime.Now : DateTime.MinValue;
  57. new Client<QuoteDocument>().Save(qdoc, "Updated Superceded Flag");
  58. return true;
  59. }
  60. private BitmapImage DocumentImage(CoreRow? arg)
  61. {
  62. return PRSDesktop.Resources.view.AsBitmapImage();
  63. }
  64. private bool ViewDocument(CoreRow? row)
  65. {
  66. if (row is null) return false;
  67. var filename = row.Get<QuoteDocument, string>(x => x.DocumentLink.FileName);
  68. if (Path.GetExtension(filename).ToUpper().Equals(".PDF"))
  69. {
  70. var viewer = new DocumentEditor(row.ToObject<QuoteDocument>());
  71. //viewer.PrintAllowed = true;
  72. viewer.SaveAllowed = true;
  73. viewer.ShowDialog();
  74. }
  75. else
  76. {
  77. var id = row.Get<QuoteDocument, Guid>(x => x.DocumentLink.ID);
  78. var docrow = new Client<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(id)).Rows.FirstOrDefault();
  79. if (docrow != null)
  80. {
  81. var tmpfile = Path.ChangeExtension(Path.GetTempFileName(), Path.GetExtension(filename));
  82. File.WriteAllBytes(tmpfile, docrow.Get<Document, byte[]>(x => x.Data));
  83. var gsProcessInfo = new ProcessStartInfo();
  84. gsProcessInfo.Verb = "open";
  85. gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal;
  86. gsProcessInfo.FileName = tmpfile;
  87. gsProcessInfo.UseShellExecute = true;
  88. Process.Start(gsProcessInfo);
  89. }
  90. else
  91. {
  92. MessageBox.Show(string.Format("Unable to retrieve {0}!", filename));
  93. }
  94. }
  95. return false;
  96. }
  97. protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
  98. {
  99. var dlg = new OpenFileDialog();
  100. dlg.Multiselect = true;
  101. if (dlg.ShowDialog() == true)
  102. {
  103. using (new WaitCursor())
  104. {
  105. var docs = new List<Document>();
  106. foreach (var filename in dlg.FileNames)
  107. {
  108. // Create a Document
  109. var doc = new Document();
  110. doc.FileName = Path.GetFileName(filename).ToLower();
  111. doc.TimeStamp = new FileInfo(dlg.FileName).LastWriteTime;
  112. doc.Data = File.ReadAllBytes(filename);
  113. doc.CRC = CoreUtils.CalculateCRC(doc.Data);
  114. docs.Add(doc);
  115. }
  116. if (docs.Any())
  117. {
  118. new Client<Document>().Save(docs, "Initial Upload");
  119. foreach (var doc in docs)
  120. {
  121. var newitem = CreateItem();
  122. newitem.DocumentLink.ID = doc.ID;
  123. newitem.DocumentLink.Synchronise(doc);
  124. SaveItem(newitem);
  125. }
  126. }
  127. }
  128. Refresh(false, true);
  129. }
  130. }
  131. public override QuoteDocument CreateItem()
  132. {
  133. var result = base.CreateItem();
  134. result.EntityLink.ID = Master?.ID ?? Guid.Empty;
  135. result.EntityLink.Synchronise(Master ?? new Quote());
  136. return result;
  137. }
  138. }
  139. }