ManufacturingTemplateAnalysis.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Input;
  10. using Comal.Classes;
  11. using InABox.Clients;
  12. using InABox.Configuration;
  13. using InABox.Core;
  14. using InABox.WPF;
  15. using PRSDesktop.WidgetGroups;
  16. using Syncfusion.UI.Xaml.Grid;
  17. using Syncfusion.UI.Xaml.Grid.Cells;
  18. using SelectionChangedEventArgs = System.Windows.Controls.SelectionChangedEventArgs;
  19. namespace PRSDesktop
  20. {
  21. public class ManufacturingTemplateAnalysisProperties : IUserConfigurationSettings, IDashboardProperties { }
  22. public class ManufacturingTemplateAnalysisElement : DashboardElement<ManufacturingTemplateAnalysis, Manufacturing, ManufacturingTemplateAnalysisProperties> { }
  23. /// <summary>
  24. /// Interaction logic for ManufacturingTemplateAnalysis.xaml
  25. /// </summary>
  26. public partial class ManufacturingTemplateAnalysis : UserControl, IPanel<ManufacturingTemplate>, IDashboardWidget<Manufacturing, ManufacturingTemplateAnalysisProperties>
  27. {
  28. private bool _activeonly;
  29. private DateTime _from;
  30. private string _search = "";
  31. private DateTime _to;
  32. private DataTable data;
  33. private readonly Dictionary<string, string> SectionDisplayNames = new() { { "Total", "Total" } };
  34. private ManufacturingSection[] sections;
  35. private Dictionary<Guid, ManufacturingTemplateStage[]> stages;
  36. private ManufacturingTemplate[] templates;
  37. public ManufacturingTemplateAnalysis()
  38. {
  39. _from = DateTime.Today.AddDays(0 - WeekDay(DateTime.Today));
  40. _to = DateTime.Today;
  41. _activeonly = true;
  42. InitializeComponent();
  43. dataGrid.CellRenderers.Remove("StackedHeader");
  44. dataGrid.CellRenderers.Add("StackedHeader", new GridCustomStackedRenderer(Resources));
  45. }
  46. public bool IsReady { get; set; }
  47. public void CreateToolbarButtons(IPanelHost host)
  48. {
  49. }
  50. public string SectionName => "Manufacturing Template Analysis";
  51. public ManufacturingTemplateAnalysisProperties Properties { get; set; }
  52. public event LoadSettings<ManufacturingTemplateAnalysisProperties>? LoadSettings;
  53. public event SaveSettings<ManufacturingTemplateAnalysisProperties>? SaveSettings;
  54. public DataModel DataModel(Selection selection)
  55. {
  56. return new AutoDataModel<ManufacturingTemplate>(null);
  57. }
  58. public void Refresh()
  59. {
  60. Progress.ShowModal("Loading Data", (progress) =>
  61. {
  62. data.Rows.Clear();
  63. MultiQuery query = new MultiQuery();
  64. query.Add(
  65. new Filter<ManufacturingTemplate>(x => x.ID).IsNotEqualTo(Guid.Empty).TextSearch(_search, x => x.Code, x => x.Name),
  66. new Columns<ManufacturingTemplate>(x => x.ID)
  67. .Add(x => x.Name)
  68. .Add(x => x.Code),
  69. new SortOrder<ManufacturingTemplate>(x => x.Code)
  70. );
  71. query.Add(
  72. new Filter<ManufacturingPacket>(x => x.Completed).IsGreaterThanOrEqualTo(_from).And(x => x.Completed).IsLessThan(_to.AddDays(1)),
  73. new Columns<ManufacturingPacket>
  74. (
  75. x => x.ID,
  76. x => x.ManufacturingTemplateLink.ID,
  77. x => x.Quantity
  78. )
  79. );
  80. query.Add(
  81. new Filter<ManufacturingHistory>(x => x.Packet.Completed).IsGreaterThanOrEqualTo(_from).And(x => x.Packet.Completed)
  82. .IsLessThan(_to.AddDays(1)),
  83. new Columns<ManufacturingHistory>
  84. (
  85. x => x.Packet.ID,
  86. x => x.Section.ID,
  87. x => x.QADuration,
  88. x => x.WorkDuration
  89. )
  90. );
  91. query.Query();
  92. templates = query.Get<ManufacturingTemplate>().ToObjects<ManufacturingTemplate>().ToArray();
  93. var packets = query.Get<ManufacturingPacket>().ToObjects<ManufacturingPacket>();
  94. ;
  95. var history = query.Get<ManufacturingHistory>().ToObjects<ManufacturingHistory>()
  96. .GroupBy(x => x.Packet.ID)
  97. .ToDictionary(x => x.Key, x => x.ToList());
  98. foreach (var template in templates)
  99. {
  100. progress.Report(string.Format("Processing {0}: {1}", template.Code, template.Name));
  101. var templateStageSections = (stages.GetValueOrDefault(template.ID) ?? Enumerable.Empty<ManufacturingTemplateStage>())
  102. .Select(x => x.Section.ID).Where(x => sections.Any(y => x == y.ID));
  103. var values = new List<object>();
  104. values.Add(template.Code);
  105. values.Add(template.Name);
  106. var times = new Dictionary<Guid, long>();
  107. foreach (var section in sections)
  108. times[section.ID] = 0L;
  109. var qty = 0;
  110. var pkts = packets.Where(p => p.ManufacturingTemplateLink.ID.Equals(template.ID));
  111. foreach (var p in pkts)
  112. {
  113. var thisqty = p.Quantity;
  114. if (thisqty > 0)
  115. {
  116. qty += thisqty;
  117. if (history.TryGetValue(p.ID, out var histories))
  118. {
  119. foreach (var section in templateStageSections)
  120. foreach (var hist in histories.Where(x => x.Section.ID == section))
  121. times[section] += hist.QADuration.Ticks + hist.WorkDuration.Ticks;
  122. }
  123. }
  124. }
  125. if (qty > 0)
  126. values.Add(qty);
  127. else
  128. values.Add(null);
  129. var total = 0L;
  130. foreach (var section in sections)
  131. {
  132. total += times[section.ID];
  133. if (qty > 0 && times[section.ID] > 0)
  134. values.Add(Math.Truncate(new TimeSpan(times[section.ID] / qty).TotalHours * 100.0F) / 100.0F);
  135. else
  136. values.Add(null);
  137. }
  138. if (qty > 0 && total > 0)
  139. values.Add(Math.Truncate(new TimeSpan(total / qty).TotalHours * 100.0F) / 100.0F);
  140. else
  141. values.Add(null);
  142. if (!_activeonly || qty > 0)
  143. data.Rows.Add(values.ToArray());
  144. }
  145. });
  146. }
  147. public Dictionary<string, object[]> Selected()
  148. {
  149. return new Dictionary<string, object[]>();
  150. }
  151. public void Setup()
  152. {
  153. FromDate.SelectedDate = _from;
  154. ToDate.SelectedDate = _to;
  155. dataGrid.ScrollMode = ScrollMode.Async;
  156. sections = new Client<ManufacturingSection>().Load(
  157. new Filter<ManufacturingSection>(x => x.Hidden).IsEqualTo(false),
  158. new SortOrder<ManufacturingSection>(x => x.Factory.Sequence).ThenBy(x => x.Sequence)
  159. );
  160. stages = new Client<ManufacturingTemplateStage>().Load().GroupBy(x => x.Template.ID).ToDictionary(x => x.Key, x => x.ToArray());
  161. data = new DataTable();
  162. data.Columns.Add("Code", typeof(string));
  163. data.Columns.Add("Description", typeof(string));
  164. data.Columns.Add("Qty", typeof(int));
  165. var columns = new Dictionary<string, List<string>>();
  166. foreach (var section in sections)
  167. {
  168. if (!columns.ContainsKey(section.Factory.Name))
  169. columns[section.Factory.Name] = new List<string>();
  170. var columnname = string.Format("{0}:{1}", section.Factory.Name, Regex.Replace(section.Name, "[^a-zA-Z0-9]", string.Empty));
  171. columns[section.Factory.Name].Add(columnname);
  172. data.Columns.Add(columnname, typeof(double));
  173. SectionDisplayNames[columnname] = section.Name;
  174. }
  175. data.Columns.Add("Total", typeof(double));
  176. dataGrid.ItemsSource = data;
  177. var stackedHeaderRow1 = new StackedHeaderRow();
  178. stackedHeaderRow1.StackedColumns.Add(new StackedColumn
  179. { ChildColumns = "Code,Description,Qty", HeaderText = "Template Details", MappingName = "TemplateDetails" });
  180. foreach (var key in columns.Keys)
  181. stackedHeaderRow1.StackedColumns.Add(new StackedColumn
  182. { ChildColumns = string.Join(",", columns[key]), HeaderText = key, MappingName = key });
  183. stackedHeaderRow1.StackedColumns.Add(new StackedColumn { ChildColumns = "Total", HeaderText = "", MappingName = "Total" });
  184. dataGrid.StackedHeaderRows.Add(stackedHeaderRow1);
  185. }
  186. public void Shutdown()
  187. {
  188. }
  189. public void Heartbeat(TimeSpan time)
  190. {
  191. }
  192. private void DataGrid_AutoGeneratingColumn(object sender, AutoGeneratingColumnArgs e)
  193. {
  194. e.Column.TextAlignment = TextAlignment.Center;
  195. e.Column.HorizontalHeaderContentAlignment = HorizontalAlignment.Center;
  196. e.Column.ColumnSizer = GridLengthUnitType.None;
  197. var value = e.Column.ValueBinding as Binding;
  198. if (value.Path.Path.Equals("Code"))
  199. {
  200. e.Column.Width = 100;
  201. e.Column.HeaderStyle = Resources["TemplateHeaderStyle"] as Style;
  202. }
  203. else if (value.Path.Path.Equals("Description"))
  204. {
  205. e.Column.Width = 300;
  206. e.Column.HeaderStyle = Resources["TemplateHeaderStyle"] as Style;
  207. }
  208. else if (value.Path.Path.Equals("Qty"))
  209. {
  210. e.Column.Width = 60;
  211. e.Column.HeaderStyle = Resources["TemplateHeaderStyle"] as Style;
  212. }
  213. else
  214. {
  215. var style = new Style(typeof(GridCell));
  216. //style.Setters.Add(new Setter(GridCell.BackgroundProperty, new Binding(value.Path.Path) { Converter = new LeaveBackgroundConverter() { Colors = activityColors } }));
  217. //style.Setters.Add(new Setter(GridCell.ForegroundProperty, new Binding(value.Path.Path) { Converter = new LeaveForegroundConverter() }));
  218. //style.Setters.Add(new Setter(GridCell.FontStyleProperty, new Binding(value.Path.Path) { Converter = new LeaveFontStyleConverter() }));
  219. //style.Setters.Add(new Setter(GridCell.FontWeightProperty, new Binding(value.Path.Path) { Converter = new LeaveFontWeightConverter() }));
  220. e.Column.CellStyle = style;
  221. e.Column.Width = 50;
  222. e.Column.HeaderStyle = Resources["DataHeaderStyle"] as Style;
  223. e.Column.HeaderText = SectionDisplayNames[value.Path.Path];
  224. }
  225. }
  226. private void DataGrid_ContextMenuOpening(object sender, ContextMenuEventArgs e)
  227. {
  228. }
  229. private void DataGrid_QueryRowHeight(object sender, QueryRowHeightEventArgs e)
  230. {
  231. e.Height = e.RowIndex == 1 ? 150 : 30;
  232. e.Handled = true;
  233. }
  234. private void ActiveItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
  235. {
  236. if (IsReady && !_changing)
  237. {
  238. _activeonly = ActiveItems.SelectedIndex == 1;
  239. Refresh();
  240. }
  241. }
  242. private void Search_KeyUp(object sender, KeyEventArgs e)
  243. {
  244. if (string.IsNullOrWhiteSpace(Search.Text) || e.Key == Key.Return)
  245. {
  246. _search = Search.Text;
  247. Refresh();
  248. }
  249. }
  250. private void Export_Click(object sender, RoutedEventArgs e)
  251. {
  252. }
  253. private class GridCustomStackedRenderer : GridStackedHeaderCellRenderer
  254. {
  255. private readonly ResourceDictionary _resources;
  256. public GridCustomStackedRenderer(ResourceDictionary resources)
  257. {
  258. _resources = resources;
  259. }
  260. public override void OnInitializeEditElement(DataColumnBase dataColumn, GridStackedHeaderCellControl uiElement, object dataContext)
  261. {
  262. uiElement.Style = _resources["GroupHeaderStyle"] as Style;
  263. base.OnInitializeEditElement(dataColumn, uiElement, dataContext);
  264. }
  265. }
  266. #region Date Handling
  267. private bool _changing;
  268. public event DataModelUpdateEvent OnUpdateDataModel;
  269. private void SetDates(DateTime from, DateTime to, bool enable)
  270. {
  271. if (_changing)
  272. return;
  273. _changing = true;
  274. _from = from;
  275. FromDate.SelectedDate = from;
  276. FromDate.IsEnabled = enable;
  277. _to = to;
  278. ToDate.SelectedDate = to;
  279. ToDate.IsEnabled = enable;
  280. _changing = false;
  281. if (!enable)
  282. Refresh();
  283. }
  284. private int WeekDay(DateTime date)
  285. {
  286. if (date.DayOfWeek == DayOfWeek.Sunday)
  287. return 7;
  288. return (int)date.DayOfWeek - 1;
  289. }
  290. private void DateRange_SelectionChanged(object sender, SelectionChangedEventArgs e)
  291. {
  292. if (!IsReady)
  293. return;
  294. if (DateRange.SelectedIndex == 0) // Week To Date
  295. SetDates(DateTime.Today.AddDays(0 - WeekDay(DateTime.Today)), DateTime.Today, false);
  296. else if (DateRange.SelectedIndex == 1) // Last 7 Days
  297. SetDates(DateTime.Today.AddDays(-6), DateTime.Today, false);
  298. else if (DateRange.SelectedIndex == 2) // Month To Date
  299. SetDates(new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1), DateTime.Today, false);
  300. else if (DateRange.SelectedIndex == 3) // Last 30 days
  301. SetDates(DateTime.Today.AddDays(-29), DateTime.Today, false);
  302. else if (DateRange.SelectedIndex == 4) // Year To Date
  303. SetDates(new DateTime(DateTime.Today.Year, 1, 1), DateTime.Today, false);
  304. else if (DateRange.SelectedIndex == 5) // Last 12 Months
  305. SetDates(DateTime.Today.AddYears(-1).AddDays(1), DateTime.Today, false);
  306. else if (DateRange.SelectedIndex == 6) // Custom
  307. SetDates(FromDate.SelectedDate.Value, ToDate.SelectedDate.Value, true);
  308. }
  309. private void FromDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
  310. {
  311. if (IsReady && !_changing)
  312. {
  313. _from = FromDate.SelectedDate.Value.Date;
  314. Refresh();
  315. }
  316. }
  317. private void ToDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
  318. {
  319. if (IsReady && !_changing)
  320. {
  321. _to = ToDate.SelectedDate.Value.Date;
  322. Refresh();
  323. }
  324. }
  325. #endregion
  326. }
  327. }