/// <summary> /// Maps the batch data. /// </summary> /// <param name="tableData">The table data.</param> /// <param name="totalRows">The total rows.</param> /// <exception cref="System.NotImplementedException"></exception> private void MapBatch(IQueryable <Row> tableData, long totalRows = 0) { var newBatches = new List <FinancialBatch>(); if (totalRows == 0) { totalRows = tableData.Count(); } var earliestBatchDate = ImportDateTime; var completedItems = 0; var percentage = (totalRows - 1) / 100 + 1; ReportProgress(0, $"Verifying batch import ({totalRows:N0} found, {ImportedBatches.Count:N0} already exist)."); foreach (var row in tableData.Where(r => r != null)) { var batchId = row["BatchID"] as int?; if (batchId.HasValue && !ImportedBatches.ContainsKey(( int )batchId)) { var batch = new FinancialBatch { CreatedByPersonAliasId = ImportPersonAliasId, ForeignKey = batchId.ToString(), ForeignId = batchId, Note = string.Empty, Status = BatchStatus.Closed, AccountingSystemCode = string.Empty }; var name = row["BatchName"] as string; if (!string.IsNullOrWhiteSpace(name)) { name = name.Trim(); batch.Name = name.Truncate(50); batch.CampusId = GetCampusId(name); } var batchDate = row["BatchDate"] as DateTime?; if (batchDate.HasValue) { batch.BatchStartDateTime = batchDate; batch.BatchEndDateTime = batchDate; if (batchDate < earliestBatchDate) { earliestBatchDate = ( DateTime )batchDate; } } var amount = row["BatchAmount"] as decimal?; if (amount.HasValue) { batch.ControlAmount = amount.Value; } newBatches.Add(batch); completedItems++; if (completedItems % percentage < 1) { var percentComplete = completedItems / percentage; ReportProgress(percentComplete, $"{completedItems:N0} batches imported ({percentComplete}% complete)."); } if (completedItems % ReportingNumber < 1) { SaveFinancialBatches(newBatches); newBatches.ForEach(b => ImportedBatches.Add(( int )b.ForeignId, ( int? )b.Id)); newBatches.Clear(); ReportPartialProgress(); } } } // add a default batch to use with contributions if (!ImportedBatches.ContainsKey(0)) { var defaultBatch = new FinancialBatch { CreatedDateTime = ImportDateTime, CreatedByPersonAliasId = ImportPersonAliasId, BatchStartDateTime = earliestBatchDate, Status = BatchStatus.Closed, Name = $"Default Batch (Imported {ImportDateTime})", ControlAmount = 0.0m, ForeignKey = "0", ForeignId = 0 }; newBatches.Add(defaultBatch); } if (newBatches.Any()) { SaveFinancialBatches(newBatches); newBatches.ForEach(b => ImportedBatches.Add(( int )b.ForeignId, ( int? )b.Id)); } ImportedBatches = new FinancialBatchService(new RockContext()).Queryable().AsNoTracking() .Where(b => b.ForeignId.HasValue) .ToDictionary(t => ( int )t.ForeignId, t => ( int? )t.Id); ReportProgress(100, $"Finished batch import: {completedItems:N0} batches imported."); }
/// <summary> /// Maps the batch data. /// </summary> /// <param name="csvData">The table data.</param> /// <exception cref="System.NotImplementedException"></exception> private int MapBatch(CSVInstance csvData) { var batchStatusClosed = Rock.Model.BatchStatus.Closed; var newBatches = new List <FinancialBatch>(); int completed = 0; ReportProgress(0, string.Format("Verifying batch import ({0:N0} already exist).", ImportedBatches.Count)); string[] row; // Uses a look-ahead enumerator: this call will move to the next record immediately while ((row = csvData.Database.FirstOrDefault()) != null) { string batchIdKey = row[BatchID]; int? batchId = batchIdKey.AsType <int?>(); if (batchId != null && !ImportedBatches.ContainsKey((int)batchId)) { var batch = new FinancialBatch(); batch.CreatedByPersonAliasId = ImportPersonAliasId; batch.ForeignKey = batchId.ToString(); batch.ForeignId = batchId; batch.Note = string.Empty; batch.Status = batchStatusClosed; batch.AccountingSystemCode = string.Empty; string name = row[BatchName] as string; if (name != null) { name = name.Trim(); batch.Name = name.Left(50); batch.CampusId = CampusList.Where(c => name.StartsWith(c.Name) || name.StartsWith(c.ShortCode)) .Select(c => (int?)c.Id).FirstOrDefault(); } string batchDateKey = row[BatchDate]; DateTime?batchDate = batchDateKey.AsType <DateTime?>(); if (batchDate != null) { batch.BatchStartDateTime = batchDate; batch.BatchEndDateTime = batchDate; } string amountKey = row[BatchAmount]; decimal?amount = amountKey.AsType <decimal?>(); if (amount != null) { batch.ControlAmount = amount.HasValue ? amount.Value : new decimal(); } newBatches.Add(batch); completed++; if (completed % (ReportingNumber * 10) < 1) { ReportProgress(0, string.Format("{0:N0} batches imported.", completed)); } else if (completed % ReportingNumber < 1) { SaveFinancialBatches(newBatches); newBatches.ForEach(b => ImportedBatches.Add((int)b.ForeignId, (int?)b.Id)); newBatches.Clear(); ReportPartialProgress(); } } } // add a default batch to use with contributions if (!ImportedBatches.ContainsKey(0)) { var defaultBatch = new FinancialBatch(); defaultBatch.CreatedDateTime = ImportDateTime; defaultBatch.CreatedByPersonAliasId = ImportPersonAliasId; defaultBatch.Status = Rock.Model.BatchStatus.Closed; defaultBatch.Name = string.Format("Default Batch (Imported {0})", ImportDateTime); defaultBatch.ControlAmount = 0.0m; defaultBatch.ForeignKey = "0"; defaultBatch.ForeignId = 0; newBatches.Add(defaultBatch); } if (newBatches.Any()) { SaveFinancialBatches(newBatches); newBatches.ForEach(b => ImportedBatches.Add((int)b.ForeignId, (int?)b.Id)); } ReportProgress(100, string.Format("Finished batch import: {0:N0} batches imported.", completed)); return(completed); }
/// <summary> /// Maps the batch data. /// </summary> /// <param name="tableData">The table data.</param> /// <exception cref="System.NotImplementedException"></exception> private void MapBatch(IQueryable <Row> tableData) { var batchStatusClosed = Rock.Model.BatchStatus.Closed; var newBatches = new List <FinancialBatch>(); int completed = 0; int totalRows = tableData.Count(); int percentage = (totalRows - 1) / 100 + 1; ReportProgress(0, string.Format("Verifying batch import ({0:N0} found, {1:N0} already exist).", totalRows, ImportedBatches.Count)); foreach (var row in tableData.Where(r => r != null)) { int?batchId = row["BatchID"] as int?; if (batchId != null && !ImportedBatches.ContainsKey((int)batchId)) { var batch = new FinancialBatch(); batch.CreatedByPersonAliasId = ImportPersonAliasId; batch.ForeignKey = batchId.ToString(); batch.ForeignId = batchId; batch.Note = string.Empty; batch.Status = batchStatusClosed; batch.AccountingSystemCode = string.Empty; string name = row["BatchName"] as string; if (name != null) { name = name.Trim(); batch.Name = name.Left(50); batch.CampusId = CampusList.Where(c => name.StartsWith(c.Name) || name.StartsWith(c.ShortCode)) .Select(c => (int?)c.Id).FirstOrDefault(); } DateTime?batchDate = row["BatchDate"] as DateTime?; if (batchDate != null) { batch.BatchStartDateTime = batchDate; batch.BatchEndDateTime = batchDate; } decimal?amount = row["BatchAmount"] as decimal?; if (amount != null) { batch.ControlAmount = amount.HasValue ? amount.Value : new decimal(); } newBatches.Add(batch); completed++; if (completed % percentage < 1) { int percentComplete = completed / percentage; ReportProgress(percentComplete, string.Format("{0:N0} batches imported ({1}% complete).", completed, percentComplete)); } else if (completed % ReportingNumber < 1) { SaveFinancialBatches(newBatches); newBatches.ForEach(b => ImportedBatches.Add((int)b.ForeignId, (int?)b.Id)); newBatches.Clear(); ReportPartialProgress(); } } } // add a default batch to use with contributions if (!ImportedBatches.ContainsKey(0)) { var defaultBatch = new FinancialBatch(); defaultBatch.CreatedDateTime = ImportDateTime; defaultBatch.CreatedByPersonAliasId = ImportPersonAliasId; defaultBatch.Status = Rock.Model.BatchStatus.Closed; defaultBatch.Name = string.Format("Default Batch (Imported {0})", ImportDateTime); defaultBatch.ControlAmount = 0.0m; defaultBatch.ForeignKey = "0"; defaultBatch.ForeignId = 0; newBatches.Add(defaultBatch); } if (newBatches.Any()) { SaveFinancialBatches(newBatches); newBatches.ForEach(b => ImportedBatches.Add((int)b.ForeignId, (int?)b.Id)); } ReportProgress(100, string.Format("Finished batch import: {0:N0} batches imported.", completed)); }
/// <summary> /// Maps the batch data. /// </summary> /// <param name="csvData">The table data.</param> /// <exception cref="System.NotImplementedException"></exception> private int MapBatch(CSVInstance csvData) { var newBatches = new List <FinancialBatch>(); var earliestBatchDate = ImportDateTime; var completed = 0; ReportProgress(0, $"Verifying batch import ({ImportedBatches.Count:N0} already exist)."); string[] row; // Uses a look-ahead enumerator: this call will move to the next record immediately while ((row = csvData.Database.FirstOrDefault()) != null) { var batchIdKey = row[BatchID]; var batchId = batchIdKey.AsType <int?>(); if (batchId != null && !ImportedBatches.ContainsKey((int)batchId)) { var batch = new FinancialBatch { CreatedByPersonAliasId = ImportPersonAliasId, ForeignKey = batchId.ToString(), ForeignId = batchId, Note = string.Empty, Status = BatchStatus.Closed, AccountingSystemCode = string.Empty }; var name = row[BatchName] as string; if (!string.IsNullOrWhiteSpace(name)) { name = name.Trim(); batch.Name = name.Left(50); batch.CampusId = CampusList.Where(c => name.StartsWith(c.Name) || name.StartsWith(c.ShortCode)) .Select(c => (int?)c.Id).FirstOrDefault(); } var batchDate = ParseDateOrDefault(row[BatchDate], null); if (batchDate.HasValue) { batch.BatchStartDateTime = batchDate; batch.BatchEndDateTime = batchDate; if (earliestBatchDate > batchDate) { earliestBatchDate = (DateTime)batchDate; } } var amountKey = row[BatchAmount]; var amount = amountKey.AsType <decimal?>(); if (amount != null) { batch.ControlAmount = amount.HasValue ? amount.Value : new decimal(); } newBatches.Add(batch); completed++; if (completed % (ReportingNumber * 10) < 1) { ReportProgress(0, $"{completed:N0} batches imported."); } else if (completed % ReportingNumber < 1) { SaveFinancialBatches(newBatches); foreach (var b in newBatches) { if (!ImportedBatches.ContainsKey((int)b.ForeignId)) { ImportedBatches.Add((int)b.ForeignId, b.Id); } else { LogException("Duplicate Batch", string.Format("Batch #{0} is a duplicate and will be skipped. Please check the source data.", b.ForeignId)); } } newBatches.Clear(); ReportPartialProgress(); } } } // add a default batch to use with contributions if (!ImportedBatches.ContainsKey(0)) { var defaultBatch = new FinancialBatch { CreatedDateTime = ImportDateTime, CreatedByPersonAliasId = ImportPersonAliasId, Status = BatchStatus.Closed, BatchStartDateTime = earliestBatchDate, Name = $"Default Batch {ImportDateTime}", ControlAmount = 0.0m, ForeignKey = "0", ForeignId = 0 }; newBatches.Add(defaultBatch); } if (newBatches.Any()) { SaveFinancialBatches(newBatches); newBatches.ForEach(b => ImportedBatches.Add((int)b.ForeignId, (int?)b.Id)); } ReportProgress(100, $"Finished batch import: {completed:N0} batches imported."); return(completed); }