ManufacturingTreatmentWindow.xaml.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 =>
  32. {
  33. options.RecordCount = true;
  34. });
  35. Packets.Treatments = treatments;
  36. }
  37. public IEnumerable<CoreRow> Selected
  38. {
  39. get
  40. {
  41. return _treatments != null
  42. ? _treatments.Rows.Where(row => selected.Contains(row.Get<ManufacturingTreatment, Guid>(col => col.ID)))
  43. : new CoreRow[] { };
  44. }
  45. }
  46. public Guid ProductID => (Guid)Treatments.SelectedValue;
  47. //public String ProductName => (Treatments.SelectedItem as KeyValuePair<Guid, String>?)?.Value ?? "";
  48. public Guid SupplierID => (Guid)Suppliers.SelectedValue;
  49. public string SupplierName => ((KeyValuePair<Guid, string>)Suppliers.SelectedItem).Value;
  50. private bool SelectPacket(CoreRow? arg)
  51. {
  52. if (arg == null)
  53. {
  54. if (selected.Any())
  55. selected.Clear();
  56. else
  57. selected.AddRange(Packets.Data.Rows.Select(row => row.Get<ManufacturingTreatment, Guid>(col => col.ID)));
  58. }
  59. else
  60. {
  61. var id = arg.Get<ManufacturingTreatment, Guid>(col => col.ID);
  62. if (selected.Contains(id))
  63. selected.Remove(id);
  64. else
  65. selected.Add(id);
  66. }
  67. OKButton.IsEnabled = selected.Any() && Suppliers.SelectedValue != null && !Suppliers.SelectedValue.Equals(Guid.Empty) &&
  68. !Suppliers.SelectedValue.Equals(CoreUtils.FullGuid);
  69. return true;
  70. }
  71. private BitmapImage? GetImage(CoreRow? arg)
  72. {
  73. if (arg == null)
  74. return tick;
  75. var id = arg.Get<ManufacturingTreatment, Guid>(col => col.ID);
  76. return selected.Contains(id) ? tick : null;
  77. }
  78. private bool Packets_OnFilterRecord(CoreRow row)
  79. {
  80. var id = Treatments.SelectedValue != null ? (Guid)Treatments.SelectedValue : CoreUtils.FullGuid;
  81. return row.Get<ManufacturingTreatment, Guid>(col => col.Product.ID).Equals(id);
  82. }
  83. private void Window_Loaded(object sender, RoutedEventArgs e)
  84. {
  85. Packets.Refresh(true, true);
  86. var treatsource = new Dictionary<Guid, string>();
  87. foreach (var row in _treatments.Rows)
  88. treatsource[row.Get<ManufacturingTreatment, Guid>(col => col.Product.ID)] =
  89. string.Format("{0}", row.Get<ManufacturingTreatment, string>(col => col.Product.Name));
  90. Treatments.ItemsSource = treatsource;
  91. }
  92. private void OKButton_Click(object sender, RoutedEventArgs e)
  93. {
  94. DialogResult = true;
  95. Close();
  96. }
  97. private void CancelButton_Click(object sender, RoutedEventArgs e)
  98. {
  99. DialogResult = false;
  100. Close();
  101. }
  102. private void Treatments_SelectionChanged(object sender, SelectionChangedEventArgs e)
  103. {
  104. var productid = Treatments.SelectedValue != null ? (Guid)Treatments.SelectedValue : CoreUtils.FullGuid;
  105. selected.Clear();
  106. var treatments = _treatments.Rows.Where(row => row.Get<ManufacturingTreatment, Guid>(col => col.Product.ID).Equals(productid));
  107. //selected.AddRange(treatments.Select(row => row.Get<ManufacturingTreatment, Guid>(col => col.ID)));
  108. var suppdict = new Dictionary<Guid, string>();
  109. var _suppliers = new Client<SupplierProduct>().Query(new Filter<SupplierProduct>(x => x.Product.ID).IsEqualTo(productid));
  110. foreach (var row in _suppliers.Rows)
  111. suppdict[row.Get<SupplierProduct, Guid>(x => x.SupplierLink.ID)] =
  112. string.Format("{0}", row.Get<SupplierProduct, string>(col => col.SupplierLink.Name));
  113. if (!suppdict.Any())
  114. {
  115. var allsups = new Client<Supplier>().Query();
  116. foreach (var row in allsups.Rows)
  117. suppdict[row.Get<Supplier, Guid>(x => x.ID)] = string.Format("{0}", row.Get<Supplier, string>(col => col.Name));
  118. }
  119. Suppliers.ItemsSource = suppdict;
  120. using (new WaitCursor())
  121. {
  122. var defsup = new Client<Product>()
  123. .Query(
  124. new Filter<Product>(x => x.ID).IsEqualTo(productid),
  125. Columns.None<Product>().Add(x => x.Supplier.ID))
  126. .Rows.FirstOrDefault();
  127. if (defsup != null)
  128. Suppliers.SelectedValue = defsup.Get<Product, Guid>(col => col.Supplier.ID);
  129. }
  130. Packets.Refresh(false, true);
  131. OKButton.IsEnabled = selected.Any() && Suppliers.SelectedValue != null && !Suppliers.SelectedValue.Equals(Guid.Empty) &&
  132. !Suppliers.SelectedValue.Equals(CoreUtils.FullGuid);
  133. }
  134. private void Suppliers_SelectionChanged(object sender, SelectionChangedEventArgs e)
  135. {
  136. OKButton.IsEnabled = selected.Any() && Suppliers.SelectedValue != null && !Suppliers.SelectedValue.Equals(Guid.Empty) &&
  137. !Suppliers.SelectedValue.Equals(CoreUtils.FullGuid);
  138. }
  139. }
  140. }