public bool saveprod(b2bProduct p)
        {
            var a = GetByID <b2bProduct>("productid", p.ProductID);

            a.ProductName = p.ProductName;
            SaveData <b2bProduct>(a, "productid");
            return(IsResult);
        }
        private bool ValidateProduct(b2bProduct model)
        {
            //Example
            if (model.ProductName == null)
            {
                IsResult = false;
            }
            else if (model.ProductNameEng == null)
            {
                IsResult = false;
            }

            return(IsResult);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateProduct(b2bProduct model)
        {
            var data = qDB.b2bProducts.Single(q => q.ProductID == model.ProductID);

            // set ค่า model
            data.ProductName = model.ProductName;
            data.RowFlag     = (short)model.RowFlag;

            //default
            data.RowVersion++;
            data.ModifiedBy   = "sa";
            data.ModifiedDate = DateTimeNow;

            qDB.SubmitChanges();// บันทึกค่า
            return(true);
        }
        public bool SaveProduct(b2bProduct model)
        {
            IsResult = true;
            if (!ValidateProduct(model))
            {
                return(IsResult);
            }

            using (var trans = new TransactionScope())
            {
                try
                {
                    if (model.ProductID > 0)
                    {
                        qDB.b2bProducts.Context.Refresh(RefreshMode.KeepCurrentValues, model);
                        qDB.b2bProducts.InsertOnSubmit(model);// ทำการ save
                        qDB.SubmitChanges();
                    }
                    else
                    {
                        qDB.b2bProducts.InsertOnSubmit(model);
                        qDB.SubmitChanges();

                        foreach (var m in model.b2bProductImages)
                        {
                            m.ProductID = model.ProductID;
                            qDB.b2bProductImages.InsertOnSubmit(m);
                            qDB.SubmitChanges();
                        }
                    }
                    trans.Complete();
                }
                catch (Exception ex)
                {
                    IsResult = false;
                    MsgError.Add(ex);
                }
            }

            return(IsResult);
        }
        public bool InsertProduct(b2bProduct product, List <b2bProductImage> productImages, int CompID, int CompLevel)
        {
            // bom ยังไม่ได้ทดสอบ method ครับ
            if (ValidateFullProduct(CompID, CompLevel))
            {
                using (var trans = new TransactionScope())
                {
                    qDB.b2bProducts.InsertOnSubmit(product);
                    qDB.SubmitChanges();
                    foreach (var img in productImages)
                    {
                        img.ProductID = product.ProductID;
                        qDB.b2bProductImages.InsertOnSubmit(img);
                        qDB.SubmitChanges();
                    }

                    trans.Complete();
                    IsResult = true;
                }
            }
            return(IsResult);
        }
        public ActionResult AddProduct
        (
            #region Param
            string ProductName,
            int?ProductGroup,
            string ProductCode,
            string Price,
            string Price_One,
            string Keyword,
            string QuickDetail,
            string FullDetail,
            string Qty,
            string QtyUnit,
            string Catecode,
            string CateLV3,
            List <string> ProductImgPath,
            int Ispromotion,
            string PromotionPrice
            #endregion
        )
        {
            var svProduct      = new ProductService();
            var svProductImage = new ProductImageService();

            b2bProduct model = new b2bProduct();

            try
            {
                #region Set Product Model
                model.ProductName      = ProductName;
                model.ProductGroupID   = (ProductGroup > 0) ? ProductGroup : 0;
                model.ProductCode      = ProductCode;
                model.Price            = DataManager.ConvertToDecimal(Price);
                model.Price_One        = DataManager.ConvertToDecimal(Price_One);
                model.ProductKeyword   = Keyword;
                model.ShortDescription = QuickDetail;
                model.ProductDetail    = FullDetail;
                model.Qty = DataManager.ConvertToDecimal(Qty);
                //model.QtyUnit = QtyUnit;
                model.ProductImgPath = ProductImgPath[0];
                model.IsPromotion    = DataManager.ConvertToBool(Ispromotion);
                model.PromotionPrice = DataManager.ConvertToDecimal(PromotionPrice);
                model.CompID         = LogonCompID;
                model.CateLV1        = Convert.ToInt32(Catecode.Substring(9, 4));
                model.CateLV2        = Convert.ToInt32(Catecode.Substring(14, 4));
                model.CateLV3        = Convert.ToInt32(CateLV3);
                model.IsShow         = true;
                model.IsJunk         = false;
                model.Complevel      = DataManager.ConvertToShort(LogonCompLevel);
                if (QtyUnit == "0")
                {
                    model.QtyUnit = "รายการ";
                }
                else
                {
                    model.QtyUnit = QtyUnit;
                }
                #region Check ว่าเป็น OutSource Add หรือ User Add
                if (LogonServiceType >= 9)
                {
                    model.RowFlag  = (short)2;
                    model.CompCode = LogonCompCode;
                }
                else
                {
                    model.RowFlag = (short)6;
                }
                #endregion

                model.ContactCount   = 0;
                model.ListNo         = 0;
                model.ViewCount      = 0;
                model.QuotationCount = 0;
                model.TelCount       = 0;
                model.CreatedDate    = DateTime.Now;
                model.ModifiedDate   = DateTime.Now;
                model.CreatedBy      = LogonCompCode;
                model.ModifiedBy     = LogonCompCode;
                #endregion

                #region Set ProductImage Model
                int i           = 1;
                var ProductImgs = new List <b2bProductImage>();
                foreach (var imgname in ProductImgPath)
                {
                    var modelImg = new b2bProductImage();
                    modelImg.ImgDetail    = imgname.Substring(0, 15);
                    modelImg.ListNo       = i;
                    modelImg.ImgPath      = imgname;
                    modelImg.RowVersion   = 1;
                    modelImg.RowFlag      = 1;
                    modelImg.CreatedDate  = DateTime.Now;
                    modelImg.ModifiedDate = DateTime.Now;
                    modelImg.CreatedBy    = "sa";
                    modelImg.ModifiedBy   = "sa";
                    ProductImgs.Add(modelImg);
                    i++;
                }
                #endregion

                #region Insert Product And ProductImage
                svProduct.InsertProduct(model, ProductImgs, LogonCompID, LogonCompLevel);
                svProduct.UpdateProductCountInCategories(Convert.ToInt32(Catecode.Substring(9, 4)), Convert.ToInt32(Catecode.Substring(14, 4)), Convert.ToInt32(CateLV3));
                svProduct.UpdateProductCountInCompany(LogonCompID);
                #endregion

                #region Save Image Files
                imgManager         = new FileHelper();
                imgManager.DirPath = "Product/" + LogonCompID + "/" + model.ProductID;


                if (Request.Cookies["AddProduct"] != null)
                {
                    imgManager.DirTempPath = Request.Cookies["AddProduct"]["UnRegis"];
                    HttpCookie myCookie = new HttpCookie("AddProduct");
                    myCookie.Expires = DateTime.Now.AddDays(-1d);
                    Response.Cookies.Add(myCookie);
                }
                else
                {
                    imgManager.DirTempPath = "Temp/Product/" + LogonCompID;
                }


                SaveFileImage(
                    imgManager.DirTempPath,
                    imgManager.DirPath,
                    ProductImgPath, 150, 450);
                #endregion

                //var mgKeyword = new KeywordMongo();
                //var isresult = mgKeyword.UpdateMongoProduct(model.ProductID);
            }
            catch (Exception ex)
            {
                CreateLogFiles(ex);
            }


            return(Json(new { IsResult = svProduct.IsResult, MsgError = GenerateMsgError(svProduct.MsgError), ID = model.ProductID }));
        }
        public ActionResult EditProductByID
        (
            #region Param
            int ProductID,
            int Rowflag,
            string ProductName,
            int?ProductGroup,
            string ProductCode,
            string Price,
            string Price_One,
            string Keyword,
            string QuickDetail,
            string FullDetail,
            string Qty,
            string QtyUnit,
            string Catecode,
            string CateLV3,
            List <string> ProductImgPath,
            List <int> ProductImgID,
            int Ispromotion,
            string PromotionPrice
            #endregion
        )
        {
            var        svProduct = new ProductService();
            b2bProduct model     = new b2bProduct();

            if (LogonCompID > 0)
            {
                try
                {
                    #region Set Model
                    model.ProductID        = ProductID;
                    model.ProductName      = ProductName;
                    model.ProductImgPath   = ProductImgPath[0];
                    model.ProductGroupID   = ProductGroup;
                    model.ProductCode      = ProductCode;
                    model.Price            = decimal.Parse(Price);
                    model.Price_One        = decimal.Parse(Price_One);
                    model.ProductKeyword   = Keyword;
                    model.ShortDescription = QuickDetail;
                    model.ProductDetail    = FullDetail;
                    model.Qty            = decimal.Parse(Qty);
                    model.QtyUnit        = QtyUnit;
                    model.IsPromotion    = DataManager.ConvertToBool(Ispromotion);
                    model.PromotionPrice = DataManager.ConvertToDecimal(PromotionPrice);
                    model.ModifiedBy     = LogonCompCode;

                    if (Catecode == null)
                    {
                        svProduct.UpdateProductCountInCategories(0, 0, Convert.ToInt32(CateLV3));
                    }
                    else if (Catecode == "")
                    {
                        svProduct.UpdateProductCountInCategories(0, 0, Convert.ToInt32(CateLV3));
                    }
                    else
                    {
                        model.CateLV1 = Convert.ToInt32(Catecode.Substring(9, 4));
                        model.CateLV2 = Convert.ToInt32(Catecode.Substring(14, 4));
                        svProduct.UpdateProductCountInCategories(Convert.ToInt32(Catecode.Substring(9, 4)), Convert.ToInt32(Catecode.Substring(14, 4)), Convert.ToInt32(CateLV3));
                    }
                    if (Convert.ToInt32(CateLV3) != 0)
                    {
                        model.CateLV3 = Convert.ToInt32(CateLV3);
                    }

                    #region OurSource Update or Member Update
                    if (LogonServiceType >= 9)
                    {
                        model.RowFlag  = 2;
                        model.CompCode = LogonCompCode;
                    }
                    else
                    {
                        // เช็ค ถ้า สินค้าไม่อนุมัติมาก่อนให้เปลี่ยนเป็น รออนุมัติ
                        if (Rowflag == 3)
                        {
                            model.RowFlag = 2;
                        }
                        else
                        {
                            model.RowFlag = 6;
                        }
                    }
                    #endregion

                    #endregion

                    #region Update Data
                    svProduct.UpdateProduct(model, ProductImgID, ProductImgPath);
                    svProduct.UpdateProductCountInCompany(LogonCompID);
                    #endregion


                    //var mgkeyword = new KeywordMongo();
                    //var isresult = mgkeyword.UpdateMongoProduct(model.ProductID);

                    #region Check And Update Files
                    if (svProduct.IsResult)
                    {
                        imgManager             = new FileHelper();
                        imgManager.DirPath     = "Product/" + LogonCompID + "/" + ProductID;
                        imgManager.DirTempPath = "Temp/Product/" + LogonCompID;
                        SaveFileImage(imgManager.DirTempPath, imgManager.DirPath, ProductImgPath, 150, 450);

                        imgManager.DirPath = "Product/" + LogonCompID + "/" + ProductID;
                        DeleteFileImage(imgManager.DirPath, svProduct.OldFiles, ProductImgPath);
                    }
                    #endregion
                }
                catch (Exception ex)
                {
                    svProduct.MsgError.Add(ex);
                    CreateLogFiles(ex);
                }
            }

            return(Json(new { IsResult = svProduct.IsResult, MsgError = GenerateMsgError(svProduct.MsgError), ID = model.ProductID }));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateProduct(b2bProduct model, List <int> ImgID, List <string> ImgName)
        {
            var data = qDB.b2bProducts.Single(q => q.ProductID == model.ProductID);

            #region Set Model
            // set ค่า model
            data.ProductName      = model.ProductName;
            data.ProductGroupID   = model.ProductGroupID;
            data.ProductCode      = model.ProductCode;
            data.Price            = model.Price;
            data.Price_One        = model.Price_One;
            data.ProductKeyword   = model.ProductKeyword;
            data.ShortDescription = model.ShortDescription;
            data.ProductDetail    = model.ProductDetail;
            data.Qty     = model.Qty;
            data.QtyUnit = model.QtyUnit;
            if (model.CateLV1 != null)
            {
                data.CateLV1 = model.CateLV1;
            }
            if (model.CateLV2 != null)
            {
                data.CateLV2 = model.CateLV2;
            }
            if (model.CateLV3 != null)
            {
                data.CateLV3 = model.CateLV3;
            }
            data.ProductImgPath = model.ProductImgPath;
            data.RowFlag        = model.RowFlag;
            data.CompCode       = model.CompCode;
            // default
            data.RowVersion++;
            data.ModifiedBy   = model.ModifiedBy;
            data.ModifiedDate = DateTimeNow;
            #endregion

            #region Save
            using (var trans = new TransactionScope())
            {
                qDB.SubmitChanges();// บันทึกค่า Product

                var OldImgs = SelectData <b2bProductImage>(" * ", " IsDelete = 0 AND ProductID =" + model.ProductID);
                OldFiles = new List <string>();
                NewFiles = new List <string>();
                OldID    = new List <int>();

                foreach (var it in OldImgs)
                {
                    OldID.Add((int)it.ProductImageID);
                    OldFiles.Add(it.ImgPath);
                }


                for (var i = 0; i < ImgName.Count; i++)
                {
                    if (ImgID[i] > 0)
                    {
                        if (OldID.Exists(m => m.Equals(ImgID[i])))
                        {
                            OldID.Remove(ImgID[i]);
                        }

                        UpdateByCondition <b2bProductImage>(@" ImgPath = N'" + ImgName[i] + "' , ListNo = " + i + ", "
                                                            + " ImgDetail = N'" + model.ProductName + "' ",
                                                            " ProductImageID = " + ImgID[i]);
                    }
                    else
                    {
                        #region กรณีเป็นรูป ใหม่ ให้ insert ข้อมูล
                        var it = new b2bProductImage();
                        it.ProductID    = model.ProductID;
                        it.ImgPath      = ImgName[i];
                        it.ImgDetail    = model.ProductName;
                        it.ListNo       = i;
                        it.IsShow       = true;
                        it.IsDelete     = false;
                        it.CreatedDate  = DateTimeNow;
                        it.ModifiedDate = DateTimeNow;
                        it.CreatedBy    = "sa";
                        it.ModifiedBy   = "sa";
                        qDB.b2bProductImages.InsertOnSubmit(it);
                        qDB.SubmitChanges();
                        #endregion
                    }
                }

                foreach (var it in OldID)
                {
                    UpdateByCondition <b2bProductImage>("IsDelete = 1 ", " ProductImageID = " + it);
                }

                trans.Complete();
                IsResult = true;
            }
            #endregion

            return(IsResult);
        }