private void ProcessOptPrices(string optPricesFilePath) { using (var csv = new CsvHelper.CsvReader(new StreamReader(optPricesFilePath, Encoding.UTF8)))// Encodings.GetEncoding()))) { LogInvalidData(string.Format("ProcessOptPrices Start 1: Encoding: {0}, File: {1}, Time: {2}", Encodings.GetEncoding(), optPricesFilePath, DateTime.Now)); csv.Configuration.Delimiter = Separators.GetCharSeparator(); csv.Configuration.HasHeaderRecord = true; Product product; bool firstRead = true; // LogInvalidData(string.Format("ProcessOptPrices Start 2: Encoding: {0}, File: {1}, Time: {2}", Encodings.GetEncoding(), optPricesFilePath, DateTime.Now)); while (csv.Read()) { try { // LogInvalidData(string.Format("ProcessOptPrices Start 3: csv: {0}, File: {1}, Time: {2}", Encodings.GetEncoding(), optPricesFilePath, DateTime.Now)); if (firstRead) { for (int i = 0; i < csv.FieldHeaders.Length; i++) { FieldMapping.Add(csv.FieldHeaders[i], i); } firstRead = false; } // LogInvalidData(string.Format("ProcessOptPrices Start 4: csv: {0}, File: {1}, Time: {2}", Encodings.GetEncoding(), optPricesFilePath, DateTime.Now)); var productInStrings = new Dictionary <ProductFields.Fields, string>(); string artNo = ProductFields.GetStringNameByEnum(ProductFields.Fields.Sku); if (FieldMapping.ContainsKey(artNo)) { productInStrings.Add(ProductFields.Fields.Sku, Convert.ToString(csv[FieldMapping[artNo]])); } //sku;amount;priceOpt1;priceOpt2;priceAkc1;priceAkc2;priceSpec;priceEur product = ProductService.GetProduct(Convert.ToString(csv[FieldMapping[artNo]])); decimal priceOpt1; decimal priceOpt2; decimal priceAkc1; decimal priceAkc2; decimal priceSpec; decimal priceEur; decimal.TryParse(csv["priceOpt1"].ToString().Replace(" ", ""), out priceOpt1); decimal.TryParse(csv["priceOpt2"].ToString().Replace(" ", ""), out priceOpt2); decimal.TryParse(csv["priceAkc1"].ToString().Replace(" ", ""), out priceAkc1); decimal.TryParse(csv["priceAkc2"].ToString().Replace(" ", ""), out priceAkc2); decimal.TryParse(csv["priceSpec"].ToString().Replace(" ", ""), out priceSpec); decimal.TryParse(csv["priceEur"].ToString().Replace(" ", ""), out priceEur); if (priceEur == 0 && csv["priceEur"].ToString().Contains(".")) { decimal.TryParse(csv["priceEur"].ToString().Replace(" ", "").Replace(".", ","), out priceEur); } string briefDescription = csv["BriefDescription"].ToString(); product.BriefDescription = briefDescription; product.Offers.Add(new Offer { Amount = product.Offers[0].Amount, OfferListId = 9, Price = priceOpt1, ShippingPrice = 0, SupplyPrice = 0, Unit = product.Offers[0].Unit, Multiplicity = product.Offers[0].Multiplicity }); product.Offers.Add(new Offer { Amount = product.Offers[0].Amount, OfferListId = 12, Price = priceOpt2, ShippingPrice = 0, SupplyPrice = 0, Unit = product.Offers[0].Unit, Multiplicity = product.Offers[0].Multiplicity }); product.Offers.Add(new Offer { Amount = product.Offers[0].Amount, OfferListId = 13, Price = priceAkc1, ShippingPrice = 0, SupplyPrice = 0, Unit = product.Offers[0].Unit, Multiplicity = product.Offers[0].Multiplicity }); product.Offers.Add(new Offer { Amount = product.Offers[0].Amount, OfferListId = 14, Price = priceAkc2, ShippingPrice = 0, SupplyPrice = 0, Unit = product.Offers[0].Unit, Multiplicity = product.Offers[0].Multiplicity }); product.Offers.Add(new Offer { Amount = product.Offers[0].Amount, OfferListId = 15, Price = priceSpec, ShippingPrice = 0, SupplyPrice = 0, Unit = product.Offers[0].Unit, Multiplicity = product.Offers[0].Multiplicity }); product.Offers.Add(new Offer { Amount = product.Offers[0].Amount, OfferListId = 16, Price = priceEur, ShippingPrice = 0, SupplyPrice = 0, Unit = product.Offers[0].Unit, Multiplicity = product.Offers[0].Multiplicity }); // LogInvalidData(string.Format("ProcessOptPrices Start 5: csv: {0}, File: {1}, Time: {2}", product.ArtNo, optPricesFilePath, DateTime.Now)); ProductService.UpdateProduct(product, false); // LogInvalidData(string.Format("ProcessOptPrices Start 6: csv: {0}, File: {1}, Time: {2}", product.ArtNo, optPricesFilePath, DateTime.Now)); } catch (Exception ex) { LogInvalidData("Ex " + ex.Message + DateTime.Now.ToString()); } } } try { File.Copy(optPricesFilePath, optPricesFilePath.Replace("OstatkiWseCeny.csv", string.Format("OstatkiWseCeny_done{0}.csv", DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")))); FileHelpers.DeleteFile(optPricesFilePath); } catch (Exception ex) { LogInvalidData("Ex File.Copy(... " + ex.Message + DateTime.Now.ToString()); } }
private void ProcessCsv() { string _filePath = FoldersHelper.GetPathAbsolut(FolderType.PriceTemp); string _fullPath = string.Format("{0}{1}", _filePath, "ostatki.csv"); LogInvalidData("Start working on Update (1C). File " + _fullPath + DateTime.Now.ToString()); if (!File.Exists(_fullPath)) { return; } long count = 0; using (var csv = new CsvHelper.CsvReader(new StreamReader(_fullPath, Encodings.GetEncoding()))) { csv.Configuration.Delimiter = Separators.GetCharSeparator(); csv.Configuration.HasHeaderRecord = true; while (csv.Read()) { count++; } } ImportStatistic.TotalRow = count; LogInvalidData("ImportStatistic.TotalRow = " + count.ToString() + DateTime.Now.ToString()); using (var csv = new CsvHelper.CsvReader(new StreamReader(_fullPath, Encodings.GetEncoding()))) { csv.Configuration.Delimiter = Separators.GetCharSeparator(); csv.Configuration.HasHeaderRecord = true; bool firstRead = true; while (csv.Read()) { try { if (firstRead) { for (int i = 0; i < csv.FieldHeaders.Length; i++) { FieldMapping.Add(csv.FieldHeaders[i], i); } firstRead = false; } var productInStrings = new Dictionary <ProductFields.Fields, string>(); string nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Sku); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Sku, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Name).Trim('*'); if (FieldMapping.ContainsKey(nameField)) { var name = Convert.ToString(csv[FieldMapping[nameField]]); if (!string.IsNullOrEmpty(name)) { productInStrings.Add(ProductFields.Fields.Name, name); } else { LogInvalidData(string.Format(Resource.Admin_ImportCsv_CanNotEmpty, ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Name), ImportStatistic.RowPosition + 2)); continue; } } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Enabled).Trim('*'); if (FieldMapping.ContainsKey(nameField)) { string enabled = Convert.ToString(csv[FieldMapping[nameField]]); productInStrings.Add(ProductFields.Fields.Enabled, enabled); //product.Enabled = !string.IsNullOrEmpty(enabled) && enabled.Trim().Equals("+"); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Discount); if (FieldMapping.ContainsKey(nameField)) { var discount = Convert.ToString(csv[FieldMapping[nameField]]); if (string.IsNullOrEmpty(discount)) { discount = "0"; } decimal tmp; if (decimal.TryParse(discount, out tmp)) { productInStrings.Add(ProductFields.Fields.Discount, tmp.ToString()); } else if (decimal.TryParse(discount, NumberStyles.Any, CultureInfo.InvariantCulture, out tmp)) { productInStrings.Add(ProductFields.Fields.Discount, tmp.ToString()); } else { LogInvalidData(string.Format(Resource.Admin_ImportCsv_MustBeNumber, ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Discount), ImportStatistic.RowPosition + 2)); continue; } } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Weight); if (FieldMapping.ContainsKey(nameField)) { var weight = Convert.ToString(csv[FieldMapping[nameField]]); if (string.IsNullOrEmpty(weight)) { weight = "0"; } decimal tmp; if (decimal.TryParse(weight, out tmp)) { productInStrings.Add(ProductFields.Fields.Weight, tmp.ToString()); } else if (decimal.TryParse(weight, NumberStyles.Any, CultureInfo.InvariantCulture, out tmp)) { productInStrings.Add(ProductFields.Fields.Weight, tmp.ToString()); } else { LogInvalidData(string.Format(Resource.Admin_ImportCsv_MustBeNumber, ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Weight), ImportStatistic.RowPosition + 2)); continue; } } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Size); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Size, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.BriefDescription); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.BriefDescription, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Description); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Description, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Price).Trim('*'); if (FieldMapping.ContainsKey(nameField)) { var price = Convert.ToString(csv[FieldMapping[nameField]]); if (string.IsNullOrEmpty(price)) { price = "0"; } decimal tmp; if (decimal.TryParse(price, out tmp)) { productInStrings.Add(ProductFields.Fields.Price, tmp.ToString()); } else if (decimal.TryParse(price, NumberStyles.Any, CultureInfo.InvariantCulture, out tmp)) { productInStrings.Add(ProductFields.Fields.Price, tmp.ToString()); } else { LogInvalidData(string.Format(Resource.Admin_ImportCsv_MustBeNumber, ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Price), ImportStatistic.RowPosition + 2)); continue; } } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.PurchasePrice); if (FieldMapping.ContainsKey(nameField)) { var sypplyprice = Convert.ToString(csv[FieldMapping[nameField]]); if (string.IsNullOrEmpty(sypplyprice)) { sypplyprice = "0"; } decimal tmp; if (decimal.TryParse(sypplyprice, out tmp)) { productInStrings.Add(ProductFields.Fields.PurchasePrice, tmp.ToString()); } else if (decimal.TryParse(sypplyprice, NumberStyles.Any, CultureInfo.InvariantCulture, out tmp)) { productInStrings.Add(ProductFields.Fields.PurchasePrice, tmp.ToString()); } else { LogInvalidData(string.Format(Resource.Admin_ImportCsv_MustBeNumber, ProductFields.GetDisplayNameByEnum(ProductFields.Fields.PurchasePrice), ImportStatistic.RowPosition + 2)); continue; } } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.ShippingPrice); if (FieldMapping.ContainsKey(nameField)) { var shippingPrice = Convert.ToString(csv[FieldMapping[nameField]]); if (string.IsNullOrEmpty(shippingPrice)) { shippingPrice = "0"; } decimal tmp; if (decimal.TryParse(shippingPrice, out tmp)) { productInStrings.Add(ProductFields.Fields.ShippingPrice, tmp.ToString()); } else if (decimal.TryParse(shippingPrice, NumberStyles.Any, CultureInfo.InvariantCulture, out tmp)) { productInStrings.Add(ProductFields.Fields.ShippingPrice, tmp.ToString()); } else { LogInvalidData(string.Format(Resource.Admin_ImportCsv_MustBeNumber, ProductFields.GetDisplayNameByEnum(ProductFields.Fields.ShippingPrice), ImportStatistic.RowPosition + 2)); continue; } } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Amount); if (FieldMapping.ContainsKey(nameField)) { var amount = Convert.ToString(csv[FieldMapping[nameField]]); if (string.IsNullOrEmpty(amount)) { amount = "0"; } int tmp; if (int.TryParse(amount, out tmp)) { productInStrings.Add(ProductFields.Fields.Amount, amount); } else { LogInvalidData(string.Format(Resource.Admin_ImportCsv_MustBeNumber, ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Amount), ImportStatistic.RowPosition + 2)); continue; } } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Unit); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Unit, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.ParamSynonym); if (FieldMapping.ContainsKey(nameField)) { string rewurl = Convert.ToString(csv[FieldMapping[nameField]]); productInStrings.Add(ProductFields.Fields.ParamSynonym, rewurl); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Title); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Title, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.MetaKeywords); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.MetaKeywords, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.MetaDescription); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.MetaDescription, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Photos); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Photos, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Markers); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Markers, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Properties); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Properties, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Producer); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Producer, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.OrderByRequest); if (FieldMapping.ContainsKey(nameField)) { string orderbyrequest = Convert.ToString(csv[FieldMapping[nameField]]); productInStrings.Add(ProductFields.Fields.OrderByRequest, orderbyrequest); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Category).Trim('*'); if (FieldMapping.ContainsKey(nameField)) { var parentCategory = Convert.ToString(csv[FieldMapping[nameField]]); if (!string.IsNullOrEmpty(parentCategory)) { productInStrings.Add(ProductFields.Fields.Category, parentCategory); } } ImportProduct.UpdateInsertProduct(productInStrings, false); } catch (Exception ex) { LogInvalidData("Ex " + ex.Message + DateTime.Now.ToString()); } } //All amounts to zero. except that was updated 0.02 = 30 minutes before and SubBrandID != -2 (tools.by) string script = "UPDATE Catalog.Offer SET Amount = 0 FROM Catalog.Product INNER JOIN Catalog.Offer ON Catalog.Product.ProductId = Catalog.Offer.ProductID WHERE (Catalog.Product.DateModified < GETDATE() - 0.02) AND Amount < 9000 "; ExecuteScript(script); LogInvalidData("Amounts of products that was not updated to zero" + DateTime.Now.ToString()); LogInvalidData(" CategoryService.RecalculateProductsCountManual() before " + DateTime.Now.ToString()); CategoryService.RecalculateProductsCountManual(); LogInvalidData(" CategoryService.RecalculateProductsCountManual() after " + DateTime.Now.ToString()); } try { LogInvalidData(" LuceneSearch.CreateAllIndexInBackground(); " + DateTime.Now.ToString()); LuceneSearch.CreateAllIndexInBackground(); CacheManager.Clean(); LogInvalidData(" File.Cop file:" + _fullPath + DateTime.Now.ToString()); // File.Copy(_fullPath, _fullPath.Replace("ostatki.csv", "ostatki_done_" + DateTime.Now.ToString("M-d-yyyy-HH-mm") + ".csv")); FileHelpers.DeleteFile(_fullPath.Replace("ostatki.csv", "Instrument_opt_by__ostatki.csv")); File.Copy(_fullPath, _fullPath.Replace("ostatki.csv", "Instrument_opt_by__ostatki.csv")); FileHelpers.DeleteFile(_fullPath); //UPdate Sorting LogInvalidData(" //UPdate Sorting " + DateTime.Now.ToString()); AdvantShop.Core.SQLDataAccess.ExecuteNonQuery("[Catalog].[sp_UpdateSortingInCategories]", CommandType.StoredProcedure); LogInvalidData(" All Done: " + DateTime.Now.ToString()); } catch (Exception ex) { LogInvalidData("Ex 2 " + ex.Message + DateTime.Now.ToString()); } }
private void ProcessCsv() { if (chboxDisableProducts.Checked) { ProductService.DisableAllProducts(); } long count = 0; using (var csv = new CsvHelper.CsvReader(new StreamReader(_fullPath, Encodings.GetEncoding()))) { csv.Configuration.Delimiter = Separators.GetCharSeparator(); csv.Configuration.HasHeaderRecord = _hasHeadrs; while (csv.Read()) { count++; } } ImportStatistic.TotalRow = count; using (var csv = new CsvHelper.CsvReader(new StreamReader(_fullPath, Encodings.GetEncoding()))) { csv.Configuration.Delimiter = Separators.GetCharSeparator(); csv.Configuration.HasHeaderRecord = _hasHeadrs; while (csv.Read()) { if (!ImportStatistic.IsRun) { csv.Dispose(); FileHelpers.DeleteFile(_fullPath); return; } try { //Added by Evgeni to Remove dots // csv.CurrentRecord[0] = csv.CurrentRecord[0].Replace(".", "").Replace(" ", ""); // Step by rows var productInStrings = new Dictionary <ProductFields.Fields, string>(); string nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Sku); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Sku, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Name).Trim('*'); if (FieldMapping.ContainsKey(nameField)) { var name = Convert.ToString(csv[FieldMapping[nameField]]); if (!string.IsNullOrEmpty(name)) { productInStrings.Add(ProductFields.Fields.Name, name); } else { LogInvalidData(string.Format(Resource.Admin_ImportCsv_CanNotEmpty, ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Name), ImportStatistic.RowPosition + 2)); continue; } } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Enabled).Trim('*'); if (FieldMapping.ContainsKey(nameField)) { string enabled = Convert.ToString(csv[FieldMapping[nameField]]); productInStrings.Add(ProductFields.Fields.Enabled, enabled); //product.Enabled = !string.IsNullOrEmpty(enabled) && enabled.Trim().Equals("+"); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Discount); if (FieldMapping.ContainsKey(nameField)) { var discount = Convert.ToString(csv[FieldMapping[nameField]]); if (string.IsNullOrEmpty(discount)) { discount = "0"; } decimal tmp; if (decimal.TryParse(discount, out tmp)) { productInStrings.Add(ProductFields.Fields.Discount, tmp.ToString()); } else if (decimal.TryParse(discount, NumberStyles.Any, CultureInfo.InvariantCulture, out tmp)) { productInStrings.Add(ProductFields.Fields.Discount, tmp.ToString()); } else { LogInvalidData(string.Format(Resource.Admin_ImportCsv_MustBeNumber, ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Discount), ImportStatistic.RowPosition + 2)); continue; } } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Weight); if (FieldMapping.ContainsKey(nameField)) { var weight = Convert.ToString(csv[FieldMapping[nameField]]); if (string.IsNullOrEmpty(weight)) { weight = "0"; } decimal tmp; if (decimal.TryParse(weight, out tmp)) { productInStrings.Add(ProductFields.Fields.Weight, tmp.ToString()); } else if (decimal.TryParse(weight, NumberStyles.Any, CultureInfo.InvariantCulture, out tmp)) { productInStrings.Add(ProductFields.Fields.Weight, tmp.ToString()); } else { LogInvalidData(string.Format(Resource.Admin_ImportCsv_MustBeNumber, ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Weight), ImportStatistic.RowPosition + 2)); continue; } } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Size); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Size, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.BriefDescription); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.BriefDescription, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Description); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Description, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Price).Trim('*'); if (FieldMapping.ContainsKey(nameField)) { var price = Convert.ToString(csv[FieldMapping[nameField]]); if (string.IsNullOrEmpty(price)) { price = "0"; } decimal tmp; if (decimal.TryParse(price, out tmp)) { productInStrings.Add(ProductFields.Fields.Price, tmp.ToString()); } else if (decimal.TryParse(price, NumberStyles.Any, CultureInfo.InvariantCulture, out tmp)) { productInStrings.Add(ProductFields.Fields.Price, tmp.ToString()); } else { LogInvalidData(string.Format(Resource.Admin_ImportCsv_MustBeNumber, ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Price), ImportStatistic.RowPosition + 2)); continue; } } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.PurchasePrice); if (FieldMapping.ContainsKey(nameField)) { var sypplyprice = Convert.ToString(csv[FieldMapping[nameField]]); if (string.IsNullOrEmpty(sypplyprice)) { sypplyprice = "0"; } decimal tmp; if (decimal.TryParse(sypplyprice, out tmp)) { productInStrings.Add(ProductFields.Fields.PurchasePrice, tmp.ToString()); } else if (decimal.TryParse(sypplyprice, NumberStyles.Any, CultureInfo.InvariantCulture, out tmp)) { productInStrings.Add(ProductFields.Fields.PurchasePrice, tmp.ToString()); } else { LogInvalidData(string.Format(Resource.Admin_ImportCsv_MustBeNumber, ProductFields.GetDisplayNameByEnum(ProductFields.Fields.PurchasePrice), ImportStatistic.RowPosition + 2)); continue; } } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.ShippingPrice); if (FieldMapping.ContainsKey(nameField)) { var shippingPrice = Convert.ToString(csv[FieldMapping[nameField]]); if (string.IsNullOrEmpty(shippingPrice)) { shippingPrice = "0"; } decimal tmp; if (decimal.TryParse(shippingPrice, out tmp)) { productInStrings.Add(ProductFields.Fields.ShippingPrice, tmp.ToString()); } else if (decimal.TryParse(shippingPrice, NumberStyles.Any, CultureInfo.InvariantCulture, out tmp)) { productInStrings.Add(ProductFields.Fields.ShippingPrice, tmp.ToString()); } else { LogInvalidData(string.Format(Resource.Admin_ImportCsv_MustBeNumber, ProductFields.GetDisplayNameByEnum(ProductFields.Fields.ShippingPrice), ImportStatistic.RowPosition + 2)); continue; } } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Amount); if (FieldMapping.ContainsKey(nameField)) { var amount = Convert.ToString(csv[FieldMapping[nameField]]); if (string.IsNullOrEmpty(amount)) { amount = "0"; } int tmp; if (int.TryParse(amount, out tmp)) { productInStrings.Add(ProductFields.Fields.Amount, amount); } else { LogInvalidData(string.Format(Resource.Admin_ImportCsv_MustBeNumber, ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Amount), ImportStatistic.RowPosition + 2)); continue; } } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Unit); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Unit, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.ParamSynonym); if (FieldMapping.ContainsKey(nameField)) { string rewurl = Convert.ToString(csv[FieldMapping[nameField]]); productInStrings.Add(ProductFields.Fields.ParamSynonym, rewurl); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Title); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Title, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.MetaKeywords); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.MetaKeywords, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.MetaDescription); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.MetaDescription, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Photos); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Photos, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Markers); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Markers, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Properties); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Properties, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Producer); if (FieldMapping.ContainsKey(nameField)) { productInStrings.Add(ProductFields.Fields.Producer, Convert.ToString(csv[FieldMapping[nameField]])); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.OrderByRequest); if (FieldMapping.ContainsKey(nameField)) { string orderbyrequest = Convert.ToString(csv[FieldMapping[nameField]]); productInStrings.Add(ProductFields.Fields.OrderByRequest, orderbyrequest); } nameField = ProductFields.GetStringNameByEnum(ProductFields.Fields.Category).Trim('*'); if (FieldMapping.ContainsKey(nameField)) { var parentCategory = Convert.ToString(csv[FieldMapping[nameField]]); if (!string.IsNullOrEmpty(parentCategory)) { productInStrings.Add(ProductFields.Fields.Category, parentCategory); } } ImportProduct.UpdateInsertProduct(productInStrings); } catch (Exception ex) { MsgErr(ex.Message + " at csv"); Debug.LogError(ex); } } CategoryService.RecalculateProductsCountManual(); } ImportStatistic.IsRun = false; LuceneSearch.CreateAllIndexInBackground(); CacheManager.Clean(); FileHelpers.DeleteFilesFromImageTempInBackground(); FileHelpers.DeleteFile(_fullPath); }
protected void Page_Init(object sender, EventArgs e) { MsgErr(true); divStart.Visible = !ImportStatistic.IsRun && (string.IsNullOrEmpty(Request["action"])); divAction.Visible = !ImportStatistic.IsRun && (Request["action"] == "start"); choseDiv.Visible = !ImportStatistic.IsRun; OutDiv.Visible = ImportStatistic.IsRun; linkCancel.Visible = ImportStatistic.IsRun; if (ImportStatistic.IsRun || (Request["action"] != "start")) { return; } if (!File.Exists(_fullPath)) { return; } var tbl = new Table() { ID = "tblValues" }; var namesRow = new TableRow { ID = "namesRow", BackColor = System.Drawing.ColorTranslator.FromHtml("#0D76B8") }; var firstValRow = new TableRow { ID = "firstValsRow" }; var ddlRow = new TableRow { ID = "ddlRow" }; var firstCell = new TableCell { Width = 200, BackColor = System.Drawing.Color.White, }; firstCell.Controls.Add(new Label { Text = Resources.Resource.Admin_ImportCsv_Column, CssClass = "firstColumn" }); var div1 = new Panel { CssClass = "arrow_left_bg" }; div1.Controls.Add(new Panel { CssClass = "arrow_right_bg" }); firstCell.Controls.Add(div1); var secondCell = new TableCell { Width = 200 }; secondCell.Controls.Add(new Label { Text = Resources.Resource.Admin_ImportCsv_FistLineInTheFile, CssClass = "firstColumn" }); var div2 = new Panel { CssClass = "arrow_left_bg_two" }; div2.Controls.Add(new Panel { CssClass = "arrow_right_bg" }); secondCell.Controls.Add(div2); var firdCell = new TableCell { Width = 200 }; firdCell.Controls.Add(new Label { Text = Resources.Resource.Admin_ImportCsv_DataType, CssClass = "firstColumn" }); var div3 = new Panel { CssClass = "arrow_left_bg_free" }; div3.Controls.Add(new Panel { CssClass = "arrow_right_bg" }); firdCell.Controls.Add(div3); var div4 = new Panel { Width = 200 }; firdCell.Controls.Add(div4); namesRow.Cells.Add(firstCell); firstValRow.Cells.Add(secondCell); ddlRow.Cells.Add(firdCell); _hasHeadrs = chbHasHeadrs.Checked; using (var csv = new CsvHelper.CsvReader(new StreamReader(_fullPath, Encodings.GetEncoding()))) { csv.Configuration.Delimiter = Separators.GetCharSeparator(); csv.Configuration.BufferSize = 0x1000; csv.Configuration.HasHeaderRecord = false; csv.Read(); if (_hasHeadrs && csv.CurrentRecord.HasDuplicates()) { var strFileds = string.Empty; foreach (var item in csv.CurrentRecord.Duplicates()) { strFileds += "\"" + item + "\","; } MsgErr(Resource.Admin_ImportCsv_DuplicateHeader + strFileds.Trim(',')); btnAction.Visible = false; } for (int i = 0; i < csv.CurrentRecord.Length; i++) { //Added by Evgeni to Import csv with dotes // csv.CurrentRecord[0] = csv.CurrentRecord[0].Replace(".", "").Replace(" ", ""); var cell = new TableCell { ID = "cell" + i.ToString() }; var lb = new Label(); bool flagMustReqField = false; if (Request["hasheadrs"].ToLower() == "true") { var tempCsv = (csv[i].Length > 50 ? csv[i].Substring(0, 49) : csv[i]).Replace("*", ""); if (_mustRequiredFfield.Any(item => ProductFields.GetStringNameByEnum(item).Replace("*", "") == tempCsv.ToLower())) { flagMustReqField = true; } lb.Text = tempCsv; } else { lb.Text = Resource.Admin_ImportCsv_Empty; } lb.ForeColor = System.Drawing.Color.White; cell.Controls.Add(lb); if (flagMustReqField) { var lbl = new Label { Text = @"*", ForeColor = System.Drawing.Color.Red }; cell.Controls.Add(lbl); } namesRow.Cells.Add(cell); cell = new TableCell() { Width = 150 }; var ddl = new DropDownList { ID = "ddlType" + i.ToString(), Width = 150 }; ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.None), ProductFields.GetStringNameByEnum(ProductFields.Fields.None))); ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Sku), ProductFields.GetStringNameByEnum(ProductFields.Fields.Sku))); ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Name), ProductFields.GetStringNameByEnum(ProductFields.Fields.Name).Trim('*'))); ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.ParamSynonym), ProductFields.GetStringNameByEnum(ProductFields.Fields.ParamSynonym))); ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Category), ProductFields.GetStringNameByEnum(ProductFields.Fields.Category).Trim('*'))); ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Enabled), ProductFields.GetStringNameByEnum(ProductFields.Fields.Enabled).Trim('*'))); ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Price), ProductFields.GetStringNameByEnum(ProductFields.Fields.Price).Trim('*'))); ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.PurchasePrice), ProductFields.GetStringNameByEnum(ProductFields.Fields.PurchasePrice))); ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Amount), ProductFields.GetStringNameByEnum(ProductFields.Fields.Amount))); ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Unit), ProductFields.GetStringNameByEnum(ProductFields.Fields.Unit))); ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Discount), ProductFields.GetStringNameByEnum(ProductFields.Fields.Discount))); ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.ShippingPrice), ProductFields.GetStringNameByEnum(ProductFields.Fields.ShippingPrice))); ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Weight), ProductFields.GetStringNameByEnum(ProductFields.Fields.Weight))); ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Size), ProductFields.GetStringNameByEnum(ProductFields.Fields.Size))); ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.BriefDescription), ProductFields.GetStringNameByEnum(ProductFields.Fields.BriefDescription))); ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Description), ProductFields.GetStringNameByEnum(ProductFields.Fields.Description))); ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Title), ProductFields.GetStringNameByEnum(ProductFields.Fields.Title))); ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.MetaKeywords), ProductFields.GetStringNameByEnum(ProductFields.Fields.MetaKeywords))); ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.MetaDescription), ProductFields.GetStringNameByEnum(ProductFields.Fields.MetaDescription))); ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Photos), ProductFields.GetStringNameByEnum(ProductFields.Fields.Photos))); ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Markers), ProductFields.GetStringNameByEnum(ProductFields.Fields.Markers))); ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Properties), ProductFields.GetStringNameByEnum(ProductFields.Fields.Properties))); ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.Producer), ProductFields.GetStringNameByEnum(ProductFields.Fields.Producer))); ddl.Items.Add(new ListItem(ProductFields.GetDisplayNameByEnum(ProductFields.Fields.OrderByRequest), ProductFields.GetStringNameByEnum(ProductFields.Fields.OrderByRequest))); ddl.SelectedValue = lb.Text.Replace("*", "").Trim().ToLower(); cell.Controls.Add(ddl); ddlRow.Cells.Add(cell); } csv.Read(); if (csv.CurrentRecord != null) { for (int i = 0; i < csv.CurrentRecord.Length; i++) { //Added by Evgeni to remove dots //csv.CurrentRecord[0] = csv.CurrentRecord[0].Replace(".", "").Replace(" ", ""); var cell = new TableCell(); if (csv[i] == null) { cell.Controls.Add(new Label { Text = string.Empty }); } else { cell.Controls.Add(new Label { Text = csv[i].Length > 50 ? csv[i].Substring(0, 49).HtmlEncode() : csv[i].HtmlEncode() }); } firstValRow.Cells.Add(cell); } } } tbl.Rows.Add(namesRow); tbl.Rows.Add(firstValRow); tbl.Rows.Add(ddlRow); choseDiv.Controls.Add(tbl); }
//Added by Evgeni public static void SaveProductsToCsvAutospace(string path, Encodings.EncodingsEnum encodeType, Separators.SeparatorsEnum delimetr) { ImportStatistic.WriteLog("Start working on SaveProductsToCsvAutospace. DateTime: " + DateTime.Now); using (var writer = new CsvHelper.CsvWriter(new StreamWriter(path, false, Encodings.GetEncoding(encodeType)))) { ImportStatistic.WriteLog("Start working on SaveProductsToCsvAutospace 2.Encoding: " + Encodings.GetEncoding(encodeType) + " DateTime: " + DateTime.Now); writer.Configuration.Delimiter = Separators.GetCharSeparator(delimetr); //var items = new List<Product>(); //using (var db = new SQLDataAccess()) //{ // db.cnOpen(); // items = (List<Product>)ProductService.GetProductsWhere(db, "ArtNo like '2.607.001.733'"); // db.cnClose(); //} var items = ProductService.GetProducts();// .Where(t => t.ArtNo.Contains("2.607.001.733")).ToList(); List <string> loadedProducts = new List <string>(); ImportStatistic.WriteLog("Start working on SaveProductsToCsvAutospace 3. DateTime: " + DateTime.Now + "Number of products to proceed: " + items.Count); //select only relevants offers // items = items.Where(t => t.Brand.Name.ToLower() == "bosch" || t.Brand.Name.ToLower() == "skil").Where(t => t.Offers.FirstOrDefault().Amount > 0 && t.Offers.FirstOrDefault().Price > 0).ToList(); if (items == null) { return; } var regex = new Regex("^[0-9]+$"); string pcs = "шт."; for (int j = 0; j < items.Count; j++) { try { if (j % 1000 == 0) { ImportStatistic.WriteLog("Start working on SaveProductsToCsvAutospace 4. DateTime: " + DateTime.Now + " File No " + j); } //check that product was not inserted yet if (loadedProducts.Where(t => t.Contains(items[j].ArtNo)).ToList().Count > 0) { continue; } else { loadedProducts.Add(items[j].ArtNo); } var product = items[j]; var offer = product.Offers.Where(t => t.OfferListId == 15).FirstOrDefault() ?? new Offer { Amount = 0, Price = 0, SupplyPrice = 0, Unit = "", ShippingPrice = 0 }; //get only spec price if (offer.OfferListId != 15 || offer.Amount <= 0 || offer.Price <= 0) { continue; } if (product.Brand == null) { if (product.ArtNo.ToLower().StartsWith("f")) { writer.WriteField("Skil"); } else { writer.WriteField("Bosch"); } } else { writer.WriteField(product.Brand.Name); } writer.WriteField(product.ArtNo); writer.WriteField(product.BriefDescription.Replace(";", "").Replace(" ", " ").Replace(",", "").Replace("<p>", "").Replace("<p/>", "")); if (product.ProductCategories.Count == 0) { writer.WriteField("Прочее"); } else { writer.WriteField(product.ProductCategories.FirstOrDefault().Name.Replace(";", "").Replace(" ", "").Replace(",", "")); } writer.WriteField(offer.Amount); writer.WriteField(offer.Price.ToString("F2")); writer.WriteField(pcs); writer.WriteField("1"); writer.NextRecord(); } catch (Exception ex) { ImportStatistic.WriteLog("Error while adding product:" + ex.Message); } } ImportStatistic.WriteLog("End working on SaveProductsToCsvAutospace 5. DateTime: " + DateTime.Now); } ImportStatistic.WriteLog("End working on SaveProductsToCsvAutospace 6. DateTime: " + DateTime.Now); }
public static void SaveProductsToCsv(string path, Encodings.EncodingsEnum encodeType, Separators.SeparatorsEnum delimetr, List <ProductFields.Fields> fieldMapping) { using (var writer = new CsvHelper.CsvWriter(new StreamWriter(path, false, Encodings.GetEncoding(encodeType)))) { writer.Configuration.Delimiter = Separators.GetCharSeparator(delimetr); //var fields = new List<string> { "SKU", "Name*", "ParamSynonym", "Category", "Enabled*", "Price*", "PurchasePrice*", "Amount*", "Unit", "Discount", "ShippingPrice", "Weight", "Size", "BriefDescription", "Description" }; //foreach (var item in fields) // writer.WriteField(item); foreach (var item in fieldMapping) { writer.WriteField(ProductFields.GetStringNameByEnum(item)); } writer.NextRecord(); var items = ProductService.GetProducts(); if (items == null) { return; } var regex = new Regex("^[0-9]+$"); for (int j = 0; j < items.Count; j++) { var product = items[j]; if (!ExportStatistic.IsRun) { return; } var offer = product.Offers.FirstOrDefault() ?? new Offer { Amount = 0, Price = 0, SupplyPrice = 0, Unit = "", ShippingPrice = 0 }; var meta = MetaInfoService.GetMetaInfo(product.ID, MetaType.Product) ?? new MetaInfo(0, 0, MetaType.Product, string.Empty, string.Empty, string.Empty); for (int i = 0; i < fieldMapping.Count; i++) { var item = fieldMapping[i]; //Changed by Evgeni to insert dotes if (item == ProductFields.Fields.Sku) { string art = product.ArtNo; //if (art.Contains("-") == false && art.Length > 7) // { // art = art.Insert(7, ".").Insert(4, ".").Insert(1, "."); // } // writer.WriteField(art); // writer.WriteField(product.ArtNo); } // if (item == ProductFields.Fields.Name) { writer.WriteField(product.Name); } if (item == ProductFields.Fields.ParamSynonym) { if (regex.IsMatch(product.UrlPath)) { writer.WriteField("IPD" + product.UrlPath); } else { writer.WriteField(product.UrlPath); } } if (item == ProductFields.Fields.Category) { writer.WriteField((GetCategoryStringByProductID(product.ProductId))); } if (item == ProductFields.Fields.Enabled) { writer.WriteField(product.Enabled ? "+" : "-"); } if (item == ProductFields.Fields.Price) { writer.WriteField(offer.Price.ToString("F2")); } if (item == ProductFields.Fields.PurchasePrice) { writer.WriteField(offer.SupplyPrice.ToString("F2")); } if (item == ProductFields.Fields.Amount) { writer.WriteField(offer.Amount.ToString()); } if (item == ProductFields.Fields.Unit) { writer.WriteField(offer.Unit); } if (item == ProductFields.Fields.ShippingPrice) { writer.WriteField(offer.ShippingPrice.ToString("F2")); } if (item == ProductFields.Fields.Discount) { writer.WriteField(product.Discount.ToString("F2")); } if (item == ProductFields.Fields.Weight) { writer.WriteField(product.Weight.ToString("F2")); } if (item == ProductFields.Fields.Size) { writer.WriteField(product.Size.Replace("|", " x ")); } if (item == ProductFields.Fields.BriefDescription) { writer.WriteField(product.BriefDescription); } if (item == ProductFields.Fields.Description) { writer.WriteField(product.Description); } if (item == ProductFields.Fields.Title) { writer.WriteField(meta.Title); } if (item == ProductFields.Fields.MetaKeywords) { writer.WriteField(meta.MetaKeywords); } if (item == ProductFields.Fields.MetaDescription) { writer.WriteField(meta.MetaDescription); } if (item == ProductFields.Fields.Markers) { writer.WriteField(MarkersToString(product)); } if (item == ProductFields.Fields.Photos) { writer.WriteField(PhotoToString(product.ProductPhotos)); } if (item == ProductFields.Fields.Properties) { writer.WriteField(PropertiesToString(product.ProductPropertyValues)); } //Changed by Evgeni if (item == ProductFields.Fields.EAN) { writer.WriteField(product.EAN); } if (item == ProductFields.Fields.SubBrandId) { writer.WriteField(product.SubBrandId); } // if (item == ProductFields.Fields.Producer) { var brand = BrandService.GetBrandById(product.BrandId); writer.WriteField(brand != null ? brand.Name : string.Empty); } if (item == ProductFields.Fields.OrderByRequest) { writer.WriteField(product.OrderByRequest ? "+" : "-"); } } writer.NextRecord(); ExportStatistic.RowPosition++; } } }