123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- using System;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- namespace PRSDesktop
- {
- /// <summary>
- /// Interaction logic for ProductPricing.xaml
- /// </summary>
- public partial class ProductPricing : UserControl, IDynamicEditorPage //, IDynamicCustomEditorPage<Product>
- {
- private readonly ProductComponentGrid components = new();
- private ProductPriceComponent[] MasterList = { };
- private readonly Guid ProductID = Guid.Empty;
- public ProductPricing()
- {
- InitializeComponent();
- //components.OnCreateItem += (o, i) => i.Product.ID = Product.ID;
- components.OnItemsChanged += o => ApplyStrategy();
- components.SetValue(Grid.ColumnProperty, 1);
- components.SetValue(Grid.RowProperty, 3);
- components.Margin = new Thickness(5);
- Grid.Children.Add(components);
- }
- public DynamicEditorGrid EditorGrid { get; set; }
- public PageType PageType => PageType.Other;
- public bool Ready { get; set; }
- public void Load(object item, Func<Type, CoreTable> PageDataHandler)
- {
- var product = (Product)item;
- Strategy.SelectedIndex = (int)product.PricingStrategy;
- BaseCost.IsEnabled = !product.UseDefaultSupplierPricing;
- BaseCost.Value = Convert.ToDecimal(product.BaseCost);
- NettCost.Value = Convert.ToDecimal(product.NettCost);
- MasterList = new Client<ProductPriceComponent>().Load(new Filter<ProductPriceComponent>(x => x.Product.ID).IsEqualTo(product.ID));
- components.Items = MasterList.ToList();
- components.Items.Clear();
- components.Items.AddRange(MasterList);
- components.Refresh(true, true);
- ApplyStrategy();
- Ready = true;
- }
- public void BeforeSave(object item)
- {
- var product = (Product)item;
- product.PricingStrategy = (ProductPricingStrategy)Strategy.SelectedIndex;
- // Base Cost is only applicable for standard and subcontractor items
- product.BaseCost = BaseCost.Value.HasValue ? Convert.ToDouble(BaseCost.Value) : 0.00;
- // Components not applicable to standard items
- if (product.PricingStrategy == ProductPricingStrategy.Standard)
- components.Items.Clear();
- // Process the components list (update & delete as required)
- var comps = MasterList.ToList();
- foreach (var comp in components.Items)
- {
- if (comp.IsChanged())
- new Client<ProductPriceComponent>().Save(comp, "Updated by User Edit", (o, e) => { });
- if (comps.Contains(comp))
- comps.Remove(comp);
- }
- foreach (var comp in comps)
- new Client<ProductPriceComponent>().Delete(comp, "Updated by User Edit", (o, e) => { });
- // Nett Price always applies
- product.NettCost = NettCost.Value.HasValue ? Convert.ToDouble(NettCost.Value) : 0.00;
- }
- public void AfterSave(object item)
- {
- var product = (Product)item;
- // First remove any deleted files
- for (var i = 0; i < MasterList.Length; i++)
- if (!components.Items.Any(x => x.ID.Equals(MasterList[i].ID)))
- new Client<ProductPriceComponent>().Delete(MasterList[i], "Component Deleted by User");
- // now add new files
- foreach (var newitem in components.Items)
- {
- newitem.Product.ID = product.ID;
- if (newitem.OriginalValues.Count > 0)
- new Client<ProductPriceComponent>().Save(newitem, "Component Added by User");
- }
- }
- public string Caption()
- {
- return "Pricing Options";
- }
- public int Order()
- {
- return 0;
- }
- public Size MinimumSize()
- {
- return new Size(400, 400);
- }
- private void Components_OnReload(object sender, Filters<ProductPriceComponent> criteria, Columns<ProductPriceComponent> columns,
- ref SortOrder<ProductPriceComponent> sortby)
- {
- criteria.Add(new Filter<ProductPriceComponent>(x => x.Product.ID).IsEqualTo(ProductID));
- }
- private void ConfigureStrategy()
- {
- if (Strategy.SelectedIndex == 0) // Standard Supplier Item
- {
- Grid.RowDefinitions[1].Height = new GridLength(1, GridUnitType.Auto);
- BaseLabel.Content = "Supplier Cost";
- Grid.RowDefinitions[2].Height = new GridLength(0, GridUnitType.Pixel);
- Grid.RowDefinitions[3].Height = new GridLength(0, GridUnitType.Pixel);
- }
- else if (Strategy.SelectedIndex == 1) // Subcontractor Item
- {
- Grid.RowDefinitions[1].Height = new GridLength(1, GridUnitType.Auto);
- BaseLabel.Content = "Work Order Cost";
- Grid.RowDefinitions[2].Height = new GridLength(1, GridUnitType.Star);
- ComponentsLabel.Content = "Subcontractor Materials";
- Grid.RowDefinitions[3].Height = new GridLength(1, GridUnitType.Auto);
- NettLabel.Content = "Labour Cost";
- }
- else if (Strategy.SelectedIndex == 2) // Kit or Grouped Item
- {
- Grid.RowDefinitions[1].Height = new GridLength(0, GridUnitType.Pixel);
- Grid.RowDefinitions[2].Height = new GridLength(1, GridUnitType.Star);
- ComponentsLabel.Content = "Included Items";
- Grid.RowDefinitions[3].Height = new GridLength(1, GridUnitType.Auto);
- NettLabel.Content = "Nett Cost";
- }
- }
- private void ApplyStrategy()
- {
- var list = Convert.ToDouble(BaseCost.Value);
- if (Strategy.SelectedIndex == 0) // Standard Supplier Item
- {
- NettCost.Value = BaseCost.Value;
- }
- else if (Strategy.SelectedIndex == 1) // Subcontractor Item
- {
- double comps = 0.0F;
- foreach (var item in components.Items)
- comps += item.Component.NettCost * item.Quantity;
- NettCost.Value = Convert.ToDecimal(list - comps);
- }
- else if (Strategy.SelectedIndex == 2) // Kit or Grouped Item
- {
- var comps = list;
- foreach (var item in components.Items)
- comps += item.Component.NettCost * item.Quantity;
- NettCost.Value = Convert.ToDecimal(comps);
- }
- }
- private void Strategy_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- ConfigureStrategy();
- if (Ready)
- ApplyStrategy();
- }
- private void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- if (Ready)
- ApplyStrategy();
- }
- }
- }
|