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