ConsignmentsPanel.xaml.cs 12 KB

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