|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\User; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
8
|
|
|
use Illuminate\Notifications\RoutesNotifications; |
|
9
|
|
|
use Illuminate\Support\Collection; |
|
10
|
|
|
use LaravelEnso\Addresses\Traits\Addressable; |
|
11
|
|
|
use LaravelEnso\Companies\Models\Company; |
|
12
|
|
|
use LaravelEnso\DynamicMethods\Traits\Relations; |
|
13
|
|
|
use LaravelEnso\Helpers\Traits\AvoidsDeletionConflicts; |
|
14
|
|
|
use LaravelEnso\Helpers\Traits\CascadesMorphMap; |
|
15
|
|
|
use LaravelEnso\Multitenancy\Traits\SystemConnection; |
|
16
|
|
|
use LaravelEnso\People\Enums\Genders; |
|
17
|
|
|
use LaravelEnso\People\Enums\Titles; |
|
18
|
|
|
use LaravelEnso\Rememberable\Traits\Rememberable; |
|
19
|
|
|
use LaravelEnso\Tables\Traits\TableCache; |
|
20
|
|
|
use LaravelEnso\TrackWho\Traits\CreatedBy; |
|
21
|
|
|
use LaravelEnso\TrackWho\Traits\UpdatedBy; |
|
22
|
|
|
|
|
23
|
|
|
class Person extends Model |
|
24
|
|
|
{ |
|
25
|
|
|
use Addressable, |
|
|
|
|
|
|
26
|
|
|
AvoidsDeletionConflicts, |
|
27
|
|
|
CascadesMorphMap, |
|
28
|
|
|
CreatedBy, |
|
29
|
|
|
Relations, |
|
30
|
|
|
Rememberable, |
|
31
|
|
|
RoutesNotifications, |
|
32
|
|
|
TableCache, |
|
33
|
|
|
UpdatedBy, |
|
34
|
|
|
SoftDeletes, |
|
35
|
|
|
SystemConnection; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct(array $attributes = []) |
|
38
|
|
|
{ |
|
39
|
|
|
parent::__construct($attributes); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The attributes that should be mutated to dates. |
|
44
|
|
|
* |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $dates = ['deleted_at', 'birthday']; |
|
48
|
|
|
|
|
49
|
|
|
protected $fillable = [ |
|
50
|
|
|
'gid', |
|
51
|
|
|
'givn', |
|
52
|
|
|
'surn', |
|
53
|
|
|
'sex', |
|
54
|
|
|
'child_in_family_id', |
|
55
|
|
|
'description', |
|
56
|
|
|
'title', 'name', 'appellative', 'uid', 'email', 'phone', 'birthday', |
|
57
|
|
|
'deathday', 'bank', 'bank_account', |
|
58
|
|
|
'uid', 'chan', 'rin', 'resn', 'rfn', 'afn', |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
|
|
public function events() |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->hasMany(PersonEvent::class); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function child_in_family() |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->belongsTo(Family::class, 'child_in_family_id'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function husband_in_family() |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->hasMany(Family::class, 'husband_id'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function wife_in_family() |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->hasMany(Family::class, 'wife_id'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function fullname() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->givn.' '.$this->surn; |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getSex() |
|
87
|
|
|
{ |
|
88
|
|
|
if ($this->sex == 'F') { |
|
|
|
|
|
|
89
|
|
|
return 'Female'; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return 'Male'; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public static function getList() |
|
96
|
|
|
{ |
|
97
|
|
|
$persons = self::get(); |
|
98
|
|
|
$result = []; |
|
99
|
|
|
foreach ($persons as $person) { |
|
100
|
|
|
$result[$person->id] = $person->fullname(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return collect($result); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function addEvent($title, $date, $place, $description = '') |
|
107
|
|
|
{ |
|
108
|
|
|
$place_id = Place::getIdByTitle($place); |
|
109
|
|
|
$event = PersonEvent::updateOrCreate( |
|
110
|
|
|
[ |
|
111
|
|
|
'person_id' => $this->id, |
|
112
|
|
|
'title' => $title, |
|
113
|
|
|
], |
|
114
|
|
|
[ |
|
115
|
|
|
'person_id' => $this->id, |
|
116
|
|
|
'title' => $title, |
|
117
|
|
|
'description' => $description, |
|
118
|
|
|
]); |
|
119
|
|
|
|
|
120
|
|
|
if ($date) { |
|
121
|
|
|
$event->date = $date; |
|
122
|
|
|
$event->save(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if ($place) { |
|
126
|
|
|
$event->places_id = $place_id; |
|
127
|
|
|
$event->save(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
// add birthyear to person table ( for form builder ) |
|
131
|
|
|
if ($title == 'BIRT' && ! empty($date)) { |
|
132
|
|
|
$this->birthday = date('Y-m-d', strtotime($date)); |
|
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
// add deathyear to person table ( for form builder ) |
|
135
|
|
|
if ($title == 'DEAT' && ! empty($date)) { |
|
136
|
|
|
$this->deathday = date('Y-m-d', strtotime($date)); |
|
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
$this->save(); |
|
139
|
|
|
|
|
140
|
|
|
return $event; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function birth() |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->events->where('title', '=', 'BIRT')->first(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function death() |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->events->where('title', '=', 'DEAT')->first(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function appellative() |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->givn; |
|
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
protected $touches = ['user']; |
|
159
|
|
|
|
|
160
|
|
|
public function user() |
|
161
|
|
|
{ |
|
162
|
|
|
return $this->hasOne(User::class); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function companies() |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->belongsToMany(Company::class) |
|
168
|
|
|
->withPivot(['position', 'is_main']); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function hasUser() |
|
172
|
|
|
{ |
|
173
|
|
|
return $this->user()->exists(); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function company() |
|
177
|
|
|
{ |
|
178
|
|
|
return $this->companies()->wherePivot('is_main', true)->first(); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function gender() |
|
182
|
|
|
{ |
|
183
|
|
|
if (! $this->title) { |
|
|
|
|
|
|
184
|
|
|
return; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return $this->title === Titles::Mr |
|
188
|
|
|
? Genders::Male |
|
189
|
|
|
: Genders::Female; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function position(Company $company) |
|
193
|
|
|
{ |
|
194
|
|
|
return $this->companies() |
|
195
|
|
|
->wherePivot('company_id', $company->id) |
|
196
|
|
|
->first()->pivot->position; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function syncCompanies($companyIds, $mainCompanyId) |
|
200
|
|
|
{ |
|
201
|
|
|
$pivotIds = (new Collection($companyIds)) |
|
202
|
|
|
->reduce(fn ($pivot, $value) => $pivot->put($value, [ |
|
203
|
|
|
'is_main' => $value === $mainCompanyId, |
|
204
|
|
|
'is_mandatary' => false, |
|
205
|
|
|
]), new Collection()); |
|
206
|
|
|
|
|
207
|
|
|
$this->companies()->sync($pivotIds->toArray()); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|