123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Configuration;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.WPF;
- namespace PRSDesktop
- {
- /// <summary>
- /// Interaction logic for ConsignmentsPanel.xaml
- /// </summary>
- public partial class ConsignmentsPanel : UserControl, IPanel<Consignment>
- {
- private readonly List<Tuple<Guid, string>> _categories = new();
- private readonly List<Tuple<Guid, string>> _types = new();
- private ConsignmentScreenSettings settings;
- public ConsignmentsPanel()
- {
- InitializeComponent();
- Consignments.HiddenColumns.Add(x => x.Supplier.Code);
- Consignments.HiddenColumns.Add(x => x.Supplier.Name);
- Consignments.HiddenColumns.Add(x => x.Number);
- Consignments.HiddenColumns.Add(x => x.Type.Description);
- Consignments.HiddenColumns.Add(x => x.Origin);
- Consignments.HiddenColumns.Add(x => x.Description);
- Consignments.HiddenColumns.Add(x => x.EstimatedDispatchDate);
- Consignments.HiddenColumns.Add(x => x.EstimatedPortArrival);
- Consignments.HiddenColumns.Add(x => x.EstimatedWarehouseArrival);
- Consignments.HiddenColumns.Add(x => x.ActualDispatchDate);
- Consignments.HiddenColumns.Add(x => x.ActualPortArrival);
- Consignments.HiddenColumns.Add(x => x.ActualWarehouseArrival);
- Consignments.HiddenColumns.Add(x => x.Status);
- Consignments.HiddenColumns.Add(x => x.Closed);
- Consignments.OnSelectItem += (o, e) =>
- {
- var row = e.Rows?.FirstOrDefault();
- ConsignmentItems.ConsignmentID = row != null ? row.Get<Consignment, Guid>(x => x.ID) : CoreUtils.FullGuid;
- ConsignmentItems.Completed = row != null ? row.Get<Consignment, DateTime>(x => x.Closed).IsEmpty() : true;
- LoadConsigmment(row);
- ConsignmentItems.Refresh(false, true);
- };
- ConsignmentItems.OnChanged += ConsignmentItemsChanged;
- }
- public bool IsReady { get; set; }
- public event DataModelUpdateEvent OnUpdateDataModel;
- public void CreateToolbarButtons(IPanelHost host)
- {
- }
- public string SectionName => "Consignments";
- public DataModel DataModel(Selection selection)
- {
- var ids = Consignments.ExtractValues(c => c.ID, selection).ToArray();
- return new BaseDataModel<Consignment>(new Filter<Consignment>(x => x.ID).InList(ids));
- }
- public void Heartbeat(TimeSpan time)
- {
- // Nothing to Do Here
- }
- public void Refresh()
- {
- Consignments.Refresh(false, true);
- }
- public Dictionary<string, object[]> Selected()
- {
- return new Dictionary<string, object[]>();
- }
- public void Setup()
- {
- settings = new UserConfiguration<ConsignmentScreenSettings>().Load();
- SplitPanel.View = settings.ViewType == ScreenViewType.Register ? DynamicSplitPanelView.Master :
- settings.ViewType == ScreenViewType.Details ? DynamicSplitPanelView.Detail : DynamicSplitPanelView.Combined;
- SplitPanel.MasterWidth = settings.ConsignmentColumnWidth;
- StatusList.SelectedIndex = settings.ShowAll ? 1 : 0;
- StatusList.SelectionChanged += StatusList_SelectionChanged;
- _types.Add(new Tuple<Guid, string>(CoreUtils.FullGuid, "All Types"));
- _categories.Add(new Tuple<Guid, string>(CoreUtils.FullGuid, "All Categories"));
- var query = new MultiQuery();
- query.Add(
- LookupFactory.DefineFilter<ConsignmentType>(),
- LookupFactory.DefineColumns<ConsignmentType>(),
- LookupFactory.DefineSort<ConsignmentType>()
- );
- query.Add(
- LookupFactory.DefineFilter<PurchaseOrderCategory>(),
- LookupFactory.DefineColumns<PurchaseOrderCategory>(),
- LookupFactory.DefineSort<PurchaseOrderCategory>()
- );
- query.Query();
- LoadLookups<ConsignmentType>(query, _types, x => x.Description);
- TypeList.ItemsSource = _types;
- TypeList.SelectedValue = _types.Any(x => x.Item1 == settings.SelectedType) ? settings.SelectedType : CoreUtils.FullGuid;
- Consignments.SelectedType = (Guid)TypeList.SelectedValue;
- TypeList.SelectionChanged += TypeList_SelectionChanged;
- LoadLookups<PurchaseOrderCategory>(query, _categories, x => x.Description);
- CategoryList.ItemsSource = _categories;
- CategoryList.SelectedValue =
- _categories.Any(x => x.Item1 == settings.SelectedCategory) ? settings.SelectedCategory : CoreUtils.FullGuid;
- Consignments.SelectedCategory = (Guid)CategoryList.SelectedValue;
- CategoryList.SelectionChanged += CategoryList_SelectionChanged;
- Consignments.ShowAll = settings.ShowAll;
- Consignments.ColumnsTag = settings.ViewType == ScreenViewType.Register ? settings.ViewType.ToString() : "";
- Consignments.Refresh(true, false);
- ConsignmentItems.Refresh(true, false);
- }
- public void Shutdown()
- {
- }
- private void ConsignmentItemsChanged(IDynamicGrid sender)
- {
- var consrow = Consignments.Data.Rows.FirstOrDefault(r => r.Get<Consignment, Guid>(c => c.ID).Equals(ConsignmentItems.ConsignmentID));
- if (consrow != null)
- {
- MessageBox.Show("Cannot find Consignment");
- return;
- }
- var consignment = consrow.ToObject<Consignment>();
- var allreceived = !ConsignmentItems.Data.Rows.Any(r => r.Get<PurchaseOrderItem, DateTime>(c => c.ReceivedDate).IsEmpty());
- var anyreceived = ConsignmentItems.Data.Rows.Any(r => !r.Get<PurchaseOrderItem, DateTime>(c => c.ReceivedDate).IsEmpty());
- if (anyreceived)
- {
- if (consignment.ActualWarehouseArrival.IsEmpty())
- consignment.ActualWarehouseArrival = DateTime.Now;
- }
- else
- {
- if (!consignment.ActualWarehouseArrival.IsEmpty())
- consignment.ActualWarehouseArrival = DateTime.MinValue;
- }
- if (allreceived)
- {
- if (consignment.Closed.IsEmpty())
- consignment.Closed = DateTime.Now;
- }
- else
- {
- if (!consignment.Closed.IsEmpty())
- consignment.Closed = DateTime.MinValue;
- }
- if (consignment.IsChanged())
- using (new WaitCursor())
- {
- new Client<Consignment>().Save(consignment, "Consignment Closed Date Updated");
- }
- }
- private string CheckDate(DateTime date)
- {
- return date.IsEmpty() ? "" : date.ToShortDateString();
- }
- private void LoadConsigmment(CoreRow row)
- {
- SupplierCode.Text = row == null ? "" : row.Get<Consignment, string>(x => x.Supplier.Code);
- SupplierName.Text = row == null ? "" : row.Get<Consignment, string>(x => x.Supplier.Name);
- Number.Text = row == null ? "" : row.Get<Consignment, string>(x => x.Number);
- Type.Text = row == null ? "" : row.Get<Consignment, string>(x => x.Type.Description);
- Origin.Text = row == null ? "" : row.Get<Consignment, string>(x => x.Origin);
- Description.Text = row == null ? "" : row.Get<Consignment, string>(x => x.Description);
- EstShip.Text = row == null ? "" : CheckDate(row.Get<Consignment, DateTime>(x => x.EstimatedDispatchDate));
- EstPort.Text = row == null ? "" : CheckDate(row.Get<Consignment, DateTime>(x => x.EstimatedPortArrival));
- EstArrival.Text = row == null ? "" : CheckDate(row.Get<Consignment, DateTime>(x => x.EstimatedWarehouseArrival));
- ActShip.Text = row == null ? "" : CheckDate(row.Get<Consignment, DateTime>(x => x.ActualDispatchDate));
- ActPort.Text = row == null ? "" : CheckDate(row.Get<Consignment, DateTime>(x => x.ActualPortArrival));
- ActArrival.Text = row == null ? "" : CheckDate(row.Get<Consignment, DateTime>(x => x.ActualWarehouseArrival));
- Status.Text = row == null ? "" : row.Get<Consignment, string>(x => x.Status);
- }
- private void CategoryList_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (!IsReady)
- return;
- Consignments.SelectedCategory = (Guid)CategoryList.SelectedValue;
- settings.SelectedCategory = Consignments.SelectedCategory;
- new UserConfiguration<ConsignmentScreenSettings>().Save(settings);
- Refresh();
- }
- private void TypeList_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (!IsReady)
- return;
- Consignments.SelectedType = (Guid)TypeList.SelectedValue;
- settings.SelectedType = Consignments.SelectedType;
- new UserConfiguration<ConsignmentScreenSettings>().Save(settings);
- Refresh();
- }
- private void LoadLookups<T>(MultiQuery query, List<Tuple<Guid, string>> lookups, Expression<Func<T, string>> display) where T : Entity
- {
- var table = query.Get<T>();
- foreach (var row in table.Rows)
- lookups.Add(new Tuple<Guid, string>(row.Get<T, Guid>(c => c.ID), row.Get(display)));
- }
- private void StatusList_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (!IsReady)
- return;
- Consignments.ShowAll = StatusList.SelectedIndex == 1;
- settings.ShowAll = Consignments.ShowAll;
- new UserConfiguration<ConsignmentScreenSettings>().Save(settings);
- Refresh();
- }
- private void Search_KeyUp(object sender, KeyEventArgs e)
- {
- }
- private void SplitPanel_OnChanged(object sender, DynamicSplitPanelChangedArgs e)
- {
- settings.ViewType = SplitPanel.View == DynamicSplitPanelView.Master ? ScreenViewType.Register :
- SplitPanel.View == DynamicSplitPanelView.Detail ? ScreenViewType.Details : ScreenViewType.Combined;
- settings.ConsignmentColumnWidth = SplitPanel.MasterWidth;
- var newTag = settings.ViewType == ScreenViewType.Register ? settings.ViewType.ToString() : "";
- if (Consignments.ColumnsTag != newTag)
- {
- Consignments.ColumnsTag = newTag;
- Consignments.Refresh(true, true);
- }
- new UserConfiguration<ConsignmentScreenSettings>().Save(settings);
- }
- }
- }
|