Example #1
0
        public ActionResult SaveExpense(ExpenseVM view)
        {
            ModelState.Remove("Id");
            if (ModelState.IsValid)
            {
                if (view.Id == 0)
                {
                    _expenseRepository.AddToDatabase(view.MapToModel());
                    TempData["expenseCreatedOrUpdatedMessage"] = "Expense successfully created!";
                }
                else
                {
                    _expenseRepository.UpdateInDatabase(view.MapToModel(), view.Id);
                    TempData["expenseCreatedOrUpdatedMessage"] = "Expense successfully updated!";
                }
                _expenseRepository.Save();

                // calculate ExchangeRate for ExpenseSum if neccessary
                var travelReport = _travelReportRepository.FindById(view.TravelReportId).MapToView();
                var country      = _countryAllowanceRepository
                                   .GetAllFromDatabaseEnumerable()
                                   .Where(c => c.Id == travelReport.CountryAllowanceId)
                                   .SingleOrDefault()
                                   .MapToView();

                travelReport.ExpenseSum = 0;
                CalculateExpenseSum(travelReport);
            }
            return(RedirectToAction("Edit/" + view.TravelReportId));
        }
        // GET: CountryAllowance
        public ActionResult Index(int?page, string query = null)
        {
            var allowances = _countryAllowanceRepository.GetAllFromDatabaseEnumerable().ToList().MapToViews().OrderBy(o => o.Country);

            // paging
            int pageSize            = 5;
            var pageNumber          = page ?? 1;                                    // if no page was specified in the querystring, default to the first page (1)
            var onePageOfAllowances = allowances.ToPagedList(pageNumber, pageSize); // will only contain 5 items max because of the pageSize

            // searching
            if (!string.IsNullOrEmpty(query))
            {
                decimal d;
                var     allowanceSearched = _countryAllowanceRepository.GetAllFromDatabaseEnumerable()
                                            .Where(a => a.Country.ToLower().Contains(query.ToLower()) ||
                                                   a.AllowanceCurrency.ToLower().Contains(query.ToLower()) ||
                                                   a.OfficialCurrency.ToLower().Contains(query.ToLower()) ||
                                                   a.Amount.Equals(decimal.TryParse(query, out d) ? d : (decimal?)null))
                                            .ToList()
                                            .MapToViews();

                onePageOfAllowances = allowanceSearched.ToPagedList(pageNumber, pageSize);
            }

            ViewBag.onePageOfAllowances = onePageOfAllowances;
            return(View(onePageOfAllowances));
        }