QuoteDocuments.cs 5.3 KB

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