diff --git a/app/Http/Controllers/Settings/ProjectSettingController.php b/app/Http/Controllers/Settings/ProjectSettingController.php index 0f23598..a453d72 100644 --- a/app/Http/Controllers/Settings/ProjectSettingController.php +++ b/app/Http/Controllers/Settings/ProjectSettingController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Settings; use App\Http\Controllers\Controller; +use App\Models\Settings\Company; use App\Models\Settings\Department; use App\Models\Settings\Location; use App\Models\Settings\ProjectSetting; @@ -12,33 +13,60 @@ class ProjectSettingController extends Controller { public function index() { - $projects = ProjectSetting::with([ - 'locations' => fn ($q) => $q->orderBy('name'), - 'departments' => fn ($q) => $q->orderBy('name'), + $companies = Company::with([ + 'projects.locations' => fn ($q) => $q->orderBy('name'), + 'projects.departments' => fn ($q) => $q->orderBy('name'), ])->orderBy('name')->get(); + $allProjects = $companies->flatMap(fn ($c) => $c->projects); + $stats = [ - 'total_projects' => $projects->count(), - 'active_projects' => $projects->where('is_active', true)->count(), - 'total_locations' => $projects->sum(fn ($p) => $p->locations->count()), - 'active_locations' => $projects->sum(fn ($p) => $p->locations->where('is_active', true)->count()), - 'total_departments' => $projects->sum(fn ($p) => $p->departments->count()), + 'total_companies' => $companies->count(), + 'total_projects' => $allProjects->count(), + 'active_projects' => $allProjects->where('is_active', true)->count(), + 'total_locations' => $allProjects->sum(fn ($p) => $p->locations->count()), + 'total_departments' => $allProjects->sum(fn ($p) => $p->departments->count()), ]; - return view('settings.projects.index', compact('projects', 'stats')); + return view('settings.projects.index', compact('companies', 'stats')); } public function store(Request $request) { - $validated = $request->validate(['name' => 'required|string|max:255|unique:settings_projects,name']); - $project = ProjectSetting::create(['name' => $validated['name'], 'is_active' => true]); + $validated = $request->validate([ + 'name' => 'required|string|max:255|unique:settings_projects,name', + 'company_id' => 'required|exists:settings_companies,id', + ]); + $project = ProjectSetting::create(['name' => $validated['name'], 'company_id' => $validated['company_id'], 'is_active' => true]); return response()->json(['project' => [ - 'id' => $project->id, - 'name' => $project->name, - 'is_active' => $project->is_active, + 'id' => $project->id, + 'name' => $project->name, + 'is_active' => $project->is_active, + 'company_id' => $project->company_id, ]]); } + // ── Company CRUD ────────────────────────────────────────────────────────── + public function storeCompany(Request $request) + { + $validated = $request->validate(['name' => 'required|string|max:255|unique:settings_companies,name']); + $company = Company::create(['name' => $validated['name'], 'is_active' => true]); + return response()->json(['company' => ['id' => $company->id, 'name' => $company->name, 'is_active' => $company->is_active]]); + } + + public function updateCompany(Request $request, Company $company) + { + $validated = $request->validate(['name' => 'required|string|max:255|unique:settings_companies,name,' . $company->id]); + $company->update(['name' => $validated['name'], 'is_active' => $request->boolean('is_active', true)]); + return response()->json(['company' => ['id' => $company->id, 'name' => $company->name, 'is_active' => $company->is_active]]); + } + + public function destroyCompany(Company $company) + { + $company->delete(); + return response()->json(['ok' => true]); + } + public function update(Request $request, ProjectSetting $project) { $validated = $request->validate([ diff --git a/app/Models/Settings/Company.php b/app/Models/Settings/Company.php new file mode 100644 index 0000000..5469524 --- /dev/null +++ b/app/Models/Settings/Company.php @@ -0,0 +1,19 @@ + 'boolean']; + + public function projects(): \Illuminate\Database\Eloquent\Relations\HasMany + { + return $this->hasMany(ProjectSetting::class, 'company_id'); + } +} diff --git a/app/Models/Settings/ProjectSetting.php b/app/Models/Settings/ProjectSetting.php index 86ac28e..cba90d9 100644 --- a/app/Models/Settings/ProjectSetting.php +++ b/app/Models/Settings/ProjectSetting.php @@ -8,7 +8,7 @@ class ProjectSetting extends Model { protected $table = 'settings_projects'; - protected $fillable = ['name', 'is_active']; + protected $fillable = ['name', 'is_active', 'company_id']; protected $casts = ['is_active' => 'boolean']; @@ -17,6 +17,11 @@ class ProjectSetting extends Model return $query->where('is_active', true); } + public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo + { + return $this->belongsTo(Company::class, 'company_id'); + } + public function locations(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(\App\Models\Settings\Location::class, 'project_id'); diff --git a/database/migrations/2026_05_26_145521_create_settings_companies_and_add_company_id_to_projects.php b/database/migrations/2026_05_26_145521_create_settings_companies_and_add_company_id_to_projects.php new file mode 100644 index 0000000..7a300e1 --- /dev/null +++ b/database/migrations/2026_05_26_145521_create_settings_companies_and_add_company_id_to_projects.php @@ -0,0 +1,41 @@ +id(); + $table->string('name', 255)->unique(); + $table->boolean('is_active')->default(true); + $table->timestamps(); + }); + + Schema::table('settings_projects', function (Blueprint $table) { + $table->foreignId('company_id')->nullable()->after('id') + ->constrained('settings_companies')->nullOnDelete(); + }); + + // Assign all existing projects to a "General" company + if (\DB::table('settings_projects')->exists()) { + $id = \DB::table('settings_companies')->insertGetId(['name' => 'General', 'is_active' => 1, 'created_at' => now(), 'updated_at' => now()]); + \DB::table('settings_projects')->update(['company_id' => $id]); + } + } + + public function down(): void + { + Schema::table('settings_projects', function (Blueprint $table) { + $table->dropForeign(['company_id']); + $table->dropColumn('company_id'); + }); + Schema::dropIfExists('settings_companies'); + } +}; diff --git a/resources/views/settings/projects/index.blade.php b/resources/views/settings/projects/index.blade.php index fa5b708..e3012f7 100644 --- a/resources/views/settings/projects/index.blade.php +++ b/resources/views/settings/projects/index.blade.php @@ -40,25 +40,25 @@ {{-- Stat boxes --}}