void PullTopic()
        {
            Dispatcher.BeginInvoke(delegate
            {
                progressIndicator.IsVisible = true;
                progressIndicator.Text = "Pulling Dues Item...";
            });

            Task.Run(new Action(() =>
            {
                try
                {
                    dues = DuesMobile.GetDuesItem(SettingsMobile.Instance.User.MemberId, SettingsMobile.Instance.User.LoginId, duesId, duesItemId);
                    item = dues.DuesFees.FirstOrDefault();
                    Members = new ObservableCollection<MemberDisplayDues>(dues.Members);
                    Dispatcher.BeginInvoke(delegate
                    {
                        Title.Title = item.PayBy.ToLongDateString();
                        DueDate.Text = item.PayBy.ToLongDateString();
                        AmountDue.Text = item.CostOfDues.ToString("N2");
                        RosterList.ItemsSource = Members;

                        progressIndicator.IsVisible = false;
                    });
                }
                catch (Exception exception)
                {
                    ErrorHandler.Save(exception, MobileTypeEnum.WP8);
                }
            }));


        }
        public static DuesPortableModel GetDuesObject(Guid leagueId, Guid currentMemberId)
        {
            DuesPortableModel due = new DuesPortableModel();
            try
            {
                var dc = new ManagementContext();
                var dues = (from xx in dc.FeeManagement.Include("LeagueOwner").Include("FeeClassifications").Include("FeeClassifications").Include("FeeClassifications.MembersClassified").Include("FeeClassifications.MembersClassified.Member")
                            where xx.LeagueOwner.LeagueId == leagueId
                            select xx).FirstOrDefault();

                if (dues != null)
                {
                    due.OwnerEntity = DuesOwnerEntityEnum.league.ToString();
                    due.DayOfMonthToCollectDefault = dues.DayOfMonthToCollectDefault;
                    due.DaysBeforeDeadlineToNotifyDefault = dues.DaysBeforeDeadlineToNotifyDefault;
                    due.DuesCostDisplay = dues.FeeCostDefault.ToString("N2");
                    due.DuesCost = dues.FeeCostDefault;
                    due.DuesId = dues.FeeManagementId;
                    due.LeagueOwnerId = dues.LeagueOwner.LeagueId;
                    due.LeagueOwnerName = dues.LeagueOwner.Name;
                    due.AcceptPaymentsOnline = dues.AcceptPaymentsOnline;
                    due.PayPalEmailAddress = dues.PayPalEmailAddress;
                    due.WhoPaysProcessorFeesEnum = (WhoPaysProcessorFeesEnum)Enum.Parse(typeof(WhoPaysProcessorFeesEnum), dues.WhoPaysProcessorFeesEnum.ToString());
                    due.DuesEmailText = dues.EmailResponse;

                    foreach (var classification in dues.FeeClassifications)
                    {
                        FeeClassified c = new FeeClassified();
                        c.FeeClassificationId = classification.FeeClassificationId;
                        c.FeeRequired = classification.FeeRequired;
                        c.Name = classification.Name;
                        c.DoesNotPayDues = classification.DoesNotPayDues;

                        foreach (var mem in classification.MembersClassified)
                            c.MembersInClass.Add(new MemberDisplayBasic() { DerbyName = mem.Member.DerbyName, MemberId = mem.Member.MemberId });

                        due.Classifications.Add(c);
                    }

                    var members = dues.LeagueOwner.Members.Where(x => x.IsInactiveForLeague == false && x.HasLeftLeague == false).OrderBy(x => x.Member.DerbyName).ToList();
                    foreach (var member in members)
                    {
                        MemberDisplayDues mem = new MemberDisplayDues();
                        mem.MemberId = member.Member.MemberId;
                        mem.DerbyName = member.Member.DerbyName;
                        mem.JoinedLeague = member.Created;
                        mem.LastName = member.Member.Lastname;
                        if (member.Member.ContactCard != null && member.Member.ContactCard.Emails.FirstOrDefault() != null)
                            mem.UserId = Guid.NewGuid();
                        else
                            mem.UserId = member.Member.AspNetUserId;

                        due.Members.Add(mem);
                    }

                    var duess = dues.Fees.OrderByDescending(x => x.PayBy);
                    foreach (var d in duess)
                    {
                        DuesItem item = new DuesItem();
                        item.CostOfDues = d.CostOfFee;
                        item.DuesItemId = d.FeeCollectionId;
                        item.PayBy = d.PayBy;
                        item.TotalPaid = 0.00;
                        item.TotalWithstanding = 0.00;
                        item.TotalPaymentNeededFromMember = d.CostOfFee;
                        item.ClassificationName = "";
                        item.MemberClassificationName = "";
                        foreach (var fee in d.FeesCollected)
                        {
                            DuesCollected col = new DuesCollected();
                            col.DuesPaid = fee.FeeCollected;
                            item.TotalPaid += fee.FeeCollected;
                            col.DuesCollectedId = fee.FeeCollectionId;
                            col.IsPaidInFull = fee.IsPaidInFull;
                            col.MemberPaidId = fee.MemberPaid.MemberId;
                            col.MemberPaidName = fee.MemberPaid.DerbyName;
                            col.Note = fee.Note;
                            col.IsWaived = fee.IsFeeWaived;
                            if (currentMemberId == fee.MemberPaid.MemberId)
                            {
                                if (col.IsPaidInFull || col.IsWaived)
                                    item.IsCurrentMemberPaidOrWaivedInFull = true;

                            }
                            item.DuesCollected.Add(col);
                        }
                        foreach (var fee in d.FeesRequired)
                        {
                            DuesRequired col = new DuesRequired();
                            col.DuesRequire = fee.FeeRequired;
                            col.DuesRequiredId = fee.FeeRequiredId;
                            col.IsPaidInFull = fee.IsPaidInFull;
                            col.MemberRequiredId = fee.MemberRequiredFrom.MemberId;
                            col.MemberRequiredName = fee.MemberRequiredFrom.DerbyName;
                            col.Note = fee.Note;
                            col.IsWaived = fee.IsFeeWaived;

                            item.DuesRequired.Add(col);
                        }

                        foreach (var member in due.Members)
                        {

                            //gets member in collection list
                            var collected = item.DuesCollected.Where(x => x.MemberPaidId == member.MemberId);
                            var required = item.DuesRequired.Where(x => x.MemberRequiredId == member.MemberId).FirstOrDefault();
                            var classification = due.Classifications.Where(x => x.MembersInClass.Where(y => y.MemberId == member.MemberId).Count() > 0).FirstOrDefault();

                            bool doesNotPayDues = false;
                            double duesDue = 0.0;
                            if (required != null)
                            {
                                duesDue = required.DuesRequire;
                                if (member.MemberId == currentMemberId)
                                {
                                    item.CostOfDuesFromMember = required.DuesRequire;
                                    item.MemberClassificationName = "Custom";
                                }
                            }
                            else if (classification != null)
                            {
                                doesNotPayDues = classification.DoesNotPayDues;
                                duesDue = classification.FeeRequired;
                                //if the user was classified and told they don't have to pay dues.
                                if (doesNotPayDues && duesDue <= 0)
                                {
                                    DuesCollected col = new DuesCollected();
                                    col.MemberPaidId = member.MemberId;
                                    col.IsWaived = true;
                                    if (currentMemberId == member.MemberId)
                                    {
                                        item.IsCurrentMemberPaidOrWaivedInFull = true;

                                    }
                                    item.DuesCollected.Add(col);
                                }

                                item.CostOfDuesFromMember = classification.FeeRequired;
                                //item.MemberClassificationName = classification.Name;
                                member.ClassificationId = classification.FeeClassificationId;
                                member.ClassificationName = classification.Name;
                                if (member.MemberId == currentMemberId)
                                    item.MemberClassificationName = classification.Name;
                            }
                            else
                            {
                                if (member.MemberId == currentMemberId)
                                {
                                    item.CostOfDuesFromMember = item.CostOfDues;
                                    item.MemberClassificationName = "Default";
                                }
                                duesDue = item.CostOfDues;
                            }

                            //no dues were collected
                            if (collected.Count() == 0)
                            {
                                //waive the user since they were created after dues was due.
                                if (item.PayBy < member.JoinedLeague)
                                {
                                    DuesCollected col = new DuesCollected();
                                    col.MemberPaidId = member.MemberId;
                                    col.IsWaived = true;
                                    if (currentMemberId == member.MemberId)
                                    {
                                        item.IsCurrentMemberPaidOrWaivedInFull = true;

                                    }
                                    item.DuesCollected.Add(col);
                                }
                                else
                                {
                                    item.TotalWithstanding -= duesDue;
                                    AddTotalWithstandingToMember(due, duesDue, member);
                                    if (member.MemberId == currentMemberId)
                                    {
                                        item.TotalPaidFromMember = 0.00;
                                        item.TotalPaymentNeededFromMember = duesDue;
                                        item.CostOfDuesFromMember = duesDue;
                                        if (duesDue <= 0)
                                            item.IsCurrentMemberPaidOrWaivedInFull = true;
                                    }
                                }

                            }
                            else
                            {
                                bool isWaived = false;
                                //gets the total dues paid by the member for this month.  Can pay in increments so thats
                                // why we check for all collected items.
                                double totalPaid = 0.00;
                                foreach (var c in collected)
                                {
                                    totalPaid += c.DuesPaid;
                                    isWaived = c.IsWaived;
                                    member.collected += c.DuesPaid;
                                }
                                //waive the user since they were created after dues was due.
                                if (item.PayBy < member.JoinedLeague)
                                    isWaived = true;

                                //if the total dues paid isn't equal to the actual cost of the month,
                                // we subtract it from the total withstanding dues.
                                if (totalPaid != item.CostOfDues && !isWaived)
                                {
                                    item.TotalWithstanding -= (duesDue - totalPaid);

                                    //generating a list of members who all have outstanding balances with the league.
                                    AddTotalWithstandingToMember(due, (duesDue - totalPaid), member);

                                }
                                //total withstanding for the current member accessing this page.
                                if (member.MemberId == currentMemberId)
                                {
                                    item.CostOfDuesFromMember = duesDue;
                                    item.TotalPaymentNeededFromMember = (duesDue - totalPaid);
                                    item.TotalPaidFromMember = totalPaid;
                                    if (item.TotalPaymentNeededFromMember <= 0)
                                        item.IsCurrentMemberPaidOrWaivedInFull = true;
                                }
                            }

                        }
                        due.DuesFees.Add(item);
                    }
                }
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return due;
        }
        public static DuesPortableModel GetDuesCollectionItem(long duesItemId, Guid duesManagementId, Guid currentMemberId)
        {
            try
            {
                DuesPortableModel due = new DuesPortableModel();
                var dc = new ManagementContext();
                var dues = (from xx in dc.FeeItem.Include("FeeManagedBy").Include("FeeManagedBy.FeeClassifications").Include("FeeManagedBy.FeeClassifications").Include("FeeManagedBy.FeeClassifications.MembersClassified").Include("FeeManagedBy.FeeClassifications.MembersClassified.Member")
                            where xx.FeeManagedBy.FeeManagementId == duesManagementId
                            where xx.FeeCollectionId == duesItemId
                            select xx).FirstOrDefault();


                if (dues != null)
                {
                    due.OwnerEntity = DuesOwnerEntityEnum.league.ToString();
                    due.DayOfMonthToCollectDefault = dues.FeeManagedBy.DayOfMonthToCollectDefault;
                    due.DaysBeforeDeadlineToNotifyDefault = dues.FeeManagedBy.DaysBeforeDeadlineToNotifyDefault;
                    due.DuesCostDisplay = dues.FeeManagedBy.FeeCostDefault.ToString("N2");
                    due.DuesCost = dues.FeeManagedBy.FeeCostDefault;
                    due.DuesId = duesManagementId;
                    due.LeagueOwnerId = dues.FeeManagedBy.LeagueOwner.LeagueId;
                    due.LeagueOwnerName = dues.FeeManagedBy.LeagueOwner.Name;
                    due.AcceptPaymentsOnline = dues.FeeManagedBy.AcceptPaymentsOnline;
                    due.PayPalEmailAddress = dues.FeeManagedBy.PayPalEmailAddress;
                    due.EmailResponse = dues.FeeManagedBy.EmailResponse;
                    due.WhoPaysProcessorFeesEnum = (WhoPaysProcessorFeesEnum)Enum.Parse(typeof(WhoPaysProcessorFeesEnum), dues.FeeManagedBy.WhoPaysProcessorFeesEnum.ToString());
                    if (dues.FeeManagedBy.CurrencyRate != null)
                        due.Currency = dues.FeeManagedBy.CurrencyRate.CurrencyAbbrName;
                    else
                        due.Currency = "USD";

                    foreach (var classification in dues.FeeManagedBy.FeeClassifications)
                    {
                        FeeClassified c = new FeeClassified();
                        c.FeeClassificationId = classification.FeeClassificationId;
                        c.FeeRequired = classification.FeeRequired;
                        c.Name = classification.Name;
                        c.DoesNotPayDues = classification.DoesNotPayDues;

                        foreach (var mem in classification.MembersClassified)
                            c.MembersInClass.Add(new MemberDisplayBasic() { DerbyName = mem.Member.DerbyName, MemberId = mem.Member.MemberId });

                        due.Classifications.Add(c);
                    }

                    DuesItem item = new DuesItem();
                    item.CostOfDues = dues.CostOfFee;
                    item.DuesItemId = dues.FeeCollectionId;
                    item.PayBy = dues.PayBy;
                    item.TotalPaid = 0.00;
                    item.TotalWithstanding = 0.00;
                    item.TotalPaymentNeededFromMember = dues.CostOfFee;

                    foreach (var fee in dues.FeesRequired)
                    {
                        DuesRequired col = new DuesRequired();
                        col.DuesRequire = fee.FeeRequired;
                        col.DuesRequiredId = fee.FeeRequiredId;
                        col.IsPaidInFull = fee.IsPaidInFull;
                        col.MemberRequiredId = fee.MemberRequiredFrom.MemberId;
                        col.MemberRequiredName = fee.MemberRequiredFrom.DerbyName;
                        col.Note = fee.Note;
                        col.IsWaived = fee.IsFeeWaived;
                        col.RequiredDate = fee.Created;

                        item.DuesRequired.Add(col);
                    }
                    foreach (var fee in dues.FeesCollected)
                    {
                        DuesCollected col = new DuesCollected();
                        col.DuesPaid = fee.FeeCollected;
                        item.TotalPaid += fee.FeeCollected;
                        col.DuesCollectedId = fee.FeeCollectionId;
                        col.IsPaidInFull = fee.IsPaidInFull;
                        col.MemberPaidId = fee.MemberPaid.MemberId;
                        col.MemberPaidName = fee.MemberPaid.DerbyName;
                        col.Note = fee.Note;
                        col.IsWaived = fee.IsFeeWaived;
                        col.WasDuesClearedByUser = fee.WasClearedByUser;
                        col.PaidDate = fee.Created;
                        if (currentMemberId == fee.MemberPaid.MemberId)
                        {
                            if (col.IsPaidInFull || col.IsWaived)
                                item.IsCurrentMemberPaidOrWaivedInFull = true;
                        }

                        item.DuesCollected.Add(col);
                    }
                    var members = dues.FeeManagedBy.LeagueOwner.Members.Where(x => x.IsInactiveForLeague == false && x.HasLeftLeague == false).OrderBy(x => x.Member.DerbyName).ToList();
                    foreach (var member in members)
                    {
                        //if the member joined after the dues was due.

                        //adds every member to the member list
                        MemberDisplayDues mem = new MemberDisplayDues();
                        mem.MemberId = member.Member.MemberId;
                        mem.DerbyName = member.Member.DerbyName;
                        mem.LastName = member.Member.Lastname;

                        if (member.Member.ContactCard != null && member.Member.ContactCard.Emails.FirstOrDefault() != null)
                            mem.UserId = Guid.NewGuid();
                        else
                            mem.UserId = member.Member.AspNetUserId;

                        var collection = item.DuesCollected.Where(x => x.MemberPaidId == mem.MemberId);
                        var required = item.DuesRequired.Where(x => x.MemberRequiredId == mem.MemberId).FirstOrDefault();
                        var classification = due.Classifications.Where(x => x.MembersInClass.Where(y => y.MemberId == mem.MemberId).Count() > 0).FirstOrDefault();

                        //set variables before defining them below.
                        mem.collected = 0.00;
                        mem.isPaidFull = false;
                        mem.isWaived = false;

                        if (required != null)
                            mem.due = required.DuesRequire;
                        else if (classification != null)
                        {
                            mem.DoesNotPayDues = classification.DoesNotPayDues;
                            mem.isWaived = classification.DoesNotPayDues;
                            mem.due = classification.FeeRequired;
                            if (mem.due <= 0)
                                mem.isPaidFull = true;
                        }
                        else
                            mem.due = item.CostOfDues;


                        foreach (var col in collection)
                        {
                            mem.collected += col.DuesPaid;
                            mem.due -= col.DuesPaid;
                            mem.isPaidFull = col.IsPaidInFull;
                            mem.isWaived = col.IsWaived;
                            mem.DatePaid = col.PaidDate;
                        }
                        bool wasDuesClearedByUser = false;
                        if (collection.Where(x => x.WasDuesClearedByUser == true).FirstOrDefault() != null)
                            wasDuesClearedByUser = true;
                        //waive the user since they were created after dues was due.
                        if (item.PayBy < member.Created && wasDuesClearedByUser == false)
                        {
                            mem.isWaived = true;
                            mem.isPaidFull = true;
                        }
                        mem.tempDue = mem.due;
                        due.Members.Add(mem);
                        //gets member in collection list
                        var collected = item.DuesCollected.Where(x => x.MemberPaidId == member.Member.MemberId);
                        if (collected.Count() == 0)
                        {
                            //if the member was created after payment, we don't require member to pay.
                            if (item.PayBy > member.Created)
                            {
                                item.TotalWithstanding -= mem.due;
                                if (member.Member.MemberId == currentMemberId)
                                {
                                    item.TotalPaidFromMember = 0.00;
                                    item.TotalPaymentNeededFromMember = mem.due;
                                    if (mem.due == 0.0)
                                        item.IsCurrentMemberPaidOrWaivedInFull = true;
                                }
                            }
                        }
                        else
                        {
                            //make sure member joined dues program before dues item is due.
                            if (item.PayBy > member.Created)
                            {
                                //gets the total dues paid by the member for this month.  Can pay in increments so thats
                                // why we check for all collected items.
                                double totalPaid = 0.00;
                                foreach (var c in collected)
                                {
                                    totalPaid += c.DuesPaid;
                                }
                                //if the total dues paid isn't equal to the actual cost of the month,
                                // we subtract it from the total withstanding dues.
                                if (totalPaid != item.CostOfDues)
                                {
                                    item.TotalWithstanding -= totalPaid;
                                }
                                //total withstanding for the current member accessing this page.
                                if (member.Member.MemberId == currentMemberId)
                                {
                                    item.TotalPaymentNeededFromMember = mem.due;
                                    item.TotalPaidFromMember = totalPaid;
                                }
                            }
                        }

                    }
                    due.DuesFees.Add(item);
                }
                return due;
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return null;
        }
 public DuesMemberItem()
 {
     Member = new MemberDisplayBasic();
     DuesItem = new DuesItem();
 }