ConsignmentsPanel.xaml.cs 11 KB

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