ConsignmentsPanel.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Input;
  8. using Comal.Classes;
  9. using InABox.Clients;
  10. using InABox.Configuration;
  11. using InABox.Core;
  12. using InABox.DynamicGrid;
  13. using InABox.WPF;
  14. namespace PRSDesktop
  15. {
  16. /// <summary>
  17. /// Interaction logic for ConsignmentsPanel.xaml
  18. /// </summary>
  19. public partial class ConsignmentsPanel : UserControl, IPanel<Consignment>
  20. {
  21. private readonly List<Tuple<Guid, string>> _categories = new();
  22. private readonly List<Tuple<Guid, string>> _types = new();
  23. private ConsignmentScreenSettings settings;
  24. public ConsignmentsPanel()
  25. {
  26. InitializeComponent();
  27. Consignments.HiddenColumns.Add(x => x.Supplier.Code);
  28. Consignments.HiddenColumns.Add(x => x.Supplier.Name);
  29. Consignments.HiddenColumns.Add(x => x.Number);
  30. Consignments.HiddenColumns.Add(x => x.Type.Description);
  31. Consignments.HiddenColumns.Add(x => x.Origin);
  32. Consignments.HiddenColumns.Add(x => x.Description);
  33. Consignments.HiddenColumns.Add(x => x.EstimatedDispatchDate);
  34. Consignments.HiddenColumns.Add(x => x.EstimatedPortArrival);
  35. Consignments.HiddenColumns.Add(x => x.EstimatedWarehouseArrival);
  36. Consignments.HiddenColumns.Add(x => x.ActualDispatchDate);
  37. Consignments.HiddenColumns.Add(x => x.ActualPortArrival);
  38. Consignments.HiddenColumns.Add(x => x.ActualWarehouseArrival);
  39. Consignments.HiddenColumns.Add(x => x.Status);
  40. Consignments.HiddenColumns.Add(x => x.Closed);
  41. Consignments.OnSelectItem += (o, e) =>
  42. {
  43. var row = e.Rows?.FirstOrDefault();
  44. ConsignmentItems.ConsignmentID = row != null ? row.Get<Consignment, Guid>(x => x.ID) : CoreUtils.FullGuid;
  45. ConsignmentItems.Completed = row != null ? row.Get<Consignment, DateTime>(x => x.Closed).IsEmpty() : true;
  46. LoadConsigmment(row);
  47. ConsignmentItems.Refresh(false, true);
  48. };
  49. ConsignmentItems.OnChanged += ConsignmentItemsChanged;
  50. }
  51. public bool IsReady { get; set; }
  52. public event DataModelUpdateEvent OnUpdateDataModel;
  53. public void CreateToolbarButtons(IPanelHost host)
  54. {
  55. }
  56. public string SectionName => "Consignments";
  57. public DataModel DataModel(Selection selection)
  58. {
  59. var ids = Consignments.ExtractValues(c => c.ID, selection).ToArray();
  60. return new BaseDataModel<Consignment>(new Filter<Consignment>(x => x.ID).InList(ids));
  61. }
  62. public void Heartbeat(TimeSpan time)
  63. {
  64. // Nothing to Do Here
  65. }
  66. public void Refresh()
  67. {
  68. Consignments.Refresh(false, true);
  69. }
  70. public Dictionary<string, object[]> Selected()
  71. {
  72. return new Dictionary<string, object[]>();
  73. }
  74. public void Setup()
  75. {
  76. settings = new UserConfiguration<ConsignmentScreenSettings>().Load();
  77. SplitPanel.View = settings.ViewType == ScreenViewType.Register ? DynamicSplitPanelView.Master :
  78. settings.ViewType == ScreenViewType.Details ? DynamicSplitPanelView.Detail : DynamicSplitPanelView.Combined;
  79. SplitPanel.MasterWidth = settings.ConsignmentColumnWidth;
  80. StatusList.SelectedIndex = settings.ShowAll ? 1 : 0;
  81. StatusList.SelectionChanged += StatusList_SelectionChanged;
  82. _types.Add(new Tuple<Guid, string>(CoreUtils.FullGuid, "All Types"));
  83. _categories.Add(new Tuple<Guid, string>(CoreUtils.FullGuid, "All Categories"));
  84. var query = new MultiQuery();
  85. query.Add(
  86. LookupFactory.DefineFilter<ConsignmentType>(),
  87. LookupFactory.DefineColumns<ConsignmentType>(),
  88. LookupFactory.DefineSort<ConsignmentType>()
  89. );
  90. query.Add(
  91. LookupFactory.DefineFilter<PurchaseOrderCategory>(),
  92. LookupFactory.DefineColumns<PurchaseOrderCategory>(),
  93. LookupFactory.DefineSort<PurchaseOrderCategory>()
  94. );
  95. query.Query();
  96. LoadLookups<ConsignmentType>(query, _types, x => x.Description);
  97. TypeList.ItemsSource = _types;
  98. TypeList.SelectedValue = _types.Any(x => x.Item1 == settings.SelectedType) ? settings.SelectedType : CoreUtils.FullGuid;
  99. Consignments.SelectedType = (Guid)TypeList.SelectedValue;
  100. TypeList.SelectionChanged += TypeList_SelectionChanged;
  101. LoadLookups<PurchaseOrderCategory>(query, _categories, x => x.Description);
  102. CategoryList.ItemsSource = _categories;
  103. CategoryList.SelectedValue =
  104. _categories.Any(x => x.Item1 == settings.SelectedCategory) ? settings.SelectedCategory : CoreUtils.FullGuid;
  105. Consignments.SelectedCategory = (Guid)CategoryList.SelectedValue;
  106. CategoryList.SelectionChanged += CategoryList_SelectionChanged;
  107. Consignments.ShowAll = settings.ShowAll;
  108. Consignments.ColumnsTag = settings.ViewType == ScreenViewType.Register ? settings.ViewType.ToString() : "";
  109. Consignments.Refresh(true, false);
  110. ConsignmentItems.Refresh(true, false);
  111. }
  112. public void Shutdown()
  113. {
  114. }
  115. private void ConsignmentItemsChanged(IDynamicGrid sender)
  116. {
  117. var consrow = Consignments.Data.Rows.FirstOrDefault(r => r.Get<Consignment, Guid>(c => c.ID).Equals(ConsignmentItems.ConsignmentID));
  118. if (consrow != null)
  119. {
  120. MessageBox.Show("Cannot find Consignment");
  121. return;
  122. }
  123. var consignment = consrow.ToObject<Consignment>();
  124. var allreceived = !ConsignmentItems.Data.Rows.Any(r => r.Get<PurchaseOrderItem, DateTime>(c => c.ReceivedDate).IsEmpty());
  125. var anyreceived = ConsignmentItems.Data.Rows.Any(r => !r.Get<PurchaseOrderItem, DateTime>(c => c.ReceivedDate).IsEmpty());
  126. if (anyreceived)
  127. {
  128. if (consignment.ActualWarehouseArrival.IsEmpty())
  129. consignment.ActualWarehouseArrival = DateTime.Now;
  130. }
  131. else
  132. {
  133. if (!consignment.ActualWarehouseArrival.IsEmpty())
  134. consignment.ActualWarehouseArrival = DateTime.MinValue;
  135. }
  136. if (allreceived)
  137. {
  138. if (consignment.Closed.IsEmpty())
  139. consignment.Closed = DateTime.Now;
  140. }
  141. else
  142. {
  143. if (!consignment.Closed.IsEmpty())
  144. consignment.Closed = DateTime.MinValue;
  145. }
  146. if (consignment.IsChanged())
  147. using (new WaitCursor())
  148. {
  149. new Client<Consignment>().Save(consignment, "Consignment Closed Date Updated");
  150. }
  151. }
  152. private string CheckDate(DateTime date)
  153. {
  154. return date.IsEmpty() ? "" : date.ToShortDateString();
  155. }
  156. private void LoadConsigmment(CoreRow row)
  157. {
  158. SupplierCode.Text = row == null ? "" : row.Get<Consignment, string>(x => x.Supplier.Code);
  159. SupplierName.Text = row == null ? "" : row.Get<Consignment, string>(x => x.Supplier.Name);
  160. Number.Text = row == null ? "" : row.Get<Consignment, string>(x => x.Number);
  161. Type.Text = row == null ? "" : row.Get<Consignment, string>(x => x.Type.Description);
  162. Origin.Text = row == null ? "" : row.Get<Consignment, string>(x => x.Origin);
  163. Description.Text = row == null ? "" : row.Get<Consignment, string>(x => x.Description);
  164. EstShip.Text = row == null ? "" : CheckDate(row.Get<Consignment, DateTime>(x => x.EstimatedDispatchDate));
  165. EstPort.Text = row == null ? "" : CheckDate(row.Get<Consignment, DateTime>(x => x.EstimatedPortArrival));
  166. EstArrival.Text = row == null ? "" : CheckDate(row.Get<Consignment, DateTime>(x => x.EstimatedWarehouseArrival));
  167. ActShip.Text = row == null ? "" : CheckDate(row.Get<Consignment, DateTime>(x => x.ActualDispatchDate));
  168. ActPort.Text = row == null ? "" : CheckDate(row.Get<Consignment, DateTime>(x => x.ActualPortArrival));
  169. ActArrival.Text = row == null ? "" : CheckDate(row.Get<Consignment, DateTime>(x => x.ActualWarehouseArrival));
  170. Status.Text = row == null ? "" : row.Get<Consignment, string>(x => x.Status);
  171. }
  172. private void CategoryList_SelectionChanged(object sender, SelectionChangedEventArgs e)
  173. {
  174. if (!IsReady)
  175. return;
  176. Consignments.SelectedCategory = (Guid)CategoryList.SelectedValue;
  177. settings.SelectedCategory = Consignments.SelectedCategory;
  178. new UserConfiguration<ConsignmentScreenSettings>().Save(settings);
  179. Refresh();
  180. }
  181. private void TypeList_SelectionChanged(object sender, SelectionChangedEventArgs e)
  182. {
  183. if (!IsReady)
  184. return;
  185. Consignments.SelectedType = (Guid)TypeList.SelectedValue;
  186. settings.SelectedType = Consignments.SelectedType;
  187. new UserConfiguration<ConsignmentScreenSettings>().Save(settings);
  188. Refresh();
  189. }
  190. private void LoadLookups<T>(MultiQuery query, List<Tuple<Guid, string>> lookups, Expression<Func<T, string>> display) where T : Entity
  191. {
  192. var table = query.Get<T>();
  193. foreach (var row in table.Rows)
  194. lookups.Add(new Tuple<Guid, string>(row.Get<T, Guid>(c => c.ID), row.Get(display)));
  195. }
  196. private void StatusList_SelectionChanged(object sender, SelectionChangedEventArgs e)
  197. {
  198. if (!IsReady)
  199. return;
  200. Consignments.ShowAll = StatusList.SelectedIndex == 1;
  201. settings.ShowAll = Consignments.ShowAll;
  202. new UserConfiguration<ConsignmentScreenSettings>().Save(settings);
  203. Refresh();
  204. }
  205. private void Search_KeyUp(object sender, KeyEventArgs e)
  206. {
  207. }
  208. private void SplitPanel_OnChanged(object sender, DynamicSplitPanelChangedArgs e)
  209. {
  210. settings.ViewType = SplitPanel.View == DynamicSplitPanelView.Master ? ScreenViewType.Register :
  211. SplitPanel.View == DynamicSplitPanelView.Detail ? ScreenViewType.Details : ScreenViewType.Combined;
  212. settings.ConsignmentColumnWidth = SplitPanel.MasterWidth;
  213. var newTag = settings.ViewType == ScreenViewType.Register ? settings.ViewType.ToString() : "";
  214. if (Consignments.ColumnsTag != newTag)
  215. {
  216. Consignments.ColumnsTag = newTag;
  217. Consignments.Refresh(true, true);
  218. }
  219. new UserConfiguration<ConsignmentScreenSettings>().Save(settings);
  220. }
  221. }
  222. }