public virtual ActionResult CouponUpdate(FormCollection formCollection) { var couponId = Convert.ToInt32(formCollection["CouponId"]); var couponNew = hopeLingerieEntities.Coupons.SingleOrDefault(a => a.CouponId == couponId && a.Active); var percentage = Convert.ToInt32(formCollection["Percentage_Raw"]); if (couponNew == null) couponNew = new Coupon(); TryUpdateModel(couponNew, formCollection); couponNew.Percentage = percentage; if (couponNew.CouponId <= 0) { couponNew.AddedDate = DateTime.Now; couponNew.AddedBy = User.Identity.Name; couponNew.Active = true; hopeLingerieEntities.Coupons.AddObject(couponNew); hopeLingerieEntities.SaveChanges(); } else { var couponExists = hopeLingerieEntities.Coupons.SingleOrDefault(a => a.Code == couponNew.Code && a.Active); // Si no existe el código if (couponExists == null) { var attachedCoupon = hopeLingerieEntities.Coupons.SingleOrDefault(a => a.CouponId == couponNew.CouponId && a.Active); attachedCoupon.Code = couponNew.Code; attachedCoupon.OrderId = couponNew.OrderId; hopeLingerieEntities.SaveChanges(); } } return Redirect("/BackOffice/Coupons"); }
public virtual ActionResult CouponUpdate(int Id) { if (Id > 0) { var coupon = hopeLingerieEntities.Coupons.SingleOrDefault(a => a.CouponId == Id); return View(coupon); } var newCoupon = new Coupon { Code = "" }; return View(newCoupon); }
/// <summary> /// Deprecated Method for adding a new object to the Coupons EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToCoupons(Coupon coupon) { base.AddObject("Coupons", coupon); }
public virtual ActionResult CouponCreate(FormCollection formCollection) { Coupon couponBase = new Coupon(); var count = 1; TryUpdateModel(couponBase, formCollection); var total = 0; var percentage = 0; try { total = Convert.ToInt32(formCollection["Total_Raw"]); } catch { } try { percentage = Convert.ToInt32(formCollection["Percentage_Raw"]); } catch { total = 0; } while (count <= total) { string generatedCode = KeyGenerationService.Generate(8); Coupon coupon = hopeLingerieEntities.Coupons.SingleOrDefault(x => x.Code == generatedCode); // Código repetido if (coupon != null) count--; else hopeLingerieEntities.Coupons.AddObject( new Coupon { Campaign = couponBase.Campaign, Code = generatedCode, ExpirationDate = couponBase.ExpirationDate, Percentage = Convert.ToInt32(formCollection["Percentage_Raw"]), Active = true, AddedDate = DateTime.Now, AddedBy = User.Identity.Name }); count++; } hopeLingerieEntities.SaveChanges(); return Redirect("/BackOffice/Coupons"); }
/// <summary> /// Create a new Coupon object. /// </summary> /// <param name="couponId">Initial value of the CouponId property.</param> /// <param name="code">Initial value of the Code property.</param> /// <param name="percentage">Initial value of the Percentage property.</param> /// <param name="addedDate">Initial value of the AddedDate property.</param> /// <param name="addedBy">Initial value of the AddedBy property.</param> /// <param name="expirationDate">Initial value of the ExpirationDate property.</param> public static Coupon CreateCoupon(global::System.Int32 couponId, global::System.String code, global::System.Int32 percentage, global::System.DateTime addedDate, global::System.String addedBy, global::System.DateTime expirationDate) { Coupon coupon = new Coupon(); coupon.CouponId = couponId; coupon.Code = code; coupon.Percentage = percentage; coupon.AddedDate = addedDate; coupon.AddedBy = addedBy; coupon.ExpirationDate = expirationDate; return coupon; }