API
This commit is contained in:
84
api/src/Operador.php
Normal file
84
api/src/Operador.php
Normal file
@ -0,0 +1,84 @@
|
||||
<?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 as_array() {
|
||||
$arr = parent::as_array();
|
||||
$arr['comisiones'] = $this->comisiones();
|
||||
return $arr;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user