using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Comal.Classes;
using InABox.Clients;
using InABox.Configuration;
using InABox.Core;
using InABox.WPF;
using InABox.Wpf;
using System.ComponentModel;
namespace PRSDesktop
{
///
/// Interaction logic for ManufacturingAllocationPanel.xaml
///
public partial class ManufacturingAllocationPanel : UserControl, IPanel
{
private Guid _section = Guid.Empty;
private CoreTable _sections;
private readonly BitmapImage barcode = PRSDesktop.Resources.barcode.AsBitmapImage();
private readonly List> columns = new();
private ListBox current;
private readonly BitmapImage disabled = PRSDesktop.Resources.disabled.AsBitmapImage();
private readonly BitmapImage grouped = PRSDesktop.Resources.grouped.AsBitmapImage();
private readonly string NEARLYDUE_COLOR = "Orange";
private readonly string NOTYETDUE_COLOR = "PaleGreen";
private readonly string ORDER_COLOR = "Gray";
private readonly string OVERDUE_COLOR = "Salmon";
private CoreTable Packets;
private readonly string PRIORITY_COLOR = "Red";
private readonly string QA_COLOR = "Silver";
private string SELECTED_COLOR = "Yellow";
private readonly string SHARED_COLOR = "Lime";
private CoreTable Stages;
public ManufacturingAllocationPanel()
{
InitializeComponent();
Kanbans = new ObservableCollection();
PendingCheck.Tag = Pending;
}
public ObservableCollection Kanbans { get; set; }
public bool IsReady { get; set; }
public void CreateToolbarButtons(IPanelHost host)
{
ManufacturingSetupActions.Standard(host);
}
public string SectionName => "Factory Allocation";
public DataModel DataModel(Selection selection)
{
var ids = Packets != null ? Packets.Rows.Select(r => r.Get(x => x.ID)).ToArray() : new Guid[] { };
return new ManufacturingPacketDataModel(new Filter(x => x.ID).InList(ids));
}
public void Refresh()
{
if (_section == Guid.Empty)
{
var sections = (Dictionary)Sections.ItemsSource;
_section = sections.Any() ? sections.First().Key : Guid.Empty;
Sections.SelectedValue = _section;
}
ReloadPackets(true);
}
public Dictionary Selected()
{
var result = new Dictionary();
return result;
}
public void Setup()
{
var settings = new UserConfiguration().Load();
var sections = new Dictionary();
ReloadSections();
foreach (var row in _sections.Rows)
sections[row.Get(x => x.ID)] = string.Format("{0}: {1}",
row.Get(x => x.Factory.Name), row.Get(x => x.Name));
_section = sections.ContainsKey(settings.Section) ? settings.Section : sections.Any() ? sections.First().Key : Guid.Empty;
Sections.ItemsSource = sections;
Sections.SelectedValue = _section;
}
public void Shutdown(CancelEventArgs? cancel)
{
}
public event DataModelUpdateEvent? OnUpdateDataModel;
public void Heartbeat(TimeSpan time)
{
// Nothing to do here
}
private BitmapImage GetBarCode(CoreRow packet)
{
if (!packet.Get(c => c.BarcodePrinted).IsEmpty())
return packet.Get(c => c.BarcodeType) == BarcodeType.Grouped ? grouped : barcode;
if (packet.Get(c => c.BarcodeType) == BarcodeType.None)
return disabled;
return null;
}
private string GetColor(DateTime duedate, DateTime estdate)
{
var color = NOTYETDUE_COLOR;
if (duedate < estdate)
color = OVERDUE_COLOR;
else if (duedate < estdate.AddDays(7))
color = NEARLYDUE_COLOR;
return color;
}
private void CreateKanban(CoreRow row, bool IsChecked)
{
try
{
var packetid = row.Get(x => x.ID);
var priority = row.Get(c => c.Priority);
var qty = row.Get(c => c.Quantity);
var barqty = row.Get(c => c.BarcodeQty);
var duedate = row.Get(c => c.DueDate);
var estdate = row.Get(c => c.EstimatedDate);
var sectionid = row.Get(c => c.StageLink.SectionID);
var pktsection = row.Get(c => c.StageLink.Section);
var stage = Stages.Rows.FirstOrDefault(r => r.Get(c => c.Parent.ID).Equals(packetid));
if (stage == null)
return;
var section = (Guid)Sections.SelectedValue;
var station = stage.Get(c => c.Station);
var quality = stage.Get(c => c.QualityStatus);
var percentage = stage.Get(c => c.PercentageComplete);
var Pending = station == 0;
var model = new ManufacturingKanban();
model.ID = row.Get(c => c.ID).ToString();
model.Title = string.Format("{0}: {1}{2}",
row.Get(c => c.Serial),
qty != barqty ? string.Format("{0} x ", qty) : "",
row.Get(c => c.Title)
);
model.Quantity = barqty;
model.JobName = string.Format("{0}: {1}",
row.Get(c => c.SetoutLink.Number),
row.Get(c => c.SetoutLink.JobLink.Name)
);
model.DueDate = duedate;
var location = row.Get(c => c.Location);
var descrip = new List
{
//row.Get(c=>c.Level.Code),
//row.Get(c=>c.Zone.Code),
string.IsNullOrEmpty(location) ? row.Get(c => c.SetoutLink.Location) : location
};
model.Description = string.Join(" / ", descrip.Where(x => !string.IsNullOrWhiteSpace(x))).Trim();
model.TemplateID = row.Get(c => c.ManufacturingTemplateLink.ID);
model.Image = GetBarCode(row);
//model.IsSelected = packet.ID.ToString() == CurrentKanbanID;
model.Tags = priority ? new[] { "PRIORITY" } : new string[] { };
model.Category = section.ToString(); // packet.StageLink.SectionID.ToString();
model.ColorKey =
Entity.IsEntityLinkValid(x => x.OrderItem, row) &&
row.Get(c => c.OrderItem.ReceivedDate).IsEmpty() ? ORDER_COLOR :
priority ? PRIORITY_COLOR :
!Pending ? GetColor(duedate.IsEmpty() ? DateTime.Today : duedate, estdate.IsEmpty() ? DateTime.Today : estdate) : QA_COLOR;
model.SelectedColor = model.ColorKey; // packet.ID.ToString() == CurrentKanbanID ? SELECTED_COLOR : model.ColorKey;
model.SharedColor = station.Equals(-1) ? SHARED_COLOR : model.ColorKey;
model.Checked = IsChecked;
model.SetoutID = row.Get(c => c.SetoutLink.ID);
model.Assignee = station.ToString();
var ratio = 1.0F / (station == -1 ? (double)columns.Count : 1.0F);
var percentleft = (100.0F - stage.Get(c => c.PercentageComplete)) / 100.0F;
model.Status = string.Format("({0:F2} hours)",
qty * stage.Get(c => c.Time).TotalHours * ratio *
percentleft); //packet.StageLink.ID.Equals(Guid.Empty) ? " " : Pending ? "PENDING" /*GetQualityStatus(quality)*/ : String.Format("{0:F0}%", percentage);
model.Flags = row.Get(c => c.Distributed)
? !sectionid.Equals(section) ? string.IsNullOrEmpty(pktsection) ? "" : pktsection.ToUpper().Trim() : "DISTRIB"
: "";
model.GroupID = row.Get(c => c.SetoutLink.Group.ID);
var groupname = row.Get(c => c.SetoutLink.Group.Name);
model.GroupName = !string.IsNullOrWhiteSpace(groupname) ?
row.Get(c => c.SetoutLink.Group.Job.JobNumber) + ": " + groupname
: "";
Kanbans.Add(model);
}
catch (Exception e)
{
Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
}
}
private TimeSpan CalcTime(IEnumerable kanbans)
{
double hours = 0.0F;
var ids = kanbans.Select(x => Guid.Parse(x.ID));
foreach (var id in ids)
{
var packet = Packets.Rows.FirstOrDefault(r => r.Get(c => c.ID).Equals(id));
var stage = Stages.Rows.FirstOrDefault(r => r.Get(c => c.Parent.ID).Equals(id));
var ratio = 1.0F / (stage.Get(c => c.Station) == -1 ? (double)columns.Count : 1.0F);
var percentleft = (100.0F - stage.Get(c => c.PercentageComplete)) / 100.0F;
hours += packet.Get(c => c.Quantity) *
stage.Get(c => c.Time).TotalHours *
ratio * percentleft;
}
return TimeSpan.FromHours(hours);
}
private void ReloadPackets(bool reloaddata)
{
using (new WaitCursor())
{
var checks = Kanbans.Where(x => x.Checked).Select(x => x.ID).ToArray();
Kanbans.Clear();
if (reloaddata && Sections.SelectedValue != null)
{
var sectionid = (Guid)Sections.SelectedValue; // CurrentSection != null ? CurrentSection.ID : CoreUtils.FullGuid;
Stages = new Client().Query(
new Filter(x => x.ManufacturingSectionLink.ID).IsEqualTo(sectionid).And(x => x.Completed)
.IsEqualTo(DateTime.MinValue),
Columns.None().Add(
x => x.ID,
x => x.Parent.ID,
x => x.ManufacturingSectionLink.ID,
x => x.Station,
x => x.QualityStatus,
x => x.PercentageComplete,
x => x.Time,
x => x.Started
)
);
//Stages = stagetable.Rows.Select(x => x.ToObject()).ToArray();
var filter = new Filter(x => x.Completed).IsEqualTo(DateTime.MinValue)
.And(x => x.Archived).IsEqualTo(DateTime.MinValue)
.And(x => x.OnHold).IsEqualTo(false);
var sectfilter = new Filter(x => x.StageLink.SectionID).IsEqualTo(sectionid).Or(x => x.Distributed)
.IsEqualTo(true);
filter.Ands.Add(sectfilter);
var columns = Columns.None();
var iprops = DatabaseSchema.Properties(typeof(ManufacturingPacket)).Where(x =>
!x.Name.Equals("CustomAttributes") && !x.Name.Equals("Stages") && !x.Name.Equals("Time") && !x.Name.Equals("ActualTime") &&
!x.Name.Equals("TimeRemaining"));
foreach (var iprop in iprops)
columns.Add(iprop.Name);
Packets = new Client().Query(
filter,
columns,
//new Columns(
// x => x.ID,
// x => x.Serial,
// x => x.Title,
// x => x.Quantity,
// x => x.SetoutLink.Number,
// x => x.SetoutLink.JobLink.JobNumber,
// x => x.SetoutLink.JobLink.Name,
// x => x.DueDate,
// x => x.SetoutLink.Location,
// x => x.SetoutLink.Reference,
// x => x.Priority,
// x => x.OrderItem.ID,
// x => x.EstimatedDate,
// x => x.Distributed,
// x => x.StageLink.SectionID,
// x => x.StageLink.Section,
// x => x.BarcodePrinted,
// x => x.BarcodeType
// ),
new SortOrder(x => x.Priority, SortDirection.Descending).ThenBy(x => x.SetoutLink.Number)
);
//Packets = table.Rows.Select(x => x.ToObject()).ToArray();
}
if (Packets != null)
foreach (var row in Packets.Rows)
CreateKanban(row, false);
Pending.ItemsSource = null;
var pendings = Kanbans.Where(x => x.Assignee.Equals("0")).OrderBy(x => x.Tags.Contains("PRIORITY") ? 0 : 1).ThenBy(x => x.DueDate);
Pending.ItemsSource = pendings;
Task.Run(() =>
{
var time = CalcTime(pendings).TotalHours;
Dispatcher.Invoke(() => { Hours.Content = string.Format("{0:F2} hrs", time); });
});
foreach (var column in columns)
{
column.Item3.ItemsSource = null;
var items = Kanbans.Where(x => x.Assignee.Equals("-1") || x.Assignee.Equals(column.Item5.ToString()))
.OrderBy(x => x.Tags.Contains("PRIORITY") ? 0 : 1).ThenBy(x => x.DueDate);
column.Item3.ItemsSource = items;
column.Item6.IsChecked = false;
Task.Run(() =>
{
var time = CalcTime(items).TotalHours;
Dispatcher.Invoke(() => { column.Item7.Content = string.Format("{0:F2} hrs", time); });
});
}
}
}
private void ReloadSections()
{
_sections = new Client().Query(
new Filter(x => x.Hidden).IsEqualTo(false),
Columns.None().Add(
x => x.ID,
x => x.Factory.Name,
x => x.Name,
x => x.Stations
),
new SortOrder(x => x.Factory.Sequence).ThenBy(x => x.Sequence)
);
}
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
base.OnRenderSizeChanged(sizeInfo);
var width = sizeInfo.NewSize.Width;
SetWidths(width);
}
private void SetWidths(double width)
{
double stations = Stations.ColumnDefinitions.Count;
var desiredwidth = (width - 5.0F) / (stations + 1.0F);
if (desiredwidth < 300.0F)
desiredwidth = 300.0F;
//Sections.Width = desiredwidth;
Pending.Width = desiredwidth;
Stations.Width = stations * desiredwidth;
}
private void Sections_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count == 0)
return;
var pair = (KeyValuePair)e.AddedItems[0];
var sectionid = pair.Key;
ReloadColumns(sectionid);
if (IsReady)
new UserConfiguration().Save(new ManufacturingAllocationSettings { Section = pair.Key });
}
private void ReloadColumns(Guid sectionid)
{
// Delete all existing lists and Grid Rows
foreach (var column in columns)
{
Stations.Children.Remove(column.Item1);
Stations.Children.Remove(column.Item3);
Stations.ColumnDefinitions.Remove(column.Item4);
}
columns.Clear();
var history = new Client().Query(
new Filter(x => x.Section.ID).IsEqualTo(sectionid).And(x => x.Date)
.IsGreaterThanOrEqualTo(DateTime.Today.AddDays(-7)),
Columns.None().Add(
x => x.Station,
x => x.Employee.Name
),
new SortOrder(x => x.Station).ThenBy(x => x.LastUpdate)
);
var section = _sections.Rows.First(r => r.Get(x => x.ID).Equals(sectionid));
var stations = section.Get(x => x.Stations);
for (var iStation = 0; iStation < stations; iStation++)
{
var row = history.Rows.LastOrDefault(r => r.Get(c => c.Station).Equals(iStation + 1));
var empname = row != null ? row.Get(x => x.Employee.Name) : string.Format("Station {0}", iStation + 1);
var Column = new ColumnDefinition { Width = new GridLength(1.0F, GridUnitType.Star) };
Stations.ColumnDefinitions.Add(Column);
// Create a Border
var Border = new Border();
Border.BorderBrush = new SolidColorBrush(Colors.Gray);
Border.BorderThickness = new Thickness(0);
Border.CornerRadius = new CornerRadius(5, 5, 0, 0);
Border.Margin = new Thickness(0, 0, 2, 2);
Border.SetValue(Grid.RowProperty, 0);
Border.SetValue(Grid.ColumnProperty, iStation);
Border.Height = 30.0F;
Stations.Children.Add(Border);
var grid = new Grid();
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1.0F, GridUnitType.Auto) });
grid.ColumnDefinitions.Add(new ColumnDefinition
{
Width = new GridLength(iStation == stations - 1 && Security.IsAllowed() ? 30.0F : 0.0F,
GridUnitType.Pixel)
});
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1.0F, GridUnitType.Star) });
grid.ColumnDefinitions.Add(new ColumnDefinition
{
Width = new GridLength(iStation == stations - 1 && Security.IsAllowed() ? 30.0F : 0.0F,
GridUnitType.Pixel)
});
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1.0F, GridUnitType.Auto) });
Border.Child = grid;
var checkborder = new Border();
checkborder.BorderBrush = new SolidColorBrush(Colors.Gray);
checkborder.BorderThickness = new Thickness(0.75);
checkborder.CornerRadius = new CornerRadius(5, 0, 0, 0);
checkborder.Margin = new Thickness(0, 0, 2, 0);
checkborder.SetValue(Grid.RowProperty, 0);
checkborder.SetValue(Grid.ColumnProperty, 0);
grid.Children.Add(checkborder);
var check = new CheckBox();
check.Margin = new Thickness(14, 0, 17, 0);
check.SetValue(Grid.ColumnProperty, 0);
check.Checked += List_Checked;
check.Unchecked += List_Checked;
check.VerticalAlignment = VerticalAlignment.Center;
checkborder.Child = check;
var remove = new Button();
remove.Margin = new Thickness(0, 0, 2, 0);
remove.SetValue(Grid.RowProperty, 0);
remove.SetValue(Grid.ColumnProperty, 1);
remove.Content = "-";
remove.Click += Remove_Click;
grid.Children.Add(remove);
var labelborder = new Border();
labelborder.BorderBrush = new SolidColorBrush(Colors.Gray);
labelborder.BorderThickness = new Thickness(0.75);
labelborder.CornerRadius = new CornerRadius(0, 0, 0, 0);
labelborder.Margin = new Thickness(0, 0, 2, 0);
labelborder.SetValue(Grid.RowProperty, 0);
labelborder.SetValue(Grid.ColumnProperty, 2);
grid.Children.Add(labelborder);
var Label = new Label();
Label.SetValue(Grid.ColumnProperty, 1);
Label.Content = empname;
Label.HorizontalContentAlignment = HorizontalAlignment.Center;
Label.VerticalContentAlignment = VerticalAlignment.Center;
labelborder.Child = Label;
var add = new Button();
add.Margin = new Thickness(0, 0, 2, 0);
add.SetValue(Grid.RowProperty, 0);
add.SetValue(Grid.ColumnProperty, 3);
add.Content = "+";
add.Click += Add_Click;
grid.Children.Add(add);
var hoursborder = new Border();
hoursborder.BorderBrush = new SolidColorBrush(Colors.Gray);
hoursborder.BorderThickness = new Thickness(0.75);
hoursborder.CornerRadius = new CornerRadius(0, 5, 0, 0);
hoursborder.Margin = new Thickness(0, 0, 0, 0);
hoursborder.SetValue(Grid.RowProperty, 0);
hoursborder.SetValue(Grid.ColumnProperty, 4);
grid.Children.Add(hoursborder);
var hours = new Label();
hours.SetValue(Grid.ColumnProperty, 2);
hours.Content = string.Format("({0} hours)", 0.0F);
hours.HorizontalContentAlignment = HorizontalAlignment.Center;
hours.VerticalContentAlignment = VerticalAlignment.Center;
hoursborder.Child = hours;
var Items = new ListBox();
Items.Margin = new Thickness(0, 0, 2, 2);
Items.SetValue(Grid.RowProperty, 1);
Items.SetValue(Grid.ColumnProperty, iStation);
Items.ItemTemplate = (DataTemplate)Resources["Packet"];
Items.HorizontalContentAlignment = HorizontalAlignment.Stretch;
Items.SetValue(ScrollViewer.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Disabled);
Items.SetValue(VirtualizingPanel.IsVirtualizingProperty, true);
Items.SetValue(VirtualizingPanel.VirtualizationModeProperty, VirtualizationMode.Recycling);
Items.PreviewMouseRightButtonDown += Items_PreviewMouseRightButtonDown;
Items.PreviewMouseDown += Items_PreviewMouseDown;
Items.SelectionChanged += Items_SelectionChanged;
Stations.Children.Add(Items);
check.Tag = Items;
remove.Tag = Items;
var column = new Tuple(Border, Label, Items, Column, iStation + 1,
check,
hours);
columns.Add(column);
}
SetWidths(ActualWidth);
ReloadPackets(true);
}
private void Items_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
current = sender as ListBox;
}
private void Items_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
current = sender as ListBox;
}
private void Items_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
current = sender as ListBox;
}
private void Add_Click(object sender, RoutedEventArgs e)
{
var sectionid = (Guid)Sections.SelectedValue;
var section = new Client().Load(new Filter(x => x.ID).IsEqualTo(sectionid)).FirstOrDefault();
if (section != null)
{
section.Stations += 1;
new Client().Save(section, "Added Station");
ReloadSections();
ReloadColumns(section.ID);
}
}
private void Remove_Click(object sender, RoutedEventArgs e)
{
var sectionid = (Guid)Sections.SelectedValue;
var section = new Client().Load(new Filter(x => x.ID).IsEqualTo(sectionid)).FirstOrDefault();
if (section != null)
{
if (section.Stations < 2)
{
MessageBox.Show("There must be at least one station available in each section!");
return;
}
var button = sender as Button;
var listbox = button.Tag as ListBox;
var kanbans = listbox.ItemsSource as IEnumerable;
if (kanbans.Any(x => !x.Assignee.Equals("-1")))
{
MessageBox.Show("Please clear out all packets before removing this station!");
return;
}
section.Stations -= 1;
new Client().Save(section, "Removed Station");
ReloadSections();
ReloadColumns(section.ID);
}
}
private void CardSelected(object sender, MouseButtonEventArgs e)
{
}
private void CardPreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
}
private void CardChecked(object sender, RoutedEventArgs e)
{
}
private void PacketMenu_Opened(object sender, RoutedEventArgs e)
{
var menu = sender as ContextMenu;
var kanban = menu.Tag as ManufacturingKanban;
var assign = menu.Items[0] as MenuItem;
var revert = menu.Items[1] as MenuItem;
var separator = menu.Items[2] as Separator;
var distribute = menu.Items[3] as MenuItem;
var undistribute = menu.Items[4] as MenuItem;
var separator2 = menu.Items[5] as Separator;
var share = menu.Items[6] as MenuItem;
var unshare = menu.Items[7] as MenuItem;
assign.Visibility = kanban.Assignee.Equals("-1") ? Visibility.Collapsed : Visibility.Visible;
revert.Visibility = kanban.Assignee.Equals("0") ? Visibility.Collapsed : Visibility.Visible;
distribute.Visibility = Security.IsAllowed() ? Visibility.Visible : Visibility.Collapsed;
undistribute.Visibility = distribute.Visibility;
separator2.Visibility = distribute.Visibility;
share.Visibility = kanban.Assignee.Equals("-1") ? Visibility.Collapsed : Visibility.Visible;
unshare.Visibility = kanban.Assignee.Equals("-1") ? Visibility.Visible : Visibility.Collapsed;
assign.Items.Clear();
foreach (var column in columns)
{
var item = new MenuItem { Header = column.Item2.Content, Tag = column.Item5 };
item.Click += SetStation_Click;
assign.Items.Add(item);
}
}
private IEnumerable CheckedKanbans(ListBox listbox)
{
var kanbans = listbox.ItemsSource as IEnumerable;
var result = kanbans.Where(x => x.Checked);
if (!result.Any())
result = new[] { listbox.SelectedValue as ManufacturingKanban };
return result;
}
private IEnumerable CheckedPackets(ListBox listbox)
{
var kanbans = CheckedKanbans(listbox).Select(x => Guid.Parse(x.ID));
var packets = Packets.Rows.Where(r => kanbans.Contains(r.Get(c => c.ID)));
return packets.Select(x => x.ToObject());
}
private IEnumerable CheckedStages(ListBox listbox)
{
var kanbans = CheckedKanbans(listbox).Select(x => Guid.Parse(x.ID));
var selstages = Stages.Rows.Where(r =>
kanbans.Contains(r.Get(c => c.Parent.ID)) &&
r.Get(c => c.ManufacturingSectionLink.ID).Equals(_section));
return selstages.Select(x => x.ToObject());
}
private Tuple GetColumn(object sender)
{
var menu = sender as MenuItem;
Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("GetColumn: Menu is {0}", menu != null ? menu.Header : "null"));
var context = menu.Parent as ContextMenu;
Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("GetColumn: Context is {0}", context != null ? "ok" : "null"));
var border = context.PlacementTarget as Border;
Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("GetColumn: Border is {0}", border != null ? "ok" : "null"));
var kanban = menu.Tag as ManufacturingKanban;
Logger.Send(LogType.Information, ClientFactory.UserID,
string.Format("GetColumn: kanban is {0}", kanban != null ? kanban.Description : "null"));
var column = columns.FirstOrDefault(x => x.Item3 == current);
Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("GetColumn: column is {0}", column != null ? "ok" : "null"));
return column;
}
private void SetStation_Click(object sender, RoutedEventArgs e)
{
var menu = sender as MenuItem;
var station = (int)menu.Tag;
Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("Allocating: Station is {0}", station));
var column = GetColumn(menu.Parent);
var list = column == null ? Pending : column.Item3;
//Logger.Send(LogType.Information, ClientFactory.UserID, String.Format("Allocating: Column is {0}", column.Item2.Content));
var stages = CheckedStages(list).ToArray();
Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("Allocating: Packet Count is {0}", stages.Length));
Logger.Send(LogType.Information, ClientFactory.UserID,
string.Format("Allocating: {0}", string.Join(", ", stages.Select(x => x.Parent.Serial))));
foreach (var stage in stages)
{
stage.Station = station;
if (stage.Started.Equals(DateTime.MinValue))
stage.Started = DateTime.Now;
}
using (new WaitCursor())
{
Logger.Send(LogType.Information, ClientFactory.UserID,
string.Format("Allocating: Updating {0} items", stages.Where(x => x.IsChanged()).ToArray().Length));
new Client().Save(stages, string.Format("Assigned to Station {0}", station));
}
Refresh();
}
private void Revert_Click(object sender, RoutedEventArgs e)
{
var column = GetColumn(sender);
var stages = CheckedStages(column != null ? column.Item3 : Pending).ToArray();
var bDeleteStarted = false;
foreach (var stage in stages)
{
if (stage.PercentageComplete > 0.0F)
bDeleteStarted = true;
stage.Station = 0;
stage.Started = DateTime.MinValue;
}
if (bDeleteStarted)
bDeleteStarted =
MessageBox.Show("Some items have already been started.\n\nRemove these packets anyway?", "Confirm", MessageBoxButton.YesNo) ==
MessageBoxResult.Yes;
using (new WaitCursor())
{
var updates = stages.Where(x => bDeleteStarted ? true : x.PercentageComplete == 0.0F).ToArray();
new Client().Save(updates, "Reverted to Pending Status");
}
Refresh();
}
private void Distribute_Click(object sender, RoutedEventArgs e)
{
var column = GetColumn(sender);
var packets = CheckedPackets(column != null ? column.Item3 : Pending).ToArray();
foreach (var packet in packets)
packet.Distributed = true;
using (new WaitCursor())
{
new Client().Save(packets, "Set Distributed Flag");
}
Refresh();
}
private void ClearDistributed_Click(object sender, RoutedEventArgs e)
{
var column = GetColumn(sender);
var packets = CheckedPackets(column != null ? column.Item3 : Pending).ToArray();
foreach (var packet in packets)
packet.Distributed = false;
using (new WaitCursor())
{
new Client().Save(packets, "Cleared Distributed Flag");
}
Refresh();
}
private void SetShared_Click(object sender, RoutedEventArgs e)
{
var column = GetColumn(sender);
var stages = CheckedStages(column != null ? column.Item3 : Pending);
foreach (var stage in stages)
{
stage.Station = -1;
if (stage.Started.Equals(DateTime.MinValue))
stage.Started = DateTime.Now;
}
using (new WaitCursor())
{
new Client().Save(stages, "Cleared Shared Flag");
}
Refresh();
}
private void ClearShared_Click(object sender, RoutedEventArgs e)
{
var column = GetColumn(sender);
var stages = CheckedStages(column != null ? column.Item3 : Pending);
foreach (var stage in stages)
stage.Station = column.Item5;
using (new WaitCursor())
{
new Client().Save(stages, "Cleared Shared Flag");
}
Refresh();
}
private void List_Checked(object sender, RoutedEventArgs e)
{
var check = sender as CheckBox;
var list = check.Tag as ListBox;
var kanbans = list.ItemsSource as IEnumerable;
foreach (var kanban in kanbans)
kanban.Checked = check.IsChecked == true;
list.ItemsSource = null;
list.ItemsSource = kanbans;
}
}
}