public EwalletModel GetAllCurrentRewardInfo(int userId) { using (SleepingPartnermanagementTestingEntities dc = new SleepingPartnermanagementTestingEntities()) { UserReward usrReward = dc.UserRewards.Where(a => a.UserId.Value.Equals(userId)).OrderByDescending(o => o.RewardId.Value).FirstOrDefault(); int MinValueId = 0; var TotalAmountLeftUsers = dc.GetParentChildsLeftSP(userId).Where(a => a.IsPaidMember.Value.Equals(true)).ToList(); var TotalAmountRightUsers = dc.GetParentChildsRightSP(userId).Where(a => a.IsPaidMember.Value.Equals(true)).ToList(); decimal TotalAmountLeftUsersShow = TotalAmountLeftUsers.Sum(x => x.PaidAmount.Value); decimal TotalAmountRightUsersShow = TotalAmountRightUsers.Sum(x => x.PaidAmount.Value); Reward rewardRightlimit = (from rwrd in dc.Rewards where rwrd.Rightlimit >= TotalAmountRightUsersShow select rwrd).FirstOrDefault(); Reward rewardLeftlimit = (from rwrd in dc.Rewards where rwrd.Leftlimit >= TotalAmountLeftUsersShow select rwrd).FirstOrDefault(); if (rewardLeftlimit == null && rewardRightlimit == null) //if all rewards complete { Reward rewardRightIfComplete = (from rwrd in dc.Rewards where rwrd.Rightlimit <= TotalAmountRightUsersShow select rwrd).OrderByDescending(o => o.Id).FirstOrDefault(); Reward rewardLeftIfComplete = (from rwrd in dc.Rewards where rwrd.Leftlimit <= TotalAmountLeftUsersShow select rwrd).OrderByDescending(o => o.Id).FirstOrDefault(); int MaxValueId = Math.Max((int)rewardRightIfComplete.Id, (int)rewardLeftIfComplete.Id); if (usrReward != null) { if (usrReward.RewardId == MaxValueId) { EwalletModel obj1 = new EwalletModel(); obj1.bonus = "Completed"; obj1.witdraw = "Completed"; return(obj1); } else { MinValueId = Math.Min((int)rewardRightIfComplete.Id, (int)rewardLeftIfComplete.Id); } } } else if (rewardLeftlimit == null) { rewardRightlimit = (from rwrd in dc.Rewards where rwrd.Rightlimit <= TotalAmountRightUsersShow select rwrd).OrderByDescending(o => o.Id).FirstOrDefault(); rewardLeftlimit = (from rwrd in dc.Rewards where rwrd.Leftlimit <= TotalAmountRightUsersShow select rwrd).OrderByDescending(o => o.Id).FirstOrDefault(); MinValueId = Math.Min((int)rewardRightlimit.Id, (int)rewardLeftlimit.Id); } else if (rewardRightlimit == null) { rewardRightlimit = (from rwrd in dc.Rewards where rwrd.Rightlimit <= TotalAmountRightUsersShow select rwrd).OrderByDescending(o => o.Id).FirstOrDefault(); rewardLeftlimit = (from rwrd in dc.Rewards where rwrd.Leftlimit <= TotalAmountRightUsersShow select rwrd).OrderByDescending(o => o.Id).FirstOrDefault(); MinValueId = Math.Min((int)rewardRightlimit.Id, (int)rewardLeftlimit.Id); } else { MinValueId = Math.Min((int)rewardRightlimit.Id, (int)rewardLeftlimit.Id); } Reward reward = dc.Rewards.Where(a => a.Id.Equals(MinValueId)).FirstOrDefault(); decimal LeftLimitPV = (decimal)reward.Leftlimit; decimal TotalLeftUserPV = TotalAmountLeftUsersShow; decimal MaxValueLeft = Math.Max(LeftLimitPV, TotalLeftUserPV); decimal RemainingLeftUserPV = MaxValueLeft - TotalLeftUserPV; decimal RightLimitPV = (decimal)reward.Rightlimit; decimal TotalRightUserPV = TotalAmountRightUsersShow; decimal MaxValueRight = Math.Max(RightLimitPV, TotalRightUserPV); decimal RemainingRightUserPV = MaxValueRight - TotalRightUserPV; if (reward != null) { EwalletModel obj1 = new EwalletModel(); obj1.bonus = RemainingLeftUserPV.ToString(); obj1.witdraw = RemainingRightUserPV.ToString(); return(obj1); } } EwalletModel obj = new EwalletModel(); obj.bonus = "0"; obj.witdraw = "0"; return(obj); }