JobDetails.xaml.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. namespace PRSDesktop
  9. {
  10. /// <summary>
  11. /// Interaction logic for JobDetails.xaml
  12. /// </summary>
  13. public partial class JobDetails : UserControl, IPanel<Job>, IJobControl
  14. {
  15. private CoreTable Data;
  16. public JobDetails()
  17. {
  18. InitializeComponent();
  19. }
  20. public Guid JobID { get; set; }
  21. public event DataModelUpdateEvent OnUpdateDataModel;
  22. public bool IsReady { get; set; }
  23. public void CreateToolbarButtons(IPanelHost host)
  24. {
  25. }
  26. public string SectionName => "Job Details";
  27. public DataModel DataModel(Selection selection)
  28. {
  29. return new JobDetailsDataModel(new Filter<Job>(x => x.ID).IsEqualTo(JobID));
  30. }
  31. public void Heartbeat(TimeSpan time)
  32. {
  33. }
  34. public void Refresh()
  35. {
  36. //var cols = new Columns<Job>(col => col.Customer.Code);
  37. new Client<Job>().Query(
  38. new Filter<Job>(x => x.ID).IsEqualTo(JobID),
  39. new Columns<Job>(
  40. col => col.JobNumber,
  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.Code,
  48. col => col.Account.Name,
  49. col => col.Name,
  50. col => col.Notes,
  51. col => col.JobStatus.ID,
  52. col => col.ID
  53. ),
  54. null,
  55. (o, e) => { UpdateScreen(o); }
  56. );
  57. }
  58. public Dictionary<string, object[]> Selected()
  59. {
  60. var rows = Data != null ? Data.Rows.ToArray() : new CoreRow[] { };
  61. return new Dictionary<string, object[]> { { typeof(Job).EntityName(), rows } };
  62. }
  63. public void Setup()
  64. {
  65. UpdateScreen(null);
  66. var statuscodes = new Dictionary<Guid, string> { { Guid.Empty, "" } };
  67. var statuses = new Client<JobStatus>().Query();
  68. foreach (var row in statuses.Rows)
  69. statuscodes[row.Get<JobStatus, Guid>(x => x.ID)] = row.Get<JobStatus, string>(x => x.Description);
  70. Status.ItemsSource = statuscodes;
  71. }
  72. public void Shutdown()
  73. {
  74. }
  75. private void UpdateScreen(CoreTable o)
  76. {
  77. Data = o;
  78. Dispatcher.Invoke(() =>
  79. {
  80. CustomerCode.Text = o != null && o.Rows.Any() ? o.Rows.First().Get<Job, string>(col => col.Customer.Code) : "";
  81. CustomerName.Text = o != null && o.Rows.Any() ? o.Rows.First().Get<Job, string>(col => col.Customer.Name) : "";
  82. Address.Text = o != null && o.Rows.Any() ? o.Rows.First().Get<Job, string>(col => col.SiteAddress.Street) : "";
  83. City.Text = o != null && o.Rows.Any() ? o.Rows.First().Get<Job, string>(col => col.SiteAddress.City) : "";
  84. State.Text = o != null && o.Rows.Any() ? o.Rows.First().Get<Job, string>(col => col.SiteAddress.State) : "";
  85. PostCode.Text = o != null && o.Rows.Any() ? o.Rows.First().Get<Job, string>(col => col.SiteAddress.PostCode) : "";
  86. AccountCode.Text = o != null && o.Rows.Any() ? o.Rows.First().Get<Job, string>(col => col.Account.Code) : "";
  87. AccountName.Text = o != null && o.Rows.Any() ? o.Rows.First().Get<Job, string>(col => col.Account.Name) : "";
  88. Title.Text = o != null && o.Rows.Any() ? o.Rows.First().Get<Job, string>(col => col.Name) : "";
  89. var notes = o != null && o.Rows.Any() ? o?.Rows.First()?.Get<Job, string[]>(col => col.Notes) : null;
  90. Notes.Text = notes != null ? string.Join("\n\n", notes)?.Replace("\r\n\r\n", "\r\n").Replace("\n\n", "\n") : "";
  91. var bIsReady = IsReady;
  92. IsReady = false;
  93. Status.SelectedValue = o != null && o.Rows.Any() ? o.Rows.First().Get<Job, Guid>(col => col.JobStatus.ID) : Guid.Empty;
  94. IsReady = bIsReady;
  95. });
  96. }
  97. private void Status_SelectionChanged(object sender, SelectionChangedEventArgs e)
  98. {
  99. var job = new Job { ID = JobID };
  100. job.JobNumber = Data != null && Data.Rows.Any() ? Data.Rows.First().Get<Job, string>(col => col.JobNumber) : "";
  101. job.JobStatus.ID = Status.SelectedValue != null ? (Guid)Status.SelectedValue : Guid.Empty;
  102. if (IsReady)
  103. new Client<Job>().Save(job, "Updated Job Status", (j, err) => { });
  104. }
  105. }
  106. }