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
{
///
/// Interaction logic for ConsignmentsPanel.xaml
///
public partial class ConsignmentsPanel : UserControl, IPanel
{
private readonly List> _categories = new();
private readonly List> _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(x => x.ID) : CoreUtils.FullGuid;
ConsignmentItems.Completed = row != null ? row.Get(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(new Filter(x => x.ID).InList(ids));
}
public void Heartbeat(TimeSpan time)
{
// Nothing to Do Here
}
public void Refresh()
{
Consignments.Refresh(false, true);
}
public Dictionary Selected()
{
return new Dictionary();
}
public void Setup()
{
settings = new UserConfiguration().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(CoreUtils.FullGuid, "All Types"));
_categories.Add(new Tuple(CoreUtils.FullGuid, "All Categories"));
var query = new MultiQuery();
query.Add(
LookupFactory.DefineFilter(),
LookupFactory.DefineColumns(),
LookupFactory.DefineSort()
);
query.Add(
LookupFactory.DefineFilter(),
LookupFactory.DefineColumns(),
LookupFactory.DefineSort()
);
query.Query();
LoadLookups(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(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(c => c.ID).Equals(ConsignmentItems.ConsignmentID));
if (consrow != null)
{
MessageBox.Show("Cannot find Consignment");
return;
}
var consignment = consrow.ToObject();
var allreceived = !ConsignmentItems.Data.Rows.Any(r => r.Get(c => c.ReceivedDate).IsEmpty());
var anyreceived = ConsignmentItems.Data.Rows.Any(r => !r.Get(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().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(x => x.Supplier.Code);
SupplierName.Text = row == null ? "" : row.Get(x => x.Supplier.Name);
Number.Text = row == null ? "" : row.Get(x => x.Number);
Type.Text = row == null ? "" : row.Get(x => x.Type.Description);
Origin.Text = row == null ? "" : row.Get(x => x.Origin);
Description.Text = row == null ? "" : row.Get(x => x.Description);
EstShip.Text = row == null ? "" : CheckDate(row.Get(x => x.EstimatedDispatchDate));
EstPort.Text = row == null ? "" : CheckDate(row.Get(x => x.EstimatedPortArrival));
EstArrival.Text = row == null ? "" : CheckDate(row.Get(x => x.EstimatedWarehouseArrival));
ActShip.Text = row == null ? "" : CheckDate(row.Get(x => x.ActualDispatchDate));
ActPort.Text = row == null ? "" : CheckDate(row.Get(x => x.ActualPortArrival));
ActArrival.Text = row == null ? "" : CheckDate(row.Get(x => x.ActualWarehouseArrival));
Status.Text = row == null ? "" : row.Get(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().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().Save(settings);
Refresh();
}
private void LoadLookups(MultiQuery query, List> lookups, Expression> display) where T : Entity
{
var table = query.Get();
foreach (var row in table.Rows)
lookups.Add(new Tuple(row.Get(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().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().Save(settings);
}
}
}