/// <summary>
        /// Returns a populated collections object
        /// </summary>
        /// <param name="collectionsId">Id of the collection object</param>
        public Collections(string collectionsId)
        {
            Collections   col     = new Collections();
            List <Budget> budgets = new List <Budget>();

            using (FinPlannerContext _context = new FinPlannerContext())
            {
                col     = _context.Collections.Find(collectionsId);
                budgets = _context.Budget.Where(x => x.CollectionId == collectionsId).ToList();
            }
            if (budgets.Count() == 0)
            {
                Budget budget = new Budget();
                budgets = budget.NewBudget(col);
            }
            BudgetTransaction transaction = new BudgetTransaction();

            foreach (Budget item in budgets)
            {
                item.Collection         = null;
                item.BudgetTransactions = transaction.GetBudgetTransactions(item.BudgetId);
            }
            CollectionsId = collectionsId;
            Name          = col.Name;
            Accounts      = col.Accounts;
            TotalAmount   = col.TotalAmount;
            DurationType  = col.DurationType;
            DateCreated   = col.DateCreated;
            UserCreated   = col.UserCreated;
            Budgets       = budgets;
            ResetDay      = col.ResetDay;
        }
Exemple #2
0
        private Budget getSingle(string collectionsId)
        {
            Collections       collections = new Collections(collectionsId);
            Budget            budget      = collections.Budgets.FirstOrDefault();
            BudgetTransaction budget1     = new BudgetTransaction();

            budget.BudgetTransactions = budget1.GetBudgetTransactions(budget.BudgetId);
            return(budget);
        }
Exemple #3
0
 public TransactionComparison(double spent, double budgeted, BudgetTransaction transaction)
 {
     CFClassification   = new CFClassification(transaction.CFClassificationId);
     CFType             = new CFType(transaction.CFTypeId);
     Budgeted           = budgeted;
     Spent              = spent;
     Remaining          = budgeted - spent;
     CFClassificationId = transaction.CFClassificationId;
     CFTypeId           = transaction.CFTypeId;
 }
Exemple #4
0
 /// <summary>
 /// Amount that is budgeted to be spent for under the current period for a given collection
 /// </summary>
 /// <param name="collectionsId">Unique Id of a collection</param>
 /// <returns>The amount that is expected to be spent in the current budgeting period (Total Expected Expenses)</returns>
 public double GetBudgetedAmount(string collectionsId)
 {
     try
     {
         string            Id          = GetBudgetNew(collectionsId).BudgetId;
         BudgetTransaction transaction = new BudgetTransaction();
         return(transaction.ExpectedExpenses(Id));
     }
     catch
     {
         return(0);
     }
 }
Exemple #5
0
        private Budget FindBudget(string collectionId)
        {
            Collections collection = new Collections(collectionId);
            Budget      budget     = new Budget();
            //Need to construct the date
            bool     check       = false;
            DateTime currentDate = DateTime.Now;

            switch (collection.DurationType)
            {
            case "Day":
                check = true;
                using (FinPlannerContext _context = new FinPlannerContext())
                {
                    budget = _context.Budget.Where(x => x.CollectionId == collectionId).OrderByDescending(x => x.EndDate).FirstOrDefault();
                }
                break;

            case "Week":
                using (FinPlannerContext _context = new FinPlannerContext())
                {
                    budget = _context.Budget.Where(x => x.CollectionId == collectionId && x.StartDate <currentDate && x.EndDate> currentDate).FirstOrDefault();
                }
                break;

            case "Month":
                DateTime resetDate = new DateTime(currentDate.Year, currentDate.Month, collection.ResetDay);
                if (currentDate > resetDate)
                {
                    currentDate = resetDate;
                }
                else
                {
                    currentDate = resetDate.AddMonths(-1);
                }
                using (FinPlannerContext _context = new FinPlannerContext())
                {
                    budget = _context
                             .Budget
                             .Where(x => x.CollectionId == collectionId && x.StartDate == currentDate)
                             .Where(x => x.Simulation == false)
                             .FirstOrDefault();
                }
                break;
            }
            BudgetTransaction budget1 = new BudgetTransaction();

            budget.BudgetTransactions = budget1.GetBudgetTransactions(budget.BudgetId);
            return(budget);
        }
 public void BankCharges(Collections collection)
 {
     try
     {
         Budget budget = collection.Budgets.Where(x => x.Simulation == false).OrderByDescending(x => x.EndDate).First();
         double fees   = 0;
         foreach (Account acc in collection.Accounts.Where(x => x.AccountType.Transactional == true))
         {
             fees = fees + acc.MonthlyFee;
             double debt = acc.AccountLimit - acc.Available;
             if (debt > 0)
             {
                 fees = fees + debt * (acc.CreditRate / 12 / 100);
             }
         }
         fees = Math.Round(fees, 2);
         budget.GetBudgetTransacions();
         using (FinPlannerContext _context = new FinPlannerContext())
         {
             if (budget.BudgetTransactions.Where(x => x.CFType.Name == "Bank Charges" && x.Automated == true).Any())
             {
                 BudgetTransaction transaction = budget.BudgetTransactions.Where(x => x.CFType.Name == "Bank Charges" && x.Automated == true).FirstOrDefault();
                 transaction.Amount = fees;
                 _context.Entry(transaction).State = EntityState.Modified;
             }
             else
             {
                 CFClassification  classification = new CFClassification("Expense");
                 CFType            type           = new CFType("Bank Charges");
                 BudgetTransaction transaction    = new BudgetTransaction()
                 {
                     BudgetId            = budget.BudgetId,
                     Automated           = true,
                     BudgetTransactionId = Guid.NewGuid().ToString(),
                     CFClassificationId  = classification.Id,
                     CFTypeId            = type.Id,
                     Name   = "Automated Bank Charges",
                     Amount = fees,
                 };
                 _context.Add(transaction);
             }
             _context.SaveChanges();
         }
     }
     catch (Exception e)
     {
         ExceptionCatcher catcher = new ExceptionCatcher();
         catcher.Catch(e.Message);
     }
 }
        public void Edit()
        {
            Account acc = new Account();

            Collections.Accounts = acc.GetAccountsSim(CollectionsId, SimulationId);
            for (int i = 0; i < this.SimulationAssumptions.NumberOfMonths; i++)
            {
                BudgetTransaction automated = Budgets[i].BudgetTransactions.Where(x => x.Automated).FirstOrDefault();
                Budgets[i].BudgetTransactions.Remove(automated);
                Budgets[i].BudgetTransactions.Add(AddAutomated(Budgets[i].BudgetId));
                Budgets[i].AccountStates
                .Where(x => x.Amount < 0 && x.Account.AccountType.Transactional)
                .OrderByDescending(x => x.Account.CreditRate)
                .FirstOrDefault()
                .Update(Budgets[i].BudgetTransactions.Sum(x => x.Amount * x.CFClassification.Sign));
            }
        }
Exemple #8
0
 public bool Edit(NewBudgetObj obj)
 {
     using (FinPlannerContext _context = new FinPlannerContext())
     {
         Budget budget = _context.Budget.Find(obj.BudgetId);
         List <BudgetTransaction> transactions = _context.BudgetTransactions.Where(x => x.BudgetId == obj.BudgetId).ToList();
         //split the transactions into two lists Present/Updated and New
         List <BudgetTransaction> newList = new List <BudgetTransaction>();
         BudgetTransaction        b       = new BudgetTransaction();
         bool check = false;
         foreach (BudgetTransaction item in obj.BudgetTransactions)
         {
             check = false;
             foreach (BudgetTransaction ex in transactions)
             {
                 if (item.CFClassificationId == ex.CFClassificationId && item.CFTypeId == ex.CFTypeId)
                 {
                     check     = true;
                     ex.Amount = item.Amount;
                     _context.Entry(ex).State = EntityState.Modified;
                     break;
                 }
             }
             if (!check)
             {
                 newList.Add(item);
             }
         }
         newList = b.CreateBudgetTransactions(newList, obj.BudgetId, budget.CollectionId);
         _context.AddRange(newList);
         try
         {
             _context.SaveChanges();
             return(true);
         }
         catch (Exception e)
         {
             return(false);
         }
     }
 }
Exemple #9
0
        public BudgetTransaction(BudgetTransaction b, string budgetId, string collectionsId)
        {
            BudgetTransactionId = Guid.NewGuid().ToString();
            BudgetId            = budgetId;
            Amount = b.Amount;
            Name   = b.Name;
            CFType        type = new CFType();
            List <CFType> list = type.GetCFList(collectionsId);

            if (list.Any(x => x.Id == b.CFTypeId))
            {
                CFTypeId = b.CFTypeId;
            }
            else
            {
                type     = type.CreateCFType(collectionsId, b.CFTypeId);
                CFTypeId = type.Id;
            }
            CFClassificationId = b.CFClassificationId;
            AspNetUsers users = new AspNetUsers();

            try
            {
                UserId = users.getUserId(b.UserId);
            }
            catch
            {
                FirebaseUser user = new FirebaseUser();
                FirebaseUserId = user.GetFirebaseUser(UserId);
            }
            //using (FinPlannerContext _context = new FinPlannerContext())
            //{
            //	CFType = _context.CFTypes.Find(CFTypeId);
            //	CFClassification = _context.CFClassifications.Find(CFClassificationId);
            //}
        }
Exemple #10
0
        public void Duplicate(Collections collections)
        {
            Budget                   budget                = GetBudget(collections.CollectionsId);
            BudgetTransaction        budgetTransaction     = new BudgetTransaction();
            List <BudgetTransaction> budgetTransactions    = new List <BudgetTransaction>();
            List <BudgetTransaction> newBudgetTransactions = new List <BudgetTransaction>();

            budgetTransactions = budgetTransaction.GetBudgetTransactions(budget.BudgetId);
            DateTime endDate   = DateTime.MinValue;
            DateTime StartDate = DateTime.Now;

            switch (collections.DurationType)
            {
            case "Day":
                endDate = DateTime.Now.AddDays(1);
                break;

            case "Week":
                int dayofweek  = (int)StartDate.DayOfWeek;
                int difference = Math.Abs(dayofweek - collections.ResetDay);
                StartDate = StartDate.AddDays(-difference);
                endDate   = StartDate.AddDays(7);
                break;

            case "Month":
                int day = StartDate.Day;
                if (day == 0)
                {
                    StartDate = new DateTime(StartDate.Year, StartDate.Month, 1).AddMonths(1);
                    StartDate = StartDate.AddDays(-1);
                }
                else if (day >= collections.ResetDay)
                {
                    StartDate = new DateTime(StartDate.Year, StartDate.Month, collections.ResetDay);
                }
                else
                {
                    StartDate = new DateTime(StartDate.Year, StartDate.Month - 1, collections.ResetDay);
                }
                if (collections.ResetDay == 28)
                {
                    StartDate = new DateTime(StartDate.Year, StartDate.Month, 1).AddMonths(1);
                    StartDate = StartDate.AddDays(-1);
                }
                endDate = StartDate.AddMonths(1);
                break;
            }
            Budget newBudget = new Budget
            {
                BudgetId     = Guid.NewGuid().ToString(),
                CollectionId = collections.CollectionsId,
                StartDate    = StartDate,
                EndDate      = endDate
            };

            foreach (BudgetTransaction item in budgetTransactions)
            {
                newBudgetTransactions.Add(new BudgetTransaction
                {
                    Amount              = item.Amount,
                    UserId              = item.UserId,
                    BudgetId            = newBudget.BudgetId,
                    BudgetTransactionId = Guid.NewGuid().ToString(),
                    CFClassificationId  = item.CFClassificationId,
                    CFTypeId            = item.CFTypeId,
                    Name  = item.Name,
                    Notes = item.Notes
                });
            }
            using (FinPlannerContext _context = new FinPlannerContext())
            {
                _context.Budget.Add(newBudget);
                _context.BudgetTransactions.AddRange(newBudgetTransactions);
                _context.SaveChanges();
            }
        }
Exemple #11
0
        public bool Create(NewBudgetObj obj)
        {
            string collectionId = obj.CollectionsId;
            //DateTime StartDate = obj.StartDate;
            List <BudgetTransaction> transactions = obj.BudgetTransactions;
            Collections col       = new Collections(collectionId);
            DateTime    EndDate   = new DateTime();
            DateTime    StartDate = DateTime.Now;

            switch (col.DurationType)
            {
            case "Day":
                EndDate = StartDate.AddDays(1);
                break;

            case "Week":
                int dayofweek  = (int)StartDate.DayOfWeek;
                int difference = Math.Abs(dayofweek - col.ResetDay);
                StartDate = StartDate.AddDays(-difference);
                EndDate   = StartDate.AddDays(7);
                break;

            case "Month":
                int day = StartDate.Day;
                if (day == 0)
                {
                    StartDate = new DateTime(StartDate.Year, StartDate.Month, 1).AddMonths(1);
                    StartDate = StartDate.AddDays(-1);
                }
                else if (day >= col.ResetDay)
                {
                    StartDate = new DateTime(StartDate.Year, StartDate.Month, col.ResetDay);
                }
                else
                {
                    StartDate = new DateTime(StartDate.Year, StartDate.Month - 1, col.ResetDay);
                }
                if (col.ResetDay == 28)
                {
                    StartDate = new DateTime(StartDate.Year, StartDate.Month, 1).AddMonths(1);
                    StartDate = StartDate.AddDays(-1);
                }
                EndDate = StartDate.AddMonths(1);
                break;
            }
            if (DateCheck(collectionId, EndDate))
            {
                Budget                   budget = new Budget(collectionId, StartDate, EndDate, false);
                BudgetTransaction        t      = new BudgetTransaction();
                List <BudgetTransaction> list   = t.CreateBudgetTransactions(transactions, budget.BudgetId, budget.CollectionId);
                try
                {
                    using (FinPlannerContext _context = new FinPlannerContext())
                    {
                        _context.Budget.Add(budget);
                        foreach (BudgetTransaction item in list)
                        {
                            _context.BudgetTransactions.Add(item);
                        }
                        _context.SaveChanges();
                    }
                    return(true);
                }
                catch (Exception e)
                {
                    return(false);
                }
            }
            else
            {
                // Now we are simply adding transactions
                using (FinPlannerContext _context = new FinPlannerContext())
                {
                    Budget            budget            = _context.Budget.Where(x => x.CollectionId == collectionId && x.StartDate == StartDate).FirstOrDefault();
                    BudgetTransaction budgetTransaction = new BudgetTransaction();
                    try
                    {
                        List <BudgetTransaction> budgetTransactions = budgetTransaction.GetBudgetTransactions(budget.BudgetId);

                        foreach (BudgetTransaction item in transactions)
                        {
                            //does it exist?
                            bool exists = budgetTransactions
                                          .Where(x => x.BudgetTransactionId == item.BudgetTransactionId)
                                          .Any();
                            //does not exist
                            if (!exists)
                            {
                                _context.BudgetTransactions.Add(new BudgetTransaction(item, budget.BudgetId, budget.CollectionId));
                            }
                            //does exist
                            else
                            {
                                BudgetTransaction newT = _context
                                                         .BudgetTransactions
                                                         .Find(item.BudgetTransactionId);
                                double amount = budgetTransactions
                                                .Where(x => x.BudgetTransactionId == item.BudgetTransactionId)
                                                .Select(x => x.Amount)
                                                .FirstOrDefault();
                                string name = budgetTransactions
                                              .Where(x => x.BudgetTransactionId == item.BudgetTransactionId)
                                              .Select(x => x.Name)
                                              .FirstOrDefault();
                                string typeId = budgetTransactions
                                                .Where(x => x.BudgetTransactionId == item.BudgetTransactionId)
                                                .Select(x => x.CFTypeId)
                                                .FirstOrDefault();
                                //if amount is different
                                if (amount != item.Amount)
                                {
                                    newT.Amount = item.Amount;
                                }
                                //if name is different
                                if (name != item.Name)
                                {
                                    newT.Name = item.Name;
                                }
                                if (typeId != item.CFTypeId)
                                {
                                    newT.CFTypeId = item.CFTypeId;
                                }
                                if (amount != item.Amount || name != item.Name || typeId != item.CFClassificationId)
                                {
                                    _context.Entry(newT).State = EntityState.Modified;
                                }
                            }
                        }
                        //remove deleted items
                        foreach (BudgetTransaction item in budgetTransactions)
                        {
                            //is it in the list
                            bool check = transactions.Where(x => x.BudgetTransactionId == item.BudgetTransactionId).Any();
                            if (!check)
                            {
                                _context.BudgetTransactions.Remove(item);
                            }
                        }
                        _context.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        ExceptionCatcher catcher = new ExceptionCatcher();
                        catcher.Catch(e.Message);
                    }
                }
                return(false);
            }
        }