LegacyIndividual.php
5.8 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
class LegacyIndividual extends Model
{
use HasFiles;
public const CREATED_AT = 'data_cad';
public const UPDATED_AT = null;
/**
* @var string
*/
protected $table = 'cadastro.fisica';
/**
* @var string
*/
protected $primaryKey = 'idpes';
protected $casts = [
'data_nasc' => 'date',
];
/**
* @var array
*/
protected $fillable = [
'idpes',
'data_nascimento',
'zona_localizacao_censo',
'idpes',
'data_nasc',
'sexo',
'idpes_mae',
'idpes_pai',
'idpes_responsavel',
'idesco',
'ideciv',
'idpes_con',
'data_uniao',
'data_obito',
'nacionalidade',
'idpais_estrangeiro',
'data_chegada_brasil',
'idmun_nascimento',
'ultima_empresa',
'nome_mae',
'nome_pai',
'nome_conjuge',
'nome_responsavel',
'justificativa_provisorio',
'idpes_rev',
'data_rev',
'origem_gravacao',
'idpes_cad',
'data_cad',
'operacao',
'ref_cod_sistema',
'cpf',
'ref_cod_religiao',
'nis_pis_pasep',
'sus',
'ocupacao',
'empresa',
'pessoa_contato',
'renda_mensal',
'data_admissao',
'ddd_telefone_empresa',
'telefone_empresa',
'falecido',
'ativo',
'ref_usuario_exc',
'data_exclusao',
'zona_localizacao_censo',
'tipo_trabalho',
'local_trabalho',
'horario_inicial_trabalho',
'horario_final_trabalho',
'nome_social',
'pais_residencia',
'localizacao_diferenciada',
'ideciv',
'observacao',
];
/**
* @return BelongsToMany
*/
public function race()
{
return $this->belongsToMany(
LegacyRace::class,
'cadastro.fisica_raca',
'ref_idpes',
'ref_cod_raca'
);
}
/**
* @return BelongsToMany
*/
public function deficiency()
{
return $this->belongsToMany(
LegacyDeficiency::class,
'cadastro.fisica_deficiencia',
'ref_idpes',
'ref_cod_deficiencia'
);
}
/**
* @return HasOne
*/
public function person()
{
return $this->belongsTo(LegacyPerson::class, 'idpes', 'idpes');
}
/**
* @return HasOne
*/
public function student()
{
return $this->hasOne(LegacyStudent::class, 'ref_idpes', 'idpes');
}
public function mother()
{
return $this->belongsTo(LegacyPerson::class, 'idpes_mae', 'idpes');
}
public function father()
{
return $this->belongsTo(LegacyPerson::class, 'idpes_pai', 'idpes');
}
public function responsible()
{
return $this->belongsTo(LegacyPerson::class, 'idpes_responsavel', 'idpes');
}
/**
* @return HasOne
*/
public function document()
{
return $this->hasOne(LegacyDocument::class, 'idpes');
}
/**
* @return HasOne
*/
public function picture()
{
return $this->hasOne(LegacyIndividualPicture::class, 'idpes');
}
public function city(): BelongsTo
{
return $this->belongsTo(LegacyCity::class, 'idmun_nascimento', 'idmun');
}
public function cityBirth(): BelongsTo
{
return $this->belongsTo(City::class, 'idmun_nascimento');
}
/**
* {@inheritDoc}
*/
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->data_cad = now();
$model->origem_gravacao = 'M';
$model->operacao = 'I';
$model->pais_residencia = $model->pais_residencia ?? 76;
});
}
public static function findByCpf(string|int $cpf): ?Model
{
$cpf = preg_replace('/\D/', '', $cpf);
if ($cpf === null) {
return null;
}
return static::query()->where('cpf', (int) $cpf)->first();
}
protected function cpf(): Attribute
{
return Attribute::make(
get: fn ($value) => int2CPF($value),
);
}
protected function genderName(): Attribute
{
return Attribute::make(
get: fn () => $this->sexo ? $this->sexo === 'M' ? 'Masculino' : 'Feminino' : null
);
}
protected function nationalityName(): Attribute
{
return Attribute::make(
get: fn () => (new Nationality())->getDescriptiveValues()[$this->nacionalidade]
);
}
protected function socialName(): Attribute
{
return Attribute::make(
get: fn () => !empty($this->nome_social) ? $this->nome_social : null
);
}
protected function realName(): Attribute
{
return Attribute::make(
get: function () {
if ($this->social_name) {
return $this->social_name;
}
return $this->person->name;
}
);
}
protected function birthdate(): Attribute
{
return Attribute::make(
get: fn () => $this->data_nasc,
);
}
protected function parentsName(): Attribute
{
return Attribute::make(
get: function () {
$data = collect();
$data->push($this->father->name);
$data->push($this->mother->name);
return $data->filter()->implode(' e ');
},
);
}
}