Browse Source

Merge remote-tracking branch 'origin/nick' into frank

Frank van den Bos 2 năm trước cách đây
mục cha
commit
db4954f841

+ 12 - 12
prs.mobile/comal.timesheets/Assignments/AssignmentDetails.xaml.cs

@@ -10,7 +10,7 @@ using Syncfusion.Drawing;
 using Xamarin.Forms;
 using Xamarin.Forms.Xaml;
 using XF.Material.Forms.UI.Dialogs;
-using Security = InABox.Core.Security;
+using PRSSecurity = InABox.Core.Security;
 
 namespace comal.timesheets
 {
@@ -48,17 +48,17 @@ namespace comal.timesheets
                 ? "Complete"
                 : InABox.Core.Security.CanEdit<Assignment>() ? "Re-Open" : $"Completed {DataModel.Item.Completed:dd/MM/yy}";
 
-            Subject.IsEnabled = open && Security.CanEdit<Assignment>();
-            ActualStart.IsEnabled = open && Security.IsAllowed<CanEditAssignmentActualTimes>();
-            ActualFinish.IsEnabled = open && Security.IsAllowed<CanEditAssignmentActualTimes>();
-            BookedStart.IsEnabled = open && Security.IsAllowed<CanEditAssignmentBookings>();
-            BookedFinish.IsEnabled = open && Security.IsAllowed<CanEditAssignmentBookings>();
-            AsBookedBtn.IsEnabled = open && Security.IsAllowed<CanEditAssignmentActualTimes>();
-            Job.IsEnabled = open && Security.CanEdit<Assignment>();
-            Task.IsEnabled = open && Security.CanEdit<Assignment>();
-            Activity.IsEnabled = open && Security.CanEdit<Assignment>();
-            Description.IsEnabled = open && Security.CanEdit<Assignment>();
-            Completed.IsEnabled = open || Security.CanEdit<Assignment>();
+            Subject.IsEnabled = open && PRSSecurity.CanEdit<Assignment>();
+            ActualStart.IsEnabled = open && PRSSecurity.IsAllowed<CanEditAssignmentActualTimes>();
+            ActualFinish.IsEnabled = open && PRSSecurity.IsAllowed<CanEditAssignmentActualTimes>();
+            BookedStart.IsEnabled = open && PRSSecurity.IsAllowed<CanEditAssignmentBookings>();
+            BookedFinish.IsEnabled = open && PRSSecurity.IsAllowed<CanEditAssignmentBookings>();
+            AsBookedBtn.IsEnabled = open && PRSSecurity.IsAllowed<CanEditAssignmentActualTimes>();
+            Job.IsEnabled = open && PRSSecurity.CanEdit<Assignment>();
+            Task.IsEnabled = open && PRSSecurity.CanEdit<Assignment>();
+            Activity.IsEnabled = open && PRSSecurity.CanEdit<Assignment>();
+            Description.IsEnabled = open && PRSSecurity.CanEdit<Assignment>();
+            Completed.IsEnabled = open || PRSSecurity.CanEdit<Assignment>();
         }
 
         private async void Activity_Clicked(object sender, EventArgs e)

+ 16 - 1
prs.mobile/comal.timesheets/CustomControls/Pages/Selection Screens/JobSelectionPage.xaml.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.ComponentModel;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
@@ -88,7 +89,20 @@ namespace comal.timesheets.CustomControls
 
     public class JobShell
     {
-        public Guid ID { get; set; }
+        public delegate void JobIDChanged(Guid jobid);
+
+        public event JobIDChanged OnJobIDChanged;
+
+        private Guid id;
+        public Guid ID 
+        {
+            get => id;
+            set 
+            {
+                id = value;
+                OnJobIDChanged?.Invoke(value);
+            }
+        }
 
         public string Name { get; set; }
 
@@ -108,5 +122,6 @@ namespace comal.timesheets.CustomControls
             JobStatusDescription = "";
             Color = Color.LightGray;
         }
+        
     }
 }

+ 2 - 1
prs.mobile/comal.timesheets/Data Classes/NotifyChanges.cs

@@ -19,7 +19,8 @@ namespace comal.timesheets
                 {
                 "- Update to assignments (use actual/booked)",
                 "- Improve digital form viewing from tasks",
-                "- Improve PDF viewer for Androids"
+                "- Improve PDF viewer for Androids",
+                "- Clock on to job now creates Assignments instead"
                 };
             foreach (string s in changes)
             {

+ 177 - 59
prs.mobile/comal.timesheets/Main/MainPage.xaml.cs

@@ -39,10 +39,11 @@ namespace comal.timesheets
         bool bSharedDeviceFirstLoad = true;
         bool bSharedDevice = false;
         int NumberOfNotfications = 0;
-        private Job _job = new Job();
+        private JobShell _job = new JobShell();
         string deviceName = "";
         string matchedDeviceName = "";
         int notCount = 1;
+        Assignment currentAssignment = null;
         #endregion
 
         #region Constructor
@@ -92,6 +93,9 @@ namespace comal.timesheets
                 //if (GlobalVariables.EmpID == Guid.Parse("40f6ccd9-5272-4b1a-99bf-de7542205aac"))
                 //RunCustomScript();
                 NotifyChanges();
+
+                CheckCurrentAssignment();
+
             }
             catch (Exception e)
             {
@@ -100,20 +104,63 @@ namespace comal.timesheets
             NavigationPage.SetHasBackButton(this, false);
         }
 
+        private void CheckCurrentAssignment()
+        {
+            Task.Run(() =>
+            {
+                Thread.Sleep(5000);
+                StartAssignmentTimer();
+                CoreTable table = new Client<Assignment>().Query(
+                    new Filter<Assignment>(x => x.EmployeeLink.ID).IsEqualTo(GlobalVariables.EmpID)
+                    .And(x => x.Date).IsEqualTo(DateTime.Today.Date)
+                    .And(x => x.Actual.Finish).IsEqualTo(null),
+                    null,
+                    new SortOrder<Assignment>(x => x.Actual.Start, SortDirection.Ascending));
+                if (!table.Rows.Any())
+                {
+                    _job.OnJobIDChanged += OnJobIDChanged;
+                    return;
+                }
 
+                var assgn = table.Rows.LastOrDefault().ToObject<Assignment>();
+                Device.BeginInvokeOnMainThread(async () =>
+                {
+                    string chosenOption = await DisplayActionSheet("Continue with current unfinished assignment?", "Cancel", null, "Yes", "No");
+                    switch(chosenOption) 
+                    {
+                        case "Yes":
+                            currentAssignment = assgn;
+                            SaveCurrentAssignment("PRS Mobile main screen - saving assignment on re-login to App");
+                            if (currentAssignment.JobLink.ID != Guid.Empty)
+                            {
+                                _job.ID = currentAssignment.JobLink.ID;
+                                var job = new Client<Job>().Query(new Filter<Job>(x => x.ID).IsEqualTo(_job.ID)).Rows.FirstOrDefault().ToObject<Job>();
+                                _job.JobNumber = job.JobNumber;
+                                _job.Name = job.Name;
+                                RefreshJobBtn();
+                                _job.OnJobIDChanged += OnJobIDChanged;
+                            }            
+                            break;
+                        default:
+                            _job.OnJobIDChanged += OnJobIDChanged;
+                            break;
+                    };
+                });               
+            });
+        }
 
         private void RunCustomScript()
         {
-            
-        }      
+
+        }
 
         private void NotifyChanges()
         {
-            Task.Run(() => 
+            Task.Run(() =>
             {
                 string changes = NotifyMobileChanges.Notifiy();
                 if (!string.IsNullOrWhiteSpace(changes))
-                    Device.BeginInvokeOnMainThread(() => 
+                    Device.BeginInvokeOnMainThread(() =>
                     {
                         DisplayAlert("Latest changes", changes, "OK");
                     });
@@ -301,8 +348,9 @@ namespace comal.timesheets
                         addNoteBtn.IsEnabled = false;
                         jobBtn.IsEnabled = false;
                     }
-                    if (firstLoad)
-                        RefreshJobFromTimeSheet(timesheet);
+                    //if (firstLoad)
+                    //    RefreshJobFromTimeSheet(timesheet);
+                    RefreshJobBtn();
 
                     homeScreenGrid.RaiseChild(CurrentLocation);
 
@@ -318,6 +366,14 @@ namespace comal.timesheets
             //CurrentLocation.IsEnabled = PRSReady && GateReady;
         }
 
+        private void RefreshJobBtn()
+        {
+            if (_job.ID == Guid.Empty)
+                jobBtn.Text = "No Job Selected";
+            else if(!string.IsNullOrWhiteSpace(_job.JobNumber))
+                jobBtn.Text = "Job: " + _job.JobNumber;
+        }
+
         private void RefreshJobFromTimeSheet(CoreRow timesheet)
         {
             Guid jobid = timesheet == null ? Guid.Empty : timesheet.Get<TimeSheet, Guid>(x => x.JobLink.ID);
@@ -331,7 +387,7 @@ namespace comal.timesheets
             else
             {
                 jobBtn.Text = "No Job Selected";
-                _job = new Job();
+                _job = new JobShell();
             }
         }
 
@@ -640,6 +696,61 @@ namespace comal.timesheets
         #endregion
 
         #region Utilities
+        private void OnJobIDChanged(Guid jobid)
+        {
+            if (currentAssignment == null)
+                CreateNewAssignment(jobid);
+
+            else
+            {
+                SaveCurrentAssignment("PRS Mobile main screen - saving assignment on job change", true);
+                CreateNewAssignment(jobid);
+            }
+        }
+
+        private void StartAssignmentTimer()
+        {
+            Timer t = new Timer(AssignmentTimerCallback, null, 300000, 300000);
+        }
+
+        private void AssignmentTimerCallback(object state)
+        {
+            if (currentAssignment != null)
+                SaveCurrentAssignment("PRS MObile main screen - Saving assignment on 5 minute timer");
+        }
+
+        private void CreateNewAssignment(Guid jobid)
+        {
+            currentAssignment = new Assignment { Date = DateTime.Now.Date };
+            currentAssignment.EmployeeLink.ID = GlobalVariables.EmpID;
+            currentAssignment.JobLink.ID = jobid;
+            currentAssignment.Actual.Start = RoundToNearestInterval(DateTime.Now, new TimeSpan(0, 5, 0)).TimeOfDay;
+            currentAssignment.Booked.Start = currentAssignment.Actual.Start;
+            currentAssignment.Booked.Finish = RoundToNearestInterval(DateTime.Now, new TimeSpan(0, 5, 0)).TimeOfDay.Add(new TimeSpan(0, 5, 0));
+            var job = new Client<Job>().Query(new Filter<Job>(x => x.ID).IsEqualTo(jobid)).Rows.FirstOrDefault().ToObject<Job>();
+            currentAssignment.Title = "Clocking on to job " + job.Name + " (" + job.JobNumber + ") on PRS Mobile";
+            new Client<Assignment>().Save(currentAssignment, "Changed job on mobile - creating new assignment");
+        }
+
+        private void SaveCurrentAssignment(string auditnote, bool complete = false)
+        {
+            if (!complete)
+                currentAssignment.Booked.Finish = RoundToNearestInterval(DateTime.Now, new TimeSpan(0, 5, 0)).TimeOfDay;
+            else
+                currentAssignment.Actual.Finish = RoundToNearestInterval(DateTime.Now, new TimeSpan(0, 5, 0)).TimeOfDay;
+
+            new Client<Assignment>().Save(currentAssignment, auditnote);
+        }
+
+        static DateTime RoundToNearestInterval(DateTime dt, TimeSpan d)
+        {
+            int f = 0;
+            double m = (double)(dt.Ticks % d.Ticks) / d.Ticks;
+            if (m >= 0.5)
+                f = 1;
+            return new DateTime(((dt.Ticks / d.Ticks) + f) * d.Ticks);
+        }
+
         private void InitNotificationCentre()
         {
             try
@@ -925,6 +1036,12 @@ namespace comal.timesheets
                 midnightTimerOn = false;
                 Timer last60Seconds = new Timer(Last60SecondsTimerCallBack, null, 5000, Timeout.Infinite);
                 clockedOffInLast5Seconds = true;
+                if (currentAssignment != null)
+                {
+                    SaveCurrentAssignment("PRS Mobile - clocking off", true);
+                    currentAssignment = null;
+                }
+
             }
             catch { }
         }
@@ -999,6 +1116,7 @@ namespace comal.timesheets
                     _job.ID = jobSelectionPage.Job.ID;
                     _job.JobNumber = jobSelectionPage.Job.JobNumber;
                     _job.Name = jobSelectionPage.Job.Name;
+                    RefreshScreen();
                     JobPage_OnItemSelected(jobSelectionPage.Job);
                 });
                 Navigation.PushAsync(jobSelectionPage);
@@ -1008,60 +1126,60 @@ namespace comal.timesheets
 
         private void JobPage_OnItemSelected(JobShell job)
         {
-            try
-            {
-                TimeSheet timesheet = App.Data.TimeSheets?.Rows.FirstOrDefault()?.ToObject<TimeSheet>();
-                if (timesheet == null)
-                    return;
+            //try
+            //{
+            //    TimeSheet timesheet = App.Data.TimeSheets?.Rows.FirstOrDefault()?.ToObject<TimeSheet>();
+            //    if (timesheet == null)
+            //        return;
 
 
 
-                String auditmessage = String.Format("Changed Selected Job to: {0}: {1}", timesheet.JobLink.JobNumber, timesheet.JobLink.Name);
-                if (ZeroLengthTimesheet())
-                {
-                    timesheet.JobLink.ID = job.ID;
-                    timesheet.JobLink.JobNumber = job.JobNumber;
-                    timesheet.JobLink.Name = job.Name;
-                    bUpdatingTimesheet = true;
-                    new Client<TimeSheet>().Save(timesheet, auditmessage);
+            //    String auditmessage = String.Format("Changed Selected Job to: {0}: {1}", timesheet.JobLink.JobNumber, timesheet.JobLink.Name);
+            //    if (ZeroLengthTimesheet())
+            //    {
+            //        timesheet.JobLink.ID = job.ID;
+            //        timesheet.JobLink.JobNumber = job.JobNumber;
+            //        timesheet.JobLink.Name = job.Name;
+            //        bUpdatingTimesheet = true;
+            //        new Client<TimeSheet>().Save(timesheet, auditmessage);
 
-                    Device.BeginInvokeOnMainThread(() =>
-                    {
-                        if (timesheet.JobLink.ID != Guid.Empty)
-                        {
-                            jobBtn.Text = "(" + timesheet.JobLink.JobNumber + ") " + timesheet.JobLink.Name;
-                        }
-                        else
-                        {
-                            jobBtn.Text = "No Job Selected";
-                        }
-                    });
-                }
-                else
-                {
-                    InABox.Core.Location here = new InABox.Core.Location()
-                    {
-                        Latitude = App.GPS.Latitude,
-                        Longitude = App.GPS.Longitude,
-                        Timestamp = DateTime.Now
-                    };
-                    TimeSpan tod = DateTime.Now - DateTime.Today;
-                    timesheet.Finish = new TimeSpan(tod.Hours, tod.Minutes, 0);
-                    timesheet.FinishLocation = here;
-                    new Client<TimeSheet>().Save(timesheet, "Changing Job");
-
-                    CreateTimeSheet(
-                        job.ID,
-                        job.JobNumber,
-                        job.Name,
-                        here,
-                        App.GPS.Address,
-                        auditmessage
-                    );
-                }
-                RefreshScreen();
-            }
-            catch { }
+            //        Device.BeginInvokeOnMainThread(() =>
+            //        {
+            //            if (timesheet.JobLink.ID != Guid.Empty)
+            //            {
+            //                jobBtn.Text = "(" + timesheet.JobLink.JobNumber + ") " + timesheet.JobLink.Name;
+            //            }
+            //            else
+            //            {
+            //                jobBtn.Text = "No Job Selected";
+            //            }
+            //        });
+            //    }
+            //    else
+            //    {
+            //        InABox.Core.Location here = new InABox.Core.Location()
+            //        {
+            //            Latitude = App.GPS.Latitude,
+            //            Longitude = App.GPS.Longitude,
+            //            Timestamp = DateTime.Now
+            //        };
+            //        TimeSpan tod = DateTime.Now - DateTime.Today;
+            //        timesheet.Finish = new TimeSpan(tod.Hours, tod.Minutes, 0);
+            //        timesheet.FinishLocation = here;
+            //        new Client<TimeSheet>().Save(timesheet, "Changing Job");
+
+            //        CreateTimeSheet(
+            //            job.ID,
+            //            job.JobNumber,
+            //            job.Name,
+            //            here,
+            //            App.GPS.Address,
+            //            auditmessage
+            //        );
+            //    }
+            //    RefreshScreen();
+            //}
+            //catch { }
         }
 
         private bool CheckTimeSheetAgainstGates(TimeSheet timesheet)
@@ -1646,7 +1764,7 @@ namespace comal.timesheets
                     };
                     Site.OnTapped += ((object sender, EventArgs e) =>
                     {
-                        Site site = new Site(_job);
+                        Site site = new Site(new Job { ID = _job.ID, Name = _job.Name, JobNumber = _job.JobNumber });
                         Navigation.PushAsync(site);
                         //if (_job.ID == Guid.Empty)
                         //{