81 lines
2.4 KiB
PHP
Executable File
81 lines
2.4 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\Countries\Support;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Modules\Countries\Models\Country;
|
|
|
|
class CountryUsageReport
|
|
{
|
|
/**
|
|
* Tables commonly using country_id in WMS and SaaS workflows.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
private array $countryIdTables = [
|
|
'companies' => 'Companies',
|
|
'warehouses' => 'Warehouses',
|
|
'users' => 'Users',
|
|
'suppliers' => 'Suppliers',
|
|
'customers' => 'Customers',
|
|
'carriers' => 'Carriers',
|
|
'addresses' => 'Addresses',
|
|
'billing_addresses' => 'Billing addresses',
|
|
'shipping_addresses' => 'Shipping addresses',
|
|
];
|
|
|
|
/**
|
|
* Tables commonly storing ISO alpha-2 country codes.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
private array $alpha2Tables = [
|
|
'companies' => 'Companies by ISO code',
|
|
'warehouses' => 'Warehouses by ISO code',
|
|
'addresses' => 'Addresses by ISO code',
|
|
'billing_addresses' => 'Billing addresses by ISO code',
|
|
'shipping_addresses' => 'Shipping addresses by ISO code',
|
|
];
|
|
|
|
/**
|
|
* Return usage counts without failing when optional business tables do not exist.
|
|
*
|
|
* @return array<int, array{label: string, table: string, column: string, count: int}>
|
|
*/
|
|
public function forCountry(Country $country): array
|
|
{
|
|
$rows = [];
|
|
|
|
foreach ($this->countryIdTables as $table => $label) {
|
|
if (! Schema::hasTable($table) || ! Schema::hasColumn($table, 'country_id')) {
|
|
continue;
|
|
}
|
|
|
|
$rows[] = [
|
|
'label' => $label,
|
|
'table' => $table,
|
|
'column' => 'country_id',
|
|
'count' => (int) DB::table($table)->where('country_id', $country->id)->count(),
|
|
];
|
|
}
|
|
|
|
foreach ($this->alpha2Tables as $table => $label) {
|
|
foreach (['country_code', 'country_alpha2', 'iso_country_code'] as $column) {
|
|
if (! Schema::hasTable($table) || ! Schema::hasColumn($table, $column)) {
|
|
continue;
|
|
}
|
|
|
|
$rows[] = [
|
|
'label' => $label,
|
|
'table' => $table,
|
|
'column' => $column,
|
|
'count' => (int) DB::table($table)->where($column, $country->alpha2)->count(),
|
|
];
|
|
}
|
|
}
|
|
|
|
return $rows;
|
|
}
|
|
}
|