Ejemplo n.º 1
0
        public ActionResult Create(ProductDetailModels model)
        {
            try
            {
                CategoriesFactory _cateFactory = new CategoriesFactory();
                var _categories = _cateFactory.GetListCategory(CurrentUser.UserId)
                                  .Where(x => x.IsActive)
                                  .Select(x => new SelectListItem
                {
                    Value = x.Id,
                    Text  = x.Name
                })
                                  .ToList();
                model.Categories = _categories;
                if (model.ListFunction != null && model.ListFunction.Count > 0)
                {
                    model.ListFunction.Where(x => x.IsDefault).ToList().ForEach(x => x.IsSelected = true);
                }

                if (string.IsNullOrEmpty(model.Name))
                {
                    ModelState.AddModelError("Name", "The Name field is required");
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return(View(model));
                }

                if (string.IsNullOrEmpty(model.Code))
                {
                    ModelState.AddModelError("Code", "The Code field is required");
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return(View(model));
                }

                if (string.IsNullOrEmpty(model.CategoryId))
                {
                    ModelState.AddModelError("CategoryId", "The Category field is required");
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return(View(model));
                }
                if (model.PictureUpload != null && !Commons.ImageExtendFiler.Contains(model.PictureUpload.ContentType))
                {
                    ModelState.AddModelError("PictureUpload", Commons.ErrorMsgFilterImage);
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return(View(model));
                }
                if (model.SaleFrom > model.SaleTo)
                {
                    ModelState.AddModelError("SaleFrom", "From Date must be less than To Date.");
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return(View(model));
                }

                if (model.ListPrice == null || (model.ListPrice != null && model.ListPrice.Count == 0))
                {
                    ModelState.AddModelError("ListPrice", "The Price Items is required");
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return(View(model));
                }



                //if (model.ListPrice != null && model.ListPrice.Count > 0)
                //{
                //    var isExistActive = model.ListPrice.Any(x => x.IsActive);
                //    if (!isExistActive)
                //    {
                //        ModelState.AddModelError("ListPrice", "Please enter a price item greater than or equal to 1 is active");
                //        Response.StatusCode = (int)HttpStatusCode.BadRequest;
                //        return View(model);
                //    }
                //}


                //if (model.IsExtend && (model.ListPriceExtend == null ||( model.ListPriceExtend != null && model.ListPriceExtend.Count == 0)))
                //{
                //    ModelState.AddModelError("ListPriceExtend", "The Extend Price Items is requered");
                //    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                //    return View(model);
                //}

                if (!ModelState.IsValid)
                {
                    if ((ModelState["PictureUpload"]) != null && (ModelState["PictureUpload"]).Errors.Count > 0)
                    {
                        model.ImageURL = "";
                    }
                    return(View(model));
                }
                /*** Processing Image *****/
                byte[] photoByte = null;
                if (model.PictureUpload != null && model.PictureUpload.ContentLength > 0)
                {
                    Byte[] imgByte = new Byte[model.PictureUpload.ContentLength];
                    model.PictureUpload.InputStream.Read(imgByte, 0, model.PictureUpload.ContentLength);
                    model.PictureByte   = imgByte;
                    model.ImageURL      = Guid.NewGuid() + Path.GetExtension(model.PictureUpload.FileName);
                    model.PictureUpload = null;
                    photoByte           = imgByte;
                }
                /*** End Processing Image ****/
                string msg = "";
                model.CreateUser  = CurrentUser.UserId;
                model.ProductType = (int)Commons.EProductType.Product;
                if (model.ListCategory == null)
                {
                    model.ListCategory = new List <ProductCategoryModels>();
                }

                model.ListCategory.Add(new ProductCategoryModels {
                    CategoryID = model.CategoryId
                });
                model.ListPrice.AddRange(model.ListPriceExtend);
                model.ListPrice.Where(x => x.IsNew).ToList().ForEach(x => x.ID = null);
                model.ListFunction = model.ListFunction.Where(x => x.IsSelected).ToList();
                var tmp = model.PictureByte;
                model.PictureByte = null;
                bool result = _factory.CreateOrEdit(model, ref msg);
                if (result)
                {
                    model.PictureByte = tmp;
                    //Save Image on Server
                    if (!string.IsNullOrEmpty(model.ImageURL) && model.PictureByte != null)
                    {
                        var          originalDirectory = new DirectoryInfo(string.Format("{0}Uploads\\", Server.MapPath(@"\")));
                        var          path = string.Format("{0}{1}", originalDirectory, model.ImageURL);
                        MemoryStream ms   = new MemoryStream(photoByte, 0, photoByte.Length);
                        ms.Write(photoByte, 0, photoByte.Length);
                        System.Drawing.Image imageTmp = System.Drawing.Image.FromStream(ms, true);
                        ImageHelper.Me.SaveCroppedImage(imageTmp, path, model.ImageURL, ref photoByte);
                        model.PictureByte = photoByte;
                        FTP.Upload(model.ImageURL, model.PictureByte);
                        ImageHelper.Me.TryDeleteImageUpdated(path);
                    }
                    return(RedirectToAction("Index"));
                }
                else
                {
                    model.ListPrice = model.ListPrice.Where(w => w.IsExtend == false).ToList();
                    model.ListPrice.Where(x => x.IsNew).ToList().ForEach(x => x.ID = Guid.NewGuid().ToString());
                    //ModelState.AddModelError("Name", Commons.ErrorMsg);
                    ModelState.AddModelError("Error", msg);
                    return(View(model));
                }
            }
            catch (Exception ex)
            {
                NSLog.Logger.Error("Create :", ex);
                return(new HttpStatusCodeResult(400, ex.Message));
            }
        }
Ejemplo n.º 2
0
        public ActionResult Create(ProductDetailModels model)
        {
            try
            {
                if (model.SaleFrom > model.SaleTo)
                {
                    ModelState.AddModelError("DateSaleValidate", "From Date must be less than To Date.");
                }
                if (model.PictureUpload != null && !Commons.ImageExtendFiler.Contains(model.PictureUpload.ContentType))
                {
                    ModelState.AddModelError("PictureUpload", Commons.ErrorMsgFilterImage);
                }
                //==========Composite
                model.ListComposite = model.ListComposite.Where(x => x.Status != (byte)Commons.EStatus.Deleted).ToList();
                model.ListComposite.ForEach(x =>
                {
                    if (x.IsUnlimited)
                    {
                        x.Quantity = -1;
                    }
                });
                //==========Price
                model.ListPrice       = model.ListPrice.Where(x => x.Status != (byte)Commons.EStatus.Deleted).ToList();
                model.ListPriceExtend = model.ListPriceExtend.Where(x => x.Status != (byte)Commons.EStatus.Deleted).ToList();
                model.ListPrice.AddRange(model.ListPriceExtend);

                if (!ModelState.IsValid)
                {
                    return(View(model));
                }

                byte[] photoByte = null;
                if (model.PictureUpload != null && model.PictureUpload.ContentLength > 0)
                {
                    Byte[] imgByte = new Byte[model.PictureUpload.ContentLength];
                    model.PictureUpload.InputStream.Read(imgByte, 0, model.PictureUpload.ContentLength);
                    model.PictureByte   = imgByte;
                    model.ImageURL      = Guid.NewGuid() + Path.GetExtension(model.PictureUpload.FileName);
                    model.PictureUpload = null;
                    photoByte           = imgByte;
                }
                //==============
                model.CreateUser  = CurrentUser.UserId;
                model.ProductType = (byte)Commons.EProductType.Package;
                string msg = "";
                var    tmp = model.PictureByte;
                model.PictureByte = null;
                bool result = _factory.CreateOrEdit(model, ref msg);
                if (result)
                {
                    model.PictureByte = tmp;
                    if (!string.IsNullOrEmpty(model.ImageURL) && model.PictureByte != null)
                    {
                        var          originalDirectory = new DirectoryInfo(string.Format("{0}Uploads\\", Server.MapPath(@"\")));
                        var          path = string.Format("{0}{1}", originalDirectory, model.ImageURL);
                        MemoryStream ms   = new MemoryStream(photoByte, 0, photoByte.Length);
                        ms.Write(photoByte, 0, photoByte.Length);
                        System.Drawing.Image imageTmp = System.Drawing.Image.FromStream(ms, true);
                        ImageHelper.Me.SaveCroppedImage(imageTmp, path, model.ImageURL, ref photoByte);
                        model.PictureByte = photoByte;
                        FTP.Upload(model.ImageURL, model.PictureByte);
                        ImageHelper.Me.TryDeleteImageUpdated(path);
                    }
                    return(RedirectToAction("Index"));
                }
                else
                {
                    //return RedirectToAction("Create");
                    //ModelState.AddModelError("Name", Commons.ErrorMsg /*msg*/);
                    ModelState.AddModelError("Error", msg);
                    model.ListPrice = model.ListPrice.Where(w => w.IsExtend == false).ToList();
                    return(View(model));
                }
            }
            catch (Exception ex)
            {
                NSLog.Logger.Error("ProductPackageCreate: ", ex);
                return(new HttpStatusCodeResult(400, ex.Message));
            }
        }
Ejemplo n.º 3
0
        public ActionResult Create(ProductDetailModels model)
        {
            try
            {
                if (model.AdditionType == (int)Commons.EAdditionType.Function)
                {
                    var lstseletedFunction = model.ListFunction.Where(x => x.IsSelected == true).ToList();
                    if (lstseletedFunction.Count < 1)
                    {
                        ModelState.AddModelError("Error", "Unable to save this additional function. At least one function must be selected.");
                    }
                }
                if (model.SaleFrom > model.SaleTo)
                {
                    ModelState.AddModelError("SaleFrom", "From Date must be less than To Date.");
                }
                model.ListCategory = model.ListCategory.Where(x => x.Status != (byte)Commons.EStatus.Deleted).ToList();
                model.ListPrice    = model.ListPrice.Where(x => x.Status != (byte)Commons.EStatus.Deleted).ToList();
                if (model.PictureUpload != null && !Commons.ImageExtendFiler.Contains(model.PictureUpload.ContentType))
                {
                    ModelState.AddModelError("PictureUpload", Commons.ErrorMsgFilterImage);
                }
                if (!ModelState.IsValid)
                {
                    //if ((ModelState["PictureUpload"]) != null && (ModelState["PictureUpload"]).Errors.Count > 0)
                    //    model.ImageURL = "";
                    //model.ImageURL = Commons.Image100_100;
                    return(View(model));
                }

                byte[] photoByte = null;
                if (model.PictureUpload != null && model.PictureUpload.ContentLength > 0)
                {
                    Byte[] imgByte = new Byte[model.PictureUpload.ContentLength];
                    model.PictureUpload.InputStream.Read(imgByte, 0, model.PictureUpload.ContentLength);
                    model.PictureByte   = imgByte;
                    model.ImageURL      = Guid.NewGuid() + Path.GetExtension(model.PictureUpload.FileName);
                    model.PictureUpload = null;
                    photoByte           = imgByte;
                }

                //============
                model.CreateUser  = CurrentUser.UserId;
                model.ProductType = (byte)Commons.EProductType.Addition;
                string msg    = "";
                bool   result = _factory.CreateOrEdit(model, ref msg);
                if (result)
                {
                    if (!string.IsNullOrEmpty(model.ImageURL) && model.PictureByte != null)
                    {
                        var          originalDirectory = new DirectoryInfo(string.Format("{0}Uploads\\", Server.MapPath(@"\")));
                        var          path = string.Format("{0}{1}", originalDirectory, model.ImageURL);
                        MemoryStream ms   = new MemoryStream(photoByte, 0, photoByte.Length);
                        ms.Write(photoByte, 0, photoByte.Length);
                        System.Drawing.Image imageTmp = System.Drawing.Image.FromStream(ms, true);
                        ImageHelper.Me.SaveCroppedImage(imageTmp, path, model.ImageURL, ref photoByte);
                        model.PictureByte = photoByte;
                        FTP.Upload(model.ImageURL, model.PictureByte);
                        ImageHelper.Me.TryDeleteImageUpdated(path);
                    }
                    return(RedirectToAction("Index"));
                }
                else
                {
                    //return RedirectToAction("Create");
                    //ModelState.AddModelError("Name", Commons.ErrorMsg /*msg*/);
                    ModelState.AddModelError("Error", msg);
                    return(View(model));
                }
            }
            catch (Exception ex)
            {
                NSLog.Logger.Error("ProductAdditionCreate: ", ex);
                return(new HttpStatusCodeResult(400, ex.Message));
            }
        }