private static async Task <JToken> PutTimesheet(PutStandardTimesheet2Request timesheetRequest) { AppRequest req = new AppRequest(); req.serviceURL = PutStandardTimesheet2Request.ServiceUrl; req.Input = JObject.FromObject(timesheetRequest); return(await GetServerData(req)); }
private static async Task <PutStandardTimesheet2Request> CreateTimesheetSubmissionForCurrentTimesheet(DateTime date) { /* * do beforehand and save values */ if (string.IsNullOrEmpty(UserURI)) { try { var User = await GetUser(); //set local variable to avoid calling this again UserURI = JsonConvert.DeserializeObject <GetUser2Response>(User.ToString()).d.uri; //8ms } catch (Exception e) { throw new Exception("Unable To Load Employee", e); } } GetStandardTimesheet2Reply currentTS; try { var timesheetReply = await GetTimesheetUri(UserURI, date); TimesheetURI = JsonConvert.DeserializeObject <GetTimesheetForDate2Response>(timesheetReply.ToString()).d .timesheet.uri; var request = (await GetTimesheet(TimesheetURI)).ToString(); currentTS = JsonConvert.DeserializeObject <GetStandardTimesheet2Reply>(request); } catch (Exception e) { throw new Exception("Unable To Load Timesheet", e); } /* * do beforehand and save values */ PutStandardTimesheet2Request newTS = new PutStandardTimesheet2Request(); try { //copy URI of current Timesheet newTS.timesheet.target.uri = TimesheetURI; //set employee newTS.timesheet.target.user.uri = currentTS.d.uri; //set date newTS.timesheet.target.date.day = DateTime.Today.Day; newTS.timesheet.target.date.month = DateTime.Today.Month; newTS.timesheet.target.date.year = DateTime.Today.Year; //copy current rows foreach (var dRow in currentTS.d.rows) { if (dRow.task != null) { dRow.task.parameterCorrelationId = "TimeTrackerApp"; } newTS.timesheet.rows.Add(dRow); } //set value of whatever this means newTS.timesheet.noticeExplicitlyAccepted = "0"; } catch (Exception e) { throw new Exception("Unable to copy current timesheet", e); } return(newTS); }
public static async Task SubmitTimesheet(List <TimeEntryParent> timeEntries) { if (timeEntries.All(x => x.Ticket == null)) { throw new Exception("Nothing to submit"); } PutStandardTimesheet2Request timesheetSubmission = null; try { timesheetSubmission = await CreateTimesheetSubmissionForCurrentTimesheet(timeEntries.FirstOrDefault().Date); } catch (Exception e) { throw new Exception("Unable to process timesheet submission, Try Again Later", e); } foreach (var timeEntry in timeEntries)//for each entry to be submitted { if (timeEntry.Ticket == null) { break; //ignore entries with no tickets } foreach (var timesheetRow in timesheetSubmission.timesheet.rows) // foreach timesheet row currently on timesheet { if (((!timesheetRow.IsProject() && timesheetRow.task.uri.Equals(timeEntry.RepliconTicketID)) || //current row is project and matches current entry (timesheetRow.IsProject() && timesheetRow.project.uri.Equals(timeEntry.RepliconTicketID))) && // current row is task and matches current entry ((string.IsNullOrEmpty(timesheetRow.billingRate?.uri) && !timeEntry.BillCustomer) || // both billable (!string.IsNullOrEmpty(timesheetRow.billingRate?.uri) && timeEntry.BillCustomer))) //neither billable { var newCell = CreateCellFromEntry(timeEntry); //find entry on row with same date var existingEntry = timesheetRow.cells.FirstOrDefault(x => x.date.day.Equals(newCell.date.day)); //if entry exists for same date replace, else add to list if (existingEntry != null) { timesheetRow.cells.Remove(existingEntry); } timesheetRow.cells.Add(newCell); timeEntry.ExistsOnTimeSheet = true; break; } } //iterated through all current rows and not match found, make a new row if (!timeEntry.ExistsOnTimeSheet) { //create new row Row newRow = new Row(); if (timeEntry.BillCustomer) { newRow.billingRate = new BillingRate() { uri = "urn:replicon:project-specific-billing-rate", name = "Project Rate", displayText = "Project Rate" }; } newRow.project = new Project(); newRow.project.uri = timeEntry.Ticket.ProjectURI; if (!timeEntry.IsProject()) { newRow.task = new TaskBasic(); newRow.task.uri = timeEntry.Ticket.uri; } var newCell = CreateCellFromEntry(timeEntry); newRow.cells.Add(newCell); timesheetSubmission.timesheet.rows.Add(newRow); } } try { await PutTimesheet(timesheetSubmission); } catch (Exception e) { throw new Exception("Unable to upload timesheet", e); } }