fixed tests
This commit is contained in:
@@ -4,7 +4,7 @@ namespace Modules\Platform\app\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Modules\Platform\app\Models\Company;
|
||||
use Modules\Platform\app\Models\LicenseKey;
|
||||
use Modules\Platform\app\Models\Payment;
|
||||
@@ -13,118 +13,51 @@ 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()
|
||||
$companies = Company::query()
|
||||
->withCount(['users', 'warehouses', 'licenseKeys'])
|
||||
->latest('id')
|
||||
->limit(8)
|
||||
->get();
|
||||
|
||||
$companiesCount = Company::count();
|
||||
$activeCompaniesCount = Company::where('status', 'active')->count();
|
||||
$licensesCount = LicenseKey::count();
|
||||
$warehousesCount = Warehouse::count();
|
||||
$pricesCount = PlatformPrice::count();
|
||||
$prices = PlatformPrice::query()
|
||||
->when(Schema::hasColumn('platform_prices', 'is_active'), static function ($query): void {
|
||||
$query->where('is_active', true);
|
||||
})
|
||||
->when(Schema::hasColumn('platform_prices', 'active'), static function ($query): void {
|
||||
$query->where('active', true);
|
||||
})
|
||||
->orderBy('sort_order')
|
||||
->orderBy('name')
|
||||
->get();
|
||||
|
||||
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(),
|
||||
'companiesCount' => Company::query()->count(),
|
||||
'activeCompaniesCount' => Company::query()->where('status', 'active')->count(),
|
||||
'licensesCount' => LicenseKey::query()->count(),
|
||||
'warehousesCount' => Warehouse::query()->count(),
|
||||
'pricesCount' => PlatformPrice::query()->count(),
|
||||
'paymentsTotal' => Payment::query()->where('status', 'paid')->sum('amount'),
|
||||
'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],
|
||||
|
||||
'prices' => $prices,
|
||||
'companyUsageChartLabels' => $companies->pluck('name')->values(),
|
||||
'companyUsageChartSeries' => [
|
||||
'Users' => $companies->pluck('users_count')->values(),
|
||||
'Warehouses' => $companies->pluck('warehouses_count')->values(),
|
||||
'Licences' => $companies->pluck('license_keys_count')->values(),
|
||||
],
|
||||
'platformMixChartLabels' => ['Companies', 'Warehouses', 'Licences'],
|
||||
'platformMixChartValues' => [
|
||||
Company::query()->count(),
|
||||
Warehouse::query()->count(),
|
||||
LicenseKey::query()->count(),
|
||||
],
|
||||
'companyStatusChartLabels' => ['Active', 'Inactive'],
|
||||
|
||||
'companyStatusChartValues' => $companiesCount > 0
|
||||
? [$activeCompaniesCount, max($companiesCount - $activeCompaniesCount, 0)]
|
||||
: [3, 1],
|
||||
'companyStatusChartValues' => [
|
||||
Company::query()->where('status', 'active')->count(),
|
||||
Company::query()->where('status', 'inactive')->count(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user