public ActionResult Create(TravelReportVM view) { ModelState.Remove("Id"); view.EmployeeId = User.Identity.GetUserId(); if (ModelState.IsValid) { _travelReportRepository.AddToDatabase(view.MapToModel()); _travelReportRepository.Save(); TempData["travelReportCreatedMessage"] = "Travel report successfully created!"; // add Id to view after new report is generated var latestId = _travelReportRepository.GetAllFromDatabaseEnumerable().Max(t => t.Id); view.Id = latestId; CalculateAllowanceExpenses(view); CalculatePrivateCarMileageExpenses(view); CalculateExpenseSum(view); } else { TempData["travelReportErrorMessage"] = "Something went wrong, please try again!"; } return(RedirectToAction("Index")); }
private void CalculateExpenseSum(TravelReportVM view) { var expenses = _expenseRepository.GetAllFromDatabaseEnumerable().Where(t => t.TravelReportId == view.Id).ToList(); foreach (var expense in expenses) { if (expense.Currency != "HRK" && expense.Currency != "1") // if not HRK --> calculate exchange rate { expense.Currency = _countryAllowanceRepository.FindById(int.Parse(expense.Currency)).OfficialCurrency; // from int to string expense.BillAmount = CalculateExchangeRate(expense.Currency, expense.Date, expense.BillAmount); } view.ExpenseSum = view.ExpenseSum + expense.BillAmount; } _travelReportRepository.UpdateInDatabase(view.MapToModel(), view.Id); _travelReportRepository.Save(); }
public ActionResult Edit(TravelReportVM view) { view.EmployeeId = User.Identity.GetUserId(); CalculateAllowanceExpenses(view); if (TempData["wrongNumberOfMeals"] != null) { return(RedirectToAction("Edit/" + view.Id)); } CalculatePrivateCarMileageExpenses(view); CalculateExpenseSum(view); CheckTempDataMessages(); if (ModelState.IsValid) { _travelReportRepository.UpdateInDatabase(view.MapToModel(), view.Id); _travelReportRepository.Save(); TempData["travelReportUpdatedMessage"] = "Travel report successfully updated!"; } return(RedirectToAction("Edit/" + view.Id)); }