QuoteDocuments.cs 6.0 KB

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