FactoryProductivity.xaml.cs 18 KB

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