public static bool WaiveDuesAmount(long duesItemId, Guid duesManagementId, Guid memberIdPaid, string note)
 {
     try
     {
         var dc = new ManagementContext();
         var dues = (from xx in dc.FeeItem
                     where xx.FeeManagedBy.FeeManagementId == duesManagementId
                     where xx.FeeCollectionId == duesItemId
                     select xx).FirstOrDefault();
         if (dues != null)
         {
             FeesCollected collected = new FeesCollected();
             collected.FeeCollected = 0.00;
             collected.IsFeeWaived = true;
             collected.FeeItem = dues;
             collected.MemberPaid = dc.Members.Where(x => x.MemberId == memberIdPaid).FirstOrDefault();
             collected.Note = note;
             collected.IsPaidInFull = true;
             dues.FeesCollected.Add(collected);
             int c = dc.SaveChanges();
             return c > 0;
         }
     }
     catch (Exception exception)
     {
         ErrorDatabaseManager.AddException(exception, exception.GetType());
     }
     return false;
 }
 public static bool RemoveDuesWaived(long duesItemId, Guid duesManagementId, Guid memberIdPaid)
 {
     try
     {
         var dc = new ManagementContext();
         var dues = (from xx in dc.FeeItem
                     where xx.FeeManagedBy.FeeManagementId == duesManagementId
                     where xx.FeeCollectionId == duesItemId
                     select xx).FirstOrDefault();
         if (dues != null)
         {
             var feeCollected = dues.FeesCollected.Where(x => x.MemberPaid.MemberId == memberIdPaid).ToList();
             foreach (var col in feeCollected)
             {
                 col.IsFeeWaived = false;
                 col.IsPaidInFull = false;
                 col.WasClearedByUser = true;
                 //if (col.FeeCollected == 0.0)
                     //dues.FeesCollected.Remove(col);
             }
             if (feeCollected.Count == 0)
             {
                 FeesCollected col = new FeesCollected();
                 col.MemberPaid = dc.Members.Where(x => x.MemberId == memberIdPaid).FirstOrDefault();
                 col.FeeCollected = 0.0;
                 col.FeeItem = dues;
                 col.WasClearedByUser = true;
                 dues.FeesCollected.Add(col);
             }
             int c = dc.SaveChanges();
             return c > 0;
         }
     }
     catch (Exception exception)
     {
         ErrorDatabaseManager.AddException(exception, exception.GetType());
     }
     return false;
 }
        public static bool PayDuesAmount(long duesItemId, Guid duesManagementId, double amountPaid, Guid memberIdPaid, string note)
        {
            try
            {
                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)
                {
                    double due = 0.0;
                    bool DoesNotPayDues = false;

                    FeesCollected collected = new FeesCollected();
                    collected.FeeCollected = amountPaid;
                    collected.FeeItem = dues;
                    collected.MemberPaid = dc.Members.Where(x => x.MemberId == memberIdPaid).FirstOrDefault();
                    collected.Note = note;
                    double totalPaid = amountPaid;

                    var fees = dues.FeesCollected.Where(x => x.MemberPaid.MemberId == memberIdPaid);
                    var required = dues.FeesRequired.Where(x => x.MemberRequiredFrom.MemberId == memberIdPaid).FirstOrDefault();
                    var classification = dues.FeeManagedBy.FeeClassifications.Where(x => x.MembersClassified.Where(y => y.Member.MemberId == memberIdPaid).Count() > 0).FirstOrDefault();

                    if (required != null)
                        due = required.FeeRequired;
                    else if (classification != null)
                    {
                        DoesNotPayDues = classification.DoesNotPayDues;
                        due = classification.FeeRequired;
                    }
                    else
                        due = dues.CostOfFee;

                    foreach (var fee in fees)
                    {
                        totalPaid += fee.FeeCollected;
                    }
                    if (totalPaid == due || totalPaid > due)
                    {
                        collected.IsPaidInFull = true;
                        foreach (var fee in fees)
                        {
                            fee.IsPaidInFull = true;
                        }
                    }
                    dues.FeesCollected.Add(collected);
                    int c = dc.SaveChanges();
                    return c > 0;
                }
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return false;
        }