123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Linq.Expressions;
- 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.Core;
- using InABox.WPF;
- using Microsoft.Win32;
- using PRSDesktop.WidgetGroups;
- using Syncfusion.UI.Xaml.Charts;
- using Syncfusion.XlsIO;
- namespace PRSDesktop
- {
- public class FactoryProductivityDashboardProperties : IDashboardProperties
- {
- public string Test { get; set; } = "Untitled";
- }
- public class FactoryProductivityDashboardElement : DashboardElement<FactoryProductivityDashboard, Manufacturing, FactoryProductivityDashboardProperties> { }
- /// <summary>
- /// Interaction logic for FactoryProductivity.xaml
- /// </summary>
- public partial class FactoryProductivityDashboard : UserControl,
- IPanel<ManufacturingHistory>,
- IDashboardWidget<Manufacturing, FactoryProductivityDashboardProperties>
- {
- public enum HistoryView
- {
- Day,
- Week,
- Month,
- Year
- }
- public enum ItemsGroup
- {
- Packet,
- Setout,
- Job
- }
- public enum StaffGroup
- {
- Employee,
- Section
- }
- private readonly BitmapImage back = PRSDesktop.Resources.back.AsBitmapImage(32, 32);
- private HistoryViewModel history;
- private readonly BitmapImage next = PRSDesktop.Resources.next.AsBitmapImage(32, 32);
- public FactoryProductivityDashboard()
- {
- InitializeComponent();
- PrevDay.Content = new Image { Source = back };
- NextDay.Content = new Image { Source = next };
- }
- public bool IsReady { get; set; }
- public event DataModelUpdateEvent OnUpdateDataModel;
- public void CreateToolbarButtons(IPanelHost host)
- {
- }
- public string SectionName => "Factory Productivity";
- public FactoryProductivityDashboardProperties Properties { get; set; }
- public DataModel DataModel(Selection selection)
- {
- if (history.To == DateTime.MinValue)
- history.To = DateTime.Today;
- return new AutoDataModel<ManufacturingHistory>(history.GetFilter());
- }
- public void Refresh()
- {
- if (history.To == DateTime.MinValue)
- history.To = DateTime.Today;
- if (history.View == HistoryView.Day)
- CurrentDate.Text = string.Format("{0:ddd, dd MMMM yyyy}", history.To);
- else
- CurrentDate.Text = string.Format("{0:dd MMM yy} - {1:dd MMM yy}", history.From, history.To);
- //List<Color> colors = new List<Color>();
- //var props = typeof(Colors).GetProperties(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public).Where(x => x.PropertyType == typeof(Color));
- //foreach (var prop in props)
- //{
- // Color color = (Color)prop.GetValue(null);
- // if ((color != Colors.Transparent) && (color.R < color.G) && (color.R < color.B))
- // colors.Add(color);
- //}
- Hours.Series.Clear();
- foreach (var key in history.Items.Keys)
- {
- var comps = key.Split('|');
- var seriesdata = history.Items[key];
- var series = new StackingColumnSeries();
- series.ShowTooltip = true;
- series.TooltipTemplate = Hours.Resources["tooltipTemplate"] as DataTemplate;
- series.ItemsSource = seriesdata;
- series.XBindingPath = "Name";
- series.YBindingPath = "Hours";
- series.Label = comps[1];
- //int colorindex = Hours.Series.Count % colors.Count;
- //var color = colors[colorindex];
- var color = Colors.Transparent;
- try
- {
- color = (Color)ColorConverter.ConvertFromString(comps[0]);
- }
- catch (Exception e)
- {
- Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
- }
- series.Interior = new SolidColorBrush(color);
- series.Stroke = new SolidColorBrush(Colors.Black);
- series.StrokeThickness = 0.75F;
- series.BorderBrush = new SolidColorBrush(Colors.Black);
- series.BorderThickness = new Thickness(0.75F, 0.75F, 00.75F, 0.0F);
- series.MouseDoubleClick += (o, e) =>
- {
- foreach (var other in Hours.Series.Where(x => x != series))
- other.Visibility = other.Visibility == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
- };
- Hours.Series.Add(series);
- }
- }
- public Dictionary<string, object[]> Selected()
- {
- return new Dictionary<string, object[]>();
- }
- public void Setup()
- {
- history = new HistoryViewModel();
- DataContext = history;
- var legends = new ChartLegendCollection();
- Hours.Legend = new ChartLegend
- {
- LegendPosition = LegendPosition.Outside,
- DockPosition = ChartDock.Top,
- //FontSize = 11,
- ItemsPanel = Hours.Resources["itemPanelTemplate"] as ItemsPanelTemplate,
- CheckBoxVisibility = Visibility.Visible,
- ToggleSeriesVisibility = true
- };
- }
- public void Shutdown()
- {
- }
- public void Heartbeat(TimeSpan time)
- {
- }
- private void CurrentDate_MouseDoubleClick(object sender, MouseButtonEventArgs e)
- {
- history.To = DateTime.Today;
- Refresh();
- }
- private void PrevDay_Click(object sender, RoutedEventArgs e)
- {
- history.Back();
- Refresh();
- }
- private void NextDay_Click(object sender, RoutedEventArgs e)
- {
- history.Forward();
- Refresh();
- }
- private void ViewStyle_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (history == null)
- return;
- history.View = ViewStyle.SelectedIndex == 0 ? HistoryView.Day :
- ViewStyle.SelectedIndex == 1 ? HistoryView.Week :
- ViewStyle.SelectedIndex == 2 ? HistoryView.Month : HistoryView.Year;
- Refresh();
- }
- private void StaffView_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (history == null)
- return;
- history.StaffGroup = StaffView.SelectedIndex == 0 ? StaffGroup.Employee : StaffGroup.Section;
- Refresh();
- }
- private void ItemsView_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (history == null)
- return;
- history.ItemsGroup = ItemsView.SelectedIndex == 0 ? ItemsGroup.Packet : ItemsView.SelectedIndex == 1 ? ItemsGroup.Setout : ItemsGroup.Job;
- Refresh();
- }
- private void Search_KeyUp(object sender, KeyEventArgs e)
- {
- if (string.IsNullOrWhiteSpace(Search.Text) || e.Key == Key.Return)
- {
- history.Search = Search.Text;
- Refresh();
- }
- }
- private void Export_Click(object sender, RoutedEventArgs e)
- {
- var excelEngine = new ExcelEngine();
- var application = excelEngine.Excel;
- var myWorkbook = excelEngine.Excel.Workbooks.Add();
- myWorkbook.Version = ExcelVersion.Excel2007;
- var mySheet = myWorkbook.Worksheets[0];
- var iCol = 2;
- foreach (var key in history.Items.Keys)
- {
- mySheet.Range[1, iCol].Text = key;
- var records = history.Items[key];
- var iRow = 2;
- foreach (var record in records)
- {
- mySheet.Range[iRow, 1].Text = record.Name;
- mySheet.Range[iRow, iCol].Number = Math.Truncate(record.Hours * 100.0F) / 100.0F;
- iRow++;
- }
- iCol++;
- }
- mySheet.UsedRange.AutofitColumns();
- foreach (var col in mySheet.UsedRange.Columns)
- col.ColumnWidth += 5;
- foreach (var row in mySheet.UsedRange.Rows)
- {
- row.RowHeight += 5;
- row.VerticalAlignment = ExcelVAlign.VAlignCenter;
- }
- var dlg = new SaveFileDialog();
- dlg.Filter = "Excel Files (*.xlsx)|*.xlsx";
- dlg.FileName = "Factory KPI Screen.xlsx";
- if (dlg.ShowDialog() == true)
- try
- {
- myWorkbook.SaveAs(dlg.FileName, ExcelSaveType.SaveAsXLS);
- Process.Start(new ProcessStartInfo(dlg.FileName) { UseShellExecute = true });
- }
- catch (Exception e2)
- {
- MessageBox.Show("Error saving spreadhseet!\n\n" + e2.Message);
- }
- }
- public class History
- {
- public string Name { get; set; }
- //public int QAPackets { get; set; }
- public int Packets { get; set; }
- //public double QAHours { get; set; }
- public double Hours { get; set; }
- public double LostTime { get; set; }
- }
- public class HistoryViewModel
- {
- private ItemsGroup _itemsgroup = ItemsGroup.Job;
- private string _search = "";
- private StaffGroup _staffgroup = StaffGroup.Employee;
- private DateTime _to = DateTime.MinValue;
- private HistoryView _view = HistoryView.Day;
- public HistoryViewModel()
- {
- Staff = new List<History>();
- Items = new Dictionary<string, List<History>>();
- }
- public StaffGroup StaffGroup
- {
- get => _staffgroup;
- set
- {
- _staffgroup = value;
- Refresh();
- }
- }
- public List<History> Staff { get; }
- public ItemsGroup ItemsGroup
- {
- get => _itemsgroup;
- set
- {
- _itemsgroup = value;
- Refresh();
- }
- }
- // Key = series
- public Dictionary<string, List<History>> Items { get; set; }
- public HistoryView View
- {
- get => _view;
- set
- {
- _view = value;
- Refresh();
- }
- }
- public DateTime To
- {
- get => _to.Date;
- set
- {
- _to = value;
- Refresh();
- }
- }
- public DateTime From => _view.Equals(HistoryView.Day) ? _to :
- _view.Equals(HistoryView.Week) ? _to.AddDays(-6) :
- _view.Equals(HistoryView.Month) ? _to.AddMonths(-1).AddDays(1) : _to.AddYears(-1).AddDays(1);
- public string Search
- {
- get => _search;
- set
- {
- _search = value;
- Refresh();
- }
- }
- public void Forward()
- {
- To = _view.Equals(HistoryView.Day) ? _to.AddDays(1) :
- _view.Equals(HistoryView.Week) ? _to.AddDays(7) :
- _view.Equals(HistoryView.Month) ? _to.AddMonths(1) : _to.AddYears(1);
- }
- public void Back()
- {
- To = _view.Equals(HistoryView.Day) ? _to.AddDays(-1) :
- _view.Equals(HistoryView.Week) ? _to.AddDays(-7) :
- _view.Equals(HistoryView.Month) ? _to.AddMonths(-1) : _to.AddYears(-1);
- }
- private void AddSearchCriteria(Filter<ManufacturingHistory> filter, string search,
- params Expression<Func<ManufacturingHistory, object>>[] expressions)
- {
- var comps = search.Trim().Split(' ');
- foreach (var comp in comps)
- {
- Filter<ManufacturingHistory> result = null;
- foreach (var expression in expressions)
- result = result == null ? new Filter<ManufacturingHistory>(expression).Contains(comp) : result.Or(expression).Contains(comp);
- filter.Ands.Add(result);
- }
- }
- public Filter<ManufacturingHistory> GetFilter()
- {
- var from = _view.Equals(HistoryView.Day) ? _to.AddDays(-1) :
- _view.Equals(HistoryView.Week) ? _to.AddDays(-7) :
- _view.Equals(HistoryView.Month) ? _to.AddMonths(-1) : _to.AddYears(-1);
- var criteria = new Filter<ManufacturingHistory>(x => x.Date).IsGreaterThan(from).And(x => x.Date).IsLessThanOrEqualTo(_to);
- if (!string.IsNullOrWhiteSpace(_search))
- {
- criteria = criteria.TextSearch(
- Search,
- x => x.Packet.SetoutLink.JobLink.JobNumber,
- x => x.Packet.SetoutLink.JobLink.Name,
- x => x.Packet.SetoutLink.Number,
- x => x.Packet.SetoutLink.Description,
- //x => x.Packet.SetoutLink.Reference,
- x => x.Packet.SetoutLink.Location,
- x => x.Packet.Serial,
- x => x.Packet.Location,
- x => x.Packet.Title,
- x => x.LostTime.Description
- );
- }
- return criteria;
- }
- private void Refresh()
- {
- var criteria = GetFilter();
- var data = new Client<ManufacturingHistory>().Query(
- criteria,
- null,
- new SortOrder<ManufacturingHistory>(x => x.Employee.Name)
- );
- Staff.Clear();
- foreach (var row in data.Rows)
- {
- var staff = _staffgroup.Equals(StaffGroup.Employee)
- ? row.Get<ManufacturingHistory, string>(x => x.Employee.Name)
- : row.Get<ManufacturingHistory, string>(x => x.Section.Name);
- var record = Staff.FirstOrDefault(x => string.Equals(x.Name, staff));
- if (record == null)
- {
- record = new History { Name = staff };
- Staff.Add(record);
- }
- var hrs = row.Get<ManufacturingHistory, TimeSpan>(x => x.QADuration).TotalHours +
- row.Get<ManufacturingHistory, TimeSpan>(x => x.WorkDuration).TotalHours;
- if (Entity.IsEntityLinkValid<ManufacturingHistory, ManufacturingPacketLink>(x => x.Packet, row))
- record.Hours += hrs;
- else
- record.LostTime += hrs;
- record.Packets += row.Get<ManufacturingHistory, int>(x => x.WorkCompleted);
- }
- Items.Clear();
- foreach (var row in data.Rows)
- {
- var staff = _staffgroup.Equals(StaffGroup.Employee)
- ? row.Get<ManufacturingHistory, string>(x => x.Employee.Name)
- : row.Get<ManufacturingHistory, string>(x => x.Section.Name);
- var packet = "";
- if (row.IsEntityLinkValid<ManufacturingHistory, ManufacturingPacketLink>(x => x.Packet))
- packet = row.Get<ManufacturingHistory, string>(x => x.Packet.SetoutLink.JobLink.Color) + "|" +
- (_itemsgroup.Equals(ItemsGroup.Packet) ? row.Get<ManufacturingHistory, string>(x => x.Packet.Serial) :
- _itemsgroup.Equals(ItemsGroup.Setout) ? row.Get<ManufacturingHistory, string>(x => x.Packet.SetoutLink.Number) :
- row.Get<ManufacturingHistory, string>(x =>
- x.Packet.SetoutLink.JobLink.JobNumber)); //row.Get<ManufacturingHistory, int>(x => x.Job.Number).ToString();
- else if (row.IsEntityLinkValid<ManufacturingHistory, ManufacturingLostTimeLink>(x => x.LostTime))
- packet = "#FF00FF00|" + row.Get<ManufacturingHistory, string>(x => x.LostTime.Description);
- else
- packet = "#FFFF0000|Lost Time";
- if (!Items.ContainsKey(packet))
- {
- var records = new List<History>();
- foreach (var _staff in Staff)
- records.Add(new History { Name = _staff.Name });
- Items[packet] = records;
- }
- var record = Items[packet].FirstOrDefault(x => string.Equals(x.Name, staff));
- record.Packets = record.Packets + row.Get<ManufacturingHistory, int>(x => x.WorkCompleted);
- record.Hours = record.Hours + row.Get<ManufacturingHistory, TimeSpan>(x => x.QADuration).TotalHours +
- row.Get<ManufacturingHistory, TimeSpan>(x => x.WorkDuration).TotalHours;
- }
- }
- }
- }
- }
|