Пример #1
0
    /// <summary>
    /// Submit Button Click Event
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        #region Declarations
        SKUAdmin skuAdminAccess = new SKUAdmin();
        ProductAdmin productAdmin = new ProductAdmin();
        SKUAttribute skuAttribute = new SKUAttribute();
        SKU sku = new SKU();
        ProductCategory productCategory = new ProductCategory();
        ProductCategoryAdmin  productCategoryAdmin=new ProductCategoryAdmin();
        Product product = new Product();
        System.IO.FileInfo fileInfo=null;
        bool retVal = false;

        //check if category was selected
        if (CategoryTreeView.CheckedNodes.Count > 0)
        {
            lblCategoryError.Visible = false;
        }
        else
        {
            lblCategoryError.Visible = true;
            return;
        }

        #endregion

        #region Set Product Properties

        //passing Values
        product.ProductID = ItemID;
        product.PortalID = ZNodeConfigManager.SiteConfig.PortalID;

        //if edit mode then get all the values first
        if (ItemID > 0)
        {
            product = productAdmin.GetByProductId(ItemID);

            if (ViewState["productSkuId"] != null)
            {
                sku.SKUID = int.Parse(ViewState["productSkuId"].ToString());
            }
        }

        //General Info
        product.Name = txtProductName.Text;
        product.ImageFile = txtimagename.Text;
        product.ProductNum = txtProductNum.Text;
        product.PortalID = ZNodeConfigManager.SiteConfig.PortalID;

        if (ProductTypeList.SelectedIndex != -1)
        {
            product.ProductTypeID = Convert.ToInt32(ProductTypeList.SelectedValue);
        }
        else
        {
            //"Please add a product type before you add a new product";
        }
        //MANUFACTURER
        if (ManufacturerList.SelectedIndex != -1)
        {
            if (ManufacturerList.SelectedItem.Text.Equals("No Manufacturer Selected"))
            {
                product.ManufacturerID = null;
            }
            else
            {
                product.ManufacturerID = Convert.ToInt32(ManufacturerList.SelectedValue);
            }
        }

        //Supplier
        if (ddlSupplier.SelectedIndex != -1)
        {
            if (ddlSupplier.SelectedItem.Text.Equals("None"))
            {
                product.SupplierID = null;
            }
            else
            {
                product.SupplierID = Convert.ToInt32(ddlSupplier.SelectedValue);
            }
        }

        product.DownloadLink = txtDownloadLink.Text.Trim();
        product.ShortDescription = txtshortdescription.Text;
        product.Description = ctrlHtmlText.Html;
        product.RetailPrice = Convert.ToDecimal(txtproductRetailPrice.Text);

        if (txtproductSalePrice.Text.Trim().Length > 0)
        {
            product.SalePrice = Convert.ToDecimal(txtproductSalePrice.Text.Trim());
        }
        else { product.SalePrice = null; }

        if (txtProductWholeSalePrice.Text.Trim().Length > 0)
        {
            product.WholesalePrice = Convert.ToDecimal(txtProductWholeSalePrice.Text.Trim());
        }
        else { product.WholesalePrice = null; }

        //Quantity Available
        product.QuantityOnHand = Convert.ToInt32(txtProductQuantity.Text);
        if (txtReOrder.Text.Trim().Length > 0)
        {
            product.ReorderLevel = Convert.ToInt32(txtReOrder.Text);
        }
        else
        {
            product.ReorderLevel = null;
        }
        if(txtMaxQuantity.Text.Equals(""))
        {
            product.MaxQty = 10;
        }
        else
        {
            product.MaxQty = Convert.ToInt32(txtMaxQuantity.Text);
        }

        // Display Settings
        product.MasterPage = ddlPageTemplateList.SelectedItem.Text;
        product.DisplayOrder = int.Parse(txtDisplayOrder.Text.Trim());

        // Tax Settings
        if(ddlTaxClass.SelectedIndex != -1)
            product.TaxClassID = int.Parse(ddlTaxClass.SelectedValue);

        //Shipping Option setting
        product.ShippingRuleTypeID = Convert.ToInt32(ShippingTypeList.SelectedValue);
        product.FreeShippingInd = chkFreeShippingInd.Checked;
        product.ShipSeparately = chkShipSeparately.Checked;

        if (txtProductWeight.Text.Trim().Length > 0)
        {
            product.Weight = Convert.ToDecimal(txtProductWeight.Text.Trim());
        }
        else { product.Weight = null; }

        //Product Height - Which will be used to determine the shipping cost
        if (txtProductHeight.Text.Trim().Length > 0)
        {
            product.Height = decimal.Parse(txtProductHeight.Text.Trim());
        }
        else { product.Height = null; }

        if (txtProductWidth.Text.Trim().Length > 0)
        {
            product.Width = decimal.Parse(txtProductWidth.Text.Trim());
        }
        else { product.Width = null; }

        if (txtProductLength.Text.Trim().Length > 0)
        {
            product.Length = decimal.Parse(txtProductLength.Text.Trim());
        }
        else { product.Length = null; }

        //Stock
        DataSet MyAttributeTypeDataSet = productAdmin.GetAttributeTypeByProductTypeID(int.Parse(ProductTypeList.SelectedValue));
        if (MyAttributeTypeDataSet.Tables[0].Rows.Count == 0)
        {
            product.SKU = txtProductSKU.Text.Trim();
            product.QuantityOnHand = Convert.ToInt32(txtProductQuantity.Text);
        }
        else
        {
            //SKU
            sku.ProductID = ItemID;
            sku.QuantityOnHand = Convert.ToInt32(txtProductQuantity.Text);
            sku.SKU = txtProductSKU.Text.Trim();
            sku.ActiveInd = true;
            product.SKU = txtProductSKU.Text.Trim();
            product.QuantityOnHand = 0; //Reset quantity available in the Product table,If SKU is selected
        }

        product.ImageAltTag = txtImageAltTag.Text.Trim();
        #endregion

        #region Image Validation

        // Validate image
        if ((ItemID == 0 || RadioProductNewImage.Checked == true) && UploadProductImage.PostedFile.FileName.Length > 0)
        {
            //Check for Product Image
            fileInfo = new System.IO.FileInfo(UploadProductImage.PostedFile.FileName);

            if (fileInfo != null)
            {
              product.ImageFile = fileInfo.Name;
              sku.SKUPicturePath = fileInfo.Name;
            }
        }
        #endregion

        #region Database & Image Updates

        //set update date
        product.UpdateDte = System.DateTime.Now;

        //create transaction
        TransactionManager tranManager = ConnectionScope.CreateTransaction();

        try
        {
            if (ItemID > 0) //PRODUCT UPDATE
            {
                //Update product Sku and Product values
                if (MyAttributeTypeDataSet.Tables[0].Rows.Count > 0) //If ProductType has SKU's
                {
                    if (sku.SKUID > 0) //For this product already SKU if on exists
                    {
                        sku.UpdateDte = System.DateTime.Now;

                        // Check whether Duplicate attributes is created
                        string Attributes = String.Empty;

                        DataSet MyAttributeTypeDataSet1 = productAdmin.GetAttributeTypeByProductTypeID(ProductTypeId);

                        foreach (DataRow MyDataRow in MyAttributeTypeDataSet1.Tables[0].Rows)
                        {
                            System.Web.UI.WebControls.DropDownList lstControl = (System.Web.UI.WebControls.DropDownList)ControlPlaceHolder.FindControl("lstAttribute" + MyDataRow["AttributeTypeId"].ToString());

                            int selValue = int.Parse(lstControl.SelectedValue);

                            Attributes += selValue.ToString() + ",";
                        }

                        // Split the string
                        string Attribute = Attributes.Substring(0, Attributes.Length - 1);

                        // To check SKU combination is already exists.
                        bool RetValue = skuAdminAccess.CheckSKUAttributes(ItemID, sku.SKUID, Attribute);

                        if (!RetValue)
                        {
                            //then Update the database with new property values
                            retVal = productAdmin.Update(product, sku);
                        }
                        else
                        {
                            //Throw error if duplicate attribute
                            lblMsg.Text = "This Attribute combination already exists for this product. Please select different combination.";
                            return;
                        }
                    }
                    else
                    {
                        retVal = productAdmin.Update(product);
                        //If Product doesn't have any SKUs yet,then create new SKU in the database
                        skuAdminAccess.Add(sku);
                    }
                }
                else
                {
                    retVal = productAdmin.Update(product);
                    // If User selectes Default product type for this product,
                    // then Remove all the existing SKUs for this product
                    skuAdminAccess.DeleteByProductId(ItemID);
                }

                if (!retVal) { throw (new ApplicationException()); }

                // Delete existing categories
                productAdmin.DeleteProductCategories(ItemID);

                // Add Product Categories
                foreach (TreeNode Node in CategoryTreeView.CheckedNodes)
                {
                    ProductCategory prodCategory = new ProductCategory();
                    ProductAdmin prodAdmin = new ProductAdmin();

                    prodCategory.CategoryID = int.Parse(Node.Value);
                    prodCategory.ProductID = ItemID;
                    prodAdmin.AddProductCategory(prodCategory);
                }

                // Delete existing SKUAttributes
                skuAdminAccess.DeleteBySKUId(sku.SKUID);

                // Add SKU Attributes
                foreach (DataRow MyDataRow in MyAttributeTypeDataSet.Tables[0].Rows)
                {
                    System.Web.UI.WebControls.DropDownList lstControl = (System.Web.UI.WebControls.DropDownList)ControlPlaceHolder.FindControl("lstAttribute" + MyDataRow["AttributeTypeId"].ToString());

                    int selValue = int.Parse(lstControl.SelectedValue);

                    if (selValue > 0)
                    {
                        skuAttribute.AttributeId = selValue;
                    }

                    skuAttribute.SKUID = sku.SKUID;

                    skuAdminAccess.AddSKUAttribute(skuAttribute);

                }

            }
            else // PRODUCT ADD
            {
                product.ActiveInd = true;

                // Add Product/SKU
                if (MyAttributeTypeDataSet.Tables[0].Rows.Count > 0)
                {
                    //if ProductType has SKUs, then insert sku with Product
                    retVal = productAdmin.Add(product, sku, out _ProductID, out SKUId);
                }
                else
                {
                    retVal = productAdmin.Add(product, out _ProductID); //if ProductType is Default
                }

                if (!retVal) { throw (new ApplicationException()); }

                // Add Category List for the Product
                foreach (TreeNode Node in CategoryTreeView.CheckedNodes)
                {
                    ProductCategory prodCategory = new ProductCategory();
                    ProductAdmin prodAdmin = new ProductAdmin();

                    prodCategory.CategoryID = int.Parse(Node.Value);
                    prodCategory.ProductID = _ProductID;
                    prodAdmin.AddProductCategory(prodCategory);
                }

                // Add SKU Attributes
                foreach (DataRow MyDataRow in MyAttributeTypeDataSet.Tables[0].Rows)
                {
                    System.Web.UI.WebControls.DropDownList lstControl = (System.Web.UI.WebControls.DropDownList)ControlPlaceHolder.FindControl("lstAttribute" + MyDataRow["AttributeTypeId"].ToString());

                    int selValue = int.Parse(lstControl.SelectedValue);

                    if (selValue > 0)
                    {
                        skuAttribute.AttributeId = selValue;
                    }

                    skuAttribute.SKUID = SKUId;

                    skuAdminAccess.AddSKUAttribute(skuAttribute);
                }

                ZNode.Libraries.Admin.ProductViewAdmin imageAdmin = new ProductViewAdmin();
                ZNode.Libraries.DataAccess.Entities.ProductImage productImage = new ProductImage();

                productImage.Name = txtimagename.Text;
                productImage.ActiveInd = false;
                productImage.ShowOnCategoryPage = false;
                productImage.ProductID = _ProductID;
                productImage.ProductImageTypeID = 1;
                productImage.DisplayOrder = 500;
                productImage.ImageAltTag = txtImageAltTag.Text.Trim();
                productImage.AlternateThumbnailImageFile = txtImageAltTag.Text.Trim();

                if (fileInfo != null)
                {
                    productImage.ImageFile = fileInfo.Name;
                }

                imageAdmin.Insert(productImage);
            }

            // Commit transaction
            tranManager.Commit();
        }
        catch // error occurred so rollback transaction
        {
            if (tranManager.IsOpen)
                tranManager.Rollback();

            lblMsg.Text = "Unable to update product. Please try again.";
            return;
        }

        // Upload File if this is a new product or the New Image option was selected for an existing product
        if (RadioProductNewImage.Checked || ItemID == 0)
        {
            if (fileInfo != null)
            {
                UploadProductImage.SaveAs(Server.MapPath(ZNodeConfigManager.EnvironmentConfig.OriginalImagePath + fileInfo.Name));

                ZNodeImage.ResizeImage(fileInfo, ZNode.Libraries.Framework.Business.ZNodeConfigManager.SiteConfig.MaxCatalogItemLargeWidth, Server.MapPath(ZNodeConfigManager.EnvironmentConfig.LargeImagePath));
                ZNodeImage.ResizeImage(fileInfo, ZNode.Libraries.Framework.Business.ZNodeConfigManager.SiteConfig.MaxCatalogItemThumbnailWidth, Server.MapPath(ZNodeConfigManager.EnvironmentConfig.ThumbnailImagePath));
                ZNodeImage.ResizeImage(fileInfo, ZNode.Libraries.Framework.Business.ZNodeConfigManager.SiteConfig.MaxCatalogItemMediumWidth, Server.MapPath(ZNodeConfigManager.EnvironmentConfig.MediumImagePath));
                ZNodeImage.ResizeImage(fileInfo, ZNode.Libraries.Framework.Business.ZNodeConfigManager.SiteConfig.MaxCatalogItemSmallWidth, Server.MapPath(ZNodeConfigManager.EnvironmentConfig.SmallImagePath));
                ZNodeImage.ResizeImage(fileInfo, ZNode.Libraries.Framework.Business.ZNodeConfigManager.SiteConfig.MaxCatalogItemSwatchWidth, Server.MapPath(ZNodeConfigManager.EnvironmentConfig.SwatchImagePath));
                ZNodeImage.ResizeImage(fileInfo, ZNode.Libraries.Framework.Business.ZNodeConfigManager.SiteConfig.MaxCatalogItemCrossSellWidth, Server.MapPath(ZNodeConfigManager.EnvironmentConfig.CrossSellImagePath));
            }
        }

        #endregion

        #region Redirect to next page
        //Redirect to next page
        if (ItemID > 0)
        {
            string ViewLink = "~/admin/secure/catalog/product/view.aspx?itemid=" + ItemID.ToString();
            Response.Redirect(ViewLink);
        }
        else
        {
            string NextLink = "~/admin/secure/catalog/product/view.aspx?itemid=" + _ProductID.ToString();
            Response.Redirect(NextLink);
        }
        #endregion
    }