Ejemplo n.º 1
0
        public static void NewAccount(HouseholdAccount model)
        {
            var account = new HouseholdAccount()
            {
                Name = model.Name,
                OriginalName = model.Name,
                Balance = model.Balance,
                ReconciledBalance = model.Balance,
                HouseholdId = (int)user.HouseholdId
            };

            db.HouseholdAccounts.Add(account);

            var transaction = new Transaction()
            {
                Description = "New Account: " + model.Name + " created.",
                Amount = model.Balance,
                Created = DateTimeOffset.Now,
                IsReconciled = true,
                HouseholdAccountId = model.HouseholdId,
                CategoryId = user.Household.Categories.FirstOrDefault(c => c.Name == "New Account Created").Id
            };

            db.Transactions.Add(transaction);
            db.SaveChangesAsync();
        }
        public async Task<IHttpActionResult> Create(HouseholdAccount model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            else
            {
                BP_Helper.NewAccount(model);
                await db.SaveChangesAsync();
                return Ok(model.Name + " created successfully.");
            }

        }
Ejemplo n.º 3
0
        public static void AdjustBalance(HouseholdAccount model)
        {
            var oldAccount = db.HouseholdAccounts.FirstOrDefault(a => a.Id == model.Id);
            var adjBal = oldAccount.Balance - model.Balance;

            var transaction = new Transaction()
                {
                    Description = "User Adjusted Balance",
                    Amount = adjBal,
                    IsReconciled = true,
                    CategoryId = user.Household.Categories.FirstOrDefault(c => c.Name == "User Adjusted Balance").Id,
                    Created = DateTimeOffset.Now,
                    HouseholdAccountId = model.Id
                };

            db.Transactions.Add(transaction);

            oldAccount.Balance -= adjBal;
            oldAccount.ReconciledBalance -= adjBal;

            db.SaveChangesAsync();
        }
        public async Task<IHttpActionResult> Edit(HouseholdAccount model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var oldAccount = db.HouseholdAccounts.FirstOrDefault(a => a.Id == model.Id);

            if (oldAccount.Name != model.Name)
            {
                oldAccount.Name = model.Name;
                oldAccount.OriginalName = model.Name;
            }

            //check balance
            if (oldAccount.Balance != model.Balance)
            {
                BP_Helper.AdjustBalance(model);
            }
            await db.SaveChangesAsync();
            return Ok();

        }