92 lines
2.3 KiB
PHP
92 lines
2.3 KiB
PHP
<?php
|
|
namespace Incoviba\old\Proyecto;
|
|
|
|
use Incoviba\Common\Alias\OldModel as Model;
|
|
use Incoviba\old\Common\Direccion;
|
|
|
|
/**
|
|
*
|
|
* @author Aldarien
|
|
*
|
|
* @property int id
|
|
* @property TipoAgente tipo
|
|
* @property int rut
|
|
* @property string descripcion
|
|
* @property string representante
|
|
* @property int telefono
|
|
* @property string correo
|
|
* @property Direccion direccion
|
|
* @property string giro
|
|
* @property string abreviacion
|
|
*
|
|
*/
|
|
class Agente extends Model {
|
|
protected $direccion_obj;
|
|
public function direccion() {
|
|
if ($this->direccion_obj === null) {
|
|
$this->direccion_obj = $this->setRelationship(Direccion::class, 'id', 'direccion')->one();
|
|
}
|
|
return $this->direccion_obj;
|
|
//return $this->belongs_to(Direccion::class, 'direccion')->findOne();
|
|
}
|
|
protected $tipo_obj;
|
|
public function tipo() {
|
|
if ($this->tipo_obj === null) {
|
|
$this->tipo_obj = $this->setRelationship(TipoAgente::class, 'id', 'tipo')->one();
|
|
}
|
|
return $this->tipo_obj;
|
|
//return $this->belongs_to(TipoAgente::class, 'tipo')->findOne();
|
|
}
|
|
protected $tipos;
|
|
public function tipos(int $tipo = 0)
|
|
{
|
|
if ($this->tipos === null) {
|
|
$this->tipos = [];
|
|
}
|
|
if (!isset($this->tipos[$tipo])) {
|
|
$this->tipos[$tipo] = $this->setRelationship(AgenteTipo::class, 'id', 'agente');
|
|
if ($tipo != 0) {
|
|
$this->tipos[$tipo] = $this->tipos[$tipo]->where(['tipo', $tipo]);
|
|
}
|
|
$this->tipos[$tipo];
|
|
}
|
|
return $this->tipos[$tipo];
|
|
|
|
/*if ($tipo == 0) {
|
|
return $this->hasMany(AgenteTipo::class, 'agente')->findMany();
|
|
}
|
|
return $this->hasMany(AgenteTipo::class, 'agente')->where('tipo', $tipo)->findOne();*/
|
|
}
|
|
protected $proyectos;
|
|
public function proyectos() {
|
|
if ($this->proyectos === null) {
|
|
$proyectos = [];
|
|
foreach ($this->tipos() as $tipo) {
|
|
foreach ($tipo->proyectos() as $proyecto) {
|
|
$proyectos []= $proyecto->proyecto();
|
|
}
|
|
}
|
|
usort($proyectos, function($a, $b) {
|
|
return strcmp($a->descripcion, $b->descripcion);
|
|
});
|
|
$this->proyectos = $proyectos;
|
|
}
|
|
return $this->proyectos;
|
|
}
|
|
public function hasProyecto(int $id): bool {
|
|
$proyectos = $this->proyectos();
|
|
foreach ($proyectos as $proyecto) {
|
|
if ($proyecto->id == $id) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
public function isOperador(): bool {
|
|
if ($this->tipos(19)) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|