public CommissionSummaryViewModel(DataSet dataset)
        {
            // Divide our dataset into separate tables for easy reading
            var commissionEarningsData = dataset.Tables[0];
            var commissionBonusesData = dataset.Tables[1];

            // Populate our details collections
            CommissionEarningsDetails = new List<CommissionEarningsDetail>();
            foreach (DataRow row in commissionEarningsData.Rows)
            {
                var detail                      = new CommissionEarningsDetail();
                detail.CommissionRunID          = Convert.ToInt32(row["CommissionRunID"]);
                detail.CommissionRunDescription = row["CommissionRunDescription"].ToString();
                detail.AcceptedDate             = Convert.ToDateTime(row["AcceptedDate"]);
                detail.PeriodTypeID             = Convert.ToInt32(row["PeriodTypeID"]);
                detail.PaidRankID               = Convert.ToInt32(row["PaidRankID"]);
                detail.PaidRankDescription      = row["PaidRankDescription"].ToString();
                detail.Earnings                 = Convert.ToDecimal(row["Earnings"]);
                detail.PaidAsRankCount          = Convert.ToInt32(row["PaidAsRankCount"]);
                CommissionEarningsDetails.Add(detail);
            }

            CommissionBonusesDetails = new List<CommissionBonusDetail>();
            foreach (DataRow row in commissionBonusesData.Rows)
            {
                var detail                      = new CommissionBonusDetail();
                detail.CommissionRunID          = Convert.ToInt32(row["CommissionRunID"]);
                detail.CommissionRunDescription = row["CommissionRunDescription"].ToString();
                detail.AcceptedDate             = Convert.ToDateTime(row["AcceptedDate"]);
                detail.PeriodTypeID             = Convert.ToInt32(row["PeriodTypeID"]);
                detail.BonusID                  = Convert.ToInt32(row["BonusID"]);
                detail.BonusDescription         = row["BonusDescription"].ToString();
                detail.BonusAmount              = Convert.ToDecimal(row["BonusAmount"]);
                CommissionBonusesDetails.Add(detail);
            }

            // Get the weekly totals
            var weeklyDetailsByRunID =
                CommissionEarningsDetails
                    .Where(c => c.PeriodTypeID == (int)PeriodTypes.Weekly)
                    .GroupBy(c => new { c.CommissionRunID, c.AcceptedDate }, (key, group) => new
                    {
                        CommissionRunID = key.CommissionRunID,
                        AcceptedDate    = key.AcceptedDate,
                        Payout          = group.Sum(a => a.Earnings)
                    });

            var orderedWeeklyPayouts = weeklyDetailsByRunID.OrderByDescending(c => c.Payout);
            if (orderedWeeklyPayouts.Count() > 0)
            {
                HighestWeeklyPayout     = orderedWeeklyPayouts.First().Payout;
                HighestWeeklyPayoutDate = orderedWeeklyPayouts.First().AcceptedDate;
                LowestWeeklyPayout      = orderedWeeklyPayouts.Last().Payout;
                LowestWeeklyPayoutDate  = orderedWeeklyPayouts.Last().AcceptedDate;
                AverageWeeklyPayout     = orderedWeeklyPayouts.Average(c => c.Payout);
            }

            // Get the monthly totals
            var monthlyDetailsByRunID =
                CommissionEarningsDetails
                        .Where(c => c.PeriodTypeID == (int)PeriodTypes.Weekly)
                        .GroupBy(c => new { c.CommissionRunID, c.AcceptedDate }, (key, group) => new
                        {
                            CommissionRunID = key.CommissionRunID,
                            AcceptedDate    = key.AcceptedDate,
                            Payout          = group.Sum(a => a.Earnings)
                        });

            var orderedMonthlyPayouts = monthlyDetailsByRunID.OrderByDescending(c => c.Payout);
            if (orderedMonthlyPayouts.Count() > 0)
            {
                HighestMonthlyPayout     = orderedMonthlyPayouts.First().Payout;
                HighestMonthlyPayoutDate = orderedMonthlyPayouts.First().AcceptedDate;
                LowestMonthlyPayout      = orderedMonthlyPayouts.Last().Payout;
                LowestMonthlyPayoutDate  = orderedMonthlyPayouts.Last().AcceptedDate;
                AverageMonthlyPayout     = orderedMonthlyPayouts.Average(c => c.Payout);
            }

            // Charts
            PopulateCommissionBonusesChartXml();
        }
        public CommissionSummaryViewModel(DataSet dataset)
        {
            // Divide our dataset into separate tables for easy reading
            var commissionEarningsData = dataset.Tables[0];
            var commissionBonusesData  = dataset.Tables[1];


            // Populate our details collections
            CommissionEarningsDetails = new List <CommissionEarningsDetail>();
            foreach (DataRow row in commissionEarningsData.Rows)
            {
                var detail = new CommissionEarningsDetail();
                detail.CommissionRunID          = Convert.ToInt32(row["CommissionRunID"]);
                detail.CommissionRunDescription = row["CommissionRunDescription"].ToString();
                detail.AcceptedDate             = Convert.ToDateTime(row["AcceptedDate"]);
                detail.PeriodTypeID             = Convert.ToInt32(row["PeriodTypeID"]);
                detail.PaidRankID          = Convert.ToInt32(row["PaidRankID"]);
                detail.PaidRankDescription = row["PaidRankDescription"].ToString();
                detail.Earnings            = Convert.ToDecimal(row["Earnings"]);
                detail.PaidAsRankCount     = Convert.ToInt32(row["PaidAsRankCount"]);
                CommissionEarningsDetails.Add(detail);
            }

            CommissionBonusesDetails = new List <CommissionBonusDetail>();
            foreach (DataRow row in commissionBonusesData.Rows)
            {
                var detail = new CommissionBonusDetail();
                detail.CommissionRunID          = Convert.ToInt32(row["CommissionRunID"]);
                detail.CommissionRunDescription = row["CommissionRunDescription"].ToString();
                detail.AcceptedDate             = Convert.ToDateTime(row["AcceptedDate"]);
                detail.PeriodTypeID             = Convert.ToInt32(row["PeriodTypeID"]);
                detail.BonusID          = Convert.ToInt32(row["BonusID"]);
                detail.BonusDescription = row["BonusDescription"].ToString();
                detail.BonusAmount      = Convert.ToDecimal(row["BonusAmount"]);
                CommissionBonusesDetails.Add(detail);
            }

            // Get the weekly totals
            var weeklyDetailsByRunID =
                CommissionEarningsDetails
                .Where(c => c.PeriodTypeID == (int)PeriodTypes.Weekly)
                .GroupBy(c => new { c.CommissionRunID, c.AcceptedDate }, (key, group) => new
            {
                CommissionRunID = key.CommissionRunID,
                AcceptedDate    = key.AcceptedDate,
                Payout          = group.Sum(a => a.Earnings)
            });

            var orderedWeeklyPayouts = weeklyDetailsByRunID.OrderByDescending(c => c.Payout);

            if (orderedWeeklyPayouts.Count() > 0)
            {
                HighestWeeklyPayout     = orderedWeeklyPayouts.First().Payout;
                HighestWeeklyPayoutDate = orderedWeeklyPayouts.First().AcceptedDate;
                LowestWeeklyPayout      = orderedWeeklyPayouts.Last().Payout;
                LowestWeeklyPayoutDate  = orderedWeeklyPayouts.Last().AcceptedDate;
                AverageWeeklyPayout     = orderedWeeklyPayouts.Average(c => c.Payout);
            }


            // Get the monthly totals
            var monthlyDetailsByRunID =
                CommissionEarningsDetails
                .Where(c => c.PeriodTypeID == (int)PeriodTypes.Weekly)
                .GroupBy(c => new { c.CommissionRunID, c.AcceptedDate }, (key, group) => new
            {
                CommissionRunID = key.CommissionRunID,
                AcceptedDate    = key.AcceptedDate,
                Payout          = group.Sum(a => a.Earnings)
            });


            var orderedMonthlyPayouts = monthlyDetailsByRunID.OrderByDescending(c => c.Payout);

            if (orderedMonthlyPayouts.Count() > 0)
            {
                HighestMonthlyPayout     = orderedMonthlyPayouts.First().Payout;
                HighestMonthlyPayoutDate = orderedMonthlyPayouts.First().AcceptedDate;
                LowestMonthlyPayout      = orderedMonthlyPayouts.Last().Payout;
                LowestMonthlyPayoutDate  = orderedMonthlyPayouts.Last().AcceptedDate;
                AverageMonthlyPayout     = orderedMonthlyPayouts.Average(c => c.Payout);
            }



            // Charts
            PopulateCommissionBonusesChartXml();
        }