| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Windows;
- using System.Windows.Media.Imaging;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.Wpf;
- using InABox.WPF;
- using Microsoft.Win32;
- namespace PRSDesktop
- {
- public class QuoteDocuments : DynamicDataGrid<QuoteDocument>, IMasterDetailControl<Quote,QuoteDocument>, IDataModelSource
- {
-
- public Quote? Master { get; set; }
- public Filter<QuoteDocument> MasterDetailFilter => Master != null && Master.ID != Guid.Empty
- ? new Filter<QuoteDocument>(x => x.EntityLink.ID).IsEqualTo(Master.ID)
- : new Filter<QuoteDocument>().None();
-
- public QuoteDocuments()
- {
- HiddenColumns.Add(x => x.DocumentLink.ID);
- HiddenColumns.Add(x => x.Superceded);
- HiddenColumns.Add(x => x.DocumentLink.FileName);
- ActionColumns.Add(new DynamicImageColumn(DocumentImage, ViewDocument) { Position = DynamicActionColumnPosition.Start });
- ActionColumns.Add(new DynamicImageColumn(SupercededImage, SupercedeDocument));
- }
- public event DataModelUpdateEvent? OnUpdateDataModel;
- public string SectionName => "Quote Documents";
- public DataModel DataModel(Selection selected)
- {
- var ids = ExtractValues(x => x.ID, selected).ToArray();
- return new BaseDataModel<QuoteDocument>(new Filter<QuoteDocument>(x => x.ID).InList(ids));
- }
- protected override void Reload(Filters<QuoteDocument> criteria, Columns<QuoteDocument> columns, ref SortOrder<QuoteDocument>? sort, Action<CoreTable?, Exception?> action)
- {
- criteria.Add(MasterDetailFilter);
- base.Reload(criteria, columns, ref sort, action);
- }
- private BitmapImage SupercededImage(CoreRow? row)
- {
- if (row == null)
- return PRSDesktop.Resources.tick.AsBitmapImage();
- if (row.Get<QuoteDocument, DateTime>(x => x.Superceded) != DateTime.MinValue)
- return PRSDesktop.Resources.warning.AsBitmapImage();
- return PRSDesktop.Resources.tick.AsBitmapImage();
- }
- private bool SupercedeDocument(CoreRow? row)
- {
- if (row == null)
- return false;
- var qdoc = row.ToObject<QuoteDocument>();
- qdoc.Superceded = qdoc.Superceded.IsEmpty() ? DateTime.Now : DateTime.MinValue;
- new Client<QuoteDocument>().Save(qdoc, "Updated Superceded Flag");
- return true;
- }
- private BitmapImage DocumentImage(CoreRow? arg)
- {
- return PRSDesktop.Resources.view.AsBitmapImage();
- }
- private bool ViewDocument(CoreRow? row)
- {
- if (row is null) return false;
- var filename = row.Get<QuoteDocument, string>(x => x.DocumentLink.FileName);
- if (Path.GetExtension(filename).ToUpper().Equals(".PDF"))
- {
- var viewer = new DocumentEditor(row.ToObject<QuoteDocument>());
- //viewer.PrintAllowed = true;
- viewer.SaveAllowed = true;
- viewer.ShowDialog();
- }
- else
- {
- var id = row.Get<QuoteDocument, Guid>(x => x.DocumentLink.ID);
- var docrow = new Client<Document>().Query(new Filter<Document>(x => x.ID).IsEqualTo(id)).Rows.FirstOrDefault();
- if (docrow != null)
- {
- var tmpfile = Path.ChangeExtension(Path.GetTempFileName(), Path.GetExtension(filename));
- File.WriteAllBytes(tmpfile, docrow.Get<Document, byte[]>(x => x.Data));
- var gsProcessInfo = new ProcessStartInfo();
- gsProcessInfo.Verb = "open";
- gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal;
- gsProcessInfo.FileName = tmpfile;
- gsProcessInfo.UseShellExecute = true;
- Process.Start(gsProcessInfo);
- }
- else
- {
- MessageBox.Show(string.Format("Unable to retrieve {0}!", filename));
- }
- }
- return false;
- }
- protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
- {
- var dlg = new OpenFileDialog();
- dlg.Multiselect = true;
- if (dlg.ShowDialog() == true)
- {
- using (new WaitCursor())
- {
- var docs = new List<Document>();
- foreach (var filename in dlg.FileNames)
- {
- // Create a Document
- var doc = new Document();
- doc.FileName = Path.GetFileName(filename).ToLower();
- doc.TimeStamp = new FileInfo(dlg.FileName).LastWriteTime;
- doc.Data = File.ReadAllBytes(filename);
- doc.CRC = CoreUtils.CalculateCRC(doc.Data);
- docs.Add(doc);
- }
- if (docs.Any())
- {
- new Client<Document>().Save(docs, "Initial Upload");
- foreach (var doc in docs)
- {
- var newitem = CreateItem();
- newitem.DocumentLink.ID = doc.ID;
- newitem.DocumentLink.Synchronise(doc);
- SaveItem(newitem);
- }
- }
- }
- Refresh(false, true);
- }
- }
- public override QuoteDocument CreateItem()
- {
- var result = base.CreateItem();
- result.EntityLink.ID = Master?.ID ?? Guid.Empty;
- result.EntityLink.Synchronise(Master ?? new Quote());
- return result;
- }
- }
- }
|