QuoteCostSheetItemGrid.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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;
  7. using Comal.Classes;
  8. using InABox.Clients;
  9. using InABox.Core;
  10. using InABox.DynamicGrid;
  11. using InABox.WPF;
  12. namespace PRSDesktop
  13. {
  14. public class QuoteCostSheetItemGrid : DynamicDataGrid<QuoteCostSheetItem>
  15. {
  16. private Guid _quotecostsheetid = Guid.Empty;
  17. private readonly DynamicGridStyle header = new DynamicGridRowStyle
  18. {
  19. Background = new SolidColorBrush(Colors.Gray),
  20. Foreground = new SolidColorBrush(Colors.White),
  21. FontWeight = FontWeights.DemiBold
  22. };
  23. public QuoteCostSheetItemGrid()
  24. {
  25. Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.SelectColumns);
  26. AddButton("Reset Cost Sheet", null, ResetCostSheetClick);
  27. AddButton("Update Pricing", null, UpdatePricingClick);
  28. HiddenColumns.Add(x => x.Product.ID);
  29. HiddenColumns.Add(x => x.Product.Deleted);
  30. HiddenColumns.Add(x => x.Cost);
  31. HiddenColumns.Add(x => x.Type);
  32. }
  33. public Guid QuoteCostSheetID
  34. {
  35. get => _quotecostsheetid;
  36. set
  37. {
  38. _quotecostsheetid = value;
  39. Refresh(false, true);
  40. }
  41. }
  42. public Guid CostSheetID { get; set; }
  43. private bool ResetCostSheetClick(Button arg1, CoreRow[] arg2)
  44. {
  45. if (MessageBox.Show("This will reset all the items in this cost sheet!\n\nAre you sure you wish to continue?", "Confirm Reset",
  46. MessageBoxButton.YesNo) != MessageBoxResult.Yes)
  47. return false;
  48. var result = SetupCostSheet(QuoteCostSheetID, CostSheetID);
  49. if (result)
  50. DoChanged();
  51. return result;
  52. }
  53. public bool SetupCostSheet(Guid quotecostsheetid, Guid costsheetid)
  54. {
  55. var result = false;
  56. Progress.Show("Clearing out old Items");
  57. var deletes = new Client<QuoteCostSheetItem>().Load(new Filter<QuoteCostSheetItem>(x => x.CostSheet.ID).IsEqualTo(quotecostsheetid));
  58. if (deletes.Any())
  59. {
  60. result = true;
  61. new Client<QuoteCostSheetItem>().Delete(deletes, "");
  62. }
  63. var updates = new List<QuoteCostSheetItem>();
  64. Progress.SetMessage("Loading Kits");
  65. var kits = new Client<CostSheetKit>().Query(
  66. new Filter<CostSheetKit>(x => x.CostSheet.ID).IsEqualTo(costsheetid),
  67. null,
  68. new SortOrder<CostSheetKit>(x => x.Sequence)
  69. );
  70. var bFirst = true;
  71. foreach (var kit in kits.Rows)
  72. {
  73. Progress.SetMessage(string.Format("Processing {0}", kit.Get<CostSheetKit, string>(x => x.Kit.Description)));
  74. var kitid = kit.EntityLinkID<CostSheetKit, KitLink>(x => x.Kit) ?? Guid.Empty;
  75. if (kitid != Guid.Empty)
  76. {
  77. if (!bFirst)
  78. {
  79. var blank = new QuoteCostSheetItem();
  80. blank.Type = QuoteCostSheetItemLineType.Unused;
  81. blank.CostSheet.ID = quotecostsheetid;
  82. updates.Add(blank);
  83. }
  84. bFirst = false;
  85. var header = new QuoteCostSheetItem();
  86. header.Type = QuoteCostSheetItemLineType.Header;
  87. header.CostSheet.ID = quotecostsheetid;
  88. header.Description = kit.Get<CostSheetKit, string>(x => x.Kit.Description);
  89. updates.Add(header);
  90. var products = new Client<KitProduct>().Query(
  91. new Filter<KitProduct>(x => x.Kit.ID).IsEqualTo(kitid),
  92. null,
  93. new SortOrder<KitProduct>(x => x.Sequence)
  94. );
  95. foreach (var product in products.Rows)
  96. {
  97. var line = new QuoteCostSheetItem();
  98. line.Type = QuoteCostSheetItemLineType.Costing;
  99. line.CostSheet.ID = quotecostsheetid;
  100. line.Product.ID = product.Get<KitProduct, Guid>(x => x.Product.ID);
  101. line.Description = product.Get<KitProduct, string>(x => x.Product.Name);
  102. line.TaxCode.ID = product.Get<KitProduct, Guid>(x => x.Product.TaxCode.ID);
  103. line.TaxCode.Rate = product.Get<KitProduct, double>(x => x.Product.TaxCode.Rate);
  104. //line.TaxRate = line.TaxCode.Rate;
  105. line.Qty = product.Get<KitProduct, double>(x => x.Multiplier);
  106. if (product.Get<KitProduct, bool>(x => x.Product.Charge.Chargeable))
  107. {
  108. if (product.Get<KitProduct, ProductPriceType>(x => x.Product.Charge.PriceType) == ProductPriceType.CostPlus)
  109. line.Cost = product.Get<KitProduct, double>(x => x.Product.NettCost) * (100.0F + product.Get<KitProduct, double>(x => x.Product.Charge.Price) / 100.0F);
  110. else
  111. line.Cost = product.Get<KitProduct, double>(x => x.Product.Charge.Price);
  112. }
  113. updates.Add(line);
  114. }
  115. }
  116. }
  117. Progress.SetMessage("Saving Items");
  118. if (updates.Any())
  119. {
  120. result = true;
  121. new Client<QuoteCostSheetItem>().Save(updates, "");
  122. }
  123. Progress.Close();
  124. return result;
  125. }
  126. private bool UpdatePricingClick(Button arg1, CoreRow[] arg2)
  127. {
  128. var bResult = false;
  129. var pricing = new CostSheetPricingSelection();
  130. if (pricing.ShowDialog() == true)
  131. {
  132. Progress.Show("Updating Costs");
  133. var items = new List<QuoteCostSheetItem>();
  134. foreach (var row in Data.Rows)
  135. {
  136. var item = row.ToObject<QuoteCostSheetItem>();
  137. if (item.Product.IsValid())
  138. items.Add(row.ToObject<QuoteCostSheetItem>());
  139. }
  140. var ids = items.Select(i => i.Product.ID).ToArray();
  141. if (ids.Any())
  142. {
  143. Progress.SetMessage("Loading Products");
  144. var products = new Client<Product>().Query(
  145. new Filter<Product>(x => x.ID).InList(ids),
  146. new Columns<Product>(x=>x.ID)
  147. .Add(x=>x.NettCost)
  148. .Add(x=>x.Supplier.ID)
  149. .Add(x=>x.Charge.Chargeable)
  150. .Add(x=>x.Charge.PriceType)
  151. .Add(x=>x.Charge.Price)
  152. );
  153. var supprods = pricing.PricingType != PricingType.Nominal
  154. ? new Client<SupplierProduct>().Query(
  155. new Filter<SupplierProduct>(x => x.Product.ID).InList(ids),
  156. new Columns<SupplierProduct>(x=>x.SupplierLink.ID)
  157. .Add(x=>x.Product.ID)
  158. .Add(x=>x.CostPrice)
  159. )
  160. : null;
  161. foreach (var item in items)
  162. {
  163. var product = products.Rows.FirstOrDefault(r => r.Get<Product, Guid>(c => c.ID).Equals(item.Product.ID));
  164. if (product.Get<Product, bool>(x => x.Charge.Chargeable))
  165. {
  166. if (product.Get<Product, ProductPriceType>(x => x.Charge.PriceType) == ProductPriceType.FixedPrice)
  167. item.Cost = product.Get<Product, double>(x => x.Charge.Price);
  168. else
  169. {
  170. var supprows = supprods?.Rows?.Where(r => r.Get<SupplierProduct, Guid>(c => c.Product.ID) == item.Product.ID)
  171. ?.ToArray();
  172. double nettCost = 0.0F;
  173. if (pricing.PricingType == PricingType.Nominal)
  174. nettCost = product.Get<Product, double>(x => x.NettCost);
  175. else if (pricing.PricingType == PricingType.Preferred)
  176. {
  177. var preferred = supprows?.FirstOrDefault(r =>
  178. r.Get<SupplierProduct, Guid>(x => x.SupplierLink.ID) ==
  179. product.Get<Product, Guid>(x => x.Supplier.SupplierLink.ID));
  180. nettCost = preferred?.Get<SupplierProduct, double>(c => c.CostPrice)
  181. ?? product.Get<Product, double>(x => x.NettCost);
  182. }
  183. else
  184. {
  185. nettCost = supprows?.Select(r => r.Get<SupplierProduct, double>(c => c.CostPrice)).Min()
  186. ?? product.Get<Product, double>(x => x.NettCost);
  187. }
  188. item.Cost = nettCost * (100.0F + product.Get<Product, double>(x => x.Charge.Price) / 100.0F);
  189. }
  190. }
  191. }
  192. Progress.SetMessage("Saving Changes");
  193. if (items.Any(x => x.IsChanged()))
  194. {
  195. new Client<QuoteCostSheetItem>().Save(items.Where(x => x.IsChanged()), "");
  196. bResult = true;
  197. }
  198. Progress.Close();
  199. MessageBox.Show("All Done");
  200. }
  201. }
  202. if (bResult)
  203. DoChanged();
  204. return true;
  205. }
  206. protected override void Reload(Filters<QuoteCostSheetItem> criteria, Columns<QuoteCostSheetItem> columns,
  207. ref SortOrder<QuoteCostSheetItem> sort,
  208. Action<CoreTable, Exception> action)
  209. {
  210. criteria.Add(new Filter<QuoteCostSheetItem>(x => x.CostSheet.ID).IsEqualTo(QuoteCostSheetID));
  211. base.Reload(criteria, columns, ref sort, action);
  212. }
  213. protected override QuoteCostSheetItem CreateItem()
  214. {
  215. var result = base.CreateItem();
  216. result.CostSheet.ID = QuoteCostSheetID;
  217. return result;
  218. }
  219. protected override DynamicGridStyle GetRowStyle(CoreRow row, DynamicGridStyle style)
  220. {
  221. var type = row.Get<QuoteCostSheetItem, QuoteCostSheetItemLineType>(x => x.Type);
  222. return base.GetRowStyle(row, type == QuoteCostSheetItemLineType.Header ? header : style);
  223. }
  224. }
  225. }