FactoryProductivity.xaml.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Linq.Expressions;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using Comal.Classes;
  13. using InABox.Clients;
  14. using InABox.Configuration;
  15. using InABox.Core;
  16. using InABox.WPF;
  17. using InABox.Wpf;
  18. using Microsoft.Win32;
  19. using PRSDesktop.WidgetGroups;
  20. using Syncfusion.UI.Xaml.Charts;
  21. using Syncfusion.XlsIO;
  22. namespace PRSDesktop
  23. {
  24. public class FactoryProductivityDashboardProperties :IUserConfigurationSettings, IDashboardProperties
  25. {
  26. public string Test { get; set; } = "Untitled";
  27. }
  28. public class FactoryProductivityDashboardElement : DashboardElement<FactoryProductivityDashboard, Manufacturing, FactoryProductivityDashboardProperties> { }
  29. /// <summary>
  30. /// Interaction logic for FactoryProductivity.xaml
  31. /// </summary>
  32. public partial class FactoryProductivityDashboard : UserControl,
  33. IPanel<ManufacturingHistory>,
  34. IDashboardWidget<Manufacturing, FactoryProductivityDashboardProperties>
  35. {
  36. public enum HistoryView
  37. {
  38. Day,
  39. Week,
  40. Month,
  41. Year
  42. }
  43. public enum ItemsGroup
  44. {
  45. Packet,
  46. Setout,
  47. Job
  48. }
  49. public enum StaffGroup
  50. {
  51. Employee,
  52. Section
  53. }
  54. private readonly BitmapImage back = PRSDesktop.Resources.back.AsBitmapImage(32, 32);
  55. private HistoryViewModel history;
  56. private readonly BitmapImage next = PRSDesktop.Resources.next.AsBitmapImage(32, 32);
  57. public FactoryProductivityDashboard()
  58. {
  59. InitializeComponent();
  60. PrevDay.Content = new Image { Source = back };
  61. NextDay.Content = new Image { Source = next };
  62. }
  63. public bool IsReady { get; set; }
  64. public event DataModelUpdateEvent? OnUpdateDataModel;
  65. public void CreateToolbarButtons(IPanelHost host)
  66. {
  67. }
  68. public string SectionName => "Factory Productivity";
  69. public FactoryProductivityDashboardProperties Properties { get; set; }
  70. public event LoadSettings<FactoryProductivityDashboardProperties>? LoadSettings;
  71. public event SaveSettings<FactoryProductivityDashboardProperties>? SaveSettings;
  72. public DataModel DataModel(Selection selection)
  73. {
  74. if (history != null)
  75. {
  76. if (history.To == DateTime.MinValue)
  77. history.To = DateTime.Today;
  78. return new AutoDataModel<ManufacturingHistory>(history.GetFilter());
  79. }
  80. return new AutoDataModel<ManufacturingHistory>(new Filter<ManufacturingHistory>().None());
  81. }
  82. public void Refresh()
  83. {
  84. if (history.To == DateTime.MinValue)
  85. history.To = DateTime.Today;
  86. if (history.View == HistoryView.Day)
  87. CurrentDate.Text = string.Format("{0:ddd, dd MMMM yyyy}", history.To);
  88. else
  89. CurrentDate.Text = string.Format("{0:dd MMM yy} - {1:dd MMM yy}", history.From, history.To);
  90. //List<Color> colors = new List<Color>();
  91. //var props = typeof(Colors).GetProperties(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public).Where(x => x.PropertyType == typeof(Color));
  92. //foreach (var prop in props)
  93. //{
  94. // Color color = (Color)prop.GetValue(null);
  95. // if ((color != Colors.Transparent) && (color.R < color.G) && (color.R < color.B))
  96. // colors.Add(color);
  97. //}
  98. Hours.Series.Clear();
  99. foreach (var key in history.Items.Keys)
  100. {
  101. var comps = key.Split('|');
  102. var seriesdata = history.Items[key];
  103. var series = new StackingColumnSeries();
  104. series.ShowTooltip = true;
  105. series.TooltipTemplate = Hours.Resources["tooltipTemplate"] as DataTemplate;
  106. series.ItemsSource = seriesdata;
  107. series.XBindingPath = "Name";
  108. series.YBindingPath = "Hours";
  109. series.Label = comps[1];
  110. //int colorindex = Hours.Series.Count % colors.Count;
  111. //var color = colors[colorindex];
  112. var color = Colors.Transparent;
  113. try
  114. {
  115. color = (Color)ColorConverter.ConvertFromString(comps[0]);
  116. }
  117. catch (Exception e)
  118. {
  119. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  120. }
  121. series.Interior = new SolidColorBrush(color);
  122. series.Stroke = new SolidColorBrush(Colors.Black);
  123. series.StrokeThickness = 0.75F;
  124. series.BorderBrush = new SolidColorBrush(Colors.Black);
  125. series.BorderThickness = new Thickness(0.75F, 0.75F, 00.75F, 0.0F);
  126. series.MouseDoubleClick += (o, e) =>
  127. {
  128. foreach (var other in Hours.Series.Where(x => x != series))
  129. other.Visibility = other.Visibility == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
  130. };
  131. Hours.Series.Add(series);
  132. }
  133. }
  134. public Dictionary<string, object[]> Selected()
  135. {
  136. return new Dictionary<string, object[]>();
  137. }
  138. public void Setup()
  139. {
  140. history = new HistoryViewModel();
  141. DataContext = history;
  142. var legends = new ChartLegendCollection();
  143. Hours.Legend = new ChartLegend
  144. {
  145. LegendPosition = LegendPosition.Outside,
  146. DockPosition = ChartDock.Top,
  147. //FontSize = 11,
  148. ItemsPanel = Hours.Resources["itemPanelTemplate"] as ItemsPanelTemplate,
  149. CheckBoxVisibility = Visibility.Visible,
  150. ToggleSeriesVisibility = true
  151. };
  152. }
  153. public void Shutdown(CancelEventArgs? cancel)
  154. {
  155. }
  156. public void Heartbeat(TimeSpan time)
  157. {
  158. }
  159. private void CurrentDate_MouseDoubleClick(object sender, MouseButtonEventArgs e)
  160. {
  161. history.To = DateTime.Today;
  162. Refresh();
  163. }
  164. private void PrevDay_Click(object sender, RoutedEventArgs e)
  165. {
  166. history.Back();
  167. Refresh();
  168. }
  169. private void NextDay_Click(object sender, RoutedEventArgs e)
  170. {
  171. history.Forward();
  172. Refresh();
  173. }
  174. private void ViewStyle_SelectionChanged(object sender, SelectionChangedEventArgs e)
  175. {
  176. if (history == null)
  177. return;
  178. history.View = ViewStyle.SelectedIndex == 0 ? HistoryView.Day :
  179. ViewStyle.SelectedIndex == 1 ? HistoryView.Week :
  180. ViewStyle.SelectedIndex == 2 ? HistoryView.Month : HistoryView.Year;
  181. Refresh();
  182. }
  183. private void StaffView_SelectionChanged(object sender, SelectionChangedEventArgs e)
  184. {
  185. if (history == null)
  186. return;
  187. history.StaffGroup = StaffView.SelectedIndex == 0 ? StaffGroup.Employee : StaffGroup.Section;
  188. Refresh();
  189. }
  190. private void ItemsView_SelectionChanged(object sender, SelectionChangedEventArgs e)
  191. {
  192. if (history == null)
  193. return;
  194. history.ItemsGroup = ItemsView.SelectedIndex == 0 ? ItemsGroup.Packet : ItemsView.SelectedIndex == 1 ? ItemsGroup.Setout : ItemsGroup.Job;
  195. Refresh();
  196. }
  197. private void Search_KeyUp(object sender, KeyEventArgs e)
  198. {
  199. if (string.IsNullOrWhiteSpace(Search.Text) || e.Key == Key.Return)
  200. {
  201. history.Search = Search.Text;
  202. Refresh();
  203. }
  204. }
  205. private void Export_Click(object sender, RoutedEventArgs e)
  206. {
  207. var excelEngine = new ExcelEngine();
  208. var application = excelEngine.Excel;
  209. var myWorkbook = excelEngine.Excel.Workbooks.Add();
  210. myWorkbook.Version = ExcelVersion.Excel2007;
  211. var mySheet = myWorkbook.Worksheets[0];
  212. var iCol = 2;
  213. foreach (var key in history.Items.Keys)
  214. {
  215. mySheet.Range[1, iCol].Text = key;
  216. var records = history.Items[key];
  217. var iRow = 2;
  218. foreach (var record in records)
  219. {
  220. mySheet.Range[iRow, 1].Text = record.Name;
  221. mySheet.Range[iRow, iCol].Number = Math.Truncate(record.Hours * 100.0F) / 100.0F;
  222. iRow++;
  223. }
  224. iCol++;
  225. }
  226. mySheet.UsedRange.AutofitColumns();
  227. foreach (var col in mySheet.UsedRange.Columns)
  228. col.ColumnWidth += 5;
  229. foreach (var row in mySheet.UsedRange.Rows)
  230. {
  231. row.RowHeight += 5;
  232. row.VerticalAlignment = ExcelVAlign.VAlignCenter;
  233. }
  234. var dlg = new SaveFileDialog();
  235. dlg.Filter = "Excel Files (*.xlsx)|*.xlsx";
  236. dlg.FileName = "Factory KPI Screen.xlsx";
  237. if (dlg.ShowDialog() == true)
  238. try
  239. {
  240. myWorkbook.SaveAs(dlg.FileName, ExcelSaveType.SaveAsXLS);
  241. Process.Start(new ProcessStartInfo(dlg.FileName) { UseShellExecute = true });
  242. }
  243. catch (Exception e2)
  244. {
  245. MessageBox.Show("Error saving spreadhseet!\n\n" + e2.Message);
  246. }
  247. }
  248. public class History
  249. {
  250. public string Name { get; set; }
  251. //public int QAPackets { get; set; }
  252. public int Packets { get; set; }
  253. //public double QAHours { get; set; }
  254. public double Hours { get; set; }
  255. public double LostTime { get; set; }
  256. }
  257. public class HistoryViewModel
  258. {
  259. private ItemsGroup _itemsgroup = ItemsGroup.Job;
  260. private string _search = "";
  261. private StaffGroup _staffgroup = StaffGroup.Employee;
  262. private DateTime _to = DateTime.MinValue;
  263. private HistoryView _view = HistoryView.Day;
  264. public HistoryViewModel()
  265. {
  266. Staff = new List<History>();
  267. Items = new Dictionary<string, List<History>>();
  268. }
  269. public StaffGroup StaffGroup
  270. {
  271. get => _staffgroup;
  272. set
  273. {
  274. _staffgroup = value;
  275. Refresh();
  276. }
  277. }
  278. public List<History> Staff { get; }
  279. public ItemsGroup ItemsGroup
  280. {
  281. get => _itemsgroup;
  282. set
  283. {
  284. _itemsgroup = value;
  285. Refresh();
  286. }
  287. }
  288. // Key = series
  289. public Dictionary<string, List<History>> Items { get; set; }
  290. public HistoryView View
  291. {
  292. get => _view;
  293. set
  294. {
  295. _view = value;
  296. Refresh();
  297. }
  298. }
  299. public DateTime To
  300. {
  301. get => _to.Date;
  302. set
  303. {
  304. _to = value;
  305. Refresh();
  306. }
  307. }
  308. public DateTime From => _view.Equals(HistoryView.Day) ? _to :
  309. _view.Equals(HistoryView.Week) ? _to.AddDays(-6) :
  310. _view.Equals(HistoryView.Month) ? _to.AddMonths(-1).AddDays(1) : _to.AddYears(-1).AddDays(1);
  311. public string Search
  312. {
  313. get => _search;
  314. set
  315. {
  316. _search = value;
  317. Refresh();
  318. }
  319. }
  320. public void Forward()
  321. {
  322. To = _view.Equals(HistoryView.Day) ? _to.AddDays(1) :
  323. _view.Equals(HistoryView.Week) ? _to.AddDays(7) :
  324. _view.Equals(HistoryView.Month) ? _to.AddMonths(1) : _to.AddYears(1);
  325. }
  326. public void Back()
  327. {
  328. To = _view.Equals(HistoryView.Day) ? _to.AddDays(-1) :
  329. _view.Equals(HistoryView.Week) ? _to.AddDays(-7) :
  330. _view.Equals(HistoryView.Month) ? _to.AddMonths(-1) : _to.AddYears(-1);
  331. }
  332. private void AddSearchCriteria(Filter<ManufacturingHistory> filter, string search,
  333. params Expression<Func<ManufacturingHistory, object>>[] expressions)
  334. {
  335. var comps = search.Trim().Split(' ');
  336. foreach (var comp in comps)
  337. {
  338. Filter<ManufacturingHistory> result = null;
  339. foreach (var expression in expressions)
  340. result = result == null ? new Filter<ManufacturingHistory>(expression).Contains(comp) : result.Or(expression).Contains(comp);
  341. filter.Ands.Add(result);
  342. }
  343. }
  344. public Filter<ManufacturingHistory> GetFilter()
  345. {
  346. var from = _view.Equals(HistoryView.Day) ? _to.AddDays(-1) :
  347. _view.Equals(HistoryView.Week) ? _to.AddDays(-7) :
  348. _view.Equals(HistoryView.Month) ? _to.AddMonths(-1) : _to.AddYears(-1);
  349. var criteria = new Filter<ManufacturingHistory>(x => x.Date).IsGreaterThan(from).And(x => x.Date).IsLessThanOrEqualTo(_to);
  350. if (!string.IsNullOrWhiteSpace(_search))
  351. {
  352. criteria = criteria.TextSearch(
  353. Search,
  354. x => x.Packet.SetoutLink.JobLink.JobNumber,
  355. x => x.Packet.SetoutLink.JobLink.Name,
  356. x => x.Packet.SetoutLink.Number,
  357. x => x.Packet.SetoutLink.Description,
  358. //x => x.Packet.SetoutLink.Reference,
  359. x => x.Packet.SetoutLink.Location,
  360. x => x.Packet.Serial,
  361. x => x.Packet.Location,
  362. x => x.Packet.Title,
  363. x => x.LostTime.Description
  364. );
  365. }
  366. return criteria;
  367. }
  368. private void Refresh()
  369. {
  370. var criteria = GetFilter();
  371. var data = new Client<ManufacturingHistory>().Query(
  372. criteria,
  373. null,
  374. new SortOrder<ManufacturingHistory>(x => x.Employee.Name)
  375. );
  376. Staff.Clear();
  377. foreach (var row in data.Rows)
  378. {
  379. var staff = _staffgroup.Equals(StaffGroup.Employee)
  380. ? row.Get<ManufacturingHistory, string>(x => x.Employee.Name)
  381. : row.Get<ManufacturingHistory, string>(x => x.Section.Name);
  382. var record = Staff.FirstOrDefault(x => string.Equals(x.Name, staff));
  383. if (record == null)
  384. {
  385. record = new History { Name = staff };
  386. Staff.Add(record);
  387. }
  388. var hrs = row.Get<ManufacturingHistory, TimeSpan>(x => x.QADuration).TotalHours +
  389. row.Get<ManufacturingHistory, TimeSpan>(x => x.WorkDuration).TotalHours;
  390. if (Entity.IsEntityLinkValid<ManufacturingHistory, ManufacturingPacketLink>(x => x.Packet, row))
  391. record.Hours += hrs;
  392. else
  393. record.LostTime += hrs;
  394. record.Packets += row.Get<ManufacturingHistory, int>(x => x.WorkCompleted);
  395. }
  396. Items.Clear();
  397. foreach (var row in data.Rows)
  398. {
  399. var staff = _staffgroup.Equals(StaffGroup.Employee)
  400. ? row.Get<ManufacturingHistory, string>(x => x.Employee.Name)
  401. : row.Get<ManufacturingHistory, string>(x => x.Section.Name);
  402. var packet = "";
  403. if (row.IsEntityLinkValid<ManufacturingHistory, ManufacturingPacketLink>(x => x.Packet))
  404. packet = row.Get<ManufacturingHistory, string>(x => x.Packet.SetoutLink.JobLink.Color) + "|" +
  405. (_itemsgroup.Equals(ItemsGroup.Packet) ? row.Get<ManufacturingHistory, string>(x => x.Packet.Serial) :
  406. _itemsgroup.Equals(ItemsGroup.Setout) ? row.Get<ManufacturingHistory, string>(x => x.Packet.SetoutLink.Number) :
  407. row.Get<ManufacturingHistory, string>(x =>
  408. x.Packet.SetoutLink.JobLink.JobNumber)); //row.Get<ManufacturingHistory, int>(x => x.Job.Number).ToString();
  409. else if (row.IsEntityLinkValid<ManufacturingHistory, ManufacturingLostTimeLink>(x => x.LostTime))
  410. packet = "#FF00FF00|" + row.Get<ManufacturingHistory, string>(x => x.LostTime.Description);
  411. else
  412. packet = "#FFFF0000|Lost Time";
  413. if (!Items.ContainsKey(packet))
  414. {
  415. var records = new List<History>();
  416. foreach (var _staff in Staff)
  417. records.Add(new History { Name = _staff.Name });
  418. Items[packet] = records;
  419. }
  420. var record = Items[packet].FirstOrDefault(x => string.Equals(x.Name, staff));
  421. record.Packets = record.Packets + row.Get<ManufacturingHistory, int>(x => x.WorkCompleted);
  422. record.Hours = record.Hours + row.Get<ManufacturingHistory, TimeSpan>(x => x.QADuration).TotalHours +
  423. row.Get<ManufacturingHistory, TimeSpan>(x => x.WorkDuration).TotalHours;
  424. }
  425. }
  426. }
  427. }
  428. }