-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAgentApplianceSalesSeeder.php
62 lines (51 loc) · 1.86 KB
/
AgentApplianceSalesSeeder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace Database\Seeders;
use App\Models\Agent;
use App\Models\AgentAssignedAppliances;
use App\Models\AgentSoldAppliance;
use App\Models\Asset;
use App\Models\Person\Person;
use Illuminate\Database\Seeder;
class AgentApplianceSalesSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run() {
// Fetch Existing Agents
$agents = Agent::all();
if ($agents->isEmpty()) {
$this->command->warn('No existing agents found. Skipping seeder.');
return;
}
// Fetch Existing Customers
$customers = Person::where('is_customer', true)->get();
if ($customers->isEmpty()) {
$this->command->warn('No existing customers found. Skipping seeder.');
return;
}
// Fetch Existing Assets
$assets = Asset::all();
if ($assets->isEmpty()) {
$this->command->warn('No existing assets found. Skipping seeder.');
return;
}
// Assign Assets to Agents
$assignedAppliances = $agents->flatMap(function ($agent) use ($assets, $customers) {
return AgentAssignedAppliances::factory()->count(3)->create([
'agent_id' => $agent->id,
'appliance_id' => $assets->random()->id, // Use existing assets
'user_id' => $customers->random()->id, // Use existing customers
]);
});
// Simulate Sales and Trigger Observer
$assignedAppliances->each(function ($assignedAppliance) use ($customers) {
AgentSoldAppliance::factory()->create([
'agent_assigned_appliance_id' => $assignedAppliance->id,
'person_id' => $customers->random()->id,
]);
});
$this->command->info('Agent Appliance Sales Seeded Successfully!');
}
}