public YodleeModel BuildYodlee(MP_CustomerMarketPlace mp, DateTime?history) { DateTime now = history ?? DateTime.UtcNow; YodleeOrderDictionary yodleeData = null; var directors = new List <string>(); _timeElapsed = new List <System.Tuple <string, double> >(); Stopwatch sw = Stopwatch.StartNew(); if (mp.Marketplace.InternalId == new YodleeServiceInfo().InternalId) { var ddh = new DatabaseDataHelper(_session); yodleeData = ddh.GetAllYodleeOrdersData(now, mp, false, out directors); } // if sw.Stop(); _timeElapsed.Add(new System.Tuple <string, double>("GetAllYodleeOrdersData", sw.Elapsed.TotalMilliseconds)); if (yodleeData == null) { Log.Debug("Yodlee model building complete for marketplace {0}: no data.", mp.Stringify()); return(null); } // if var model = new YodleeModel(); sw.Restart(); var ruleModelBuilder = new YodleeRuleModelBuilder(_session); model.RuleModel = ruleModelBuilder.Build(); sw.Stop(); _timeElapsed.Add(new System.Tuple <string, double>("YodleeRuleModel", sw.Elapsed.TotalMilliseconds)); model.BankStatementDataModel = new BankStatementDataModel(); var banks = new List <YodleeBankModel>(); var yearAgo = new DateTime(now.Year, now.Month, 1).AddYears(-1); Log.Debug("Yodlee model is being built for marketplace {0}...", mp.Stringify()); sw.Restart(); foreach (var bank in yodleeData.Data.Keys) { Log.Debug("Yodlee model is being built for marketplace {0}, bank {1}...", mp.Stringify(), bank.customName); double?availableBalance = CurrencyXchg(bank.availableBalance, bank.asOfDate.date); double?currentBalance = CurrencyXchg(bank.currentBalance, bank.asOfDate.date); double?overdraftProtection = CurrencyXchg(bank.overdraftProtection, bank.asOfDate.date); var yodleeBankModel = new YodleeBankModel { isDeleted = bank.isDeleted != 0, accountNumber = bank.accountNumber, accountHolder = bank.accountHolder, accountType = bank.acctType, availableBalance = availableBalance, currentBalance = currentBalance, accountName = bank.accountName, routingNumber = bank.routingNumber, asOfDate = bank.asOfDate.date, overdraftProtection = overdraftProtection, }; var transactions = new List <YodleeTransactionModel>(); Log.Debug("Yodlee model is being built for marketplace {0}, bank {1}, going over transactions...", mp.Stringify(), bank.customName); foreach (var transaction in yodleeData.Data[bank]) { DateTime?oDate = transaction.postDate.date ?? transaction.transactionDate.date; if (!oDate.HasValue || oDate.Value < yearAgo) { continue; //take only 12 month of data } double?transactionAmount = CurrencyXchg(transaction.transactionAmount, oDate); double?runningBalance = CurrencyXchg(transaction.runningBalance, oDate); var yodleeTransactionModel = new YodleeTransactionModel { transactionBaseType = transaction.transactionBaseType, transactionType = transaction.transactionType, transactionDate = (transaction.postDate.date ?? transaction.transactionDate.date).Value, categoryName = transaction.memo, categoryType = transaction.userDescription, transactionAmount = transactionAmount, description = transaction.description, runningBalance = runningBalance, transactionStatus = transaction.transactionStatus, bankTransactionId = transaction.bankTransactionId, ezbobGroup = transaction.siteCategory, ezbobSubGroup = transaction.siteCategoryType, ezbobGroupPriority = transaction.customCategoryId ?? 0 }; transactions.Add(yodleeTransactionModel); } // for each transaction Log.Debug("Yodlee model is being built for marketplace {0}, bank {1}, done going over transactions.", mp.Stringify(), bank.customName); yodleeBankModel.transactions = transactions.OrderByDescending(t => t.transactionDate).ToList(); banks.Add(yodleeBankModel); Log.Debug("Yodlee model is being built for marketplace {0}, done with bank {1}.", mp.Stringify(), bank.customName); } // for each bank model.banks = banks; sw.Stop(); _timeElapsed.Add(new System.Tuple <string, double>("YodleeTransactionsModel", sw.Elapsed.TotalMilliseconds)); Log.Debug("Yodlee model is being built for marketplace {0}, done with banks.", mp.Stringify()); YodleeSearchWordsModel yodleeSearchWordsModel; YodleeRunningBalanceModel yodleeRunningBalanceModel; sw.Restart(); model.CashFlowReportModel = CreateYodleeCashFlowModel(model, mp.Id, mp.Customer, directors, out yodleeSearchWordsModel, out yodleeRunningBalanceModel); model.SearchWordsModel = yodleeSearchWordsModel; model.RunningBalanceModel = yodleeRunningBalanceModel; sw.Stop(); _timeElapsed.Add(new System.Tuple <string, double>("YodleeCashFlowModel", sw.Elapsed.TotalMilliseconds)); Log.Debug("Yodlee model is ready for marketplace {0}.", mp.Stringify()); LogElapsedTimes(mp.Id, mp.Customer.Id); return(model); }
} // HasYodleeOrders public void StoreYodleeOrdersData( IDatabaseCustomerMarketPlace databaseCustomerMarketPlace, YodleeOrderDictionary ordersData, MP_CustomerMarketplaceUpdatingHistory historyRecord ) { MP_CustomerMarketPlace customerMarketPlace = GetCustomerMarketPlace(databaseCustomerMarketPlace.Id); if (ordersData == null) { return; } //don't store parsed data twice if (databaseCustomerMarketPlace.DisplayName == "ParsedBank") { if (customerMarketPlace.YodleeOrders.Any() && customerMarketPlace.YodleeOrders.SelectMany(x => x.OrderItems) .Any(x => x.accountName == ordersData.Data.Keys.First() .accountName)) { _Log.InfoFormat("ParsedBank same file won't be stored in db"); return; } } DateTime submittedDate = DateTime.UtcNow; var mpOrder = new MP_YodleeOrder { CustomerMarketPlace = customerMarketPlace, Created = submittedDate, HistoryRecord = historyRecord }; Customer customer = databaseCustomerMarketPlace.Customer; int customerId = customer.Id; string customerSurName = customer.PersonalInfo == null ? "" : customer.PersonalInfo.Surname; List <MP_YodleeGroup> yodleeGroups = new YodleeGroupRepository(this._session).GetAll().ToList(); List <MP_YodleeGroupRuleMap> yodleeGroupRules = new YodleeGroupRuleMapRepository(this._session).GetAll().ToList(); var directors = GetExperianDirectors(customer); foreach (var item in ordersData.Data.Keys) { var avaliableBalance = CurrencyXchg(item.availableBalance, item.asOfDate); var currentBalance = CurrencyXchg(item.currentBalance, item.asOfDate); var overdraftProtection = CurrencyXchg(item.overdraftProtection, item.asOfDate); var mpOrderItem = new MP_YodleeOrderItem { Order = mpOrder, isSeidFromDataSource = item.isSeidFromDataSource, isSeidMod = item.isSeidMod, acctTypeId = item.acctTypeId, acctType = item.acctType, localizedAcctType = item.localizedAcctType, srcElementId = item.srcElementId, bankAccountId = item.bankAccountId, isDeleted = item.isDeleted, lastUpdated = item.lastUpdated, hasDetails = item.hasDetails, interestRate = item.interestRate, accountNumber = item.accountNumber, link = item.link, accountHolder = item.accountHolder, tranListToDate = (item.tranListToDate != null && item.tranListToDate.dateSpecified) ? item.tranListFromDate.date : null, tranListFromDate = (item.tranListFromDate != null && item.tranListFromDate.dateSpecified) ? item.tranListFromDate.date : null, availableBalance = avaliableBalance != null ? avaliableBalance.Value : (double?)null, availableBalanceCurrency = avaliableBalance != null ? avaliableBalance.CurrencyCode : null, currentBalance = currentBalance != null ? currentBalance.Value : (double?)null, currentBalanceCurrency = currentBalance != null ? currentBalance.CurrencyCode : null, overdraftProtection = overdraftProtection != null ? overdraftProtection.Value : (double?)null, overdraftProtectionCurrency = overdraftProtection != null ? overdraftProtection.CurrencyCode : null, accountName = item.accountName, routingNumber = item.routingNumber, maturityDate = (item.maturityDate != null && item.maturityDate.dateSpecified) ? item.maturityDate.date : null, asOfDate = (item.asOfDate != null && item.asOfDate.dateSpecified) ? item.asOfDate.date : mpOrder.Created, isPaperlessStmtOn = item.isPaperlessStmtOn, siteAccountStatus = item.siteAccountStatus.ToString(), accountClassification = item.accountClassification.ToString(), created = item.created, secondaryAccountHolderName = item.secondaryAccountHolderName, accountOpenDate = (item.accountOpenDate != null && item.accountOpenDate.dateSpecified) ? item.accountOpenDate.date : null, accountCloseDate = (item.accountCloseDate != null && item.accountCloseDate.dateSpecified) ? item.accountCloseDate.date : null, itemAccountId = item.itemAccountId }; foreach (var bankTransaction in ordersData.Data[item]) { var date = bankTransaction.transactionDate; if (bankTransaction.transactionDate == null || !bankTransaction.transactionDate.date.HasValue) { date = bankTransaction.postDate; } var runningBalance = CurrencyXchg(bankTransaction.runningBalance, date); var calcRunningBalance = CurrencyXchg(bankTransaction.calcRunningBalance, date); var transactionAmount = CurrencyXchg(bankTransaction.transactionAmount, date); var orderBankTransaction = new MP_YodleeOrderItemBankTransaction { YodleeOrderItem = mpOrderItem, isSeidFromDataSource = bankTransaction.isSeidFromDataSource, isSeidMod = bankTransaction.isSeidMod, srcElementId = bankTransaction.srcElementId, transactionTypeId = bankTransaction.transactionTypeId, transactionType = bankTransaction.transactionType, transactionStatusId = bankTransaction.transactionStatusId, transactionStatus = bankTransaction.transactionStatus, transactionBaseTypeId = bankTransaction.transactionBaseTypeId, transactionBaseType = bankTransaction.transactionBaseType, categoryId = bankTransaction.categoryId, bankTransactionId = bankTransaction.bankTransactionId, bankAccountId = bankTransaction.bankAccountId, bankStatementId = bankTransaction.bankStatementId, isDeleted = bankTransaction.isDeleted, lastUpdated = bankTransaction.lastUpdated, hasDetails = bankTransaction.hasDetails, transactionId = bankTransaction.transactionId, transactionCategory = this.yodleeTransactionCategoriesRepository.GetYodleeTransactionCategoryByCategoryId( bankTransaction.transactionCategoryId), classUpdationSource = bankTransaction.classUpdationSource, lastCategorised = bankTransaction.lastCategorised, transactionDate = (bankTransaction.transactionDate != null && bankTransaction.transactionDate.dateSpecified) ? bankTransaction.transactionDate.date : null, prevLastCategorised = bankTransaction.prevLastCategorised, runningBalance = runningBalance != null ? runningBalance.Value : (double?)null, runningBalanceCurrency = runningBalance != null ? runningBalance.CurrencyCode : null, categorisationSourceId = bankTransaction.categorisationSourceId, plainTextDescription = bankTransaction.plainTextDescription, calcRunningBalance = calcRunningBalance != null ? calcRunningBalance.Value : (double?)null, calcRunningBalanceCurrency = calcRunningBalance != null ? calcRunningBalance.CurrencyCode : null, category = bankTransaction.category, link = bankTransaction.link, postDate = (bankTransaction.postDate != null && bankTransaction.postDate.dateSpecified) ? bankTransaction.postDate.date : null, prevTransactionCategoryId = bankTransaction.prevTransactionCategoryId, descriptionViewPref = bankTransaction.descriptionViewPref, prevCategorisationSourceId = bankTransaction.prevCategorisationSourceId, transactionAmount = transactionAmount != null ? transactionAmount.Value : (double?)null, transactionAmountCurrency = transactionAmount != null ? transactionAmount.CurrencyCode : null, checkNumber = bankTransaction.checkNumber, description = bankTransaction.description, categorizationKeyword = bankTransaction.categorizationKeyword, ezbobCategory = CategorizeTransaction( yodleeGroups, yodleeGroupRules, bankTransaction.description, bankTransaction.transactionBaseType, transactionAmount != null ? (int)transactionAmount.Value : 0, customerId, customerSurName, directors) }; mpOrderItem.OrderItemBankTransactions.Add(orderBankTransaction); } mpOrder.OrderItems.Add(mpOrderItem); } customerMarketPlace.YodleeOrders.Add(mpOrder); this._CustomerMarketplaceRepository.Update(customerMarketPlace); this._session.Flush(); } // StoreYodleeOrdersData
} // ConvertData private ElapsedTimeInfo UpdateClientOrdersInfo(IDatabaseCustomerMarketPlace databaseCustomerMarketPlace, YodleeSecurityInfo securityInfo, MP_CustomerMarketplaceUpdatingHistory historyRecord) { Dictionary <BankData, List <BankTransactionData> > ordersList; if (databaseCustomerMarketPlace.DisplayName == "ParsedBank") { //retrieve data from file var fileInfo = Helper.GetFileInfo((int)securityInfo.ItemId); var lastTransactionId = Helper.GetLastTransactionId(); if (fileInfo == null) { throw new Exception("file not found"); } var parser = new TransactionsParser(); var parsedData = parser.ParseFile(fileInfo.FilePath); if (parsedData == null) { throw new Exception(string.Format("failed to parse the file {0}", fileInfo.FileName)); } if (!string.IsNullOrEmpty(parsedData.Error)) { throw new Exception(string.Format("failed to parse the file {0} \n {1}", fileInfo.FileName, parsedData.Error)); } ordersList = ConvertData(parsedData, fileInfo.FileName, securityInfo.ItemId, lastTransactionId); } else { //retrieve data from Yodlee API ordersList = YodleeConnector.GetOrders(securityInfo.Name, Encrypted.Decrypt(securityInfo.Password), securityInfo.ItemId); } var elapsedTimeInfo = new ElapsedTimeInfo(); if (ordersList != null) { var newOrders = new YodleeOrderDictionary { Data = ordersList }; //save orders data ElapsedTimeHelper.CalculateAndStoreElapsedTimeForCallInSeconds(elapsedTimeInfo, databaseCustomerMarketPlace.Id, ElapsedDataMemberType.StoreDataToDatabase, () => Helper.StoreYodleeOrdersData( databaseCustomerMarketPlace, newOrders, historyRecord) ); } List <string> directors; ElapsedTimeHelper.CalculateAndStoreElapsedTimeForCallInSeconds(elapsedTimeInfo, databaseCustomerMarketPlace.Id, ElapsedDataMemberType.AggregateData, () => Helper.GetAllYodleeOrdersData(DateTime.Now, databaseCustomerMarketPlace, false, out directors)); //Thread.Sleep(15000); return(elapsedTimeInfo); } // UpdateClientsOrderInfo
} // CalculateYodleeRunningBalance public YodleeOrderDictionary GetAllYodleeOrdersData( DateTime history, IDatabaseCustomerMarketPlace databaseCustomerMarketPlace, bool isFirstTime, out List <string> directors ) { directors = new List <string>(); MP_CustomerMarketPlace mp = GetCustomerMarketPlace(databaseCustomerMarketPlace.Id); var orders = new YodleeOrderDictionary { Data = new Dictionary <BankData, List <BankTransactionData> >() }; var mpYodleeOrder = mp.YodleeOrders.OrderByDescending(x => x.Created) .FirstOrDefault(y => y.Created.Date <= history); if (mpYodleeOrder == null) { return(orders); } var orderItems = mpYodleeOrder.OrderItems.Where(x => x.isSeidMod.HasValue && x.isSeidMod.Value == 0); //Not Retrieving Seid transactions as they seem to be duplicated data foreach (MP_YodleeOrderItem item in orderItems) { BankData bankData = BankDataExt.FromYodleeOrderItem(item); List <MP_YodleeOrderItemBankTransaction> transactions = GetTransactions(mp, item.srcElementId, out directors); if (!isFirstTime) { double currentBalance = item.currentBalance == null ? 0 : item.currentBalance.Value; string currentBalanceCurrency = string.IsNullOrEmpty(item.currentBalanceCurrency) ? "GBP" : item.currentBalanceCurrency; CalculateYodleeRunningBalance(mp, item.srcElementId, this._CurrencyConvertor.ConvertToBaseCurrency( currentBalanceCurrency, currentBalance, item.asOfDate ), transactions, directors ); } // Not Retrieving Seid transactions as they seem to be duplicated data List <BankTransactionData> bankTransactionsDataList = transactions .Where(t => (t.transactionDate.HasValue && t.transactionDate.Value.Date <= history) || (t.postDate.HasValue && t.postDate.Value.Date <= history) ) .Select(BankTransactionDataExt.FromYodleeOrderItemBankTransaction) .Distinct(new YodleeOrderComparer()) .ToList(); orders.Data.Add(bankData, bankTransactionsDataList); } // for each order item this._session.Flush(); return(orders); } // GetAllYodleeOrdersData