ConsignmentsPanel.xaml.cs 11 KB

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