Browse Source

PRS MOBILE - Digital Forms now viewed by Employee Roles

Nick-PRSDigital@bitbucket.org 2 years ago
parent
commit
d46a9fd2d2

+ 118 - 77
prs.mobile/comal.timesheets/DigitalForms/DigitalFormPickerScreen/DigitalFormsPicker.xaml.cs

@@ -19,9 +19,9 @@ using XF.Material.Forms.UI.Dialogs;
 
 namespace comal.timesheets
 {
-	[XamlCompilation(XamlCompilationOptions.Compile)]
-	public partial class DigitalFormsPicker : ContentPage
-	{
+    [XamlCompilation(XamlCompilationOptions.Compile)]
+    public partial class DigitalFormsPicker : ContentPage
+    {
         #region Fields
         bool _searching = false;
         Kanban addToTaskKanban = new Kanban();
@@ -36,7 +36,7 @@ namespace comal.timesheets
         #endregion
         public DigitalFormsPicker(string appliesTo = "Kanban", Guid _jobid = new Guid()) //normal Forms Library - default is kanban type
         {
-			InitializeComponent ();
+            InitializeComponent();
             JobID = _jobid;
             NavigationPage.SetHasBackButton(this, false);
             LoadScreen(appliesTo);
@@ -74,86 +74,123 @@ namespace comal.timesheets
             base.OnAppearing();
         }
 
-        private async void LoadScreen(string appliesTo)
+        private void LoadScreen(string appliesTo)
         {
             try
             {
-                await Task.Run(() =>
+                Task.Run(()=> 
                 {
                     types.Add("All");
-                    var forms = new Client<DigitalFormLayout>();
-                    Filter<DigitalFormLayout> filter = new Filter<DigitalFormLayout>(x => x.Type).IsEqualTo(DFLayoutType.Mobile).And(x => x.Active).IsEqualTo(true).And(x => x.Form.Secure).IsEqualTo(false)
-                        .And(x => x.Form.Active).IsEqualTo(true);
-                    filter = filter.And(x => x.Form.AppliesTo).IsEqualTo(appliesTo);
-                    Columns<DigitalFormLayout> columns = new Columns<DigitalFormLayout>(
-                        x => x.Description, //0
-                        x => x.ID, //1
-                        x => x.Code, //2
-                        x => x.Form.AppliesTo, //3
-                        x => x.Form.ID, //4
-                        x => x.Layout, //5
-                        x => x.Form.Group.Description //6
-                        );
-                    var table = forms.Query(
-                        filter,
-                        columns
-                        ,
-                        new SortOrder<DigitalFormLayout>(x => x.Description)
-                        );
+
+                    CoreTable table = LoadEmployeeRoleForms(appliesTo);
+
                     foreach (CoreRow row in table.Rows)
-                    {
-                        List<object> list = row.Values;
-                        if (list[0] == null) list[0] = ""; //0
-                        if (list[1] == null) list[1] = Guid.Empty; //1
-                        if (list[2] == null) list[2] = ""; //2
-                        if (list[3] == null) list[3] = ""; //3
-                        if (list[4] == null) list[4] = Guid.Empty; //4
-                        if (list[5] == null) list[5] = ""; //5
-                        if (list[6] == null) list[6] = ""; //6
-
-                        DigitalFormLayoutShell layout = new DigitalFormLayoutShell();
-                        layout.Description = list[0].ToString();
-                        layout.ID = Guid.Parse(list[1].ToString());
-                        layout.Code = list[2].ToString();
-                        layout.AppliesTo = list[3].ToString();
-                        layout.FormID = Guid.Parse(list[4].ToString());
-                        layout.Layout = list[5].ToString();
-                        layout.FormGroupDescription = list[6].ToString();
-                        layouts.Add(layout);
-
-                        if (!types.Contains(layout.FormGroupDescription) && !string.IsNullOrWhiteSpace(layout.FormGroupDescription))
-                        {
-                            types.Add(layout.FormGroupDescription);
-                        }
-                    }
-                    GetAverages(appliesTo);
+                        CreateAndAddShell(row);
+
+                    GetAverages();
 
                     Device.BeginInvokeOnMainThread(() =>
                     {
-                        if (appliesTo == "Kanban")
-                        {
-                            layoutsList.ItemsSource = layouts.Where(x => x.FormGroupDescription.Equals("Factory"));
-                            filterOptionsControl.Options = types;
-                            filterOptionsControl.CreateRadioButtonsAndSetDefault("Factory");
-                        }
-                        else
-                        {
-                            layoutsList.ItemsSource = layouts;
-                            filterOptionsControl.Options = types;
-                            filterOptionsControl.CreateRadioButtonsAndSetDefault(types.First());
-                        }
+                        layoutsList.ItemsSource = layouts;
+                        filterOptionsControl.Options = types;
+                        filterOptionsControl.CreateRadioButtonsAndSetDefault(types.First());
                     });
                     filterOptionsControl.OnFilterOptionChanged += FilterOptionsControl_OnFilterOptionChanged;
-                });
-                firstLoad = false;
+
+                    firstLoad = false;
+                });               
             }
             catch (Exception e)
             {
-                string error = e.Message;
+                DisplayAlert("Error", e.Message, "OK");
+            }
+        }
+
+        private void CreateAndAddShell(CoreRow row)
+        {
+            DigitalFormLayoutShell layout = new DigitalFormLayoutShell();
+            layout.ID = row.Get<DigitalFormLayout, Guid>(x => x.ID);
+            layout.Description = row.Get<DigitalFormLayout, string>(x => x.Description);
+            layout.Code = row.Get<DigitalFormLayout, string>(x => x.Code);
+            layout.AppliesTo = row.Get<DigitalFormLayout, string>(x => x.Form.AppliesTo);
+            layout.FormID = row.Get<DigitalFormLayout, Guid>(x => x.Form.ID);
+            layout.Layout = row.Get<DigitalFormLayout, string>(x => x.Layout);
+            layout.FormGroupDescription = row.Get<DigitalFormLayout, string>(x => x.Form.Group.Description);
+            if (string.IsNullOrWhiteSpace(layout.FormGroupDescription))
+                layout.FormGroupDescription = "All";
+            layouts.Add(layout);
+
+            if (!types.Contains(layout.FormGroupDescription) && !string.IsNullOrWhiteSpace(layout.FormGroupDescription))
+            {
+                types.Add(layout.FormGroupDescription);
             }
         }
 
-        private void GetAverages(string appliesTo)
+        private CoreTable LoadEmployeeRoleForms(string appliesTo)
+        {
+            CoreTable table = new Client<EmployeeRole>().Query(
+                new Filter<EmployeeRole>(x => x.EmployeeLink.ID).IsEqualTo(GlobalVariables.EmpID)
+                , new Columns<EmployeeRole>(x => x.RoleLink.ID));
+            if (!table.Rows.Any())
+            {
+                DisplayAlert("Alert", "No Roles found for your employee profile. Please check with your administrator.", "OK");
+                return table;
+            }
+
+            CoreTable roleForms = QueryRoleForms(table);
+
+            if (!roleForms.Rows.Any())
+            {
+                DisplayAlert("Alert", "No Forms found for your employee role. Please check with your administrator.", "OK");
+                return table;
+            }
+
+            return QueryFormLayouts(roleForms, appliesTo);
+        }
+        private CoreTable QueryRoleForms(CoreTable table)
+        {
+            List<Guid> roleIDS = new List<Guid>();
+            foreach (CoreRow row in table.Rows)
+            {
+                roleIDS.Add(row.Get<EmployeeRole, Guid>(x => x.RoleLink.ID));
+            }
+            return new Client<RoleForm>().Query(
+                new Filter<RoleForm>(x => x.Role.ID).InList(roleIDS.ToArray()),
+                new Columns<RoleForm>(x => x.Form.ID));
+        }
+
+        private CoreTable QueryFormLayouts(CoreTable roleForms, string appliesTo)
+        {
+            List<Guid> formIDs = new List<Guid>();
+            foreach (CoreRow row in roleForms.Rows)
+            {
+                formIDs.Add(row.Get<RoleForm, Guid>(x => x.Form.ID));
+            }
+            return new Client<DigitalFormLayout>().Query
+                (
+                new Filter<DigitalFormLayout>(x => x.Form.ID).InList(formIDs.ToArray())
+                .And(x => x.Type).IsEqualTo(DFLayoutType.Mobile)
+                .And(x => x.Active).IsEqualTo(true)
+                .And(x => x.Form.Secure).IsEqualTo(false)
+                .And(x => x.Form.Active).IsEqualTo(true)
+                .And(x => x.Form.AppliesTo).IsEqualTo(appliesTo),
+
+                new Columns<DigitalFormLayout>(
+                x => x.Description,
+                x => x.ID,
+                x => x.Code,
+                x => x.Form.AppliesTo,
+                x => x.Form.ID,
+                x => x.Layout,
+                x => x.Form.Group.Description),
+
+                new SortOrder<DigitalFormLayout>(x => x.Description)
+                );
+        }
+
+
+
+        private void GetAverages()
         {
             try
             {
@@ -290,18 +327,22 @@ namespace comal.timesheets
 
         private void FilterOptionsControl_OnFilterOptionChanged(string filterOption)
         {
-            if (filterOption == filterOptionsControl.CurrentOption)
-                return;
-
-            filterOptionsControl.CurrentOption = filterOption;
-            if (filterOption == "All")
-            {
-                layoutsList.ItemsSource = layouts;
-            }
-            else
+            try
             {
-                layoutsList.ItemsSource = layouts.Where(x => x.FormGroupDescription.Equals(filterOption));
+                if (filterOption == filterOptionsControl.CurrentOption)
+                    return;
+
+                filterOptionsControl.CurrentOption = filterOption;
+                if (filterOption == "All")
+                {
+                    layoutsList.ItemsSource = layouts;
+                }
+                else
+                {
+                    layoutsList.ItemsSource = layouts.Where(x => x.FormGroupDescription.Equals(filterOption));
+                }
             }
+            catch { }
         }
         #endregion
 

+ 0 - 1
prs.mobile/comal.timesheets/DigitalForms/Renderer/QAFormViewer.cs

@@ -113,7 +113,6 @@ namespace comal.timesheets.QAForms
         #region Load Form Layout
         private async void LoadForm()
         {
-            using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading Form"))
             {
                 LoadRowsAndColumns();
                 LoadElements();