Beispiel #1
0
        public static TendencyToken Value2TendencyToken(this decimal value1, decimal?value2)
        {
            var tendency = new TendencyToken();

            if (value2 == null)
            {
                tendency.Direction = ReportEnums.eTendencyDirections.Up;
                tendency.Percent   = 100;
            }
            else
            {
                var difference = value1 - value2;

                if (difference == 0)
                {
                    tendency.Direction = ReportEnums.eTendencyDirections.Equal;
                    tendency.Percent   = 0;
                }
                else
                {
                    tendency.Direction = difference > 0 ? ReportEnums.eTendencyDirections.Up : ReportEnums.eTendencyDirections.Down;

                    // ReSharper disable once PossibleLossOfFraction
                    tendency.Percent = (value2 > 0 ? Math.Abs(100 - Math.Round((decimal)(value1 * 100 / value2), 0)) : 0) * (difference > 0 ? 1 : -1);
                }
            }
            tendency.Tooltip = (value2 != null ? value2.FormatdDecimal(2) : 0).ToString();
            return(tendency);
        }
        public AdminDashboardToken GetAdminDashboardToken()
        {
            try
            {
                var now = DateTime.Now;

                var prev = now.AddMonths(-1);

                var rows = _payoutServices.GetPayoutCurrencySummaryRows(now.Year, now.Month, null, null, -1);

                var previous = _payoutServices.GetPayoutCurrencySummaryRows(prev.Year, prev.Month, null, null, -1);

                var p = new List <AdminPayoutToken>();

                var sellerTop = new TopSellersToken();

                foreach (var token in rows)
                {
                    var pt = token.PayoutCurrencySummaryDto2AdminPayoutToken();

                    var row = previous.FirstOrDefault(x => x.Currency.CurrencyId == token.Currency.CurrencyId);

                    var tendency = new TendencyToken();

                    if (row == null)
                    {
                        tendency.Direction = ReportEnums.eTendencyDirections.Up;
                        tendency.Percent   = 100;
                    }
                    else
                    {
                        if (token.Currency.CurrencyId == USD_CURRENCY_ID || token.Currency.CurrencyId == EUR_CURRENCY_ID)
                        {
                            var top = row.Rows.OrderByDescending(x => x.TotalSales).Take(Math.Min(row.Rows.Count, 5)).Select(x => new AuthorPayoutToken
                            {
                                Seller     = x.Seller
                                , Currency = x.Currency
                                , Sales    = x.TotalSales
                            }).ToList();
                            if (top.Count < 5)
                            {
                                var toAdd = 5 - top.Count;

                                for (var i = 0; i < toAdd; i++)
                                {
                                    top.Add(new AuthorPayoutToken {
                                        Currency = token.Currency
                                    });
                                }
                            }

                            switch (token.Currency.CurrencyId)
                            {
                            case USD_CURRENCY_ID:
                                sellerTop.Top_USD = top;
                                break;

                            case EUR_CURRENCY_ID:
                                sellerTop.Top_EUR = top;
                                break;
                            }
                        }

                        var ptr = row.PayoutCurrencySummaryDto2AdminPayoutToken();

                        var revenueChange = pt.TotalRevenue - ptr.TotalRevenue;

                        if (revenueChange == 0)
                        {
                            tendency.Direction = ReportEnums.eTendencyDirections.Equal;
                            tendency.Percent   = 0;
                        }
                        else
                        {
                            tendency.Direction = revenueChange > 0 ? ReportEnums.eTendencyDirections.Up : ReportEnums.eTendencyDirections.Down;

                            tendency.Percent = (ptr.TotalRevenue > 0 ? Math.Abs(100 - Math.Round((pt.TotalRevenue * 100 / ptr.TotalRevenue), 0)) : 0) * (revenueChange > 0 ? 1 : -1);
                        }
                    }

                    pt.Tendency = tendency;

                    p.Add(pt);
                }

                AdjustTopSellers(sellerTop);

                return(new AdminDashboardToken
                {
                    NextPayoutList = p
                    , TopSellers = sellerTop
                    , VideoStats = GetVideoStats()
                    , AuthorTotalStats = GetAuthorTotalStats()
                });
            }
            catch (Exception ex)
            {
                Logger.Error("Get Admin Dashboard token", ex, CommonEnums.LoggerObjectTypes.Reports);

                return(new AdminDashboardToken {
                    IsValid = false, Message = FormatError(ex)
                });
            }
        }