ConsignmentsPanel.xaml.cs 11 KB

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