public MonthlyStatementsFactory CalculateAndSavePayouts()
        {

            double authorsCut = Statement.TotalProfitMade * PAYOUTPERCENTAGETOAUTHORS;

            //removes SpoiledTechies Posts cause he doesn't want to get paid.
            //removes posts that aren't allowed to pay, mainly sponsors.
            var allPosts = PostsToPay.Where(x => x.DisablePaymentsForPost == false).Where(x => x.AuthorUserId == ServerConfig.DEFAULT_JAMIES_USER_ID || x.AuthorUserId == ServerConfig.DEFAULT_SCOTTS_USER_ID || x.AuthorUserId == ServerConfig.DEFAULT_ADMIN_USER_ID).ToList();
            foreach (var post in allPosts)
            {
                PostsToPay.Remove(post);
            }


            //gets the total views of all the posts in this month.
            double totalViews = PostsToPay.Sum(x => x.TotalMonthlyViews);
            //caculates the post percentages.
            foreach (var post in PostsToPay)
            {
                post.PercentageOfTotalViews = (double)post.TotalMonthlyViews / totalViews;
            }
            TotalPayments = (from p in PostsToPay
                             group p by p.AuthorUserId into g
                             select new TotalPayment
                             {
                                 UserId = g.Key,
                                 TotalPercentageBeingPaid = g.Sum(x => x.PercentageOfTotalViews),
                                 TotalPageViewsThisMonth = g.Sum(x => x.TotalMonthlyViews)
                             }).ToList();

            var dc = new RNManagementContext();
            MonthlyStatement newMonth = new MonthlyStatement();
            newMonth.StatementDateTime = DateTime.UtcNow;
            newMonth.TotalPageViews = (long)totalViews;
            newMonth.TotalProfitMade = Statement.TotalProfitMade;
            newMonth.TotalWritersPaid = TotalPayments.Count;
            newMonth.TotalWritersPayoutProfit = authorsCut;
            dc.MonthlyStatements.Add(newMonth);


            foreach (var group in TotalPayments)
            {
                //add the funds to the account.
                var fund = dc.Funds.Where(x => x.UserId == group.UserId).FirstOrDefault();
                group.TotalAddedToAccount = (group.TotalPercentageBeingPaid * authorsCut);
                if (fund != null)
                {
                    fund.ActiveInUserAccount += group.TotalAddedToAccount;
                    group.TotalActiveInAccount = fund.ActiveInUserAccount;

                }
                else
                {
                    FundsForWriter f = new FundsForWriter();
                    f.ActiveInUserAccount = group.TotalAddedToAccount;
                    f.UserId = group.UserId;
                    dc.Funds.Add(f);
                    group.TotalActiveInAccount = f.ActiveInUserAccount;

                }
            }
            int c = dc.SaveChanges();

            return this;
        }
 public bool AddNewStatement(Statement statement)
 {
     var dc = new RNManagementContext();
     MonthlyStatement s = new MonthlyStatement();
     s.StatementDateTime = DateTime.UtcNow;
     s.TotalPageViews = statement.TotalPageViews;
     s.TotalProfitMade = statement.TotalProfitMade;
     s.TotalWritersPaid = statement.TotalWritersPaid;
     s.TotalWritersPayoutProfit = statement.TotalWritersPayoutProfit;
     dc.MonthlyStatements.Add(s);
     int c = dc.SaveChanges();
     return c > 0;
 }