QuoteDocuments.cs 5.3 KB

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