operador === null) { $pa = $this->proyecto_agente(); if ($pa) { $id = $this->proyecto_agente()->agente_tipo()->agente()->id; $this->operador = Model::factory(Operador::class)->find_one($id); } } return $this->operador; } protected $proyecto_agente; public function proyecto_agente() { if ($this->proyecto_agente === null) { $this->proyecto_agente = $this->belongs_to(ProyectoAgente::class, 'agente')->find_one(); } return $this->proyecto_agente; } protected $estados; public function estados() { if ($this->estados === null) { $this->estados = $this->has_many(EstadoVenta::class, 'venta')->order_by_asc('fecha', 'id')->find_many(); } return $this->estados; } protected $activa; public function activa() { if ($this->activa === null) { $this->activa = false; foreach ($this->estados() as $estado) { if ($estado->tipo()->activa == 1) { $this->activa = true; } } } return $this->activa; } protected $propietario_obj; public function propietario() { if ($this->propietario_obj === null) { $this->propietario_obj = $this->belongs_to(Propietario::class, 'propietario', 'rut')->find_one(); } return $this->propietario_obj; } protected $propiedad_obj; public function propiedad() { if ($this->propiedad_obj === null) { $this->propiedad_obj = $this->belongs_to(Propiedad::class, 'propiedad')->find_one(); } return $this->propiedad_obj; } protected $comision; public function comision() { if ($this->comision === null) { $proyecto_id = $this->propiedad()->unidades()[0]->proyecto_tipo_unidad()->proyecto()->id; $comision = array_values(array_filter($this->operador()->comisiones(), function($item) use ($proyecto_id) { return ($item->proyecto->id == $proyecto_id); })); $comision = $comision[0]->comision; $this->comision = (object) [ 'valor' => $comision / 100, 'total' => $comision / 100 * $this->valor_uf ]; } return $this->comision; } public function as_array() { $arr = parent::as_array(); $arr['propietario'] = $this->propietario()->as_array(); $arr['propiedad'] = $this->propiedad()->as_array(); $arr['valor'] = '$ ' . number_format($this->valor_uf, 2, ',', '.'); if ($this->operador()) { $arr['operador'] = $this->operador()->as_array(); $arr['comision'] = (array) $this->comision(); $arr['comision']['formateada'] = '$ ' . number_format($this->comision()->total, 2, ',', '.'); } return $arr; } }