private async void SaveDailyTimesheetToDatabase(Boolean displayAlert) { try { ApplyActivityIndicator(); var staff = App.StaffDatabase.GetStaffById(Convert.ToInt32(App.SettingsDatabase.GetSetting("AssignedStaffId").Value)); var dailyTimesheet = App.DailyTimesheetsDatabase.GetDailyTimesheetById(Convert.ToInt32(Id.Text)); var timeSpan = new TimeSpan(0, 0, 0, 0, 0); var m = new Models.DailyTimesheets() { DailyMileage = DailyMileage_Entry.Text, Date = Date_DatePicker.Date, IsDeleted = false, LineItems = null, LunchEndTime = null, LunchStartTime = null, MileageEnd = MileageEnd_Entry.Text, MileageStart = MileageStart_Entry.Text, OfficeId = null, SentToOffice = false, StaffId = staff.Id, StaffSignature = null, CallOut = CallOut_CheckBox.IsChecked, TotalHours = 0, WeekNumber = Weeks.GetISO8601WeekOfYear(Date_DatePicker.Date) }; if (LunchEndTime_TimePicker.Time != timeSpan) { m.LunchEndTime = LunchEndTime_TimePicker.Time; } if (LunchStartTime_TimePicker.Time != timeSpan) { m.LunchStartTime = LunchStartTime_TimePicker.Time; } #region Line Items Decimal?totalHours = 0; var lineItems = new List <DailyTimesheetLineItemViewModel>(); foreach (var item in ListItems.Children) { if (item.GetType() == typeof(ListItems)) { var items = (ListItems)item; var section = items.GetItems(); if (section.JobNumber != 0 && section.JobHours != 0) { lineItems.Add(new DailyTimesheetLineItemViewModel() { ArriveSite = section.ArriveSite, SiteAddress = section.SiteAddress, Comment = section.Comment, Complete = section.Complete, FinishJob = section.FinishJob, JobHours = section.JobHours, JobNumber = section.JobNumber, LeaveSite = section.LeaveSite, StartJob = section.StartJob }); totalHours = totalHours + section.JobHours; } } } m.TotalHours = totalHours ?? 0; m.LineItems = JsonConvert.SerializeObject(new { DailyTimesheetLineItems = lineItems }); #endregion if (dailyTimesheet != null) { if (dailyTimesheet.StaffSignature != null) { m.StaffSignature = dailyTimesheet.StaffSignature; } else { if (StaffSign != null) { StaffSign_StackLayout.IsVisible = true; var image = await StaffSign.GetImageStreamAsync(SignatureImageFormat.Png); if (image != null) { byte[] arr; using (var ms = new MemoryStream()) { await image.CopyToAsync(ms); arr = ms.ToArray(); } m.StaffSignature = arr; } } } } else { if (StaffSign != null) { var image = await StaffSign.GetImageStreamAsync(SignatureImageFormat.Png); if (image != null) { byte[] arr; using (var ms = new MemoryStream()) { await image.CopyToAsync(ms); arr = ms.ToArray(); } m.StaffSignature = arr; } } } if (!String.IsNullOrEmpty(Id.Text)) { m.Id = Convert.ToInt32(Id.Text); App.DailyTimesheetsDatabase.UpdateDailyTimesheet(m); if (displayAlert) { ResetActivityIndicator(); await DisplayAlert("Saved", "This daily timesheet has been saved successfully.", "Acknowledge"); } } else { App.DailyTimesheetsDatabase.InsertDailyTimesheet(m); await Navigation.PushAsync(new DailyTimesheet(m.Id)); Navigation.RemovePage(this); } } catch (Exception ex) { ResetActivityIndicator(); await DisplayAlert("Error", "An error has occurred. " + ex.Message, "Acknowledge"); } }