ConsignmentsPanel.xaml.cs 11 KB

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