added complex address and devisions to company
This commit is contained in:
+40
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('company_addresses', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->morphs('addressable');
|
||||
$table->string('label', 80)->default('Registered office');
|
||||
$table->string('attention')->nullable();
|
||||
$table->string('line_1');
|
||||
$table->string('line_2')->nullable();
|
||||
$table->string('line_3')->nullable();
|
||||
$table->string('locality')->comment('City, town or locality.');
|
||||
$table->string('administrative_area')->nullable()->comment('County, state, province or region.');
|
||||
$table->string('postal_code', 32)->nullable();
|
||||
$table->foreignId('country_id')->constrained('countries')->restrictOnDelete();
|
||||
$table->foreignId('country_subdivision_id')->nullable()->constrained('country_subdivisions')->nullOnDelete();
|
||||
$table->decimal('latitude', 10, 7)->nullable();
|
||||
$table->decimal('longitude', 10, 7)->nullable();
|
||||
$table->boolean('is_primary')->default(true);
|
||||
$table->json('metadata')->nullable();
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['addressable_type', 'addressable_id'], 'company_addresses_one_per_owner_unique');
|
||||
$table->index(['country_id', 'locality']);
|
||||
$table->index(['postal_code']);
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('company_addresses');
|
||||
}
|
||||
};
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('company_divisions', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('company_id')->constrained('companies')->cascadeOnDelete();
|
||||
$table->uuid('uuid')->unique();
|
||||
$table->string('name');
|
||||
$table->string('code', 40)->nullable();
|
||||
$table->string('slug');
|
||||
$table->string('status', 32)->default('active')->index();
|
||||
$table->text('description')->nullable();
|
||||
$table->json('metadata')->nullable();
|
||||
$table->timestamps();
|
||||
$table->softDeletes();
|
||||
|
||||
$table->unique(['company_id', 'slug']);
|
||||
$table->unique(['company_id', 'code']);
|
||||
$table->index(['company_id', 'status', 'name']);
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('company_divisions');
|
||||
}
|
||||
};
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('company_division_user', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('company_id')->constrained('companies')->cascadeOnDelete();
|
||||
$table->foreignId('company_division_id')->constrained('company_divisions')->cascadeOnDelete();
|
||||
$table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
|
||||
$table->string('role', 80)->default('member')->index();
|
||||
$table->boolean('is_director')->default(false)->index();
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['company_division_id', 'user_id']);
|
||||
$table->index(['company_id', 'user_id']);
|
||||
$table->index(['company_id', 'is_director']);
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('company_division_user');
|
||||
}
|
||||
};
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('company_director_assignments', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('company_id')->constrained('companies')->cascadeOnDelete();
|
||||
$table->foreignId('company_division_id')->nullable()->constrained('company_divisions')->cascadeOnDelete();
|
||||
$table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
|
||||
$table->string('coverage_type', 32)->default('company')->index();
|
||||
$table->date('starts_at')->nullable();
|
||||
$table->date('ends_at')->nullable();
|
||||
$table->text('notes')->nullable();
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['company_id', 'company_division_id', 'user_id'], 'company_director_coverage_unique');
|
||||
$table->index(['company_id', 'coverage_type']);
|
||||
$table->index(['user_id', 'coverage_type']);
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('company_director_assignments');
|
||||
}
|
||||
};
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('warehouses', function (Blueprint $table): void {
|
||||
if (! Schema::hasColumn('warehouses', 'division_id')) {
|
||||
$table->foreignId('division_id')->nullable()->after('company_id')->constrained('company_divisions')->nullOnDelete();
|
||||
$table->index(['company_id', 'division_id']);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('warehouses', function (Blueprint $table): void {
|
||||
if (Schema::hasColumn('warehouses', 'division_id')) {
|
||||
$table->dropConstrainedForeignId('division_id');
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Company\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Modules\Company\Models\Company;
|
||||
use Modules\Company\Models\CompanyDivision;
|
||||
use Modules\Warehouse\app\Models\Warehouse;
|
||||
|
||||
class CompanyAddressController extends Controller
|
||||
{
|
||||
public function updateCompany(Request $request, Company $company)
|
||||
{
|
||||
$this->upsertAddress($request, $company);
|
||||
|
||||
return redirect()->route('platform.companies.show', $company)->with('status', 'Company address updated.');
|
||||
}
|
||||
|
||||
public function updateDivision(Request $request, Company $company, CompanyDivision $division)
|
||||
{
|
||||
abort_unless($division->company_id === $company->id, 404);
|
||||
|
||||
$this->upsertAddress($request, $division, 'Operating address');
|
||||
|
||||
return redirect()->route('platform.companies.show', $company)->with('status', 'Division address updated.');
|
||||
}
|
||||
|
||||
public function updateWarehouse(Request $request, Company $company, Warehouse $warehouse)
|
||||
{
|
||||
abort_unless($warehouse->company_id === $company->id, 404);
|
||||
|
||||
$this->upsertAddress($request, $warehouse, 'Warehouse address');
|
||||
|
||||
return redirect()->route('platform.companies.show', $company)->with('status', 'Warehouse address updated.');
|
||||
}
|
||||
|
||||
private function upsertAddress(Request $request, object $owner, string $defaultLabel = 'Registered office'): void
|
||||
{
|
||||
$data = $request->validate([
|
||||
'label' => ['nullable', 'string', 'max:80'],
|
||||
'attention' => ['nullable', 'string', 'max:255'],
|
||||
'line_1' => ['required', 'string', 'max:255'],
|
||||
'line_2' => ['nullable', 'string', 'max:255'],
|
||||
'line_3' => ['nullable', 'string', 'max:255'],
|
||||
'locality' => ['required', 'string', 'max:255'],
|
||||
'administrative_area' => ['nullable', 'string', 'max:255'],
|
||||
'postal_code' => ['nullable', 'string', 'max:32'],
|
||||
'country_id' => ['required', Rule::exists('countries', 'id')->where('is_active', true)],
|
||||
'country_subdivision_id' => ['nullable', 'exists:country_subdivisions,id'],
|
||||
'latitude' => ['nullable', 'numeric', 'between:-90,90'],
|
||||
'longitude' => ['nullable', 'numeric', 'between:-180,180'],
|
||||
]);
|
||||
|
||||
$data['label'] = $data['label'] ?: $defaultLabel;
|
||||
$data['is_primary'] = true;
|
||||
|
||||
$owner->address()->updateOrCreate([], $data);
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Modules\Company\Models\Company;
|
||||
use Modules\Countries\Models\Country;
|
||||
|
||||
class CompanyController extends Controller
|
||||
{
|
||||
@@ -17,7 +18,8 @@ class CompanyController extends Controller
|
||||
{
|
||||
return view('company::index', [
|
||||
'companies' => Company::query()
|
||||
->withCount(['users', 'warehouses', 'licenseKeys'])
|
||||
->with(['address.country'])
|
||||
->withCount(['users', 'warehouses', 'licenseKeys', 'divisions'])
|
||||
->latest('id')
|
||||
->paginate(20),
|
||||
]);
|
||||
@@ -53,13 +55,25 @@ class CompanyController extends Controller
|
||||
*/
|
||||
public function show(Company $company)
|
||||
{
|
||||
$company->loadCount(['users', 'warehouses', 'licenseKeys', 'payments']);
|
||||
$company->load([
|
||||
'address.country',
|
||||
'divisions.address.country',
|
||||
'divisions.warehouses',
|
||||
'divisions.users',
|
||||
'warehouses.address.country',
|
||||
'warehouses.division',
|
||||
'directorAssignments.user',
|
||||
'directorAssignments.division',
|
||||
])->loadCount(['users', 'warehouses', 'licenseKeys', 'payments', 'divisions']);
|
||||
|
||||
return view('company::show', [
|
||||
'company' => $company,
|
||||
'recentUsers' => $company->users()->latest('id')->limit(5)->get(),
|
||||
'recentWarehouses' => $company->warehouses()->latest('id')->limit(5)->get(),
|
||||
'recentLicenseKeys' => $company->licenseKeys()->latest('id')->limit(5)->get(),
|
||||
'countries' => Country::query()->active()->orderBy('sort_order')->orderBy('name')->get(['id', 'alpha2', 'name', 'display_name', 'common_name']),
|
||||
'companyUsers' => $company->users()->orderBy('name')->get(['id', 'name', 'email', 'company_id']),
|
||||
'unassignedWarehouses' => $company->warehouses()->whereNull('division_id')->orderBy('name')->get(),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Company\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Modules\Company\Models\Company;
|
||||
use Modules\Company\Models\CompanyDivision;
|
||||
|
||||
class CompanyDivisionController extends Controller
|
||||
{
|
||||
public function store(Request $request, Company $company)
|
||||
{
|
||||
$division = $company->divisions()->create($this->validated($request, $company));
|
||||
|
||||
return redirect()->route('platform.companies.show', $company)->with('status', "Division {$division->name} created.");
|
||||
}
|
||||
|
||||
public function update(Request $request, Company $company, CompanyDivision $division)
|
||||
{
|
||||
abort_unless($division->company_id === $company->id, 404);
|
||||
|
||||
$division->update($this->validated($request, $company, $division));
|
||||
|
||||
return redirect()->route('platform.companies.show', $company)->with('status', 'Division updated.');
|
||||
}
|
||||
|
||||
public function destroy(Company $company, CompanyDivision $division)
|
||||
{
|
||||
abort_unless($division->company_id === $company->id, 404);
|
||||
|
||||
abort_if($division->warehouses()->exists(), 422, 'Move warehouses before deleting this division.');
|
||||
|
||||
$division->delete();
|
||||
|
||||
return redirect()->route('platform.companies.show', $company)->with('status', 'Division deleted.');
|
||||
}
|
||||
|
||||
private function validated(Request $request, Company $company, ?CompanyDivision $division = null): array
|
||||
{
|
||||
$data = $request->validate([
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'code' => ['nullable', 'string', 'max:40', Rule::unique('company_divisions', 'code')->where('company_id', $company->id)->ignore($division?->id)],
|
||||
'status' => ['required', Rule::in(['active', 'suspended', 'inactive'])],
|
||||
'description' => ['nullable', 'string', 'max:2000'],
|
||||
]);
|
||||
|
||||
$data['slug'] = Str::slug($data['name']);
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Company\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Modules\Company\Models\Company;
|
||||
use Modules\Company\Models\CompanyDivision;
|
||||
use Modules\Company\Models\CompanyDirectorAssignment;
|
||||
|
||||
class CompanyMembershipController extends Controller
|
||||
{
|
||||
public function assignDivision(Request $request, Company $company)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'user_id' => ['required', Rule::exists('users', 'id')->where('company_id', $company->id)],
|
||||
'company_division_id' => ['required', Rule::exists('company_divisions', 'id')->where('company_id', $company->id)],
|
||||
'role' => ['nullable', 'string', 'max:80'],
|
||||
'is_director' => ['nullable', 'boolean'],
|
||||
]);
|
||||
|
||||
$division = CompanyDivision::where('company_id', $company->id)->findOrFail($data['company_division_id']);
|
||||
$division->users()->syncWithoutDetaching([
|
||||
$data['user_id'] => [
|
||||
'company_id' => $company->id,
|
||||
'role' => $data['role'] ?? 'member',
|
||||
'is_director' => (bool) ($data['is_director'] ?? false),
|
||||
],
|
||||
]);
|
||||
|
||||
if ((bool) ($data['is_director'] ?? false)) {
|
||||
CompanyDirectorAssignment::updateOrCreate([
|
||||
'company_id' => $company->id,
|
||||
'company_division_id' => $division->id,
|
||||
'user_id' => $data['user_id'],
|
||||
], ['coverage_type' => 'division']);
|
||||
}
|
||||
|
||||
return redirect()->route('platform.companies.show', $company)->with('status', 'Division membership updated.');
|
||||
}
|
||||
|
||||
public function assignDirector(Request $request, Company $company)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'user_id' => ['required', Rule::exists('users', 'id')->where('company_id', $company->id)],
|
||||
'coverage_type' => ['required', Rule::in(['company', 'division'])],
|
||||
'company_division_id' => ['nullable', Rule::exists('company_divisions', 'id')->where('company_id', $company->id)],
|
||||
'starts_at' => ['nullable', 'date'],
|
||||
'ends_at' => ['nullable', 'date', 'after_or_equal:starts_at'],
|
||||
'notes' => ['nullable', 'string', 'max:2000'],
|
||||
]);
|
||||
|
||||
if ($data['coverage_type'] === 'company') {
|
||||
$data['company_division_id'] = null;
|
||||
} else {
|
||||
abort_if(empty($data['company_division_id']), 422, 'Division coverage requires a division.');
|
||||
}
|
||||
|
||||
CompanyDirectorAssignment::updateOrCreate([
|
||||
'company_id' => $company->id,
|
||||
'company_division_id' => $data['company_division_id'],
|
||||
'user_id' => $data['user_id'],
|
||||
], [
|
||||
'coverage_type' => $data['coverage_type'],
|
||||
'starts_at' => $data['starts_at'] ?? null,
|
||||
'ends_at' => $data['ends_at'] ?? null,
|
||||
'notes' => $data['notes'] ?? null,
|
||||
]);
|
||||
|
||||
return redirect()->route('platform.companies.show', $company)->with('status', 'Director coverage updated.');
|
||||
}
|
||||
|
||||
public function removeDirector(Company $company, CompanyDirectorAssignment $assignment)
|
||||
{
|
||||
abort_unless($assignment->company_id === $company->id, 404);
|
||||
|
||||
$assignment->delete();
|
||||
|
||||
return redirect()->route('platform.companies.show', $company)->with('status', 'Director coverage removed.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Company\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Modules\Company\Models\Company;
|
||||
use Modules\Warehouse\app\Models\Warehouse;
|
||||
|
||||
class CompanyWarehouseAssignmentController extends Controller
|
||||
{
|
||||
public function update(Request $request, Company $company, Warehouse $warehouse)
|
||||
{
|
||||
abort_unless($warehouse->company_id === $company->id, 404);
|
||||
|
||||
$data = $request->validate([
|
||||
'division_id' => ['nullable', Rule::exists('company_divisions', 'id')->where('company_id', $company->id)],
|
||||
]);
|
||||
|
||||
$warehouse->update(['division_id' => $data['division_id'] ?? null]);
|
||||
|
||||
return redirect()->route('platform.companies.show', $company)->with('status', 'Warehouse division assignment updated.');
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace Modules\Company\Models;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphOne;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Support\Str;
|
||||
use Modules\Platform\app\Models\LicenseKey;
|
||||
@@ -35,6 +36,22 @@ class Company extends Model
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public function address(): MorphOne
|
||||
{
|
||||
return $this->morphOne(CompanyAddress::class, 'addressable');
|
||||
}
|
||||
|
||||
public function divisions(): HasMany
|
||||
{
|
||||
return $this->hasMany(CompanyDivision::class);
|
||||
}
|
||||
|
||||
public function directorAssignments(): HasMany
|
||||
{
|
||||
return $this->hasMany(CompanyDirectorAssignment::class);
|
||||
}
|
||||
|
||||
public function users(): HasMany
|
||||
{
|
||||
return $this->hasMany(User::class);
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Company\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||
use Modules\Countries\Models\Country;
|
||||
use Modules\Countries\Models\CountrySubdivision;
|
||||
|
||||
class CompanyAddress extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'addressable_type', 'addressable_id', 'label', 'attention', 'line_1', 'line_2', 'line_3',
|
||||
'locality', 'administrative_area', 'postal_code', 'country_id', 'country_subdivision_id',
|
||||
'latitude', 'longitude', 'is_primary', 'metadata',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_primary' => 'boolean',
|
||||
'metadata' => 'array',
|
||||
'latitude' => 'decimal:7',
|
||||
'longitude' => 'decimal:7',
|
||||
];
|
||||
|
||||
public function addressable(): MorphTo
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
public function country(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Country::class);
|
||||
}
|
||||
|
||||
public function subdivision(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(CountrySubdivision::class, 'country_subdivision_id');
|
||||
}
|
||||
|
||||
public function getFormattedAttribute(): string
|
||||
{
|
||||
return collect([
|
||||
$this->attention,
|
||||
$this->line_1,
|
||||
$this->line_2,
|
||||
$this->line_3,
|
||||
$this->locality,
|
||||
$this->administrative_area,
|
||||
$this->postal_code,
|
||||
$this->country?->effective_name ?? $this->country?->name,
|
||||
])->filter()->implode(', ');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Company\Models;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class CompanyDirectorAssignment extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'company_id', 'company_division_id', 'user_id', 'coverage_type', 'starts_at', 'ends_at', 'notes',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'starts_at' => 'date',
|
||||
'ends_at' => 'date',
|
||||
];
|
||||
|
||||
public function company(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Company::class);
|
||||
}
|
||||
|
||||
public function division(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(CompanyDivision::class, 'company_division_id');
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Company\Models;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphOne;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Support\Str;
|
||||
use Modules\Warehouse\app\Models\Warehouse;
|
||||
|
||||
class CompanyDivision extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
protected $fillable = [
|
||||
'company_id', 'uuid', 'name', 'code', 'slug', 'status', 'description', 'metadata',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'metadata' => 'array',
|
||||
];
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::creating(function (CompanyDivision $division): void {
|
||||
$division->uuid ??= (string) Str::uuid();
|
||||
$division->slug ??= Str::slug($division->name);
|
||||
$division->status ??= 'active';
|
||||
});
|
||||
}
|
||||
|
||||
public function company(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Company::class);
|
||||
}
|
||||
|
||||
public function address(): MorphOne
|
||||
{
|
||||
return $this->morphOne(CompanyAddress::class, 'addressable');
|
||||
}
|
||||
|
||||
public function users(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(User::class, 'company_division_user')
|
||||
->withPivot(['company_id', 'role', 'is_director'])
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
public function warehouses(): HasMany
|
||||
{
|
||||
return $this->hasMany(Warehouse::class, 'division_id');
|
||||
}
|
||||
|
||||
public function directorAssignments(): HasMany
|
||||
{
|
||||
return $this->hasMany(CompanyDirectorAssignment::class, 'company_division_id');
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@
|
||||
<tr class="border-b border-zinc-200 dark:border-zinc-800">
|
||||
<th class="p-4 text-left">Name</th>
|
||||
<th class="p-4 text-left">Users</th>
|
||||
<th class="p-4 text-left">Divisions</th>
|
||||
<th class="p-4 text-left">Warehouses</th>
|
||||
<th class="p-4 text-left">Licences</th>
|
||||
<th class="p-4 text-left">Status</th>
|
||||
@@ -40,6 +41,10 @@
|
||||
{{ $company->users_count }}
|
||||
</td>
|
||||
|
||||
<td class="p-4 text-zinc-700 dark:text-zinc-300">
|
||||
{{ $company->divisions_count }}
|
||||
</td>
|
||||
|
||||
<td class="p-4 text-zinc-700 dark:text-zinc-300">
|
||||
{{ $company->warehouses_count }}
|
||||
</td>
|
||||
@@ -100,7 +105,7 @@
|
||||
</tr>
|
||||
@empty
|
||||
<tr>
|
||||
<td colspan="6" class="p-8 text-center text-zinc-500 dark:text-zinc-400">
|
||||
<td colspan="7" class="p-8 text-center text-zinc-500 dark:text-zinc-400">
|
||||
No companies found.
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
@php($address = $address ?? null)
|
||||
<div class="grid gap-4 md:grid-cols-2">
|
||||
<div>
|
||||
<label class="block text-sm font-semibold text-zinc-700 dark:text-zinc-200">Label</label>
|
||||
<input name="label" value="{{ old('label', $address?->label ?? $defaultLabel ?? 'Registered office') }}" class="mt-2 w-full rounded-xl border-zinc-300 dark:border-zinc-700 dark:bg-zinc-950 dark:text-white">
|
||||
</div>
|
||||
<div>
|
||||
<label class="block text-sm font-semibold text-zinc-700 dark:text-zinc-200">Attention</label>
|
||||
<input name="attention" value="{{ old('attention', $address?->attention) }}" class="mt-2 w-full rounded-xl border-zinc-300 dark:border-zinc-700 dark:bg-zinc-950 dark:text-white">
|
||||
</div>
|
||||
<div class="md:col-span-2">
|
||||
<label class="block text-sm font-semibold text-zinc-700 dark:text-zinc-200">Address line 1</label>
|
||||
<input name="line_1" value="{{ old('line_1', $address?->line_1) }}" class="mt-2 w-full rounded-xl border-zinc-300 dark:border-zinc-700 dark:bg-zinc-950 dark:text-white" required>
|
||||
</div>
|
||||
<div>
|
||||
<label class="block text-sm font-semibold text-zinc-700 dark:text-zinc-200">Address line 2</label>
|
||||
<input name="line_2" value="{{ old('line_2', $address?->line_2) }}" class="mt-2 w-full rounded-xl border-zinc-300 dark:border-zinc-700 dark:bg-zinc-950 dark:text-white">
|
||||
</div>
|
||||
<div>
|
||||
<label class="block text-sm font-semibold text-zinc-700 dark:text-zinc-200">Address line 3</label>
|
||||
<input name="line_3" value="{{ old('line_3', $address?->line_3) }}" class="mt-2 w-full rounded-xl border-zinc-300 dark:border-zinc-700 dark:bg-zinc-950 dark:text-white">
|
||||
</div>
|
||||
<div>
|
||||
<label class="block text-sm font-semibold text-zinc-700 dark:text-zinc-200">Town / City</label>
|
||||
<input name="locality" value="{{ old('locality', $address?->locality) }}" class="mt-2 w-full rounded-xl border-zinc-300 dark:border-zinc-700 dark:bg-zinc-950 dark:text-white" required>
|
||||
</div>
|
||||
<div>
|
||||
<label class="block text-sm font-semibold text-zinc-700 dark:text-zinc-200">County / State / Region</label>
|
||||
<input name="administrative_area" value="{{ old('administrative_area', $address?->administrative_area) }}" class="mt-2 w-full rounded-xl border-zinc-300 dark:border-zinc-700 dark:bg-zinc-950 dark:text-white">
|
||||
</div>
|
||||
<div>
|
||||
<label class="block text-sm font-semibold text-zinc-700 dark:text-zinc-200">Postcode / ZIP</label>
|
||||
<input name="postal_code" value="{{ old('postal_code', $address?->postal_code) }}" class="mt-2 w-full rounded-xl border-zinc-300 dark:border-zinc-700 dark:bg-zinc-950 dark:text-white">
|
||||
</div>
|
||||
<div>
|
||||
<label class="block text-sm font-semibold text-zinc-700 dark:text-zinc-200">Country</label>
|
||||
<select name="country_id" class="mt-2 w-full rounded-xl border-zinc-300 dark:border-zinc-700 dark:bg-zinc-950 dark:text-white" required>
|
||||
<option value="">Select country</option>
|
||||
@foreach ($countries as $country)
|
||||
<option value="{{ $country->id }}" @selected((int) old('country_id', $address?->country_id) === $country->id)>{{ $country->effective_name ?? $country->name }}</option>
|
||||
@endforeach
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mt-4">
|
||||
<button class="rounded-xl bg-zinc-900 px-4 py-2 text-sm font-semibold text-white dark:bg-white dark:text-zinc-900">Save address</button>
|
||||
</div>
|
||||
@@ -1,144 +1,140 @@
|
||||
<x-layouts::app :title="$company->name">
|
||||
<div class="space-y-6">
|
||||
<x-wms.page-hero
|
||||
eyebrow="Company Workbench"
|
||||
:title="$company->name"
|
||||
description="Review tenant health, linked records and administration shortcuts."
|
||||
icon="fa-solid fa-building"
|
||||
/>
|
||||
<x-wms.page-hero eyebrow="Company Workbench" :title="$company->name" description="Manage the tenant profile, address, divisions, director coverage and warehouse alignment." icon="fa-solid fa-building" />
|
||||
|
||||
<div class="grid gap-4 md:grid-cols-2 xl:grid-cols-4">
|
||||
<div class="rounded-3xl bg-white p-5 shadow-sm ring-1 ring-zinc-200 dark:bg-zinc-900 dark:ring-zinc-800">
|
||||
<p class="text-sm font-medium text-zinc-500 dark:text-zinc-400">Users</p>
|
||||
<p class="mt-2 text-3xl font-bold text-zinc-900 dark:text-white">{{ $company->users_count }}</p>
|
||||
</div>
|
||||
|
||||
<div class="rounded-3xl bg-white p-5 shadow-sm ring-1 ring-zinc-200 dark:bg-zinc-900 dark:ring-zinc-800">
|
||||
<p class="text-sm font-medium text-zinc-500 dark:text-zinc-400">Warehouses</p>
|
||||
<p class="mt-2 text-3xl font-bold text-zinc-900 dark:text-white">{{ $company->warehouses_count }}</p>
|
||||
</div>
|
||||
|
||||
<div class="rounded-3xl bg-white p-5 shadow-sm ring-1 ring-zinc-200 dark:bg-zinc-900 dark:ring-zinc-800">
|
||||
<p class="text-sm font-medium text-zinc-500 dark:text-zinc-400">Licences</p>
|
||||
<p class="mt-2 text-3xl font-bold text-zinc-900 dark:text-white">{{ $company->license_keys_count }}</p>
|
||||
</div>
|
||||
|
||||
<div class="rounded-3xl bg-white p-5 shadow-sm ring-1 ring-zinc-200 dark:bg-zinc-900 dark:ring-zinc-800">
|
||||
<p class="text-sm font-medium text-zinc-500 dark:text-zinc-400">Payments</p>
|
||||
<p class="mt-2 text-3xl font-bold text-zinc-900 dark:text-white">{{ $company->payments_count }}</p>
|
||||
</div>
|
||||
<div class="grid gap-4 md:grid-cols-2 xl:grid-cols-5">
|
||||
<div class="rounded-3xl bg-white p-5 shadow-sm ring-1 ring-zinc-200 dark:bg-zinc-900 dark:ring-zinc-800"><p class="text-sm text-zinc-500">Users</p><p class="mt-2 text-3xl font-bold">{{ $company->users_count }}</p></div>
|
||||
<div class="rounded-3xl bg-white p-5 shadow-sm ring-1 ring-zinc-200 dark:bg-zinc-900 dark:ring-zinc-800"><p class="text-sm text-zinc-500">Divisions</p><p class="mt-2 text-3xl font-bold">{{ $company->divisions_count }}</p></div>
|
||||
<div class="rounded-3xl bg-white p-5 shadow-sm ring-1 ring-zinc-200 dark:bg-zinc-900 dark:ring-zinc-800"><p class="text-sm text-zinc-500">Warehouses</p><p class="mt-2 text-3xl font-bold">{{ $company->warehouses_count }}</p></div>
|
||||
<div class="rounded-3xl bg-white p-5 shadow-sm ring-1 ring-zinc-200 dark:bg-zinc-900 dark:ring-zinc-800"><p class="text-sm text-zinc-500">Licences</p><p class="mt-2 text-3xl font-bold">{{ $company->license_keys_count }}</p></div>
|
||||
<div class="rounded-3xl bg-white p-5 shadow-sm ring-1 ring-zinc-200 dark:bg-zinc-900 dark:ring-zinc-800"><p class="text-sm text-zinc-500">Payments</p><p class="mt-2 text-3xl font-bold">{{ $company->payments_count }}</p></div>
|
||||
</div>
|
||||
|
||||
<div class="grid gap-6 xl:grid-cols-3">
|
||||
<div class="rounded-3xl bg-white p-6 shadow-sm ring-1 ring-zinc-200 dark:bg-zinc-900 dark:ring-zinc-800 xl:col-span-2">
|
||||
<div class="flex items-start justify-between gap-4">
|
||||
<div>
|
||||
<h2 class="text-lg font-bold text-zinc-900 dark:text-white">Company profile</h2>
|
||||
<p class="mt-1 text-sm text-zinc-500 dark:text-zinc-400">Core tenant identity and operational status.</p>
|
||||
</div>
|
||||
|
||||
<span class="inline-flex rounded-full px-3 py-1 text-xs font-semibold
|
||||
@if ($company->is_system_company)
|
||||
bg-blue-100 text-blue-700 dark:bg-blue-950 dark:text-blue-300
|
||||
@elseif ($company->status === 'active')
|
||||
bg-green-100 text-green-700 dark:bg-green-950 dark:text-green-300
|
||||
@else
|
||||
bg-red-100 text-red-700 dark:bg-red-950 dark:text-red-300
|
||||
@endif
|
||||
">
|
||||
{{ $company->is_system_company ? 'System' : ucfirst($company->status) }}
|
||||
</span>
|
||||
<div><h2 class="text-lg font-bold text-zinc-900 dark:text-white">Company profile</h2><p class="mt-1 text-sm text-zinc-500">Core tenant identity and operational status.</p></div>
|
||||
<a href="{{ route('platform.companies.edit', $company) }}" class="rounded-xl bg-blue-600 px-4 py-2 text-sm font-semibold text-white">Edit company</a>
|
||||
</div>
|
||||
|
||||
<dl class="mt-6 grid gap-5 md:grid-cols-2">
|
||||
<div>
|
||||
<dt class="text-sm font-medium text-zinc-500 dark:text-zinc-400">Billing email</dt>
|
||||
<dd class="mt-1 font-semibold text-zinc-900 dark:text-white">{{ $company->billing_email ?? '—' }}</dd>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<dt class="text-sm font-medium text-zinc-500 dark:text-zinc-400">Timezone</dt>
|
||||
<dd class="mt-1 font-semibold text-zinc-900 dark:text-white">{{ $company->timezone }}</dd>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<dt class="text-sm font-medium text-zinc-500 dark:text-zinc-400">Slug</dt>
|
||||
<dd class="mt-1 font-semibold text-zinc-900 dark:text-white">{{ $company->slug }}</dd>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<dt class="text-sm font-medium text-zinc-500 dark:text-zinc-400">Created</dt>
|
||||
<dd class="mt-1 font-semibold text-zinc-900 dark:text-white">{{ $company->created_at?->format('d M Y') }}</dd>
|
||||
</div>
|
||||
<div><dt class="text-sm text-zinc-500">Billing email</dt><dd class="font-semibold">{{ $company->billing_email ?? '—' }}</dd></div>
|
||||
<div><dt class="text-sm text-zinc-500">Status</dt><dd class="font-semibold">{{ $company->is_system_company ? 'System' : ucfirst($company->status) }}</dd></div>
|
||||
<div><dt class="text-sm text-zinc-500">Timezone</dt><dd class="font-semibold">{{ $company->timezone }}</dd></div>
|
||||
<div><dt class="text-sm text-zinc-500">Slug</dt><dd class="font-semibold">{{ $company->slug }}</dd></div>
|
||||
</dl>
|
||||
</div>
|
||||
|
||||
<div class="rounded-3xl bg-white p-6 shadow-sm ring-1 ring-zinc-200 dark:bg-zinc-900 dark:ring-zinc-800">
|
||||
<h2 class="text-lg font-bold text-zinc-900 dark:text-white">Quick actions</h2>
|
||||
<div class="mt-5 space-y-3">
|
||||
<a href="{{ route('platform.companies.edit', $company) }}" class="block rounded-xl bg-blue-600 px-4 py-2 text-center text-sm font-semibold text-white hover:bg-blue-700">
|
||||
Edit company
|
||||
</a>
|
||||
|
||||
<a href="{{ route('platform.companies.index') }}" class="block rounded-xl border border-zinc-300 px-4 py-2 text-center text-sm font-semibold text-zinc-700 hover:bg-zinc-50 dark:border-zinc-700 dark:text-zinc-200 dark:hover:bg-zinc-800">
|
||||
Back to companies
|
||||
</a>
|
||||
|
||||
<a href="{{ route('platform.companies.index') }}" class="block rounded-xl border border-zinc-300 px-4 py-2 text-center text-sm font-semibold">Back to companies</a>
|
||||
@unless ($company->is_system_company)
|
||||
<form method="POST" action="{{ route('platform.companies.destroy', $company) }}" onsubmit="return confirm('Delete this company? This cannot be undone.');">
|
||||
@csrf
|
||||
@method('DELETE')
|
||||
<button type="submit" class="w-full rounded-xl bg-red-600 px-4 py-2 text-sm font-semibold text-white hover:bg-red-700">
|
||||
Delete company
|
||||
</button>
|
||||
</form>
|
||||
<form method="POST" action="{{ route('platform.companies.destroy', $company) }}" onsubmit="return confirm('Delete this company? This cannot be undone.');">@csrf @method('DELETE')<button class="w-full rounded-xl bg-red-600 px-4 py-2 text-sm font-semibold text-white">Delete company</button></form>
|
||||
@endunless
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="grid gap-6 xl:grid-cols-3">
|
||||
<div class="rounded-3xl bg-white p-6 shadow-sm ring-1 ring-zinc-200 dark:bg-zinc-900 dark:ring-zinc-800">
|
||||
<h2 class="text-lg font-bold text-zinc-900 dark:text-white">Company address</h2>
|
||||
@if ($company->address)
|
||||
<p class="mt-2 text-sm text-zinc-600 dark:text-zinc-300">{{ $company->address->formatted }}</p>
|
||||
@else
|
||||
<p class="mt-2 text-sm text-zinc-500">No company address has been recorded.</p>
|
||||
@endif
|
||||
<form method="POST" action="{{ route('platform.companies.address.update', $company) }}" class="mt-5">
|
||||
@csrf @method('PUT')
|
||||
@include('company::partials.address-form', ['address' => $company->address, 'defaultLabel' => 'Registered office'])
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div class="grid gap-6 xl:grid-cols-2">
|
||||
<div class="rounded-3xl bg-white p-6 shadow-sm ring-1 ring-zinc-200 dark:bg-zinc-900 dark:ring-zinc-800">
|
||||
<h2 class="text-lg font-bold text-zinc-900 dark:text-white">Recent users</h2>
|
||||
<div class="mt-5 space-y-3">
|
||||
@forelse ($recentUsers as $user)
|
||||
<div class="rounded-2xl border border-zinc-200 p-3 dark:border-zinc-800">
|
||||
<p class="font-semibold text-zinc-900 dark:text-white">{{ $user->name }}</p>
|
||||
<p class="text-sm text-zinc-500 dark:text-zinc-400">{{ $user->email }}</p>
|
||||
</div>
|
||||
@empty
|
||||
<p class="text-sm text-zinc-500 dark:text-zinc-400">No users found.</p>
|
||||
@endforelse
|
||||
</div>
|
||||
<h2 class="text-lg font-bold text-zinc-900 dark:text-white">Create division</h2>
|
||||
<form method="POST" action="{{ route('platform.companies.divisions.store', $company) }}" class="mt-5 grid gap-4 md:grid-cols-2">
|
||||
@csrf
|
||||
<input name="name" placeholder="Division name" class="rounded-xl border-zinc-300 dark:border-zinc-700 dark:bg-zinc-950" required>
|
||||
<input name="code" placeholder="Optional code" class="rounded-xl border-zinc-300 dark:border-zinc-700 dark:bg-zinc-950">
|
||||
<select name="status" class="rounded-xl border-zinc-300 dark:border-zinc-700 dark:bg-zinc-950"><option value="active">Active</option><option value="suspended">Suspended</option><option value="inactive">Inactive</option></select>
|
||||
<button class="rounded-xl bg-zinc-900 px-4 py-2 font-semibold text-white dark:bg-white dark:text-zinc-900">Create division</button>
|
||||
<textarea name="description" placeholder="Description" class="md:col-span-2 rounded-xl border-zinc-300 dark:border-zinc-700 dark:bg-zinc-950"></textarea>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div class="rounded-3xl bg-white p-6 shadow-sm ring-1 ring-zinc-200 dark:bg-zinc-900 dark:ring-zinc-800">
|
||||
<h2 class="text-lg font-bold text-zinc-900 dark:text-white">Recent warehouses</h2>
|
||||
<h2 class="text-lg font-bold text-zinc-900 dark:text-white">Director coverage</h2>
|
||||
<form method="POST" action="{{ route('platform.companies.director-assignments.store', $company) }}" class="mt-5 grid gap-4 md:grid-cols-2">
|
||||
@csrf
|
||||
<select name="user_id" class="rounded-xl border-zinc-300 dark:border-zinc-700 dark:bg-zinc-950" required><option value="">Select user</option>@foreach($companyUsers as $user)<option value="{{ $user->id }}">{{ $user->name }}</option>@endforeach</select>
|
||||
<select name="coverage_type" class="rounded-xl border-zinc-300 dark:border-zinc-700 dark:bg-zinc-950"><option value="company">Whole company</option><option value="division">Specific division</option></select>
|
||||
<select name="company_division_id" class="rounded-xl border-zinc-300 dark:border-zinc-700 dark:bg-zinc-950"><option value="">No division</option>@foreach($company->divisions as $division)<option value="{{ $division->id }}">{{ $division->name }}</option>@endforeach</select>
|
||||
<button class="rounded-xl bg-zinc-900 px-4 py-2 font-semibold text-white dark:bg-white dark:text-zinc-900">Assign director</button>
|
||||
</form>
|
||||
<div class="mt-5 space-y-3">
|
||||
@forelse ($recentWarehouses as $warehouse)
|
||||
<div class="rounded-2xl border border-zinc-200 p-3 dark:border-zinc-800">
|
||||
<p class="font-semibold text-zinc-900 dark:text-white">{{ $warehouse->name }}</p>
|
||||
<p class="text-sm text-zinc-500 dark:text-zinc-400">{{ $warehouse->code ?? '—' }}</p>
|
||||
</div>
|
||||
@empty
|
||||
<p class="text-sm text-zinc-500 dark:text-zinc-400">No warehouses found.</p>
|
||||
@endforelse
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="rounded-3xl bg-white p-6 shadow-sm ring-1 ring-zinc-200 dark:bg-zinc-900 dark:ring-zinc-800">
|
||||
<h2 class="text-lg font-bold text-zinc-900 dark:text-white">Recent licences</h2>
|
||||
<div class="mt-5 space-y-3">
|
||||
@forelse ($recentLicenseKeys as $license)
|
||||
<div class="rounded-2xl border border-zinc-200 p-3 dark:border-zinc-800">
|
||||
<p class="font-semibold text-zinc-900 dark:text-white">{{ ucfirst($license->plan ?? 'Licence') }}</p>
|
||||
<p class="text-sm text-zinc-500 dark:text-zinc-400">{{ ucfirst($license->status ?? 'unknown') }}</p>
|
||||
</div>
|
||||
@empty
|
||||
<p class="text-sm text-zinc-500 dark:text-zinc-400">No licences found.</p>
|
||||
@endforelse
|
||||
@forelse($company->directorAssignments as $assignment)
|
||||
<div class="flex items-center justify-between rounded-2xl border border-zinc-200 p-3 dark:border-zinc-800"><span>{{ $assignment->user?->name }} — {{ $assignment->coverage_type === 'company' ? 'Whole company' : $assignment->division?->name }}</span><form method="POST" action="{{ route('platform.companies.director-assignments.destroy', [$company, $assignment]) }}">@csrf @method('DELETE')<button class="text-sm font-semibold text-red-600">Remove</button></form></div>
|
||||
@empty <p class="text-sm text-zinc-500">No director coverage recorded.</p> @endforelse
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="rounded-3xl bg-white p-6 shadow-sm ring-1 ring-zinc-200 dark:bg-zinc-900 dark:ring-zinc-800">
|
||||
<h2 class="text-lg font-bold text-zinc-900 dark:text-white">Warehouse division and address alignment</h2>
|
||||
<p class="mt-1 text-sm text-zinc-500">Warehouses remain tenant-scoped to the company. Assign a division only when that operating structure exists and record an optional warehouse operating address where needed.</p>
|
||||
<div class="mt-5 space-y-5">
|
||||
@forelse($company->warehouses as $warehouse)
|
||||
<div class="rounded-2xl border border-zinc-200 p-4 dark:border-zinc-800">
|
||||
<div class="grid gap-3 md:grid-cols-3">
|
||||
<div>
|
||||
<p class="font-semibold text-zinc-900 dark:text-white">{{ $warehouse->name }}</p>
|
||||
<p class="text-sm text-zinc-500">{{ $warehouse->code }} · {{ $warehouse->division?->name ?? 'Company-level warehouse' }}</p>
|
||||
@if ($warehouse->address)
|
||||
<p class="mt-2 text-xs text-zinc-500">{{ $warehouse->address->formatted }}</p>
|
||||
@else
|
||||
<p class="mt-2 text-xs text-zinc-500">No warehouse address recorded.</p>
|
||||
@endif
|
||||
</div>
|
||||
<form method="POST" action="{{ route('platform.companies.warehouses.division.update', [$company, $warehouse]) }}" class="contents">
|
||||
@csrf @method('PUT')
|
||||
<select name="division_id" class="rounded-xl border-zinc-300 dark:border-zinc-700 dark:bg-zinc-950">
|
||||
<option value="">Company-level warehouse</option>
|
||||
@foreach($company->divisions as $divisionOption)
|
||||
<option value="{{ $divisionOption->id }}" @selected($warehouse->division_id === $divisionOption->id)>{{ $divisionOption->name }}</option>
|
||||
@endforeach
|
||||
</select>
|
||||
<button class="rounded-xl bg-blue-600 px-4 py-2 text-sm font-semibold text-white">Update assignment</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<details class="mt-4 rounded-2xl bg-zinc-50 p-4 dark:bg-zinc-950">
|
||||
<summary class="cursor-pointer text-sm font-semibold text-zinc-900 dark:text-white">Edit warehouse address</summary>
|
||||
<form method="POST" action="{{ route('platform.companies.warehouses.address.update', [$company, $warehouse]) }}" class="mt-4">
|
||||
@csrf @method('PUT')
|
||||
@include('company::partials.address-form', ['address' => $warehouse->address, 'defaultLabel' => 'Warehouse address'])
|
||||
</form>
|
||||
</details>
|
||||
</div>
|
||||
@empty
|
||||
<p class="text-sm text-zinc-500">No warehouses exist for this company yet.</p>
|
||||
@endforelse
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="space-y-6">
|
||||
<h2 class="text-xl font-bold text-zinc-900 dark:text-white">Divisions</h2>
|
||||
@forelse($company->divisions as $division)
|
||||
<div class="rounded-3xl bg-white p-6 shadow-sm ring-1 ring-zinc-200 dark:bg-zinc-900 dark:ring-zinc-800">
|
||||
<div class="flex flex-wrap items-start justify-between gap-4">
|
||||
<div><h3 class="text-lg font-bold">{{ $division->name }}</h3><p class="text-sm text-zinc-500">{{ $division->code ?? 'No code' }} · {{ ucfirst($division->status) }} · {{ $division->warehouses->count() }} warehouses</p></div>
|
||||
<form method="POST" action="{{ route('platform.companies.divisions.destroy', [$company, $division]) }}" onsubmit="return confirm('Delete this division?');">@csrf @method('DELETE')<button class="rounded-xl bg-red-600 px-4 py-2 text-sm font-semibold text-white">Delete division</button></form>
|
||||
</div>
|
||||
<form method="POST" action="{{ route('platform.companies.divisions.update', [$company, $division]) }}" class="mt-5 grid gap-4 md:grid-cols-4">@csrf @method('PUT')<input name="name" value="{{ $division->name }}" class="rounded-xl border-zinc-300 dark:border-zinc-700 dark:bg-zinc-950" required><input name="code" value="{{ $division->code }}" class="rounded-xl border-zinc-300 dark:border-zinc-700 dark:bg-zinc-950"><select name="status" class="rounded-xl border-zinc-300 dark:border-zinc-700 dark:bg-zinc-950"><option value="active" @selected($division->status === 'active')>Active</option><option value="suspended" @selected($division->status === 'suspended')>Suspended</option><option value="inactive" @selected($division->status === 'inactive')>Inactive</option></select><button class="rounded-xl bg-blue-600 px-4 py-2 font-semibold text-white">Update division</button><textarea name="description" class="md:col-span-4 rounded-xl border-zinc-300 dark:border-zinc-700 dark:bg-zinc-950">{{ $division->description }}</textarea></form>
|
||||
<div class="mt-6"><h4 class="font-semibold">Division address</h4><form method="POST" action="{{ route('platform.companies.divisions.address.update', [$company, $division]) }}" class="mt-4">@csrf @method('PUT')@include('company::partials.address-form', ['address' => $division->address, 'defaultLabel' => 'Operating address'])</form></div>
|
||||
</div>
|
||||
@empty
|
||||
<div class="rounded-3xl bg-white p-6 text-sm text-zinc-500 shadow-sm ring-1 ring-zinc-200 dark:bg-zinc-900 dark:ring-zinc-800">No divisions exist. Warehouses can remain directly under the company until divisions are introduced.</div>
|
||||
@endforelse
|
||||
</div>
|
||||
</div>
|
||||
</x-layouts::app>
|
||||
|
||||
@@ -1,21 +1,27 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Web Routes
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Here is where you can register web routes for your application. These
|
||||
| routes are loaded by the RouteServiceProvider within a group which
|
||||
| contains the "web" middleware group. Now create something great!
|
||||
|
|
||||
*/
|
||||
|
||||
use Modules\Company\Http\Controllers\CompanyAddressController;
|
||||
use Modules\Company\Http\Controllers\CompanyController;
|
||||
use Modules\Company\Http\Controllers\CompanyDivisionController;
|
||||
use Modules\Company\Http\Controllers\CompanyMembershipController;
|
||||
use Modules\Company\Http\Controllers\CompanyWarehouseAssignmentController;
|
||||
|
||||
Route::middleware(['web', 'auth', 'verified', 'tenant.assigned', 'platform.admin'])
|
||||
->prefix('admin')
|
||||
->name('platform.')
|
||||
->group(function () {
|
||||
Route::resource('companies', CompanyController::class);
|
||||
|
||||
Route::put('companies/{company}/address', [CompanyAddressController::class, 'updateCompany'])->name('companies.address.update');
|
||||
Route::post('companies/{company}/divisions', [CompanyDivisionController::class, 'store'])->name('companies.divisions.store');
|
||||
Route::put('companies/{company}/divisions/{division}', [CompanyDivisionController::class, 'update'])->name('companies.divisions.update');
|
||||
Route::delete('companies/{company}/divisions/{division}', [CompanyDivisionController::class, 'destroy'])->name('companies.divisions.destroy');
|
||||
Route::put('companies/{company}/divisions/{division}/address', [CompanyAddressController::class, 'updateDivision'])->name('companies.divisions.address.update');
|
||||
|
||||
Route::post('companies/{company}/division-memberships', [CompanyMembershipController::class, 'assignDivision'])->name('companies.division-memberships.store');
|
||||
Route::post('companies/{company}/director-assignments', [CompanyMembershipController::class, 'assignDirector'])->name('companies.director-assignments.store');
|
||||
Route::delete('companies/{company}/director-assignments/{assignment}', [CompanyMembershipController::class, 'removeDirector'])->name('companies.director-assignments.destroy');
|
||||
|
||||
Route::put('companies/{company}/warehouses/{warehouse}/division', [CompanyWarehouseAssignmentController::class, 'update'])->name('companies.warehouses.division.update');
|
||||
Route::put('companies/{company}/warehouses/{warehouse}/address', [CompanyAddressController::class, 'updateWarehouse'])->name('companies.warehouses.address.update');
|
||||
});
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Company\Tests\Browser;
|
||||
|
||||
use App\Models\User;
|
||||
use Laravel\Dusk\Browser;
|
||||
use Modules\Company\Models\Company;
|
||||
use Modules\Countries\Models\Country;
|
||||
use Tests\DuskTestCase;
|
||||
|
||||
class CompanyAdministrationBrowserTest extends DuskTestCase
|
||||
{
|
||||
public function test_platform_admin_can_see_company_address_divisions_and_director_sections(): void
|
||||
{
|
||||
$system = Company::query()->firstOrCreate(['slug' => 'mpba-limited'], ['name' => 'MPBA Limited', 'is_system_company' => true, 'status' => 'active']);
|
||||
$tenant = Company::query()->firstOrCreate(['slug' => 'sentinel'], ['name' => 'Sentinel', 'status' => 'active']);
|
||||
Country::query()->firstOrCreate(['alpha2' => 'GB'], ['alpha3' => 'GBR', 'numeric' => '826', 'name' => 'United Kingdom', 'is_active' => true]);
|
||||
$admin = User::query()->firstOrCreate(['email' => 'company-admin-browser@example.test'], [
|
||||
'company_id' => $system->id,
|
||||
'name' => 'Company Browser Admin',
|
||||
'password' => bcrypt('password'),
|
||||
'is_platform_admin' => true,
|
||||
'status' => 'active',
|
||||
]);
|
||||
|
||||
$this->browse(function (Browser $browser) use ($admin, $tenant): void {
|
||||
$browser->loginAs($admin)
|
||||
->visitRoute('platform.companies.show', $tenant)
|
||||
->assertSee('Company address')
|
||||
->assertSee('Create division')
|
||||
->assertSee('Director coverage')
|
||||
->assertSee('Warehouse division and address alignment')
|
||||
->assertSee('Divisions');
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Company\Tests\Feature;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Modules\Company\Models\Company;
|
||||
use Modules\Countries\Models\Country;
|
||||
use Modules\Warehouse\app\Models\Warehouse;
|
||||
use Tests\TestCase;
|
||||
|
||||
class CompanyAdministrationRoutesTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_platform_admin_can_update_company_address_create_division_and_assign_director(): void
|
||||
{
|
||||
$system = Company::create(['name' => 'MPBA Limited', 'is_system_company' => true]);
|
||||
$tenant = Company::create(['name' => 'Sentinel', 'billing_email' => 'sentinel@example.test']);
|
||||
$country = Country::create(['alpha2' => 'GB', 'alpha3' => 'GBR', 'numeric' => '826', 'name' => 'United Kingdom', 'is_active' => true]);
|
||||
$admin = User::create(['company_id' => $system->id, 'name' => 'Platform Admin', 'email' => 'admin@example.test', 'password' => 'password', 'is_platform_admin' => true]);
|
||||
$director = User::create(['company_id' => $tenant->id, 'name' => 'Division Director', 'email' => 'director@example.test', 'password' => 'password']);
|
||||
|
||||
$this->actingAs($admin)->put(route('platform.companies.address.update', $tenant), [
|
||||
'line_1' => '1 Example Street',
|
||||
'locality' => 'London',
|
||||
'postal_code' => 'SW1A 1AA',
|
||||
'country_id' => $country->id,
|
||||
])->assertRedirect(route('platform.companies.show', $tenant));
|
||||
|
||||
$this->actingAs($admin)->post(route('platform.companies.divisions.store', $tenant), [
|
||||
'name' => 'Northern Operations',
|
||||
'code' => 'NORTH',
|
||||
'status' => 'active',
|
||||
])->assertRedirect(route('platform.companies.show', $tenant));
|
||||
|
||||
$division = $tenant->divisions()->where('code', 'NORTH')->firstOrFail();
|
||||
|
||||
$this->actingAs($admin)->post(route('platform.companies.director-assignments.store', $tenant), [
|
||||
'user_id' => $director->id,
|
||||
'coverage_type' => 'division',
|
||||
'company_division_id' => $division->id,
|
||||
])->assertRedirect(route('platform.companies.show', $tenant));
|
||||
|
||||
$this->assertDatabaseHas('company_addresses', ['line_1' => '1 Example Street', 'country_id' => $country->id]);
|
||||
$this->assertDatabaseHas('company_divisions', ['company_id' => $tenant->id, 'code' => 'NORTH']);
|
||||
$this->assertDatabaseHas('company_director_assignments', ['company_id' => $tenant->id, 'company_division_id' => $division->id, 'user_id' => $director->id]);
|
||||
}
|
||||
public function test_platform_admin_can_update_warehouse_address(): void
|
||||
{
|
||||
$system = Company::create(['name' => 'MPBA Limited', 'is_system_company' => true]);
|
||||
$tenant = Company::create(['name' => 'Sentinel', 'billing_email' => 'sentinel@example.test']);
|
||||
$country = Country::create(['alpha2' => 'GB', 'alpha3' => 'GBR', 'numeric' => '826', 'name' => 'United Kingdom', 'is_active' => true]);
|
||||
$warehouse = Warehouse::create(['company_id' => $tenant->id, 'code' => 'MAIN', 'name' => 'Main Warehouse']);
|
||||
$admin = User::create(['company_id' => $system->id, 'name' => 'Platform Admin', 'email' => 'warehouse-admin@example.test', 'password' => 'password', 'is_platform_admin' => true]);
|
||||
|
||||
$this->actingAs($admin)->put(route('platform.companies.warehouses.address.update', [$tenant, $warehouse]), [
|
||||
'label' => 'Warehouse address',
|
||||
'line_1' => '100 Distribution Park',
|
||||
'locality' => 'Birmingham',
|
||||
'postal_code' => 'B1 1AA',
|
||||
'country_id' => $country->id,
|
||||
])->assertRedirect(route('platform.companies.show', $tenant));
|
||||
|
||||
$this->assertDatabaseHas('company_addresses', [
|
||||
'addressable_type' => Warehouse::class,
|
||||
'addressable_id' => $warehouse->id,
|
||||
'line_1' => '100 Distribution Park',
|
||||
'country_id' => $country->id,
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Company\Tests\Feature;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Modules\Company\Models\Company;
|
||||
use Modules\Company\Models\CompanyAddress;
|
||||
use Modules\Company\Models\CompanyDirectorAssignment;
|
||||
use Modules\Company\Models\CompanyDivision;
|
||||
use Modules\Countries\Models\Country;
|
||||
use Modules\Warehouse\app\Models\Warehouse;
|
||||
use Tests\TestCase;
|
||||
|
||||
class CompanyStructureTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_company_has_one_editable_country_backed_address(): void
|
||||
{
|
||||
$company = Company::create(['name' => 'Abbott Logistics Limited', 'billing_email' => 'billing@abbott.example']);
|
||||
$country = Country::create(['alpha2' => 'GB', 'alpha3' => 'GBR', 'numeric' => '826', 'name' => 'United Kingdom', 'is_active' => true]);
|
||||
|
||||
$company->address()->create([
|
||||
'line_1' => '1 Warehouse Way',
|
||||
'locality' => 'London',
|
||||
'postal_code' => 'SW1A 1AA',
|
||||
'country_id' => $country->id,
|
||||
]);
|
||||
|
||||
$company->address()->update(['line_1' => '2 Warehouse Way']);
|
||||
|
||||
$this->assertSame(1, CompanyAddress::count());
|
||||
$this->assertSame('2 Warehouse Way', $company->fresh()->address->line_1);
|
||||
$this->assertSame('United Kingdom', $company->fresh()->address->country->name);
|
||||
}
|
||||
|
||||
public function test_company_division_has_address_users_directors_and_optional_warehouses(): void
|
||||
{
|
||||
$company = Company::create(['name' => 'Sentinel', 'billing_email' => 'sentinel@example.test']);
|
||||
$country = Country::create(['alpha2' => 'GB', 'alpha3' => 'GBR', 'numeric' => '826', 'name' => 'United Kingdom', 'is_active' => true]);
|
||||
$user = User::create(['company_id' => $company->id, 'name' => 'Jane Director', 'email' => 'jane@example.test', 'password' => 'password']);
|
||||
$division = $company->divisions()->create(['name' => 'North Operations', 'code' => 'NORTH']);
|
||||
$warehouse = Warehouse::create(['company_id' => $company->id, 'division_id' => $division->id, 'code' => 'NTH', 'name' => 'North DC']);
|
||||
|
||||
$division->address()->create([
|
||||
'label' => 'Operating address',
|
||||
'line_1' => 'North Road',
|
||||
'locality' => 'Manchester',
|
||||
'country_id' => $country->id,
|
||||
]);
|
||||
$division->users()->attach($user->id, ['company_id' => $company->id, 'role' => 'director', 'is_director' => true]);
|
||||
CompanyDirectorAssignment::create(['company_id' => $company->id, 'company_division_id' => $division->id, 'user_id' => $user->id, 'coverage_type' => 'division']);
|
||||
|
||||
$this->assertTrue($company->fresh()->divisions()->exists());
|
||||
$this->assertSame('North Operations', $warehouse->fresh()->division->name);
|
||||
$this->assertSame('Manchester', $division->fresh()->address->locality);
|
||||
$this->assertTrue($user->fresh()->divisions()->whereKey($division->id)->exists());
|
||||
$this->assertTrue($user->fresh()->directorAssignments()->where('company_division_id', $division->id)->exists());
|
||||
}
|
||||
|
||||
public function test_warehouse_can_remain_at_company_level_without_division(): void
|
||||
{
|
||||
$company = Company::create(['name' => 'Company Level Warehousing']);
|
||||
$warehouse = Warehouse::create(['company_id' => $company->id, 'code' => 'MAIN', 'name' => 'Main Warehouse']);
|
||||
|
||||
$this->assertNull($warehouse->division_id);
|
||||
$this->assertSame('Company Level Warehousing', $warehouse->company->name);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,14 @@
|
||||
latest: 1.1.0
|
||||
latest: 1.2.0
|
||||
|
||||
versions:
|
||||
- version: 1.2.0
|
||||
date: 2026-07-02
|
||||
changes:
|
||||
- Added one-address-per-owner company address records using an industry-standard address layout backed by the Countries module.
|
||||
- Added company divisions with optional codes, status, soft deletes, one address per division, user memberships and director coverage.
|
||||
- Added optional division assignment and proper address relationship support for warehouses.
|
||||
- Added Company module migrations with foreign keys, unique constraints and performance indexes.
|
||||
- Added feature and Dusk coverage for company addresses, divisions, director coverage and warehouse division behaviour.
|
||||
- version: 1.1.0
|
||||
date: 2026-07-02
|
||||
changes:
|
||||
@@ -8,12 +16,11 @@ versions:
|
||||
- Added company workbench show page with profile, KPI cards, recent users, warehouses and licences.
|
||||
- Added guarded deletion so the system company cannot be deleted.
|
||||
- Standardised Company module views on x-layouts::app and company::_form includes.
|
||||
- version: 1.0.2
|
||||
date: 2026-07-02
|
||||
changes:
|
||||
- Fixed company create/update validation when timezone is omitted from request payloads.
|
||||
- version: 1.0.0
|
||||
date: 2026-07-02
|
||||
changes:
|
||||
- Initial Company module skeleton.
|
||||
|
||||
- version: 1.0.2
|
||||
date: 2026-07-02
|
||||
changes:
|
||||
- Fixed company create/update validation when timezone is omitted from request payloads.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "Company",
|
||||
"alias": "company",
|
||||
"version": "1.1.0",
|
||||
"version": "1.2.0",
|
||||
"description": "",
|
||||
"keywords": [],
|
||||
"priority": 0,
|
||||
|
||||
@@ -5,13 +5,16 @@ namespace Modules\Warehouse\app\Models;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphOne;
|
||||
use Illuminate\Support\Str;
|
||||
use Modules\Company\Models\Company;
|
||||
use Modules\Company\Models\CompanyAddress;
|
||||
use Modules\Company\Models\CompanyDivision;
|
||||
|
||||
class Warehouse extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'company_id', 'uuid',
|
||||
'company_id', 'division_id', 'uuid',
|
||||
'code',
|
||||
'name',
|
||||
'status',
|
||||
@@ -38,6 +41,17 @@ class Warehouse extends Model
|
||||
return $this->belongsTo(Company::class);
|
||||
}
|
||||
|
||||
|
||||
public function division(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(CompanyDivision::class, 'division_id');
|
||||
}
|
||||
|
||||
public function address(): MorphOne
|
||||
{
|
||||
return $this->morphOne(CompanyAddress::class, 'addressable');
|
||||
}
|
||||
|
||||
public function validationRules(): HasMany
|
||||
{
|
||||
return $this->hasMany(WarehouseValidationRule::class);
|
||||
|
||||
@@ -5,6 +5,8 @@ namespace App\Models;
|
||||
use Database\Factories\UserFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Support\Str;
|
||||
@@ -12,6 +14,8 @@ use Laravel\Fortify\Contracts\PasskeyUser;
|
||||
use Laravel\Fortify\PasskeyAuthenticatable;
|
||||
use Laravel\Fortify\TwoFactorAuthenticatable;
|
||||
use Modules\Company\Models\Company;
|
||||
use Modules\Company\Models\CompanyDirectorAssignment;
|
||||
use Modules\Company\Models\CompanyDivision;
|
||||
use Spatie\Permission\Traits\HasRoles;
|
||||
|
||||
class User extends Authenticatable implements PasskeyUser
|
||||
@@ -40,6 +44,19 @@ class User extends Authenticatable implements PasskeyUser
|
||||
return $this->belongsTo(Company::class);
|
||||
}
|
||||
|
||||
|
||||
public function divisions(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(CompanyDivision::class, 'company_division_user')
|
||||
->withPivot(['company_id', 'role', 'is_director'])
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
public function directorAssignments(): HasMany
|
||||
{
|
||||
return $this->hasMany(CompanyDirectorAssignment::class);
|
||||
}
|
||||
|
||||
public function initials(): string
|
||||
{
|
||||
return Str::of($this->name)
|
||||
|
||||
Reference in New Issue
Block a user