QuoteDetails.xaml.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Controls;
  5. using Comal.Classes;
  6. using InABox.Clients;
  7. using InABox.Core;
  8. using InABox.DynamicGrid;
  9. namespace PRSDesktop
  10. {
  11. /// <summary>
  12. /// Interaction logic for QuoteDetails.xaml
  13. /// </summary>
  14. public partial class QuoteDetails : UserControl, IPanel<Quote>, IQuotePage
  15. {
  16. private CoreTable Data;
  17. public QuoteDetails()
  18. {
  19. InitializeComponent();
  20. }
  21. public event DataModelUpdateEvent OnUpdateDataModel;
  22. public bool IsReady { get; set; }
  23. public void CreateToolbarButtons(IPanelHost host)
  24. {
  25. }
  26. public string SectionName => "Quotes";
  27. public DataModel DataModel(Selection selection)
  28. {
  29. return new QuoteDataModel(new Filter<Quote>(x => x.ID).IsEqualTo(ParentID));
  30. }
  31. public void Heartbeat(TimeSpan time)
  32. {
  33. }
  34. public void Refresh()
  35. {
  36. new Client<Quote>().Query(
  37. new Filter<Quote>(x => x.ID).IsEqualTo(ParentID),
  38. new Columns<Quote>(
  39. col => col.Number,
  40. col => col.Customer.ID,
  41. col => col.Customer.Code,
  42. col => col.Customer.Name,
  43. col => col.SiteAddress.Street,
  44. col => col.SiteAddress.City,
  45. col => col.SiteAddress.State,
  46. col => col.SiteAddress.PostCode,
  47. col => col.Account.ID,
  48. col => col.Account.Code,
  49. col => col.Account.Name,
  50. col => col.Title,
  51. col => col.Notes,
  52. col => col.Status.ID,
  53. col => col.ExTax
  54. ),
  55. null,
  56. (o, e) => { UpdateScreen(o, e); }
  57. );
  58. Proposals.ParentID = ParentID;
  59. }
  60. public Dictionary<string, object[]> Selected()
  61. {
  62. var rows = Data != null ? Data.Rows.ToArray() : new CoreRow[] { };
  63. return new Dictionary<string, object[]> { { typeof(Quote).EntityName(), rows } };
  64. }
  65. public void Setup()
  66. {
  67. UpdateScreen(null, null);
  68. var statuscodes = new Dictionary<Guid, string> { { Guid.Empty, "" } };
  69. var statuses = new Client<QuoteStatus>().Query();
  70. foreach (var row in statuses.Rows)
  71. statuscodes[row.Get<QuoteStatus, Guid>(x => x.ID)] = row.Get<QuoteStatus, string>(x => x.Description);
  72. Status.ItemsSource = statuscodes;
  73. Proposals.Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.SelectColumns);
  74. Proposals.Refresh(true, false);
  75. Title.EditorDefinition = new TextBoxEditor();
  76. Title.ColumnName = "Title";
  77. Title.Configure();
  78. Title.OnEditorValueChanged += Title_OnEditorValueChanged;
  79. Title.Loaded = true;
  80. Customer.EditorDefinition = new CodePopupEditor(typeof(Customer));
  81. Customer.ColumnName = "Customer.ID";
  82. Customer.CodeColumn = "Code";
  83. Customer.OtherColumns["Code"] = "Customer.Code";
  84. Customer.OtherColumns["Name"] = "Customer.Name";
  85. Customer.OtherColumns["Account.ID"] = "Account.ID";
  86. Customer.OtherColumns["Account.Code"] = "Account.Code";
  87. Customer.OtherColumns["Account.Name"] = "Account.Name";
  88. Customer.Configure();
  89. Customer.OnEditorValueChanged += Customer_OnEditorValueChanged;
  90. Customer.Loaded = true;
  91. Account.EditorDefinition = new CodePopupEditor(typeof(Customer));
  92. Account.ColumnName = "Account.ID";
  93. Account.CodeColumn = "Code";
  94. Account.OtherColumns["Code"] = "Account.Code";
  95. Account.OtherColumns["Name"] = "Account.Name";
  96. Account.Configure();
  97. Account.OnEditorValueChanged += Account_OnEditorValueChanged;
  98. Account.Loaded = true;
  99. }
  100. public void Shutdown()
  101. {
  102. }
  103. public Guid ParentID { get; set; }
  104. public Dictionary<Type, CoreTable> DataEnvironment()
  105. {
  106. return new Dictionary<Type, CoreTable>();
  107. }
  108. private void UpdateScreen(CoreTable o, Exception e)
  109. {
  110. Data = o;
  111. Dispatcher.Invoke(() =>
  112. {
  113. var bIsReady = IsReady;
  114. IsReady = false;
  115. Title.Value = o != null && o.Rows.Any() ? o.Rows.First().Get<Quote, string>(col => col.Title) : "";
  116. Customer.Value = o != null && o.Rows.Any() ? o.Rows.First().Get<Quote, Guid>(col => col.Customer.ID) : Guid.Empty;
  117. //CustomerCode.Text = (o != null) && o.Rows.Any() ? o.Rows.First().Get<Quote, String>(col => col.Customer.Code) : "";
  118. //CustomerName.Text = (o != null) && o.Rows.Any() ? o.Rows.First().Get<Quote, String>(col => col.Customer.Name) : "";
  119. Address.Text = o != null && o.Rows.Any() ? o.Rows.First().Get<Quote, string>(col => col.SiteAddress.Street) : "";
  120. City.Text = o != null && o.Rows.Any() ? o.Rows.First().Get<Quote, string>(col => col.SiteAddress.City) : "";
  121. State.Text = o != null && o.Rows.Any() ? o.Rows.First().Get<Quote, string>(col => col.SiteAddress.State) : "";
  122. PostCode.Text = o != null && o.Rows.Any() ? o.Rows.First().Get<Quote, string>(col => col.SiteAddress.PostCode) : "";
  123. Account.Value = o != null && o.Rows.Any() ? o.Rows.First().Get<Quote, Guid>(col => col.Account.ID) : Guid.Empty;
  124. //AccountCode.Text = (o != null) && o.Rows.Any() ? o.Rows.First().Get<Quote, String>(col => col.Account.Code) : "";
  125. //AccountName.Text = (o != null) && o.Rows.Any() ? o.Rows.First().Get<Quote, String>(col => col.Account.Name) : "";
  126. var notes = o != null && o.Rows.Any() ? o.Rows.First().Get<Quote, string[]>(col => col.Notes) : null;
  127. Notes.Text = notes != null ? string.Join("\n\n", notes).Replace("\r\n\r\n", "\r\n").Replace("\n\n", "\n") : "";
  128. Status.SelectedValue = o != null && o.Rows.Any() ? o.Rows.First().Get<Quote, Guid>(col => col.Status.ID) : Guid.Empty;
  129. QuoteValue.Text = o != null && o.Rows.Any() ? string.Format("${0:F2}", o.Rows.First().Get<Quote, double>(col => col.ExTax)) : "";
  130. IsReady = bIsReady;
  131. });
  132. }
  133. private void Title_OnEditorValueChanged(IDynamicEditorControl sender, Dictionary<string, object> values)
  134. {
  135. if (IsReady)
  136. {
  137. var quote = new Quote { ID = ParentID };
  138. quote.Number = Data != null && Data.Rows.Any() ? Data.Rows.First().Get<Quote, string>(col => col.Number) : "";
  139. quote.Title = Title.Value;
  140. new Client<Quote>().Save(quote, "Updated Title", (j, err) => { });
  141. }
  142. }
  143. private void Customer_OnEditorValueChanged(IDynamicEditorControl sender, Dictionary<string, object> values)
  144. {
  145. Account.Value = (Guid)values["Account.ID"];
  146. if (IsReady)
  147. {
  148. var quote = new Quote { ID = ParentID };
  149. quote.Number = Data != null && Data.Rows.Any() ? Data.Rows.First().Get<Quote, string>(col => col.Number) : "";
  150. quote.Customer.ID = (Guid)values["Customer.ID"];
  151. quote.Account.ID = (Guid)values["Account.ID"];
  152. new Client<Quote>().Save(quote, "Updated Customer ID", (j, err) => { });
  153. }
  154. }
  155. private void Account_OnEditorValueChanged(IDynamicEditorControl sender, Dictionary<string, object> values)
  156. {
  157. if (IsReady)
  158. {
  159. var quote = new Quote { ID = ParentID };
  160. quote.Number = Data != null && Data.Rows.Any() ? Data.Rows.First().Get<Quote, string>(col => col.Number) : "";
  161. quote.Account.ID = (Guid)values["Account.ID"];
  162. new Client<Quote>().Save(quote, "Updated Account ID", (j, err) => { });
  163. }
  164. }
  165. private void Status_SelectionChanged(object sender, SelectionChangedEventArgs e)
  166. {
  167. if (IsReady)
  168. {
  169. var quote = new Quote { ID = ParentID };
  170. quote.Number = Data != null && Data.Rows.Any() ? Data.Rows.First().Get<Quote, string>(col => col.Number) : "";
  171. quote.Status.ID = Status.SelectedValue != null ? (Guid)Status.SelectedValue : Guid.Empty;
  172. new Client<Quote>().Save(quote, "Updated Quote Status", (j, err) => { });
  173. }
  174. }
  175. }
  176. }