ProductPricing.xaml.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.Linq;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using Comal.Classes;
  6. using InABox.Clients;
  7. using InABox.Core;
  8. using InABox.DynamicGrid;
  9. namespace PRSDesktop
  10. {
  11. /// <summary>
  12. /// Interaction logic for ProductPricing.xaml
  13. /// </summary>
  14. public partial class ProductPricing : UserControl, IDynamicEditorPage //, IDynamicCustomEditorPage<Product>
  15. {
  16. private readonly ProductComponentGrid components = new();
  17. private ProductPriceComponent[] MasterList = { };
  18. private readonly Guid ProductID = Guid.Empty;
  19. public ProductPricing()
  20. {
  21. InitializeComponent();
  22. //components.OnCreateItem += (o, i) => i.Product.ID = Product.ID;
  23. components.OnItemsChanged += o => ApplyStrategy();
  24. components.SetValue(Grid.ColumnProperty, 1);
  25. components.SetValue(Grid.RowProperty, 3);
  26. components.Margin = new Thickness(5);
  27. Grid.Children.Add(components);
  28. }
  29. public DynamicEditorGrid EditorGrid { get; set; }
  30. public PageType PageType => PageType.Other;
  31. public bool Ready { get; set; }
  32. public void Load(object item, Func<Type, CoreTable> PageDataHandler)
  33. {
  34. var product = (Product)item;
  35. Strategy.SelectedIndex = (int)product.PricingStrategy;
  36. BaseCost.IsEnabled = !product.UseDefaultSupplierPricing;
  37. BaseCost.Value = Convert.ToDecimal(product.BaseCost);
  38. NettCost.Value = Convert.ToDecimal(product.NettCost);
  39. MasterList = new Client<ProductPriceComponent>().Load(new Filter<ProductPriceComponent>(x => x.Product.ID).IsEqualTo(product.ID));
  40. components.Items = MasterList.ToList();
  41. components.Items.Clear();
  42. components.Items.AddRange(MasterList);
  43. components.Refresh(true, true);
  44. ApplyStrategy();
  45. Ready = true;
  46. }
  47. public void BeforeSave(object item)
  48. {
  49. var product = (Product)item;
  50. product.PricingStrategy = (ProductPricingStrategy)Strategy.SelectedIndex;
  51. // Base Cost is only applicable for standard and subcontractor items
  52. product.BaseCost = BaseCost.Value.HasValue ? Convert.ToDouble(BaseCost.Value) : 0.00;
  53. // Components not applicable to standard items
  54. if (product.PricingStrategy == ProductPricingStrategy.Standard)
  55. components.Items.Clear();
  56. // Process the components list (update & delete as required)
  57. var comps = MasterList.ToList();
  58. foreach (var comp in components.Items)
  59. {
  60. if (comp.IsChanged())
  61. new Client<ProductPriceComponent>().Save(comp, "Updated by User Edit", (o, e) => { });
  62. if (comps.Contains(comp))
  63. comps.Remove(comp);
  64. }
  65. foreach (var comp in comps)
  66. new Client<ProductPriceComponent>().Delete(comp, "Updated by User Edit", (o, e) => { });
  67. // Nett Price always applies
  68. product.NettCost = NettCost.Value.HasValue ? Convert.ToDouble(NettCost.Value) : 0.00;
  69. }
  70. public void AfterSave(object item)
  71. {
  72. var product = (Product)item;
  73. // First remove any deleted files
  74. for (var i = 0; i < MasterList.Length; i++)
  75. if (!components.Items.Any(x => x.ID.Equals(MasterList[i].ID)))
  76. new Client<ProductPriceComponent>().Delete(MasterList[i], "Component Deleted by User");
  77. // now add new files
  78. foreach (var newitem in components.Items)
  79. {
  80. newitem.Product.ID = product.ID;
  81. if (newitem.OriginalValues.Count > 0)
  82. new Client<ProductPriceComponent>().Save(newitem, "Component Added by User");
  83. }
  84. }
  85. public string Caption()
  86. {
  87. return "Pricing Options";
  88. }
  89. public int Order()
  90. {
  91. return 0;
  92. }
  93. public Size MinimumSize()
  94. {
  95. return new Size(400, 400);
  96. }
  97. private void Components_OnReload(object sender, Filters<ProductPriceComponent> criteria, Columns<ProductPriceComponent> columns,
  98. ref SortOrder<ProductPriceComponent> sortby)
  99. {
  100. criteria.Add(new Filter<ProductPriceComponent>(x => x.Product.ID).IsEqualTo(ProductID));
  101. }
  102. private void ConfigureStrategy()
  103. {
  104. if (Strategy.SelectedIndex == 0) // Standard Supplier Item
  105. {
  106. Grid.RowDefinitions[1].Height = new GridLength(1, GridUnitType.Auto);
  107. BaseLabel.Content = "Supplier Cost";
  108. Grid.RowDefinitions[2].Height = new GridLength(0, GridUnitType.Pixel);
  109. Grid.RowDefinitions[3].Height = new GridLength(0, GridUnitType.Pixel);
  110. }
  111. else if (Strategy.SelectedIndex == 1) // Subcontractor Item
  112. {
  113. Grid.RowDefinitions[1].Height = new GridLength(1, GridUnitType.Auto);
  114. BaseLabel.Content = "Work Order Cost";
  115. Grid.RowDefinitions[2].Height = new GridLength(1, GridUnitType.Star);
  116. ComponentsLabel.Content = "Subcontractor Materials";
  117. Grid.RowDefinitions[3].Height = new GridLength(1, GridUnitType.Auto);
  118. NettLabel.Content = "Labour Cost";
  119. }
  120. else if (Strategy.SelectedIndex == 2) // Kit or Grouped Item
  121. {
  122. Grid.RowDefinitions[1].Height = new GridLength(0, GridUnitType.Pixel);
  123. Grid.RowDefinitions[2].Height = new GridLength(1, GridUnitType.Star);
  124. ComponentsLabel.Content = "Included Items";
  125. Grid.RowDefinitions[3].Height = new GridLength(1, GridUnitType.Auto);
  126. NettLabel.Content = "Nett Cost";
  127. }
  128. }
  129. private void ApplyStrategy()
  130. {
  131. var list = Convert.ToDouble(BaseCost.Value);
  132. if (Strategy.SelectedIndex == 0) // Standard Supplier Item
  133. {
  134. NettCost.Value = BaseCost.Value;
  135. }
  136. else if (Strategy.SelectedIndex == 1) // Subcontractor Item
  137. {
  138. double comps = 0.0F;
  139. foreach (var item in components.Items)
  140. comps += item.Component.NettCost * item.Quantity;
  141. NettCost.Value = Convert.ToDecimal(list - comps);
  142. }
  143. else if (Strategy.SelectedIndex == 2) // Kit or Grouped Item
  144. {
  145. var comps = list;
  146. foreach (var item in components.Items)
  147. comps += item.Component.NettCost * item.Quantity;
  148. NettCost.Value = Convert.ToDecimal(comps);
  149. }
  150. }
  151. private void Strategy_SelectionChanged(object sender, SelectionChangedEventArgs e)
  152. {
  153. ConfigureStrategy();
  154. if (Ready)
  155. ApplyStrategy();
  156. }
  157. private void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  158. {
  159. if (Ready)
  160. ApplyStrategy();
  161. }
  162. }
  163. }