using System; using System.Diagnostics; using System.IO; using System.Linq; using System.Threading; using System.Windows; using System.Windows.Media.Imaging; using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.DynamicGrid; using InABox.WPF; namespace PRSDesktop { internal class ConsignmentGrid : DynamicDataGrid { //Button receive = null; public ConsignmentGrid() { ActionColumns.Add(new DynamicImageColumn(ConsignmentImage, ShowBillOfLading) { Position = DynamicActionColumnPosition.Start }); HiddenColumns.Add(x => x.BillOfLading.ID); HiddenColumns.Add(x => x.Closed); HiddenColumns.Add(x => x.Supplier.Code); } protected override void DoReconfigure(DynamicGridOptions options) { base.DoReconfigure(options); options.RecordCount = true; options.SelectColumns = true; options.FilterRows = true; } public bool ShowAll { get; set; } public Guid SelectedCategory { get; set; } public Guid SelectedType { get; set; } //private bool ReceiveConsignment(Button sender, CoreRow[] rows) //{ // if (!rows.Any()) // { // MessageBox.Show("Please select a row first"); // return false; // } // if (!rows.First().Get(x => x.Closed).IsEmpty()) // { // MessageBox.Show("Consignment is already closed!"); // return false; // } // Guid id = rows.First().Get(x => x.ID); // DateTime now = DateTime.Now; // using (new WaitCursor()) // { // PurchaseOrderItem[] items = new Client().Load(new Filter(x => x.Consignment.ID).IsEqualTo(id).And(x => x.ReceivedDate).IsEqualTo(DateTime.MinValue)); // foreach (var item in items) // item.ReceivedDate = now; // new Client().Save(items, "Consignment Received"); // //Consignment consignment = LoadItem(rows.First()); // //consignment.Closed = now; // //new Client().Save(consignment, "Consignment Received"); // } // return true; //} //protected override void SelectItems(CoreRow[] rows) //{ // receive.IsEnabled = (rows != null) && rows.Any(r=>r.Get(c => c.Closed).IsEmpty()); // base.SelectItems(rows); //} protected override void Reload( Filters criteria, Columns columns, ref SortOrder? sort, CancellationToken token, Action action) { if (!ShowAll) criteria.Add(new Filter(x => x.ActualWarehouseArrival).IsEqualTo(DateTime.MinValue)); if (SelectedCategory != CoreUtils.FullGuid) criteria.Add(new Filter(x => x.Category.ID).IsEqualTo(SelectedCategory)); if (SelectedType != CoreUtils.FullGuid) criteria.Add(new Filter(x => x.Type.ID).IsEqualTo(SelectedType)); base.Reload(criteria, columns, ref sort, token, action); } private bool ShowBillOfLading(CoreRow? arg) { if(arg is null) { return false; } var id = arg.Get(x => x.BillOfLading.ID); if (!Entity.IsEntityLinkValid(x => x.BillOfLading, arg)) return false; var doc = new Client().Load(new Filter(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); var gsProcessInfo = new ProcessStartInfo(); gsProcessInfo.Verb = "open"; gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal; gsProcessInfo.FileName = tmpfile; gsProcessInfo.UseShellExecute = true; Process.Start(gsProcessInfo); return false; } private BitmapImage? ConsignmentImage(CoreRow? arg) { if (arg == null) return PRSDesktop.Resources.doc_pdf.AsBitmapImage(); var id = arg.Get(x => x.BillOfLading.ID); return !Entity.IsEntityLinkValid(x => x.BillOfLading, arg) ? null : PRSDesktop.Resources.doc_pdf.AsBitmapImage(); } protected override void DoReconfigureEditors(DynamicEditorGrid grid, Consignment[] items) { base.DoReconfigureEditors(grid, items); var fc = grid.FindEditor(nameof(Consignment.ForeignCurrencyCost)) as CurrencyEditorControl; if (fc != null) { fc.SetEnabled(items.All(x => x.Supplier.Currency.ID != Guid.Empty)); fc.CurrencySymbol = items.First().Supplier.Currency.Symbol; } var et = grid.FindEditor(nameof(Consignment.ExTax)); if (et != null) { et.SetEnabled(items.All(x=>x.Supplier.Currency.ID == Guid.Empty)); } } } }