public void PayMemberManualPayment(int weeklyPaymentId, int currentOrderId, int memberPlanId, string paymentConfirmation, string comment = "") { DIYPTEntities db = new DIYPTEntities(); try { db.Database.Connection.Open(); PrizeOrder myCurrentOrder; MemberExercisePlan myPlan; MemberManualPayment myManualPayment; DateTime currentEndDate = PrizeCommonUtils.GetSystemDate(); if (currentOrderId >= 0) { // Get Weekly payment. myManualPayment = db.MemberManualPayments.Single(o => o.Id == weeklyPaymentId); myManualPayment.PaidDate = currentEndDate; myManualPayment.Comment = comment; myManualPayment.Status = PrizeConstants.STATUS_PLAN_MANUAL_PAYMENT_APPROVED + myManualPayment.Status[1]; // Get the order based on order id. myCurrentOrder = db.PrizeOrders.Single(o => o.OrderId == currentOrderId); // Update the order to reflect payment has been completed. myCurrentOrder.PaymentTransactionId = paymentConfirmation; myPlan = db.MemberExercisePlans.Single(o => o.Id == myCurrentOrder.MemberPlanId); myPlan.Status = PrizeConstants.STATUS_PLAN_NOT_STARTED + PrizeConstants.STATUS_PLAN_PAID; if (myPlan.StartDate < currentEndDate) { DateTime startDate = PrizeCommonUtils.GetNextWeekStart(currentEndDate); DateTime endDate = PrizeCommonUtils.GetWeekEnd(startDate); myPlan.StartDate = startDate; IList <MemberExercisePlanWeek> myPlanWeeks = (from c in db.MemberExercisePlanWeeks where c.MemberExercisePlanId == myPlan.Id orderby c.StartDate select c).ToList(); foreach (MemberExercisePlanWeek myPlanWeek in myPlanWeeks) { myPlanWeek.StartDate = startDate; myPlanWeek.EndDate = endDate; myPlanWeek.Status = PrizeConstants.STATUS_PLAN_WEEK_NOT_STARTED; myPlan.EndDate = endDate; db.SaveChanges(); startDate = startDate.AddDays(7); endDate = endDate.AddDays(7); } } // Save to DB. db.SaveChanges(); } } finally { db.Dispose(); } }
public int BuyNewPlan(int newPlanId, ref PrizeExercisePlan prizePlan, ref MemberExercisePlan newMemberPlan) { DIYPTEntities db = new DIYPTEntities(); try { if (PrizeMemberAuthUtils.CurrentUserLogin() != true) { return(PrizeErrorCode.ERROR_NOT_LOGGED_IN); } int memberId = PrizeMemberAuthUtils.GetMemberID(); db.Database.Connection.Open(); PrizeExercisePlan plan = (from c in db.PrizeExercisePlans where c.Id == newPlanId select c).FirstOrDefault(); if (plan == null) { return(-1); } //using (TransactionScope transaction = new TransactionScope()) //{ if (plan == null) { return(PrizeErrorCode.ERROR_PLAN_NOT_EXIST); } MemberExercisePlan myExistingPaidPlan = (from c in db.MemberExercisePlans where c.MemberId == memberId && (c.Status.Equals(PrizeConstants.STATUS_PLAN_STARTED + PrizeConstants.STATUS_PLAN_PAID) || c.Status.Equals(PrizeConstants.STATUS_PLAN_NOT_STARTED + PrizeConstants.STATUS_PLAN_PAID)) orderby c.EndDate descending select c).FirstOrDefault(); DateTime currentEndDate; if (myExistingPaidPlan != null) { currentEndDate = myExistingPaidPlan.EndDate.Value; } else { currentEndDate = PrizeCommonUtils.GetSystemDate(); } List <MemberExercisePlan> myNotPaidPlans = (from c in db.MemberExercisePlans where c.MemberId == memberId && (c.Status.EndsWith(PrizeConstants.STATUS_PLAN_NOT_PAID) || c.Status.EndsWith(PrizeConstants.STATUS_PLAN_MANUAL_PAYMENT)) select c).ToList(); foreach (MemberExercisePlan notPaidPlan in myNotPaidPlans) { IQueryable <MemberExercisePlanWeek> notPaidPlanWeeks = (from c in db.MemberExercisePlanWeeks where c.MemberExercisePlanId == notPaidPlan.Id select c); foreach (var week in notPaidPlanWeeks) { MemberPlanWeekResult weekResult = (from c in db.MemberPlanWeekResults where c.MemberExercisePlanWeekId == week.Id select c).SingleOrDefault(); db.MemberExercisePlanWeeks.Remove(week); db.MemberPlanWeekResults.Remove(weekResult); } notPaidPlan.Status = PrizeConstants.STATUS_PLAN_NOT_STARTED + PrizeConstants.STATUS_PLAN_PAYMENT_CANCELLED; List <MemberManualPayment> manualPayments = (from c in db.MemberManualPayments where c.MemberExercisePlanId == notPaidPlan.Id && c.Status.StartsWith(PrizeConstants.STATUS_PLAN_MANUAL_PAYMENT_NOT_APPROVED) select c).ToList(); foreach (var notPaidRecord in manualPayments) { db.MemberManualPayments.Remove(notPaidRecord); db.SaveChanges(); } } db.SaveChanges(); DateTime startDate = PrizeCommonUtils.GetNextWeekStart(currentEndDate); DateTime endDate = PrizeCommonUtils.GetWeekEnd(startDate); MemberExercisePlan myPlan = new MemberExercisePlan(); myPlan.MemberId = memberId; myPlan.ExercisePlanId = plan.Id; myPlan.StartDate = startDate; myPlan.Status = PrizeConstants.STATUS_PLAN_NOT_STARTED + PrizeConstants.STATUS_PLAN_NOT_PAID; //Not paid db.MemberExercisePlans.Add(myPlan); db.SaveChanges(); MemberPlanWeekResult myWeekResult; if (plan.IsTrialPlan != 1) { MemberExercisePlanWeek myPlanWeekOrientation = new MemberExercisePlanWeek(); myPlanWeekOrientation.MemberExercisePlanId = myPlan.Id; myPlanWeekOrientation.ExercisePlanWeekId = 0; myPlanWeekOrientation.MemberId = memberId; myPlanWeekOrientation.StartDate = startDate; myPlanWeekOrientation.EndDate = endDate; myPlanWeekOrientation.Status = PrizeConstants.STATUS_PLAN_WEEK_NOT_STARTED; myPlanWeekOrientation.Week = 0; db.MemberExercisePlanWeeks.Add(myPlanWeekOrientation); db.SaveChanges(); myWeekResult = new MemberPlanWeekResult(); myWeekResult.MemberId = memberId; myWeekResult.MemberExercisePlanWeekId = myPlanWeekOrientation.Id; InitialiseWeekResult(ref myWeekResult); db.MemberPlanWeekResults.Add(myWeekResult); db.SaveChanges(); myPlan.EndDate = endDate; startDate = startDate.AddDays(7); endDate = endDate.AddDays(7); } IList <PrizeExercisePlanWeek> planWeeks = plan.PrizeExercisePlanWeeks.OrderBy(s => s.StartWeek).ToList(); foreach (PrizeExercisePlanWeek planWeek in planWeeks) { for (int i = planWeek.StartWeek; i <= planWeek.EndWeek; i++) { MemberExercisePlanWeek myPlanWeek = new MemberExercisePlanWeek(); myPlanWeek.MemberExercisePlanId = myPlan.Id; myPlanWeek.ExercisePlanWeekId = planWeek.Id; myPlanWeek.MemberId = memberId; myPlanWeek.StartDate = startDate; myPlanWeek.EndDate = endDate; myPlanWeek.Status = PrizeConstants.STATUS_PLAN_WEEK_NOT_STARTED; myPlanWeek.Week = i; db.MemberExercisePlanWeeks.Add(myPlanWeek); db.SaveChanges(); myWeekResult = new MemberPlanWeekResult(); myWeekResult.MemberId = memberId; myWeekResult.MemberExercisePlanWeekId = myPlanWeek.Id; InitialiseWeekResult(ref myWeekResult); db.MemberPlanWeekResults.Add(myWeekResult); myPlan.EndDate = endDate; db.SaveChanges(); startDate = startDate.AddDays(7); endDate = endDate.AddDays(7); } } //transaction.Complete(); newMemberPlan = myPlan; prizePlan = plan; return(newPlanId); //} } finally { db.Dispose(); } }