123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827 |
- 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;
- namespace PRSDesktop
- {
- /// <summary>
- /// Interaction logic for ManufacturingAllocationPanel.xaml
- /// </summary>
- public partial class ManufacturingAllocationPanel : UserControl, IPanel<ManufacturingPacket>
- {
- private Guid _section = Guid.Empty;
- private CoreTable _sections;
- private readonly BitmapImage barcode = PRSDesktop.Resources.barcode.AsBitmapImage();
- private readonly List<Tuple<Border, Label, ListBox, ColumnDefinition, int, CheckBox, Label>> 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<ManufacturingKanban>();
- PendingCheck.Tag = Pending;
- }
- public ObservableCollection<ManufacturingKanban> Kanbans { get; set; }
- public bool IsReady { get; set; }
- public void CreateToolbarButtons(IPanelHost host)
- {
- }
- public string SectionName => "Factory Allocation";
- public DataModel DataModel(Selection selection)
- {
- var ids = Packets != null ? Packets.Rows.Select(r => r.Get<ManufacturingPacket, Guid>(x => x.ID)).ToArray() : new Guid[] { };
- return new ManufacturingPacketDataModel(new Filter<ManufacturingPacket>(x => x.ID).InList(ids));
- }
- public void Refresh()
- {
- if (_section == Guid.Empty)
- {
- var sections = (Dictionary<Guid, string>)Sections.ItemsSource;
- _section = sections.Any() ? sections.First().Key : Guid.Empty;
- Sections.SelectedValue = _section;
- }
- ReloadPackets(true);
- }
- public Dictionary<string, object[]> Selected()
- {
- var result = new Dictionary<string, object[]>();
- return result;
- }
- public void Setup()
- {
- var settings = new UserConfiguration<ManufacturingAllocationSettings>().Load();
- var sections = new Dictionary<Guid, string>();
- ReloadSections();
- foreach (var row in _sections.Rows)
- sections[row.Get<ManufacturingSection, Guid>(x => x.ID)] = string.Format("{0}: {1}",
- row.Get<ManufacturingSection, string>(x => x.Factory.Name), row.Get<ManufacturingSection, string>(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()
- {
- }
- public event DataModelUpdateEvent OnUpdateDataModel;
- public void Heartbeat(TimeSpan time)
- {
- // Nothing to do here
- }
- public Dictionary<Type, CoreTable> DataEnvironment()
- {
- var result = new Dictionary<Type, CoreTable>();
- return result;
- }
- private BitmapImage GetBarCode(CoreRow packet)
- {
- if (!packet.Get<ManufacturingPacket, DateTime>(c => c.BarcodePrinted).IsEmpty())
- return packet.Get<ManufacturingPacket, BarcodeType>(c => c.BarcodeType) == BarcodeType.Grouped ? grouped : barcode;
- if (packet.Get<ManufacturingPacket, BarcodeType>(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<ManufacturingPacket, Guid>(x => x.ID);
- var priority = row.Get<ManufacturingPacket, bool>(c => c.Priority);
- var qty = row.Get<ManufacturingPacket, int>(c => c.Quantity);
- var barqty = row.Get<ManufacturingPacket, int>(c => c.BarcodeQty);
- var duedate = row.Get<ManufacturingPacket, DateTime>(c => c.DueDate);
- var estdate = row.Get<ManufacturingPacket, DateTime>(c => c.EstimatedDate);
-
- var sectionid = row.Get<ManufacturingPacket, Guid>(c => c.StageLink.SectionID);
- var pktsection = row.Get<ManufacturingPacket, string>(c => c.StageLink.Section);
- var stage = Stages.Rows.FirstOrDefault(r => r.Get<ManufacturingPacketStage, Guid>(c => c.Parent.ID).Equals(packetid));
- if (stage == null)
- return;
- var section = (Guid)Sections.SelectedValue;
- var station = stage.Get<ManufacturingPacketStage, int>(c => c.Station);
- var quality = stage.Get<ManufacturingPacketStage, QualityStatus>(c => c.QualityStatus);
- var percentage = stage.Get<ManufacturingPacketStage, double>(c => c.PercentageComplete);
- var Pending = station == 0;
- var model = new ManufacturingKanban();
- model.ID = row.Get<ManufacturingPacket, Guid>(c => c.ID).ToString();
- model.Title = string.Format("{0}: {1}{2}",
- row.Get<ManufacturingPacket, string>(c => c.Serial),
- qty != barqty ? string.Format("{0} x ", qty) : "",
- row.Get<ManufacturingPacket, string>(c => c.Title)
- );
- model.Quantity = barqty;
- model.JobName = string.Format("{0}: {1}",
- row.Get<ManufacturingPacket, string>(c => c.SetoutLink.Number),
- row.Get<ManufacturingPacket, string>(c => c.SetoutLink.JobLink.Name)
- );
- model.DueDate = duedate;
- var location = row.Get<ManufacturingPacket, string>(c => c.Location);
- var descrip = new List<string>
- {
- //row.Get<ManufacturingPacket,String>(c=>c.Level.Code),
- //row.Get<ManufacturingPacket,String>(c=>c.Zone.Code),
- string.IsNullOrEmpty(location) ? row.Get<ManufacturingPacket, string>(c => c.SetoutLink.Location) : location
- };
- model.Description = string.Join(" / ", descrip.Where(x => !string.IsNullOrWhiteSpace(x))).Trim();
- model.TemplateID = row.Get<ManufacturingPacket, Guid>(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<ManufacturingPacket, PurchaseOrderItemLink>(x => x.OrderItem, row) &&
- row.Get<ManufacturingPacket, DateTime>(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<ManufacturingPacket, Guid>(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<ManufacturingPacketStage, double>(c => c.PercentageComplete)) / 100.0F;
- model.Status = string.Format("({0:F2} hours)",
- qty * stage.Get<ManufacturingPacketStage, TimeSpan>(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<ManufacturingPacket, bool>(c => c.Distributed)
- ? !sectionid.Equals(section) ? string.IsNullOrEmpty(pktsection) ? "" : pktsection.ToUpper().Trim() : "DISTRIB"
- : "";
- model.GroupID = row.Get<ManufacturingPacket, Guid>(c => c.SetoutLink.Group.ID);
- model.GroupName = row.Get<ManufacturingPacket, string>(c => c.SetoutLink.Group.Name);
- 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<ManufacturingKanban> 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<ManufacturingPacket, Guid>(c => c.ID).Equals(id));
- var stage = Stages.Rows.FirstOrDefault(r => r.Get<ManufacturingPacketStage, Guid>(c => c.Parent.ID).Equals(id));
- var ratio = 1.0F / (stage.Get<ManufacturingPacketStage, int>(c => c.Station) == -1 ? (double)columns.Count : 1.0F);
- var percentleft = (100.0F - stage.Get<ManufacturingPacketStage, double>(c => c.PercentageComplete)) / 100.0F;
- hours += packet.Get<ManufacturingPacket, int>(c => c.Quantity) *
- stage.Get<ManufacturingPacketStage, TimeSpan>(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<ManufacturingPacketStage>().Query(
- new Filter<ManufacturingPacketStage>(x => x.ManufacturingSectionLink.ID).IsEqualTo(sectionid).And(x => x.Completed)
- .IsEqualTo(DateTime.MinValue),
- new Columns<ManufacturingPacketStage>(
- 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<ManufacturingPacketStage>()).ToArray();
- var filter = new Filter<ManufacturingPacket>(x => x.Completed).IsEqualTo(DateTime.MinValue)
- .And(x => x.Archived).IsEqualTo(DateTime.MinValue)
- .And(x => x.OnHold).IsEqualTo(false);
- var sectfilter = new Filter<ManufacturingPacket>(x => x.StageLink.SectionID).IsEqualTo(sectionid).Or(x => x.Distributed)
- .IsEqualTo(true);
- filter.Ands.Add(sectfilter);
- var columns = new Columns<ManufacturingPacket>();
- 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<ManufacturingPacket>().Query(
- filter,
- columns,
- //new Columns<ManufacturingPacket>(
- // 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<ManufacturingPacket>(x => x.Priority, SortDirection.Descending).ThenBy(x => x.SetoutLink.Number)
- );
- //Packets = table.Rows.Select(x => x.ToObject<ManufacturingPacket>()).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<ManufacturingSection>().Query(
- new Filter<ManufacturingSection>(x => x.Hidden).IsEqualTo(false),
- new Columns<ManufacturingSection>(
- x => x.ID,
- x => x.Factory.Name,
- x => x.Name,
- x => x.Stations
- ),
- new SortOrder<ManufacturingSection>(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<Guid, string>)e.AddedItems[0];
- var sectionid = pair.Key;
- ReloadColumns(sectionid);
- if (IsReady)
- new UserConfiguration<ManufacturingAllocationSettings>().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<ManufacturingHistory>().Query(
- new Filter<ManufacturingHistory>(x => x.Section.ID).IsEqualTo(sectionid).And(x => x.Date)
- .IsGreaterThanOrEqualTo(DateTime.Today.AddDays(-7)),
- new Columns<ManufacturingHistory>(
- x => x.Station,
- x => x.Employee.Name
- ),
- new SortOrder<ManufacturingHistory>(x => x.Station).ThenBy(x => x.LastUpdate)
- );
- var section = _sections.Rows.First(r => r.Get<ManufacturingSection, Guid>(x => x.ID).Equals(sectionid));
- var stations = section.Get<ManufacturingSection, int>(x => x.Stations);
- for (var iStation = 0; iStation < stations; iStation++)
- {
- var row = history.Rows.LastOrDefault(r => r.Get<ManufacturingHistory, int>(c => c.Station).Equals(iStation + 1));
- var empname = row != null ? row.Get<ManufacturingHistory, string>(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<CanViewFactorySettings>() ? 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<CanViewFactorySettings>() ? 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, ListBox, ColumnDefinition, int, CheckBox, Label>(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<ManufacturingSection>().Load(new Filter<ManufacturingSection>(x => x.ID).IsEqualTo(sectionid)).FirstOrDefault();
- if (section != null)
- {
- section.Stations += 1;
- new Client<ManufacturingSection>().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<ManufacturingSection>().Load(new Filter<ManufacturingSection>(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<ManufacturingKanban>;
- 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<ManufacturingSection>().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<CanDistributePackets>() ? 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<ManufacturingKanban> CheckedKanbans(ListBox listbox)
- {
- var kanbans = listbox.ItemsSource as IEnumerable<ManufacturingKanban>;
- var result = kanbans.Where(x => x.Checked);
- if (!result.Any())
- result = new[] { listbox.SelectedValue as ManufacturingKanban };
- return result;
- }
- private IEnumerable<ManufacturingPacket> CheckedPackets(ListBox listbox)
- {
- var kanbans = CheckedKanbans(listbox).Select(x => Guid.Parse(x.ID));
- var packets = Packets.Rows.Where(r => kanbans.Contains(r.Get<ManufacturingPacket, Guid>(c => c.ID)));
- return packets.Select(x => x.ToObject<ManufacturingPacket>());
- }
- private IEnumerable<ManufacturingPacketStage> CheckedStages(ListBox listbox)
- {
- var kanbans = CheckedKanbans(listbox).Select(x => Guid.Parse(x.ID));
- var selstages = Stages.Rows.Where(r =>
- kanbans.Contains(r.Get<ManufacturingPacketStage, Guid>(c => c.Parent.ID)) &&
- r.Get<ManufacturingPacketStage, Guid>(c => c.ManufacturingSectionLink.ID).Equals(_section));
- return selstages.Select(x => x.ToObject<ManufacturingPacketStage>());
- }
- private Tuple<Border, Label, ListBox, ColumnDefinition, int, CheckBox, Label> 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<ManufacturingPacketStage>().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<ManufacturingPacketStage>().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<ManufacturingPacket>().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<ManufacturingPacket>().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<ManufacturingPacketStage>().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<ManufacturingPacketStage>().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<ManufacturingKanban>;
- foreach (var kanban in kanbans)
- kanban.Checked = check.IsChecked == true;
- list.ItemsSource = null;
- list.ItemsSource = kanbans;
- }
- }
- }
|