256 lines
8.9 KiB
PHP
256 lines
8.9 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Database\Seeder;
|
|
use Modules\ContractorManagement\Models\Contractor;
|
|
use Modules\UserManagement\Models\EmployeeProfile;
|
|
use Spatie\Permission\Models\Permission;
|
|
use Spatie\Permission\Models\Role;
|
|
use Spatie\Permission\PermissionRegistrar;
|
|
|
|
class ProductionSetupSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Run all production-grade setup seeds for GSB Construction ERP.
|
|
*/
|
|
public function run(): void
|
|
{
|
|
// 1. Reset cached roles and permissions
|
|
app()[PermissionRegistrar::class]->forgetCachedPermissions();
|
|
$this->command->info('1. Permission cache reset successfully.');
|
|
|
|
// 2. Define and seed all system permissions
|
|
$permissions = [
|
|
// Core Access
|
|
'dashboard.access',
|
|
'projects.access',
|
|
'contractors.access',
|
|
'users.access',
|
|
'materials-catalog.access',
|
|
'inventory.access',
|
|
'finance.access',
|
|
'documents.access',
|
|
'approvals.access',
|
|
'roles.access',
|
|
'labors.access',
|
|
'equipments.access',
|
|
|
|
// User & Role Management
|
|
'users.view',
|
|
'users.create',
|
|
'users.edit',
|
|
'users.delete',
|
|
'users.manage-status',
|
|
'users.link-contractor',
|
|
'roles.view',
|
|
'roles.create',
|
|
'roles.edit',
|
|
'roles.delete',
|
|
'roles.assign',
|
|
|
|
// Projects & Tasks
|
|
'create projects',
|
|
'edit projects',
|
|
'delete projects',
|
|
'create tasks',
|
|
'edit tasks',
|
|
'delete tasks',
|
|
'edit master schedule',
|
|
|
|
// Daily Reports & QC
|
|
'create daily reports',
|
|
'edit daily reports',
|
|
'delete daily reports',
|
|
'create inspections',
|
|
'approve inspections',
|
|
|
|
// Documents
|
|
'upload documents',
|
|
'approve documents',
|
|
|
|
// Settings & System
|
|
'manage settings',
|
|
'manage system settings',
|
|
'manage main contractors',
|
|
'manage sub contractors',
|
|
'view all data',
|
|
'delete data',
|
|
'view reports',
|
|
'approve_mr',
|
|
'approve_po',
|
|
];
|
|
|
|
foreach ($permissions as $permissionName) {
|
|
Permission::firstOrCreate(['name' => $permissionName, 'guard_name' => 'web']);
|
|
}
|
|
$this->command->info('2. Created / verified ' . count($permissions) . ' system permissions.');
|
|
|
|
// 3. Define Roles
|
|
$superAdmin = Role::firstOrCreate(['name' => 'Super Admin', 'guard_name' => 'web']);
|
|
$admin = Role::firstOrCreate(['name' => 'admin', 'guard_name' => 'web']);
|
|
$mainContractorAdmin = Role::firstOrCreate(['name' => 'Main Contractor Admin', 'guard_name' => 'web']);
|
|
$contractorProjectManager = Role::firstOrCreate(['name' => 'Contractor Project Manager', 'guard_name' => 'web']);
|
|
$projectManager = Role::firstOrCreate(['name' => 'Project Manager', 'guard_name' => 'web']);
|
|
$siteTechnical = Role::firstOrCreate(['name' => 'Site Technical', 'guard_name' => 'web']);
|
|
$constructionSupervisor = Role::firstOrCreate(['name' => 'Construction Supervisor', 'guard_name' => 'web']);
|
|
|
|
// Give Super Admin & admin all permissions
|
|
$allPermissions = Permission::all();
|
|
$superAdmin->syncPermissions($allPermissions);
|
|
$admin->syncPermissions($allPermissions);
|
|
|
|
// Main Contractor Admin Permissions
|
|
$mainContractorAdmin->syncPermissions([
|
|
'dashboard.access',
|
|
'projects.access',
|
|
'contractors.access',
|
|
'users.access',
|
|
'materials-catalog.access',
|
|
'inventory.access',
|
|
'finance.access',
|
|
'documents.access',
|
|
'approvals.access',
|
|
'create projects',
|
|
'edit projects',
|
|
'create tasks',
|
|
'edit tasks',
|
|
'view all data',
|
|
'view reports',
|
|
]);
|
|
|
|
// Contractor Project Manager Permissions (Operational powers without financial/chain approval permissions)
|
|
$contractorProjectManager->syncPermissions([
|
|
'dashboard.access',
|
|
'projects.access',
|
|
'materials-catalog.access',
|
|
'inventory.access',
|
|
'documents.access',
|
|
'labors.access',
|
|
'equipments.access',
|
|
'create tasks',
|
|
'edit tasks',
|
|
'delete tasks',
|
|
'upload documents',
|
|
'view reports',
|
|
]);
|
|
|
|
// Project Manager Permissions
|
|
$projectManager->syncPermissions([
|
|
'dashboard.access',
|
|
'projects.access',
|
|
'contractors.access',
|
|
'materials-catalog.access',
|
|
'inventory.access',
|
|
'finance.access',
|
|
'documents.access',
|
|
'approvals.access',
|
|
'labors.access',
|
|
'equipments.access',
|
|
'create tasks',
|
|
'edit tasks',
|
|
'delete tasks',
|
|
'upload documents',
|
|
'approve documents',
|
|
'approve inspections',
|
|
'edit master schedule',
|
|
'approve_mr',
|
|
'approve_po',
|
|
]);
|
|
|
|
// Site Technical Permissions
|
|
$siteTechnical->syncPermissions([
|
|
'dashboard.access',
|
|
'projects.access',
|
|
'inventory.access',
|
|
'documents.access',
|
|
'edit tasks',
|
|
'create daily reports',
|
|
'create inspections',
|
|
'upload documents',
|
|
]);
|
|
|
|
// Construction Supervisor Permissions
|
|
$constructionSupervisor->syncPermissions([
|
|
'dashboard.access',
|
|
'projects.access',
|
|
'inventory.access',
|
|
'materials-catalog.access',
|
|
'documents.access',
|
|
'edit tasks',
|
|
'create daily reports',
|
|
]);
|
|
|
|
$this->command->info('3. Roles and permission mappings configured successfully.');
|
|
|
|
// 4. Ensure Great SwissMetal Builders Corporation as Main Contractor
|
|
$mainContractor = Contractor::firstOrCreate(
|
|
['company_name' => 'Great SwissMetal Builders Corporation'],
|
|
[
|
|
'contact_person' => 'Super Administrator',
|
|
'email' => 'info@gsbcorp.com',
|
|
'phone' => null,
|
|
'specialization' => 'General Construction',
|
|
'address' => null,
|
|
'tax_id' => null,
|
|
'payment_terms' => 'Net 30',
|
|
'status' => 'active',
|
|
'type' => 'main',
|
|
'parent_id' => null,
|
|
'user_limit' => 999,
|
|
'shares_materials_catalog' => true,
|
|
]
|
|
);
|
|
$this->command->info('4. Main Contractor (Great SwissMetal Builders Corporation) verified.');
|
|
|
|
// 5. Ensure Super Admin Account
|
|
$superAdminUser = User::firstOrCreate(
|
|
['email' => 'superadmin@gsb-cons.com'],
|
|
[
|
|
'name' => 'Super Administrator',
|
|
'password' => bcrypt('password'),
|
|
'user_type' => 'admin',
|
|
'status' => 'active',
|
|
'contractor_id' => $mainContractor->id,
|
|
]
|
|
);
|
|
|
|
// Update to guarantee proper user_type, status, and contractor_id
|
|
$superAdminUser->update([
|
|
'user_type' => 'admin',
|
|
'status' => 'active',
|
|
'contractor_id' => $mainContractor->id,
|
|
]);
|
|
$superAdminUser->syncRoles(['Super Admin', 'admin']);
|
|
|
|
// Profile for Super Admin
|
|
$profile = EmployeeProfile::firstOrCreate(
|
|
['user_id' => $superAdminUser->id],
|
|
[
|
|
'department' => 'Administration',
|
|
'position' => 'Super Administrator',
|
|
'employee_code' => 'SUPER-0001',
|
|
'hire_date' => now(),
|
|
]
|
|
);
|
|
$superAdminUser->update([
|
|
'userable_type' => EmployeeProfile::class,
|
|
'userable_id' => $profile->id,
|
|
]);
|
|
|
|
// 6. Seed Master Project Classifications
|
|
$this->call(ProjectClassificationSeeder::class);
|
|
$this->command->info('6. Master Construction Classifications seeded.');
|
|
|
|
// 7. Clear permission cache one final time
|
|
app()[PermissionRegistrar::class]->forgetCachedPermissions();
|
|
|
|
$this->command->info('----------------------------------------------------');
|
|
$this->command->info('✅ Production Setup Seeding Complete!');
|
|
$this->command->info('Main Contractor: Great SwissMetal Builders Corporation');
|
|
$this->command->info('Super Admin: superadmin@gsb-cons.com (password)');
|
|
$this->command->info('----------------------------------------------------');
|
|
}
|
|
}
|