public int GetImageTypeID(string Type)
 {
     baioEntities db = new baioEntities();
     var ImgType = db.ImageType.Where(x => x.Name == Type).FirstOrDefault();
     if (ImgType != null)
         return ImgType.ID;
     else
         return 1;
 }
        public string Handle(int TypeId, int ImgId,string filename,string ext, System.Drawing.Image image,int Rank)
        {
            baioEntities db = new baioEntities();
            ImageType it = db.ImageType.Where(x => x.IsDelete == false & x.ID == TypeId).FirstOrDefault();
            if (it != null)
            {
                photoWidth = Convert.ToInt32(it.Width);
                photoHeight = Convert.ToInt32(it.Height);
                photoMaxWidth = Convert.ToInt32(it.MaxWidth);
            }
            else
            {
                photoWidth = 100;
                photoHeight = 100;
                photoMaxWidth = 1000;
            }

            if (ImgId == 0) // Ekleme İşlemi, değilse güncellenecek
            {
                //Images i = new Images();
                ////i.SiteID = SiteID;
                //i.Src = filename;
                //i.ImageTypeID = TypeId;
                //i.Date = DateTime.Now;
                //i.IsDelete = false;
                //i.Rank = Rank;
                //i.Src = filename;
                //db.Images.Add(i);
                //db.SaveChanges();

                ImgId = (int)db.sp_ImagesAdd(filename, TypeId, Rank).FirstOrDefault();

            }
            else
            {
                // Güncelleme İşlemi

                //string oldFile = db.sp_ImageGetSrc(ImgId).FirstOrDefault();

                db.sp_ImageSrcUpdate(ImgId, filename);
                //tryToDelete(oldFile, "~/Assets/photo/r/");
                //tryToDelete(oldFile, "~/Assets/photo/u/");

            }

            int newwidthimg = photoMaxWidth;
            float AspectRatio = (float)image.Size.Width / (float)image.Size.Height;
            int newHeight = Convert.ToInt32(newwidthimg / AspectRatio);

            Bitmap bmPhoto;
            Graphics grPhoto;
            ImageCodecInfo ici;

            isTransparent = ContainsTransparent((Bitmap)image);
            if (isTransparent)
            {
                ici = GetEncoderInfo("image/png");
                bmPhoto = new Bitmap(newwidthimg, newHeight, PixelFormat.Format32bppArgb);
                bmPhoto.SetResolution(72, 72);
                grPhoto = Graphics.FromImage(bmPhoto);
                grPhoto.Clear(Color.Transparent);
            }
            else {
                ici = GetEncoderInfo("image/jpeg");
                bmPhoto = new Bitmap(newwidthimg, newHeight, PixelFormat.Format24bppRgb);
                bmPhoto.SetResolution(72, 72);
                grPhoto = Graphics.FromImage(bmPhoto);

            }

            grPhoto.CompositingQuality = CompositingQuality.HighQuality;
            grPhoto.SmoothingMode = SmoothingMode.HighQuality;
            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
            grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;

            grPhoto.DrawImage
                    (
                    image,
                    new Rectangle(0, 0, newwidthimg, newHeight),
                    0,
                   0,
                    image.Width,
                    image.Height,
                    GraphicsUnit.Pixel
                    );
            //Filigran
            //grPhoto.DrawImage(System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("~/www/seyir/Assets/photo/filigran.png")), new Point(0, (int)((newHeight - 900) / 2)));
            EncoderParameters ep = new EncoderParameters(1);
            ep.Param[0] = new EncoderParameter(Encoder.Quality, (long)100);

            //Graphics g = Graphics.FromImage(bmPhoto);

            grPhoto.Dispose();
            string path1 = HttpContext.Current.Server.MapPath("~/Assets/photo/r/" + filename + "_b.jpg");
            string path2 = HttpContext.Current.Server.MapPath("~/Assets/photo/r/" + filename + "_b.png");
            bmPhoto.Save(path1, ici, ep);
            bmPhoto.Save(path2, ici, ep);
            bmPhoto.Dispose();
            image.Dispose();

            if (isTransparent)
            {
                getAspectRatioPng(HttpContext.Current.Server.MapPath("~/Assets/photo/r/" + filename + "_b.png"),
                HttpContext.Current.Server.MapPath("~/Assets/photo/r/" + filename + "_t.jpg"),
                HttpContext.Current.Server.MapPath("~/Assets/photo/r/" + filename + "_t.png"), photoWidth, photoHeight, photoWidth, photoHeight, ext);
            }
            else
            {
                getAspectRatio(HttpContext.Current.Server.MapPath("~/Assets/photo/r/" + filename + "_b.jpg"),
                HttpContext.Current.Server.MapPath("~/Assets/photo/r/" + filename + "_t.png"),
                HttpContext.Current.Server.MapPath("~/Assets/photo/r/" + filename + "_t.jpg"), photoWidth, photoHeight, photoWidth, photoHeight, ext);

            }

            tryToDelete(SpecialDate(DateTime.Now.AddDays(-1)), "~/Assets/photo/u/");

            string result = filename + "|%|" + ImgId.ToString();
            return result;
        }
 public ActionResult Index()
 {
     baioEntities db = new baioEntities();
     ViewData["Categories"] = db.sp_ProductGetAllbyPaging(1,50,0,false,FirmID);
     return View();
 }
 public JsonResult GetCategories(string Type)
 {
     baioEntities db = new baioEntities();
     return Json(db.sp_CategoryGetAll(GetFirmID(), Type));
 }
        public JsonResult GetProducts(DataTableParameters param)
        {
            baioEntities db = new baioEntities();
            //string CategoryType = param.pageType;
            //IEnumerable<sp_ProductGetAllbyPaging_Result> data = db.sp_ProductGetAllbyPaging(1, param.iDisplayLength, 0,false).ToList();

            IEnumerable<sp_ProductGetAllbyPaging_Result> data = db.sp_ProductGetAllbyPaging((param.iDisplayStart / param.iDisplayLength) + 1, param.iDisplayLength, 0, false,1).ToList();

            string sKey;

            if (!string.IsNullOrEmpty(param.sSearch))
            {

                sKey = param.sSearch.ToLower();
                data = data.Where(x => !string.IsNullOrEmpty(x.Name) ? x.Name.ToLower().Contains(sKey) : true);
            }
            var sortColumnIndex = Convert.ToInt32(Request["iSortCol_0"]);
            Func<sp_ProductGetAllbyPaging_Result, string> orderingFunction = (p => sortColumnIndex == 1 ? p.Name :
               sortColumnIndex == 2 ? p.Stock.ToString() :
               p.Price.ToString());

            var pagedData = data.OrderBy(x => x.Rank);
            var sortDirection = Request["sSortDir_0"];
            if (sortDirection == "asc")
                pagedData = pagedData.OrderBy(orderingFunction);
            else
                pagedData = pagedData.OrderByDescending(orderingFunction);

            //string CacheKey = CategoryType + param.iDisplayLength + param.iDisplayStart + param.sSearch + param.iDisplayLength + sortColumnIndex + sortDirection;
            //var _Context = System.Web.HttpContext.Current;
            //var PageCache = _Context.Cache.Get(CacheKey) as List<sp_GetPagesLite_Result>;
            var lastData = new List<sp_ProductGetAllbyPaging_Result>();
            //if (PageCache == null)
            //if (true)
            //{
                lastData = pagedData
                  //  .Skip(param.iDisplayStart / param.iDisplayLength * param.iDisplayLength)
                  //.Take(param.iDisplayLength)
                  .Select(x => new sp_ProductGetAllbyPaging_Result
                  {
                     ImageSrc = x.ImageSrc,
                     Total = x.Total,
                     ID = x.ID,
                     Name = x.Name,
                     IsActive = x.IsActive,
                     Stock = x.Stock,
                     OrderCount = x.OrderCount,
                     Variant = x.Variant,
                      Price = x.Price
                  }).ToList();
                //_Context.Cache.Add(CacheKey, lastData, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(6, 0, 0), System.Web.Caching.CacheItemPriority.Default, null);
            //}
            //else
            //{
            //    lastData = PageCache;
            //}

            return Json(new
            {
                sEcho = param.sEcho,
                iTotalRecords = data.Count(),
                iTotalDisplayRecords = data.FirstOrDefault() != null ? data.FirstOrDefault().Total : 0,
                aaData = lastData
            },
            JsonRequestBehavior.AllowGet);
        }
        public bool SetPageRank(int[] IDs, int firstIndex, int lastIndex
            //, string CacheKey
            )
        {
            try
            {
                baioEntities db = new baioEntities();
                int a = 0;
                for (int i = firstIndex; i <= lastIndex; i++)
                {
                    int ID = IDs[a];
                    Product p = db.Product.Where(x => x.ID == ID).FirstOrDefault();
                    if (p != null)
                    {
                        p.Rank = i;
                    }

                    a++;
                }
                db.SaveChanges();
                //string[] CacheKeys = { CacheKey };
                //HelpClass.RemoveCaches(CacheKeys);
                return true;
            }
            catch
            {
                return false;
            }
        }
 public void SetAP(int ID, bool AP)
 {
     baioEntities db = new baioEntities();
     db.sp_ProductSetAP(ID, AP);
 }
        public JsonResult ProductFeatureAdd(int ProductID, string[] FeatureNames, decimal[] PriceDifferences)
        {
            baioEntities db = new baioEntities();
            for (int i = 0; i < FeatureNames.Length; i++)
            {
                db.sp_ProductFeatureAdd(FirmID, ProductID, FeatureNames[i], PriceDifferences[i]);
            }

            return Json(1);
        }
        public JsonResult ProductAdd(Product p)
        {
            baioEntities db = new baioEntities();

            List<int> ImageIDs = new List<int>();
            List<string> ImageFilenames = new List<string>();
            string filename = "";
            for (int j = 0; j < Request.Files.Count; j++)
            {
                var file = Request.Files[j];
                Guid gd = Guid.NewGuid();
                filename = gd.ToString();
                var _Server = System.Web.HttpContext.Current.Server;
                file.SaveAs(_Server.MapPath("~/Assets/photo/u/" + filename + ".jpg"));
                Image img = Image.FromFile(_Server.MapPath("~/Assets/photo/u/" + filename + ".jpg"));
                imageProcessor ip = new imageProcessor();
                int ImgTypeID = GetImageTypeID("Product");
                string imgInfo = ip.Handle(ImgTypeID, 0, filename, "jpg", img, j + 1);
                int ImageID = Convert.ToInt32(imgInfo.Split(new[] { "|%|" }, StringSplitOptions.RemoveEmptyEntries)[1]);
                ImageIDs.Add(ImageID);
                //ImageFilenames.Add(filename);

            }

            int ProductID = (int)db.sp_ProductAdd(FirmID,
                p.CategoryID,
                p.Name,
                p.Ingredients,
                p.Cost,
                p.Price,
                p.Stock, filename, p.CriticalStock).FirstOrDefault();

            foreach (var imgID in ImageIDs)
            {
                db.sp_ProductImagesAdd(ProductID, imgID);
            }

            return Json(ProductID);
        }