using System; using System.Data; using System.Web; using System.Web.UI.WebControls; using CMS.CMSHelper; using CMS.Ecommerce; using CMS.GlobalHelper; using CMS.SettingsProvider; using CMS.DocumentEngine; using TreeNode = CMS.DocumentEngine.TreeNode; using CMS.SiteProvider; /// /// Summary description for Functions. /// public static class EcommerceFunctions { #region "Price - obsolete methods" /// /// Returns formatted price using current shopping cart data from CMS context. Customer discount level is not applied to the product price. /// /// SKU price [Obsolete("Use SKUInfoProvider.GetSKUFormattedPrice(SKUInfo, null, false, false) in your web project or GetSKUFormattedPrice(false, false) in transformation.")] public static string GetFormatedPrice(object SKUPrice) { return GetFormatedPrice(SKUPrice, 0, null, 0, true); } /// /// Returns formatted price using current shopping cart data from CMS context. Customer discount level is applied to the product price when this discount level is assigned to the specified product department. /// /// SKU price /// SKU department ID [Obsolete("Use SKUInfoProvider.GetSKUFormattedPrice(SKUInfo, null, true, false) in your web project or GetSKUFormattedPrice(true, false) in transformation.")] public static string GetFormatedPrice(object SKUPrice, object SKUDepartmentId) { return GetFormatedPrice(SKUPrice, SKUDepartmentId, null, 0, true); } /// /// Returns formatted price using specified shopping cart data. /// /// SKU price /// SKU department ID /// Shopping cart object with required data for price formatting, if it is NULL shopping cart data from CMS context are used [Obsolete("Use SKUInfoProvider.GetSKUFormattedPrice(SKUInfo, ShoppingCartInfo, true, false) in your web project or GetSKUFormattedPrice(true, false) in transformation.")] public static string GetFormatedPrice(object SKUPrice, object SKUDepartmentId, ShoppingCartInfo cart) { return GetFormatedPrice(SKUPrice, SKUDepartmentId, cart, 0, true); } /// /// Returns formatted price using specified shopping cart data. /// /// SKU price /// SKU department ID /// Product ID [Obsolete("Use SKUInfoProvider.GetSKUFormattedPrice(SKUInfo, null, true, true) in your web project or GetSKUFormattedPrice(true, true) in transformation.")] public static string GetFormatedPrice(object SKUPrice, object SKUDepartmentId, object SKUId) { return GetFormatedPrice(SKUPrice, SKUDepartmentId, null, SKUId, true); } /// /// Returns formatted price using specified shopping cart data. /// /// SKU price /// SKU department ID /// Shopping cart object with required data for price formatting, if it is NULL shopping cart data from CMS context are used /// Product ID [Obsolete("Use SKUInfoProvider.GetSKUFormattedPrice(SKUInfo, ShoppingCartInfo, true, true) in your web project or GetSKUFormattedPrice(true, true) in transformation.")] public static string GetFormatedPrice(object SKUPrice, object SKUDepartmentId, ShoppingCartInfo cart, object SKUId) { return GetFormatedPrice(SKUPrice, SKUDepartmentId, cart, SKUId, true); } /// /// Returns product price including all its taxes. /// /// SKU price /// SKU department ID /// Shopping cart with shopping data /// SKU ID [Obsolete("Use SKUInfoProvider.GetSKUPrice(SKUInfo, ShoppingCartInfo, true, true) instead.")] public static double GetPrice(object SKUPrice, object SKUDepartmentId, ShoppingCartInfo cart, object SKUId) { string price = GetFormatedPrice(SKUPrice, SKUDepartmentId, cart, SKUId, false); return ValidationHelper.GetDouble(price, 0); } /// /// Returns product price without its taxes. /// /// SKU price /// SKU department ID /// Shopping cart with shopping data [Obsolete("Use SKUInfoProvider.GetSKUPrice(SKUInfo, ShoppingCartInfo, true, false) instead.")] public static double GetPrice(object SKUPrice, object SKUDepartmentId, ShoppingCartInfo cart) { string price = GetFormatedPrice(SKUPrice, SKUDepartmentId, cart, 0, false); return ValidationHelper.GetDouble(price, 0); } /// /// Returns formatted price using specified shopping cart data. /// /// SKU price /// SKU department ID /// Shopping cart object with required data for price formatting, if it is NULL shopping cart data from CMS context are used /// Product ID /// Format output price /// True if price belongs to global product. [Obsolete("Use SKUInfoProvider.GetSKUFormattedPrice(SKUInfo, ShoppingCartInfo, true, true) in your web project or GetSKUFormattedPrice(true, true) in transformation.")] public static string GetFormatedPrice(object SKUPrice, object SKUDepartmentId, ShoppingCartInfo cart, object SKUId, bool FormatPrice) { double price = ValidationHelper.GetDouble(SKUPrice, 0); int departmentId = ValidationHelper.GetInteger(SKUDepartmentId, 0); int skuId = ValidationHelper.GetInteger(SKUId, 0); // When on the live site if (cart == null) { cart = ECommerceContext.CurrentShoppingCart; } if (departmentId > 0) { // Try site discount level DiscountLevelInfo discountLevel = cart.SiteDiscountLevel; bool valid = (discountLevel != null) && discountLevel.IsInDepartment(departmentId) && discountLevel.IsValid; if (!valid) { // Try global discount level discountLevel = cart.DiscountLevel; valid = (discountLevel != null) && discountLevel.IsInDepartment(departmentId) && discountLevel.IsValid; } // Apply discount to product price if (valid) { // Remember price before discount double oldPrice = price; // Get new price after discount price = price * (1 - discountLevel.DiscountLevelValue / 100); if ((oldPrice > 0) && (price < 0)) { price = 0; } } } int stateId = cart.StateID; int countryId = cart.CountryID; bool isTaxIDSupplied = ((cart.Customer != null) && (cart.Customer.CustomerTaxRegistrationID != "")); if ((skuId > 0) && ((stateId > 0) || (countryId > 0))) { // Apply taxes price += GetSKUTotalTax(price, skuId, stateId, countryId, isTaxIDSupplied); } // Apply exchange rate price = cart.ApplyExchangeRate(price); if (FormatPrice) { // Get formatted price return cart.GetFormattedPrice(price, true); } else { // Get not-formated price return price.ToString(); } } /// /// Returns product total tax in site main currency. /// /// SKU price /// SKU ID /// Customer billing address state ID /// Customer billing addres country ID /// Indicates if customer tax registration ID is supplied private static double GetSKUTotalTax(double skuPrice, int skuId, int stateId, int countryId, bool isTaxIDSupplied) { double totalTax = 0; int cacheMinutes = 0; // Try to get data from cache using (CachedSection cs = new CachedSection(ref totalTax, cacheMinutes, true, null, "skutotaltax|", skuId, skuPrice, stateId, countryId, isTaxIDSupplied)) { if (cs.LoadData) { // Get all the taxes and their values which are applied to the specified product DataSet ds = TaxClassInfoProvider.GetTaxes(skuId, countryId, stateId, null); if (!DataHelper.DataSourceIsEmpty(ds)) { foreach (DataRow dr in ds.Tables[0].Rows) { bool zeroTax = ValidationHelper.GetBoolean(dr["TaxClassZeroIfIDSupplied"], false); if (!(isTaxIDSupplied && zeroTax)) { double taxValue = ValidationHelper.GetDouble(dr["TaxValue"], 0); bool isFlat = ValidationHelper.GetBoolean(dr["TaxIsFlat"], false); // Add tax value totalTax += TaxClassInfoProvider.GetTaxValue(skuPrice, taxValue, isFlat); } } } // Cache the data cs.Data = totalTax; } } return totalTax; } #endregion #region "Images - obsolete methods" /// /// Returns complete HTML code of the specified product image, if not such image exists, default image is returned. /// /// Product image URL /// Image alternate text [Obsolete("In transformation, use tag with src parameter set to GetSKUImageUrl() method instead.")] public static string GetProductImage(object imageUrl, object alt) { return GetImage(imageUrl, 0, 0, 0, alt); } /// /// Returns complete HTML code of the specified resized product image, if not such image exists, default image is returned. /// /// Product image URL /// Height of image /// Image alternate text /// Width of image [Obsolete("In transformation, use tag with src parameter set to GetSKUImageUrl(width, height) method instead.")] public static string GetProductImage(object imageUrl, object width, object height, object alt) { return GetImage(imageUrl, width, height, 0, alt); } /// /// Returns complete HTML code of the specified resized product image, if such image does not exist, default image is returned. /// /// Product image URL /// Max side size /// Image alternate text [Obsolete("In transformation, use tag with src parameter set to GetSKUImageUrl(maxSideSize) method instead.")] public static string GetProductImage(object imageUrl, object maxSideSize, object alt) { return GetImage(imageUrl, 0, 0, maxSideSize, alt); } /// /// Returns complete HTML code of the specified resized product image, if not such image exists, default image is returned. /// /// Product image URL /// Width of image /// Height of image /// Image alternate text /// ID of the site the product belongs to [Obsolete("In transformation, use tag with src parameter set to GetSKUImageUrl(width, height) method instead.")] public static string GetProductImageForSite(object imageUrl, object width, object height, object alt, object siteId) { return GetImage(imageUrl, width, height, 0, alt, siteId); } /// /// Returns complete HTML code of the specified resized product image, if such image does not exist, default image is returned. /// /// Product image URL /// Max side size /// Image alternate text /// ID of the site the product belongs to [Obsolete("In transformation, use tag with src parameter set to GetSKUImageUrl(maxSideSize) method instead.")] public static string GetProductImageForSite(object imageUrl, object maxSideSize, object alt, object siteId) { return GetImage(imageUrl, 0, 0, maxSideSize, alt, siteId); } private static string GetImage(object imageUrl, object width, object height, object maxSideSize, object alt) { return GetImage(imageUrl, width, height, maxSideSize, alt, CMSContext.CurrentSiteID); } private static string GetImage(object imageUrl, object width, object height, object maxSideSize, object alt, object siteId) { // Get image tooltip string tooltip = ValidationHelper.GetString(alt, ""); if (tooltip != "") { tooltip = HTMLHelper.HTMLEncode(ResHelper.LocalizeString(tooltip)); tooltip = string.Format(" alt=\"{0}\" title=\"{1}\"", tooltip, tooltip); } // Get SKU image URL string url = GetSKUImageUrl(imageUrl, width, height, maxSideSize, siteId); if (!string.IsNullOrEmpty(url)) { return string.Format("", HTMLHelper.HTMLEncode(url), tooltip); } return ""; } #endregion #region "Links" /// /// Returns link to "add to shopping cart". /// /// Product ID /// Indicates whether product is enabled or not public static string GetAddToShoppingCartLink(object productId, object enabled) { return GetAddToShoppingCartLink(productId, enabled, null); } /// /// Returns link to "add to shopping cart". /// /// Product ID /// Indicates whether product is enabled or not /// Image URL public static string GetAddToShoppingCartLink(object productId, object enabled, string imageUrl) { if (ValidationHelper.GetBoolean(enabled, false) && (ValidationHelper.GetInteger(productId, 0) != 0)) { // Get default image URL imageUrl = imageUrl ?? "CMSModules/CMS_Ecommerce/addorder.png"; return "\"Add" + ResHelper.GetString("EcommerceFunctions.AddToShoppingCart") + ""; } else { return ""; } } /// /// Returns link to "add to shopping cart". /// /// Product ID public static string GetAddToShoppingCartLink(object productId) { return GetAddToShoppingCartLink(productId, true); } /// /// Returns link to add specified product to the user's wish list. /// /// Product ID public static string GetAddToWishListLink(object productId) { return GetAddToWishListLink(productId, null); } /// /// Returns link to add specified product to the user's wish list. /// /// Product ID /// Image URL public static string GetAddToWishListLink(object productId, string imageUrl) { if (ValidationHelper.GetInteger(productId, 0) != 0) { // Get default image URL imageUrl = imageUrl ?? "CMSModules/CMS_Ecommerce/addtowishlist.png"; return "\"Add" + ResHelper.GetString("EcommerceFunctions.AddToWishlist") + ""; } else { return ""; } } /// /// Returns link to remove specified product from the user's wish list. /// /// Product ID public static string GetRemoveFromWishListLink(object productId) { if ((productId != DBNull.Value) && (!CMSContext.CurrentUser.IsPublic())) { return "" + ResHelper.GetString("Wishlist.RemoveFromWishlist") + ""; } else { return ""; } } #endregion #region "URLs" /// /// Returns URL to the shopping cart. /// /// Site name public static string ShoppingCartURL(string siteName) { return URLHelper.ResolveUrl(ECommerceSettings.ShoppingCartURL(siteName)); } /// /// Returns URL to the wish list. /// /// Site name public static string WishlistURL(string siteName) { return URLHelper.ResolveUrl(ECommerceSettings.WishListURL(siteName)); } /// /// Returns product URL. /// /// SKU ID public static string GetProductUrl(object SKUID) { return URLHelper.ResolveUrl("~/CMSPages/GetProduct.aspx?productId=" + Convert.ToString(SKUID)); } /// /// Returns user friendly URL of the specified SKU and site name. /// /// SKU Guid /// SKU Name /// Site Name public static string GetProductUrl(object skuGuid, object skuName, object siteNameObj) { Guid guid = ValidationHelper.GetGuid(skuGuid, Guid.Empty); string name = Convert.ToString(skuName); string siteName = ValidationHelper.GetString(siteNameObj, null); return URLHelper.ResolveUrl(SKUInfoProvider.GetSKUUrl(guid, name, siteName)); } /// /// Returns user friendly URL of the specified SKU. /// /// SKU Guid /// SKU Name public static string GetProductUrl(object skuGuid, object skuName) { Guid guid = ValidationHelper.GetGuid(skuGuid, Guid.Empty); string name = Convert.ToString(skuName); return URLHelper.ResolveUrl(SKUInfoProvider.GetSKUUrl(guid, name)); } /// /// Returns SKU image URL including dimension's modifiers (width, height or maxsidesize) and site name parameter if product is from different site than current. If image URL is not specified, SKU default image URL is used. /// /// SKU image URL /// Image requested width, has no effect if maxsidesize is specified /// Image requested height, has no effect if maxsidesize is specified /// Image requested maximum side size /// SKU site ID. If empty, current site ID is used. public static string GetSKUImageUrl(object imageUrl, object width, object height, object maxsidesize, object siteId) { int iSiteId = ValidationHelper.GetInteger(siteId, 0); bool notCurrentSite = ((iSiteId > 0) && (iSiteId != CMSContext.CurrentSiteID)); string siteName = null; // Get site name if (notCurrentSite) { siteName = SiteInfoProvider.GetSiteName(iSiteId); } else { siteName = CMSContext.CurrentSiteName; } // Get product image URL string url = ValidationHelper.GetString(imageUrl, null); if (String.IsNullOrEmpty(url)) { // Get default product image URL url = ECommerceSettings.DefaultProductImageURL(siteName); } if (!String.IsNullOrEmpty(url)) { // Resolve URL url = URLHelper.ResolveUrl(url); int slashIndex = url.LastIndexOfCSafe('/'); if (slashIndex >= 0) { string urlStartPart = url.Substring(0, slashIndex); string urlEndPart = url.Substring(slashIndex); url = urlStartPart + HttpUtility.UrlPathEncode(urlEndPart); // Add site name if not current if (notCurrentSite) { url = URLHelper.AddParameterToUrl(url, "siteName", siteName); } // Add max side size int iMaxSideSize = ValidationHelper.GetInteger(maxsidesize, 0); if (iMaxSideSize > 0) { url = URLHelper.AddParameterToUrl(url, "maxsidesize", iMaxSideSize.ToString()); } else { // Add width int iWidth = ValidationHelper.GetInteger(width, 0); if (iWidth > 0) { url = URLHelper.AddParameterToUrl(url, "width", iWidth.ToString()); } // Add height int iHeight = ValidationHelper.GetInteger(height, 0); if (iHeight > 0) { url = URLHelper.AddParameterToUrl(url, "height", iHeight.ToString()); } } // Encode URL url = HTMLHelper.HTMLEncode(url); } } return url; } #endregion #region "SKU related objects' properties" /// /// Returns the public SKU status display name. /// /// Status ID [Obsolete("Use GetPublicStatusProperty(\"PublicStatusDisplayName\") in transformation instead.")] public static string GetPublicStatusName(object statusId) { int sid = ValidationHelper.GetInteger(statusId, 0); if (sid > 0) { PublicStatusInfo si = PublicStatusInfoProvider.GetPublicStatusInfo(sid); if (si != null) { return ResHelper.LocalizeString(si.PublicStatusDisplayName); } } return ""; } /// /// Gets object from the specified column of the manufacturer with specific ID. /// /// Manufacturer ID /// Column name public static object GetManufacturer(object Id, string column) { int id = ValidationHelper.GetInteger(Id, 0); if ((id > 0) && !DataHelper.IsEmpty(column)) { // Get manufacturer ManufacturerInfo mi = ManufacturerInfoProvider.GetManufacturerInfo(id); return GetColumnValue(mi, column); } return ""; } /// /// Gets object from the specified column of the department with specific ID. /// /// Department ID /// Column name public static object GetDepartment(object Id, string column) { int id = ValidationHelper.GetInteger(Id, 0); if (id > 0 && !DataHelper.IsEmpty(column)) { // Get department DepartmentInfo di = DepartmentInfoProvider.GetDepartmentInfo(id); return GetColumnValue(di, column); } return ""; } /// /// Gets object from the specified column of the supplier with specific ID. /// /// Supplier ID /// Column name public static object GetSupplier(object Id, string column) { int id = ValidationHelper.GetInteger(Id, 0); if ((id > 0) && !DataHelper.IsEmpty(column)) { // Get supplier SupplierInfo si = SupplierInfoProvider.GetSupplierInfo(id); return GetColumnValue(si, column); } return ""; } /// /// Gets object from the specified column of the internal status with specific ID. /// /// Internal status ID /// Column name public static object GetInternalStatus(object Id, string column) { int id = ValidationHelper.GetInteger(Id, 0); if ((id > 0) && !DataHelper.IsEmpty(column)) { // Get internal status InternalStatusInfo status = InternalStatusInfoProvider.GetInternalStatusInfo(id); return GetColumnValue(status, column); } return ""; } /// /// Gets object from the specified column of the public status with specific ID. /// /// Public status ID /// Column name public static object GetPublicStatus(object Id, string column) { int id = ValidationHelper.GetInteger(Id, 0); if ((id > 0) && !DataHelper.IsEmpty(column)) { // Get public status PublicStatusInfo status = PublicStatusInfoProvider.GetPublicStatusInfo(id); return GetColumnValue(status, column); } return ""; } /// /// Returns value of the given object column. /// /// Object data /// Column name private static object GetColumnValue(BaseInfo info, string columnName) { if ((info != null) && info.ContainsColumn(columnName)) { return info.GetValue(columnName); } return null; } /// /// Gets document name of specified node id. /// public static string GetDocumentName(object nodeIdent) { int nodeId = ValidationHelper.GetInteger(nodeIdent, 0); if (nodeId != 0) { TreeProvider tree = new TreeProvider(CMSContext.CurrentUser); TreeNode node = tree.SelectSingleNode(nodeId, CMSContext.PreferredCultureCode); if (node != null) { return node.GetDocumentName(); } } return String.Empty; } #endregion #region "SKU properties" /// /// Returns value of the specified product public status column. /// If the product is evaluated as a new product in the store, public status set by 'CMSStoreNewProductStatus' setting is used, otherwise product public status is used. /// /// SKU data /// Name of the product public status column the value should be retrieved from public static object GetSKUIndicatorProperty(SKUInfo sku, string column) { // Do not process if (sku == null) { return null; } PublicStatusInfo status = null; string siteName = SiteInfoProvider.GetSiteName(sku.SKUSiteID); string statusName = ECommerceSettings.NewProductStatus(siteName); if (!string.IsNullOrEmpty(statusName) && SKUInfoProvider.IsSKUNew(sku)) { // Get 'new product' status status = PublicStatusInfoProvider.GetPublicStatusInfo(statusName, siteName); } else { // Get product public status if (sku.SKUPublicStatusID > 0) { status = PublicStatusInfoProvider.GetPublicStatusInfo(sku.SKUPublicStatusID); } } // Get specified column value return GetColumnValue(status, column); } /// /// Returns amount of saved money based on the difference between product seller price and product retail price. /// /// Indicates if discounts should be applied to the seller price before the saved amount is calculated /// Indicates if taxes should be applied to both retail price and seller price before the saved amount is calculated /// Name of the column from which the seller price is retrieved, if empty SKUPrice column is used /// Name of the column from which the retail price is retrieved, if empty SKURetailPrice column is used /// True - result is percentage, False - result is in the current currency public static double GetSKUPriceSaving(SKUInfo sku, bool discounts, bool taxes, string column1, string column2, bool percentage) { // Do not process if (sku == null) { return 0; } // Ensure columns column1 = string.IsNullOrEmpty(column1) ? "SKUPrice" : column1; column2 = string.IsNullOrEmpty(column2) ? "SKURetailPrice" : column2; // Prices double price = SKUInfoProvider.GetSKUPrice(sku, null, discounts, taxes, false, column1); double retailPrice = SKUInfoProvider.GetSKUPrice(sku, null, false, taxes, false, column2); // If shopping cart is in context - apply exchange rates if (ECommerceContext.CurrentShoppingCart != null) { retailPrice = ECommerceContext.CurrentShoppingCart.ApplyExchangeRate(retailPrice); price = ECommerceContext.CurrentShoppingCart.ApplyExchangeRate(price); } // Round prices using current currency price = CurrencyInfoProvider.RoundTo(price, ECommerceContext.CurrentCurrency); retailPrice = CurrencyInfoProvider.RoundTo(retailPrice, ECommerceContext.CurrentCurrency); // Saved amount double savedAmount = retailPrice - price; // When seller price is greater than retail price if (((price > 0) && (savedAmount < 0)) || ((price < 0) && (savedAmount > 0))) { // Zero saved amount savedAmount = 0; } else if (percentage) { // Percentage saved amount savedAmount = ((retailPrice == 0) ? 0 : Math.Round(100 * savedAmount / retailPrice)); } return savedAmount; } #endregion #region "UI methods" /// /// Sets different css styles to enabled and disabled dropdownlist items. /// /// Dropdownlist control /// Field name with ID value /// Field name with status value /// Enabled item style /// Disabled item style public static void MarkEnabledAndDisabledItems(DropDownList drpTemp, string valueFieldName, string statusFieldName, string itemEnabledStyle, string itemDisabledStyle) { itemEnabledStyle = (itemEnabledStyle == "") ? "DropDownItemEnabled" : itemEnabledStyle; itemDisabledStyle = (itemDisabledStyle == "") ? "DropDownItemDisabled" : itemDisabledStyle; if (!DataHelper.DataSourceIsEmpty(drpTemp.DataSource)) { if (drpTemp.DataSource is DataSet) { ListItem li = null; foreach (DataRow row in ((DataSet)drpTemp.DataSource).Tables[0].Rows) { li = drpTemp.Items.FindByValue(Convert.ToString(row[valueFieldName])); if ((li != null) && (li.Value != "0")) { // Item is enabled if (ValidationHelper.GetBoolean(row[statusFieldName], false)) { li.Attributes.Add("class", itemEnabledStyle); } // Item is disabled else { li.Attributes.Add("class", itemDisabledStyle); } } } } } } /// /// Sets different css styles to enabled and disabled dropdownlist items. /// /// Dropdownlist control /// Field name with ID value /// Field name with status value public static void MarkEnabledAndDisabledItems(DropDownList drpTemp, string valueFieldName, string statusFieldName) { MarkEnabledAndDisabledItems(drpTemp, valueFieldName, statusFieldName, "", ""); } #endregion }