Пример #1
0
        public static bool SaveCurrentPaypalBitcoinInformation(Fund fund)
        {

            var dc = new RNManagementContext();
            var funds = dc.Funds.Where(x => x.UserId == fund.UserId).FirstOrDefault();
            if (funds == null)
            {
                FundsForWriter f = new FundsForWriter();
                f.BitCoinId = fund.BitCoinId;
                f.PaypalAddress = fund.PaypalAddress;

                f.UserId = fund.UserId;
                dc.Funds.Add(f);
            }
            else
            {
                funds.PaypalAddress = fund.PaypalAddress;
                funds.BitCoinId = fund.BitCoinId;
            }
            int c = dc.SaveChanges();

            return c > 0;
        }
        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;
        }