Exemplo n.º 1
0
        public async Task <IActionResult> Add([Bind("Name,Amount,Variable,Amount,WorkHours,Function,Company,StartDate")] IncomesAddVm incomeVm)
        {
            if (ModelState.IsValid)
            {
                string           data        = HttpContext.Session.GetString("User");
                UserSessionModel userSession = JsonConvert.DeserializeObject <UserSessionModel>(data);

                var fk = await _context.tbl_users.FirstOrDefaultAsync(u => u.Id == userSession.Id);

                Income newIncomes = new Income {
                    FK = fk,

                    Name      = incomeVm.Name,
                    Amount    = incomeVm.Amount,
                    WorkHours = incomeVm.WorkHours,
                    Variable  = incomeVm.Variable,

                    Function  = incomeVm.Function,
                    Company   = incomeVm.Company,
                    StartDate = incomeVm.StartDate
                };

                _context.Add(newIncomes);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(incomeVm));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Add([Bind("Monthly,Type,Description,AccountNumber,StartDate")] SecurityAddVm securityVm)
        {
            if (ModelState.IsValid)
            {
                string           data        = HttpContext.Session.GetString("User");
                UserSessionModel userSession = JsonConvert.DeserializeObject <UserSessionModel>(data);

                var fk = await _context.tbl_users.FirstOrDefaultAsync(u => u.Id == userSession.Id);

                Security newSecurities = new Security {
                    FK = fk,

                    StartDate = DateTime.Parse(securityVm.StartDate),
                    Monthly   = securityVm.Monthly,
                    Type      = securityVm.Type,

                    Description   = securityVm.Description,
                    AccountNumber = securityVm.AccountNumber,
                };

                _context.Add(newSecurities);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(securityVm));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Add([Bind("Name,Life,Type,Variable,Cost,AccountNumber,Creditor,expenseTypes")] ExpenseAddVm expenseVm)
        {
            if (ModelState.IsValid)
            {
                string           data        = HttpContext.Session.GetString("User");
                UserSessionModel userSession = JsonConvert.DeserializeObject <UserSessionModel>(data);

                var fk = await _context.tbl_users.Where(u => u.Id == userSession.Id).FirstOrDefaultAsync();

                Expense newExpense = new Expense {
                    FK            = fk,
                    Name          = expenseVm.Name,
                    AccountNumber = expenseVm.AccountNumber,
                    Life          = expenseVm.Life,
                    Type          = expenseVm.Type,
                    Variable      = expenseVm.Variable,
                    Cost          = expenseVm.Cost,
                    Creditor      = expenseVm.Creditor
                };

                _context.Add(newExpense);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(expenseVm));
        }
Exemplo n.º 4
0
        public async Task <User> UpdateUserData(User user)
        {
            user.Total    = 0;
            user.Negative = 0;
            user.Positive = 0;

            // update expenses
            var expenses = _context.Entry(user).Collection(u => u.Expenses).Query().AsEnumerable();

            if (expenses != null)
            {
                foreach (var exp in expenses)
                {
                    user.Negative += exp.Cost;
                }
            }

            // update incomes


            var incomes = _context.Entry(user).Collection(u => u.Incomes).Query().AsEnumerable();

            if (incomes != null)
            {
                foreach (var inc in incomes)
                {
                    user.Positive += inc.Amount;
                }
            }

            user.Total = user.Positive - user.Negative;

            // save changes to db
            _context.Update(user);
            await _context.SaveChangesAsync();

            // return user to display details
            return(user);
        }