Пример #1
0
        private void _updateProductVariation(tbl_Account acc)
        {
            if (String.IsNullOrEmpty(hdfVariableListInsert.Value))
            {
                var strFunction = "";
                strFunction = "HoldOn.close();";
                strFunction = "swal({title: 'Lỗi', text: 'Quá trình cập nhật biến thể đã xảy ra lỗi', type: 'error'});";

                JavaScript.BeforePageLoad(Page).ExecuteCustomScript(strFunction);
                return;
            }

            var productID    = ViewState["ID"].ToString().ToInt(0);
            var productSKU   = ViewState["SKU"].ToString();
            var uploadPath   = "/uploads/images/";
            var imageDefault = "/App_Themes/Ann/image/placeholder.png";

            #region Xóa biến thể
            var variationRemovalList = JsonConvert.DeserializeObject <List <string> >(hdfVariationRemovalList.Value);

            foreach (var sku in variationRemovalList)
            {
                ProductVariableController.removeByVariationSKU(sku);
            }
            #endregion

            #region Cập nhật biến thể
            var productVariationUpdateList = JsonConvert.DeserializeObject <List <ProductVariationUpdateModel> >(hdfVariableListInsert.Value);

            if (uploadVariationImage.HasFiles)
            {
                foreach (var file in uploadVariationImage.PostedFiles)
                {
                    var fileName  = file.FileName;
                    var imageName = _uploadImage(productID, file);

                    productVariationUpdateList
                    .Where(x => x.image == uploadPath + fileName)
                    .Select(x =>
                    {
                        x.image = uploadPath + imageName;

                        return(x);
                    });
                }
            }

            foreach (var item in productVariationUpdateList)
            {
                var now = DateTime.Now;
                // Kiểm tra sản phẩm biến thể có tồn tại hay không để thực hiện insert hoặc update
                var productVariation = ProductVariableController.GetByID(item.productVariationID);
                var image            = String.Empty;

                if (item.image != imageDefault)
                {
                    image = item.image.Replace(uploadPath, String.Empty);
                }

                if (productVariation != null)
                {
                    // Thực hiện update biến thể
                    var productVariationID = ProductVariableController.Update(
                        ID: productVariation.ID,
                        ProductID: productID,
                        ParentSKU: productSKU,
                        SKU: item.sku,
                        Stock: Convert.ToDouble(productVariation.Stock),
                        StockStatus: Convert.ToInt32(productVariation.StockStatus),
                        Regular_Price: item.regularPrice,
                        CostOfGood: item.costOfGood,
                        RetailPrice: item.retailPrice,
                        Image: image,
                        ManageStock: true,
                        IsHidden: false,
                        ModifiedDate: now,
                        ModifiedBy: acc.Username,
                        SupplierID: Convert.ToInt32(productVariation.SupplierID),
                        SupplierName: productVariation.SupplierName,
                        MinimumInventoryLevel: item.minimumInventoryLevel,
                        MaximumInventoryLevel: item.maximumInventoryLevel
                        );

                    // Xóa tất cả giá trị của biến thể để cập nhật lại
                    ProductVariableValueController.DeleteByProductVariableID(productVariation.ID);

                    // Khởi tạo biến thể cho sản phẩm con
                    _createVariationValue(new VariationValueUpdateModel()
                    {
                        productVariationID  = Convert.ToInt32(productVariationID),
                        productVariationSKU = item.sku,
                        variationValueID    = item.variationValueID,
                        variationName       = item.variationName,
                        variationValueName  = item.variationValueName,
                        isHidden            = false,
                        createdDate         = now,
                        createdBy           = acc.Username
                    });
                }
                else
                {
                    // Tạo sản phẩm biến thể mới
                    var productVariationID = ProductVariableController.Insert(
                        ProductID: productID,
                        ParentSKU: productSKU,
                        SKU: item.sku,
                        Stock: 0,
                        StockStatus: item.stockStatus,
                        Regular_Price: item.regularPrice,
                        CostOfGood: item.costOfGood,
                        RetailPrice: item.retailPrice,
                        Image: image,
                        ManageStock: true,
                        IsHidden: false,
                        CreatedDate: now,
                        CreatedBy: acc.Username,
                        SupplierID: ddlSupplier.SelectedValue.ToInt(0),
                        SupplierName: ddlSupplier.SelectedItem.ToString(),
                        MinimumInventoryLevel: item.minimumInventoryLevel,
                        MaximumInventoryLevel: item.maximumInventoryLevel);

                    // Khởi tạo biến thể cho sản phẩm con
                    _createVariationValue(new VariationValueUpdateModel()
                    {
                        productVariationID  = Convert.ToInt32(productVariationID),
                        productVariationSKU = item.sku,
                        variationValueID    = item.variationValueID,
                        variationName       = item.variationName,
                        variationValueName  = item.variationValueName,
                        isHidden            = false,
                        createdDate         = now,
                        createdBy           = acc.Username
                    });
                }
            }
            #endregion
        }
Пример #2
0
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string username  = Request.Cookies["userLoginSystem"].Value;
            var    acc       = AccountController.GetByUsername(username);
            int    cateID    = ViewState["cateID"].ToString().ToInt(0);
            int    ProductID = ViewState["ID"].ToString().ToInt(0);

            if (cateID > 0)
            {
                string ProductTitle   = txtProductTitle.Text;
                string ProductContent = pContent.Content;
                string ProductSKU     = ViewState["SKU"].ToString();
                double ProductStock   = 0;
                int    StockStatus    = 0;
                bool   ManageStock    = true;
                double Regular_Price  = Convert.ToDouble(pRegular_Price.Text);
                double CostOfGood     = Convert.ToDouble(pCostOfGood.Text);
                double Retail_Price   = Convert.ToDouble(pRetailPrice.Text);
                bool   IsHidden       = chkIsHidden.Checked;
                int    CategoryID     = hdfParentID.Value.ToInt();

                double MinimumInventoryLevel = 0;
                if (pMinimumInventoryLevel.Text != "")
                {
                    MinimumInventoryLevel = Convert.ToDouble(pMinimumInventoryLevel.Text);
                }

                double MaximumInventoryLevel = 0;
                if (pMaximumInventoryLevel.Text != "")
                {
                    MaximumInventoryLevel = Convert.ToDouble(pMaximumInventoryLevel.Text);
                }

                //Phần thêm ảnh đại diện sản phẩm
                string path         = "/uploads/images/";
                string ProductImage = ListProductThumbnail.Value;
                if (ProductThumbnailImage.UploadedFiles.Count > 0)
                {
                    foreach (UploadedFile f in ProductThumbnailImage.UploadedFiles)
                    {
                        var o = path + ProductID + '-' + convertToSlug(Path.GetFileName(f.FileName));
                        try
                        {
                            f.SaveAs(Server.MapPath(o));
                            ProductImage = o;
                        }
                        catch { }
                    }
                }

                if (ProductImage != ListProductThumbnail.Value)
                {
                    if (File.Exists(Server.MapPath(ListProductThumbnail.Value)))
                    {
                        File.Delete(Server.MapPath(ListProductThumbnail.Value));
                    }
                }

                // Delete Image Gallery

                string deleteImageGallery = hdfDeleteImageGallery.Value;

                if (deleteImageGallery != "")
                {
                    string[] deletelist = deleteImageGallery.Split(',');

                    for (int i = 0; i < deletelist.Length - 1; i++)
                    {
                        var img = ProductImageController.GetByID(Convert.ToInt32(deletelist[i]));
                        if (img != null)
                        {
                            var product = ProductController.GetByID(ProductID);

                            // Delete image
                            if (!string.IsNullOrEmpty(img.ProductImage) && img.ProductImage != product.ProductImage)
                            {
                                string fileImage = Server.MapPath(img.ProductImage);
                                File.Delete(fileImage);
                            }
                            string delete = ProductImageController.Delete(img.ID);
                        }
                    }
                }

                // Update product

                string kq = ProductController.Update(ProductID, CategoryID, 0, ProductTitle, ProductContent, ProductSKU, ProductStock,
                                                     StockStatus, ManageStock, Regular_Price, CostOfGood, Retail_Price, ProductImage, 0,
                                                     IsHidden, DateTime.Now, username, ddlSupplier.SelectedValue.ToInt(0), ddlSupplier.SelectedItem.ToString(),
                                                     txtMaterials.Text, MinimumInventoryLevel, MaximumInventoryLevel);

                // Upload image gallery

                if (UploadImages.HasFiles)
                {
                    foreach (HttpPostedFile uploadedFile in UploadImages.PostedFiles)
                    {
                        var o = path + ProductID + '-' + convertToSlug(Path.GetFileName(uploadedFile.FileName));
                        uploadedFile.SaveAs(Server.MapPath(o));
                        ProductImageController.Insert(ProductID, o, false, DateTime.Now, username);
                    }
                }



                if (kq.ToInt(0) > 0)
                {
                    // Update Variable
                    if (hdfsetStyle.Value == "2")
                    {
                        string variable = hdfVariableListInsert.Value;
                        if (!string.IsNullOrEmpty(variable))
                        {
                            string[] items = variable.Split(',');
                            for (int i = 0; i < items.Length - 1; i++)
                            {
                                string   item        = items[i];
                                string[] itemElement = item.Split(';');

                                string   datanameid             = itemElement[0];
                                string[] datavalueid            = itemElement[1].Split('|');
                                string   datanametext           = itemElement[2];
                                string   datavaluetext          = itemElement[3];
                                string   productvariablesku     = itemElement[4].Trim().ToUpper();
                                string   regularprice           = itemElement[5];
                                string   costofgood             = itemElement[6];
                                string   retailprice            = itemElement[7];
                                string[] datanamevalue          = itemElement[8].Split('|');
                                string   imageUpload            = itemElement[8];
                                int      _MaximumInventoryLevel = itemElement[9].ToInt(0);
                                int      _MinimumInventoryLevel = itemElement[10].ToInt(0);
                                int      stockstatus            = itemElement[11].ToInt();
                                string   imageSrc = itemElement[13];
                                string   kq1      = "";

                                // Check variable
                                var Variable = ProductVariableController.GetBySKU(productvariablesku);
                                if (Variable != null)
                                {
                                    // Update image

                                    string image = Variable.Image;
                                    if (imageSrc == "/App_Themes/Ann/image/placeholder.png")
                                    {
                                        // Delete old image
                                        if (!string.IsNullOrEmpty(Variable.Image))
                                        {
                                            string fileImage = Server.MapPath(Variable.Image);
                                            File.Delete(fileImage);
                                            image = "";
                                        }
                                    }
                                    else
                                    {
                                        if (imageSrc != Variable.Image)
                                        {
                                            HttpPostedFile postedFile = Request.Files["" + imageUpload + ""];
                                            if (postedFile != null && postedFile.ContentLength > 0)
                                            {
                                                // Upload image
                                                var o = path + ProductID + '-' + convertToSlug(Path.GetFileName(postedFile.FileName));
                                                postedFile.SaveAs(Server.MapPath(o));
                                                image = o;

                                                // Delete old image
                                                if (!string.IsNullOrEmpty(Variable.Image))
                                                {
                                                    string fileImage = Server.MapPath(Variable.Image);
                                                    File.Delete(fileImage);
                                                }
                                            }
                                            else
                                            {
                                                // Delete old image
                                                if (!string.IsNullOrEmpty(Variable.Image))
                                                {
                                                    string fileImage = Server.MapPath(Variable.Image);
                                                    File.Delete(fileImage);
                                                    image = "";
                                                }
                                            }
                                        }
                                    }

                                    // Update variable

                                    kq1 = ProductVariableController.Update(Variable.ID, ProductID, Variable.ParentSKU, productvariablesku, Convert.ToDouble(Variable.Stock), Convert.ToInt32(Variable.StockStatus), Convert.ToDouble(regularprice), Convert.ToDouble(costofgood), Convert.ToDouble(retailprice), image, true, false, DateTime.Now, username, Convert.ToInt32(Variable.SupplierID), Variable.SupplierName, _MinimumInventoryLevel, _MaximumInventoryLevel);

                                    // Delete all productVariableValue

                                    bool deleteVariableValue = ProductVariableValueController.DeleteByProductVariableID(Variable.ID);
                                }
                                else
                                {
                                    string image = "";

                                    HttpPostedFile postedFile = Request.Files["" + imageUpload + ""];
                                    if (postedFile != null && postedFile.ContentLength > 0)
                                    {
                                        // Upload image
                                        var o = path + ProductID + '-' + convertToSlug(Path.GetFileName(postedFile.FileName));
                                        postedFile.SaveAs(Server.MapPath(o));
                                        image = o;
                                    }

                                    // Insert new variable

                                    kq1 = ProductVariableController.Insert(ProductID, ProductSKU, productvariablesku, 0, stockstatus, Convert.ToDouble(regularprice),
                                                                           Convert.ToDouble(costofgood), Convert.ToDouble(retailprice), image, true, false, DateTime.Now, username,
                                                                           ddlSupplier.SelectedValue.ToInt(0), ddlSupplier.SelectedItem.ToString(), _MinimumInventoryLevel, _MaximumInventoryLevel);
                                }

                                // Update ProductVariableValue

                                if (kq1.ToInt(0) > 0)
                                {
                                    string[] Data      = datanametext.Split('|');
                                    string[] DataValue = datavaluetext.Split('|');
                                    for (int k = 0; k < Data.Length - 1; k++)
                                    {
                                        int    variablevalueID   = datavalueid[k].ToInt();
                                        string variableName      = Data[k];
                                        string variableValueName = DataValue[k];
                                        ProductVariableValueController.Insert(kq1.ToInt(), productvariablesku, variablevalueID, variableName, variableValueName, false, DateTime.Now, username);
                                    }
                                }
                            }
                        }
                    }

                    PJUtils.ShowMessageBoxSwAlert("Cập nhật sản phẩm thành công", "s", true, Page);
                }
            }
        }