QuoteDetails.xaml.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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, IDynamicEditorHost
  15. {
  16. private CoreRow[] _rows;
  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. return new Dictionary<String, object[]>() { { typeof(Quote).EntityName(), _rows } };
  63. }
  64. public void Setup()
  65. {
  66. UpdateScreen(null, null);
  67. var statuscodes = new Dictionary<Guid, string> { { Guid.Empty, "" } };
  68. var statuses = new Client<QuoteStatus>().Query();
  69. foreach (var row in statuses.Rows)
  70. statuscodes[row.Get<QuoteStatus, Guid>(x => x.ID)] = row.Get<QuoteStatus, string>(x => x.Description);
  71. Status.ItemsSource = statuscodes;
  72. Proposals.Options.AddRange(DynamicGridOption.RecordCount, DynamicGridOption.SelectColumns);
  73. Proposals.Refresh(true, false);
  74. Title.EditorDefinition = new TextBoxEditor();
  75. Title.ColumnName = "Title";
  76. Title.Configure();
  77. Title.OnEditorValueChanged += Title_OnEditorValueChanged;
  78. Title.Loaded = true;
  79. Customer.EditorDefinition = new CodePopupEditor(typeof(Customer));
  80. Customer.ColumnName = "Customer.ID";
  81. Customer.CodeColumn = "Code";
  82. Customer.OtherColumns["Code"] = "Customer.Code";
  83. Customer.OtherColumns["Name"] = "Customer.Name";
  84. Customer.OtherColumns["Account.ID"] = "Account.ID";
  85. Customer.OtherColumns["Account.Code"] = "Account.Code";
  86. Customer.OtherColumns["Account.Name"] = "Account.Name";
  87. Customer.Host = this;
  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.Host = this;
  97. Account.Configure();
  98. Account.OnEditorValueChanged += Account_OnEditorValueChanged;
  99. Account.Loaded = true;
  100. }
  101. public void Shutdown()
  102. {
  103. }
  104. public Guid ParentID { get; set; }
  105. public Dictionary<Type, CoreTable> DataEnvironment()
  106. {
  107. return new Dictionary<Type, CoreTable>();
  108. }
  109. private void UpdateScreen(CoreTable o, Exception e)
  110. {
  111. _rows = o?.Rows.ToArray() ?? new CoreRow[] { };
  112. Dispatcher.Invoke(() =>
  113. {
  114. var bIsReady = IsReady;
  115. IsReady = false;
  116. Title.Value = o?.Rows.FirstOrDefault()?.Get<Quote, string>(col => col.Title) ?? "";
  117. Customer.Value = o?.Rows.FirstOrDefault()?.Get<Quote, Guid>(col => col.Customer.ID) ?? Guid.Empty;
  118. Address.Text = o?.Rows.FirstOrDefault()?.Get<Quote, string>(col => col.SiteAddress.Street) ?? "";
  119. City.Text = o?.Rows.FirstOrDefault()?.Get<Quote, string>(col => col.SiteAddress.City) ?? "";
  120. State.Text = o?.Rows.FirstOrDefault()?.Get<Quote, string>(col => col.SiteAddress.State) ?? "";
  121. PostCode.Text = o?.Rows.FirstOrDefault()?.Get<Quote, string>(col => col.SiteAddress.PostCode) ?? "";
  122. Account.Value = o?.Rows.FirstOrDefault()?.Get<Quote, Guid>(col => col.Account.ID) ?? Guid.Empty;
  123. var notes = o?.Rows.FirstOrDefault()?.Get<Quote, string[]>(col => col.Notes) ?? null;
  124. Notes.Text = notes != null ? string.Join("\n\n", notes).Replace("\r\n\r\n", "\r\n").Replace("\n\n", "\n") : "";
  125. Status.SelectedValue = o?.Rows.FirstOrDefault()?.Get<Quote, Guid>(col => col.Status.ID) ?? Guid.Empty;
  126. QuoteValue.Text = o != null && o.Rows.Any() ? string.Format("${0:F2}", o.Rows.First().Get<Quote, double>(col => col.ExTax)) : "";
  127. IsReady = bIsReady;
  128. });
  129. }
  130. private void Title_OnEditorValueChanged(IDynamicEditorControl sender, Dictionary<string, object> values)
  131. {
  132. if (IsReady)
  133. {
  134. var quote = new Quote { ID = ParentID };
  135. quote.Number = _rows?.FirstOrDefault()?.Get<Quote, string>(col => col.Number) ?? "";
  136. quote.Title = Title.Value;
  137. new Client<Quote>().Save(quote, "Updated Title", (j, err) => { });
  138. }
  139. }
  140. private void Customer_OnEditorValueChanged(IDynamicEditorControl sender, Dictionary<string, object> values)
  141. {
  142. Account.Value = (Guid)values["Account.ID"];
  143. if (IsReady)
  144. {
  145. var quote = new Quote { ID = ParentID };
  146. quote.Number = _rows.FirstOrDefault()?.Get<Quote, string>(col => col.Number) ?? "";
  147. quote.Customer.ID = (Guid)values["Customer.ID"];
  148. quote.Account.ID = (Guid)values["Account.ID"];
  149. new Client<Quote>().Save(quote, "Updated Customer ID", (j, err) => { });
  150. }
  151. }
  152. private void Account_OnEditorValueChanged(IDynamicEditorControl sender, Dictionary<string, object> values)
  153. {
  154. if (IsReady)
  155. {
  156. var quote = new Quote { ID = ParentID };
  157. quote.Number = _rows?.FirstOrDefault()?.Get<Quote, string>(col => col.Number) ?? "";
  158. quote.Account.ID = (Guid)values["Account.ID"];
  159. new Client<Quote>().Save(quote, "Updated Account ID", (j, err) => { });
  160. }
  161. }
  162. private void Status_SelectionChanged(object sender, SelectionChangedEventArgs e)
  163. {
  164. if (IsReady)
  165. {
  166. var quote = new Quote { ID = ParentID };
  167. quote.Number = _rows?.FirstOrDefault()?.Get<Quote, string>(col => col.Number) ?? "";
  168. quote.Status.ID = Status.SelectedValue != null ? (Guid)Status.SelectedValue : Guid.Empty;
  169. new Client<Quote>().Save(quote, "Updated Quote Status", (j, err) => { });
  170. }
  171. }
  172. #region IDynamicEditorHost
  173. public IEnumerable<DynamicGridColumn> Columns { get; } = InitialiseColumns();
  174. private static DynamicGridColumns InitialiseColumns()
  175. {
  176. var columns = new DynamicGridColumns();
  177. columns.ExtractColumns(typeof(Quote));
  178. return columns;
  179. }
  180. public void LoadColumns(string column, Dictionary<string, string> columns)
  181. {
  182. columns.Clear();
  183. var comps = column.Split('.').ToList();
  184. comps.RemoveAt(comps.Count - 1);
  185. var prefix = string.Format("{0}.", string.Join(".", comps));
  186. var cols = Columns.Where(x => !x.ColumnName.Equals(column) && x.ColumnName.StartsWith(prefix));
  187. foreach (var col in cols)
  188. columns[col.ColumnName.Replace(prefix, "")] = col.ColumnName;
  189. }
  190. public IFilter? DefineFilter(Type type) => LookupFactory.DefineFilter<Quote>(new Quote[] { new Quote() }, type);
  191. public void LoadLookups(ILookupEditorControl editor)
  192. {
  193. //if (editor == Activity) Activity_OnDefineLookups(editor);
  194. //else if (editor == Kanban) Task_OnDefineLookups(editor);
  195. //else if (editor == ITP) ITP_OnDefineLookups(editor);
  196. }
  197. public Document? FindDocument(string filename)
  198. {
  199. return new Client<Document>().Load(new Filter<Document>(x => x.FileName).IsEqualTo(filename)).FirstOrDefault();
  200. }
  201. public Document? GetDocument(Guid id)
  202. {
  203. Document? doc = null;
  204. if (id != Guid.Empty)
  205. doc = new Client<Document>().Load(new Filter<Document>(x => x.ID).IsEqualTo(id)).FirstOrDefault();
  206. return doc;
  207. }
  208. public void SaveDocument(Document document)
  209. {
  210. new Client<Document>().Save(document, "");
  211. }
  212. object?[] IDynamicEditorHost.GetItems() => new object?[] { new Quote() };
  213. public BaseEditor? GetEditor(DynamicGridColumn column) => column.Editor.CloneEditor();
  214. #endregion
  215. }
  216. }