100 lines
3.8 KiB
PHP
100 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use Illuminate\Database\Seeder;
|
|
use App\Models\User;
|
|
use Modules\ContractorManagement\Models\Contractor;
|
|
use Modules\ProjectManagement\Models\Project;
|
|
use Modules\ProjectManagement\Enums\ProjectStatus;
|
|
use Faker\Factory as Faker;
|
|
use Illuminate\Support\Str;
|
|
|
|
class DemoProjectContractorSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Run the database seeds.
|
|
*/
|
|
public function run(): void
|
|
{
|
|
$faker = Faker::create();
|
|
|
|
$this->command->info('Seeding Customers...');
|
|
$customers = [];
|
|
for ($i = 0; $i < 3; $i++) {
|
|
$customers[] = User::create([
|
|
'name' => $faker->company . ' (Customer)',
|
|
'email' => $faker->unique()->safeEmail,
|
|
'password' => bcrypt('password'),
|
|
'user_type' => 'customer',
|
|
]);
|
|
}
|
|
|
|
$this->command->info('Seeding Contractors...');
|
|
$specializations = ['General Building', 'MEP', 'Civil Works', 'Landscaping', 'Interior Fit-out'];
|
|
$contractors = [];
|
|
for ($i = 0; $i < 5; $i++) {
|
|
$contractors[] = Contractor::create([
|
|
'company_name' => $faker->company . ' Construction',
|
|
'contact_person' => $faker->name,
|
|
'email' => $faker->companyEmail,
|
|
'phone' => $faker->phoneNumber,
|
|
'specialization' => $faker->randomElement($specializations),
|
|
'address' => $faker->address,
|
|
'tax_id' => $faker->numerify('TAX-####-####'),
|
|
'payment_terms' => $faker->randomElement(['Net 30', 'Net 60', 'Upon Completion']),
|
|
'rating' => $faker->randomFloat(1, 3, 5),
|
|
'status' => 'active',
|
|
'type' => 'sub',
|
|
]);
|
|
}
|
|
|
|
$this->command->info('Seeding Projects...');
|
|
$statuses = [
|
|
ProjectStatus::Planning,
|
|
ProjectStatus::InProgress,
|
|
ProjectStatus::Completed,
|
|
ProjectStatus::OnHold
|
|
];
|
|
|
|
for ($i = 0; $i < 10; $i++) {
|
|
$contractor = $faker->randomElement($contractors);
|
|
$customer = $faker->randomElement($customers);
|
|
|
|
$startDate = $faker->dateTimeBetween('-1 year', '+1 month');
|
|
$endDate = (clone $startDate)->modify('+' . $faker->numberBetween(3, 24) . ' months');
|
|
|
|
$project = Project::create([
|
|
'name' => 'Project ' . ucwords($faker->word) . ' ' . $faker->city,
|
|
// code is auto-generated by boot method
|
|
'description' => $faker->paragraph,
|
|
'customer_id' => $customer->id,
|
|
'client_name' => $customer->name,
|
|
'contractor_id' => $contractor->id,
|
|
'location' => $faker->address,
|
|
'status' => $faker->randomElement($statuses),
|
|
'contract_value' => $faker->randomFloat(2, 50000, 5000000),
|
|
'contract_duration' => $faker->numberBetween(3, 24),
|
|
'start_date' => $startDate->format('Y-m-d'),
|
|
'target_end_date' => $endDate->format('Y-m-d'),
|
|
'completion_percentage' => $faker->randomFloat(2, 0, 100),
|
|
'total_capitalization' => 0, // usually calculated
|
|
]);
|
|
|
|
// Attach contractor to the pivot table as well
|
|
$contractor->projects()->attach($project->id, [
|
|
'role' => 'Primary Contractor',
|
|
'contract_amount' => $project->contract_value,
|
|
]);
|
|
|
|
// Optionally assign a PM from users
|
|
$pm = User::role('Project Manager')->first();
|
|
if ($pm) {
|
|
$project->personnel()->attach($pm->id, ['role' => 'pm']);
|
|
}
|
|
}
|
|
|
|
$this->command->info('Demo Data for Projects and Contractors seeded successfully!');
|
|
}
|
|
}
|