SupplierPurchaseOrders.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. //try
  35. //{
  36. // var form = sender as DynamicEditorForm;
  37. // var itemspage = form?.Pages.FirstOrDefault(x => x is DynamicOneToManyGrid<PurchaseOrder, PurchaseOrderItem>) as DynamicOneToManyGrid<PurchaseOrder, PurchaseOrderItem>;
  38. // if ((itemspage == null) || (itemspage.Items.Count == 0))
  39. // return result;
  40. // if (name.Equals("DueDate") && (MessageBox.Show("Update Due Date on existing items?", "Alert", MessageBoxButton.YesNo) == MessageBoxResult.Yes))
  41. // {
  42. // foreach (var item in itemspage.Items)
  43. // item.DueDate = Convert.ToDateTime(value);
  44. // itemspage.Refresh(false, true);
  45. // }
  46. // else if (name.Equals("SupplierLink.ID") &&
  47. // (MessageBox.Show("Update Supplier Pricing to existihg items?", "Alert", MessageBoxButton.YesNo) == MessageBoxResult.Yes))
  48. // {
  49. // PurchaseOrder.UpdateCosts(itemspage.Items, (Guid)value, null);
  50. // // var productids = itemspage.Items.Where(x => x.Product.ID != Guid.Empty).Select(x => x.Product.ID).ToArray();
  51. // // MultiQuery query = new MultiQuery();
  52. // // query.Add(
  53. // // new Filter<SupplierProduct>(x=>x.SupplierLink.ID).IsEqualTo(value).And(x=>x.ProductLink.ID).InList(productids),
  54. // // new Columns<SupplierProduct>(x=>x.ProductLink.ID).Add(x=>x.Job.ID).Add(x=>x.CostPrice)
  55. // // );
  56. // // query.Add(
  57. // // new Filter<Product>(x=>x.ID).InList(productids),
  58. // // new Columns<Product>(x=>x.ID).Add(x=>x.NettCost)
  59. // // );
  60. // // query.Query();
  61. // //
  62. // // foreach (var item in itemspage.Items)
  63. // // {
  64. // // CoreRow? row = query.Get<SupplierProduct>()?.Rows.FirstOrDefault(r =>
  65. // // (r.Get<SupplierProduct, Guid>(c => c.ProductLink.ID) == item.Product.ID)
  66. // // && (r.Get<SupplierProduct, Guid>(c => c.Job.ID) == item.Job.ID)
  67. // // );
  68. // // if (row == null)
  69. // // row = query.Get<SupplierProduct>()?.Rows.FirstOrDefault(r =>
  70. // // (r.Get<SupplierProduct, Guid>(c => c.ProductLink.ID) == item.Product.ID)
  71. // // && (r.Get<SupplierProduct, Guid>(c => c.Job.ID) == Guid.Empty)
  72. // // );
  73. // // if (row != null)
  74. // // item.Cost = row.Get<SupplierProduct, double>(c => c.CostPrice);
  75. // // else
  76. // // {
  77. // // row = query.Get<Product>()?.Rows.FirstOrDefault(r =>
  78. // // (r.Get<Product, Guid>(c => c.ID) == item.Product.ID)
  79. // // );
  80. // // if (row != null)
  81. // // item.Cost = row.Get<Product, double>(c => c.NettCost);
  82. // // }
  83. // //
  84. // // }
  85. // itemspage.Refresh(false, true);
  86. // }
  87. //}
  88. //catch
  89. //{ }
  90. return result;
  91. }
  92. // private List<SupplierProduct> GetSupplierProducts(object value)
  93. // {
  94. // List<SupplierProduct> supplierProducts = new List<SupplierProduct>();
  95. // CoreTable supplierProductstable = new Client<SupplierProduct>().Query(new Filter<SupplierProduct>(x => x.SupplierLink.ID).IsEqualTo(Guid.Parse(value.ToString())),
  96. // new Columns<SupplierProduct>(
  97. // x => x.ID,
  98. // x => x.SupplierLink.ID,
  99. // x => x.ProductLink.ID,
  100. // x => x.Job.ID,
  101. // x => x.TradePrice));
  102. // if (supplierProductstable.Rows.Any())
  103. // {
  104. // foreach (CoreRow row in supplierProductstable.Rows)
  105. // {
  106. // SupplierProduct supplierProduct = row.ToObject<SupplierProduct>();
  107. // supplierProducts.Add(supplierProduct);
  108. // }
  109. // }
  110. // return supplierProducts;
  111. // }
  112. public bool ShowAll { get; set; }
  113. public Guid SelectedCategory { get; set; }
  114. protected override void Reload(Filters<PurchaseOrder> criteria, Columns<PurchaseOrder> columns, ref SortOrder<PurchaseOrder> sort,
  115. Action<CoreTable, Exception> action)
  116. {
  117. if (!ShowAll)
  118. criteria.Add(new Filter<PurchaseOrder>(x => x.ClosedDate).IsEqualTo(DateTime.MinValue));
  119. if (SelectedCategory != CoreUtils.FullGuid)
  120. criteria.Add(new Filter<PurchaseOrder>(x => x.Category.ID).IsEqualTo(SelectedCategory));
  121. base.Reload(criteria, columns, ref sort, action);
  122. }
  123. protected override void SelectItems(CoreRow[] rows)
  124. {
  125. close.IsEnabled = rows != null && rows.Any(r =>
  126. r.Get<PurchaseOrder, DateTime>(c => c.ClosedDate).IsEmpty() && r.Get<PurchaseOrder, double>(x => x.Balance).Equals(0.0F));
  127. base.SelectItems(rows);
  128. }
  129. private bool CloseOrder(Button arg1, CoreRow[] arg2)
  130. {
  131. var orders = new List<PurchaseOrder>();
  132. foreach (var row in arg2)
  133. {
  134. var order = row.ToObject<PurchaseOrder>();
  135. order.ClosedDate = DateTime.Now;
  136. orders.Add(order);
  137. }
  138. new Client<PurchaseOrder>().Save(orders, "Marking Order as Closed");
  139. return true;
  140. }
  141. }
  142. }