public async Task <IActionResult> CreateSpecialDiscount(string m)
        {
            if (ModelState.IsValid)
            {
                var specialDiscount = new SpecialDiscount();
                try
                {
                    var             splitActiveDate = Input.ActivationDate.Split("/");
                    var             splitExpireDate = Input.ExpiredDate.Split("/");
                    PersianCalendar persianCalendar = new PersianCalendar();
                    specialDiscount.ActivationDate = persianCalendar
                                                     .ToDateTime(Int32.Parse(splitActiveDate[0]), Int32.Parse(splitActiveDate[1]),
                                                                 Int32.Parse(splitActiveDate[2]), 0, 0, 0, 0);
                    specialDiscount.ExpirationDate = persianCalendar
                                                     .ToDateTime(Int32.Parse(splitExpireDate[0]), Int32.Parse(splitExpireDate[1]),
                                                                 Int32.Parse(splitExpireDate[2]), 0, 0, 0, 0);
                    if (specialDiscount.ExpirationDate <= specialDiscount.ActivationDate)
                    {
                        ModelState.AddModelError("ExpiredDate", "تاریخ انقضا باید بعد از تاریخ فعالسازی باشد");
                        return(View());
                    }
                }
                catch (Exception)
                {
                    ModelState.AddModelError("ExpiredDate", "فرمت تاریخ های وارد شده صحیح نیست");
                    ModelState.AddModelError("ActivationDate", "فرمت تاریخ های وارد شده صحیح نیست");
                    return(View());
                }
                if (Input.ProductId < 1)
                {
                    ModelState.AddModelError("ProductId", "انتخاب محصول الزامی است");
                    return(View());
                }
                specialDiscount.DiscountPrice = Input.DiscountPrice;
                specialDiscount.IsPublished   = Input.Publishment;
                var oldSpecialDiscount = await _db.SpecialDiscount.AsNoTracking()
                                         .Where(c => c.ProductId.Equals(Input.ProductId))
                                         .FirstOrDefaultAsync();

                if (oldSpecialDiscount != null)
                {
                    ModelState.AddModelError("ProductId", "قبلا یک پیشنهاد شگفت انگیز برای این محصول ساخته شده است");
                    return(View());
                }
                specialDiscount.ProductId   = Input.ProductId;
                specialDiscount.ProductName = await _db.Products.AsNoTracking()
                                              .Where(c => c.Id.Equals(Input.ProductId))
                                              .Select(c => c.Title).FirstOrDefaultAsync();

                specialDiscount.BasePrice = await _db.Products.AsNoTracking()
                                            .Where(c => c.Id.Equals(Input.ProductId))
                                            .Select(c => c.BasePrice)
                                            .FirstOrDefaultAsync();

                await _db.SpecialDiscount.AddAsync(specialDiscount);

                await _db.SaveChangesAsync();


                await _db.SaveChangesAsync();

                HttpContext.Session.SetInt32("Message", (int)Messages.SpecialDiscountCreatedSuccessfully);
                return(RedirectToAction(actionName: "Index"));
            }

            return(View());
        }
示例#2
0
        public async Task <IActionResult> CreateSpecialDiscount(string m)
        {
            if (ModelState.IsValid)
            {
                var specialDiscount = new SpecialDiscount();
                try
                {
                    var             splitActiveDate = Input.ActivationDate.Split("/");
                    var             splitExpireDate = Input.ExpiredDate.Split("/");
                    PersianCalendar persianCalendar = new PersianCalendar();
                    specialDiscount.ActivationDate = persianCalendar
                                                     .ToDateTime(Int32.Parse(splitActiveDate[0]), Int32.Parse(splitActiveDate[1]),
                                                                 Int32.Parse(splitActiveDate[2]), 0, 0, 0, 0);
                    specialDiscount.ExpirationDate = persianCalendar
                                                     .ToDateTime(Int32.Parse(splitExpireDate[0]), Int32.Parse(splitExpireDate[1]),
                                                                 Int32.Parse(splitExpireDate[2]), 0, 0, 0, 0);
                    if (specialDiscount.ExpirationDate <= specialDiscount.ActivationDate)
                    {
                        ModelState.AddModelError("ExpiredDate", "end date should be after start date");
                        return(View());
                    }
                }
                catch (Exception)
                {
                    ModelState.AddModelError("ExpiredDate", "date is invalid");
                    ModelState.AddModelError("ActivationDate", "date is invalid");
                    return(View());
                }
                if (Input.ProductId < 1)
                {
                    ModelState.AddModelError("ProductId", "please select a product");
                    return(View());
                }
                specialDiscount.DiscountPrice = Input.DiscountPrice;
                specialDiscount.IsPublished   = Input.Publishment;
                var oldSpecialDiscount = await _db.SpecialDiscount.AsNoTracking()
                                         .Where(c => c.ProductId.Equals(Input.ProductId))
                                         .FirstOrDefaultAsync();

                if (oldSpecialDiscount != null)
                {
                    ModelState.AddModelError("ProductId", "there is another special offer for this product");
                    return(View());
                }
                specialDiscount.ProductId   = Input.ProductId;
                specialDiscount.ProductName = await _db.Products.AsNoTracking()
                                              .Where(c => c.Id.Equals(Input.ProductId))
                                              .Select(c => c.Title).FirstOrDefaultAsync();

                specialDiscount.BasePrice = await _db.Products.AsNoTracking()
                                            .Where(c => c.Id.Equals(Input.ProductId))
                                            .Select(c => c.BasePrice)
                                            .FirstOrDefaultAsync();

                await _db.SpecialDiscount.AddAsync(specialDiscount);

                await _db.SaveChangesAsync();


                await _db.SaveChangesAsync();

                HttpContext.Session.SetInt32("Message", (int)Messages.SpecialDiscountCreatedSuccessfully);
                return(RedirectToAction(actionName: "Index"));
            }

            return(View());
        }