103 lines
2.5 KiB
PHP
103 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace Koneko\VuexyAdmin\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class ContactableAddress extends Model
|
|
{
|
|
protected $table = 'contactable_addresses';
|
|
|
|
protected $fillable = [
|
|
'contactable_id',
|
|
'contactable_type',
|
|
'type',
|
|
'c_pais',
|
|
'c_codigo_postal',
|
|
'c_estado',
|
|
'c_localidad',
|
|
'c_municipio',
|
|
'c_colonia',
|
|
'direccion',
|
|
'num_ext',
|
|
'num_int',
|
|
'referencia',
|
|
'lat',
|
|
'lng',
|
|
'preference_level',
|
|
'notes'
|
|
];
|
|
|
|
protected $casts = [
|
|
'lat' => 'decimal:6',
|
|
'lng' => 'decimal:6',
|
|
'preference_level' => 'integer',
|
|
];
|
|
|
|
/**
|
|
* Relación polimórfica con cualquier modelo (User, Empresa, etc.)
|
|
*/
|
|
public function contactable()
|
|
{
|
|
return $this->morphTo();
|
|
}
|
|
|
|
/**
|
|
* Obtiene el país de la dirección basado en el catálogo SAT
|
|
*/
|
|
public function country()
|
|
{
|
|
return $this->belongsTo(SatPais::class, 'c_pais', 'c_pais');
|
|
}
|
|
|
|
/**
|
|
* Obtiene el estado basado en el catálogo SAT
|
|
*/
|
|
public function state()
|
|
{
|
|
return $this->belongsTo(SatEstado::class, ['c_estado', 'c_pais'], ['c_estado', 'c_pais']);
|
|
}
|
|
|
|
/**
|
|
* Obtiene el municipio basado en el catálogo SAT
|
|
*/
|
|
public function municipality()
|
|
{
|
|
return $this->belongsTo(SatMunicipio::class, ['c_municipio', 'c_estado'], ['c_municipio', 'c_estado']);
|
|
}
|
|
|
|
/**
|
|
* Obtiene la localidad basada en el catálogo SAT
|
|
*/
|
|
public function locality()
|
|
{
|
|
return $this->belongsTo(SatLocalidad::class, ['c_localidad', 'c_estado'], ['c_localidad', 'c_estado']);
|
|
}
|
|
|
|
/**
|
|
* Obtiene la colonia basada en el catálogo SAT
|
|
*/
|
|
public function neighborhood()
|
|
{
|
|
return $this->belongsTo(SatColonia::class, ['c_colonia', 'c_codigo_postal'], ['c_colonia', 'c_codigo_postal']);
|
|
}
|
|
|
|
/**
|
|
* Define si la dirección es preferida
|
|
*/
|
|
public function isPreferred()
|
|
{
|
|
return $this->preference_level === 1;
|
|
}
|
|
|
|
/**
|
|
* Devuelve la dirección en formato legible
|
|
*/
|
|
public function getFullAddressAttribute()
|
|
{
|
|
return "{$this->direccion}, {$this->num_ext}" .
|
|
($this->num_int ? " Int. {$this->num_int}" : '') .
|
|
", {$this->neighborhood->nombre ?? ''}, {$this->municipality->nombre ?? ''}, {$this->state->nombre ?? ''}, {$this->country->nombre ?? ''}";
|
|
}
|
|
}
|