public static IList<ProductSpecificationModel> PrepareProductSpecificationModel(this Controller controller, IWorkContext workContext, ISpecificationAttributeService specificationAttributeService, ICacheManager cacheManager, Product product) { if (product == null) throw new ArgumentNullException("product"); string cacheKey = string.Format(ModelCacheEventConsumer.PRODUCT_SPECS_MODEL_KEY, product.Id, workContext.WorkingLanguage.Id); return cacheManager.Get(cacheKey, () => { var model = specificationAttributeService.GetProductSpecificationAttributesByProductId(product.Id, null, true) .Select(psa => { return new ProductSpecificationModel() { SpecificationAttributeId = psa.SpecificationAttributeOption.SpecificationAttributeId, SpecificationAttributeName = psa.SpecificationAttributeOption.SpecificationAttribute.GetLocalized(x => x.Name), SpecificationAttributeOption = !String.IsNullOrEmpty(psa.CustomValue) ? psa.CustomValue : psa.SpecificationAttributeOption.GetLocalized(x => x.Name), }; }).ToList(); return model; }); }
public static IList <ProductSpecificationModel> PrepareProductSpecificationModel(this Controller controller, IWorkContext workContext, ISpecificationAttributeService specificationAttributeService, ICacheManager cacheManager, Product product) { if (product == null) { throw new ArgumentNullException("product"); } string cacheKey = string.Format(ModelCacheEventConsumer.PRODUCT_SPECS_MODEL_KEY, product.Id, workContext.WorkingLanguage.Id); return(cacheManager.Get(cacheKey, () => { var model = specificationAttributeService.GetProductSpecificationAttributesByProductId(product.Id, null, true) .Select(psa => { return new ProductSpecificationModel() { SpecificationAttributeId = psa.SpecificationAttributeOption.SpecificationAttributeId, SpecificationAttributeName = psa.SpecificationAttributeOption.SpecificationAttribute.GetLocalized(x => x.Name), SpecificationAttributeOption = !String.IsNullOrEmpty(psa.CustomValue) ? psa.CustomValue : psa.SpecificationAttributeOption.GetLocalized(x => x.Name), }; }).ToList(); return model; })); }
public static IList <ProductSpecificationModel> PrepareProductSpecificationModel(this Controller controller, IWorkContext workContext, ISpecificationAttributeService specificationAttributeService, ICacheManager cacheManager, Product product) { if (product == null) { throw new ArgumentNullException("product"); } string cacheKey = string.Format(ModelCacheEventConsumer.PRODUCT_SPECS_MODEL_KEY, product.Id, workContext.WorkingLanguage.Id); return(cacheManager.Get(cacheKey, () => specificationAttributeService.GetProductSpecificationAttributesByProductId(product.Id, null, true) .Select(psa => { var m = new ProductSpecificationModel { SpecificationAttributeId = psa.SpecificationAttributeOption.SpecificationAttributeId, SpecificationAttributeName = psa.SpecificationAttributeOption.SpecificationAttribute.GetLocalized(x => x.Name), }; switch (psa.AttributeType) { case SpecificationAttributeType.Option: m.ValueRaw = HttpUtility.HtmlEncode(psa.SpecificationAttributeOption.GetLocalized(x => x.Name)); break; case SpecificationAttributeType.CustomText: m.ValueRaw = HttpUtility.HtmlEncode(psa.CustomValue); break; case SpecificationAttributeType.CustomHtmlText: m.ValueRaw = psa.CustomValue; break; case SpecificationAttributeType.Hyperlink: m.ValueRaw = string.Format("<a href='{0}' target='_blank'>{0}</a>", psa.CustomValue); break; default: break; } return m; }).ToList() )); }
public static IList<ProductSpecificationModel> PrepareProductSpecificationModel(this Controller controller, IWorkContext workContext, ISpecificationAttributeService specificationAttributeService, ICacheManager cacheManager, Product product) { if (product == null) throw new ArgumentNullException("product"); string cacheKey = string.Format(ModelCacheEventConsumer.PRODUCT_SPECS_MODEL_KEY, product.Id, workContext.WorkingLanguage.Id); return cacheManager.Get(cacheKey, () => specificationAttributeService.GetProductSpecificationAttributesByProductId(product.Id, null, true) .Select(psa => { var m = new ProductSpecificationModel { SpecificationAttributeId = psa.SpecificationAttributeOption.SpecificationAttributeId, SpecificationAttributeName = psa.SpecificationAttributeOption.SpecificationAttribute.GetLocalized(x => x.Name), }; switch (psa.AttributeType) { case SpecificationAttributeType.Option: m.ValueRaw = HttpUtility.HtmlEncode(psa.SpecificationAttributeOption.GetLocalized(x => x.Name)); break; case SpecificationAttributeType.CustomText: m.ValueRaw = HttpUtility.HtmlEncode(psa.CustomValue); break; case SpecificationAttributeType.CustomHtmlText: m.ValueRaw = psa.CustomValue; break; case SpecificationAttributeType.Hyperlink: m.ValueRaw = string.Format("<a href='{0}' target='_blank'>{0}</a>", psa.CustomValue); break; default: break; } return m; }).ToList() ); }
public void AddUpdateProductVariantToNebim(ProductVariant productVariant) { var nebimIntegrationProvider = LoadNebimIntegrationServiceBySystemName("Misc.Nebim"); IProductAttributeParser productAttributeParser = EngineContext.Current.Resolve <IProductAttributeParser>(); ITaxService taxService = EngineContext.Current.Resolve <ITaxService>(); IWorkContext workContext = EngineContext.Current.Resolve <IWorkContext>(); ISpecificationAttributeService specificationAttributeService = EngineContext.Current.Resolve <ISpecificationAttributeService>(); var pv = productVariant; var product = pv.Product; if (pv == null) { return; } try { string barcode = ""; string nameTr = "", nameEn = ""; int dimTypeCode = 0; string colorCode = ""; string colorNameTr = "", colorNameEn = ""; string dim1Code = "", dim1TR = "", dim1En = "", dim2Code = ""; string taxRate = string.Format("%{0}", taxService.GetTaxRate(pv.TaxCategoryId, workContext.CurrentCustomer)); Manufacturer manufacturer = null; string manufacturerName = "", manufacturerCode = ""; string attributeCode = "", attributeNameTR = "", attributeNameEN = "", attribute3Code = "", attribute3NameTR = "", attribute3NameEN = ""; decimal? USD = null, TL = null, EURO = null, CHF = null; string currencyCode = "TRY"; decimal price; decimal purchasePrice; IList <Tuple <string, string, string, string, string, string, string> > combinations = new List <Tuple <string, string, string, string, string, string, string> >(); IList <Tuple <byte, string, string, string> > attributes = new List <Tuple <byte, string, string, string> >(); nameTr = pv.GetLocalized(x => x.Name, 2); if (string.IsNullOrWhiteSpace(nameTr)) { nameTr = product.GetLocalized(x => x.Name, 2); } nameEn = pv.GetLocalized(x => x.Name, 1); if (string.IsNullOrWhiteSpace(nameEn)) { nameEn = product.GetLocalized(x => x.Name, 1); } #region hierarchy //var category = product.GetDefaultProductCategory(); var category = product.GetPublishDefaultProductCategory(); List <Category> categories = new List <Category>(); List <int> categoryIds = new List <int>(); while (category != null && //category is not null !category.Deleted && //category is not deleted category.Published) //category is published { categories.Add(category); category = _categoryService.GetCategoryById(category.ParentCategoryId); } categories.Reverse(); categoryIds = categories.Select(x => x.Id).ToList(); #region match hierarchy by categoryId and hierarchylevelCode matching input //List<int> hierarchyLevelCodes = new List<int>(); //var categoryHierarchyLevelMatchDictionary = new Dictionary<int, int>(); //try //{ // var categoryHierarchyLevelMatches = _NebimIntegrationSettings.Category_API_HierarchyLevelCodes.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); // foreach (var match in categoryHierarchyLevelMatches) // { // var ids = match.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries); // categoryHierarchyLevelMatchDictionary.Add(int.Parse(ids[0]), int.Parse(ids[1])); // } // foreach (var catId in categoryIds) // { // hierarchyLevelCodes.Add(categoryHierarchyLevelMatchDictionary[catId]); // } //} //catch (Exception ex) //{ // throw new Exception("categoryHierarchyLevelMatchDictionary matching error", ex); //} #endregion match hierarchy by categoryId and hierarchylevelCode matching input Dictionary <int, string> hierarchyLevelCodeNames = new Dictionary <int, string>(); for (int i = 0; i < categories.Count; i++) { var cat = categories[i]; hierarchyLevelCodeNames.Add(i + 1, cat.GetLocalized(x => x.Name, 2)); } //there are always 3 levels hierarchy for af if not add standard "none" category if (_NebimIntegrationSettings.Database == "V3_DaimaModa" || _NebimIntegrationSettings.Database == "V3_Test") { while (hierarchyLevelCodeNames.Count < 3) { hierarchyLevelCodeNames.Add(hierarchyLevelCodeNames.Count + 1, "NONE"); } } #endregion hierarchy #region attributes manufacturer = product.GetDefaultManufacturer(); if (manufacturer != null) { manufacturerName = manufacturer.Name; manufacturerCode = manufacturer.Id.ToString(); if (_NebimIntegrationSettings.API_AttributeTypeCodeForManufacturer > 0 && _NebimIntegrationSettings.API_AttributeTypeCodeForManufacturer < 21) { attributes.Add(Tuple.Create <byte, string, string, string>(_NebimIntegrationSettings.API_AttributeTypeCodeForManufacturer, manufacturer.Id.ToString(), manufacturer.Name, manufacturer.Name)); } } var psas = specificationAttributeService.GetProductSpecificationAttributesByProductId(pv.ProductId); //matching convention: "specificationId-attributeTypeCode" by comma separated. ex: 3-1,5-2,34-3 foreach (var match in _NebimIntegrationSettings.SpecificationAttribute_API_AttributeTypeCodes.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { try { var ids = match.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries); var specificationAttributeId = int.Parse(ids[0]); var attributeTypeCode = byte.Parse(ids[1]); var specification = psas.FirstOrDefault(x => x.SpecificationAttributeOption.SpecificationAttributeId == specificationAttributeId); if (specification != null) { attributeCode = specification.SpecificationAttributeOptionId.ToString(); attributeNameTR = specification.SpecificationAttributeOption.GetLocalized(x => x.Name, 2); attributeNameEN = specification.SpecificationAttributeOption.GetLocalized(x => x.Name, 1); attributes.Add(Tuple.Create <byte, string, string, string>(attributeTypeCode, attributeCode, attributeNameTR, attributeNameEN)); } } catch (Exception ex) { throw new Exception("Specification attribute-Nebim attribute matching error", ex); } } #endregion attributes if (pv.CurrencyId.HasValue) { currencyCode = pv.Currency.CurrencyCode; price = pv.CurrencyPrice.HasValue ? pv.CurrencyPrice.Value : pv.Price; switch (pv.CurrencyId.Value) { case 12: //tl TL = pv.Price; break; case 1: //$ USD = pv.CurrencyPrice; break; case 6: //Euro EURO = pv.CurrencyPrice; break; case 13: //CHF CHF = pv.CurrencyPrice; break; default: TL = pv.Price; break; } } else { TL = pv.Price; price = pv.Price; } //TODO: calculate purchase price, ?should or not include vat?? // public virtual decimal GetProductPrice(ProductVariant productVariant, int taxCategoryId, //decimal price, bool includingTax, Customer customer, //bool priceIncludesTax, out decimal taxRate) decimal taxR = 0; purchasePrice = _taxService.GetProductPrice(pv, pv.TaxCategoryId, price, false, _workContext.CurrentCustomer, true, out taxR); var pvas = pv.ProductVariantAttributes; var combs = pv.ProductVariantAttributeCombinations; // var pvaValues = productAttributeParser.ParseProductVariantAttributeValues(comb.AttributesXml); if (!pvas.Any()) { //dimTypeCode = 0; //colorCode = ""; //colorNameTr = ""; //colorNameEn = ""; //barcode = productVariant.Gtin; dimTypeCode = 2; colorCode = _NebimIntegrationSettings.API_StandardColorCode; colorNameTr = _NebimIntegrationSettings.API_StandardColorCode; colorNameEn = _NebimIntegrationSettings.API_StandardColorCode; dim1Code = _NebimIntegrationSettings.API_StandardSizeCode; dim1TR = _NebimIntegrationSettings.API_StandardSizeCode; dim1En = _NebimIntegrationSettings.API_StandardSizeCode; barcode = productVariant.Gtin; combinations.Add(Tuple.Create <string, string, string, string, string, string, string>(colorCode, colorNameTr, colorNameEn, dim1Code, dim1TR, dim1En, barcode)); } else if (!combs.Any()) { if (pvas.Any(x => x.ProductAttribute.Id == _NebimIntegrationSettings.ColorAttributeId))//only color attribute { //dimTypeCode = 1; //barcode = productVariant.Gtin; //var value = pvas.FirstOrDefault(x => x.ProductAttribute.Id == _NebimIntegrationSettings.ColorAttributeId).ProductVariantAttributeValues.FirstOrDefault(); //if (value != null) //{ // colorNameTr = value.GetLocalized(pvav => pvav.Name, 2); // colorNameEn = value.GetLocalized(pvav => pvav.Name, 1); // colorCode = value.ProductAttributeOptionId.ToString(); //} dimTypeCode = 2; barcode = productVariant.Gtin; var value = pvas.FirstOrDefault(x => x.ProductAttribute.Id == _NebimIntegrationSettings.ColorAttributeId).ProductVariantAttributeValues.FirstOrDefault(); if (value != null) { colorNameTr = value.GetLocalized(pvav => pvav.Name, 2); colorNameEn = value.GetLocalized(pvav => pvav.Name, 1); colorCode = value.ProductAttributeOptionId.ToString(); } dim1Code = _NebimIntegrationSettings.API_StandardSizeCode; dim1TR = _NebimIntegrationSettings.API_StandardSizeCode; dim1En = _NebimIntegrationSettings.API_StandardSizeCode; combinations.Add(Tuple.Create <string, string, string, string, string, string, string>(colorCode, colorNameTr, colorNameEn, dim1Code, dim1TR, dim1En, barcode)); } else // add default color so have +1 attributes. if dimension attribute exists and there is no combination, save the poduct eith std color and dimension { //colorCode = "STD"; //colorNameTr = ""; //colorNameEn = ""; //dimTypeCode = 2; //var nonColorAtributes = pvas.Where(x => x.ProductAttribute.Id != _NebimIntegrationSettings.ColorAttributeId).ToList(); //var first = nonColorAtributes.FirstOrDefault(); //if (first != null) //{ // var value = first.ProductVariantAttributeValues.FirstOrDefault(); // dim1Code = value == null ? "" : value.GetLocalized(x => x.Name, 2); // dim1TR = dim1Code; // dim1En = value == null ? "" : value.GetLocalized(x => x.Name, 1); //} dimTypeCode = 2; colorCode = _NebimIntegrationSettings.API_StandardColorCode; colorNameTr = _NebimIntegrationSettings.API_StandardColorCode; colorNameEn = _NebimIntegrationSettings.API_StandardColorCode; dim1Code = _NebimIntegrationSettings.API_StandardSizeCode; dim1TR = _NebimIntegrationSettings.API_StandardSizeCode; dim1En = _NebimIntegrationSettings.API_StandardSizeCode; barcode = productVariant.Gtin; combinations.Add(Tuple.Create <string, string, string, string, string, string, string>(colorCode, colorNameTr, colorNameEn, dim1Code, dim1TR, dim1En, barcode)); } } if (combs.Any())//has combinations { //add rows for each combination foreach (var comb in combs) { var pvaValues = productAttributeParser.ParseProductVariantAttributeValues(comb.AttributesXml); var pvavColor = pvaValues.FirstOrDefault(x => x.ProductVariantAttribute.ProductAttribute.Id == _NebimIntegrationSettings.ColorAttributeId); //for af there are 2 dim attribute, for bisse and hatemoglu there is 1. var pvavDims = pvaValues.Where(x => (x.ProductVariantAttribute.ProductAttribute.Id == _NebimIntegrationSettings.Dim1AttributeId) || (x.ProductVariantAttribute.ProductAttribute.Id == _NebimIntegrationSettings.Dim2AttributeId)).ToList(); barcode = comb.ProductVariantBarcode; if (pvavColor != null && pvavDims.Count == 0) { //dimTypeCode = 1; //colorNameTr = pvavColor.GetLocalized(pvav => pvav.Name, 2); //colorNameEn = pvavColor.GetLocalized(pvav => pvav.Name, 1); //colorCode = pvavColor.ProductAttributeOptionId.ToString(); dimTypeCode = 2; if (pvavColor != null) { colorNameTr = pvavColor.GetLocalized(pvav => pvav.Name, 2); colorNameEn = pvavColor.GetLocalized(pvav => pvav.Name, 1); colorCode = pvavColor.ProductAttributeOptionId.ToString(); } dim1Code = _NebimIntegrationSettings.API_StandardSizeCode; dim1TR = _NebimIntegrationSettings.API_StandardSizeCode; dim1En = _NebimIntegrationSettings.API_StandardSizeCode; } else if (pvavColor != null && pvavDims.Count > 0) { var dim1 = pvavDims.First(); dimTypeCode = 2; colorNameTr = pvavColor.GetLocalized(pvav => pvav.Name, 2); colorNameEn = pvavColor.GetLocalized(pvav => pvav.Name, 1); colorCode = pvavColor.ProductAttributeOptionId.ToString(); dim1Code = dim1.GetLocalized(x => x.Name, 2); dim1TR = dim1Code; dim1En = dim1.GetLocalized(x => x.Name, 1); } else//no color attribute { dimTypeCode = 2; colorNameTr = _NebimIntegrationSettings.API_StandardColorCode; colorNameEn = _NebimIntegrationSettings.API_StandardColorCode; colorCode = _NebimIntegrationSettings.API_StandardColorCode; var dim1 = pvavDims.FirstOrDefault(); dim1Code = dim1 == null ? "" : dim1.GetLocalized(x => x.Name, 2); dim1TR = dim1Code; dim1En = dim1 == null ? "" : dim1.GetLocalized(x => x.Name, 1); } //TODO: store and get barcode from combination! combinations.Add(Tuple.Create <string, string, string, string, string, string, string>(colorCode, colorNameTr, colorNameEn, dim1Code, dim1TR, dim1En, barcode)); } } //GetProductCode(pv) // nebimIntegrationProvider.AddUpdateProductToNebim(pv.Sku, nameTr, nameEn, dimTypeCode, combinations, "AD", "TR", taxRate, taxRate, taxRate, hierarchyLevelCodes.ToArray(), attributes, price, currencyCode); nebimIntegrationProvider.AddUpdateProductToNebim(barcode, GetProductCode(pv), nameTr, nameEn, dimTypeCode, combinations, "AD", "TR", taxRate, taxRate, taxRate, hierarchyLevelCodeNames, attributes, price, purchasePrice, 0, currencyCode); } catch (Exception ex) { throw new Exception("NebimExport: productid='" + product.Id + "' variantId:'" + pv.Id + "'. Inner Message:" + ex.Message, ex); } }