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;
 }
 public DbExpenseReport SaveExpenseReport(DbExpenseReport report)
 {
     using (var context = new ExpensesContext())
     {
         if (report.ExpenseReportId != 0)
         {
             context.Reports.Attach(report);
             context.Entry <DbExpenseReport>(report).State = System.Data.Entity.EntityState.Modified;
         }
         else
         {
             context.Reports.Add(report);
         }
         context.SaveChanges();
         return(report);
     }
 }
 public DbExpenseReport SaveExpenseReport(DbExpenseReport report)
 {
     using (var context = new ExpensesContext())
     {
         if (report.ExpenseReportId != 0)
         {
             context.Reports.Attach(report);
             context.Entry<DbExpenseReport>(report).State = System.Data.Entity.EntityState.Modified;
         }
         else
         {
             context.Reports.Add(report);
         }
         context.SaveChanges();
         return report;
     }
 }
        public static DbEmployee CreateNewDemoEmployee(string alias)
        {
            var repository = new ExpensesRepository();
            string managerAlias = "manager";

            if (string.Compare(alias, "rogreen", true) != 0)
            {
                managerAlias = "rogreen";
            }

            DbEmployee employee =
                new DbEmployee()
                {
                    Alias = alias,
                    Manager = managerAlias,
                    Name = "New Employee"
                };

            employee = repository.SaveEmployee(employee);

            List<DbCharge> charges = new List<DbCharge>();

            repository.SaveCharge(new DbCharge()
            {
                EmployeeId = employee.EmployeeId,
                BilledAmount = 200M,
                Description = "REF# 27438948",
                ExpenseDate = DateTime.Today.AddDays(-45),
                Location = "San Francisco, CA",
                Merchant = "Northwind Inn",
                Notes = string.Empty,
                TransactionAmount = 200M
            });

            repository.SaveCharge(new DbCharge()
            {
                EmployeeId = employee.EmployeeId,
                BilledAmount = 40,
                Description = "REF# 77384751",
                ExpenseDate = DateTime.Today.AddDays(-20),
                Location = "Seattle, WA",
                Merchant = "Contoso Taxi",
                Notes = string.Empty,
                TransactionAmount = 40
            });

            repository.SaveCharge(new DbCharge()
            {
                EmployeeId = employee.EmployeeId,
                BilledAmount = 67,
                Description = "REF# 33748563",
                ExpenseDate = DateTime.Today.AddDays(-8),
                Location = "Seattle, WA",
                Merchant = "Fourth Coffee",
                Notes = string.Empty,
                TransactionAmount = 12
            });

            repository.SaveCharge(new DbCharge()
            {
                EmployeeId = employee.EmployeeId,
                BilledAmount = 17,
                Description = "REF# 33748876",
                ExpenseDate = DateTime.Today.AddDays(-4),
                Location = "Seattle, WA",
                Merchant = "Fourth Coffee",
                Notes = string.Empty,
                TransactionAmount = 15
            });

            repository.SaveExpenseReport(new DbExpenseReport()
            {
                EmployeeId = employee.EmployeeId,
                Amount = 640M,
                Approver = managerAlias,
                CostCenter = 50992,
                DateSubmitted = DateTime.Today.AddDays(-7),
                Notes = (managerAlias == "rogreen") ? "Kim Akers" : "Visit to Blue Yonder Airlines",
                Status = DbExpenseReportStatus.Saved
            });

            DbExpenseReport report = new DbExpenseReport()
            {
                EmployeeId = employee.EmployeeId,
                Amount = 450M,
                Approver = managerAlias,
                CostCenter = 50992,
                DateSubmitted = DateTime.Today.AddDays(-7),
                Notes = (managerAlias == "rogreen") ? "Kim Akers" : "Visit to Tailspin Toys",
                Status = DbExpenseReportStatus.Saved
            };
            report = repository.SaveExpenseReport(report);

            repository.SaveCharge(new DbCharge()
            {
                EmployeeId = employee.EmployeeId,
                ExpenseReportId = report.ExpenseReportId,
                BilledAmount = 350M,
                Description = "Airfare to San Francisco",
                ExpenseDate = DateTime.Today.AddDays(-60),
                Location = "Chicago, IL",
                Merchant = "Blue Yonder Airlines",
                Notes = string.Empty,
                TransactionAmount = 350M
            });

            repository.SaveCharge(new DbCharge()
            {
                EmployeeId = employee.EmployeeId,
                ExpenseReportId = report.ExpenseReportId,
                BilledAmount = 50,
                Description = "Cab from airport",
                ExpenseDate = DateTime.Today.AddDays(-45),
                Location = "San Francisco, CA",
                Merchant = "Contoso Taxi",
                Notes = string.Empty,
                TransactionAmount = 50
            });

            repository.SaveCharge(new DbCharge()
            {
                EmployeeId = employee.EmployeeId,
                ExpenseReportId = report.ExpenseReportId,
                BilledAmount = 50,
                Description = "Cab to airport",
                ExpenseDate = DateTime.Today.AddDays(-45),
                Location = "San Francisco, CA",
                Merchant = "Contoso Taxi",
                Notes = string.Empty,
                TransactionAmount = 50
            });

            // Add a year of every other month customer visits
            int x = -75;
            for (int i = 1; i <= 6; i++)
            {
                report = new DbExpenseReport()
                {
                    EmployeeId = employee.EmployeeId,
                    Amount = 850M,
                    Approver = managerAlias,
                    CostCenter = 50992,
                    DateSubmitted = DateTime.Today.AddDays(x - 5),
                    DateResolved = DateTime.Today.AddDays(x),
                    Notes = "Visit to Tailspin Toys",
                    Status = DbExpenseReportStatus.Approved,
                };
                repository.SaveExpenseReport(report);

                repository.SaveCharge(new DbCharge()
                {
                    EmployeeId = employee.EmployeeId,
                    ExpenseReportId = report.ExpenseReportId,
                    BilledAmount = 350M,
                    Description = "Airfare to Chicago",
                    ExpenseDate = DateTime.Today.AddDays(x - 15),
                    Location = "Chicago, IL",
                    Merchant = "Blue Yonder Airlines",
                    Notes = string.Empty,
                    TransactionAmount = 350M
                });

                repository.SaveCharge(new DbCharge()
                {
                    EmployeeId = employee.EmployeeId,
                    ExpenseReportId = report.ExpenseReportId,
                    BilledAmount = 50M,
                    Description = "Cab from airport",
                    ExpenseDate = DateTime.Today.AddDays(x - 5),
                    Location = "Chicago, IL",
                    Merchant = "Contoso Taxi",
                    Notes = string.Empty,
                    TransactionAmount = 50M
                });

                repository.SaveCharge(new DbCharge()
                {
                    EmployeeId = employee.EmployeeId,
                    ExpenseReportId = report.ExpenseReportId,
                    BilledAmount = 50M,
                    Description = "Cab to airport",
                    ExpenseDate = DateTime.Today.AddDays(x - 3),
                    Location = "Chicago, IL",
                    Merchant = "Contoso Taxi",
                    Notes = string.Empty,
                    TransactionAmount = 50M
                });

                repository.SaveCharge(new DbCharge()
                {
                    EmployeeId = employee.EmployeeId,
                    ExpenseReportId = report.ExpenseReportId,
                    BilledAmount = 400M,
                    Description = "2 nights hotel",
                    ExpenseDate = DateTime.Today.AddDays(x - 3),
                    Location = "Chicago, IL",
                    Merchant = "Northwind Inn",
                    Notes = string.Empty,
                    TransactionAmount = 400M
                });

                x -= 60;
            }

            // Add 18 months of cell phone charges
            x = -30;
            for (int i = 1; i <= 18; i++)
            {
                report = new DbExpenseReport()
                {
                    EmployeeId = employee.EmployeeId,
                    Amount = 850M,
                    Approver = managerAlias,
                    CostCenter = 50992,
                    DateSubmitted = DateTime.Today.AddDays(x - 5),
                    DateResolved = DateTime.Today.AddDays(x),
                    Notes = "Last month's cell phone",
                    Status = DbExpenseReportStatus.Approved,
                };
                repository.SaveExpenseReport(report);

                repository.SaveCharge(new DbCharge()
                {
                    EmployeeId = employee.EmployeeId,
                    ExpenseReportId = report.ExpenseReportId,
                    BilledAmount = 50M,
                    Description = "Cell phone bill",
                    ExpenseDate = DateTime.Today.AddDays(x - 10),
                    Location = "Seattle, WA",
                    Merchant = "The Phone Company",
                    Notes = string.Empty,
                    TransactionAmount = 50M
                });

                x -= 30;
            }

            return repository.GetEmployee(employee.EmployeeId);
        }
Beispiel #5
0
        public static DbEmployee CreateNewDemoEmployee(string alias)
        {
            var    repository   = new ExpensesRepository();
            string managerAlias = "manager";

            if (string.Compare(alias, "rogreen", true) != 0)
            {
                managerAlias = "rogreen";
            }

            DbEmployee employee =
                new DbEmployee()
            {
                Alias   = alias,
                Manager = managerAlias,
                Name    = "Kim Akers"
            };

            employee = repository.SaveEmployee(employee);

            List <DbCharge> charges = new List <DbCharge>();

            repository.SaveCharge(new DbCharge()
            {
                EmployeeId        = employee.EmployeeId,
                BilledAmount      = 200M,
                Description       = "REF# 27438948",
                ExpenseDate       = DateTime.Today.AddDays(-45),
                Location          = "San Francisco, CA",
                Merchant          = "Northwind Inn",
                Notes             = string.Empty,
                TransactionAmount = 200M
            });

            repository.SaveCharge(new DbCharge()
            {
                EmployeeId        = employee.EmployeeId,
                BilledAmount      = 40,
                Description       = "REF# 77384751",
                ExpenseDate       = DateTime.Today.AddDays(-20),
                Location          = "Seattle, WA",
                Merchant          = "Contoso Taxi",
                Notes             = string.Empty,
                TransactionAmount = 40
            });

            repository.SaveCharge(new DbCharge()
            {
                EmployeeId        = employee.EmployeeId,
                BilledAmount      = 67,
                Description       = "REF# 33748563",
                ExpenseDate       = DateTime.Today.AddDays(-8),
                Location          = "Seattle, WA",
                Merchant          = "Fourth Coffee",
                Notes             = string.Empty,
                TransactionAmount = 12
            });

            repository.SaveCharge(new DbCharge()
            {
                EmployeeId        = employee.EmployeeId,
                BilledAmount      = 17,
                Description       = "REF# 33748876",
                ExpenseDate       = DateTime.Today.AddDays(-4),
                Location          = "Seattle, WA",
                Merchant          = "Fourth Coffee",
                Notes             = string.Empty,
                TransactionAmount = 15
            });

            DbExpenseReport reportBlueYonder = repository.SaveExpenseReport(new DbExpenseReport()
            {
                EmployeeId    = employee.EmployeeId,
                Amount        = 440M,
                Approver      = managerAlias,
                CostCenter    = 50992,
                DateSubmitted = DateTime.Today.AddDays(-7),
                Notes         = "Visit to Blue Yonder Airlines",
                Status        = DbExpenseReportStatus.Saved
            });

            repository.SaveCharge(new DbCharge()
            {
                EmployeeId        = employee.EmployeeId,
                ExpenseReportId   = reportBlueYonder.ExpenseReportId,
                BilledAmount      = 277M,
                Description       = "Lodging for 1 night",
                ExpenseDate       = DateTime.Today.AddDays(-10),
                Location          = "Baldwin City, KS",
                Merchant          = "Baldwin Inn",
                Notes             = string.Empty,
                TransactionAmount = 277M
            });

            repository.SaveCharge(new DbCharge()
            {
                EmployeeId        = employee.EmployeeId,
                ExpenseReportId   = reportBlueYonder.ExpenseReportId,
                BilledAmount      = 55M,
                Description       = "Business meal",
                ExpenseDate       = DateTime.Today.AddDays(-10),
                Location          = "Baldwin City, KS",
                Merchant          = "Parkside Bar and Grill",
                Notes             = string.Empty,
                TransactionAmount = 55M
            });

            repository.SaveCharge(new DbCharge()
            {
                EmployeeId        = employee.EmployeeId,
                ExpenseReportId   = reportBlueYonder.ExpenseReportId,
                BilledAmount      = 108M,
                Description       = "Supplies",
                ExpenseDate       = DateTime.Today.AddDays(-10),
                Location          = "Baldwin City, KS",
                Merchant          = "Office Depot",
                Notes             = string.Empty,
                TransactionAmount = 108M
            });

            DbExpenseReport reportTailspinToys = new DbExpenseReport()
            {
                EmployeeId    = employee.EmployeeId,
                Amount        = 450M,
                Approver      = managerAlias,
                CostCenter    = 50992,
                DateSubmitted = DateTime.Today.AddDays(-7),
                Notes         = "Visit to Tailspin Toys",
                Status        = DbExpenseReportStatus.Saved
            };

            reportTailspinToys = repository.SaveExpenseReport(reportTailspinToys);

            repository.SaveCharge(new DbCharge()
            {
                EmployeeId        = employee.EmployeeId,
                ExpenseReportId   = reportTailspinToys.ExpenseReportId,
                BilledAmount      = 350M,
                Description       = "Airfare to San Francisco",
                ExpenseDate       = DateTime.Today.AddDays(-60),
                Location          = "Chicago, IL",
                Merchant          = "Blue Yonder Airlines",
                Notes             = string.Empty,
                TransactionAmount = 350M
            });

            repository.SaveCharge(new DbCharge()
            {
                EmployeeId        = employee.EmployeeId,
                ExpenseReportId   = reportTailspinToys.ExpenseReportId,
                BilledAmount      = 50,
                Description       = "Cab from airport",
                ExpenseDate       = DateTime.Today.AddDays(-45),
                Location          = "San Francisco, CA",
                Merchant          = "Contoso Taxi",
                Notes             = string.Empty,
                TransactionAmount = 50
            });

            repository.SaveCharge(new DbCharge()
            {
                EmployeeId        = employee.EmployeeId,
                ExpenseReportId   = reportTailspinToys.ExpenseReportId,
                BilledAmount      = 50,
                Description       = "Cab to airport",
                ExpenseDate       = DateTime.Today.AddDays(-45),
                Location          = "San Francisco, CA",
                Merchant          = "Contoso Taxi",
                Notes             = string.Empty,
                TransactionAmount = 50
            });

            // Add a year of every other month customer visits
            int x = -75;

            for (int i = 1; i <= 6; i++)
            {
                reportTailspinToys = new DbExpenseReport()
                {
                    EmployeeId    = employee.EmployeeId,
                    Amount        = 850M,
                    Approver      = managerAlias,
                    CostCenter    = 50992,
                    DateSubmitted = DateTime.Today.AddDays(x - 5),
                    DateResolved  = DateTime.Today.AddDays(x),
                    Notes         = "Visit to Tailspin Toys",
                    Status        = DbExpenseReportStatus.Approved,
                };
                repository.SaveExpenseReport(reportTailspinToys);

                repository.SaveCharge(new DbCharge()
                {
                    EmployeeId        = employee.EmployeeId,
                    ExpenseReportId   = reportTailspinToys.ExpenseReportId,
                    BilledAmount      = 350M,
                    Description       = "Airfare to Chicago",
                    ExpenseDate       = DateTime.Today.AddDays(x - 15),
                    Location          = "Chicago, IL",
                    Merchant          = "Blue Yonder Airlines",
                    Notes             = string.Empty,
                    TransactionAmount = 350M
                });

                repository.SaveCharge(new DbCharge()
                {
                    EmployeeId        = employee.EmployeeId,
                    ExpenseReportId   = reportTailspinToys.ExpenseReportId,
                    BilledAmount      = 50M,
                    Description       = "Cab from airport",
                    ExpenseDate       = DateTime.Today.AddDays(x - 5),
                    Location          = "Chicago, IL",
                    Merchant          = "Contoso Taxi",
                    Notes             = string.Empty,
                    TransactionAmount = 50M
                });

                repository.SaveCharge(new DbCharge()
                {
                    EmployeeId        = employee.EmployeeId,
                    ExpenseReportId   = reportTailspinToys.ExpenseReportId,
                    BilledAmount      = 50M,
                    Description       = "Cab to airport",
                    ExpenseDate       = DateTime.Today.AddDays(x - 3),
                    Location          = "Chicago, IL",
                    Merchant          = "Contoso Taxi",
                    Notes             = string.Empty,
                    TransactionAmount = 50M
                });

                repository.SaveCharge(new DbCharge()
                {
                    EmployeeId        = employee.EmployeeId,
                    ExpenseReportId   = reportTailspinToys.ExpenseReportId,
                    BilledAmount      = 400M,
                    Description       = "2 nights hotel",
                    ExpenseDate       = DateTime.Today.AddDays(x - 3),
                    Location          = "Chicago, IL",
                    Merchant          = "Northwind Inn",
                    Notes             = string.Empty,
                    TransactionAmount = 400M
                });

                x -= 60;
            }

            // Add 18 months of cell phone charges
            x = -30;
            for (int i = 1; i <= 18; i++)
            {
                reportTailspinToys = new DbExpenseReport()
                {
                    EmployeeId    = employee.EmployeeId,
                    Amount        = 850M,
                    Approver      = managerAlias,
                    CostCenter    = 50992,
                    DateSubmitted = DateTime.Today.AddDays(x - 5),
                    DateResolved  = DateTime.Today.AddDays(x),
                    Notes         = "Last month's cell phone",
                    Status        = DbExpenseReportStatus.Approved,
                };
                repository.SaveExpenseReport(reportTailspinToys);

                repository.SaveCharge(new DbCharge()
                {
                    EmployeeId        = employee.EmployeeId,
                    ExpenseReportId   = reportTailspinToys.ExpenseReportId,
                    BilledAmount      = 50M,
                    Description       = "Cell phone bill",
                    ExpenseDate       = DateTime.Today.AddDays(x - 10),
                    Location          = "Seattle, WA",
                    Merchant          = "The Phone Company",
                    Notes             = string.Empty,
                    TransactionAmount = 50M
                });

                x -= 30;
            }

            return(repository.GetEmployee(employee.EmployeeId));
        }
        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);
        }