-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAgentSoldApplianceService.php
106 lines (94 loc) · 3.2 KB
/
AgentSoldApplianceService.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
namespace App\Services;
use App\Models\Agent;
use App\Models\AgentSoldAppliance;
use App\Models\AssetPerson;
use App\Services\Interfaces\IBaseService;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @implements IBaseService<AgentSoldAppliance>
*/
class AgentSoldApplianceService implements IBaseService {
public function __construct(
private AgentSoldAppliance $agentSoldAppliance,
private AssetPerson $assetPerson,
) {}
public function create($applianceData): AgentSoldAppliance {
return $this->agentSoldAppliance->newQuery()->create($applianceData);
}
public function getById(int $agentId, ?int $customerId = null): ?AssetPerson {
return $this->assetPerson->newQuery()->with(['person', 'device', 'rates'])
->whereHasMorph(
'creator',
[Agent::class],
function ($q) use ($agentId) {
$q->where('id', $agentId);
}
)
->where('person_id', $customerId)
->latest()
// Not sure why it want to return a paginate here.
// Commenting out for now to return a singleton.
// ->paginate();
->first();
}
public function update($model, array $data): AgentSoldAppliance {
throw new \Exception('Method update() not yet implemented.');
}
public function delete($model): ?bool {
throw new \Exception('Method delete() not yet implemented.');
}
public function getAll(
?int $limit = null,
$agentId = null,
$customerId = null,
$forApp = false,
): Collection|LengthAwarePaginator {
if ($forApp) {
return $this->list($agentId);
}
$query = $this->agentSoldAppliance->newQuery()->with([
'assignedAppliance',
'assignedAppliance.appliance.assetType',
'person',
]);
if ($agentId) {
$query->whereHas(
'assignedAppliance',
function ($q) use ($agentId) {
$q->whereHas(
'agent',
function ($q) use ($agentId) {
$q->where('agent_id', $agentId);
}
);
}
);
}
if ($customerId) {
$query->where('person_id', $customerId);
}
if ($limit) {
return $query->latest()->paginate($limit);
} else {
return $query->latest()->paginate();
}
}
public function list($agentId) {
return $this->assetPerson->newQuery()->with(['person', 'device', 'rates'])
->whereHasMorph(
'creator',
[Agent::class],
function ($q) use ($agentId) {
$q->where('id', $agentId);
}
)->latest()
->paginate();
}
public function getAgentsByCustomerId(int $customerId): Collection {
return Agent::whereHas('soldAppliances', function ($query) use ($customerId) {
$query->where('person_id', $customerId);
})->get();
}
}