Beispiel #1
0
        // GET: User
        public ActionResult Index()
        {
            if (Session["UserName"] == null)
            {
                return(RedirectToAction("Login", "Authentication"));
            }
            else
            {
                List <User>    userList    = db.GetUserList();
                List <Budget>  budgetList  = db.GetBudgetList();
                List <Expense> expenseList = db.GetExpenseList();

                string currentUser = Session["UserId"].ToString();
                int    uid         = Int32.Parse(currentUser);
                User   user        = userList.Where(a => a.UserId == uid).Single();
                //Wyświetlenie 10 ostatnio dodanych wydatków
                var expense = expenseList.Where(a => a.UserId == uid).OrderByDescending(a => a.ReleaseDate).Take(10).ToList();
                //Ostatnio dodany budżet
                Budget  lastBudget = budgetList.Where(b => b.UserId == uid).OrderByDescending(b => b.BudgetId).FirstOrDefault();
                decimal allBudgets = budgetList.Where(b => b.UserId == uid).Sum(b => b.Salary);

                //Dane do wykresu
                try
                {
                    decimal billExpense      = expenseList.Where(x => x.Type == Expense.ExpenseType.Rachunek && x.UserId == uid).Sum(x => x.Cost);
                    decimal shoppingExpense  = expenseList.Where(x => x.Type == Expense.ExpenseType.Zakupy && x.UserId == uid).Sum(x => x.Cost);
                    decimal departureExpense = expenseList.Where(x => x.Type == Expense.ExpenseType.Wyjścia && x.UserId == uid).Sum(x => x.Cost);
                    var     list             = Enum.GetValues(typeof(Expense.ExpenseType)).Cast <Expense.ExpenseType>().ToList();
                    decimal allExpense       = billExpense + shoppingExpense + departureExpense;
                    decimal _value           = allBudgets - allExpense;
                    ViewBag.Message = _value;


                    var myChart = new Chart(width: 600, height: 500)
                                  .AddTitle("Wydatki")
                                  .AddSeries(
                        name: "Wydatki",
                        chartType: "column",
                        xValue: new[] { "Rachunki", "Zakupy", "Wyjścia" },
                        yValues: new[] { billExpense, shoppingExpense, departureExpense });
                    myChart.Save("~/Content/chart.png");
                }
                catch { }

                //Przekazanie do viewmodelu
                DetailsVM detailsVM = new DetailsVM();
                detailsVM.user    = user;
                detailsVM.budget  = lastBudget;
                detailsVM.expense = expense;

                return(View("Index", detailsVM));
            }
        }
Beispiel #2
0
        public ActionResult Create(Budget budget)
        {
            string        currentUser = Session["UserId"].ToString();
            int           uid         = Int32.Parse(currentUser);
            List <Budget> budgetList  = db.GetBudgetList();


            if (Session["UserName"] == null)
            {
                return(RedirectToAction("Login", "Authentication"));
            }
            else
            {
                if (ModelState.IsValid)
                {
                    budget.UserId = int.TryParse(Session["UserId"].ToString(), out var result) ? result : -1;
                    if (budgetList.Where(a => a.UserId == uid).Count() >= 1)
                    {
                        //Wyciagniecie 2 wstecz
                        //var _budget = budgetList.Where(a => a.UserId == uid).OrderByDescending(a => a.BudgetId).Take(2).OrderBy(a => a.BudgetId).FirstOrDefault();
                        var _budget = budgetList.Where(a => a.UserId == uid).OrderByDescending(a => a.BudgetId).FirstOrDefault();
                        _budget.EndBudgetDate = budget.SalaryDate.Value.AddDays(-1);
                    }
                    db.Budgets.Add(budget);
                    db.SaveChanges();
                    return(RedirectToAction("List", "Budget"));
                }
                return(View(budget));
            }
        }