/// <summary> /// Imports sales from a csv stream into the given context. /// The caller is responsible for calling SaveChanges(). /// </summary> /// <param name="csv">The CSV stream</param> /// <param name="context">The DB Context</param> private static Tuple <int, int> SalesFromCSV(Stream csv, JBOContext context) { List <Sale> allImportedSales = new List <Sale>(); var profiler = MiniProfiler.Current; List <int> salesCodes; if (csv != null && csv.Length > 0) { var csvReader = new CsvReader(new StreamReader(csv)); csvReader.Configuration.RegisterClassMap <SalesClassMap>(); using (profiler.Step("Parsing csv")) { allImportedSales = csvReader.GetRecords <Sale>().ToList(); } //List<Sale> updated = new List<Sale>(); List <Sale> newItems = new List <Sale>(); using (profiler.Step("Getting transaction codes")) { salesCodes = context.Sales.Select(s => s.transCode).ToList(); } // Don't update sales like we did in products. // Transaction codes are unique to a transaction. // A transaction could have 30 items sold. // The system is currently not designed to distinguish which of these items // it may need to update within a given transaction. // It is also likely that ShopKeep does not allow changing this data, and we won't // receive transactions that have been changed anyway. //updated = (from s in allImportedSales where salesCodes.Contains(s.transCode) select s).ToList(); using (profiler.Step("Finding new sales")) { newItems = (from s in allImportedSales where !salesCodes.Contains(s.transCode) select s).ToList(); } using (profiler.Step("Inserting sales")) { // We are in a transaction so go ahead and insert the sales. context.BulkInsert(newItems); } return(new Tuple <int, int>(0, newItems.Count)); } return(new Tuple <int, int>(0, 0)); }
/// <summary> /// Imports products from a csv stream into the given context. /// The caller is responsible for calling SaveChanges(). /// </summary> /// <param name="csv">The CSV stream</param> /// <param name="context">The DB Context</param> private static Tuple <int, int> ProductsFromCSV(Stream csv, JBOContext context) { List <Product> allImportedProducts = new List <Product>(); var profiler = MiniProfiler.Current; List <int> products; if (csv != null && csv.Length > 0) { var csvReader = new CsvReader(new StreamReader(csv)); csvReader.Configuration.RegisterClassMap <ProductClassMap>(); allImportedProducts = csvReader.GetRecords <Product>().ToList(); List <Product> updated = new List <Product>(); List <Product> newItems = new List <Product>(); using (profiler.Step("Get Product codes")) { products = (from p in context.Products select p.productCode).ToList(); } using (profiler.Step("Find updated products")) { updated = (from p in allImportedProducts where products.Contains(p.productCode) select p).ToList(); } using (profiler.Step("Find new products")) { newItems = (from p in allImportedProducts where !products.Contains(p.productCode) select p).ToList(); } using (profiler.Step("Inserting new products")) { // We are in a transaction so go ahead and insert the // new items with the BulkInsert extension. context.BulkInsert(newItems); } using (profiler.Step("Updating products in context")) { try { context.Configuration.AutoDetectChangesEnabled = false; foreach (Product i in updated) { Product prod = context.Products.First(p => p.productCode == i.productCode); prod.productCode = i.productCode; prod.description = i.description; prod.department = i.department; prod.category = i.category; prod.upc = i.upc; prod.storeCode = i.storeCode; prod.unitPrice = i.unitPrice; prod.discountable = i.discountable; prod.taxable = i.taxable; prod.inventoryMethod = i.inventoryMethod; prod.quantity = i.quantity; prod.orderTrigger = i.orderTrigger; prod.recommendedOrder = i.recommendedOrder; prod.lastSoldDate = i.lastSoldDate; prod.supplier = i.supplier; prod.liabilityItem = i.liabilityItem; prod.LRT = i.LRT; } } finally { context.Configuration.AutoDetectChangesEnabled = true; } } return(new Tuple <int, int>(updated.Count, newItems.Count)); } return(new Tuple <int, int>(0, 0)); }