private async void SaveVehicleChecklistToDatabase(Boolean displayAlert) { try { ApplyActivityIndicator(); var staff = App.StaffDatabase.GetStaffById(Convert.ToInt32(App.SettingsDatabase.GetSetting("AssignedStaffId").Value)); var vehicleChecklist = App.VehicleChecklistsDatabase.GetVehicleChecklistById(Convert.ToInt32(Id.Text)); var m = new Models.VehicleChecklists() { CreatedDate = CreatedDate_DatePicker.Date, IsDeleted = false, Information = null, OfficeId = null, SentToOffice = false, StaffId = staff.Id, StaffSignature = null, Comments = Comments_Entry.Text, Mileage = Mileage_Entry.Text, Registration = Registration_Entry.Text.ToUpper() }; #region Line Items var issues = new List <VehicleChecklistIssuesViewModel> { new VehicleChecklistIssuesViewModel() { IssueName = OCFL_Label.Text, Comments = OCFL_Comments.Text, Condition = OCFL_Picker.SelectedIndex != -1 ? OCFL_Picker.SelectedItem.ToString() : "N/A", IssueCategory = "Vehicle" }, new VehicleChecklistIssuesViewModel() { IssueName = VLWC_Label.Text, Comments = VLWC_Comments.Text, Condition = VLWC_Picker.SelectedIndex != -1 ? VLWC_Picker.SelectedItem.ToString() : "N/A", IssueCategory = "Vehicle" }, new VehicleChecklistIssuesViewModel() { IssueName = WW_Label.Text, Comments = WW_Comments.Text, Condition = WW_Picker.SelectedIndex != -1 ? WW_Picker.SelectedItem.ToString() : "N/A", IssueCategory = "Vehicle" }, new VehicleChecklistIssuesViewModel() { IssueName = CWM_Label.Text, Comments = CWM_Comments.Text, Condition = CWM_Picker.SelectedIndex != -1 ? CWM_Picker.SelectedItem.ToString() : "N/A", IssueCategory = "Vehicle" }, new VehicleChecklistIssuesViewModel() { IssueName = H_Label.Text, Comments = H_Comments.Text, Condition = H_Picker.SelectedIndex != -1 ? H_Picker.SelectedItem.ToString() : "N/A", IssueCategory = "Vehicle" }, new VehicleChecklistIssuesViewModel() { IssueName = HB_Label.Text, Comments = HB_Comments.Text, Condition = HB_Picker.SelectedIndex != -1 ? HB_Picker.SelectedItem.ToString() : "N/A", IssueCategory = "Vehicle" }, new VehicleChecklistIssuesViewModel() { IssueName = TP_Label.Text, Comments = TP_Comments.Text, Condition = TP_Picker.SelectedIndex != -1 ? TP_Picker.SelectedItem.ToString() : "N/A", IssueCategory = "Vehicle" }, new VehicleChecklistIssuesViewModel() { IssueName = TC_Label.Text, Comments = TC_Comments.Text, Condition = TC_Picker.SelectedIndex != -1 ? TC_Picker.SelectedItem.ToString() : "N/A", IssueCategory = "Vehicle" }, new VehicleChecklistIssuesViewModel() { IssueName = CVN_Label.Text, Comments = CVN_Comments.Text, Condition = CVN_Picker.SelectedIndex != -1 ? CVN_Picker.SelectedItem.ToString() : "N/A", IssueCategory = "Vehicle" }, new VehicleChecklistIssuesViewModel() { IssueName = SW_Label.Text, Comments = SW_Comments.Text, Condition = SW_Picker.SelectedIndex != -1 ? SW_Picker.SelectedItem.ToString() : "N/A", IssueCategory = "Vehicle" }, new VehicleChecklistIssuesViewModel() { IssueName = CCV_Label.Text, Comments = CCV_Comments.Text, Condition = CCV_Picker.SelectedIndex != -1 ? CCV_Picker.SelectedItem.ToString() : "N/A", IssueCategory = "Vehicle" }, new VehicleChecklistIssuesViewModel() { IssueName = ISS_Label.Text, Comments = ISS_Comments.Text, Condition = ISS_Picker.SelectedIndex != -1 ? ISS_Picker.SelectedItem.ToString() : "N/A", IssueCategory = "Vehicle" }, new VehicleChecklistIssuesViewModel() { IssueName = FE_Label.Text, Comments = FE_Comments.Text, Condition = FE_Picker.SelectedIndex != -1 ? FE_Picker.SelectedItem.ToString() : "N/A", IssueCategory = "Vehicle" }, new VehicleChecklistIssuesViewModel() { IssueName = FA_Label.Text, Comments = FA_Comments.Text, Condition = FA_Picker.SelectedIndex != -1 ? FA_Picker.SelectedItem.ToString() : "N/A", IssueCategory = "Vehicle" } }; m.Information = JsonConvert.SerializeObject(new { Issues = issues }); #endregion if (vehicleChecklist != null) { if (vehicleChecklist.StaffSignature != null) { m.StaffSignature = vehicleChecklist.StaffSignature; } 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; } } } } 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.VehicleChecklistsDatabase.UpdateVehicleChecklist(m); if (displayAlert) { ResetActivityIndicator(); await DisplayAlert("Saved", "This vehicle checklist has been saved successfully.", "Acknowledge"); } } else { App.VehicleChecklistsDatabase.InsertVehicleChecklist(m); await Navigation.PushAsync(new VehicleChecklist(m.Id)); Navigation.RemovePage(this); } } catch (Exception ex) { ResetActivityIndicator(); await DisplayAlert("Error", "An error has occurred. " + ex.Message, "Acknowledge"); } }
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"); } }