Agentes
This commit is contained in:
@ -2,9 +2,20 @@
|
||||
use Incoviba\Controller\API\Inmobiliarias;
|
||||
|
||||
$app->group('/inmobiliarias', function($app) {
|
||||
$folder = implode(DIRECTORY_SEPARATOR, [__DIR__, 'inmobiliarias']);
|
||||
if (file_exists($folder)) {
|
||||
$files = new FilesystemIterator($folder);
|
||||
foreach ($files as $file) {
|
||||
if ($file->isDir()) {
|
||||
continue;
|
||||
}
|
||||
include_once $file->getRealPath();
|
||||
}
|
||||
}
|
||||
$app->get('[/]', Inmobiliarias::class);
|
||||
});
|
||||
$app->group('/inmobiliaria/{inmobiliaria_rut}', function($app) {
|
||||
$app->post('/agentes', [Inmobiliarias::class, 'agentes']);
|
||||
$app->get('/cuentas[/]', [Inmobiliarias::class, 'cuentas']);
|
||||
$app->get('/proyectos[/]', [Inmobiliarias::class, 'proyectos']);
|
||||
});
|
||||
|
11
app/resources/routes/api/inmobiliarias/agentes.php
Normal file
11
app/resources/routes/api/inmobiliarias/agentes.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
use Incoviba\Controller\API\Inmobiliarias\Agentes;
|
||||
|
||||
$app->group('/agentes', function($app) {
|
||||
$app->post('/add[/]', [Agentes::class, 'add']);
|
||||
$app->post('/register[/]', [Agentes::class, 'register']);
|
||||
$app->get('[/]', Agentes::class);
|
||||
});
|
||||
$app->group('/agente/{agente_id}', function($app) {
|
||||
$app->post('/edit[/]', [Agentes::class, 'edit']);
|
||||
});
|
@ -55,4 +55,29 @@ class Inmobiliarias
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function agentes(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Repository\Inmobiliaria $inmobiliariaRepository,
|
||||
Repository\Inmobiliaria\SociedadAgente $sociedadAgenteRepository,
|
||||
Repository\Inmobiliaria\TipoAgente $tipoAgenteRepository,
|
||||
int $inmobiliaria_rut): ResponseInterface
|
||||
{
|
||||
$input = $request->getParsedBody();
|
||||
$output = [
|
||||
'sociedad_rut' => $inmobiliaria_rut,
|
||||
'input' => $input,
|
||||
'sociedad' => null,
|
||||
'agentes' => []
|
||||
];
|
||||
try {
|
||||
$inmobiliaria = $inmobiliariaRepository->fetchById($inmobiliaria_rut);
|
||||
$output['sociedad'] = $inmobiliaria;
|
||||
if (isset($input['tipo_agente_id'])) {
|
||||
$tipo = $tipoAgenteRepository->fetchById($input['tipo_agente_id']);
|
||||
$output['agentes'] = $sociedadAgenteRepository->fetchBySociedadAndTipo($inmobiliaria->rut, $tipo->id);
|
||||
} else {
|
||||
$output['agentes'] = $sociedadAgenteRepository->fetchBySociedad($inmobiliaria->rut);
|
||||
}
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
||||
|
90
app/src/Controller/API/Inmobiliarias/Agentes.php
Normal file
90
app/src/Controller/API/Inmobiliarias/Agentes.php
Normal file
@ -0,0 +1,90 @@
|
||||
<?php
|
||||
namespace Incoviba\Controller\API\Inmobiliarias;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Controller\API\withJson;
|
||||
use Incoviba\Repository;
|
||||
|
||||
class Agentes
|
||||
{
|
||||
use withJson;
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Repository\Inmobiliaria\Agente $agenteRepository): ResponseInterface
|
||||
{
|
||||
$output = [
|
||||
'agentes' => []
|
||||
];
|
||||
try {
|
||||
$output['agentes'] = $agenteRepository->fetchAll('abreviacion');
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function add(ServerRequestInterface $request, ResponseInterface $response, Repository\Inmobiliaria\Agente $agenteRepository): ResponseInterface
|
||||
{
|
||||
$input = $request->getParsedBody();
|
||||
$output = [
|
||||
'input' => $input,
|
||||
'agente' => null
|
||||
];
|
||||
try {
|
||||
$agente = $agenteRepository->create($input);
|
||||
$output['agente'] = $agenteRepository->save($agente);
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function edit(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Repository\Inmobiliaria\Agente $agenteRepository, int $agente_id): ResponseInterface
|
||||
{
|
||||
$input = $request->getParsedBody();
|
||||
$output = [
|
||||
'agente_id' => $agente_id,
|
||||
'input' => $input,
|
||||
'agente' => null
|
||||
];
|
||||
try {
|
||||
$agente = $agenteRepository->fetchById($agente_id);
|
||||
$output['agente'] = $agenteRepository->edit($agente, $input);
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function register(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Repository\Inmobiliaria $inmobiliariaRepository,
|
||||
Repository\Inmobiliaria\Agente $agenteRepository,
|
||||
Repository\Inmobiliaria\TipoAgente $tipoAgenteRepository,
|
||||
Repository\Inmobiliaria\AgenteTipo $agenteTipoRepository,
|
||||
Repository\Inmobiliaria\SociedadAgente $sociedadAgenteRepository): ResponseInterface
|
||||
{
|
||||
$input = $request->getParsedBody();
|
||||
$output = [
|
||||
'input' => $input,
|
||||
'sociedad' => null,
|
||||
'agente' => null,
|
||||
'tipo_agente' => null,
|
||||
'sociedad_agente' => null
|
||||
];
|
||||
try {
|
||||
$sociedad = $inmobiliariaRepository->fetchById($input['sociedad_rut']);
|
||||
$output['sociedad'] = $sociedad;
|
||||
$agente = $agenteRepository->fetchById($input['agente_id']);
|
||||
$output['agente'] = $agente;
|
||||
$tipo = $tipoAgenteRepository->fetchById($input['tipo_agente_id']);
|
||||
$output['tipo_agente'] = $tipo;
|
||||
$agenteTipo = $agenteTipoRepository->fetchByAgenteAndTipo($agente->id, $tipo->id);
|
||||
try {
|
||||
$output['sociedad_agente'] = $sociedadAgenteRepository->fetchBySociedadAndAgenteAndTipo($sociedad->rut, $agente->id, $tipo->id);
|
||||
} catch (EmptyResult) {
|
||||
$data = [
|
||||
'sociedad_rut' => $sociedad->rut,
|
||||
'agente_tipo_id' => $agenteTipo->id
|
||||
];
|
||||
$sociedadAgente = $sociedadAgenteRepository->create($data);
|
||||
$output['sociedad_agente'] = $sociedadAgenteRepository->save($sociedadAgente);
|
||||
}
|
||||
} catch (EmptyResult) {
|
||||
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
40
app/src/Model/Inmobiliaria/Agente.php
Normal file
40
app/src/Model/Inmobiliaria/Agente.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
namespace Incoviba\Model\Inmobiliaria;
|
||||
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Model;
|
||||
|
||||
class Agente extends Ideal\Model
|
||||
{
|
||||
public ?int $rut;
|
||||
public string $descripcion;
|
||||
public ?string $contacto;
|
||||
public ?int $telefono;
|
||||
public ?string $correo;
|
||||
public ?Model\Direccion $direccion;
|
||||
public ?string $giro;
|
||||
public string $abreviacion;
|
||||
|
||||
protected ?array $tiposAgentes;
|
||||
public function tipos(): ?array
|
||||
{
|
||||
if (!isset($this->tiposAgentes)) {
|
||||
$this->tiposAgentes = $this->runFactory('tipos_agentes');
|
||||
}
|
||||
return $this->tiposAgentes;
|
||||
}
|
||||
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return array_merge(parent::jsonSerialize(), [
|
||||
'rut' => $this->rut,
|
||||
'descripcion' => $this->descripcion,
|
||||
'contacto' => $this->contacto,
|
||||
'telefono' => $this->telefono,
|
||||
'correo' => $this->correo,
|
||||
'direccion' => $this->direccion,
|
||||
'giro' => $this->giro,
|
||||
'abreviacion' => $this->abreviacion,
|
||||
]);
|
||||
}
|
||||
}
|
10
app/src/Model/Inmobiliaria/AgenteTipo.php
Normal file
10
app/src/Model/Inmobiliaria/AgenteTipo.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
namespace Incoviba\Model\Inmobiliaria;
|
||||
|
||||
use Incoviba\Common\Ideal;
|
||||
|
||||
class AgenteTipo extends Ideal\Model
|
||||
{
|
||||
public Agente $agente;
|
||||
public TipoAgente $tipoAgente;
|
||||
}
|
19
app/src/Model/Inmobiliaria/SociedadAgente.php
Normal file
19
app/src/Model/Inmobiliaria/SociedadAgente.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
namespace Incoviba\Model\Inmobiliaria;
|
||||
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Model;
|
||||
|
||||
class SociedadAgente extends Ideal\Model
|
||||
{
|
||||
public Model\Inmobiliaria $sociedad;
|
||||
public AgenteTipo $agenteTipo;
|
||||
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return [
|
||||
'sociedad_rut' => $this->sociedad->rut,
|
||||
'agente_tipo_id' => $this->agenteTipo->id
|
||||
];
|
||||
}
|
||||
}
|
11
app/src/Model/Inmobiliaria/TipoAgente.php
Normal file
11
app/src/Model/Inmobiliaria/TipoAgente.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
namespace Incoviba\Model\Inmobiliaria;
|
||||
|
||||
use Incoviba\Model;
|
||||
|
||||
class TipoAgente extends Model\Tipo
|
||||
{
|
||||
public string $icono;
|
||||
public string $color;
|
||||
public string $bgcolor;
|
||||
}
|
47
app/src/Repository/Inmobiliaria/Agente.php
Normal file
47
app/src/Repository/Inmobiliaria/Agente.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace Incoviba\Repository\Inmobiliaria;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Implement;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
|
||||
class Agente extends Ideal\Repository
|
||||
{
|
||||
public function __construct(Define\Connection $connection, protected Repository\Direccion $direccionRepository, protected TipoAgente $tipoAgenteRepository)
|
||||
{
|
||||
parent::__construct($connection);
|
||||
$this->setTable('agente');
|
||||
}
|
||||
|
||||
public function create(?array $data = null): Model\Inmobiliaria\Agente
|
||||
{
|
||||
$map = (new Implement\Repository\MapperParser(['rut', 'descripcion', 'telefono', 'correo', 'giro', 'abreviacion']))
|
||||
->register('representante', (new Implement\Repository\Mapper())
|
||||
->setProperty('contacto'))
|
||||
->register('direccion', (new Implement\Repository\Mapper())
|
||||
->setFunction(function(?array $data) {
|
||||
if ($data === null) {
|
||||
return null;
|
||||
}
|
||||
return $this->direccionRepository->fetchById($data['direccion']);
|
||||
}));
|
||||
return $this->parseData(new Model\Inmobiliaria\Agente(), $data, $map);
|
||||
}
|
||||
public function save(Define\Model $model): Model\Inmobiliaria\Agente
|
||||
{
|
||||
$model->id = $this->saveNew([
|
||||
'rut', 'descripcion', 'representante', 'telefono', 'correo', 'giro', 'abreviacion'
|
||||
], [
|
||||
$model->rut, $model->descripcion, $model->contacto, $model->telefono, $model->correo, $model->giro, $model->abreviacion
|
||||
]);
|
||||
return $model;
|
||||
}
|
||||
public function edit(Define\Model $model, array $new_data): Model\Inmobiliaria\Agente
|
||||
{
|
||||
return $this->update($model, [
|
||||
'rut', 'descripcion', 'representante', 'telefono', 'correo', 'giro', 'abreviacion'
|
||||
], $new_data);
|
||||
}
|
||||
}
|
68
app/src/Repository/Inmobiliaria/AgenteTipo.php
Normal file
68
app/src/Repository/Inmobiliaria/AgenteTipo.php
Normal file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
namespace Incoviba\Repository\Inmobiliaria;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Implement;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
|
||||
class AgenteTipo extends Ideal\Repository
|
||||
{
|
||||
public function __construct(Define\Connection $connection, protected Agente $agenteRepository,
|
||||
protected TipoAgente $tipoAgenteRepository)
|
||||
{
|
||||
parent::__construct($connection);
|
||||
$this->setTable('agente_tipo');
|
||||
}
|
||||
|
||||
public function create(?array $data = null): Model\Inmobiliaria\AgenteTipo
|
||||
{
|
||||
$map = (new Implement\Repository\MapperParser())
|
||||
->register('agente', (new Implement\Repository\Mapper())
|
||||
->setFunction(function(?array $data) {
|
||||
return ($data === null) ? null : $this->agenteRepository->fetchById($data['agente']);
|
||||
}))
|
||||
->register('tipo', (new Implement\Repository\Mapper())
|
||||
->setFunction(function(?array $data) {
|
||||
return ($data === null) ? null : $this->tipoAgenteRepository->fetchById($data['tipo']);
|
||||
}));
|
||||
return $this->parseData(new Model\Inmobiliaria\AgenteTipo(), $data, $map);
|
||||
}
|
||||
public function save(Define\Model $model): Model\Inmobiliaria\AgenteTipo
|
||||
{
|
||||
$model->id = $this->saveNew(['agente', 'tipo'], [
|
||||
$model->agente->id, $model->agenteTipo->id
|
||||
]);
|
||||
return $model;
|
||||
}
|
||||
public function edit(Define\Model $model, array $new_data): Model\Inmobiliaria\AgenteTipo
|
||||
{
|
||||
return $this->update($model, ['agente', 'tipo'], $new_data);
|
||||
}
|
||||
|
||||
public function fetchByAgente(int $agente_id): array
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where('agente = ?');
|
||||
return $this->fetchMany($query, [$agente_id]);
|
||||
}
|
||||
public function fetchByTipo(int $tipo_agente_id): array
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where('tipo = ?');
|
||||
return $this->fetchMany($query, [$tipo_agente_id]);
|
||||
}
|
||||
public function fetchByAgenteAndTipo(int $agente_id, int $tipo_agente_id): Model\Inmobiliaria\AgenteTipo
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where('agente = ? AND tipo = ?');
|
||||
return $this->fetchOne($query, [$agente_id, $tipo_agente_id]);
|
||||
}
|
||||
}
|
105
app/src/Repository/Inmobiliaria/SociedadAgente.php
Normal file
105
app/src/Repository/Inmobiliaria/SociedadAgente.php
Normal file
@ -0,0 +1,105 @@
|
||||
<?php
|
||||
namespace Incoviba\Repository\Inmobiliaria;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Implement;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
|
||||
class SociedadAgente extends Ideal\Repository
|
||||
{
|
||||
public function __construct(Define\Connection $connection, protected Repository\Inmobiliaria $inmobiliariaRepository,
|
||||
protected AgenteTipo $agenteRepository)
|
||||
{
|
||||
parent::__construct($connection);
|
||||
$this->setTable('sociedad_agente');
|
||||
}
|
||||
public function create(?array $data = null): Model\Inmobiliaria\SociedadAgente
|
||||
{
|
||||
$map = (new Implement\Repository\MapperParser())
|
||||
->register('sociedad_rut', (new Implement\Repository\Mapper())
|
||||
->setProperty('sociedad')
|
||||
->setFunction(function(?array $data) {
|
||||
return ($data === null) ? null : $this->inmobiliariaRepository->fetchById($data['sociedad_rut']);
|
||||
}))
|
||||
->register('agente_tipo_id', (new Implement\Repository\Mapper())
|
||||
->setProperty('agenteTipo')
|
||||
->setFunction(function(?array $data) {
|
||||
return ($data === null) ? null : $this->agenteRepository->fetchById($data['agente_tipo_id']);
|
||||
}));
|
||||
return $this->parseData(new Model\Inmobiliaria\SociedadAgente(), $data, $map);
|
||||
}
|
||||
public function save(Define\Model $model): Model\Inmobiliaria\SociedadAgente
|
||||
{
|
||||
$model->id = $this->saveNew([
|
||||
'sociedad_rut', 'agente_tipo_id'
|
||||
], [
|
||||
$model->sociedad->rut, $model->agenteTipo->id
|
||||
]);
|
||||
return $model;
|
||||
}
|
||||
public function edit(Define\Model $model, array $new_data): Model\Inmobiliaria\SociedadAgente
|
||||
{
|
||||
return $this->update($model, ['sociedad_rut', 'agente_tipo_id'], $new_data);
|
||||
}
|
||||
public function load(array $data_row): Define\Model
|
||||
{
|
||||
$model = $this->create($data_row);
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function fetchBySociedad(int $sociedad_rut): array
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where('sociedad_rut = ?');
|
||||
return $this->fetchMany($query, [$sociedad_rut]);
|
||||
}
|
||||
public function fetchByAgente(int $agente_id): array
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select('a.*')
|
||||
->from("{$this->getTable()} a")
|
||||
->joined('JOIN agente_tipo at ON a.agente_tipo_id = at.id')
|
||||
->where('at.agente = ?');
|
||||
return $this->fetchMany($query, [$agente_id]);
|
||||
}
|
||||
public function fetchByTipo(int $tipo_agente_id): array
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select('a.*')
|
||||
->from("{$this->getTable()} a")
|
||||
->joined('JOIN agente_tipo at ON a.agente_tipo_id = at.id')
|
||||
->where('at.tipo = ?');
|
||||
return $this->fetchMany($query, [$tipo_agente_id]);
|
||||
}
|
||||
public function fetchBySociedadAndTipo(int $sociedad_rut, int $tipo_agente_id): array
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select('a.*')
|
||||
->from("{$this->getTable()} a")
|
||||
->joined('JOIN agente_tipo at ON a.agente_tipo_id = at.id')
|
||||
->where('sociedad_rut = ? AND at.tipo = ?');
|
||||
return $this->fetchMany($query, [$sociedad_rut, $tipo_agente_id]);
|
||||
}
|
||||
public function fetchBySociedadAndAgente(int $sociedad_rut, int $agente_id): array
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select('a.*')
|
||||
->from("{$this->getTable()} a")
|
||||
->joined('JOIN agente_tipo at ON a.agente_tipo_id = at.id')
|
||||
->where('sociedad_rut = ? AND at.agente = ?');
|
||||
return $this->fetchMany($query, [$sociedad_rut, $agente_id]);
|
||||
}
|
||||
public function fetchBySociedadAndAgenteAndTipo(int $sociedad_rut, int $agente_id, int $tipo_agente_id): array
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select('a.*')
|
||||
->from("{$this->getTable()} a")
|
||||
->joined('JOIN agente_tipo at ON a.agente_tipo_id = at.id')
|
||||
->where('sociedad_rut = ? AND at.agente = ? AND at.tipo = ?');
|
||||
return $this->fetchMany($query, [$sociedad_rut, $agente_id, $tipo_agente_id]);
|
||||
}
|
||||
}
|
20
app/src/Repository/Inmobiliaria/TipoAgente.php
Normal file
20
app/src/Repository/Inmobiliaria/TipoAgente.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
namespace Incoviba\Repository\Inmobiliaria;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
|
||||
class TipoAgente extends Repository\Tipo
|
||||
{
|
||||
public function __construct(Define\Connection $connection)
|
||||
{
|
||||
parent::__construct($connection);
|
||||
$this->setTable('tipo_agente');
|
||||
}
|
||||
|
||||
protected function getBlank(): Model\Inmobiliaria\TipoAgente
|
||||
{
|
||||
return new Model\Inmobiliaria\TipoAgente();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user