SupplierPurchaseOrders.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using Comal.Classes;
  8. using InABox.Clients;
  9. using InABox.Core;
  10. using InABox.DynamicGrid;
  11. using InABox.WPF;
  12. using sun.misc;
  13. namespace PRSDesktop
  14. {
  15. public class SupplierPurchaseOrders : DynamicDataGrid<PurchaseOrder>
  16. {
  17. private readonly Button close;
  18. public SupplierPurchaseOrders()
  19. {
  20. Options.AddRange(
  21. DynamicGridOption.RecordCount,
  22. DynamicGridOption.FilterRows,
  23. DynamicGridOption.SelectColumns
  24. );
  25. OnEditorValueChanged += SupplierPurchaseOrders_OnEditorValueChanged;
  26. HiddenColumns.Add(x => x.ClosedDate);
  27. HiddenColumns.Add(x => x.Balance);
  28. close = AddButton("Close Order", null, CloseOrder);
  29. close.IsEnabled = false;
  30. }
  31. private Dictionary<string, object> SupplierPurchaseOrders_OnEditorValueChanged(object sender, string name, object value)
  32. {
  33. Dictionary<string, object> result = new Dictionary<string, object>();
  34. var form = sender as DynamicEditorForm;
  35. var itemspage = form?.Pages.FirstOrDefault(x => x is DynamicOneToManyGrid<PurchaseOrder, PurchaseOrderItem>) as DynamicOneToManyGrid<PurchaseOrder, PurchaseOrderItem>;
  36. if ((itemspage == null) || (itemspage.Items.Count == 0))
  37. return result;
  38. if (name.Equals("DueDate") && (MessageBox.Show("Update Due Date on existing items?", "Alert", MessageBoxButton.YesNo) == MessageBoxResult.Yes))
  39. {
  40. foreach (var item in itemspage.Items)
  41. item.DueDate = Convert.ToDateTime(value);
  42. itemspage.Refresh(false, true);
  43. }
  44. else if (name.Equals("SupplierLink.ID") &&
  45. (MessageBox.Show("Update Supplier Pricing to existihg items?", "Alert", MessageBoxButton.YesNo) == MessageBoxResult.Yes))
  46. {
  47. PurchaseOrder.UpdateCosts(itemspage.Items, (Guid)value, null);
  48. // var productids = itemspage.Items.Where(x => x.Product.ID != Guid.Empty).Select(x => x.Product.ID).ToArray();
  49. // MultiQuery query = new MultiQuery();
  50. // query.Add(
  51. // new Filter<SupplierProduct>(x=>x.SupplierLink.ID).IsEqualTo(value).And(x=>x.ProductLink.ID).InList(productids),
  52. // new Columns<SupplierProduct>(x=>x.ProductLink.ID).Add(x=>x.Job.ID).Add(x=>x.CostPrice)
  53. // );
  54. // query.Add(
  55. // new Filter<Product>(x=>x.ID).InList(productids),
  56. // new Columns<Product>(x=>x.ID).Add(x=>x.NettCost)
  57. // );
  58. // query.Query();
  59. //
  60. // foreach (var item in itemspage.Items)
  61. // {
  62. // CoreRow? row = query.Get<SupplierProduct>()?.Rows.FirstOrDefault(r =>
  63. // (r.Get<SupplierProduct, Guid>(c => c.ProductLink.ID) == item.Product.ID)
  64. // && (r.Get<SupplierProduct, Guid>(c => c.Job.ID) == item.Job.ID)
  65. // );
  66. // if (row == null)
  67. // row = query.Get<SupplierProduct>()?.Rows.FirstOrDefault(r =>
  68. // (r.Get<SupplierProduct, Guid>(c => c.ProductLink.ID) == item.Product.ID)
  69. // && (r.Get<SupplierProduct, Guid>(c => c.Job.ID) == Guid.Empty)
  70. // );
  71. // if (row != null)
  72. // item.Cost = row.Get<SupplierProduct, double>(c => c.CostPrice);
  73. // else
  74. // {
  75. // row = query.Get<Product>()?.Rows.FirstOrDefault(r =>
  76. // (r.Get<Product, Guid>(c => c.ID) == item.Product.ID)
  77. // );
  78. // if (row != null)
  79. // item.Cost = row.Get<Product, double>(c => c.NettCost);
  80. // }
  81. //
  82. // }
  83. itemspage.Refresh(false, true);
  84. }
  85. return result;
  86. }
  87. // private List<SupplierProduct> GetSupplierProducts(object value)
  88. // {
  89. // List<SupplierProduct> supplierProducts = new List<SupplierProduct>();
  90. // CoreTable supplierProductstable = new Client<SupplierProduct>().Query(new Filter<SupplierProduct>(x => x.SupplierLink.ID).IsEqualTo(Guid.Parse(value.ToString())),
  91. // new Columns<SupplierProduct>(
  92. // x => x.ID,
  93. // x => x.SupplierLink.ID,
  94. // x => x.ProductLink.ID,
  95. // x => x.Job.ID,
  96. // x => x.TradePrice));
  97. // if (supplierProductstable.Rows.Any())
  98. // {
  99. // foreach (CoreRow row in supplierProductstable.Rows)
  100. // {
  101. // SupplierProduct supplierProduct = row.ToObject<SupplierProduct>();
  102. // supplierProducts.Add(supplierProduct);
  103. // }
  104. // }
  105. // return supplierProducts;
  106. // }
  107. public bool ShowAll { get; set; }
  108. public Guid SelectedCategory { get; set; }
  109. protected override void Reload(Filters<PurchaseOrder> criteria, Columns<PurchaseOrder> columns, ref SortOrder<PurchaseOrder> sort,
  110. Action<CoreTable, Exception> action)
  111. {
  112. if (!ShowAll)
  113. criteria.Add(new Filter<PurchaseOrder>(x => x.ClosedDate).IsEqualTo(DateTime.MinValue));
  114. if (SelectedCategory != CoreUtils.FullGuid)
  115. criteria.Add(new Filter<PurchaseOrder>(x => x.Category.ID).IsEqualTo(SelectedCategory));
  116. base.Reload(criteria, columns, ref sort, action);
  117. }
  118. protected override void SelectItems(CoreRow[] rows)
  119. {
  120. close.IsEnabled = rows != null && rows.Any(r =>
  121. r.Get<PurchaseOrder, DateTime>(c => c.ClosedDate).IsEmpty() && r.Get<PurchaseOrder, double>(x => x.Balance).Equals(0.0F));
  122. base.SelectItems(rows);
  123. }
  124. private bool CloseOrder(Button arg1, CoreRow[] arg2)
  125. {
  126. var orders = new List<PurchaseOrder>();
  127. foreach (var row in arg2)
  128. {
  129. var order = row.ToObject<PurchaseOrder>();
  130. order.ClosedDate = DateTime.Now;
  131. orders.Add(order);
  132. }
  133. new Client<PurchaseOrder>().Save(orders, "Marking Order as Closed");
  134. return true;
  135. }
  136. }
  137. }