ManufacturingAllocationPanel.xaml.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  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.Configuration;
  14. using InABox.Core;
  15. using InABox.WPF;
  16. namespace PRSDesktop
  17. {
  18. /// <summary>
  19. /// Interaction logic for ManufacturingAllocationPanel.xaml
  20. /// </summary>
  21. public partial class ManufacturingAllocationPanel : UserControl, IPanel<ManufacturingPacket>
  22. {
  23. private Guid _section = Guid.Empty;
  24. private CoreTable _sections;
  25. private readonly BitmapImage barcode = PRSDesktop.Resources.barcode.AsBitmapImage();
  26. private readonly List<Tuple<Border, Label, ListBox, ColumnDefinition, int, CheckBox, Label>> columns = new();
  27. private ListBox current;
  28. private readonly BitmapImage disabled = PRSDesktop.Resources.disabled.AsBitmapImage();
  29. private readonly BitmapImage grouped = PRSDesktop.Resources.grouped.AsBitmapImage();
  30. private readonly string NEARLYDUE_COLOR = "Orange";
  31. private readonly string NOTYETDUE_COLOR = "PaleGreen";
  32. private readonly string ORDER_COLOR = "Gray";
  33. private readonly string OVERDUE_COLOR = "Salmon";
  34. private CoreTable Packets;
  35. private readonly string PRIORITY_COLOR = "Red";
  36. private readonly string QA_COLOR = "Silver";
  37. private string SELECTED_COLOR = "Yellow";
  38. private readonly string SHARED_COLOR = "Lime";
  39. private CoreTable Stages;
  40. public ManufacturingAllocationPanel()
  41. {
  42. InitializeComponent();
  43. Kanbans = new ObservableCollection<ManufacturingKanban>();
  44. PendingCheck.Tag = Pending;
  45. }
  46. public ObservableCollection<ManufacturingKanban> Kanbans { get; set; }
  47. public bool IsReady { get; set; }
  48. public void CreateToolbarButtons(IPanelHost host)
  49. {
  50. }
  51. public string SectionName => "Factory Allocation";
  52. public DataModel DataModel(Selection selection)
  53. {
  54. var ids = Packets != null ? Packets.Rows.Select(r => r.Get<ManufacturingPacket, Guid>(x => x.ID)).ToArray() : new Guid[] { };
  55. return new ManufacturingPacketDataModel(new Filter<ManufacturingPacket>(x => x.ID).InList(ids));
  56. }
  57. public void Refresh()
  58. {
  59. if (_section == Guid.Empty)
  60. {
  61. var sections = (Dictionary<Guid, string>)Sections.ItemsSource;
  62. _section = sections.Any() ? sections.First().Key : Guid.Empty;
  63. Sections.SelectedValue = _section;
  64. }
  65. ReloadPackets(true);
  66. }
  67. public Dictionary<string, object[]> Selected()
  68. {
  69. var result = new Dictionary<string, object[]>();
  70. return result;
  71. }
  72. public void Setup()
  73. {
  74. var settings = new UserConfiguration<ManufacturingAllocationSettings>().Load();
  75. var sections = new Dictionary<Guid, string>();
  76. ReloadSections();
  77. foreach (var row in _sections.Rows)
  78. sections[row.Get<ManufacturingSection, Guid>(x => x.ID)] = string.Format("{0}: {1}",
  79. row.Get<ManufacturingSection, string>(x => x.Factory.Name), row.Get<ManufacturingSection, string>(x => x.Name));
  80. _section = sections.ContainsKey(settings.Section) ? settings.Section : sections.Any() ? sections.First().Key : Guid.Empty;
  81. Sections.ItemsSource = sections;
  82. Sections.SelectedValue = _section;
  83. }
  84. public void Shutdown()
  85. {
  86. }
  87. public event DataModelUpdateEvent OnUpdateDataModel;
  88. public void Heartbeat(TimeSpan time)
  89. {
  90. // Nothing to do here
  91. }
  92. public Dictionary<Type, CoreTable> DataEnvironment()
  93. {
  94. var result = new Dictionary<Type, CoreTable>();
  95. return result;
  96. }
  97. private BitmapImage GetBarCode(CoreRow packet)
  98. {
  99. if (!packet.Get<ManufacturingPacket, DateTime>(c => c.BarcodePrinted).IsEmpty())
  100. return packet.Get<ManufacturingPacket, BarcodeType>(c => c.BarcodeType) == BarcodeType.Grouped ? grouped : barcode;
  101. if (packet.Get<ManufacturingPacket, BarcodeType>(c => c.BarcodeType) == BarcodeType.None)
  102. return disabled;
  103. return null;
  104. }
  105. private string GetColor(DateTime duedate, DateTime estdate)
  106. {
  107. var color = NOTYETDUE_COLOR;
  108. if (duedate < estdate)
  109. color = OVERDUE_COLOR;
  110. else if (duedate < estdate.AddDays(7))
  111. color = NEARLYDUE_COLOR;
  112. return color;
  113. }
  114. private void CreateKanban(CoreRow row, bool IsChecked)
  115. {
  116. try
  117. {
  118. var packetid = row.Get<ManufacturingPacket, Guid>(x => x.ID);
  119. var priority = row.Get<ManufacturingPacket, bool>(c => c.Priority);
  120. var qty = row.Get<ManufacturingPacket, int>(c => c.Quantity);
  121. var barqty = row.Get<ManufacturingPacket, int>(c => c.BarcodeQty);
  122. var duedate = row.Get<ManufacturingPacket, DateTime>(c => c.DueDate);
  123. var estdate = row.Get<ManufacturingPacket, DateTime>(c => c.EstimatedDate);
  124. var sectionid = row.Get<ManufacturingPacket, Guid>(c => c.StageLink.SectionID);
  125. var pktsection = row.Get<ManufacturingPacket, string>(c => c.StageLink.Section);
  126. var stage = Stages.Rows.FirstOrDefault(r => r.Get<ManufacturingPacketStage, Guid>(c => c.Parent.ID).Equals(packetid));
  127. if (stage == null)
  128. return;
  129. var section = (Guid)Sections.SelectedValue;
  130. var station = stage.Get<ManufacturingPacketStage, int>(c => c.Station);
  131. var quality = stage.Get<ManufacturingPacketStage, QualityStatus>(c => c.QualityStatus);
  132. var percentage = stage.Get<ManufacturingPacketStage, double>(c => c.PercentageComplete);
  133. var Pending = station == 0;
  134. var model = new ManufacturingKanban();
  135. model.ID = row.Get<ManufacturingPacket, Guid>(c => c.ID).ToString();
  136. model.Title = string.Format("{0}: {1}{2}",
  137. row.Get<ManufacturingPacket, string>(c => c.Serial),
  138. qty != barqty ? string.Format("{0} x ", qty) : "",
  139. row.Get<ManufacturingPacket, string>(c => c.Title)
  140. );
  141. model.Quantity = barqty;
  142. model.JobName = string.Format("{0}: {1}",
  143. row.Get<ManufacturingPacket, string>(c => c.SetoutLink.Number),
  144. row.Get<ManufacturingPacket, string>(c => c.SetoutLink.JobLink.Name)
  145. );
  146. model.DueDate = duedate;
  147. var location = row.Get<ManufacturingPacket, string>(c => c.Location);
  148. var descrip = new List<string>
  149. {
  150. //row.Get<ManufacturingPacket,String>(c=>c.Level.Code),
  151. //row.Get<ManufacturingPacket,String>(c=>c.Zone.Code),
  152. string.IsNullOrEmpty(location) ? row.Get<ManufacturingPacket, string>(c => c.SetoutLink.Location) : location
  153. };
  154. model.Description = string.Join(" / ", descrip.Where(x => !string.IsNullOrWhiteSpace(x))).Trim();
  155. model.TemplateID = row.Get<ManufacturingPacket, Guid>(c => c.ManufacturingTemplateLink.ID);
  156. model.Image = GetBarCode(row);
  157. //model.IsSelected = packet.ID.ToString() == CurrentKanbanID;
  158. model.Tags = priority ? new[] { "PRIORITY" } : new string[] { };
  159. model.Category = section.ToString(); // packet.StageLink.SectionID.ToString();
  160. model.ColorKey =
  161. Entity.IsEntityLinkValid<ManufacturingPacket, PurchaseOrderItemLink>(x => x.OrderItem, row) &&
  162. row.Get<ManufacturingPacket, DateTime>(c => c.OrderItem.ReceivedDate).IsEmpty() ? ORDER_COLOR :
  163. priority ? PRIORITY_COLOR :
  164. !Pending ? GetColor(duedate.IsEmpty() ? DateTime.Today : duedate, estdate.IsEmpty() ? DateTime.Today : estdate) : QA_COLOR;
  165. model.SelectedColor = model.ColorKey; // packet.ID.ToString() == CurrentKanbanID ? SELECTED_COLOR : model.ColorKey;
  166. model.SharedColor = station.Equals(-1) ? SHARED_COLOR : model.ColorKey;
  167. model.Checked = IsChecked;
  168. model.SetoutID = row.Get<ManufacturingPacket, Guid>(c => c.SetoutLink.ID);
  169. model.Assignee = station.ToString();
  170. var ratio = 1.0F / (station == -1 ? (double)columns.Count : 1.0F);
  171. var percentleft = (100.0F - stage.Get<ManufacturingPacketStage, double>(c => c.PercentageComplete)) / 100.0F;
  172. model.Status = string.Format("({0:F2} hours)",
  173. qty * stage.Get<ManufacturingPacketStage, TimeSpan>(c => c.Time).TotalHours * ratio *
  174. percentleft); //packet.StageLink.ID.Equals(Guid.Empty) ? " " : Pending ? "PENDING" /*GetQualityStatus(quality)*/ : String.Format("{0:F0}%", percentage);
  175. model.Flags = row.Get<ManufacturingPacket, bool>(c => c.Distributed)
  176. ? !sectionid.Equals(section) ? string.IsNullOrEmpty(pktsection) ? "" : pktsection.ToUpper().Trim() : "DISTRIB"
  177. : "";
  178. Kanbans.Add(model);
  179. }
  180. catch (Exception e)
  181. {
  182. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  183. }
  184. }
  185. private TimeSpan CalcTime(IEnumerable<ManufacturingKanban> kanbans)
  186. {
  187. double hours = 0.0F;
  188. var ids = kanbans.Select(x => Guid.Parse(x.ID));
  189. foreach (var id in ids)
  190. {
  191. var packet = Packets.Rows.FirstOrDefault(r => r.Get<ManufacturingPacket, Guid>(c => c.ID).Equals(id));
  192. var stage = Stages.Rows.FirstOrDefault(r => r.Get<ManufacturingPacketStage, Guid>(c => c.Parent.ID).Equals(id));
  193. var ratio = 1.0F / (stage.Get<ManufacturingPacketStage, int>(c => c.Station) == -1 ? (double)columns.Count : 1.0F);
  194. var percentleft = (100.0F - stage.Get<ManufacturingPacketStage, double>(c => c.PercentageComplete)) / 100.0F;
  195. hours += packet.Get<ManufacturingPacket, int>(c => c.Quantity) *
  196. stage.Get<ManufacturingPacketStage, TimeSpan>(c => c.Time).TotalHours *
  197. ratio * percentleft;
  198. }
  199. return TimeSpan.FromHours(hours);
  200. }
  201. private void ReloadPackets(bool reloaddata)
  202. {
  203. using (new WaitCursor())
  204. {
  205. var checks = Kanbans.Where(x => x.Checked).Select(x => x.ID).ToArray();
  206. Kanbans.Clear();
  207. if (reloaddata && Sections.SelectedValue != null)
  208. {
  209. var sectionid = (Guid)Sections.SelectedValue; // CurrentSection != null ? CurrentSection.ID : CoreUtils.FullGuid;
  210. Stages = new Client<ManufacturingPacketStage>().Query(
  211. new Filter<ManufacturingPacketStage>(x => x.ManufacturingSectionLink.ID).IsEqualTo(sectionid).And(x => x.Completed)
  212. .IsEqualTo(DateTime.MinValue),
  213. new Columns<ManufacturingPacketStage>(
  214. x => x.ID,
  215. x => x.Parent.ID,
  216. x => x.ManufacturingSectionLink.ID,
  217. x => x.Station,
  218. x => x.QualityStatus,
  219. x => x.PercentageComplete,
  220. x => x.Time,
  221. x => x.Started
  222. )
  223. );
  224. //Stages = stagetable.Rows.Select(x => x.ToObject<ManufacturingPacketStage>()).ToArray();
  225. //Filter<ManufacturingPacket> filter = new Filter<ManufacturingPacket>(x => x.Completed).IsLessThan(DateTime.MinValue.AddDays(1)).And(x => x.Archived).IsLessThan(DateTime.MinValue.AddDays(1)).And(x => x.OnHold).IsEqualTo(false);
  226. var filter = new Filter<ManufacturingPacket>(x => x.Completed).IsLessThan(DateTime.MinValue.AddDays(1)).And(x => x.Archived)
  227. .IsLessThan(DateTime.MinValue.AddDays(1)).And(x => x.OnHold).IsEqualTo(false);
  228. var sectfilter = new Filter<ManufacturingPacket>(x => x.StageLink.SectionID).IsEqualTo(sectionid).Or(x => x.Distributed)
  229. .IsEqualTo(true);
  230. filter.Ands.Add(sectfilter);
  231. var columns = new Columns<ManufacturingPacket>();
  232. var iprops = DatabaseSchema.Properties(typeof(ManufacturingPacket)).Where(x =>
  233. !x.Name.Equals("CustomAttributes") && !x.Name.Equals("Stages") && !x.Name.Equals("Time") && !x.Name.Equals("ActualTime") &&
  234. !x.Name.Equals("TimeRemaining"));
  235. foreach (var iprop in iprops)
  236. columns.Add(iprop.Name);
  237. Packets = new Client<ManufacturingPacket>().Query(
  238. filter,
  239. columns,
  240. //new Columns<ManufacturingPacket>(
  241. // x => x.ID,
  242. // x => x.Serial,
  243. // x => x.Title,
  244. // x => x.Quantity,
  245. // x => x.SetoutLink.Number,
  246. // x => x.SetoutLink.JobLink.JobNumber,
  247. // x => x.SetoutLink.JobLink.Name,
  248. // x => x.DueDate,
  249. // x => x.SetoutLink.Location,
  250. // x => x.SetoutLink.Reference,
  251. // x => x.Priority,
  252. // x => x.OrderItem.ID,
  253. // x => x.EstimatedDate,
  254. // x => x.Distributed,
  255. // x => x.StageLink.SectionID,
  256. // x => x.StageLink.Section,
  257. // x => x.BarcodePrinted,
  258. // x => x.BarcodeType
  259. // ),
  260. new SortOrder<ManufacturingPacket>(x => x.Priority, SortDirection.Descending).ThenBy(x => x.SetoutLink.Number)
  261. );
  262. //Packets = table.Rows.Select(x => x.ToObject<ManufacturingPacket>()).ToArray();
  263. }
  264. if (Packets != null)
  265. foreach (var row in Packets.Rows)
  266. CreateKanban(row, false);
  267. Pending.ItemsSource = null;
  268. var pendings = Kanbans.Where(x => x.Assignee.Equals("0")).OrderBy(x => x.Tags.Contains("PRIORITY") ? 0 : 1).ThenBy(x => x.DueDate);
  269. Pending.ItemsSource = pendings;
  270. Task.Run(() =>
  271. {
  272. var time = CalcTime(pendings).TotalHours;
  273. Dispatcher.Invoke(() => { Hours.Content = string.Format("{0:F2} hrs", time); });
  274. });
  275. foreach (var column in columns)
  276. {
  277. column.Item3.ItemsSource = null;
  278. var items = Kanbans.Where(x => x.Assignee.Equals("-1") || x.Assignee.Equals(column.Item5.ToString()))
  279. .OrderBy(x => x.Tags.Contains("PRIORITY") ? 0 : 1).ThenBy(x => x.DueDate);
  280. column.Item3.ItemsSource = items;
  281. column.Item6.IsChecked = false;
  282. Task.Run(() =>
  283. {
  284. var time = CalcTime(items).TotalHours;
  285. Dispatcher.Invoke(() => { column.Item7.Content = string.Format("{0:F2} hrs", time); });
  286. });
  287. }
  288. }
  289. }
  290. private void ReloadSections()
  291. {
  292. _sections = new Client<ManufacturingSection>().Query(
  293. new Filter<ManufacturingSection>(x => x.Hidden).IsEqualTo(false),
  294. new Columns<ManufacturingSection>(
  295. x => x.ID,
  296. x => x.Factory.Name,
  297. x => x.Name,
  298. x => x.Stations
  299. ),
  300. new SortOrder<ManufacturingSection>(x => x.Factory.Sequence).ThenBy(x => x.Sequence)
  301. );
  302. }
  303. protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
  304. {
  305. base.OnRenderSizeChanged(sizeInfo);
  306. var width = sizeInfo.NewSize.Width;
  307. SetWidths(width);
  308. }
  309. private void SetWidths(double width)
  310. {
  311. double stations = Stations.ColumnDefinitions.Count;
  312. var desiredwidth = (width - 5.0F) / (stations + 1.0F);
  313. if (desiredwidth < 300.0F)
  314. desiredwidth = 300.0F;
  315. //Sections.Width = desiredwidth;
  316. Pending.Width = desiredwidth;
  317. Stations.Width = stations * desiredwidth;
  318. }
  319. private void Sections_SelectionChanged(object sender, SelectionChangedEventArgs e)
  320. {
  321. if (e.AddedItems.Count == 0)
  322. return;
  323. var pair = (KeyValuePair<Guid, string>)e.AddedItems[0];
  324. var sectionid = pair.Key;
  325. ReloadColumns(sectionid);
  326. if (IsReady)
  327. new UserConfiguration<ManufacturingAllocationSettings>().Save(new ManufacturingAllocationSettings { Section = pair.Key });
  328. }
  329. private void ReloadColumns(Guid sectionid)
  330. {
  331. // Delete all existing lists and Grid Rows
  332. foreach (var column in columns)
  333. {
  334. Stations.Children.Remove(column.Item1);
  335. Stations.Children.Remove(column.Item3);
  336. Stations.ColumnDefinitions.Remove(column.Item4);
  337. }
  338. columns.Clear();
  339. var history = new Client<ManufacturingHistory>().Query(
  340. new Filter<ManufacturingHistory>(x => x.Section.ID).IsEqualTo(sectionid).And(x => x.Date)
  341. .IsGreaterThanOrEqualTo(DateTime.Today.AddDays(-7)),
  342. new Columns<ManufacturingHistory>(
  343. x => x.Station,
  344. x => x.Employee.Name
  345. ),
  346. new SortOrder<ManufacturingHistory>(x => x.Station).ThenBy(x => x.LastUpdate)
  347. );
  348. var section = _sections.Rows.First(r => r.Get<ManufacturingSection, Guid>(x => x.ID).Equals(sectionid));
  349. var stations = section.Get<ManufacturingSection, int>(x => x.Stations);
  350. for (var iStation = 0; iStation < stations; iStation++)
  351. {
  352. var row = history.Rows.LastOrDefault(r => r.Get<ManufacturingHistory, int>(c => c.Station).Equals(iStation + 1));
  353. var empname = row != null ? row.Get<ManufacturingHistory, string>(x => x.Employee.Name) : string.Format("Station {0}", iStation + 1);
  354. var Column = new ColumnDefinition { Width = new GridLength(1.0F, GridUnitType.Star) };
  355. Stations.ColumnDefinitions.Add(Column);
  356. // Create a Border
  357. var Border = new Border();
  358. Border.BorderBrush = new SolidColorBrush(Colors.Gray);
  359. Border.BorderThickness = new Thickness(0);
  360. Border.CornerRadius = new CornerRadius(5, 5, 0, 0);
  361. Border.Margin = new Thickness(0, 0, 2, 2);
  362. Border.SetValue(Grid.RowProperty, 0);
  363. Border.SetValue(Grid.ColumnProperty, iStation);
  364. Border.Height = 30.0F;
  365. Stations.Children.Add(Border);
  366. var grid = new Grid();
  367. grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1.0F, GridUnitType.Auto) });
  368. grid.ColumnDefinitions.Add(new ColumnDefinition
  369. {
  370. Width = new GridLength(iStation == stations - 1 && Security.IsAllowed<CanViewFactorySettings>() ? 30.0F : 0.0F,
  371. GridUnitType.Pixel)
  372. });
  373. grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1.0F, GridUnitType.Star) });
  374. grid.ColumnDefinitions.Add(new ColumnDefinition
  375. {
  376. Width = new GridLength(iStation == stations - 1 && Security.IsAllowed<CanViewFactorySettings>() ? 30.0F : 0.0F,
  377. GridUnitType.Pixel)
  378. });
  379. grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1.0F, GridUnitType.Auto) });
  380. Border.Child = grid;
  381. var checkborder = new Border();
  382. checkborder.BorderBrush = new SolidColorBrush(Colors.Gray);
  383. checkborder.BorderThickness = new Thickness(0.75);
  384. checkborder.CornerRadius = new CornerRadius(5, 0, 0, 0);
  385. checkborder.Margin = new Thickness(0, 0, 2, 0);
  386. checkborder.SetValue(Grid.RowProperty, 0);
  387. checkborder.SetValue(Grid.ColumnProperty, 0);
  388. grid.Children.Add(checkborder);
  389. var check = new CheckBox();
  390. check.Margin = new Thickness(14, 0, 17, 0);
  391. check.SetValue(Grid.ColumnProperty, 0);
  392. check.Checked += List_Checked;
  393. check.Unchecked += List_Checked;
  394. check.VerticalAlignment = VerticalAlignment.Center;
  395. checkborder.Child = check;
  396. var remove = new Button();
  397. remove.Margin = new Thickness(0, 0, 2, 0);
  398. remove.SetValue(Grid.RowProperty, 0);
  399. remove.SetValue(Grid.ColumnProperty, 1);
  400. remove.Content = "-";
  401. remove.Click += Remove_Click;
  402. grid.Children.Add(remove);
  403. var labelborder = new Border();
  404. labelborder.BorderBrush = new SolidColorBrush(Colors.Gray);
  405. labelborder.BorderThickness = new Thickness(0.75);
  406. labelborder.CornerRadius = new CornerRadius(0, 0, 0, 0);
  407. labelborder.Margin = new Thickness(0, 0, 2, 0);
  408. labelborder.SetValue(Grid.RowProperty, 0);
  409. labelborder.SetValue(Grid.ColumnProperty, 2);
  410. grid.Children.Add(labelborder);
  411. var Label = new Label();
  412. Label.SetValue(Grid.ColumnProperty, 1);
  413. Label.Content = empname;
  414. Label.HorizontalContentAlignment = HorizontalAlignment.Center;
  415. Label.VerticalContentAlignment = VerticalAlignment.Center;
  416. labelborder.Child = Label;
  417. var add = new Button();
  418. add.Margin = new Thickness(0, 0, 2, 0);
  419. add.SetValue(Grid.RowProperty, 0);
  420. add.SetValue(Grid.ColumnProperty, 3);
  421. add.Content = "+";
  422. add.Click += Add_Click;
  423. grid.Children.Add(add);
  424. var hoursborder = new Border();
  425. hoursborder.BorderBrush = new SolidColorBrush(Colors.Gray);
  426. hoursborder.BorderThickness = new Thickness(0.75);
  427. hoursborder.CornerRadius = new CornerRadius(0, 5, 0, 0);
  428. hoursborder.Margin = new Thickness(0, 0, 0, 0);
  429. hoursborder.SetValue(Grid.RowProperty, 0);
  430. hoursborder.SetValue(Grid.ColumnProperty, 4);
  431. grid.Children.Add(hoursborder);
  432. var hours = new Label();
  433. hours.SetValue(Grid.ColumnProperty, 2);
  434. hours.Content = string.Format("({0} hours)", 0.0F);
  435. hours.HorizontalContentAlignment = HorizontalAlignment.Center;
  436. hours.VerticalContentAlignment = VerticalAlignment.Center;
  437. hoursborder.Child = hours;
  438. var Items = new ListBox();
  439. Items.Margin = new Thickness(0, 0, 2, 2);
  440. Items.SetValue(Grid.RowProperty, 1);
  441. Items.SetValue(Grid.ColumnProperty, iStation);
  442. Items.ItemTemplate = (DataTemplate)Resources["Packet"];
  443. Items.HorizontalContentAlignment = HorizontalAlignment.Stretch;
  444. Items.SetValue(ScrollViewer.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Disabled);
  445. Items.SetValue(VirtualizingPanel.IsVirtualizingProperty, true);
  446. Items.SetValue(VirtualizingPanel.VirtualizationModeProperty, VirtualizationMode.Recycling);
  447. Items.PreviewMouseRightButtonDown += Items_PreviewMouseRightButtonDown;
  448. Items.PreviewMouseDown += Items_PreviewMouseDown;
  449. Items.SelectionChanged += Items_SelectionChanged;
  450. Stations.Children.Add(Items);
  451. check.Tag = Items;
  452. remove.Tag = Items;
  453. var column = new Tuple<Border, Label, ListBox, ColumnDefinition, int, CheckBox, Label>(Border, Label, Items, Column, iStation + 1,
  454. check,
  455. hours);
  456. columns.Add(column);
  457. }
  458. SetWidths(ActualWidth);
  459. ReloadPackets(true);
  460. }
  461. private void Items_SelectionChanged(object sender, SelectionChangedEventArgs e)
  462. {
  463. if (e.AddedItems.Count > 0)
  464. current = sender as ListBox;
  465. }
  466. private void Items_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  467. {
  468. current = sender as ListBox;
  469. }
  470. private void Items_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  471. {
  472. current = sender as ListBox;
  473. }
  474. private void Add_Click(object sender, RoutedEventArgs e)
  475. {
  476. var sectionid = (Guid)Sections.SelectedValue;
  477. var section = new Client<ManufacturingSection>().Load(new Filter<ManufacturingSection>(x => x.ID).IsEqualTo(sectionid)).FirstOrDefault();
  478. if (section != null)
  479. {
  480. section.Stations += 1;
  481. new Client<ManufacturingSection>().Save(section, "Added Station");
  482. ReloadSections();
  483. ReloadColumns(section.ID);
  484. }
  485. }
  486. private void Remove_Click(object sender, RoutedEventArgs e)
  487. {
  488. var sectionid = (Guid)Sections.SelectedValue;
  489. var section = new Client<ManufacturingSection>().Load(new Filter<ManufacturingSection>(x => x.ID).IsEqualTo(sectionid)).FirstOrDefault();
  490. if (section != null)
  491. {
  492. if (section.Stations < 2)
  493. {
  494. MessageBox.Show("There must be at least one station available in each section!");
  495. return;
  496. }
  497. var button = sender as Button;
  498. var listbox = button.Tag as ListBox;
  499. var kanbans = listbox.ItemsSource as IEnumerable<ManufacturingKanban>;
  500. if (kanbans.Any(x => !x.Assignee.Equals("-1")))
  501. {
  502. MessageBox.Show("Please clear out all packets before removing this station!");
  503. return;
  504. }
  505. section.Stations -= 1;
  506. new Client<ManufacturingSection>().Save(section, "Removed Station");
  507. ReloadSections();
  508. ReloadColumns(section.ID);
  509. }
  510. }
  511. private void CardSelected(object sender, MouseButtonEventArgs e)
  512. {
  513. }
  514. private void CardPreviewMouseWheel(object sender, MouseWheelEventArgs e)
  515. {
  516. }
  517. private void CardChecked(object sender, RoutedEventArgs e)
  518. {
  519. }
  520. private void PacketMenu_Opened(object sender, RoutedEventArgs e)
  521. {
  522. var menu = sender as ContextMenu;
  523. var kanban = menu.Tag as ManufacturingKanban;
  524. var assign = menu.Items[0] as MenuItem;
  525. var revert = menu.Items[1] as MenuItem;
  526. var separator = menu.Items[2] as Separator;
  527. var distribute = menu.Items[3] as MenuItem;
  528. var undistribute = menu.Items[4] as MenuItem;
  529. var separator2 = menu.Items[5] as Separator;
  530. var share = menu.Items[6] as MenuItem;
  531. var unshare = menu.Items[7] as MenuItem;
  532. assign.Visibility = kanban.Assignee.Equals("-1") ? Visibility.Collapsed : Visibility.Visible;
  533. revert.Visibility = kanban.Assignee.Equals("0") ? Visibility.Collapsed : Visibility.Visible;
  534. distribute.Visibility = Security.IsAllowed<CanDistributePackets>() ? Visibility.Visible : Visibility.Collapsed;
  535. undistribute.Visibility = distribute.Visibility;
  536. separator2.Visibility = distribute.Visibility;
  537. share.Visibility = kanban.Assignee.Equals("-1") ? Visibility.Collapsed : Visibility.Visible;
  538. unshare.Visibility = kanban.Assignee.Equals("-1") ? Visibility.Visible : Visibility.Collapsed;
  539. assign.Items.Clear();
  540. foreach (var column in columns)
  541. {
  542. var item = new MenuItem { Header = column.Item2.Content, Tag = column.Item5 };
  543. item.Click += SetStation_Click;
  544. assign.Items.Add(item);
  545. }
  546. }
  547. private IEnumerable<ManufacturingKanban> CheckedKanbans(ListBox listbox)
  548. {
  549. var kanbans = listbox.ItemsSource as IEnumerable<ManufacturingKanban>;
  550. var result = kanbans.Where(x => x.Checked);
  551. if (!result.Any())
  552. result = new[] { listbox.SelectedValue as ManufacturingKanban };
  553. return result;
  554. }
  555. private IEnumerable<ManufacturingPacket> CheckedPackets(ListBox listbox)
  556. {
  557. var kanbans = CheckedKanbans(listbox).Select(x => Guid.Parse(x.ID));
  558. var packets = Packets.Rows.Where(r => kanbans.Contains(r.Get<ManufacturingPacket, Guid>(c => c.ID)));
  559. return packets.Select(x => x.ToObject<ManufacturingPacket>());
  560. }
  561. private IEnumerable<ManufacturingPacketStage> CheckedStages(ListBox listbox)
  562. {
  563. var kanbans = CheckedKanbans(listbox).Select(x => Guid.Parse(x.ID));
  564. var selstages = Stages.Rows.Where(r =>
  565. kanbans.Contains(r.Get<ManufacturingPacketStage, Guid>(c => c.Parent.ID)) &&
  566. r.Get<ManufacturingPacketStage, Guid>(c => c.ManufacturingSectionLink.ID).Equals(_section));
  567. return selstages.Select(x => x.ToObject<ManufacturingPacketStage>());
  568. }
  569. private Tuple<Border, Label, ListBox, ColumnDefinition, int, CheckBox, Label> GetColumn(object sender)
  570. {
  571. var menu = sender as MenuItem;
  572. Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("GetColumn: Menu is {0}", menu != null ? menu.Header : "null"));
  573. var context = menu.Parent as ContextMenu;
  574. Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("GetColumn: Context is {0}", context != null ? "ok" : "null"));
  575. var border = context.PlacementTarget as Border;
  576. Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("GetColumn: Border is {0}", border != null ? "ok" : "null"));
  577. var kanban = menu.Tag as ManufacturingKanban;
  578. Logger.Send(LogType.Information, ClientFactory.UserID,
  579. string.Format("GetColumn: kanban is {0}", kanban != null ? kanban.Description : "null"));
  580. var column = columns.FirstOrDefault(x => x.Item3 == current);
  581. Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("GetColumn: column is {0}", column != null ? "ok" : "null"));
  582. return column;
  583. }
  584. private void SetStation_Click(object sender, RoutedEventArgs e)
  585. {
  586. var menu = sender as MenuItem;
  587. var station = (int)menu.Tag;
  588. Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("Allocating: Station is {0}", station));
  589. var column = GetColumn(menu.Parent);
  590. var list = column == null ? Pending : column.Item3;
  591. //Logger.Send(LogType.Information, ClientFactory.UserID, String.Format("Allocating: Column is {0}", column.Item2.Content));
  592. var stages = CheckedStages(list).ToArray();
  593. Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("Allocating: Packet Count is {0}", stages.Length));
  594. Logger.Send(LogType.Information, ClientFactory.UserID,
  595. string.Format("Allocating: {0}", string.Join(", ", stages.Select(x => x.Parent.Serial))));
  596. foreach (var stage in stages)
  597. {
  598. stage.Station = station;
  599. if (stage.Started.Equals(DateTime.MinValue))
  600. stage.Started = DateTime.Now;
  601. }
  602. using (new WaitCursor())
  603. {
  604. Logger.Send(LogType.Information, ClientFactory.UserID,
  605. string.Format("Allocating: Updating {0} items", stages.Where(x => x.IsChanged()).ToArray().Length));
  606. new Client<ManufacturingPacketStage>().Save(stages, string.Format("Assigned to Station {0}", station));
  607. }
  608. Refresh();
  609. }
  610. private void Revert_Click(object sender, RoutedEventArgs e)
  611. {
  612. var column = GetColumn(sender);
  613. var stages = CheckedStages(column != null ? column.Item3 : Pending).ToArray();
  614. var bDeleteStarted = false;
  615. foreach (var stage in stages)
  616. {
  617. if (stage.PercentageComplete > 0.0F)
  618. bDeleteStarted = true;
  619. stage.Station = 0;
  620. stage.Started = DateTime.MinValue;
  621. }
  622. if (bDeleteStarted)
  623. bDeleteStarted =
  624. MessageBox.Show("Some items have already been started.\n\nRemove these packets anyway?", "Confirm", MessageBoxButton.YesNo) ==
  625. MessageBoxResult.Yes;
  626. using (new WaitCursor())
  627. {
  628. var updates = stages.Where(x => bDeleteStarted ? true : x.PercentageComplete == 0.0F).ToArray();
  629. new Client<ManufacturingPacketStage>().Save(updates, "Reverted to Pending Status");
  630. }
  631. Refresh();
  632. }
  633. private void Distribute_Click(object sender, RoutedEventArgs e)
  634. {
  635. var column = GetColumn(sender);
  636. var packets = CheckedPackets(column != null ? column.Item3 : Pending).ToArray();
  637. foreach (var packet in packets)
  638. packet.Distributed = true;
  639. using (new WaitCursor())
  640. {
  641. new Client<ManufacturingPacket>().Save(packets, "Set Distributed Flag");
  642. }
  643. Refresh();
  644. }
  645. private void ClearDistributed_Click(object sender, RoutedEventArgs e)
  646. {
  647. var column = GetColumn(sender);
  648. var packets = CheckedPackets(column != null ? column.Item3 : Pending).ToArray();
  649. foreach (var packet in packets)
  650. packet.Distributed = false;
  651. using (new WaitCursor())
  652. {
  653. new Client<ManufacturingPacket>().Save(packets, "Cleared Distributed Flag");
  654. }
  655. Refresh();
  656. }
  657. private void SetShared_Click(object sender, RoutedEventArgs e)
  658. {
  659. var column = GetColumn(sender);
  660. var stages = CheckedStages(column != null ? column.Item3 : Pending);
  661. foreach (var stage in stages)
  662. {
  663. stage.Station = -1;
  664. if (stage.Started.Equals(DateTime.MinValue))
  665. stage.Started = DateTime.Now;
  666. }
  667. using (new WaitCursor())
  668. {
  669. new Client<ManufacturingPacketStage>().Save(stages, "Cleared Shared Flag");
  670. }
  671. Refresh();
  672. }
  673. private void ClearShared_Click(object sender, RoutedEventArgs e)
  674. {
  675. var column = GetColumn(sender);
  676. var stages = CheckedStages(column != null ? column.Item3 : Pending);
  677. foreach (var stage in stages)
  678. stage.Station = column.Item5;
  679. using (new WaitCursor())
  680. {
  681. new Client<ManufacturingPacketStage>().Save(stages, "Cleared Shared Flag");
  682. }
  683. Refresh();
  684. }
  685. private void List_Checked(object sender, RoutedEventArgs e)
  686. {
  687. var check = sender as CheckBox;
  688. var list = check.Tag as ListBox;
  689. var kanbans = list.ItemsSource as IEnumerable<ManufacturingKanban>;
  690. foreach (var kanban in kanbans)
  691. kanban.Checked = check.IsChecked == true;
  692. list.ItemsSource = null;
  693. list.ItemsSource = kanbans;
  694. }
  695. }
  696. }