131 lines
4.2 KiB
PHP
131 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace Modules\Platform\app\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Support\Collection;
|
|
use Modules\Platform\app\Models\Company;
|
|
use Modules\Platform\app\Models\LicenseKey;
|
|
use Modules\Platform\app\Models\Payment;
|
|
use Modules\Platform\app\Models\PlatformPrice;
|
|
use Modules\Warehouse\app\Models\Warehouse;
|
|
|
|
class PlatformDashboardController extends Controller
|
|
{
|
|
/**
|
|
* Show the platform administration dashboard.
|
|
*/
|
|
public function __invoke(): View
|
|
{
|
|
$companies = Company::withCount(['users', 'warehouses', 'licenseKeys'])
|
|
->latest()
|
|
->limit(8)
|
|
->get();
|
|
|
|
$companiesCount = Company::count();
|
|
$activeCompaniesCount = Company::where('status', 'active')->count();
|
|
$licensesCount = LicenseKey::count();
|
|
$warehousesCount = Warehouse::count();
|
|
$pricesCount = PlatformPrice::count();
|
|
|
|
return view('platform::dashboard.index', [
|
|
'companiesCount' => $companiesCount,
|
|
'activeCompaniesCount' => $activeCompaniesCount,
|
|
'licensesCount' => $licensesCount,
|
|
'paymentsTotal' => Payment::where('status', 'paid')->sum('amount'),
|
|
'warehousesCount' => $warehousesCount,
|
|
'pricesCount' => $pricesCount,
|
|
'prices' => PlatformPrice::query()
|
|
->where('is_active', true)
|
|
->ordered()
|
|
->get(),
|
|
'companies' => $companies,
|
|
|
|
'companyUsageChartLabels' => $companies->isNotEmpty()
|
|
? $companies->pluck('name')->values()->all()
|
|
: ['MPBA', 'Abbott Demo', 'North Hub', 'South Hub'],
|
|
|
|
'companyUsageChartSeries' => $companies->isNotEmpty()
|
|
? $this->companyUsageChartSeries($companies)
|
|
: $this->exampleCompanyUsageChartSeries(),
|
|
|
|
'platformMixChartLabels' => ['Companies', 'Warehouses', 'Prices', 'Licences'],
|
|
|
|
'platformMixChartValues' => $this->hasPlatformMixData(
|
|
$companiesCount,
|
|
$warehousesCount,
|
|
$pricesCount,
|
|
$licensesCount
|
|
)
|
|
? [$companiesCount, $warehousesCount, $pricesCount, $licensesCount]
|
|
: [4, 11, 4, 9],
|
|
|
|
'companyStatusChartLabels' => ['Active', 'Inactive'],
|
|
|
|
'companyStatusChartValues' => $companiesCount > 0
|
|
? [$activeCompaniesCount, max($companiesCount - $activeCompaniesCount, 0)]
|
|
: [3, 1],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Build the company usage chart series.
|
|
*
|
|
* @param Collection<int, Company> $companies
|
|
* @return array<int, array{label: string, data: array<int, int>}>
|
|
*/
|
|
private function companyUsageChartSeries(Collection $companies): array
|
|
{
|
|
return [
|
|
[
|
|
'label' => 'Users',
|
|
'data' => $companies->pluck('users_count')->values()->all(),
|
|
],
|
|
[
|
|
'label' => 'Warehouses',
|
|
'data' => $companies->pluck('warehouses_count')->values()->all(),
|
|
],
|
|
[
|
|
'label' => 'Licences',
|
|
'data' => $companies->pluck('license_keys_count')->values()->all(),
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Build example company usage chart series for an empty platform.
|
|
*
|
|
* @return array<int, array{label: string, data: array<int, int>}>
|
|
*/
|
|
private function exampleCompanyUsageChartSeries(): array
|
|
{
|
|
return [
|
|
[
|
|
'label' => 'Users',
|
|
'data' => [14, 9, 6, 4],
|
|
],
|
|
[
|
|
'label' => 'Warehouses',
|
|
'data' => [5, 3, 2, 1],
|
|
],
|
|
[
|
|
'label' => 'Licences',
|
|
'data' => [4, 2, 2, 1],
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Determine whether the platform mix chart has real data.
|
|
*/
|
|
private function hasPlatformMixData(
|
|
int $companiesCount,
|
|
int $warehousesCount,
|
|
int $pricesCount,
|
|
int $licensesCount
|
|
): bool {
|
|
return ($companiesCount + $warehousesCount + $pricesCount + $licensesCount) > 0;
|
|
}
|
|
}
|