ManufacturingTreatmentWindow.xaml.cs 6.1 KB

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