Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

15.1 KB
Raw Blame
@using Grand.Business.Core.Interfaces.Catalog.Prices
@using Grand.Business.Core.Interfaces.Common.Directory
@using Grand.Business.Core.Interfaces.Common.Security
@using Grand.Business.Core.Interfaces.Customers
@using Grand.Business.Core.Interfaces.System.Reports
@using Grand.Domain.Permissions
@using Grand.Domain.Customers
@inject CustomerSettings customerSettings
@inject IPermissionService permissionService
@inject ICustomerService customerService
@inject IOrderReportService orderReportService
@inject IPriceFormatter priceFormatter
@inject IContextAccessor contextAccessor
@inject ICurrencyService currencyService
@inject IGroupService groupService
@functions {
    //resources added in 2.4 arrive with the upgrade; until they are imported the English text is
    //shown instead of the raw key
    string Text(string key, string fallback)
    {
        var value = Loc[key].Value;
        return string.IsNullOrEmpty(value) || string.Equals(value, key, StringComparison.OrdinalIgnoreCase) ? fallback : value;
    }
}
@{
    //page title
    ViewBag.Title = Loc["Admin.Dashboard"];
    var isAdminAccess = await groupService.IsAdmin(contextAccessor.WorkContext.CurrentCustomer);
    var canAccessReports = await permissionService.Authorize(StandardPermission.ManageReports);
    var canManageOrders = await permissionService.Authorize(StandardPermission.ManageOrders);
    var customersOnline = 0;
    var customersShoppingCart = 0;
    var ordersAmount = string.Empty;
    var ordersCount = 0;
    if (canManageOrders)
    {
        customersOnline = (await customerService.GetOnlineCustomers(DateTime.UtcNow.AddMinutes(-customerSettings.OnlineCustomerMinutes), null, contextAccessor.WorkContext.CurrentCustomer.StaffStoreId, contextAccessor.WorkContext.CurrentCustomer.SeId, 1, 1)).TotalCount;
        customersShoppingCart = await customerService.GetCountOnlineShoppingCart(DateTime.UtcNow.AddMinutes(-customerSettings.OnlineShoppingCartMinutes), contextAccessor.WorkContext.CurrentCustomer.StaffStoreId, contextAccessor.WorkContext.CurrentCustomer.SeId);
        var ordersToday = await orderReportService.GetOrderPeriodReport(0, contextAccessor.WorkContext.CurrentCustomer.StaffStoreId, contextAccessor.WorkContext.CurrentCustomer.SeId);
        ordersAmount = priceFormatter.FormatPrice(ordersToday.Amount, await currencyService.GetPrimaryStoreCurrency());
        ordersCount = ordersToday.Count;
    }
}

<div class="grand-dashboard">
    <vc:admin-widget widget-zone="admin_dashboard_top" additional-data="null"/>

    <header class="grand-dashboard__header">
        <h1 class="grand-dashboard__title">@Loc["Admin.Dashboard"]</h1>
        @if (canManageOrders)
        {
            <button type="button" id="dashboard-report-range" class="btn grand-dashboard__range" title="@Loc["Admin.Dashboard.Summary"]">
                <i class="bi bi-calendar3" aria-hidden="true"></i>
                <span class="grand-dashboard__range-text"></span>
                <i class="bi bi-chevron-down" aria-hidden="true"></i>
            </button>
        }
    </header>

    <vc:admin-page system-name="AdminPortalInfo"/>

    @if (canManageOrders)
    {
        <div class="row g-3 top_tiles grand-dashboard__stats">
            <div class="col-12 col-sm-6 col-xl-4">
                <div class="grand-stat grand-stat--visitors">
                    <span class="grand-stat__icon" aria-hidden="true"><i class="bi bi-people"></i></span>
                    <div class="grand-stat__body">
                        <a class="grand-stat__label stretched-link" href="@Url.Action("List", "OnlineCustomer", new { area = Constants.AreaAdmin })">
                            @Loc["Admin.Dashboard.OnlineCustomers"]
                        </a>
                        <div class="grand-stat__value">@customersOnline</div>
                        <div class="grand-stat__hint">@Loc["Admin.Dashboard.OnlineCustomerMinutes", customerSettings.OnlineCustomerMinutes]</div>
                    </div>
                </div>
            </div>
            <div class="col-12 col-sm-6 col-xl-4">
                <div class="grand-stat grand-stat--carts">
                    <span class="grand-stat__icon" aria-hidden="true"><i class="bi bi-cart3"></i></span>
                    <div class="grand-stat__body">
                        <a class="grand-stat__label stretched-link" href="@Url.Action("CurrentCarts", "ShoppingCart", new { area = Constants.AreaAdmin })">
                            @Loc["Admin.Dashboard.ActiveShoppingCarts"]
                        </a>
                        <div class="grand-stat__value">@customersShoppingCart</div>
                        <div class="grand-stat__hint">@Loc["Admin.Dashboard.OnlineShoppingCart", customerSettings.OnlineShoppingCartMinutes]</div>
                    </div>
                </div>
            </div>
            <div class="col-12 col-sm-12 col-xl-4">
                <div class="grand-stat grand-stat--orders">
                    <span class="grand-stat__icon" aria-hidden="true"><i class="bi bi-bag-check"></i></span>
                    <div class="grand-stat__body">
                        <a class="grand-stat__label stretched-link" href="@Url.Action("List", "Order", new { startDate = DateTime.UtcNow.ToUniversalTime(), area = Constants.AreaAdmin })">
                            @Loc["Admin.Dashboard.TodayOrders"]
                        </a>
                        <div class="grand-stat__value">
                            <span>@ordersAmount</span>
                            <span class="grand-stat__value-secondary">/ @ordersCount</span>
                        </div>
                        <div class="grand-stat__hint">@Loc["Admin.Dashboard.TodayOrders.AmountCount"]</div>
                    </div>
                </div>
            </div>
        </div>

        @if (canAccessReports || isAdminAccess)
        {
            <div class="row g-3">
                @if (canAccessReports)
                {
                    <div class="col-12 @(isAdminAccess ? "col-xl-8" : "")">
                        <section class="grand-card h-100" aria-labelledby="dashboard-customers-chart">
                            <header class="grand-card__header">
                                <i class="bi bi-person-plus grand-card__icon" aria-hidden="true"></i>
                                <h2 id="dashboard-customers-chart" class="grand-card__title">@Loc["Admin.Dashboard.ReportCustomerTime.TitleChart"]</h2>
                            </header>
                            <div class="grand-card__body">
                                @await Component.InvokeAsync("CustomerReportTimeChart")
                            </div>
                        </section>
                    </div>
                }
                @if (isAdminAccess)
                {
                    <div class="col-12 @(canAccessReports ? "col-xl-4" : "")">
                        <section class="grand-card h-100" aria-labelledby="dashboard-activity">
                            <header class="grand-card__header">
                                <i class="bi bi-activity grand-card__icon" aria-hidden="true"></i>
                                <h2 id="dashboard-activity" class="grand-card__title">@Loc["Admin.Dashboard.ActivityOverview"]</h2>
                            </header>
                            <div class="grand-card__body grand-card__body--flush">
                                <div class="partialContents" data-url="@Url.Action("DashboardActivity", new { area = Constants.AreaAdmin })">
                                    <div class="grand-dashboard__loading" role="status">
                                        <span class="spinner-border spinner-border-sm" aria-hidden="true"></span>
                                        <span>@Text("Admin.Dashboard.Loading", "Loading...")</span>
                                    </div>
                                </div>
                            </div>
                        </section>
                    </div>
                }
            </div>
        }

        @if (canAccessReports)
        {
            <section class="grand-card" aria-labelledby="dashboard-orders-chart">
                <header class="grand-card__header">
                    <i class="bi bi-graph-up-arrow grand-card__icon" aria-hidden="true"></i>
                    <h2 id="dashboard-orders-chart" class="grand-card__title">@Loc["Admin.Dashboard.ReportOrderTime.TitleChart"]</h2>
                </header>
                <div class="grand-card__body">
                    @await Component.InvokeAsync("OrderTimeChart")
                </div>
            </section>
        }

        <section class="grand-card" aria-labelledby="dashboard-latest-orders">
            <header class="grand-card__header">
                <i class="bi bi-receipt grand-card__icon" aria-hidden="true"></i>
                <h2 id="dashboard-latest-orders" class="grand-card__title">@Loc["Admin.Dashboard.ReportLatestOrder"]</h2>
                <a class="grand-card__action" href="@Url.Action("List", "Order", new { area = Constants.AreaAdmin })">
                    @Loc["Admin.Dashboard.ViewMore"]
                    <i class="bi bi-arrow-right grand-card__action-arrow" aria-hidden="true"></i>
                </a>
            </header>
            <div class="grand-card__body">
                @await Component.InvokeAsync("LatestOrder")
            </div>
        </section>

        <section class="grand-card" id="store_news_card" aria-labelledby="dashboard-news" hidden>
            <header class="grand-card__header">
                <i class="bi bi-megaphone grand-card__icon" aria-hidden="true"></i>
                <h2 id="dashboard-news" class="grand-card__title">@Text("Admin.Dashboard.News", "News from GrandNode")</h2>
            </header>
            <div class="grand-card__body">
                <ul id="store_news" class="grand-news"></ul>
            </div>
        </section>
    }

    <vc:admin-widget widget-zone="admin_dashboard_bottom" additional-data="null"/>
</div>
<script>
    $(document).ready(function (e) {
        var site = site || {};
        site.baseUrl = site.baseUrl || "";
        $(".partialContents").each(function (index, item) {
            var url = site.baseUrl + $(item).data("url");
            if (url && url.length > 0) {
                $(item).load(url);
            }
        });
    });

    //grandnode.com news: the message is shown as text - remote markup is never put into the panel
    (function () {
        var list = document.getElementById('store_news');
        if (!list) return;
        $.getJSON("https://grandnode.com/news.json", function (data) {
            var count = 0;
            $.each(data || [], function (key, value) {
                if (!value || !value.Name) return;
                var item = document.createElement('li');
                item.className = 'grand-news__item';
                var title = document.createElement('h3');
                title.className = 'grand-news__title';
                var url = String(value.Url || '');
                if (/^https?:\/\//i.test(url)) {
                    var link = document.createElement('a');
                    link.href = url;
                    link.target = '_blank';
                    link.rel = 'noopener noreferrer';
                    link.textContent = value.Name;
                    title.appendChild(link);
                } else {
                    title.textContent = value.Name;
                }
                var text = document.createElement('p');
                text.className = 'grand-news__text';
                text.textContent = new DOMParser().parseFromString(String(value.Message || ''), 'text/html').body.textContent.trim();
                item.appendChild(title);
                item.appendChild(text);
                list.appendChild(item);
                count++;
            });
            if (count > 0) document.getElementById('store_news_card').hidden = false;
        });
    })();

    var Dashboard = function () {
        return {
            initDashboardDaterange: function () {
                if (!jQuery().daterangepicker || !$('#dashboard-report-range').length) {
                    return;
                }

                $('#dashboard-report-range').daterangepicker({
                    startDate: moment().startOf('month'),
                    "ranges": {
                        'Today': [moment(), moment()],
                        'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                        'Last 7 Days': [moment().subtract(6, 'days'), moment()],
                        'Last 30 Days': [moment().subtract(29, 'days'), moment()],
                        'This Month': [moment().startOf('month'), moment().endOf('month')],
                        'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
                    },
                    "locale": {
                        "format": "MM/DD/YYYY",
                        "separator": " - ",
                        "applyLabel": "Apply",
                        "cancelLabel": "Cancel",
                        "fromLabel": "From",
                        "toLabel": "To",
                        "customRangeLabel": "Custom",
                        "daysOfWeek": [
                            "Su",
                            "Mo",
                            "Tu",
                            "We",
                            "Th",
                            "Fr",
                            "Sa"
                        ],
                        "monthNames": [
                            "January",
                            "February",
                            "March",
                            "April",
                            "May",
                            "June",
                            "July",
                            "August",
                            "September",
                            "October",
                            "November",
                            "December"
                        ],
                        "firstDay": 1
                    },
                    opens: ($('html').css('direction') === 'rtl' ? 'right' : 'left'),
                }, function (start, end, label) {
                    $('#dashboard-report-range .grand-dashboard__range-text').text(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
                });

                $('#dashboard-report-range .grand-dashboard__range-text').text(moment().startOf('month').format('MMMM D, YYYY') + ' - ' + moment().format('MMMM D, YYYY'));
                $('#dashboard-report-range').show();
            },

            init: function () {
                this.initDashboardDaterange();
            }
        }
    }();

    Dashboard.init();

    $('#dashboard-report-range').on('apply.daterangepicker', function (ev, picker) {
        if ($.isFunction(window.initReportCustomerTimeChart)) {
            initReportCustomerTimeChart(false);
        };
        if ($.isFunction(window.initReportOrderTimeChart)) {
            initReportOrderTimeChart(false);
        };
        var gridOrders = GrandAdmin.grids.get('#latest-orders-grid');
        if (gridOrders != undefined) {
            gridOrders.dataSource.read();
        }
    });</script>