FactoryProductivity.xaml.cs 18 KB

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