Example #1
0
        public JsonResult Discount(string coupon_code)
        {
            CMS_DiscountModels models = new CMS_DiscountModels();
            var status = 200;

            try
            {
                NSLog.Logger.Info("Discount_Request:", coupon_code);
                var result = _facOrder.Discount(coupon_code, ref models);
                if (!result)
                {
                    status = 500;
                }
            }
            catch (Exception ex)
            {
                NSLog.Logger.Error("Discount:", ex);
            }
            var obj = new
            {
                data   = models,
                Status = status
            };

            return(Json(obj, JsonRequestBehavior.AllowGet));
        }
Example #2
0
        public bool Discount(string coupon_code, ref CMS_DiscountModels model)
        {
            var result = true;

            try
            {
                using (var db = new CMS_Context())
                {
                    var data = db.CMS_Discount.Where(o => o.DiscountCode.Equals(coupon_code))
                               .Select(o => new CMS_DiscountModels
                    {
                        DiscountCode = o.DiscountCode,
                        Id           = o.ID,
                        Value        = o.Value.HasValue ? o.Value.Value : 0,
                        IsPercent    = o.ValueType == (byte)Commons.EValueType.Percent ? true : false,
                        ValueType    = o.ValueType
                    }).FirstOrDefault();
                    if (data == null)
                    {
                        result = false;
                    }
                    model = data;
                }
            }
            catch (Exception ex)
            {
                NSLog.Logger.Error("Discount:", ex);
                result = false;
            }
            return(result);
        }
Example #3
0
        public ActionResult Create(CMS_DiscountModels model)
        {
            try
            {
                byte[] photoByte = null;

                if (!ModelState.IsValid)
                {
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return(PartialView("_Create", model));
                }
                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;
                }
                var Id  = "";
                var msg = "";
                model.CreatedBy = CurrentUser.UserId;
                model.UpdatedBy = CurrentUser.UserId;
                var result = _factory.CreateOrUpdate(model, ref Id, ref msg);
                if (result)
                {
                    if (!string.IsNullOrEmpty(model.ImageURL) && model.PictureByte != null)
                    {
                        var          path = Server.MapPath("~/Uploads/Discounts/" + 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, Commons.WidthDisc, Commons.WidthDisc, Commons.HeightDisc);
                    }
                    return(RedirectToAction("Index"));
                }
                ModelState.AddModelError("DiscountCode", msg);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(PartialView("_Create", model));
            }
            catch (Exception ex)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(PartialView("_Create", model));
            }
        }
Example #4
0
        public ActionResult Delete(CMS_DiscountModels model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return(PartialView("_Delete", model));
                }
                var orgImageUrl = "";
                if (!string.IsNullOrEmpty(model.ImageURL))
                {
                    model.ImageURL = model.ImageURL.Replace(Commons._PublicImages, "").Replace("Discounts/", "").Replace(Commons.Image200_100, "");
                    orgImageUrl    = model.ImageURL;
                }

                var msg    = "";
                var result = _factory.Delete(model.Id, ref msg);
                if (result)
                {
                    if (System.IO.File.Exists(Server.MapPath("~/Uploads/Discounts/" + orgImageUrl)))
                    {
                        ImageHelper.Me.TryDeleteImageUpdated(Server.MapPath("~/Uploads/Discounts/" + orgImageUrl));
                    }

                    return(RedirectToAction("Index"));
                }
                ModelState.AddModelError("DiscountName", msg);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(PartialView("_Delete", model));
            }
            catch (Exception ex)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(PartialView("_Delete", model));
            }
        }
Example #5
0
        public CMS_DiscountModels GetDetail(string Id)
        {
            NSLog.Logger.Info("DiscountGetDetail", Id);
            CMS_DiscountModels result = null;

            try
            {
                using (var cxt = new CMS_Context())
                {
                    var data = cxt.CMS_Discount.Where(o => o.ID == Id && o.Status != (byte)Commons.EStatus.Deleted)
                               .Select(o => new CMS_DiscountModels
                    {
                        Id               = o.ID,
                        StoreID          = o.StoreID,
                        Name             = o.Name,
                        DiscountCode     = o.DiscountCode,
                        Description      = o.Description,
                        ImageURL         = string.IsNullOrEmpty(o.ImageUrl) ? "" : Commons._PublicImages + "Discounts/" + o.ImageUrl,
                        IsAllowOpenValue = o.IsAllowOpenValue ?? false,
                        IsApplyTotalBill = o.IsApplyTotalBill,
                        IsActive         = o.IsActive ?? false,
                        IsPercent        = (o.ValueType ?? (byte)Commons.EValueType.Percent) == (byte)Commons.EValueType.Percent,
                        Value            = o.Value ?? 0,
                    }).FirstOrDefault();

                    result = data;

                    NSLog.Logger.Info("ResponseDiscountGetDetail", result);
                }
            }
            catch (Exception ex)
            {
                NSLog.Logger.Error("ErrorDiscountGetDetail", ex);
            }
            return(result);
        }
Example #6
0
        public ActionResult Create()
        {
            CMS_DiscountModels model = new CMS_DiscountModels();

            return(PartialView("_Create", model));
        }
Example #7
0
        public bool CreateOrUpdate(CMS_DiscountModels model, ref string Id, ref string msg)
        {
            NSLog.Logger.Info("DiscountCreateOrUpdate", model);
            var Result = true;

            using (var cxt = new CMS_Context())
            {
                try
                {
                    model.DiscountCode = model.DiscountCode.Trim();
                    if (string.IsNullOrEmpty(model.Id)) /* insert */
                    {
                        Id = Guid.NewGuid().ToString();
                        var checkDup = cxt.CMS_Discount.Where(o => o.DiscountCode.Trim() == model.DiscountCode.Trim()).FirstOrDefault();
                        if (checkDup == null)
                        {
                            var e = new CMS_Discount
                            {
                                ID               = Id,
                                StoreID          = model.StoreID,
                                Name             = model.Name,
                                DiscountCode     = model.DiscountCode,
                                Description      = model.Description,
                                ImageUrl         = model.ImageURL,
                                IsAllowOpenValue = model.IsAllowOpenValue,
                                IsApplyTotalBill = model.IsApplyTotalBill,
                                IsActive         = model.IsActive,
                                Value            = model.Value,
                                ValueType        = model.IsPercent ? (byte)Commons.EValueType.Percent : (byte)Commons.EValueType.Currency,
                                Status           = (byte)Commons.EStatus.Actived,
                                CreatedDate      = DateTime.Now,
                                CreatedUser      = model.CreatedBy,
                                ModifiedUser     = model.CreatedBy,
                                LastModified     = DateTime.Now,
                            };
                            cxt.CMS_Discount.Add(e);
                        }
                        else
                        {
                            msg    = "Duplicate Code";
                            Result = false;
                        }
                    }
                    else /* updated */
                    {
                        var checkDupCode = cxt.CMS_Discount.Where(o => o.DiscountCode.Trim() == model.DiscountCode.Trim() && o.ID != model.Id).FirstOrDefault();
                        if (checkDupCode == null)
                        {
                            var e = cxt.CMS_Discount.Find(model.Id);
                            if (e != null)
                            {
                                e.Name             = model.Name;
                                e.DiscountCode     = model.DiscountCode;
                                e.Description      = model.Description;
                                e.ImageUrl         = model.ImageURL;
                                e.IsAllowOpenValue = model.IsAllowOpenValue;
                                e.IsApplyTotalBill = model.IsApplyTotalBill;
                                e.IsActive         = model.IsActive;
                                e.Value            = model.Value;
                                e.ValueType        = model.IsPercent ? (byte)Commons.EValueType.Percent : (byte)Commons.EValueType.Currency;
                                e.Status           = (byte)Commons.EStatus.Actived;
                                e.ModifiedUser     = model.CreatedBy;
                                e.LastModified     = DateTime.Now;
                            }
                            else
                            {
                                Result = false;
                                msg    = "Unable to find Discount.";
                            }
                        }
                        else
                        {
                            msg    = "Duplicate Code";
                            Result = false;
                        }
                    }

                    cxt.SaveChanges();
                    NSLog.Logger.Info("ResponseDiscountCreateOrUpdate", new { Result, msg });
                }
                catch (Exception ex)
                {
                    Result = false;
                    msg    = "System Error.";
                    NSLog.Logger.Error("ErrorDiscountCreateOrUpdate", ex);
                }
            }
            return(Result);
        }