using System;
using System.Collections.Generic;
using System.ComponentModel;
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;
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();
ConsignmentItems.GetParcels = () => ConsignmentParcels.Data;
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();
Guid consignmentid = row != null ? row.Get(x => x.ID) : CoreUtils.FullGuid;
bool completed = row == null || !row.Get(x => x.Closed).IsEmpty();
LoadConsigmment(row);
ConsignmentParcels.ConsignmentID = consignmentid;
ConsignmentParcels.Completed = completed;
ConsignmentParcels.Refresh(false, true);
ConsignmentItems.ConsignmentID = consignmentid;
ConsignmentItems.ParcelID = CoreUtils.FullGuid;
ConsignmentItems.Completed = completed;
ConsignmentItems.Refresh(false, true);
};
ConsignmentItems.OnChanged += ConsignmentItemsChanged;
}
public bool IsReady { get; set; }
public event DataModelUpdateEvent? OnUpdateDataModel;
public void CreateToolbarButtons(IPanelHost host)
{
if (Security.CanView())
{
host.CreateSetupAction(new PanelAction("Consignment Types",PRSDesktop.Resources.service,
(action) =>
{
var list = new MasterList(typeof(ConsignmentType));
list.ShowDialog();
}
));
}
}
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.AnchorWidth = settings.AnchorWidth;
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);
ConsignmentParcels.Refresh(true, false);
ConsignmentItems.Refresh(true, false);
}
public void Shutdown(CancelEventArgs? cancel)
{
}
private void ConsignmentItemsChanged(object? sender, EventArgs args)
{
var consrow = Consignments.Data.Rows.FirstOrDefault(r => r.Get(c => c.ID).Equals(ConsignmentItems.ConsignmentID));
if (consrow is 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, DynamicSplitPanelSettings e)
{
settings.ViewType = SplitPanel.View == DynamicSplitPanelView.Master ? ScreenViewType.Register :
SplitPanel.View == DynamicSplitPanelView.Detail ? ScreenViewType.Details : ScreenViewType.Combined;
settings.AnchorWidth = SplitPanel.AnchorWidth;
var newTag = settings.ViewType == ScreenViewType.Register ? settings.ViewType.ToString() : "";
if (Consignments.ColumnsTag != newTag)
{
Consignments.ColumnsTag = newTag;
Consignments.Refresh(true, true);
}
new UserConfiguration().Save(settings);
}
private void ConsignmentParcels_OnOnSelectItem(object sender, DynamicGridSelectionEventArgs e)
{
ConsignmentItems.ParcelID =
ConsignmentParcels.SelectedRows.FirstOrDefault()?.Get(x => x.ID) ??
CoreUtils.FullGuid;
ConsignmentItems.Refresh(false,true);
}
}
}