123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Core;
- using InABox.DynamicGrid;
- using InABox.WPF;
- namespace PRSDesktop
- {
- public class QuoteCostSheetItemGrid : DynamicDataGrid<QuoteCostSheetItem>
- {
- private Guid _quotecostsheetid = Guid.Empty;
- private readonly DynamicGridStyle header = new DynamicGridRowStyle
- {
- Background = new SolidColorBrush(Colors.Gray),
- Foreground = new SolidColorBrush(Colors.White),
- FontWeight = FontWeights.DemiBold
- };
- public QuoteCostSheetItemGrid()
- {
- Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.SelectColumns);
- AddButton("Reset Cost Sheet", null, ResetCostSheetClick);
- AddButton("Update Pricing", null, UpdatePricingClick);
- HiddenColumns.Add(x => x.Product.ID);
- HiddenColumns.Add(x => x.Product.Deleted);
- HiddenColumns.Add(x => x.Cost);
- HiddenColumns.Add(x => x.Type);
- }
- public Guid QuoteCostSheetID
- {
- get => _quotecostsheetid;
- set
- {
- _quotecostsheetid = value;
- Refresh(false, true);
- }
- }
- public Guid CostSheetID { get; set; }
- private bool ResetCostSheetClick(Button arg1, CoreRow[] arg2)
- {
- if (MessageBox.Show("This will reset all the items in this cost sheet!\n\nAre you sure you wish to continue?", "Confirm Reset",
- MessageBoxButton.YesNo) != MessageBoxResult.Yes)
- return false;
- var result = SetupCostSheet(QuoteCostSheetID, CostSheetID);
- if (result)
- DoChanged();
- return result;
- }
- public bool SetupCostSheet(Guid quotecostsheetid, Guid costsheetid)
- {
- var result = false;
- Progress.Show("Clearing out old Items");
- var deletes = new Client<QuoteCostSheetItem>().Load(new Filter<QuoteCostSheetItem>(x => x.CostSheet.ID).IsEqualTo(quotecostsheetid));
- if (deletes.Any())
- {
- result = true;
- new Client<QuoteCostSheetItem>().Delete(deletes, "");
- }
- var updates = new List<QuoteCostSheetItem>();
- Progress.SetMessage("Loading Kits");
- var kits = new Client<CostSheetKit>().Query(
- new Filter<CostSheetKit>(x => x.CostSheet.ID).IsEqualTo(costsheetid),
- null,
- new SortOrder<CostSheetKit>(x => x.Sequence)
- );
- var bFirst = true;
- foreach (var kit in kits.Rows)
- {
- Progress.SetMessage(string.Format("Processing {0}", kit.Get<CostSheetKit, string>(x => x.Kit.Description)));
- var kitid = kit.EntityLinkID<CostSheetKit, KitLink>(x => x.Kit) ?? Guid.Empty;
- if (kitid != Guid.Empty)
- {
- if (!bFirst)
- {
- var blank = new QuoteCostSheetItem();
- blank.Type = QuoteCostSheetItemLineType.Unused;
- blank.CostSheet.ID = quotecostsheetid;
- updates.Add(blank);
- }
- bFirst = false;
- var header = new QuoteCostSheetItem();
- header.Type = QuoteCostSheetItemLineType.Header;
- header.CostSheet.ID = quotecostsheetid;
- header.Description = kit.Get<CostSheetKit, string>(x => x.Kit.Description);
- updates.Add(header);
- var products = new Client<KitProduct>().Query(
- new Filter<KitProduct>(x => x.Kit.ID).IsEqualTo(kitid),
- null,
- new SortOrder<KitProduct>(x => x.Sequence)
- );
- foreach (var product in products.Rows)
- {
- var line = new QuoteCostSheetItem();
- line.Type = QuoteCostSheetItemLineType.Costing;
- line.CostSheet.ID = quotecostsheetid;
- line.Product.ID = product.Get<KitProduct, Guid>(x => x.Product.ID);
- line.Description = product.Get<KitProduct, string>(x => x.Product.Name);
- line.TaxCode.ID = product.Get<KitProduct, Guid>(x => x.Product.TaxCode.ID);
- line.TaxCode.Rate = product.Get<KitProduct, double>(x => x.Product.TaxCode.Rate);
- //line.TaxRate = line.TaxCode.Rate;
- line.Qty = product.Get<KitProduct, double>(x => x.Multiplier);
- if (product.Get<KitProduct, bool>(x => x.Product.Charge.Chargeable))
- {
- if (product.Get<KitProduct, ProductPriceType>(x => x.Product.Charge.PriceType) == ProductPriceType.CostPlus)
- line.Cost = product.Get<KitProduct, double>(x => x.Product.NettCost) * (100.0F + product.Get<KitProduct, double>(x => x.Product.Charge.Price) / 100.0F);
- else
- line.Cost = product.Get<KitProduct, double>(x => x.Product.Charge.Price);
- }
- updates.Add(line);
- }
- }
- }
- Progress.SetMessage("Saving Items");
- if (updates.Any())
- {
- result = true;
- new Client<QuoteCostSheetItem>().Save(updates, "");
- }
- Progress.Close();
- return result;
- }
- private bool UpdatePricingClick(Button arg1, CoreRow[] arg2)
- {
- var bResult = false;
- var pricing = new CostSheetPricingSelection();
- if (pricing.ShowDialog() == true)
- {
- Progress.Show("Updating Costs");
- var items = new List<QuoteCostSheetItem>();
- foreach (var row in Data.Rows)
- {
- var item = row.ToObject<QuoteCostSheetItem>();
- if (item.Product.IsValid())
- items.Add(row.ToObject<QuoteCostSheetItem>());
- }
- var ids = items.Select(i => i.Product.ID).ToArray();
- if (ids.Any())
- {
- Progress.SetMessage("Loading Products");
- var products = new Client<Product>().Query(
- new Filter<Product>(x => x.ID).InList(ids),
- new Columns<Product>(x=>x.ID)
- .Add(x=>x.NettCost)
- .Add(x=>x.Supplier.ID)
- .Add(x=>x.Charge.Chargeable)
- .Add(x=>x.Charge.PriceType)
- .Add(x=>x.Charge.Price)
- );
- var supprods = pricing.PricingType != PricingType.Nominal
- ? new Client<SupplierProduct>().Query(
- new Filter<SupplierProduct>(x => x.Product.ID).InList(ids),
- new Columns<SupplierProduct>(x=>x.SupplierLink.ID)
- .Add(x=>x.Product.ID)
- .Add(x=>x.CostPrice)
- )
- : null;
- foreach (var item in items)
- {
- var product = products.Rows.FirstOrDefault(r => r.Get<Product, Guid>(c => c.ID).Equals(item.Product.ID));
- if (product.Get<Product, bool>(x => x.Charge.Chargeable))
- {
- if (product.Get<Product, ProductPriceType>(x => x.Charge.PriceType) == ProductPriceType.FixedPrice)
- item.Cost = product.Get<Product, double>(x => x.Charge.Price);
- else
- {
- var supprows = supprods?.Rows?.Where(r => r.Get<SupplierProduct, Guid>(c => c.Product.ID) == item.Product.ID)
- ?.ToArray();
- double nettCost = 0.0F;
- if (pricing.PricingType == PricingType.Nominal)
- nettCost = product.Get<Product, double>(x => x.NettCost);
- else if (pricing.PricingType == PricingType.Preferred)
- {
- var preferred = supprows?.FirstOrDefault(r =>
- r.Get<SupplierProduct, Guid>(x => x.SupplierLink.ID) ==
- product.Get<Product, Guid>(x => x.Supplier.SupplierLink.ID));
- nettCost = preferred?.Get<SupplierProduct, double>(c => c.CostPrice)
- ?? product.Get<Product, double>(x => x.NettCost);
- }
- else
- {
- nettCost = supprows?.Select(r => r.Get<SupplierProduct, double>(c => c.CostPrice)).Min()
- ?? product.Get<Product, double>(x => x.NettCost);
- }
- item.Cost = nettCost * (100.0F + product.Get<Product, double>(x => x.Charge.Price) / 100.0F);
- }
- }
- }
- Progress.SetMessage("Saving Changes");
- if (items.Any(x => x.IsChanged()))
- {
- new Client<QuoteCostSheetItem>().Save(items.Where(x => x.IsChanged()), "");
- bResult = true;
- }
- Progress.Close();
- MessageBox.Show("All Done");
- }
- }
- if (bResult)
- DoChanged();
- return true;
- }
- protected override void Reload(Filters<QuoteCostSheetItem> criteria, Columns<QuoteCostSheetItem> columns,
- ref SortOrder<QuoteCostSheetItem> sort,
- Action<CoreTable, Exception> action)
- {
- criteria.Add(new Filter<QuoteCostSheetItem>(x => x.CostSheet.ID).IsEqualTo(QuoteCostSheetID));
- base.Reload(criteria, columns, ref sort, action);
- }
- protected override QuoteCostSheetItem CreateItem()
- {
- var result = base.CreateItem();
- result.CostSheet.ID = QuoteCostSheetID;
- return result;
- }
- protected override DynamicGridStyle GetRowStyle(CoreRow row, DynamicGridStyle style)
- {
- var type = row.Get<QuoteCostSheetItem, QuoteCostSheetItemLineType>(x => x.Type);
- return base.GetRowStyle(row, type == QuoteCostSheetItemLineType.Header ? header : style);
- }
- }
- }
|