ManufacturingAllocationPanel.xaml.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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. model.GroupID = row.Get<ManufacturingPacket, Guid>(c => c.SetoutLink.Group.ID);
  179. model.GroupName = row.Get<ManufacturingPacket, string>(c => c.SetoutLink.Group.Name);
  180. Kanbans.Add(model);
  181. }
  182. catch (Exception e)
  183. {
  184. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  185. }
  186. }
  187. private TimeSpan CalcTime(IEnumerable<ManufacturingKanban> kanbans)
  188. {
  189. double hours = 0.0F;
  190. var ids = kanbans.Select(x => Guid.Parse(x.ID));
  191. foreach (var id in ids)
  192. {
  193. var packet = Packets.Rows.FirstOrDefault(r => r.Get<ManufacturingPacket, Guid>(c => c.ID).Equals(id));
  194. var stage = Stages.Rows.FirstOrDefault(r => r.Get<ManufacturingPacketStage, Guid>(c => c.Parent.ID).Equals(id));
  195. var ratio = 1.0F / (stage.Get<ManufacturingPacketStage, int>(c => c.Station) == -1 ? (double)columns.Count : 1.0F);
  196. var percentleft = (100.0F - stage.Get<ManufacturingPacketStage, double>(c => c.PercentageComplete)) / 100.0F;
  197. hours += packet.Get<ManufacturingPacket, int>(c => c.Quantity) *
  198. stage.Get<ManufacturingPacketStage, TimeSpan>(c => c.Time).TotalHours *
  199. ratio * percentleft;
  200. }
  201. return TimeSpan.FromHours(hours);
  202. }
  203. private void ReloadPackets(bool reloaddata)
  204. {
  205. using (new WaitCursor())
  206. {
  207. var checks = Kanbans.Where(x => x.Checked).Select(x => x.ID).ToArray();
  208. Kanbans.Clear();
  209. if (reloaddata && Sections.SelectedValue != null)
  210. {
  211. var sectionid = (Guid)Sections.SelectedValue; // CurrentSection != null ? CurrentSection.ID : CoreUtils.FullGuid;
  212. Stages = new Client<ManufacturingPacketStage>().Query(
  213. new Filter<ManufacturingPacketStage>(x => x.ManufacturingSectionLink.ID).IsEqualTo(sectionid).And(x => x.Completed)
  214. .IsEqualTo(DateTime.MinValue),
  215. new Columns<ManufacturingPacketStage>(
  216. x => x.ID,
  217. x => x.Parent.ID,
  218. x => x.ManufacturingSectionLink.ID,
  219. x => x.Station,
  220. x => x.QualityStatus,
  221. x => x.PercentageComplete,
  222. x => x.Time,
  223. x => x.Started
  224. )
  225. );
  226. //Stages = stagetable.Rows.Select(x => x.ToObject<ManufacturingPacketStage>()).ToArray();
  227. var filter = new Filter<ManufacturingPacket>(x => x.Completed).IsEqualTo(DateTime.MinValue)
  228. .And(x => x.Archived).IsEqualTo(DateTime.MinValue)
  229. .And(x => x.OnHold).IsEqualTo(false);
  230. var sectfilter = new Filter<ManufacturingPacket>(x => x.StageLink.SectionID).IsEqualTo(sectionid).Or(x => x.Distributed)
  231. .IsEqualTo(true);
  232. filter.Ands.Add(sectfilter);
  233. var columns = new Columns<ManufacturingPacket>();
  234. var iprops = DatabaseSchema.Properties(typeof(ManufacturingPacket)).Where(x =>
  235. !x.Name.Equals("CustomAttributes") && !x.Name.Equals("Stages") && !x.Name.Equals("Time") && !x.Name.Equals("ActualTime") &&
  236. !x.Name.Equals("TimeRemaining"));
  237. foreach (var iprop in iprops)
  238. columns.Add(iprop.Name);
  239. Packets = new Client<ManufacturingPacket>().Query(
  240. filter,
  241. columns,
  242. //new Columns<ManufacturingPacket>(
  243. // x => x.ID,
  244. // x => x.Serial,
  245. // x => x.Title,
  246. // x => x.Quantity,
  247. // x => x.SetoutLink.Number,
  248. // x => x.SetoutLink.JobLink.JobNumber,
  249. // x => x.SetoutLink.JobLink.Name,
  250. // x => x.DueDate,
  251. // x => x.SetoutLink.Location,
  252. // x => x.SetoutLink.Reference,
  253. // x => x.Priority,
  254. // x => x.OrderItem.ID,
  255. // x => x.EstimatedDate,
  256. // x => x.Distributed,
  257. // x => x.StageLink.SectionID,
  258. // x => x.StageLink.Section,
  259. // x => x.BarcodePrinted,
  260. // x => x.BarcodeType
  261. // ),
  262. new SortOrder<ManufacturingPacket>(x => x.Priority, SortDirection.Descending).ThenBy(x => x.SetoutLink.Number)
  263. );
  264. //Packets = table.Rows.Select(x => x.ToObject<ManufacturingPacket>()).ToArray();
  265. }
  266. if (Packets != null)
  267. foreach (var row in Packets.Rows)
  268. CreateKanban(row, false);
  269. Pending.ItemsSource = null;
  270. var pendings = Kanbans.Where(x => x.Assignee.Equals("0")).OrderBy(x => x.Tags.Contains("PRIORITY") ? 0 : 1).ThenBy(x => x.DueDate);
  271. Pending.ItemsSource = pendings;
  272. Task.Run(() =>
  273. {
  274. var time = CalcTime(pendings).TotalHours;
  275. Dispatcher.Invoke(() => { Hours.Content = string.Format("{0:F2} hrs", time); });
  276. });
  277. foreach (var column in columns)
  278. {
  279. column.Item3.ItemsSource = null;
  280. var items = Kanbans.Where(x => x.Assignee.Equals("-1") || x.Assignee.Equals(column.Item5.ToString()))
  281. .OrderBy(x => x.Tags.Contains("PRIORITY") ? 0 : 1).ThenBy(x => x.DueDate);
  282. column.Item3.ItemsSource = items;
  283. column.Item6.IsChecked = false;
  284. Task.Run(() =>
  285. {
  286. var time = CalcTime(items).TotalHours;
  287. Dispatcher.Invoke(() => { column.Item7.Content = string.Format("{0:F2} hrs", time); });
  288. });
  289. }
  290. }
  291. }
  292. private void ReloadSections()
  293. {
  294. _sections = new Client<ManufacturingSection>().Query(
  295. new Filter<ManufacturingSection>(x => x.Hidden).IsEqualTo(false),
  296. new Columns<ManufacturingSection>(
  297. x => x.ID,
  298. x => x.Factory.Name,
  299. x => x.Name,
  300. x => x.Stations
  301. ),
  302. new SortOrder<ManufacturingSection>(x => x.Factory.Sequence).ThenBy(x => x.Sequence)
  303. );
  304. }
  305. protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
  306. {
  307. base.OnRenderSizeChanged(sizeInfo);
  308. var width = sizeInfo.NewSize.Width;
  309. SetWidths(width);
  310. }
  311. private void SetWidths(double width)
  312. {
  313. double stations = Stations.ColumnDefinitions.Count;
  314. var desiredwidth = (width - 5.0F) / (stations + 1.0F);
  315. if (desiredwidth < 300.0F)
  316. desiredwidth = 300.0F;
  317. //Sections.Width = desiredwidth;
  318. Pending.Width = desiredwidth;
  319. Stations.Width = stations * desiredwidth;
  320. }
  321. private void Sections_SelectionChanged(object sender, SelectionChangedEventArgs e)
  322. {
  323. if (e.AddedItems.Count == 0)
  324. return;
  325. var pair = (KeyValuePair<Guid, string>)e.AddedItems[0];
  326. var sectionid = pair.Key;
  327. ReloadColumns(sectionid);
  328. if (IsReady)
  329. new UserConfiguration<ManufacturingAllocationSettings>().Save(new ManufacturingAllocationSettings { Section = pair.Key });
  330. }
  331. private void ReloadColumns(Guid sectionid)
  332. {
  333. // Delete all existing lists and Grid Rows
  334. foreach (var column in columns)
  335. {
  336. Stations.Children.Remove(column.Item1);
  337. Stations.Children.Remove(column.Item3);
  338. Stations.ColumnDefinitions.Remove(column.Item4);
  339. }
  340. columns.Clear();
  341. var history = new Client<ManufacturingHistory>().Query(
  342. new Filter<ManufacturingHistory>(x => x.Section.ID).IsEqualTo(sectionid).And(x => x.Date)
  343. .IsGreaterThanOrEqualTo(DateTime.Today.AddDays(-7)),
  344. new Columns<ManufacturingHistory>(
  345. x => x.Station,
  346. x => x.Employee.Name
  347. ),
  348. new SortOrder<ManufacturingHistory>(x => x.Station).ThenBy(x => x.LastUpdate)
  349. );
  350. var section = _sections.Rows.First(r => r.Get<ManufacturingSection, Guid>(x => x.ID).Equals(sectionid));
  351. var stations = section.Get<ManufacturingSection, int>(x => x.Stations);
  352. for (var iStation = 0; iStation < stations; iStation++)
  353. {
  354. var row = history.Rows.LastOrDefault(r => r.Get<ManufacturingHistory, int>(c => c.Station).Equals(iStation + 1));
  355. var empname = row != null ? row.Get<ManufacturingHistory, string>(x => x.Employee.Name) : string.Format("Station {0}", iStation + 1);
  356. var Column = new ColumnDefinition { Width = new GridLength(1.0F, GridUnitType.Star) };
  357. Stations.ColumnDefinitions.Add(Column);
  358. // Create a Border
  359. var Border = new Border();
  360. Border.BorderBrush = new SolidColorBrush(Colors.Gray);
  361. Border.BorderThickness = new Thickness(0);
  362. Border.CornerRadius = new CornerRadius(5, 5, 0, 0);
  363. Border.Margin = new Thickness(0, 0, 2, 2);
  364. Border.SetValue(Grid.RowProperty, 0);
  365. Border.SetValue(Grid.ColumnProperty, iStation);
  366. Border.Height = 30.0F;
  367. Stations.Children.Add(Border);
  368. var grid = new Grid();
  369. grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1.0F, GridUnitType.Auto) });
  370. grid.ColumnDefinitions.Add(new ColumnDefinition
  371. {
  372. Width = new GridLength(iStation == stations - 1 && Security.IsAllowed<CanViewFactorySettings>() ? 30.0F : 0.0F,
  373. GridUnitType.Pixel)
  374. });
  375. grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1.0F, GridUnitType.Star) });
  376. grid.ColumnDefinitions.Add(new ColumnDefinition
  377. {
  378. Width = new GridLength(iStation == stations - 1 && Security.IsAllowed<CanViewFactorySettings>() ? 30.0F : 0.0F,
  379. GridUnitType.Pixel)
  380. });
  381. grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1.0F, GridUnitType.Auto) });
  382. Border.Child = grid;
  383. var checkborder = new Border();
  384. checkborder.BorderBrush = new SolidColorBrush(Colors.Gray);
  385. checkborder.BorderThickness = new Thickness(0.75);
  386. checkborder.CornerRadius = new CornerRadius(5, 0, 0, 0);
  387. checkborder.Margin = new Thickness(0, 0, 2, 0);
  388. checkborder.SetValue(Grid.RowProperty, 0);
  389. checkborder.SetValue(Grid.ColumnProperty, 0);
  390. grid.Children.Add(checkborder);
  391. var check = new CheckBox();
  392. check.Margin = new Thickness(14, 0, 17, 0);
  393. check.SetValue(Grid.ColumnProperty, 0);
  394. check.Checked += List_Checked;
  395. check.Unchecked += List_Checked;
  396. check.VerticalAlignment = VerticalAlignment.Center;
  397. checkborder.Child = check;
  398. var remove = new Button();
  399. remove.Margin = new Thickness(0, 0, 2, 0);
  400. remove.SetValue(Grid.RowProperty, 0);
  401. remove.SetValue(Grid.ColumnProperty, 1);
  402. remove.Content = "-";
  403. remove.Click += Remove_Click;
  404. grid.Children.Add(remove);
  405. var labelborder = new Border();
  406. labelborder.BorderBrush = new SolidColorBrush(Colors.Gray);
  407. labelborder.BorderThickness = new Thickness(0.75);
  408. labelborder.CornerRadius = new CornerRadius(0, 0, 0, 0);
  409. labelborder.Margin = new Thickness(0, 0, 2, 0);
  410. labelborder.SetValue(Grid.RowProperty, 0);
  411. labelborder.SetValue(Grid.ColumnProperty, 2);
  412. grid.Children.Add(labelborder);
  413. var Label = new Label();
  414. Label.SetValue(Grid.ColumnProperty, 1);
  415. Label.Content = empname;
  416. Label.HorizontalContentAlignment = HorizontalAlignment.Center;
  417. Label.VerticalContentAlignment = VerticalAlignment.Center;
  418. labelborder.Child = Label;
  419. var add = new Button();
  420. add.Margin = new Thickness(0, 0, 2, 0);
  421. add.SetValue(Grid.RowProperty, 0);
  422. add.SetValue(Grid.ColumnProperty, 3);
  423. add.Content = "+";
  424. add.Click += Add_Click;
  425. grid.Children.Add(add);
  426. var hoursborder = new Border();
  427. hoursborder.BorderBrush = new SolidColorBrush(Colors.Gray);
  428. hoursborder.BorderThickness = new Thickness(0.75);
  429. hoursborder.CornerRadius = new CornerRadius(0, 5, 0, 0);
  430. hoursborder.Margin = new Thickness(0, 0, 0, 0);
  431. hoursborder.SetValue(Grid.RowProperty, 0);
  432. hoursborder.SetValue(Grid.ColumnProperty, 4);
  433. grid.Children.Add(hoursborder);
  434. var hours = new Label();
  435. hours.SetValue(Grid.ColumnProperty, 2);
  436. hours.Content = string.Format("({0} hours)", 0.0F);
  437. hours.HorizontalContentAlignment = HorizontalAlignment.Center;
  438. hours.VerticalContentAlignment = VerticalAlignment.Center;
  439. hoursborder.Child = hours;
  440. var Items = new ListBox();
  441. Items.Margin = new Thickness(0, 0, 2, 2);
  442. Items.SetValue(Grid.RowProperty, 1);
  443. Items.SetValue(Grid.ColumnProperty, iStation);
  444. Items.ItemTemplate = (DataTemplate)Resources["Packet"];
  445. Items.HorizontalContentAlignment = HorizontalAlignment.Stretch;
  446. Items.SetValue(ScrollViewer.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Disabled);
  447. Items.SetValue(VirtualizingPanel.IsVirtualizingProperty, true);
  448. Items.SetValue(VirtualizingPanel.VirtualizationModeProperty, VirtualizationMode.Recycling);
  449. Items.PreviewMouseRightButtonDown += Items_PreviewMouseRightButtonDown;
  450. Items.PreviewMouseDown += Items_PreviewMouseDown;
  451. Items.SelectionChanged += Items_SelectionChanged;
  452. Stations.Children.Add(Items);
  453. check.Tag = Items;
  454. remove.Tag = Items;
  455. var column = new Tuple<Border, Label, ListBox, ColumnDefinition, int, CheckBox, Label>(Border, Label, Items, Column, iStation + 1,
  456. check,
  457. hours);
  458. columns.Add(column);
  459. }
  460. SetWidths(ActualWidth);
  461. ReloadPackets(true);
  462. }
  463. private void Items_SelectionChanged(object sender, SelectionChangedEventArgs e)
  464. {
  465. if (e.AddedItems.Count > 0)
  466. current = sender as ListBox;
  467. }
  468. private void Items_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  469. {
  470. current = sender as ListBox;
  471. }
  472. private void Items_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  473. {
  474. current = sender as ListBox;
  475. }
  476. private void Add_Click(object sender, RoutedEventArgs e)
  477. {
  478. var sectionid = (Guid)Sections.SelectedValue;
  479. var section = new Client<ManufacturingSection>().Load(new Filter<ManufacturingSection>(x => x.ID).IsEqualTo(sectionid)).FirstOrDefault();
  480. if (section != null)
  481. {
  482. section.Stations += 1;
  483. new Client<ManufacturingSection>().Save(section, "Added Station");
  484. ReloadSections();
  485. ReloadColumns(section.ID);
  486. }
  487. }
  488. private void Remove_Click(object sender, RoutedEventArgs e)
  489. {
  490. var sectionid = (Guid)Sections.SelectedValue;
  491. var section = new Client<ManufacturingSection>().Load(new Filter<ManufacturingSection>(x => x.ID).IsEqualTo(sectionid)).FirstOrDefault();
  492. if (section != null)
  493. {
  494. if (section.Stations < 2)
  495. {
  496. MessageBox.Show("There must be at least one station available in each section!");
  497. return;
  498. }
  499. var button = sender as Button;
  500. var listbox = button.Tag as ListBox;
  501. var kanbans = listbox.ItemsSource as IEnumerable<ManufacturingKanban>;
  502. if (kanbans.Any(x => !x.Assignee.Equals("-1")))
  503. {
  504. MessageBox.Show("Please clear out all packets before removing this station!");
  505. return;
  506. }
  507. section.Stations -= 1;
  508. new Client<ManufacturingSection>().Save(section, "Removed Station");
  509. ReloadSections();
  510. ReloadColumns(section.ID);
  511. }
  512. }
  513. private void CardSelected(object sender, MouseButtonEventArgs e)
  514. {
  515. }
  516. private void CardPreviewMouseWheel(object sender, MouseWheelEventArgs e)
  517. {
  518. }
  519. private void CardChecked(object sender, RoutedEventArgs e)
  520. {
  521. }
  522. private void PacketMenu_Opened(object sender, RoutedEventArgs e)
  523. {
  524. var menu = sender as ContextMenu;
  525. var kanban = menu.Tag as ManufacturingKanban;
  526. var assign = menu.Items[0] as MenuItem;
  527. var revert = menu.Items[1] as MenuItem;
  528. var separator = menu.Items[2] as Separator;
  529. var distribute = menu.Items[3] as MenuItem;
  530. var undistribute = menu.Items[4] as MenuItem;
  531. var separator2 = menu.Items[5] as Separator;
  532. var share = menu.Items[6] as MenuItem;
  533. var unshare = menu.Items[7] as MenuItem;
  534. assign.Visibility = kanban.Assignee.Equals("-1") ? Visibility.Collapsed : Visibility.Visible;
  535. revert.Visibility = kanban.Assignee.Equals("0") ? Visibility.Collapsed : Visibility.Visible;
  536. distribute.Visibility = Security.IsAllowed<CanDistributePackets>() ? Visibility.Visible : Visibility.Collapsed;
  537. undistribute.Visibility = distribute.Visibility;
  538. separator2.Visibility = distribute.Visibility;
  539. share.Visibility = kanban.Assignee.Equals("-1") ? Visibility.Collapsed : Visibility.Visible;
  540. unshare.Visibility = kanban.Assignee.Equals("-1") ? Visibility.Visible : Visibility.Collapsed;
  541. assign.Items.Clear();
  542. foreach (var column in columns)
  543. {
  544. var item = new MenuItem { Header = column.Item2.Content, Tag = column.Item5 };
  545. item.Click += SetStation_Click;
  546. assign.Items.Add(item);
  547. }
  548. }
  549. private IEnumerable<ManufacturingKanban> CheckedKanbans(ListBox listbox)
  550. {
  551. var kanbans = listbox.ItemsSource as IEnumerable<ManufacturingKanban>;
  552. var result = kanbans.Where(x => x.Checked);
  553. if (!result.Any())
  554. result = new[] { listbox.SelectedValue as ManufacturingKanban };
  555. return result;
  556. }
  557. private IEnumerable<ManufacturingPacket> CheckedPackets(ListBox listbox)
  558. {
  559. var kanbans = CheckedKanbans(listbox).Select(x => Guid.Parse(x.ID));
  560. var packets = Packets.Rows.Where(r => kanbans.Contains(r.Get<ManufacturingPacket, Guid>(c => c.ID)));
  561. return packets.Select(x => x.ToObject<ManufacturingPacket>());
  562. }
  563. private IEnumerable<ManufacturingPacketStage> CheckedStages(ListBox listbox)
  564. {
  565. var kanbans = CheckedKanbans(listbox).Select(x => Guid.Parse(x.ID));
  566. var selstages = Stages.Rows.Where(r =>
  567. kanbans.Contains(r.Get<ManufacturingPacketStage, Guid>(c => c.Parent.ID)) &&
  568. r.Get<ManufacturingPacketStage, Guid>(c => c.ManufacturingSectionLink.ID).Equals(_section));
  569. return selstages.Select(x => x.ToObject<ManufacturingPacketStage>());
  570. }
  571. private Tuple<Border, Label, ListBox, ColumnDefinition, int, CheckBox, Label> GetColumn(object sender)
  572. {
  573. var menu = sender as MenuItem;
  574. Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("GetColumn: Menu is {0}", menu != null ? menu.Header : "null"));
  575. var context = menu.Parent as ContextMenu;
  576. Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("GetColumn: Context is {0}", context != null ? "ok" : "null"));
  577. var border = context.PlacementTarget as Border;
  578. Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("GetColumn: Border is {0}", border != null ? "ok" : "null"));
  579. var kanban = menu.Tag as ManufacturingKanban;
  580. Logger.Send(LogType.Information, ClientFactory.UserID,
  581. string.Format("GetColumn: kanban is {0}", kanban != null ? kanban.Description : "null"));
  582. var column = columns.FirstOrDefault(x => x.Item3 == current);
  583. Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("GetColumn: column is {0}", column != null ? "ok" : "null"));
  584. return column;
  585. }
  586. private void SetStation_Click(object sender, RoutedEventArgs e)
  587. {
  588. var menu = sender as MenuItem;
  589. var station = (int)menu.Tag;
  590. Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("Allocating: Station is {0}", station));
  591. var column = GetColumn(menu.Parent);
  592. var list = column == null ? Pending : column.Item3;
  593. //Logger.Send(LogType.Information, ClientFactory.UserID, String.Format("Allocating: Column is {0}", column.Item2.Content));
  594. var stages = CheckedStages(list).ToArray();
  595. Logger.Send(LogType.Information, ClientFactory.UserID, string.Format("Allocating: Packet Count is {0}", stages.Length));
  596. Logger.Send(LogType.Information, ClientFactory.UserID,
  597. string.Format("Allocating: {0}", string.Join(", ", stages.Select(x => x.Parent.Serial))));
  598. foreach (var stage in stages)
  599. {
  600. stage.Station = station;
  601. if (stage.Started.Equals(DateTime.MinValue))
  602. stage.Started = DateTime.Now;
  603. }
  604. using (new WaitCursor())
  605. {
  606. Logger.Send(LogType.Information, ClientFactory.UserID,
  607. string.Format("Allocating: Updating {0} items", stages.Where(x => x.IsChanged()).ToArray().Length));
  608. new Client<ManufacturingPacketStage>().Save(stages, string.Format("Assigned to Station {0}", station));
  609. }
  610. Refresh();
  611. }
  612. private void Revert_Click(object sender, RoutedEventArgs e)
  613. {
  614. var column = GetColumn(sender);
  615. var stages = CheckedStages(column != null ? column.Item3 : Pending).ToArray();
  616. var bDeleteStarted = false;
  617. foreach (var stage in stages)
  618. {
  619. if (stage.PercentageComplete > 0.0F)
  620. bDeleteStarted = true;
  621. stage.Station = 0;
  622. stage.Started = DateTime.MinValue;
  623. }
  624. if (bDeleteStarted)
  625. bDeleteStarted =
  626. MessageBox.Show("Some items have already been started.\n\nRemove these packets anyway?", "Confirm", MessageBoxButton.YesNo) ==
  627. MessageBoxResult.Yes;
  628. using (new WaitCursor())
  629. {
  630. var updates = stages.Where(x => bDeleteStarted ? true : x.PercentageComplete == 0.0F).ToArray();
  631. new Client<ManufacturingPacketStage>().Save(updates, "Reverted to Pending Status");
  632. }
  633. Refresh();
  634. }
  635. private void Distribute_Click(object sender, RoutedEventArgs e)
  636. {
  637. var column = GetColumn(sender);
  638. var packets = CheckedPackets(column != null ? column.Item3 : Pending).ToArray();
  639. foreach (var packet in packets)
  640. packet.Distributed = true;
  641. using (new WaitCursor())
  642. {
  643. new Client<ManufacturingPacket>().Save(packets, "Set Distributed Flag");
  644. }
  645. Refresh();
  646. }
  647. private void ClearDistributed_Click(object sender, RoutedEventArgs e)
  648. {
  649. var column = GetColumn(sender);
  650. var packets = CheckedPackets(column != null ? column.Item3 : Pending).ToArray();
  651. foreach (var packet in packets)
  652. packet.Distributed = false;
  653. using (new WaitCursor())
  654. {
  655. new Client<ManufacturingPacket>().Save(packets, "Cleared Distributed Flag");
  656. }
  657. Refresh();
  658. }
  659. private void SetShared_Click(object sender, RoutedEventArgs e)
  660. {
  661. var column = GetColumn(sender);
  662. var stages = CheckedStages(column != null ? column.Item3 : Pending);
  663. foreach (var stage in stages)
  664. {
  665. stage.Station = -1;
  666. if (stage.Started.Equals(DateTime.MinValue))
  667. stage.Started = DateTime.Now;
  668. }
  669. using (new WaitCursor())
  670. {
  671. new Client<ManufacturingPacketStage>().Save(stages, "Cleared Shared Flag");
  672. }
  673. Refresh();
  674. }
  675. private void ClearShared_Click(object sender, RoutedEventArgs e)
  676. {
  677. var column = GetColumn(sender);
  678. var stages = CheckedStages(column != null ? column.Item3 : Pending);
  679. foreach (var stage in stages)
  680. stage.Station = column.Item5;
  681. using (new WaitCursor())
  682. {
  683. new Client<ManufacturingPacketStage>().Save(stages, "Cleared Shared Flag");
  684. }
  685. Refresh();
  686. }
  687. private void List_Checked(object sender, RoutedEventArgs e)
  688. {
  689. var check = sender as CheckBox;
  690. var list = check.Tag as ListBox;
  691. var kanbans = list.ItemsSource as IEnumerable<ManufacturingKanban>;
  692. foreach (var kanban in kanbans)
  693. kanban.Checked = check.IsChecked == true;
  694. list.ItemsSource = null;
  695. list.ItemsSource = kanbans;
  696. }
  697. }
  698. }