/// <summary> /// Add user and collection to collection mapping table /// </summary> /// <param name="obj"> New Collection Object containing user eamil as obj.user and collectionsharing id as obj.name</param> /// <returns></returns> public bool AddUserToCollection(NewCollectionsObj obj) { // 3 checks // 1 - is count == 0 CollectionSharing sharing = ShareCounter(obj.name); // 2 - is the user different from the other users-- already mapped into UserCollectionsMapping UserCollectionMapping mapping = new UserCollectionMapping(sharing.CollectionId, obj.User); if (sharing.count == 0 && mapping.CheckUser()) { // set the counter to +1 sharing.CountIncrament(); try { using (FinPlannerContext _context = new FinPlannerContext()) { _context.Entry(sharing).State = EntityState.Modified; _context.UserCollectionMapping.Add(mapping); _context.SaveChanges(); } return(true); } catch (Exception e) { return(false); } } else { return(false); } }
public ReturnModel AddAccount(Account account) { ReturnModel model = new ReturnModel(); account.AccountIdentifier = "xxxx" + account.AccountIdentifier; using (FinPlannerContext _context = new FinPlannerContext()) { if (account.Id == null) { account.Id = Guid.NewGuid().ToString(); _context.Account.Add(account); } else { Account account1 = _context.Account.Find(account.Id); account1.Update(account); _context.Entry(account1).State = EntityState.Modified; } try { _context.SaveChanges(); model.result = true; } catch (Exception e) { model.result = false; model.returnStr = e.Message; ExceptionCatcher catcher = new ExceptionCatcher(); catcher.Catch(e.Message); } } return(model);; }
public async Task <bool> UpdateAccounts(string collectionsId, List <Account> accounts) { YodleeAccountModel model = new YodleeAccountModel(); List <YodleeAccountLevel> yodleeAccounts = await model.GetYodleeAccounts(collectionsId); try { using (FinPlannerContext _context = new FinPlannerContext()) { foreach (Account item in accounts) { if (item.YodleeId == 0 && item.AccountIdentifier != null) { item.YodleeId = yodleeAccounts.Where(x => x.accountNumber == item.AccountIdentifier).Select(x => x.id).FirstOrDefault(); } else { YodleeAccountLevel accountLevel = yodleeAccounts.Where(x => x.accountNumber == item.AccountIdentifier).FirstOrDefault(); if (accountLevel != null) { if (accountLevel.availableBalance != null) { item.Available = accountLevel.availableBalance.amount; } else if (accountLevel.availableCredit != null) { item.Available = accountLevel.availableCredit.amount; } else { item.Available = accountLevel.balance.amount; } } } _context.Entry(item).State = EntityState.Modified; AccountBalance balance = new AccountBalance() { AccountBalanceId = Guid.NewGuid().ToString(), AccountId = item.Id, Amount = item.Available, Date = DateTime.Now.Date }; _context.Add(balance); } _context.SaveChanges(); } return(true); } catch (Exception e) { ExceptionCatcher catcher = new ExceptionCatcher(); catcher.Catch(e.Message); return(false); } }
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); } }
/// <summary> /// Saves the current Simulation /// </summary> private string Save() { using (FinPlannerContext _context = new FinPlannerContext()) { if (this.SimulationId == "temp") { this.SimulationId = Guid.NewGuid().ToString(); _context.Add(this.SimulationAssumptions); _context.Add(this); } else { _context.Entry(this).State = EntityState.Modified; } //_context.SaveChanges(); } return(this.SimulationId); }
public void AddAccountChange(ManualCashFlow flow) { AccountChange change = new AccountChange(); change.AccountChangeId = Guid.NewGuid().ToString(); using (FinPlannerContext _context = new FinPlannerContext()) { Account account = _context.Account.Find(flow.AccountId); CFClassification classification = _context.CFClassifications.Find(flow.CFClassificationId); change.UpdatedBalance = Math.Round(account.Available + (flow.Amount * classification.Sign), 2); account.Available = change.UpdatedBalance; change.DateAdded = DateTime.Now; change.ManualCashFlowId = flow.Id; _context.Entry(account).State = EntityState.Modified; _context.AccountChange.Add(change); _context.SaveChanges(); } }
public void updateTransaction(string manId, string autoId) { try { using (FinPlannerContext _context = new FinPlannerContext()) { ManualCashFlow man = _context.ManualCashFlows.Find(manId); man.AutomatedCashFlowId = autoId; _context.Entry(man).State = EntityState.Modified; _context.SaveChanges(); } } catch (Exception e) { ExceptionCatcher exceptionCatcher = new ExceptionCatcher(); exceptionCatcher.Catch(e.Message); } }
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); } } }
private ReturnModel updateTransaction(AutomatedCashFlow automatedCashFlow) { using (FinPlannerContext _context = new FinPlannerContext()) { try { _context.Entry(automatedCashFlow).State = EntityState.Modified; _context.SaveChanges(); return(new ReturnModel() { result = true }); } catch (Exception e) { ExceptionCatcher catcher = new ExceptionCatcher(); catcher.Catch(e.Message); return(new ReturnModel() { result = false, returnStr = e.Message }); } } }
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); } }
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); }