1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
7
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
8
|
|
|
|
9
|
|
|
class Customer extends Model |
10
|
|
|
{ |
11
|
|
|
use HasFactory; |
12
|
|
|
use SoftDeletes; |
13
|
|
|
|
14
|
|
|
protected $primaryKey = 'cust_id'; |
15
|
|
|
protected $guarded = ['updated_at', 'created_at', 'deleted_at']; |
16
|
|
|
protected $hidden = ['updated_at', 'created_at', 'deleted_at']; |
17
|
|
|
protected $appends = ['child_count']; |
18
|
|
|
|
19
|
|
|
/* |
20
|
|
|
* If a customer is part of a multi-site customer, each site can be listed separately yet still be linked to the main site |
21
|
|
|
*/ |
22
|
|
|
public function Parent() |
23
|
|
|
{ |
24
|
|
|
return $this->belongsTo(Customer::class, 'parent_id', 'cust_id'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/* |
28
|
|
|
* If a customer is the parent and has children below it, they will be counted |
29
|
|
|
*/ |
30
|
|
|
public function getChildCountAttribute() |
31
|
|
|
{ |
32
|
|
|
return Customer::where('parent_id', $this->cust_id)->count(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/* |
36
|
|
|
* Several equipment types can be assigned to a customer |
37
|
|
|
*/ |
38
|
|
|
public function EquipmentType() |
39
|
|
|
{ |
40
|
|
|
return $this->hasManyThrough('App\Models\EquipmentType', 'App\Models\CustomerEquipment', 'cust_id', 'equip_id', 'cust_id', 'equip_id'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/* |
44
|
|
|
* Equipment that is assigned to a customer will be listed |
45
|
|
|
*/ |
46
|
|
|
public function CustomerEquipment() |
47
|
|
|
{ |
48
|
|
|
return $this->hasMany(CustomerEquipment::class, 'cust_id', 'cust_id'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/* |
52
|
|
|
* Equipment that is shared and belong to the parent site will show up for this customer |
53
|
|
|
*/ |
54
|
|
|
public function ParentEquipment() |
55
|
|
|
{ |
56
|
|
|
return $this->hasMany(CustomerEquipment::class, 'cust_id', 'parent_id')->whereShared(true); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/* |
60
|
|
|
* Site and other contacts for the customer |
61
|
|
|
*/ |
62
|
|
|
public function CustomerContact() |
63
|
|
|
{ |
64
|
|
|
return $this->hasMany(CustomerContact::class, 'cust_id', 'cust_id'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/* |
68
|
|
|
* Shared contacts throughout all linked sites |
69
|
|
|
*/ |
70
|
|
|
public function ParentContact() |
71
|
|
|
{ |
72
|
|
|
return $this->hasMany(CustomerContact::class, 'cust_id', 'parent_id')->where('shared', true); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/* |
76
|
|
|
* Site Specific notes for this customer |
77
|
|
|
*/ |
78
|
|
|
public function CustomerNote() |
79
|
|
|
{ |
80
|
|
|
return $this->hasMany(CustomerNote::class, 'cust_id', 'cust_id')->orderBy('urgent', 'DESC'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/* |
84
|
|
|
* Shared notes throughout all sites |
85
|
|
|
*/ |
86
|
|
|
public function ParentNote() |
87
|
|
|
{ |
88
|
|
|
return $this->hasMany(CustomerNote::class, 'cust_id', 'parent_id')->orderBy('urgent', 'DESC')->whereShared(true); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/* |
92
|
|
|
* Files attached to this customer |
93
|
|
|
*/ |
94
|
|
|
public function CustomerFile() |
95
|
|
|
{ |
96
|
|
|
return $this->hasMany(CustomerFile::class, 'cust_id', 'cust_id'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/* |
100
|
|
|
* Files that are shared throughout all linked sites |
101
|
|
|
*/ |
102
|
|
|
public function ParentFile() |
103
|
|
|
{ |
104
|
|
|
return $this->hasMany(CustomerFile::class, 'cust_id', 'parent_id')->where('shared', true); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|