using comal.timesheets.CustomControls; using Comal.Classes; using InABox.Clients; using InABox.Core; using iText.Layout.Element; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using Xamarin.Forms; using PRSClasses; using PRSSecurity = InABox.Core.Security; namespace comal.timesheets { public static class CacheLoader { #region Jobs public static async Task LoadJobs() { await Task.Run(() => { try { List jobShells = new List(); Filter filter = AssignFilter(); CoreTable table = DoJobsQuery(filter); foreach (CoreRow row in table.Rows) jobShells.Add(CreateJobShell(row)); AssignJobShells(jobShells); } catch { } }); } private static void AssignJobShells(List jobShells) { GlobalVariables.JobShells = jobShells; GlobalVariables.JobShells.Insert(0, new JobShell { ID = Guid.Empty, JobNumber = "No Job", Name = "Empty Job", JobStatusDescription = "Hidden" }); GlobalVariables.JobsLoaded = true; } private static Filter AssignFilter() { Filter filter = new Filter(x => x.JobStatus.Active).IsEqualTo(true); if (!PRSSecurity.IsAllowed()) { List jobIDs = new List(); CoreTable jobEmployees = new Client().Query( new Filter(x => x.EmployeeLink.ID).IsEqualTo(GlobalVariables.EmpID), new Columns(x => x.JobLink.ID) ); foreach (CoreRow row in jobEmployees.Rows) { jobIDs.Add(row.Get(x => x.JobLink.ID)); } filter = filter.And(x => x.ID).InList(jobIDs.ToArray()); } return filter; } private static CoreTable DoJobsQuery(Filter filter) { return new Client().Query( filter, new Columns(x => x.ID, x => x.Name, x => x.JobNumber, x => x.JobStatus.Description, x => x.Color), new SortOrder(x => x.JobNumber) ); } private static JobShell CreateJobShell(CoreRow row) { Job job = row.ToObject(); if (job.Color == null) job.Color = ""; JobShell jobshell = new JobShell { ID = job.ID, Name = job.Name, JobNumber = job.JobNumber, JobStatusDescription = job.JobStatus.Description, Color = Color.FromHex(job.Color) }; if (jobshell.JobStatusDescription.Equals("Active Projects")) jobshell.JobStatusDescription = "Active"; else if (jobshell.JobStatusDescription.Equals("Projects - Hidden from View")) jobshell.JobStatusDescription = "Hidden"; else if (jobshell.JobStatusDescription.Equals("Projects in Defect Liability Period")) jobshell.JobStatusDescription = "Defect Liability"; jobshell.DisplayName = "(" + jobshell.JobNumber + ") " + jobshell.Name; return jobshell; } #endregion } }