public async Task <ActionResult> CouponModal(int couponId) { ViewBag.Label = "Thêm"; Coupon coupon = new Coupon(); if (couponId > 0) { ViewBag.Label = "Cập nhật"; coupon = await _coupon.FindAsync(x => x.CouponId == couponId); } return(PartialView("~/Areas/Admin/Views/CouponAdmin/_CouponModal.cshtml", coupon)); }
public async Task <JsonResult> UpdateCoupon(int orderId, string code) { var order = await _order.GetByIdAsync(orderId); if (code.IsBlank()) { order.CouponId = null; await _order.UpdateAsync(order); return(Json("OK", JsonRequestBehavior.AllowGet)); } code = code.ToUpper(); var coupon = await _coupon.FindAsync(x => x.Code.Equals(code)); if (coupon == null) { return(Json("Mã giảm giá không hợp lệ!", JsonRequestBehavior.AllowGet)); } else if (coupon.EndDate < DateTime.Now) { return(Json("Mã giảm giá đã hết hạn!", JsonRequestBehavior.AllowGet)); } else if (coupon.Quantity <= 0) { return(Json("Mã giảm giá đã dùng hết!", JsonRequestBehavior.AllowGet)); } order.CouponId = coupon.CouponId; if (await _order.UpdateAsync(order)) { coupon.Quantity -= 1; await _coupon.UpdateAsync(coupon); } return(Json("OK", JsonRequestBehavior.AllowGet)); }