123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media.Imaging;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.WPF;
- namespace PRSDesktop
- {
- public class EquipmentGrid : DynamicDataGrid<Equipment>
- {
- private readonly BitmapImage docs = PRSDesktop.Resources.doc_misc.AsBitmapImage();
- private readonly BitmapImage specification = PRSDesktop.Resources.doc_pdf.AsBitmapImage();
-
- public Guid CustomerID { get; set; }
- public Guid GroupID { get; set; }
- public EquipmentGrid()
- {
- CustomerID = Guid.Empty;
- GroupID = CoreUtils.FullGuid;
- }
- protected override void Init()
- {
- base.Init();
- HiddenColumns.Add(x => x.Customer.ID);
- HiddenColumns.Add(x => x.GroupLink.ID);
- ActionColumns.Add(new DynamicImageColumn(SpecificationImage, ShowSpecificationSheet) { Position = DynamicActionColumnPosition.Start });
- //HiddenColumns.Add(x => x.Specification.FileName);
- HiddenColumns.Add(x => x.SpecificationSheet.ID);
- ActionColumns.Add(new DynamicScheduleEditorColumn<Equipment>());
- HiddenColumns.Add(x => x.ActiveSchedules);
- ActionColumns.Add(new DynamicImageColumn(DocumentsImage, DocumentsClick));
- HiddenColumns.Add(x => x.Documents);
- ActionColumns.Add(new DynamicMapColumn<Equipment>(this, x => x.TrackerLink.Location));
- AddButton("Duplicate", PRSDesktop.Resources.copy.AsBitmapImage(), DuplicateEquipment);
- }
- protected override void DoReconfigure(DynamicGridOptions options)
- {
- base.DoReconfigure(options);
- options.RecordCount = true;
- options.SelectColumns = true;
- options.FilterRows = true;
- }
- private bool DocumentsClick(CoreRow arg)
- {
- if (arg == null)
- return false;
- var docs = new List<IEntityDocument>();
- using (new WaitCursor())
- {
- var deliveryid = arg.Get<Delivery, Guid>(x => x.ID);
- var table = new Client<EquipmentDocument>().Query(
- new Filter<EquipmentDocument>(x => x.EntityLink.ID).IsEqualTo(deliveryid)
- );
- foreach (var row in table.Rows)
- docs.Add(row.ToObject<EquipmentDocument>());
- }
- if (docs.Any())
- {
- var editor = new DocumentEditor(docs.ToArray());
- //editor.PrintAllowed = true;
- editor.SaveAllowed = true;
- editor.ShowDialog();
- }
- else
- {
- MessageBox.Show("No Documents Available!");
- }
- return false;
- }
- private BitmapImage DocumentsImage(CoreRow arg)
- {
- if (arg == null)
- return docs;
- return arg.Get<Equipment, int>(x => x.Documents) > 0 ? docs : null;
- }
- private bool DuplicateEquipment(Button sender, CoreRow[] rows)
- {
- if (rows.Length != 1)
- {
- MessageBox.Show("Please select one equipment item to duplicate!");
- return false;
- }
- var row = rows.First();
- var res = MessageBox.Show("Do you wish to copy the attached documents along with this item?", "Copy Documents",
- MessageBoxButton.YesNoCancel);
- if (res == MessageBoxResult.Cancel)
- return false;
- Progress.Show("");
- var equipment = new Client<Equipment>().Load(new Filter<Equipment>(x => x.ID).IsEqualTo(row.Get<Equipment, Guid>(x => x.ID)))
- .FirstOrDefault();
- Progress.SetMessage("Duplicating Equipment");
- var id = equipment.ID;
- equipment.ID = Guid.Empty;
- equipment.Code = equipment.Code + " - COPY";
- equipment.ActiveSchedules = 0;
- equipment.CommitChanges();
- new Client<Equipment>().Save(equipment, "Equipment Item Duplicated");
- Progress.SetMessage("Duplicating Schedules");
- var schedules = new Client<Schedule>().Load(new Filter<Schedule>(x => x.DocumentID).IsEqualTo(id));
- foreach (var schedule in schedules)
- {
- schedule.ID = Guid.Empty;
- schedule.DocumentID = equipment.ID;
- schedule.CommitChanges();
- }
- new Client<Schedule>().Save(schedules, "Equipment Item Duplicated");
- if (res == MessageBoxResult.Yes)
- {
- Progress.SetMessage("Duplicating Document References");
- var docs = new Client<EquipmentDocument>().Load(new Filter<EquipmentDocument>(x => x.EntityLink.ID).IsEqualTo(id));
- foreach (var doc in docs)
- {
- doc.ID = Guid.Empty;
- doc.EntityLink.ID = equipment.ID;
- doc.CommitChanges();
- }
- new Client<EquipmentDocument>().Save(docs, "Equipment Item Duplicated");
- }
- Progress.Close();
- MessageBox.Show(string.Format("{0} created!", equipment.Code));
- return true;
- }
- protected override void Reload(
- Filters<Equipment> criteria, Columns<Equipment> columns, ref SortOrder<Equipment>? sort,
- CancellationToken token, Action<CoreTable?, Exception?> action)
- {
- if (GroupID != CoreUtils.FullGuid)
- criteria.Add(new Filter<Equipment>(x => x.GroupLink.ID).IsEqualTo(GroupID));
-
- if (CustomerID != CoreUtils.FullGuid)
- criteria.Add(new Filter<Equipment>(x => x.Customer.ID).IsEqualTo(CustomerID));
-
- base.Reload(criteria, columns, ref sort, token, action);
- }
- private bool ShowSpecificationSheet(CoreRow arg)
- {
- var id = Entity.EntityLinkID<Equipment, ImageDocumentLink>(x => x.SpecificationSheet, arg);
- //String file = arg.Get<Equipment, String>(x => x.Specification.FileName);
- if (id != null)
- return false;
- var doc = new Client<Document>().Load(new Filter<Document>(x => x.ID).IsEqualTo(id)).FirstOrDefault();
- if (doc == null)
- {
- MessageBox.Show("Unable to Load Document");
- return false;
- }
- var ext = Path.GetExtension(doc.FileName);
- var tmpfile = Path.ChangeExtension(Path.GetTempFileName(), ext);
- File.WriteAllBytes(tmpfile, doc.Data);
- ProcessStartInfo gsProcessInfo = new ProcessStartInfo();
- gsProcessInfo.Verb = "open";
- gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal;
- gsProcessInfo.FileName = tmpfile;
- gsProcessInfo.UseShellExecute = true;
- Process.Start(gsProcessInfo);
- return false;
- }
- private BitmapImage? SpecificationImage(CoreRow arg)
- {
- if (arg == null)
- return specification;
- var id = Entity.EntityLinkID<Equipment, ImageDocumentLink>(x => x.SpecificationSheet, arg);
- return id == null ? null : specification;
- }
- public override bool EditItems(Equipment[] items, Func<Type, CoreTable>? PageDataHandler = null, bool PreloadPages = false)
- {
- var result = base.EditItems(items, PageDataHandler, PreloadPages);
- if (result)
- {
- var changedcustomers = items.Any(x => x.Customer.ID != CustomerID);
- var changedgroups = GroupID != CoreUtils.FullGuid
- ? items.Cast<Equipment>().Any(x => x.GroupLink.ID != GroupID)
- : false;
- if (changedcustomers || changedgroups)
- {
- Refresh(false, true);
- return false;
- }
- }
- return result;
- }
- protected override BaseEditor? GetEditor(object item, DynamicGridColumn column)
- {
- if (String.Equals(column.ColumnName, CoreUtils.GetFullPropertyName<Equipment, Guid>(x => x.Customer.ID, ".")))
- {
- if (CustomerID == CoreUtils.FullGuid)
- return new NullEditor();
- }
- return base.GetEditor(item, column);
- }
- }
- }
|