ManufacturingTreatmentWindow.xaml.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media.Imaging;
  7. using Comal.Classes;
  8. using InABox.Clients;
  9. using InABox.Core;
  10. using InABox.DynamicGrid;
  11. using InABox.Wpf;
  12. using InABox.WPF;
  13. namespace PRSDesktop
  14. {
  15. /// <summary>
  16. /// Interaction logic for TreatmentWindow.xaml
  17. /// </summary>
  18. public partial class ManufacturingTreatmentWindow : ThemableWindow
  19. {
  20. private readonly CoreTable _treatments;
  21. private readonly List<Guid> selected = new();
  22. private readonly BitmapImage tick = PRSDesktop.Resources.tick.AsBitmapImage();
  23. public ManufacturingTreatmentWindow(CoreTable treatments)
  24. {
  25. _treatments = treatments;
  26. InitializeComponent();
  27. Packets.OnFilterRecord += Packets_OnFilterRecord;
  28. Packets.HiddenColumns.Add(x => x.ID);
  29. Packets.HiddenColumns.Add(x => x.Packet.SetoutLink.JobLink.ID);
  30. Packets.ActionColumns.Add(new DynamicImageColumn(GetImage, SelectPacket));
  31. Packets.Reconfigure(options => options.AddRange(DynamicGridOption.RecordCount));
  32. Packets.Treatments = treatments;
  33. }
  34. public IEnumerable<CoreRow> Selected
  35. {
  36. get
  37. {
  38. return _treatments != null
  39. ? _treatments.Rows.Where(row => selected.Contains(row.Get<ManufacturingTreatment, Guid>(col => col.ID)))
  40. : new CoreRow[] { };
  41. }
  42. }
  43. public Guid ProductID => (Guid)Treatments.SelectedValue;
  44. //public String ProductName => (Treatments.SelectedItem as KeyValuePair<Guid, String>?)?.Value ?? "";
  45. public Guid SupplierID => (Guid)Suppliers.SelectedValue;
  46. public string SupplierName => ((KeyValuePair<Guid, string>)Suppliers.SelectedItem).Value;
  47. private bool SelectPacket(CoreRow? arg)
  48. {
  49. if (arg == null)
  50. {
  51. if (selected.Any())
  52. selected.Clear();
  53. else
  54. selected.AddRange(Packets.Data.Rows.Select(row => row.Get<ManufacturingTreatment, Guid>(col => col.ID)));
  55. }
  56. else
  57. {
  58. var id = arg.Get<ManufacturingTreatment, Guid>(col => col.ID);
  59. if (selected.Contains(id))
  60. selected.Remove(id);
  61. else
  62. selected.Add(id);
  63. }
  64. OKButton.IsEnabled = selected.Any() && Suppliers.SelectedValue != null && !Suppliers.SelectedValue.Equals(Guid.Empty) &&
  65. !Suppliers.SelectedValue.Equals(CoreUtils.FullGuid);
  66. return true;
  67. }
  68. private BitmapImage? GetImage(CoreRow? arg)
  69. {
  70. if (arg == null)
  71. return tick;
  72. var id = arg.Get<ManufacturingTreatment, Guid>(col => col.ID);
  73. return selected.Contains(id) ? tick : null;
  74. }
  75. private bool Packets_OnFilterRecord(CoreRow row)
  76. {
  77. var id = Treatments.SelectedValue != null ? (Guid)Treatments.SelectedValue : CoreUtils.FullGuid;
  78. return row.Get<ManufacturingTreatment, Guid>(col => col.Product.ID).Equals(id);
  79. }
  80. private void Window_Loaded(object sender, RoutedEventArgs e)
  81. {
  82. Packets.Refresh(true, true);
  83. var treatsource = new Dictionary<Guid, string>();
  84. foreach (var row in _treatments.Rows)
  85. treatsource[row.Get<ManufacturingTreatment, Guid>(col => col.Product.ID)] =
  86. string.Format("{0}", row.Get<ManufacturingTreatment, string>(col => col.Product.Name));
  87. Treatments.ItemsSource = treatsource;
  88. }
  89. private void OKButton_Click(object sender, RoutedEventArgs e)
  90. {
  91. DialogResult = true;
  92. Close();
  93. }
  94. private void CancelButton_Click(object sender, RoutedEventArgs e)
  95. {
  96. DialogResult = false;
  97. Close();
  98. }
  99. private void Treatments_SelectionChanged(object sender, SelectionChangedEventArgs e)
  100. {
  101. var productid = Treatments.SelectedValue != null ? (Guid)Treatments.SelectedValue : CoreUtils.FullGuid;
  102. selected.Clear();
  103. var treatments = _treatments.Rows.Where(row => row.Get<ManufacturingTreatment, Guid>(col => col.Product.ID).Equals(productid));
  104. //selected.AddRange(treatments.Select(row => row.Get<ManufacturingTreatment, Guid>(col => col.ID)));
  105. var suppdict = new Dictionary<Guid, string>();
  106. var _suppliers = new Client<SupplierProduct>().Query(new Filter<SupplierProduct>(x => x.Product.ID).IsEqualTo(productid));
  107. foreach (var row in _suppliers.Rows)
  108. suppdict[row.Get<SupplierProduct, Guid>(x => x.SupplierLink.ID)] =
  109. string.Format("{0}", row.Get<SupplierProduct, string>(col => col.SupplierLink.Name));
  110. if (!suppdict.Any())
  111. {
  112. var allsups = new Client<Supplier>().Query();
  113. foreach (var row in allsups.Rows)
  114. suppdict[row.Get<Supplier, Guid>(x => x.ID)] = string.Format("{0}", row.Get<Supplier, string>(col => col.Name));
  115. }
  116. Suppliers.ItemsSource = suppdict;
  117. using (new WaitCursor())
  118. {
  119. var defsup = new Client<Product>()
  120. .Query(new Filter<Product>(x => x.ID).IsEqualTo(productid), new Columns<Product>(x => x.Supplier.ID))
  121. .Rows.FirstOrDefault();
  122. if (defsup != null)
  123. Suppliers.SelectedValue = defsup.Get<Product, Guid>(col => col.Supplier.ID);
  124. }
  125. Packets.Refresh(false, true);
  126. OKButton.IsEnabled = selected.Any() && Suppliers.SelectedValue != null && !Suppliers.SelectedValue.Equals(Guid.Empty) &&
  127. !Suppliers.SelectedValue.Equals(CoreUtils.FullGuid);
  128. }
  129. private void Suppliers_SelectionChanged(object sender, SelectionChangedEventArgs e)
  130. {
  131. OKButton.IsEnabled = selected.Any() && Suppliers.SelectedValue != null && !Suppliers.SelectedValue.Equals(Guid.Empty) &&
  132. !Suppliers.SelectedValue.Equals(CoreUtils.FullGuid);
  133. }
  134. }
  135. }