public async Task <ActionResult> ApproveReportPostback(int?Id, string command) { if (Id.HasValue && !String.IsNullOrEmpty(command)) { DbExpenseReport dbReport = await db.Reports.FindAsync(Id.Value); if (dbReport == null) { return(new HttpStatusCodeResult(HttpStatusCode.NotFound)); } if (dbReport.Status != DbExpenseReportStatus.Submitted) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (command == "Approve") { dbReport.Status = DbExpenseReportStatus.Approved; dbReport.DateResolved = DateTime.Today; } else if (command == "Reject") { dbReport.Status = DbExpenseReportStatus.Saved; } db.Entry(dbReport).State = EntityState.Modified; await db.SaveChangesAsync(); return(RedirectToAction("ViewReport", new { id = Id })); } return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); }
// GET: Expenses/Report/5 public async Task <ActionResult> ViewReport(int?id) { var employee = await this.GetEmployee(); var employeeId = employee.Item1; ViewBag.UserName = employee.Item2; if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } DbExpenseReport dbReport = await db.Reports.FindAsync(id); if (dbReport == null) { return(HttpNotFound()); } var associatedCharges = await db.Charges .Where(c => c.EmployeeId == employeeId && c.ExpenseReportId.HasValue && c.ExpenseReportId.Value == id) .ToListAsync(); var outstandingCharges = await db.Charges .Where(c => c.EmployeeId == employeeId && !c.ExpenseReportId.HasValue) .ToListAsync(); var report = new ExpenseReportModel(dbReport); report.EmployeeId = employeeId; report.AssociatedCharges = associatedCharges.Select(c => new ChargeModel(c)).ToList(); report.OutstandingCharges = outstandingCharges.Select(c => new ChargeModel(c)).ToList(); return(View(report)); }
public ExpenseReport(DbExpenseReport dbExpenseReport) { this.Amount = dbExpenseReport.Amount; this.Approver = dbExpenseReport.Approver; this.CostCenter = dbExpenseReport.CostCenter; this.DateResolved = dbExpenseReport.DateResolved; this.DateSubmitted = dbExpenseReport.DateSubmitted; this.EmployeeId = dbExpenseReport.EmployeeId; this.ExpenseReportId = dbExpenseReport.ExpenseReportId; this.Notes = dbExpenseReport.Notes; this.Status = (ExpenseReportStatus)dbExpenseReport.Status; }
public ExpenseReport(DbExpenseReport dbExpenseReport) { this.Amount = dbExpenseReport.Amount; this.Approver = dbExpenseReport.Approver; this.CostCenter = dbExpenseReport.CostCenter; this.DateResolved = dbExpenseReport.DateResolved; this.DateSubmitted = dbExpenseReport.DateSubmitted; this.EmployeeId = dbExpenseReport.EmployeeId; this.ExpenseReportId = dbExpenseReport.ExpenseReportId; this.Notes = dbExpenseReport.Notes; this.Status = (ExpenseReportStatus)dbExpenseReport.Status; }
public ExpenseReportModel(DbExpenseReport dbReport) { this.Amount = dbReport.Amount; this.Approver = dbReport.Approver; this.AssociatedCharges = dbReport.Charges.Select(c => new ChargeModel(c)).ToList(); this.CostCenter = dbReport.CostCenter; this.DateResolved = dbReport.DateResolved; this.DateSubmitted = dbReport.DateSubmitted; this.EmployeeId = dbReport.EmployeeId; this.Id = dbReport.ExpenseReportId; this.Notes = dbReport.Notes; this.Status = (ExpenseReportStatus)dbReport.Status; }
// GET: Expenses/DeleteReport/5 public async Task <ActionResult> DeleteReport(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } DbExpenseReport dbReport = await db.Reports.FindAsync(id); if (dbReport == null) { return(HttpNotFound()); } return(View(new ExpenseReportModel(dbReport))); }
public async Task <ActionResult> DeleteReportConfirmed(int id) { // find all associated charges and dis-associate them var charges = await db.Charges .Where(c => c.ExpenseReportId.HasValue && c.ExpenseReportId.Value == id) .ToListAsync(); foreach (var c in charges) { c.ExpenseReportId = null; db.Entry(c).State = EntityState.Modified; } DbExpenseReport dbReport = await db.Reports.FindAsync(id); db.Reports.Remove(dbReport); await db.SaveChangesAsync(); return(RedirectToAction("Reports")); }
public async Task <ActionResult> NewReport( ExpenseReportModel expenseReportModel, string associatedChargesIds, string outstandingChargesIds, int?addCharge, int?removeCharge) { var employee = await db.Employees.FirstOrDefaultAsync(e => e.Alias == ExpensesDemoData.DefaultEmployeeAlias); var employeeId = employee.EmployeeId; ViewBag.UserName = employee.Name; expenseReportModel.AssociatedCharges = await GetChargesFromSerializedIdList(associatedChargesIds, employeeId); expenseReportModel.OutstandingCharges = await GetChargesFromSerializedIdList(outstandingChargesIds, employeeId); if (addCharge.HasValue) { // the request was to add a charge to the report ReplaceChargeObjectBetweenLists(addCharge.Value, expenseReportModel.OutstandingCharges, expenseReportModel.AssociatedCharges); } else if (removeCharge.HasValue) { // the request was to remove a charge from the report ReplaceChargeObjectBetweenLists(removeCharge.Value, expenseReportModel.AssociatedCharges, expenseReportModel.OutstandingCharges); } else { // Submit request if (ModelState.IsValid) { expenseReportModel.EmployeeId = employeeId; var dbExpenseReport = new DbExpenseReport() { Amount = expenseReportModel.Amount, Approver = expenseReportModel.Approver, Charges = expenseReportModel.AssociatedCharges.Select(c => c.ConvertToDbCharge(employeeId)).ToList(), CostCenter = expenseReportModel.CostCenter, DateResolved = expenseReportModel.DateResolved, DateSubmitted = expenseReportModel.DateSubmitted, Employee = employee, EmployeeId = expenseReportModel.EmployeeId, ExpenseReportId = expenseReportModel.Id, Notes = expenseReportModel.Notes, Status = (DbExpenseReportStatus)expenseReportModel.Status, }; db.Reports.Add(dbExpenseReport); await db.SaveChangesAsync(); // add charges to report foreach (var c in expenseReportModel.AssociatedCharges) { var dbCharge = await db.Charges.FindAsync(c.Id); dbCharge.ExpenseReportId = dbExpenseReport.ExpenseReportId; db.Entry(dbCharge).State = EntityState.Modified; } await db.SaveChangesAsync(); return(RedirectToAction("Report", new { id = dbExpenseReport.ExpenseReportId })); } } // This is required to update hidden fields in the view. ModelState.Clear(); // Recalculate the total expense amount, since we have added or removed some charges. expenseReportModel.Amount = expenseReportModel.AssociatedCharges.Sum(c => c.BilledAmount); return(View(expenseReportModel)); }