public void ImportAccountMultipliers(DateTime?fromDate) { _log.InfoFormat("Start ImportAccountMultipliers from Date: {0}", fromDate); this.Db.ReadOnly = true; if (fromDate == null) { try { fromDate = this.Db.Context.AccountMultipliers.Max(m => m.Timestamp).AddHours(-1); } // Use timestamp } catch { fromDate = null; } } if (fromDate < DateTime.Today.AddDays(-365)) { fromDate = null; } var import = this.GetAccountMultipliers(fromDate).Where(d => d.Id != null && d.CrmAccountNumber != null && d.CrmAccountNumber != "" && d.DiscountPercentage.HasValue) .ToList(); if (import.Count == 0) { return; } // Get all current products which match imported records var currentsLookUp = Db.Context.AccountMultipliers.Distinct().ToList(); var duplicateRecords = from ac in currentsLookUp group ac by new { ac.AccountMultiplierId } into grac where grac.Count() > 1 select grac.Key; //currentsLookUp.RemoveAll(ac => duplicateRecords.Any(dac => dac.AccountMultiplierId == ac.AccountMultiplierId)); var temp = (from am in currentsLookUp join bs in Db.Businesses on am.BusinessId equals bs.BusinessId where bs.AccountId != null select new { bs.AccountId, am }) .ToList(); var currents = new Dictionary <string, DPO.Data.AccountMultiplier>(); var duplicates = new List <string>(); int i = 0; foreach (var item in temp) { var key = item.AccountId + item.am.ProductClassCode; if (i <= 0) { currents.Add(key, item.am); i++; } else { var found = currents.Any(dup => dup.Key.Contains(key)); if (!found) { currents.Add(key, item.am); } } } var businessLookup = this.Db.Businesses.Where(b => b.AccountId != null && b.AccountId != "") .ToArray().ToDictionary(b => b.AccountId); // Add any document types var addedRecords = new List <Data.AccountMultiplier>(); Db.ReadOnly = false; foreach (var irecord in import) { DPO.Data.AccountMultiplier multiplier; var key = irecord.CrmAccountNumber + irecord.ItemClassCode; if (currents.TryGetValue(key, out multiplier)) { decimal percentage = irecord.DiscountPercentage.Value; if (multiplier.Multiplier != percentage) { multiplier.Multiplier = percentage; multiplier.MultiplierTypeId = this.Db.Context.MultiplierTypes .Where(m => m.Name == irecord.ItemClassCode) .Select(m => m.MultiplierTypeId).FirstOrDefault(); multiplier.Timestamp = irecord.ModifiedOn; } } else { Business business; if (businessLookup.TryGetValue(irecord.CrmAccountNumber, out business)) { multiplier = new DPO.Data.AccountMultiplier { AccountMultiplierId = Db.Context.GenerateNextLongId(), BusinessId = business.BusinessId, Multiplier = irecord.DiscountPercentage.Value, ProductClassCode = irecord.ItemClassCode, MultiplierTypeId = this.Db.Context.MultiplierTypes .Where(m => m.Name == irecord.ItemClassCode) .Select(m => m.MultiplierTypeId).FirstOrDefault(), Timestamp = irecord.ModifiedOn }; addedRecords.Add(multiplier); currents.Add(key, multiplier); } } } Db.Context.IgnoreTimestampChecking = true; try { Db.SaveChanges(); } catch (Exception ex) { _log.FatalFormat("adding AccountMultiplier to database Failed"); _log.FatalFormat("Excetion Details: {0}", (ex.InnerException != null) ? ex.InnerException.InnerException.Message : ex.Message); } Db.Context.IgnoreTimestampChecking = false; Db.ReadOnly = true; Db.Context.AccountMultipliers.AddRange(addedRecords); try { Db.SaveChanges(); } catch (Exception ex) { _log.FatalFormat("Update database Failed"); _log.FatalFormat("Excetion Details: {0}", (ex.InnerException != null) ? ex.InnerException.InnerException.Message : ex.Message); } }
// #################################################################################### // Add products to a quote // #################################################################################### private void SeedTestAddProductsToQuote(Quote quote, List <Product> products, int productSelectionVariance, AccountMultiplier multiplier) { for (int i = 0; i < products.Count; i++) { if (productSelectionVariance % (i + 1) == 0) { var item = CreateTestQuoteItem(context, quote, products[i], i + 1, multiplier); quote.QuoteItems.Add(item); quote.Total = +item.Price * ((item.Multiplier == null) ? 1 : item.Multiplier.Value) * item.Quantity; } } }