public void GetBudgetTransacions() { using (FinPlannerContext _context = new FinPlannerContext()) { BudgetTransactions = _context.BudgetTransactions.Where(x => x.BudgetId == this.BudgetId).ToList(); } CFClassification classification = new CFClassification(); List <CFClassification> classifications = classification.GetList(); CFType type = new CFType(); List <CFType> types = type.GetCFList(this.CollectionId); foreach (BudgetTransaction item in BudgetTransactions) { item.CFClassification = classifications.Where(x => x.Id == item.CFClassificationId).FirstOrDefault(); item.CFType = types.Where(x => x.Id == item.CFTypeId).FirstOrDefault(); } }
public List <Account> GetAccountsWithCF(string collectionsId, int count) { CFClassification classification = new CFClassification(); List <CFClassification> classifications = classification.GetList(); List <Account> accounts = GetAccountsEmpty(collectionsId); Institution institution = new Institution(); List <Institution> institutions = institution.GetInstitutions(); AccountType type = new AccountType(); List <AccountType> types = type.GetAccountTypes(); ManualCashFlow manual = new ManualCashFlow(); AutomatedCashFlow automated = new AutomatedCashFlow(); Collections collection = new Collections(); foreach (Account item in accounts) { item.Institution = institutions .Where(x => x.Id == item.InstitutionId) .FirstOrDefault(); item.AccountType = types .Where(x => x.AccountTypeId == item.AccountTypeId) .FirstOrDefault(); item.AccountType.Accounts = null; item.ManualCashFlows = manual.GetManualCashFlows(item.Id, count); item.AutomatedCashFlows = automated.GetAutomatedCashFlows(item.Id, count); foreach (ManualCashFlow flow in item.ManualCashFlows) { flow.CFClassification = classifications.Where(x => x.Id == flow.CFClassificationId).FirstOrDefault();; flow.CFClassification.ManualCashFlows = null; } foreach (AutomatedCashFlow flow in item.AutomatedCashFlows) { flow.CFClassification = classifications.Where(x => x.Id == flow.CFClassificationId).FirstOrDefault();; flow.CFClassification.ManualCashFlows = null; } item.Collections = collection.GetCollections(collectionsId); //foreach(ManualCashFlow flow in item.ManualCashFlows) //{ // flow.Account.ManualCashFlows = null; //} } return(accounts); }
public UnseenModel(string userId) { AspNetUsers users = new AspNetUsers(); string id = users.getUserId(userId); using (FinPlannerContext _context = new FinPlannerContext()) { List <string> collections = _context .UserCollectionMapping .Where(x => x.Id == id) .Select(x => x.CollectionsId) .ToList(); List <string> accountsStr = _context .Account .Where(Acc => collections.Contains(Acc.CollectionsId)) .Select(x => x.Id) .ToList(); Account account = new Account(); Accounts = new List <Account>(); foreach (string item in collections) { Accounts .AddRange(account.GetAccounts(item)); } AutomatedCashFlow automatedCash = new AutomatedCashFlow(); ManualCashFlow manualCash = new ManualCashFlow(); CFType type = new CFType(); CFClassification classification = new CFClassification(); AutomatedCashFlows = automatedCash .GetAutomatedCashFlowsUnseen(accountsStr); ManualCashFlows = manualCash .GetManualCahFlowsUnseen(accountsStr); CFTypes = type .GetCFList(collections) .GroupBy(x => x.Id) .Select(x => x.First()) .ToList(); CFClassifications = classification .GetList(); } }
public List <ReportedTransaction> GetTransactions(List <string> accountIds, int count, List <string> collectionsIds) { //get transaction List <ManualCashFlow> manualCashFlows = new List <ManualCashFlow>(); List <AutomatedCashFlow> automatedCashFlows = new List <AutomatedCashFlow>(); using (FinPlannerContext _context = new FinPlannerContext()) { manualCashFlows = _context .ManualCashFlows .Where(cf => accountIds.Contains(cf.AccountId)) .Where(x => x.AutomatedCashFlow == null) .OrderByDescending(x => x.DateBooked) .Take(count) .ToList(); automatedCashFlows = _context .AutomatedCashFlows .Where(cf => accountIds.Contains(cf.AccountId)) .OrderByDescending(x => x.DateBooked) .Take(count) .ToList(); } //get type and classification lists CFType type = new CFType(); CFClassification classification = new CFClassification(); List <CFType> types = type.GetCFList(collectionsIds); List <CFClassification> classifications = classification.GetList(); //reconfigure cash flows List <ReportedTransaction> transactions = new List <ReportedTransaction>(); foreach (AutomatedCashFlow item in automatedCashFlows) { transactions.Add(new ReportedTransaction(item, types, classifications)); } foreach (ManualCashFlow item in manualCashFlows) { transactions.Add(new ReportedTransaction(item, types, classifications)); } return(transactions); } /// <summary>
public ReturnModel AddTransfer(TransferObject obj) { CFClassification classification = new CFClassification(); List <CFClassification> classifications = classification.GetList(); ManualCashFlow to = new ManualCashFlow() { Id = Guid.NewGuid().ToString(), CFClassificationId = classifications.Where(x => x.Sign == 1).Select(x => x.Id).FirstOrDefault(), CFTypeId = "999", Amount = obj.Amount, SourceOfExpense = "Transfer", Description = "Transfer", DateBooked = obj.DateBooked, DateCaptured = DateTime.Now, AccountId = obj.TransferTo, }; ManualCashFlow from = new ManualCashFlow() { Id = Guid.NewGuid().ToString(), CFClassificationId = classifications.Where(x => x.Sign == -1).Select(x => x.Id).FirstOrDefault(), CFTypeId = "999", SourceOfExpense = "Transfer", Description = "Transfer", Amount = obj.Amount, DateBooked = obj.DateBooked, DateCaptured = DateTime.Now, AccountId = obj.TransferFrom, }; to.Save(); from.Save(); return(new ReturnModel() { result = true }); }
private void Build() { this.Collections = new Collections(this.CollectionsId); // Get base information Budget budget = Collections.Budgets.Where(x => x.Simulation == false).OrderByDescending(x => x.StartDate).FirstOrDefault(); this.Collections.Budgets = null; Account account = new Account(); this.Collections.Accounts = account.GetAccounts(this.CollectionsId); foreach (Account acc in Collections.Accounts) { acc.ReportedTransactions = null; acc.AutomatedCashFlows = null; acc.ManualCashFlows = null; acc.Institution = null; } //this.SimulationAssumptions = new SimulationAssumptions(SimulationAssumptionsId); this.Budgets = new List <Budget>(); CFClassification cf = new CFClassification(); List <CFClassification> cfList = cf.GetList(); CFType type = new CFType(); List <CFType> typeList = type.GetCFList(CollectionsId); switch (this.Collections.DurationType) { case "Month": // Get Date of starting DateTime date = DateTime.Now; date = new DateTime(date.Year, date.Month, this.Collections.ResetDay); if (date > DateTime.Now) { date.AddMonths(1); } // Set Simulation Object Simulation simulation = new Simulation(); // Loop for the number of Months for (int i = 0; i < SimulationAssumptions.NumberOfMonths; i++) { Budget sim = new Budget(CollectionsId, date.AddMonths(i), date.AddMonths(i + 1), true); sim.AccountStates = new List <AccountState>(); // If first month, then pull BudgetTransactions from actual BudgetTransactions ELSE pull from previous month if (i == 0) { sim.BudgetTransactions = budget.BudgetTransactions.Where(x => x.Automated == false).ToList(); foreach (BudgetTransaction item in sim.BudgetTransactions) { item.CFClassification = cfList.Where(x => x.Id == item.CFClassificationId).FirstOrDefault(); item.CFType = typeList.Where(x => x.Id == item.CFTypeId).FirstOrDefault(); } foreach (Account item in Collections.Accounts) { sim.AccountStates.Add(new AccountState(item, sim.BudgetId)); } } else { sim.BudgetTransactions = this.Budgets[i - 1].BudgetTransactions.Where(x => x.Automated == false).ToList(); foreach (BudgetTransaction item in sim.BudgetTransactions) { item.CFClassification = cfList.Where(x => x.Id == item.CFClassificationId).FirstOrDefault(); } foreach (Account item in this.Collections.Accounts) { sim.AccountStates.Add(new AccountState(item, sim.BudgetId, this .Budgets[i - 1] .AccountStates .Where(x => x.AccountId == item.Id) .FirstOrDefault())); } } //======================================================================================================================================================= // Bonus Handling //======================================================================================================================================================= // Add Bonus multiple if the correct month if (SimulationAssumptions.Bonus && SimulationAssumptions.BonusMonth == date.AddMonths(i).Month) { foreach (BudgetTransaction transaction in sim.BudgetTransactions.Where(x => x.CFType.Name == "Salary")) { transaction.Amount = transaction.Amount * SimulationAssumptions.BonusAmount; } } // Strip Bonus Amount out if following Month if (SimulationAssumptions.Bonus && SimulationAssumptions.BonusMonth == date.AddMonths(i + 1).Month) { foreach (BudgetTransaction transaction in sim.BudgetTransactions.Where(x => x.CFType.Name == "Salary")) { transaction.Amount = this.Budgets[i - 1].BudgetTransactions.Where(x => x.Name == transaction.Name).Select(x => x.Amount).FirstOrDefault(); } } //======================================================================================================================================================= // Increase Handling //======================================================================================================================================================= // Add Increase if correct month (only add apportionment of previous month if not bonus month) if (SimulationAssumptions.Increase && SimulationAssumptions.IncreaseMonth == date.AddMonths(i).Month) { // if Bonus month == Increase month then use previous month if (SimulationAssumptions.IncreaseMonth == SimulationAssumptions.BonusMonth) { foreach (BudgetTransaction transaction in sim.BudgetTransactions.Where(x => x.CFType.Name == "Salary")) { transaction.Amount = this .Budgets[i - 1] .BudgetTransactions.Where(x => x.Name == transaction.Name) .Select(x => x.Amount) .FirstOrDefault() * (1 + SimulationAssumptions.IncreasePercentage); } } else { foreach (BudgetTransaction transaction in sim.BudgetTransactions.Where(x => x.CFType.Name == "Salary")) { transaction.Amount = transaction.Amount * (1 + SimulationAssumptions.IncreasePercentage); } } } //======================================================================================================================================================= // Inflation Handling //======================================================================================================================================================= if (date.AddMonths(i).Month == 1) { foreach (BudgetTransaction item in sim.BudgetTransactions.Where(x => x.CFType.Infaltion)) { item.Amount = item.Amount * 1.06; } } //======================================================================================================================================================= // Cashflow Handling //======================================================================================================================================================= if (i > 0) { //double fees = 0; //foreach(Account acc in Collections.Accounts.Where(x=>x.AccountType.Transactional)) //{ // 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); //CFClassification classification = new CFClassification("Expense"); //CFType typeT = new CFType("Bank Charges"); //sim.BudgetTransactions.Add(new BudgetTransaction //{ // BudgetId = sim.BudgetId, // Automated = true, // BudgetTransactionId = Guid.NewGuid().ToString(), // CFClassificationId = classification.Id, // CFClassification = classification, // CFType = typeT, // CFTypeId = typeT.Id, // Name = "Automated Bank Charges", // Amount = fees, //}); sim.BudgetTransactions.Add(AddAutomated(sim.BudgetId)); sim.AccountStates .Where(x => x.Amount < 0 && x.Account.AccountType.Transactional) .OrderByDescending(x => x.Account.CreditRate) .FirstOrDefault() .Update(sim.BudgetTransactions.Sum(x => x.Amount * x.CFClassification.Sign)); } this.Budgets.Add(sim); } break; } }
private void Payment() { CFClassification cf = new CFClassification(); List <CFClassification> cfList = cf.GetList(); CFType type = new CFType(); List <CFType> typeList = type.GetCFList(CollectionsId); if (SimulationAssumptions.Recurring) { for (int i = 0; i < SimulationAssumptions.NumberOfMonths; i++) { Budgets[i].BudgetTransactions.Add(new BudgetTransaction { Amount = SimulationAssumptions.Amount, Automated = true, BudgetId = Budgets[i].BudgetId, BudgetTransactionId = Guid.NewGuid().ToString(), CFClassificationId = cfList.Where(x => x.Sign == 1).FirstOrDefault().Id, CFClassification = cfList.Where(x => x.Sign == 1).FirstOrDefault(), CFTypeId = "999", CFType = typeList.Where(x => x.Id == "999").FirstOrDefault(), Name = "Recurring Payment", }); Budgets[i].BudgetTransactions.Add(new BudgetTransaction { Amount = SimulationAssumptions.Amount, Automated = true, BudgetId = Budgets[i].BudgetId, BudgetTransactionId = Guid.NewGuid().ToString(), CFClassificationId = cfList.Where(x => x.Sign == 1).FirstOrDefault().Id, CFClassification = cfList.Where(x => x.Sign == -1).FirstOrDefault(), CFTypeId = "999", CFType = typeList.Where(x => x.Id == "999").FirstOrDefault(), Name = "Recurring Payment", }); Budgets[i].AccountStates .Where(x => x.Amount < 0 && x.Account.AccountType.Transactional) .OrderByDescending(x => x.Account.CreditRate) .FirstOrDefault() .Update(-SimulationAssumptions.Amount); Budgets[i].AccountStates .Where(x => x.AccountId == SimulationAssumptions.AccountId) .FirstOrDefault() .Update(SimulationAssumptions.Amount); } } else { for (int i = 0; i < SimulationAssumptions.NumberOfMonths; i++) { if (SimulationAssumptions.ChangeDate == Budgets[i].StartDate) { Budgets[i].BudgetTransactions.Add(new BudgetTransaction { Amount = SimulationAssumptions.Amount, Automated = true, BudgetId = Budgets[i].BudgetId, BudgetTransactionId = Guid.NewGuid().ToString(), CFClassificationId = cfList.Where(x => x.Sign == 1).FirstOrDefault().Id, CFClassification = cfList.Where(x => x.Sign == 1).FirstOrDefault(), CFTypeId = "999", CFType = typeList.Where(x => x.Id == "999").FirstOrDefault(), Name = "Recurring Payment", }); Budgets[i].BudgetTransactions.Add(new BudgetTransaction { Amount = SimulationAssumptions.Amount, Automated = true, BudgetId = Budgets[i].BudgetId, BudgetTransactionId = Guid.NewGuid().ToString(), CFClassificationId = cfList.Where(x => x.Sign == 1).FirstOrDefault().Id, CFClassification = cfList.Where(x => x.Sign == -1).FirstOrDefault(), CFTypeId = "999", CFType = typeList.Where(x => x.Id == "999").FirstOrDefault(), Name = "Recurring Payment", }); Budgets[i].AccountStates .Where(x => x.Amount < 0 && x.Account.AccountType.Transactional) .OrderByDescending(x => x.Account.CreditRate) .FirstOrDefault() .Update(-SimulationAssumptions.Amount); Budgets[i].AccountStates .Where(x => x.AccountId == SimulationAssumptions.AccountId) .FirstOrDefault() .Update(SimulationAssumptions.Amount); } } } }
public async Task <bool> UpdateTransactions() { //Invoke Classes CFClassification classification = new CFClassification(); YodleeTransactionModel transactionModel = new YodleeTransactionModel(); Collections collection = new Collections(); YodleeTransactionType transactionType = new YodleeTransactionType(); ManualCashFlow manualCash = new ManualCashFlow(); AutomatedCashFlow automatedCashFlow = new AutomatedCashFlow(); //Get Static Lists List <Collections> collections = collection.GetCollections("", ""); List <CFClassification> classifications = classification.GetList(); foreach (Collections item in collections.Where(x => x.Accounts.Any())) { AutomateReturnList returnList = new AutomateReturnList(); returnList.automateReturns = new List <AutomateReturn>(); if (item.Accounts.Where(x => x.AccountIdentifier != null).Any()) { YodleeModel yodlee = new YodleeModel(); string token = await yodlee.getToken(item.CollectionsId, ""); List <YodleeTransactionLevel> transactions = await transactionModel.GetYodleeTransactions(item.CollectionsId, token); if (transactions != null) { DateTime smallest = transactions.Select(x => x.transactionDate).Min(); if (smallest > DateTime.MinValue) { smallest = smallest.AddDays(-3); } List <CFType> yodleeTypes = await transactionType.YodleeTransform(token, item.CollectionsId); foreach (Account account in item.Accounts.Where(x => x.YodleeId != 0)) { Account tempAccount = account; List <ManualCashFlow> manualFlows = manualCash.GetManualCashFlows(account.Id); foreach (ManualCashFlow m in manualFlows) { m.CFClassification = classifications.Where(x => x.Id == m.CFClassificationId).FirstOrDefault(); } account.AutomatedCashFlows = automatedCashFlow.GetAutomatedCashFlows(account.Id, smallest, DateTime.Now.AddDays(1)); foreach (YodleeTransactionLevel transaction in transactions.Where(x => x.accountId == account.YodleeId)) { if (!account.AutomatedCashFlows.Where(x => x.YodleeId == transaction.id).Any()) { ManualCashFlow manualCashFlow = manualFlows .Where(x => x.AutomatedCashFlowId == null) .Where(x => x.Amount == transaction.amount.amount && x.CFClassification.Name.ToLower() == transaction.categoryType.ToLower() && x.DateBooked > transaction.transactionDate.AddDays(-2) && x.DateBooked < transaction.transactionDate.AddDays(5)) .FirstOrDefault(); try { returnList.automateReturns.Add(AddTransaction(transaction, account.Id, yodleeTypes, manualCashFlow, classifications, tempAccount)); } catch (Exception e) { ExceptionCatcher catcher = new ExceptionCatcher(); catcher.Catch(e.Message + ";" + JsonConvert.SerializeObject(transaction) + ";" + JsonConvert.SerializeObject(manualCashFlow) + ""); } if (manualCashFlow != null) { manualFlows.Remove(manualCashFlow); } } } //item.Accounts.Where(x=>x.Id == account.Id).FirstOrDefault() = tempAccount; } try { List <AccountChange> accChangeList = new List <AccountChange>(); List <AutomatedCashFlow> cashFlows = new List <AutomatedCashFlow>(); List <ManualCashFlow> manualCashFlows = new List <ManualCashFlow>(); //ac foreach (AutomateReturn returnItem in returnList.automateReturns) { if (returnItem.AccountChange.AccountChangeId != "") { returnItem.AccountChange.Account = null; returnItem.AccountChange.AutomatedCashFlow = null; accChangeList.Add(returnItem.AccountChange); } returnItem.AutomatedCashFlow.Account = null; returnItem.AutomatedCashFlow.CFClassification = null; cashFlows.Add(returnItem.AutomatedCashFlow); if (returnItem.ManualCashFlow.Id != "") { manualCashFlows.Add(returnItem.ManualCashFlow); } } using (FinPlannerContext _context = new FinPlannerContext()) { _context.AccountChange.AddRange(accChangeList); _context.AutomatedCashFlows.AddRange(cashFlows); foreach (ManualCashFlow manual in manualCashFlows) { _context.Entry(manual).State = EntityState.Modified; } _context.SaveChanges(); } } catch (Exception e) { ExceptionCatcher catcher = new ExceptionCatcher(); catcher.Catch(e.Message); } } } } return(true); }