QuotePanel.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Threading;
  8. using Comal.Classes;
  9. using InABox.Clients;
  10. using InABox.Configuration;
  11. using InABox.Core;
  12. using InABox.DynamicGrid;
  13. namespace PRSDesktop
  14. {
  15. /// <summary>
  16. /// Interaction logic for JobPanel.xaml
  17. /// </summary>
  18. public partial class QuotePanel : UserControl, IPanel<Quote>
  19. {
  20. private int CurrentPage = -1;
  21. private DateTime lastselection = DateTime.MaxValue;
  22. private IDataModelSource modelsource;
  23. private QuoteContracts QuoteContracts;
  24. private QuoteCostSheets QuoteCostSheets;
  25. private QuoteDesigns QuoteDesigns;
  26. //ComboBox QuoteStatus = null;
  27. //Quotes Quotes = null;
  28. //TabControl QuotePages = null;
  29. private QuoteDetails QuoteDetails;
  30. private QuoteDiagrams QuoteDiagrams;
  31. private QuoteDocuments QuoteDocuments;
  32. private QuoteProposals QuoteProposals;
  33. private QuoteTakeoffs QuoteTakeoffs;
  34. private QuoteScreenSettings settings;
  35. private readonly DispatcherTimer timer = new();
  36. public QuotePanel()
  37. {
  38. InitializeComponent();
  39. Quotes.OnDoubleClick += OpenQuote;
  40. }
  41. public bool IsReady { get; set; }
  42. public event DataModelUpdateEvent OnUpdateDataModel;
  43. public Dictionary<string, object[]> Selected()
  44. {
  45. var result = new Dictionary<string, object[]> { { typeof(Job).EntityName(), Quotes.SelectedRows } };
  46. if (QuotePages.SelectedIndex == (int)PageIndex.Details)
  47. return QuoteDetails.Selected();
  48. if (QuotePages.SelectedIndex == (int)PageIndex.Documents)
  49. result[typeof(QuoteDocument).EntityName()] = QuoteDocuments.SelectedRows;
  50. else if (QuotePages.SelectedIndex == (int)PageIndex.Diagrams)
  51. return QuoteDiagrams.Selected();
  52. else if (QuotePages.SelectedIndex == (int)PageIndex.Takeoffs)
  53. result[typeof(QuoteTakeoff).EntityName()] = QuoteTakeoffs.SelectedRows;
  54. else if (QuotePages.SelectedIndex == (int)PageIndex.Designs)
  55. return QuoteDesigns.Selected();
  56. else if (QuotePages.SelectedIndex == (int)PageIndex.CostSheets)
  57. return QuoteCostSheets.Selected();
  58. else if (QuotePages.SelectedIndex == (int)PageIndex.Proposals)
  59. return QuoteProposals.Selected();
  60. else if (QuotePages.SelectedIndex == (int)PageIndex.Contract)
  61. return QuoteContracts.Selected();
  62. return result;
  63. }
  64. public void Setup()
  65. {
  66. QuoteDiagramSymbolCache.Refresh();
  67. settings = new UserConfiguration<QuoteScreenSettings>().Load();
  68. SplitPanel.View = settings.ViewType == ScreenViewType.Register
  69. ? DynamicSplitPanelView.Master
  70. : settings.ViewType == ScreenViewType.Details
  71. ? DynamicSplitPanelView.Detail
  72. : DynamicSplitPanelView.Combined;
  73. SplitPanel.MasterWidth = settings.QuoteColumnWidth;
  74. Diagrams.Visibility = ClientFactory.IsSupported<QuoteDiagram>() ? Visibility.Visible : Visibility.Collapsed;
  75. var sc = new Dictionary<Guid, string> { { Guid.Empty, "All Quotes" } };
  76. var statuses = new Client<QuoteStatus>().Query();
  77. foreach (var row in statuses.Rows)
  78. sc[row.Get<QuoteStatus, Guid>(x => x.ID)] = row.Get<QuoteStatus, string>(x => x.Description);
  79. QuoteStatus.ItemsSource = sc;
  80. if (sc.ContainsKey(settings.QuoteStatus))
  81. QuoteStatus.SelectedValue = settings.QuoteStatus;
  82. else
  83. QuoteStatus.SelectedValue = sc.Keys.First();
  84. Quotes.OnSelectItem += Quotes_OnSelectItem;
  85. Quotes.ColumnsTag = settings.ViewType == ScreenViewType.Register ? settings.ViewType.ToString() : "";
  86. Quotes.Refresh(true, false);
  87. }
  88. public void Shutdown()
  89. {
  90. }
  91. public void Refresh()
  92. {
  93. if (!timer.IsEnabled)
  94. {
  95. timer.Interval = new TimeSpan(0, 0, 0, 0, 100);
  96. timer.Tick += Timer_Tick;
  97. timer.IsEnabled = true;
  98. }
  99. Quotes.Refresh(false, true);
  100. lastselection = DateTime.MinValue;
  101. }
  102. public void CreateToolbarButtons(IPanelHost host)
  103. {
  104. }
  105. public string SectionName => "Quotes";
  106. public DataModel DataModel(Selection selected)
  107. {
  108. //Guid[] ids = Quotes.ExtractValues<Guid>(x => x.ID, selected).ToArray();
  109. //return new QuoteDataModel(new Filter<Quote>(x => x.ID).InList(ids));
  110. if (modelsource == null)
  111. {
  112. var row = Quotes.SelectedRows.FirstOrDefault();
  113. var quoteid = row != null ? row.Get<Quote, Guid>(x => x.ID) : CoreUtils.FullGuid;
  114. return new QuoteDataModel(new Filter<Quote>(x => x.ID).IsEqualTo(quoteid));
  115. }
  116. return modelsource.DataModel(selected);
  117. }
  118. public void Heartbeat(TimeSpan time)
  119. {
  120. }
  121. private void OpenQuote(object sender, HandledEventArgs args)
  122. {
  123. if (SplitPanel.View != DynamicSplitPanelView.Detail)
  124. {
  125. SplitPanel.View = DynamicSplitPanelView.Detail;
  126. settings.ViewType = SplitPanel.View == DynamicSplitPanelView.Master
  127. ? ScreenViewType.Register
  128. : SplitPanel.View == DynamicSplitPanelView.Combined
  129. ? ScreenViewType.Combined
  130. : ScreenViewType.Details;
  131. new UserConfiguration<QuoteScreenSettings>().Save(settings);
  132. }
  133. args.Handled = true;
  134. }
  135. private void Timer_Tick(object sender, EventArgs e)
  136. {
  137. if (lastselection < DateTime.Now.AddMilliseconds(-500))
  138. {
  139. lastselection = DateTime.MaxValue;
  140. var row = Quotes.SelectedRows.FirstOrDefault();
  141. var quoteid = row != null ? row.Get<Quote, Guid>(x => x.ID) : CoreUtils.FullGuid;
  142. if (QuotePages.SelectedIndex == (int)PageIndex.Details)
  143. RefreshPanel(Details, ref QuoteDetails, quoteid, row != null);
  144. else if (QuotePages.SelectedIndex == (int)PageIndex.Documents)
  145. RefreshGrid(Documents, ref QuoteDocuments, quoteid, row != null);
  146. else if (QuotePages.SelectedIndex == (int)PageIndex.Diagrams)
  147. RefreshPanel(Diagrams, ref QuoteDiagrams, quoteid, row != null);
  148. else if (QuotePages.SelectedIndex == (int)PageIndex.Takeoffs)
  149. RefreshGrid(Takeoffs, ref QuoteTakeoffs, quoteid, row != null);
  150. else if (QuotePages.SelectedIndex == (int)PageIndex.Designs)
  151. RefreshPanel(Designs, ref QuoteDesigns, quoteid, row != null);
  152. else if (QuotePages.SelectedIndex == (int)PageIndex.CostSheets)
  153. RefreshPanel(CostSheets, ref QuoteCostSheets, quoteid, row != null);
  154. else if (QuotePages.SelectedIndex == (int)PageIndex.Proposals)
  155. RefreshPanel(Proposals, ref QuoteProposals, quoteid, row != null);
  156. else if (QuotePages.SelectedIndex == (int)PageIndex.Contract)
  157. RefreshPanel(Contract, ref QuoteContracts, quoteid, row != null);
  158. }
  159. }
  160. private void RefreshPanel<T>(TabItem container, ref T panel, Guid quoteid, bool data) where T : IBasePanel, IQuotePage, new()
  161. {
  162. if (panel == null)
  163. {
  164. panel = new T();
  165. panel.IsReady = false;
  166. panel.QuoteID = CoreUtils.FullGuid;
  167. panel.Setup();
  168. panel.IsReady = true;
  169. container.Content = panel;
  170. }
  171. if (QuotePages.SelectedIndex != CurrentPage)
  172. {
  173. modelsource = panel;
  174. OnUpdateDataModel?.Invoke(panel.SectionName, panel.DataModel(Selection.None));
  175. CurrentPage = QuotePages.SelectedIndex;
  176. }
  177. panel.QuoteID = quoteid;
  178. panel.Refresh();
  179. }
  180. private void RefreshGrid<T>(TabItem container, ref T grid, Guid quoteid, bool data)
  181. where T : IDynamicGrid, IQuotePage, IDataModelSource, new()
  182. {
  183. var bInitialised = false;
  184. if (grid == null)
  185. {
  186. grid = new T();
  187. container.Content = grid;
  188. }
  189. else
  190. {
  191. bInitialised = true;
  192. }
  193. if (QuotePages.SelectedIndex != CurrentPage)
  194. {
  195. modelsource = grid;
  196. OnUpdateDataModel?.Invoke(grid.SectionName, grid.DataModel(Selection.None));
  197. CurrentPage = QuotePages.SelectedIndex;
  198. }
  199. grid.QuoteID = quoteid;
  200. grid.Refresh(!bInitialised, data);
  201. }
  202. private void Quotes_OnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  203. {
  204. if (SplitPanel.View != DynamicSplitPanelView.Master)
  205. lastselection = DateTime.Now;
  206. }
  207. public Dictionary<Type, CoreTable> DataEnvironment()
  208. {
  209. var env = new Dictionary<Type, CoreTable>();
  210. env[typeof(Job)] = Quotes.Data;
  211. return env;
  212. }
  213. private void QuotePages_SelectionChanged(object sender, SelectionChangedEventArgs e)
  214. {
  215. if (e.Source == QuotePages)
  216. lastselection = DateTime.MinValue;
  217. }
  218. private void QuoteStatus_SelectionChanged(object sender, SelectionChangedEventArgs e)
  219. {
  220. if (IsReady)
  221. {
  222. settings.QuoteStatus = (Guid)QuoteStatus.SelectedValue;
  223. new UserConfiguration<QuoteScreenSettings>().Save(settings);
  224. Quotes.StatusID = (Guid)QuoteStatus.SelectedValue;
  225. }
  226. }
  227. private void SplitPanel_OnChanged(object sender, DynamicSplitPanelChangedArgs e)
  228. {
  229. settings.ViewType = SplitPanel.View == DynamicSplitPanelView.Master ? ScreenViewType.Register :
  230. SplitPanel.View == DynamicSplitPanelView.Detail ? ScreenViewType.Details : ScreenViewType.Combined;
  231. settings.QuoteColumnWidth = SplitPanel.MasterWidth;
  232. var newTag = settings.ViewType == ScreenViewType.Register ? settings.ViewType.ToString() : "";
  233. if (Quotes.ColumnsTag != newTag)
  234. {
  235. Quotes.ColumnsTag = newTag;
  236. Quotes.Refresh(true, true);
  237. }
  238. new UserConfiguration<QuoteScreenSettings>().Save(settings);
  239. if (settings.ViewType == ScreenViewType.Details)
  240. lastselection = DateTime.Now;
  241. }
  242. private enum PageIndex
  243. {
  244. Details = 00,
  245. Documents,
  246. Diagrams,
  247. Takeoffs,
  248. Designs,
  249. CostSheets,
  250. Proposals,
  251. Contract
  252. }
  253. }
  254. }