JobITPDetails.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. using comal.timesheets.CustomControls;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using InABox.Core;
  8. using Comal.Classes;
  9. using InABox.Clients;
  10. using Xamarin.Forms;
  11. using Xamarin.Forms.Xaml;
  12. using XF.Material.Forms.UI.Dialogs;
  13. using comal.timesheets.QAForms;
  14. namespace comal.timesheets.SiteITPModule
  15. {
  16. [XamlCompilation(XamlCompilationOptions.Compile)]
  17. public partial class JobITPDetails
  18. {
  19. private JobITPShell _itp = null;
  20. Guid itpID = Guid.Empty;
  21. DigitalFormLayout layout = new DigitalFormLayout();
  22. bool layoutLoaded;
  23. List<ITPFormShell> iTPFormShells = new List<ITPFormShell>();
  24. JobITP jobITP = new JobITP();
  25. public JobITPDetails(JobITPShell itp)
  26. {
  27. InitializeComponent();
  28. _itp = itp;
  29. layoutLoaded = false;
  30. ITPNameLbl.Text = "ITP Area: " + itp.Description;
  31. }
  32. protected override void OnAppearing()
  33. {
  34. LoadList();
  35. base.OnAppearing();
  36. }
  37. private async void LoadLayoutIDForITP()
  38. {
  39. try
  40. {
  41. await Task.Run(() =>
  42. {
  43. CoreTable table1 = new Client<JobITP>().Query
  44. (
  45. new Filter<JobITP>(x => x.ID).IsEqualTo(itpID),
  46. new Columns<JobITP>(x => x.DigitalForm.ID,
  47. x => x.ID,
  48. x => x.Code)
  49. );
  50. if (table1.Rows.Any())
  51. {
  52. List<object> list = table1.Rows.First().Values;
  53. if (list[0] == null) list[0] = Guid.Empty;
  54. jobITP.ID = Guid.Parse(list[1].ToString());
  55. jobITP.Code = list[2].ToString();
  56. if (Guid.Parse(list[0].ToString()) != Guid.Empty)
  57. {
  58. CoreTable table = new Client<DigitalFormLayout>().Query(
  59. new Filter<DigitalFormLayout>(x => x.Form.ID).IsEqualTo(Guid.Parse(list[0].ToString()))
  60. );
  61. if (table.Rows.Any())
  62. {
  63. layout = table.Rows.FirstOrDefault().ToObject<DigitalFormLayout>();
  64. layoutLoaded = true;
  65. Device.BeginInvokeOnMainThread(() =>
  66. {
  67. DFDescriptionLbl.Text = "Digital Form Template: " + layout.Description;
  68. });
  69. }
  70. else
  71. {
  72. Device.BeginInvokeOnMainThread(() =>
  73. {
  74. DisplayAlert("Alert", "No form layouts found for ITP", "OK");
  75. });
  76. }
  77. }
  78. else
  79. {
  80. Device.BeginInvokeOnMainThread(() =>
  81. {
  82. DisplayAlert("Alert", "No form layouts found for ITP", "OK");
  83. });
  84. }
  85. }
  86. else if (iTPFormShells.Count > 0)
  87. {
  88. CoreTable table = new Client<DigitalFormLayout>().Query(new Filter<DigitalFormLayout>(x => x.Form.ID).IsEqualTo(iTPFormShells.First().FormID));
  89. if (table.Rows.Any())
  90. {
  91. layout = table.Rows.FirstOrDefault().ToObject<DigitalFormLayout>();
  92. layoutLoaded = true;
  93. Device.BeginInvokeOnMainThread(() =>
  94. {
  95. DFDescriptionLbl.Text = "Digital Form Template: " + layout.Description;
  96. });
  97. }
  98. }
  99. else
  100. {
  101. Device.BeginInvokeOnMainThread(() =>
  102. {
  103. DisplayAlert("Alert", "No form layouts found for ITP", "OK");
  104. });
  105. }
  106. });
  107. }
  108. catch { }
  109. }
  110. private async void LoadList()
  111. {
  112. try
  113. {
  114. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  115. {
  116. iTPFormShells.Clear();
  117. CoreTable table = new Client<JobITPForm>().Query(
  118. new Filter<JobITPForm>(x => x.Parent.ID).IsEqualTo(itpID),
  119. new Columns<JobITPForm>(
  120. x => x.ID, //0
  121. x => x.Form.Description, //1
  122. x => x.FormCompleted, //2
  123. x => x.Form.ID, //3
  124. x => x.Description, //4
  125. x => x.FormData, //5
  126. x => x.LastUpdate, //6
  127. x => x.LastUpdateBy, //7
  128. x => x.Created //8
  129. ),
  130. new SortOrder<JobITPForm>(x => x.FormCompleted)
  131. );
  132. foreach (CoreRow row in table.Rows)
  133. {
  134. List<object> list = row.Values;
  135. if (list[0] == null) { list[0] = Guid.Empty; } //0
  136. if (list[1] == null) { list[1] = ""; } //1
  137. if (list[2] == null) { list[2] = DateTime.MinValue; } //2
  138. if (list[3] == null) { list[3] = Guid.Empty; } //3
  139. if (list[4] == null) { list[4] = ""; } //4
  140. if (list[5] == null) { list[5] = ""; } //5
  141. if (list[6] == null) { list[6] = DateTime.MinValue; } //6
  142. if (list[7] == null) { list[7] = ""; } //7
  143. if (list[8] == null) { list[8] = DateTime.MinValue; } //8
  144. ITPFormShell shell = new ITPFormShell
  145. {
  146. ID = Guid.Parse(list[0].ToString()),
  147. Description = list[4].ToString(),
  148. Completed = DateTime.Parse(list[2].ToString()),
  149. FormID = Guid.Parse(list[3].ToString())
  150. };
  151. if (string.IsNullOrWhiteSpace(shell.Description))
  152. {
  153. shell.Description = list[1].ToString();
  154. }
  155. if (shell.Completed != DateTime.MinValue)
  156. {
  157. shell.Color = Color.FromHex("#15C7C1"); //light blue
  158. shell.Status = "Completed (" + shell.Completed.ToString("dd-MMMM-yy") + ")";
  159. }
  160. else if (!string.IsNullOrWhiteSpace(list[5].ToString()))
  161. {
  162. shell.Color = Color.Orange;
  163. shell.Status = "In Progress (Updated: " + DateTime.Parse(list[6].ToString()).ToString("dd-MMMM-yy") + ")";
  164. }
  165. else
  166. {
  167. shell.Color = Color.FromHex("#f08080"); //light coral / red
  168. shell.Status = "To Do (Created " + DateTime.Parse(list[8].ToString()).ToString("dd-MMMM-yy") + ")";
  169. }
  170. iTPFormShells.Add(shell);
  171. }
  172. if (!layoutLoaded)
  173. LoadLayoutIDForITP();
  174. Device.BeginInvokeOnMainThread(() =>
  175. {
  176. ITPFormDisplayList.ItemsSource = null;
  177. ITPFormDisplayList.ItemsSource = iTPFormShells;
  178. countLbl.Text = "Number of Forms: " + iTPFormShells.Count;
  179. });
  180. }
  181. }
  182. catch { }
  183. }
  184. private async void Add_Clicked(object sender, EventArgs e)
  185. {
  186. try
  187. {
  188. if (!layoutLoaded)
  189. {
  190. DisplayAlert("No form layouts found for ITP", "Unable to add a form", "OK");
  191. return;
  192. }
  193. JobITPForm newForm = new JobITPForm();
  194. newForm.Parent.ID = itpID;
  195. string chosenOption = await DisplayPromptAsync("Enter Location", "", "OK", "Cancel");
  196. if (!string.IsNullOrEmpty(chosenOption))
  197. {
  198. if (chosenOption != "Cancel")
  199. {
  200. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  201. {
  202. newForm.Form.ID = layout.Form.ID;
  203. newForm.Description = chosenOption;
  204. new Client<JobITPForm>().Save(newForm, "Added on mobile device");
  205. }
  206. LoadList();
  207. }
  208. }
  209. }
  210. catch { }
  211. }
  212. private async void ITPFormList_Tapped(object sender, EventArgs e)
  213. {
  214. try
  215. {
  216. if (!layoutLoaded)
  217. return;
  218. ITPFormShell shell = ITPFormDisplayList.SelectedItem as ITPFormShell;
  219. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  220. {
  221. bool readOnly = false;
  222. CoreTable table0 = new Client<JobITPForm>().Query(
  223. new Filter<JobITPForm>(x => x.ID).IsEqualTo(shell.ID),
  224. new Columns<JobITPForm>(
  225. x => x.ID, //0
  226. x => x.Form.Description, //1
  227. x => x.Form.AppliesTo, //2
  228. x => x.FormCompleted, //3
  229. x => x.FormCompletedBy.ID, //4
  230. x => x.FormData, //5
  231. x => x.Form.ID, //6
  232. x => x.Parent.ID, //7
  233. x => x.FormStarted, //8
  234. x => x.FormOpen, //9
  235. x => x.BlobData
  236. )
  237. );
  238. if (table0.Rows.Any())
  239. {
  240. JobITPForm existingForm = table0.Rows.First().ToObject<JobITPForm>();
  241. if (existingForm.FormCompleted != DateTime.MinValue)
  242. {
  243. readOnly = true;
  244. }
  245. DigitalFormHostModel<JobITP, JobITPLink, JobITPForm> model = new DigitalFormHostModel<JobITP, JobITPLink, JobITPForm>();
  246. model.LoadItems(jobITP, existingForm, layout);
  247. DigitalFormHost host = new DigitalFormHost(model);
  248. Navigation.PushAsync(host);
  249. }
  250. }
  251. }
  252. catch
  253. {
  254. DisplayAlert("Error opening form", "", "OK");
  255. }
  256. }
  257. }
  258. public class ITPFormShell
  259. {
  260. public Guid ID { get; set; }
  261. public string Description { get; set; }
  262. public string Status { get; set; }
  263. public string StatusDetails { get; set; }
  264. public Color Color { get; set; }
  265. public DateTime Completed { get; set; }
  266. public Guid FormID { get; set; }
  267. public ITPFormShell()
  268. {
  269. ID = Guid.Empty;
  270. Description = "";
  271. Status = "";
  272. StatusDetails = "";
  273. Color = Color.Default;
  274. Completed = DateTime.MinValue;
  275. FormID = Guid.Empty;
  276. }
  277. }
  278. }