/// <summary> /// Maps the pledge. /// </summary> /// <param name="queryable">The queryable.</param> /// <exception cref="System.NotImplementedException"></exception> private void MapPledge(IQueryable <Row> tableData) { var lookupContext = new RockContext(); List <FinancialAccount> accountList = new FinancialAccountService(lookupContext).Queryable().ToList(); List <FinancialPledge> importedPledges = new FinancialPledgeService(lookupContext).Queryable().ToList(); var pledgeFrequencies = DefinedTypeCache.Read(new Guid(Rock.SystemGuid.DefinedType.FINANCIAL_FREQUENCY), lookupContext).DefinedValues; var newPledges = new List <FinancialPledge>(); int completed = 0; int totalRows = tableData.Count(); int percentage = (totalRows - 1) / 100 + 1; ReportProgress(0, string.Format("Verifying pledge import ({0:N0} found).", totalRows)); foreach (var row in tableData) { decimal? amount = row["Total_Pledge"] as decimal?; DateTime?startDate = row["Start_Date"] as DateTime?; DateTime?endDate = row["End_Date"] as DateTime?; if (amount != null && startDate != null && endDate != null) { int?individualId = row["Individual_ID"] as int?; int?householdId = row["Household_ID"] as int?; int?personId = GetPersonAliasId(individualId, householdId); if (personId != null && !importedPledges.Any(p => p.PersonAliasId == personId && p.TotalAmount == amount && p.StartDate.Equals(startDate))) { var pledge = new FinancialPledge(); pledge.CreatedByPersonAliasId = ImportPersonAlias.Id; pledge.StartDate = (DateTime)startDate; pledge.EndDate = (DateTime)endDate; pledge.TotalAmount = (decimal)amount; string frequency = row["Pledge_Frequency_Name"].ToString().ToLower(); if (frequency != null) { if (frequency == "one time" || frequency == "as can") { pledge.PledgeFrequencyValueId = pledgeFrequencies.FirstOrDefault(f => f.Guid == new Guid(Rock.SystemGuid.DefinedValue.TRANSACTION_FREQUENCY_ONE_TIME)).Id; } else { pledge.PledgeFrequencyValueId = pledgeFrequencies .Where(f => f.Value.ToLower().StartsWith(frequency) || f.Description.ToLower().StartsWith(frequency)) .Select(f => f.Id).FirstOrDefault(); } } string fundName = row["Fund_Name"] as string; string subFund = row["Sub_Fund_Name"] as string; if (fundName != null) { FinancialAccount matchingAccount = null; int?fundCampusId = null; if (subFund != null) { // match by campus if the subfund appears to be a campus fundCampusId = CampusList.Where(c => c.Name.StartsWith(subFund) || c.ShortCode == subFund) .Select(c => (int?)c.Id).FirstOrDefault(); if (fundCampusId != null) { matchingAccount = accountList.FirstOrDefault(a => a.Name.StartsWith(fundName) && a.CampusId != null && a.CampusId.Equals(fundCampusId)); } else { matchingAccount = accountList.FirstOrDefault(a => a.Name.StartsWith(fundName) && a.Name.StartsWith(subFund)); } } else { matchingAccount = accountList.FirstOrDefault(a => a.Name.StartsWith(fundName)); } if (matchingAccount == null) { matchingAccount = AddAccount(lookupContext, fundName, fundCampusId); accountList.Add(matchingAccount); } pledge.AccountId = matchingAccount.Id; } // Attributes to add? // Pledge_Drive_Name newPledges.Add(pledge); completed++; if (completed % percentage < 1) { int percentComplete = completed / percentage; ReportProgress(percentComplete, string.Format("{0:N0} pledges imported ({1}% complete).", completed, percentComplete)); } else if (completed % ReportingNumber < 1) { SavePledges(newPledges); ReportPartialProgress(); } } } } if (newPledges.Any()) { SavePledges(newPledges); } ReportProgress(100, string.Format("Finished pledge import: {0:N0} pledges imported.", completed)); }