93 lines
2.3 KiB
PHP
93 lines
2.3 KiB
PHP
<?php
|
|
namespace Incoviba;
|
|
|
|
use \Model;
|
|
|
|
class Operador extends Agente {
|
|
protected $venta;
|
|
public function ventas() {
|
|
if ($this->ventas === null) {
|
|
$ventas = [];
|
|
foreach ($this->agente_tipos() as $at) {
|
|
foreach ($at->proyecto_agentes() as $pa) {
|
|
$ventas = array_merge($ventas, $pa->ventas());
|
|
}
|
|
}
|
|
$this->ventas = $ventas;
|
|
}
|
|
return $this->ventas;
|
|
}
|
|
protected $comisiones;
|
|
public function comisiones() {
|
|
if ($this->comisiones === null) {
|
|
$comisiones = [];
|
|
foreach ($this->agente_tipos() as $at) {
|
|
foreach ($at->proyecto_agentes() as $pa) {
|
|
$comisiones []= (object) ['proyecto' => (object) $pa->proyecto()->as_array(), 'comision' => $pa->comision];
|
|
}
|
|
}
|
|
$this->comisiones = $comisiones;
|
|
}
|
|
return $this->comisiones;
|
|
}
|
|
|
|
public static function add($data) {
|
|
$fields = [
|
|
'rut',
|
|
'descripcion',
|
|
'abreviacion',
|
|
'representante',
|
|
'telefono',
|
|
'correo'
|
|
];
|
|
$input = array_intersect_key($data, array_combine($fields, $fields));
|
|
$tipo = Model::factory(TipoAgente::class)->where('descripcion', 'operador')->find_one();
|
|
$input['tipo'] = $tipo->id;
|
|
$validate = [
|
|
'rut',
|
|
'descripcion',
|
|
'abreviacion'
|
|
];
|
|
$found = false;
|
|
foreach ($validate as $val) {
|
|
$operador = Model::factory(Operador::class)->where($val, $input[$val])->find_one();
|
|
if ($operador) {
|
|
$found = true;
|
|
break;
|
|
}
|
|
}
|
|
$created = false;
|
|
if (!$found) {
|
|
$operador = Operador::create($input);
|
|
$created = $operador->save();
|
|
$input = [
|
|
'agente' => $operador->id,
|
|
'tipo' => $tipo->id
|
|
];
|
|
$at = AgenteTipo::create($input);
|
|
$at->save();
|
|
}
|
|
$output = [
|
|
'input' => $data,
|
|
'operador' => $operador->as_array(),
|
|
'new' => !$found,
|
|
'created' => $created
|
|
];
|
|
return $output;
|
|
}
|
|
public function addProyecto($data) {
|
|
$data['agente'] = $this->findAgenteTipo('operador')->id;
|
|
$pa = ProyectoAgente::add($data);
|
|
return [
|
|
'operador' => $this->as_array(),
|
|
'proyecto_agente' => $pa
|
|
];
|
|
}
|
|
|
|
public function as_array() {
|
|
$arr = parent::as_array();
|
|
$arr['comisiones'] = $this->comisiones();
|
|
return $arr;
|
|
}
|
|
}
|