Compare commits

10 Commits

Author SHA1 Message Date
4d00f10274 Mappers 2023-06-26 10:49:25 -04:00
42a97bb074 0.1.0 2022-06-13 21:36:52 -04:00
3580738273 Files 2021-12-25 23:17:15 -03:00
bbee033a8a Files 2021-11-30 22:33:41 -03:00
f589ff960b Files 2021-11-30 18:04:41 -03:00
87323b22d8 Composer 2021-11-30 18:04:27 -03:00
e91fa9a5dd Sample environment files 2021-11-30 18:04:08 -03:00
ef46450dd3 Ignore key 2021-11-30 18:03:57 -03:00
0e1d02e5b7 Docker 2021-11-30 18:02:29 -03:00
1e3018b6b3 Ignore environment and logs 2021-11-30 18:01:49 -03:00
179 changed files with 7443 additions and 0 deletions

2
.adminer.env.sample Normal file
View File

@ -0,0 +1,2 @@
ADMINER_DESIGN=dracula
ADMINER_PLUGINS=dump-json

1
.api.key.sample Normal file
View File

@ -0,0 +1 @@
API_KEY=######################

4
.db.env.sample Normal file
View File

@ -0,0 +1,4 @@
MYSQL_ROOT_PASSWORD=password
MYSQL_USER=user
MYSQL_PASSWORD=password
MYSQL_DATABASE=database

3
.env.sample Normal file
View File

@ -0,0 +1,3 @@
DEBUG=true
BASE_URL=""
MYSQL_HOST=db

3
.gitignore vendored
View File

@ -1,3 +1,6 @@
**/*.env
**/*.key
**/vendor/
**/composer.lock
**/.idea
**/logs/

12
Dockerfile Normal file
View File

@ -0,0 +1,12 @@
FROM php:8-fpm
RUN apt-get update -y && apt-get install -y zip libzip-dev git
RUN docker-php-ext-install pdo pdo_mysql zip
RUN pecl install xdebug-3.1.1 \
&& docker-php-ext-enable xdebug
WORKDIR /app/api
COPY --from=composer /usr/bin/composer /usr/bin/composer

View File

@ -0,0 +1,30 @@
<?php
namespace Incoviba\API\Common\Alias;
use Incoviba\API\Common\Define\Controller\Json;
use Incoviba\API\Common\Factory\Mapper as MapperFactory;
use Incoviba\Mapper\Mapper;
class Controller
{
use Json;
protected MapperFactory $mapperFactory;
public function __construct(MapperFactory $mapperFactory) {
$this->mapperFactory = $mapperFactory;
}
protected array $mappers;
public function getMapper(string $name): Mapper
{
if (!class_exists($name)) {
throw new \InvalidArgumentException("Mapper {$name} not found.");
}
if (!isset($this->mappers)) {
$this->mappers = [];
}
if (!isset($this->mappers[$name])) {
$this->mappers[$name] = $this->mapperFactory->get($name);
}
return $this->mappers[$name];
}
}

120
common/Alias/Model.php Normal file
View File

@ -0,0 +1,120 @@
<?php
namespace Incoviba\API\Common\Alias;
use http\Exception\InvalidArgumentException;
use \Model as BaseModel;
use Incoviba\API\Common\Define\Model as ModelInterface;
use Incoviba\API\Common\Factory\Model as ModelFactory;
abstract class Model extends BaseModel implements ModelInterface {
const SELF_KEY = 'self_key';
const CHILD_KEY = 'child_key';
const PARENT_KEY = 'parent_key';
const SIBLING_KEY = 'sibling_key';
const SELF_THROUGH_KEY = 'self_through_key';
const SIBLING_THROUGH_KEY = 'sibling_through_key';
public function getTable(): string {
return static::$_table;
}
protected ModelFactory $factory;
public function setFactory(ModelFactory $factory): ModelInterface {
$this->factory = $factory;
return $this;
}
protected function validateDefinitions(array $definitions, array $minimum_requirements, array $default_values): array {
foreach ($default_values as $key => $val) {
if (!isset($definitions[$key])) {
$definitions[$key] = $val;
}
}
foreach ($minimum_requirements as $key) {
if (!isset($definitions[$key])) {
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
$class = get_called_class();
throw new InvalidArgumentException("Missing {$key} in {$trace[1]['function']} called in {$class}.");
}
}
return $definitions;
}
public function parentOf(string $child_class, array $definitions): bool|array {
$definitions = $this->validateDefinitions(
$definitions,
[Model::CHILD_KEY, Model::SELF_KEY],
[Model::SELF_KEY => 'id']
);
$where = [[$definitions[Model::CHILD_KEY], $this->{$definitions[Model::SELF_KEY]}]];
return $this->factory->find($child_class)->where($where)->many();
}
public function childOf(string $parent_class, array $definitions): bool|ModelInterface {
$definitions = $this->validateDefinitions(
$definitions,
[Model::PARENT_KEY, Model::SELF_KEY],
[Model::PARENT_KEY => 'id']
);
$where = [[$definitions[Model::PARENT_KEY], $this->{$definitions[Model::SELF_KEY]}]];
return $this->factory->find($parent_class)->where($where)->one();
}
public function siblingOf(string $sibling_class, string $connecting_table, array $definitions): ?array {
$definitions = $this->validateDefinitions(
$definitions,
[Model::SIBLING_KEY, Model::SIBLING_THROUGH_KEY, Model::SELF_THROUGH_KEY, Model::SELF_KEY],
[Model::SIBLING_KEY => 'id', Model::SELF_KEY => 'id']
);
$sibling_table = (new $sibling_class())->getTable();
$joins = [
[
$connecting_table,
"{$connecting_table}.{$definitions[Model::SIBLING_THROUGH_KEY]}",
"{$sibling_table}.{$definitions[Model::SIBLING_KEY]}"
]
];
$where = [
[$definitions[Model::SIBLING_KEY], "{$connecting_table}.{$definitions[Model::SIBLING_THROUGH_KEY]}"],
["{$connecting_table}.{$definitions[Model::SELF_THROUGH_KEY]}", $this->{$definitions[Model::SELF_KEY]}]
];
return $this->factory
->find($sibling_class)
->join($joins)
->select("{$sibling_table}.*")
->where($where)
->many();
}
public function toArray(): array {
return $this->as_array();
}
protected static function parseInput($input): array {
return array_intersect_key((array) $input, array_combine(static::$fields, static::$fields));
}
public static function add(ModelFactory $factory, $input): bool|ModelInterface {
$data = static::parseInput($input);
$class = get_called_class();
if (method_exists($class, 'find')) {
$obj = static::find($factory, $input);
} else {
$where = $data;
array_walk($where, function(&$item, $key) {
$item = [$key, $item];
});
$where = array_values($where);
$obj = $factory->find($class)->where($where)->one();
}
if ($obj === false or $obj === null) {
$obj = $factory->create($class, $data);
}
return $obj;
}
public function edit($input): ?array {
$data = static::parseInput($input);
foreach (static::$fields as $field) {
if ($this->{$field} != $data[$field]) {
$this->{$field} = $data[$field];
}
}
return $this->save();
}
}

View File

@ -0,0 +1,76 @@
<?php
namespace Incoviba\API\Common\Controller;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Incoviba\API\Common\Define\Controller\Json;
use Incoviba\API\Common\Service\Auth as Service;
use Incoviba\API\Common\Service\Login as LoginService;
use Incoviba\Mapper\User as UserMapper;
class Auth {
use Json;
public function generate(Request $request, Response $response, Service $service): Response {
$key = $service->generate();
return $this->withJson($response, compact('key'));
}
public function login(Request $request, Response $response, Service $service, LoginService $loginService, UserMapper $mapper): Response {
$post = json_decode($request->getBody());
$user = $mapper->fetchByName($post->name);
$output = [
'login' => false,
'token' => ''
];
if (!$user or !$user->enabled) {
return $this->withJson($response, $output);
}
if ($user->validate($post->password)) {
$token = $service->generateToken();
$status = $loginService->setToken($user, $token->selector, $token->token);
if ($status['logged_in']) {
$output['login'] = true;
$output['token'] = $token->full;
$output['expires'] = $status['expires'];
}
}
return $this->withJson($response, $output);
}
public function validate(Request $request, Response $response, LoginService $loginService): Response {
$post = json_decode($request->getBody());
$valid = $loginService->validate($post);
$output = [
'token' => $post->token
];
if ($valid) {
$output['status'] = 'Authorized';
} else {
$output['error'] = 'Not authorized';
}
return $this->withJson($response, $output, $valid ? 200 : 401);
}
public function user(Request $request, Response $response, LoginService $loginService): Response {
$post = json_decode($request->getBody());
$user = $loginService->getUser($post);
if (!$user) {
return $this->withJson($response, ['token' => $post->token, 'error' => 'Not authorized'], 401);
}
$output = [
'token' => $post->token,
'user' => $user->name
];
return $this->withJson($response, $output);
}
public function logout(Request $request, Response $response, LoginService $loginService): Response {
$post = json_decode($request->getBody());
$user = $loginService->getUser($post);
if (!$user) {
return $this->withJson($response, ['logout' => true]);
}
$output = [
'token' => $post->token,
'logout' => $loginService->logout($user)
];
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,80 @@
<?php
namespace Incoviba\API\Common\Controller;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Incoviba\API\Common\Factory\Model as Factory;
use Incoviba\API\Common\Define\Controller\Json;
use Incoviba\Common\Banco;
class Bancos {
use Json;
public function __invoke(Request $request, Response $response, Factory $factory): Response {
$bancos = $factory->find(Banco::class)->array();
$url = '' . $request->getUri();
array_walk($bancos, function (&$item) use ($url) {
$item['link'] = [
'rel' => 'banco',
'title' => $item['nombre'],
'href' => str_replace('/bancos', "/banco/{$item['id']}", $url)
];
});
return $this->withJson($response, compact('bancos'));
}
public function show(Request $request, Response $response, Factory $factory, $banco_id): Response {
$banco = $factory->find(Banco::class)->one($banco_id);
$output = [
'input' => $banco_id,
'banco' => $banco->toArray(),
'link' => [
'rel' => 'bancos',
'title' => 'Bancos',
'href' => str_replace("/banco/{$banco_id}", '/bancos', $request->getUri())
]
];
return $this->withJson($response, $output);
}
public function add(Request $request, Response $response, Factory $factory): Response {
$post = $request->getParsedBody();
$output = [
'input' => $post
];
if (in_array('bancos', $post)) {
$output['bancos'] = [];
foreach ($post['bancos'] as $input) {
$banco = Banco::add($factory, $input);
$banco []= [
'banco' => $banco->toArray(),
'created' => $banco->is_new() ? $banco->save() : false
];
}
} elseif (in_array('banco', $post)) {
$banco = Banco::add($factory, $post);
$output['banco'] = $banco;
$output['created'] = $banco->is_new() ? $banco->save() : false;
}
return $this->withJson($response, $output);
}
public function edit(Request $request, Response $response, Factory $factory, $banco_id): Response {
$post = $request->getParsedBody();
$input = compact('banco_id', 'post');
$banco = $factory->find(Banco::class)->one($banco_id);
$output = [
'input' => $input,
'banco' => $banco->toArray()
];
$output['edited'] = $banco->edit($post);
$output['changes'] = $banco->toArray();
return $this->withJson($response, $output);
}
public function delete(Request $request, Response $response, Factory $factory, $banco_id): Response {
$banco = $factory->find(Banco::class)->one($banco_id);
$output = [
'input' => $banco_id,
'banco' => $banco->toArray()
];
$output['deleted'] = $banco->delete();
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace Incoviba\API\Common\Controller;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Incoviba\API\Common\Define\Controller\Json;
class Base {
use Json;
public function __invoke(Request $request, Response $response): Response {
return $this->withJson($response, [
'api' => [
'version' => '1.0.0'
]
]);
}
public function info(Request $request, Response $response): Response {
phpinfo();
return $response;
}
}

View File

@ -0,0 +1,43 @@
<?php
namespace Incoviba\API\Common\Controller;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Incoviba\API\Common\Factory\Model as Factory;
use Incoviba\API\Common\Define\Controller\Json;
use Incoviba\Admin\Config;
class Configs {
use Json;
public function get(Request $request, Response $response, Factory $factory, $config_name): Response {
$config = $factory->find(Config::class)->where((['name', $config_name]))->one();
$output = [
'name' => $config_name,
'valid' => $config !== false,
'value' => $config->value
];
return $this->withJson($response, $output);
}
public function set(Request $request, Response $response, Factory $factory): Response {
$post = $request->getParsedBody();
$config = $factory->find(Config::class)->where([['name', $post['name']]])->one();
if (!$config) {
$config = Config::add($factory, $post);
} else {
$config->edit($post);
}
$output = [
'input' => $post,
'config' => null
];
if ($config !== false) {
$config->save();
$output['config'] = [
'name' => $config->name,
'value' => $config->value
];
}
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,166 @@
<?php
namespace Incoviba\API\Common\Controller;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Incoviba\API\Common\Define\Controller\Json;
use Incoviba\API\Common\Factory\Model as Factory;
use Incoviba\Inmobiliaria\Inmobiliaria;
class Inmobiliarias {
use Json;
public function __invoke(Request $request, Response $response, Factory $factory): Response {
$inmobiliarias = $factory->find(Inmobiliaria::class)->array();
$base_url = str_replace('/inmobiliarias', '{URL}', $request->getUri());
array_walk($inmobiliarias, function (&$item) use ($base_url) {
$link = [
'rel' => 'inmobiliaria',
'title' => $item['abreviacion'],
'href' => str_replace('{URL}',"/inmobiliaria/{$item['rut']}", $base_url)
];
$item['link'] = $link;
});
return $this->withJson($response, compact('inmobiliarias'));
}
protected function getInmobiliaria(Factory $factory, $inmobiliaria_rut): bool|Inmobiliaria {
return $factory->find(Inmobiliaria::class)->where([['rut', $inmobiliaria_rut]])->one();
}
public function show(Request $request, Response $response, Factory $factory, $inmobiliaria_rut): Response {
$inmobiliaria = $this->getInmobiliaria($factory, $inmobiliaria_rut);
$output = [
'input' => $inmobiliaria_rut,
'inmobiliaria' => $inmobiliaria->toArray(),
'link' => [
'rel' => 'inmobiliarias',
'title' => 'Inmobiliarias',
'href' => str_replace("/inmobiliaria/{$inmobiliaria_rut}", '/inmobiliarias', $request->getUri())
]
];
$base_url = str_replace("/inmobiliaria/{$inmobiliaria_rut}", "/inmobiliaria/{$inmobiliaria_rut}{URL}", $request->getUri());
$output['inmobiliaria']['links'] = [
[
'rel' => 'banco',
'title' => $inmobiliaria->banco()->nombre,
'href' => str_replace("/inmobiliaria/{$inmobiliaria_rut}", "/banco/{$inmobiliaria->banco}", $request->getUri())
],
[
'rel' => 'sociedad',
'title' => $inmobiliaria->sociedad()->descripcion,
'href' => str_replace("/inmobiliaria/{$inmobiliaria_rut}", "/sociedad/{$inmobiliaria->sociedad}", $request->getUri())
],
[
'rel' => 'proyectos',
'title' => 'Proyectos',
'href' => str_replace('{URL}', '/proyectos', $base_url)
]
];
return $this->withJson($response, $output);
}
public function add(Request $request, Response $response, Factory $factory): Response {
$post = $request->getParsedBody();
$output = [
'input' => $post
];
if (in_array('inmobiliarias', $post)) {
$output['inmobiliarias'] = [];
foreach ($post['inmobiliarias'] as $input) {
$inmobiliaria = Inmobiliaria::add($factory, $input);
$inmobiliarias []= [
'inmobiliaria' => $inmobiliaria->toArray(),
'created' => $inmobiliaria->is_new() ? $inmobiliaria->save() : false
];
}
} elseif (in_array('inmobiliaria', $post)) {
$inmobiliaria = Inmobiliaria::add($factory, $post);
$output['inmobiliaria'] = $inmobiliaria;
$output['created'] = $inmobiliaria->is_new() ? $inmobiliaria->save() : false;
}
return $this->withJson($response, $output);
}
public function edit(Request $request, Response $response, Factory $factory, $inmobiliaria_rut): Response {
$post = $request->getParsedBody();
$input = compact('inmobiliaria_rut', 'post');
$inmobiliaria = $this->getInmobiliaria($factory, $inmobiliaria_rut);
$output = [
'input' => $input,
'inmobiliaria' => $inmobiliaria->toArray()
];
$output['edited'] = $inmobiliaria->edit($post);
$output['changes'] = $inmobiliaria->toArray();
return $this->withJson($response, $output);
}
public function delete(Request $request, Response $response, Factory $factory, $inmobiliaria_rut): Response {
$inmobiliaria = $this->getInmobiliaria($factory, $inmobiliaria_rut);
$output = [
'input' => $inmobiliaria_rut,
'inmobiliaria' => $inmobiliaria->toArray()
];
$output['deleted'] = $inmobiliaria->delete();
return $this->withJson($response, $output);
}
public function proyectos(Request $request, Response $response, Factory $factory, $inmobiliaria_rut): Response {
$inmobiliaria = $this->getInmobiliaria($factory, $inmobiliaria_rut);
$proyectos = $inmobiliaria?->proyectos();
$base_url = str_replace("/inmobiliaria/{$inmobiliaria_rut}/proyectos", '{URL}', $request->getUri());
$output = [
'input' => $inmobiliaria_rut,
'inmobiliaria' => $inmobiliaria->toArray(),
'proyectos' => ($proyectos) ? array_map(function ($item) use ($base_url) {
$arr = $item->toArray();
$arr['link'] = [
'rel' => 'proyecto',
'title' => $item->descripcion,
'href' => str_replace('{URL}', "/proyecto/{$item->id}", $base_url)
];
return $arr;
}, $proyectos) : []
];
$output['inmobiliaria']['link'] = [
'rel' => 'inmobiliaria',
'title' => $inmobiliaria->abreviacion,
'href' => str_replace('{URL}', "/inmobiliaria/{$inmobiliaria_rut}", $base_url)
];
return $this->withJson($response, $output);
}
public function sociedad(Request $request, Response $response, Factory $factory, $inmobiliaria_rut): Response {
$inmobiliaria = $this->getInmobiliaria($factory, $inmobiliaria_rut);
$base_url = str_replace("/inmobiliaria/{$inmobiliaria_rut}/sociedad", '{URL}', $request->getUri());
$output = [
'input' => $inmobiliaria_rut,
'inmobiliaria' => $inmobiliaria->toArray(),
'sociedad' => $inmobiliaria->sociedad()->toArray()
];
$output['inmobiliaria']['link'] = [
'rel' => 'inmobiliaria',
'title' => $inmobiliaria->abreviacion,
'href' => str_replace('{URL}', "/inmobiliaria/{$inmobiliaria_rut}", $base_url)
];
$output['sociedad']['link'] = [
'rel' => 'tipo_sociedad',
'title' => $inmobiliaria->sociedad()->descripcion,
'href' => str_replace('{URL}', "/tipos/sociedad/{$inmobiliaria->sociedad}", $base_url)
];
return $this->withJson($response, $output);
}
public function banco(Request $request, Response $response, Factory $factory, $inmobiliaria_rut): Response {
$inmobiliaria = $this->getInmobiliaria($factory, $inmobiliaria_rut);
$base_url = str_replace("/inmobiliaria/{$inmobiliaria_rut}/banco", '{URL}', $request->getUri());
$output = [
'input' => $inmobiliaria_rut,
'inmobiliaria' => $inmobiliaria->toArray(),
'banco' => $inmobiliaria->banco()->toArray()
];
$output['inmobiliaria']['link'] = [
'rel' => 'inmobiliaria',
'title' => $inmobiliaria->abreviacion,
'href' => str_replace('{URL}', "/inmobiliaria/{$inmobiliaria_rut}", $base_url)
];
$output['banco']['link'] = [
'rel' => 'banco',
'title' => $inmobiliaria->banco()->nombre,
'href' => str_replace('{URL}', "/banco/{$inmobiliaria->banco}", $base_url)
];
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,91 @@
<?php
namespace Incoviba\API\Common\Controller;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Incoviba\API\Common\Define\Controller\Json;
use Incoviba\API\Common\Factory\Model as Factory;
use Incoviba\Venta\Propietario;
class Propietarios {
use Json;
public function __invoke(Request $request, Response $response, Factory $factory): Response {
$propietarios = $factory->find(Propietario::class)->array();
$base_url = str_replace('/propietarios', '{URL}', $request->getUri());
array_walk($propietarios, function (&$item) use ($base_url) {
$link = [
'rel' => 'propietario',
'title' => implode(' ', [$item['nombres'], $item['apellido_paterno'], $item['apellido_materno']]),
'href' => str_replace('{URL}', "/propietario/{$item['rut']}", $base_url)
];
$item['link'] = $link;
});
return $this->withJson($response, compact('propietarios'));
}
protected function get(Factory $factory, string $class, $rut) {
return $factory->find($class)->where([['rut', $rut]])->one();
}
public function show(Request $request, Response $response, Factory $factory, $propietario_rut): Response {
$propietario = $this->get($factory, Propietario::class, $propietario_rut);
$output = [
'input' => $propietario_rut,
'propietario' => $propietario->toArray(),
'link' => [
'rel' => 'propietarios',
'title' => 'Propietarios',
'href' => str_replace("/propietario/{$propietario_rut}", '/propietarios', $request->getUri())
]
];
$output['links'] = [
[
'rel' => 'direccion',
'title' => $propietario->direccion()->calle,
'href' => str_replace("/propietario/{$propietario_rut}", "/direccion/{$propietario->direccion}", $request->getUri())
]
];
return $this->withJson($response, $output);
}
public function add(Request $request, Response $response, Factory $factory): Response {
$post = $request->getParsedBody();
$output = [
'input' => $post
];
if (in_array('propietarios', $post)) {
$output['propietarios'] = [];
foreach ($post['propietarios'] as $input) {
$propietario = Propietario::add($factory, $input);
$propietario []= [
'propietario' => $propietario->toArray(),
'created' => $propietario->is_new() ? $propietario->save() : false
];
}
} elseif (in_array('propietario', $post)) {
$propietario = Propietario::add($factory, $post);
$output['propietario'] = $propietario;
$output['created'] = $propietario->is_new() ? $propietario->save() : false;
}
return $this->withJson($response, $output);
}
public function edit(Request $request, Response $response, Factory $factory, $propietario_rut): Response {
$post = $request->getParsedBody();
$input = compact('propietario_rut', 'post');
$propietario = $this->get($factory, Propietario::class, $propietario_rut);
$output = [
'input' => $input,
'propietario' => $propietario->toArray()
];
$output['edited'] = $propietario->edit($post);
$output['changes'] = $propietario->toArray();
return $this->withJson($response, $output);
}
public function delete(Request $request, Response $response, Factory $factory, $propietario_rut): Response {
$propietario = $this->get($factory, Propietario::class, $propietario_rut);
$output = [
'input' => $propietario_rut,
'propietario' => $propietario->toArray()
];
$output['deleted'] = $propietario->delete();
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,184 @@
<?php
namespace Incoviba\API\Common\Controller;
use Carbon\Carbon;
use Incoviba\API\Common\Alias\Controller;
use Incoviba\Mapper\EstadoVenta;
use Incoviba\Mapper\ProyectoAgente;
use Incoviba\Mapper\TipoEstadoProyecto;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Incoviba\API\Common\Factory\Model as Factory;
use Incoviba\Proyecto\Proyecto;
use Incoviba\Mapper\Proyecto as ProyectoMapper;
use Incoviba\Mapper\EstadoProyecto;
use Incoviba\Mapper\Venta as VentaMapper;
use Incoviba\Mapper\ProyectoTipoUnidad as TipoMapper;
use Incoviba\Mapper\Unidad as UnidadMapper;
use Incoviba\Mapper\Precio as PrecioMapper;
use Incoviba\Mapper\EstadoPrecio as EstadoMapper;
use Incoviba\API\Common\Service\Format;
class Proyectos extends Controller {
public function __invoke(Request $request, Response $response): Response {
$proyectos = $this->getMapper(ProyectoMapper::class)->fetchAll();
usort($proyectos, function($a, $b) {
return strcmp($a->descripcion, $b->descripcion);
});
$proyectos = json_decode(json_encode($proyectos), JSON_OBJECT_AS_ARRAY);
foreach ($proyectos as &$proyecto) {
$proyecto['ventas'] = $this->getMapper(VentaMapper::class)->fetchCountByProyecto($proyecto['id']);
}
return $this->withJson($response, compact('proyectos'));
}
public function show(Request $request, Response $response, $proyecto_id): Response {
$proyecto = $this->getMapper(ProyectoMapper::class)->fetchById($proyecto_id);
$output = [
'proyecto' => json_decode(json_encode($proyecto), JSON_OBJECT_AS_ARRAY)
];
return $this->withJson($response, $output);
}
public function add(Request $request, Response $response, Factory $factory): Response {
$post = $request->getParsedBody();
$output = [
'input' => $post
];
if (in_array('proyectos', $post)) {
$output['proyectos'] = [];
foreach ($post['proyectos'] as $input) {
$proyecto = Proyecto::add($factory, $input);
$proyecto []= [
'proyecto' => $proyecto->toArray(),
'created' => $proyecto->is_new() ? $proyecto->save() : false
];
}
} elseif (in_array('proyecto', $post)) {
$proyecto = Proyecto::add($factory, $post);
$output['proyecto'] = $proyecto;
$output['created'] = $proyecto->is_new() ? $proyecto->save() : false;
}
return $this->withJson($response, $output);
}
public function edit(Request $request, Response $response, Factory $factory, $proyecto_id): Response {
$post = $request->getParsedBody();
$input = compact('proyecto_id', 'post');
$proyecto = $factory->find(Proyecto::class)->one($proyecto_id);
$output = [
'input' => $input,
'proyecto' => $proyecto->toArray()
];
$output['edited'] = $proyecto->edit($post);
$output['changes'] = $proyecto->toArray();
return $this->withJson($response, $output);
}
public function delete(Request $request, Response $response, Factory $factory, $proyecto_id): Response {
$proyecto = $factory->find(Proyecto::class)->one($proyecto_id);
$output = [
'input' => $proyecto_id,
'proyecto' => $proyecto->toArray()
];
$output['deleted'] = $proyecto->delete();
return $this->withJson($response, $output);
}
public function ventas(Request $request, Response $response, int $proyecto_id): Response
{
$proyecto = $this->getMapper(ProyectoMapper::class)->fetchById($proyecto_id);
$ventas = $this->getMapper(VentaMapper::class)->fetchActivaByProyecto($proyecto_id);
$output = ['proyecto' => json_decode(json_encode($proyecto), JSON_OBJECT_AS_ARRAY)];
$output['proyecto']['ventas'] = json_decode(json_encode($ventas), JSON_OBJECT_AS_ARRAY);
foreach ($ventas as $i => $venta) {
$estado = $this->getMapper(EstadoVenta::class)->fetchLastByVenta($venta->id);
$output['proyecto']['ventas'][$i]['estado'] = $estado;
}
return $this->withJson($response, $output);
}
public function precios(Request $request, Response $response, Format $format, int $proyecto_id): Response {
$proyecto = $this->getMapper(ProyectoMapper::class)->fetchById($proyecto_id);
$output = json_decode(json_encode(compact('proyecto')), JSON_OBJECT_AS_ARRAY);
$tipos = $this->getMapper(TipoMapper::class)->fetchByProyecto($proyecto_id);
usort($tipos, function($a, $b) {
$t = strcmp($a->tipo->id, $b->tipo->id);
if ($t === 0) {
return strcmp($a->nombre, $b->nombre);
}
return $t;
});
$output['proyecto']['tipos'] = json_decode(json_encode($tipos), JSON_OBJECT_AS_ARRAY);
foreach ($tipos as $i => $tipo) {
$unidades = $this->getMapper(UnidadMapper::class)->fetchByTipo($tipo->id);
$output['proyecto']['tipos'][$i]['unidades'] = json_decode(json_encode($unidades), JSON_OBJECT_AS_ARRAY);
foreach ($unidades as $k => $unidad) {
if ($unidad->subtipo === null) {
$output['proyecto']['tipos'][$i]['unidades'][$k]['subtipo'] = $tipo->nombre;
}
$precio = $this->getMapper(PrecioMapper::class)->fetchLastByUnidad($unidad->id);
if (!$precio) {
$output['proyecto']['tipos'][$i]['unidades'][$k]['precio'] = ['valor' => 0, 'formateado' => 0, 'estado' => ['estado' => ['descripcion' => '']]];
continue;
}
$output['proyecto']['tipos'][$i]['unidades'][$k]['precio'] = json_decode(json_encode($precio), JSON_OBJECT_AS_ARRAY);
$output['proyecto']['tipos'][$i]['unidades'][$k]['precio']['estado'] = json_decode(json_encode($this->getMapper(EstadoMapper::class)->fetchLastByPrecio($precio->id)), JSON_OBJECT_AS_ARRAY);
$output['proyecto']['tipos'][$i]['unidades'][$k]['precio']['formateado'] = $format->uf($precio->valor);
}
}
return $this->withJson($response, $output);
}
public function estado(Request $request, Response $response, int $proyecto_id): Response
{
$proyecto = $this->getMapper(ProyectoMapper::class)->fetchById($proyecto_id);
$estado = $this->getMapper(EstadoProyecto::class)->fetchLastByProyecto($proyecto_id);
$output = [
'proyecto' => $proyecto,
'estado' => json_decode(json_encode($estado), JSON_OBJECT_AS_ARRAY)
];
$output['estado']['fecha']['diff'] = Carbon::createFromTimestamp($estado->fecha->getTimestamp())->locale('es-CL')->diffForHumans();
return $this->withJson($response, $output);
}
public function inicio(Request $request, Response $response, int $proyecto_id): Response
{
$proyecto = $this->getMapper(ProyectoMapper::class)->fetchById($proyecto_id);
$estado = $this->getMapper(EstadoProyecto::class)->fetchFirstByProyecto($proyecto_id);
$output = [
'proyecto' => $proyecto,
'estado' => json_decode(json_encode($estado), JSON_OBJECT_AS_ARRAY)
];
$output['estado']['fecha']['diff'] = Carbon::createFromTimestamp($estado->fecha->getTimestamp())->locale('es-CL')->diffForHumans();
if ($estado->fecha->getTimestamp() < 0) {
$output['estado']['fecha']['diff'] = '-';
}
return $this->withJson($response, $output);
}
public function progreso(Request $request, Response $response, int $proyecto_id): Response
{
$proyecto = $this->getMapper(ProyectoMapper::class)->fetchById($proyecto_id);
$inicio = $this->getMapper(EstadoProyecto::class)->fetchFirstByProyecto($proyecto_id);
$estado = $this->getMapper(EstadoProyecto::class)->fetchLastByProyecto($proyecto_id);
$orden_tipo_estados = array_map(function($item) {
return $item->orden;
}, $this->getMapper(TipoEstadoProyecto::class)->fetchAll());
$estados = $this->getMapper(EstadoProyecto::class)->fetchByProyecto($proyecto_id);
$total = 1;
if ($estado->tipo->orden < max($orden_tipo_estados) - 1 and count($estados) > 1) {
$tf = Carbon::createFromTimestamp($estado->fecha->getTimestamp());
$t0 = Carbon::createFromTimestamp($inicio->fecha->getTimestamp());
$df = $tf->diffInSeconds($t0);
$hoy = Carbon::now();
$dh = $hoy->diffInSeconds($t0);
$total = $df / $dh;
}
$valor = round(($estado->tipo->orden + 1) / max($orden_tipo_estados) * 100 * $total);
$output = [
'proyecto' => $proyecto,
'progreso' => $valor
];
return $this->withJson($response, $output);
}
public function operadores(Request $request, Response $response, int $proyecto_id): Response
{
$proyecto = $this->getMapper(ProyectoMapper::class)->fetchById($proyecto_id);
$operadores = $this->getMapper(ProyectoAgente::class)->fetchOperadoresVigenteByProyecto($proyecto_id);
$output = compact('proyecto', 'operadores');
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace Incoviba\API\Common\Controller\Proyectos;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Incoviba\API\Common\Define\Controller\Json;
use Incoviba\Mapper\Proyecto as ProyectoMapper;
use Incoviba\Mapper\Cierre as CierreMapper;
class Cierres {
use Json;
public function __invoke(Request $request, Response $response, ProyectoMapper $proyectoMapper, CierreMapper $cierreMapper): Response {
$proyectos = $proyectoMapper->fetchAll();
$cierres = [];
function filter_cierres($cierres, $estado) {
return array_filter($cierres, function ($item) use ($estado) {
return $item->estado()->tipo->descripcion === $estado;
});
}
foreach ($proyectos as $proyecto) {
$cs = $cierreMapper->fetchByProyecto($proyecto->id);
if (count($cs) == 0) {
continue;
}
$pendientes = filter_cierres($cs, 'pendiente');
$cierres[$proyecto->descripcion] = [
'proyecto' => $proyecto->descripcion,
'total' => count($cs),
'promesados' => count(filter_cierres($cs, 'promesado')),
'rechazados' => count(filter_cierres($cs, 'rechazado')),
'pendientes' => count($pendientes),
'ultimo_pendiente' => (count($pendientes) > 0) ? $pendientes[0]->periodo() : 0
];
}
return $this->withJson($response, compact('cierres'));
}
}

View File

@ -0,0 +1,57 @@
<?php
namespace Incoviba\API\Common\Controller\Proyectos;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Carbon\Carbon;
use Incoviba\Proyecto\Proyecto;
use Incoviba\API\Common\Define\Controller\Json;
use Incoviba\API\Common\Factory\Model as Factory;
use Incoviba\Mapper\Proyecto as ProyectoMapper;
use Incoviba\Mapper\Cuota as CuotaMapper;
class Cuotas {
use Json;
public function mes(Request $request, Response $response, ProyectoMapper $proyectoMapper, CuotaMapper $cuotaMapper): Response {
$proyectos = $proyectoMapper->fetchAll();
$dias = [];
foreach ($proyectos as $proyecto) {
$cuotas = $cuotaMapper->fetchByProyectoAndMes($proyecto->id, new \DateTimeImmutable());
foreach ($cuotas as $cuota) {
$f = Carbon::createFromTimestamp($cuota->pago->fecha->getTimestamp());
if ($f->isoWeekday() == 6 or $f->isoWeekDay() == 7) {
$f = $f->copy()->addDays(2)->startOfWeek();
}
$dia = $f->format('Y-m-d');
if (!isset($dias[$dia])) {
$dias[$dia] = ['dia' => $dia, 'proyectos' => [$proyecto->descripcion => ['proyecto' => $proyecto->descripcion, 'cantidad' => 0]]];
}
if (!isset($dias[$dia]['proyectos'][$proyecto->descripcion])) {
$dias[$dia]['proyectos'][$proyecto->descripcion] = ['proyecto' => $proyecto->descripcion, 'cantidad' => 0];
}
$dias[$dia]['proyectos'][$proyecto->descripcion]['cantidad'] ++;
}
}
uksort($dias, function($a, $b) {
return strcmp($a, $b);
});
return $this->withJson($response, ['dias' => $dias]);
}
public function hoy(Request $request, Response $response, ProyectoMapper $proyectoMapper, CuotaMapper $cuotaMapper): Response {
$proyectos = $proyectoMapper->fetchAll();
$hoy = 0;
foreach ($proyectos as $proyecto) {
$hoy += count($cuotaMapper->fetchByProyectoAndFecha($proyecto->id, new \DateTimeImmutable()));
}
return $this->withJson($response, ['hoy' => $hoy]);
}
public function pendientes(Request $request, Response $response, ProyectoMapper $proyectoMapper, CuotaMapper $cuotaMapper): Response {
$proyectos = $proyectoMapper->fetchAll();
$pendientes = 0;
foreach ($proyectos as $proyecto) {
$pendientes += count($cuotaMapper->fetchByProyectoAndPendiente($proyecto->id));
}
return $this->withJson($response, ['pendientes' => $pendientes]);
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace Incoviba\API\Common\Controller;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Incoviba\API\Common\Define\Controller\Json;
use Incoviba\API\Common\Factory\Model as Factory;
use Incoviba\Inmobiliaria\TipoSociedad;
class Sociedades {
use Json;
public function __invoke(Request $request, Response $response, Factory $factory): Response {
$sociedades = $factory->find(TipoSociedad::class)->array();
$base_url = str_replace('/sociedades', '{URL}', $request->getUri());
array_walk($sociedades, function (&$item) use ($base_url) {
$link = [
'rel' => 'sociedad',
'title' => $item['descripcion'],
'href' => str_replace('{URL}', "/sociedad/{$item['id']}", $base_url)
];
$item['link'] = $link;
});
return $this->withJson($response, compact('sociedades'));
}
public function show(Request $request, Response $response, Factory $factory, $sociedad_id): Response {
$sociedad = $factory->find(TipoSociedad::class)->one($sociedad_id);
$output = [
'input' => $sociedad_id,
'sociedad' => $sociedad->toArray(),
'link' => [
'rel' => 'sociedades',
'title' => 'Sociedades',
'href' => str_replace("/sociedad/{$sociedad_id}", '/sociedades', $request->getUri())
]
];
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,158 @@
<?php
namespace Incoviba\API\Common\Controller;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Incoviba\API\Common\Define\Controller\Json;
use Incoviba\API\Common\Factory\Model as Factory;
use Incoviba\Venta\Venta;
use Incoviba\Mapper\Venta as VentaMapper;
class Ventas {
use Json;
public function __invoke(Request $request, Response $response, Factory $factory, VentaMapper $mapper): Response {
$ventas = $mapper->fetchAll();
error_log(var_export($ventas, true));
$ventas = $factory->find(Venta::class)->array();
$url = '' . $request->getUri();
array_walk($ventas, function (&$item) use ($url) {
$item['link'] = [
'rel' => 'venta',
'title' => 'Venta',
'href' => str_replace('/ventas', "/venta/{$item['id']}", $url)
];
});
return $this->withJson($response, compact('ventas'));
}
public function show(Request $request, Response $response, Factory $factory, $venta_id): Response {
$venta = $factory->find(Venta::class)->one($venta_id);
$output = [
'input' => $venta_id,
'venta' => $venta->toArray(),
'link' => [
'rel' => 'ventas',
'title' => 'Ventas',
'href' => str_replace("/venta/{$venta_id}", '/ventas', $request->getUri())
]
];
$output['links'] = [
[
'rel' => 'propietario',
'title' => $venta->propietario()->nombreCompleto(),
'href' => str_replace("/venta/{$venta_id}", "/propietario/{$venta->propietario}", $request->getUri())
],
[
'rel' => 'propiedad',
'title' => 'Propiedad',
'href' => str_replace("/venta/{$venta_id}", "/propiedad/{$venta->propiedad}", $request->getUri())
]
];
if ($venta->pie()) {
$output['links'] []= [
'rel' => 'pie',
'title' => 'Pie',
'href' => str_replace("/venta/{$venta_id}", "/pie/{$venta->pie}", $request->getUri())
];
}
if ($venta->bono()) {
$output['links'] []= [
'rel' => 'bono_pie',
'title' => 'Bono Pie',
'href' => str_replace("/venta/{$venta_id}", "/bono/{$venta->bono_pie}", $request->getUri())
];
}
if ($venta->credito()) {
$output['links'] []= [
'rel' => 'credito',
'title' => 'Credito',
'href' => str_replace("/venta/{$venta_id}", "/credito/{$venta->credito}", $request->getUri())
];
}
if ($venta->escritura()) {
$output['links'] []= [
'rel' => 'escritura',
'title' => 'Escritura',
'href' => str_replace("/venta/{$venta_id}", "/escritura/{$venta->escritura}", $request->getUri())
];
}
if ($venta->subsidio()) {
$output['links'] []= [
'rel' => 'subsidio',
'title' => 'Subsidio',
'href' => str_replace("/venta/{$venta_id}", "/subsidio/{$venta->subsidio}", $request->getUri())
];
}
if ($venta->entrega()) {
$output['links'] []= [
'rel' => 'entrega',
'title' => 'Entrega',
'href' => str_replace("/venta/{$venta_id}", "/entrega/{$venta->entrega}", $request->getUri())
];
}
if ($venta->proyectoAgente()) {
$output['links'] []= [
'rel' => 'proyecto_agente',
'title' => 'Proyecto Agente',
'href' => str_replace("/venta/{$venta_id}", "/proyecto_agente/{$venta->agente}", $request->getUri())
];
}
if ($venta->promocion()) {
$output['links'] []= [
'rel' => 'promocion',
'title' => 'Promocion',
'href' => str_replace("/venta/{$venta_id}", "/promocion/{$venta->promocion}", $request->getUri())
];
}
if ($venta->resciliacion()) {
$output['links'] []= [
'rel' => 'resciliacion',
'title' => 'Resciliacion',
'href' => str_replace("/venta/{$venta_id}", "/resciliacion/{$venta->resciliacion}", $request->getUri())
];
}
return $this->withJson($response, $output);
}
public function add(Request $request, Response $response, Factory $factory): Response {
$post = $request->getParsedBody();
$output = [
'input' => $post
];
if (in_array('ventas', $post)) {
$output['ventas'] = [];
foreach ($post['ventas'] as $input) {
$venta = Venta::add($factory, $input);
$venta []= [
'venta' => $venta->toArray(),
'created' => $venta->is_new() ? $venta->save() : false
];
}
} elseif (in_array('venta', $post)) {
$venta = Venta::add($factory, $post);
$output['venta'] = $venta;
$output['created'] = $venta->is_new() ? $venta->save() : false;
}
return $this->withJson($response, $output);
}
public function edit(Request $request, Response $response, Factory $factory, $venta_id): Response {
$post = $request->getParsedBody();
$input = compact('venta_id', 'post');
$venta = $factory->find(Venta::class)->one($venta_id);
$output = [
'input' => $input,
'venta' => $venta->toArray()
];
$output['edited'] = $venta->edit($post);
$output['changes'] = $venta->toArray();
return $this->withJson($response, $output);
}
public function delete(Request $request, Response $response, Factory $factory, $venta_id): Response {
$venta = $factory->find(Venta::class)->one($venta_id);
$output = [
'input' => $venta_id,
'venta' => $venta->toArray()
];
$output['deleted'] = $venta->delete();
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace Incoviba\API\Common\Define\Controller;
use Psr\Http\Message\ResponseInterface as Response;
trait Json {
public function withJson(Response $response, $data, $status = 200): Response {
$response->getBody()->write(json_encode($data, JSON_UNESCAPED_SLASHES));
return $response
->withStatus($status)
->withHeader('content-type', 'application/json');
}
}

62
common/Define/Model.php Normal file
View File

@ -0,0 +1,62 @@
<?php
namespace Incoviba\API\Common\Define;
use Incoviba\API\Common\Factory\Model as ModelFactory;
interface Model {
/**
* @return string
*/
public function getTable(): string;
/**
* @param ModelFactory $factory
* @return Model
*/
public function setFactory(ModelFactory $factory): Model;
/**
* Get all children models of class {$child_class}
* @param string $child_class Class of child model
* @param array $definitions array of conditions for connecting to the child table
* @return array|null
*/
public function parentOf(string $child_class, array $definitions): bool|array;
/**
* Get parent model of class {$parent_class}
* @param string $parent_class Class of parent model
* @param array $definitions array of conditions for connecting to the parent table
* @return Model|null
*/
public function childOf(string $parent_class, array $definitions): bool|Model;
/**
* Get all siblings of class {$sibling_class}
* @param string $sibling_class Class of sibling model
* @param string $connecting_table table connecting both models
* @param array $definitions array of conditions for connecting to the sibling and connecting tables
* @return array|null
*/
public function siblingOf(string $sibling_class, string $connecting_table, array $definitions): ?array;
/**
* @return array
*/
public function toArray(): array;
/**
* Add a new model of this type to the database, parsing {$input} data
* @param ModelFactory $factory Factory object for connecting to the database
* @param array $input Input data
* @return Model|null
*/
public static function add(ModelFactory $factory, array $input): bool|Model;
/**
* Edit current model parsing {$input} data
* @param array $input Input data
* @return array|null
*/
public function edit(array $input): ?array;
}

22
common/Factory/Mapper.php Normal file
View File

@ -0,0 +1,22 @@
<?php
namespace Incoviba\API\Common\Factory;
use Incoviba\Mapper\Mapper as BaseMapper;
use Psr\Container\ContainerInterface;
class Mapper
{
protected ContainerInterface $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function get(string $name): BaseMapper
{
if (!class_exists($name)) {
throw new \InvalidArgumentException();
}
return $this->container->get($name);
}
}

404
common/Factory/Model.php Normal file
View File

@ -0,0 +1,404 @@
<?php
namespace Incoviba\API\Common\Factory;
use InvalidArgumentException;
use ORM;
use Incoviba\API\Common\Alias\Model as BaseModel;
use Incoviba\API\Common\Define\Model as ModelInterface;
class Model {
public static function for(string $class): Model {
$factory = new Model();
return $factory->find($class);
}
protected function reset(): Model {
$resets = ['columns', 'joins', 'conditions', 'groups', 'orders', 'having', 'limit', 'offset'];
foreach ($resets as $r) {
$this->{$r} = null;
}
return $this;
}
public function create(string $model_class, array $data = null): bool|ModelInterface {
// Check if it already exists
if ($data !== null) {
$model = $this->find($model_class);
foreach ($data as $f => $v) {
$model = $model->where([[$f, $v]]);
}
$model = $model->one();
if ($model !== false and $model !== null) {
return $model;
}
}
$model = BaseModel::factory($model_class)->create($data);
if (is_a($model, \ORMWrapper::class)) {
$model = $model->find_one();
}
$model->setFactory($this);
return $model;
}
protected string $class;
public function find(string $class): Model {
$this->reset();
if (!class_exists($class)) {
throw new InvalidArgumentException("Model {$class} not found.");
}
$this->class = $class;
return $this;
}
protected ?array $columns;
public function select($columns): Model {
if (!is_array($columns)) {
$columns = [$columns];
}
foreach ($columns as $c) {
$this->addColumn($c);
}
return $this;
}
protected function addColumn($column) {
$col = [
'table' => '',
'column' => $column
];
if (is_array($column)) {
$col['table'] = $column['table'] ?? $column[0];
$col['column'] = $column['column'] ?? $column[1];
}
$this->columns []= $col;
}
protected function parseColumns(ORM $orm): ORM {
if ($this->columns === null) {
return $orm;
}
foreach ($this->columns as $column) {
$orm = $orm->select("{$column['table']}.{$column['column']}");
}
return $orm;
}
protected ?array $joins;
public function join($joins): Model {
foreach ($joins as $join) {
$this->addJoin($join);
}
return $this;
}
protected function addJoin($join) {
$map = [
'table' => ['table', 't', 0],
'from' => ['from', 'f', 1],
'to' => ['to', 2],
'operator' => ['operator', 'op', 'o', 3],
'type' => 'type',
'alias' => 'alias',
'params' => ['parameters', 'params']
];
$defaults = ['operator' => '=', 'type' => 'inner'];
$required = ['table', 'from', 'to', 'operator', 'type'];
$j = [];
foreach ($join as $key => $val) {
$k = -1;
foreach ($map as $i => $m) {
if (is_array($m)) {
if (in_array($key, $m)) {
$k = $i;
break;
}
continue;
}
if ($m == $key) {
$k = $i;
}
}
$j[$k] = $val;
}
foreach ($defaults as $key => $val) {
if (!isset($j[$key])) {
$j[$key] = $val;
}
}
foreach ($required as $key) {
if (!isset($j[$key])) {
throw new InvalidArgumentException("Missing {$key} when joining in " . get_class());
}
}
$this->joins []= (object) $j;
}
protected function parseJoins(ORM $orm): ORM {
if ($this->joins === null) {
return $orm;
}
foreach ($this->joins as $join) {
$method = match(strtolower($join->type)) {
'raw' => 'rawJoin',
'inner' => 'innerJoin',
'left', 'left outer', 'left_outer', 'leftouter' => 'leftOuterJoin',
'right', 'right outer', 'right_outer', 'rightouter' => 'rightOuterJoin',
'full', 'full outer', 'full_outer', 'fullouter', 'outer' => 'fullOuterJoin'
};
if (strtolower($join->type) == 'raw') {
if (isset($join->params)) {
if (isset($join->alias)) {
$orm = $orm->{$method}($join->table, [$join->from, $join->operator, $join->to], $join->alias, $join->params);
} else {
$orm = $orm->{$method}($join->table, [$join->from, $join->operator, $join->to], $join->params);
}
} else {
if (isset($join->alias)) {
$orm = $orm->{$method}($join->table, [$join->from, $join->operator, $join->to], $join->alias);
} else {
$orm = $orm->{$method}($join->table, [$join->from, $join->operator, $join->to]);
}
}
} elseif (isset($join->alias)) {
$orm = $orm->{$method}($join->table, [$join->from, $join->operator, $join->to], $join->alias);
} else {
$orm = $orm->{$method}($join->table, [$join->from, $join->operator, $join->to]);
}
}
return $orm;
}
protected ?array $conditions;
public function where(array $conditions): Model {
foreach ($conditions as $condition) {
$this->addCondition($condition);
}
return $this;
}
protected function addCondition(array $condition) {
$map = [
'column' => ['column', 'field', 'col', 'c', 'f', 0],
'value' => ['value', 'val', 'v', 1],
'operator' => ['operator', 'op', 'o', 'type', 't', 2]
];
$defaults = ['operator' => '=', 'type' => 'eq'];
$required = ['column', 'value', 'operator'];
$c = [];
foreach ($condition as $key => $val) {
$k = -1;
foreach ($map as $i => $m) {
if (is_array($m)) {
if (in_array($key, $m)) {
$k = $i;
break;
}
continue;
}
if ($key == $m) {
$k = $i;
}
}
$c[$k] = $val;
}
foreach ($defaults as $key => $val) {
if (!isset($c[$key])) {
$c[$key] = $val;
}
}
foreach ($required as $key) {
if (!isset($c[$key])) {
throw new InvalidArgumentException("Missing {$key} when adding conditions in " . get_class());
}
}
$this->conditions []= (object) $c;
}
protected function parseConditions(ORM $orm): ORM {
if ($this->conditions === null) {
return $orm;
}
foreach ($this->conditions as $condition) {
$method = match(strtolower($condition->operator)) {
'', '=', 'eq', 'equal', 'equals' => 'where',
'<', 'lt', 'less than', 'less_than', 'lessthan' => 'whereLt',
'<=', 'lte', 'less equal', 'less equals', 'less_equal', 'less_equals', 'lessequal', 'lessequals',
'less than equal', 'less than equals', 'less_than_equal', 'less_than_equals', 'lessthanequal',
'lessthanequals' => 'whereLte',
'>', 'gt', 'greater than', 'greater_than', 'greaterthan' => 'whereGt',
'>=', 'gte', 'greater equal', 'greater equals', 'greater_equal', 'greater_equals', 'greaterequal',
'greaterequals', 'greater than equal', 'greater than equals', 'greater_than_equal', 'greater_than_equals',
'greaterthanequal', 'greaterthanequals' => 'whereGte',
'!=', 'not equal', 'not equals', 'not_equal', 'not_equals', 'notequal', 'notequals' => 'whereNotEqual',
'like' => 'whereLike',
'not like', 'not_like', 'notlike' => 'whereNotLike',
'in' => 'whereIn',
'not in', 'not_in', 'notin' => 'whereNotIn',
'raw' => 'raw'
};
$orm = $orm->{$method}($condition->column, $condition->value);
}
return $orm;
}
protected ?array $groups;
public function group($groups): Model {
if (!is_array($groups)) {
$groups = [$groups];
}
foreach ($groups as $group) {
$this->addGroup($group);
}
return $this;
}
protected function addGroup(string $group) {
$this->groups []= $group;
}
protected function parseGroups(ORM $orm): ORM {
if ($this->groups === null) {
return $orm;
}
foreach ($this->groups as $group) {
if (str_contains($group, '(')) {
$orm = $orm->groupByExpr($group);
continue;
}
$orm = $orm->groupBy($group);
}
return $orm;
}
protected ?array $orders;
public function order($orders): Model {
if (!is_array($orders)) {
$orders = [$orders];
}
foreach ($orders as $order) {
$this->addOrder($order);
}
return $this;
}
protected function addOrder($order) {
$map = [
'column' => ['column', 'col', 'c', 0],
'direction' => ['direction', 'dir', 'd', 1]
];
if (!is_array($order)) {
$order = [$order];
}
$defaults = ['direction' => 'asc'];
$required = ['column', 'direction'];
$o = [];
foreach ($order as $key => $val) {
/*$k = match (strtolower($key)) {
'column', 'col', 'c', 0 => 'column',
'direction', 'dir', 'd', 1 => 'direction'
};*/
$k = -1;
foreach ($map as $i => $m) {
if (is_array($m)) {
if (in_array($key, $m)) {
$k = $i;
break;
}
continue;
}
if ($key == $m) {
$k = $i;
}
}
$o[$k] = $val;
}
foreach ($defaults as $key => $val) {
if (!isset($o[$key])) {
$o[$key] = $val;
}
}
foreach ($required as $key) {
if (!isset($o[$key])) {
throw new InvalidArgumentException("Missing {$key} in order.");
}
}
$this->orders []= (object) $o;
}
protected function parseOrders(ORM $orm): ORM {
if ($this->orders === null) {
return $orm;
}
foreach ($this->orders as $order) {
if (str_contains($order->column, '(')) {
$orm = $orm->orderByExpr($order->column);
continue;
}
$method = match (strtolower($order->direction)) {
'ascending', 'asc', '' => 'orderByAsc',
'descending', 'desc' => 'orderByDesc'
};
$orm = $orm->{$method}($order->column);
}
return $orm;
}
protected ?int $limit;
public function limit(int $limit): Model {
$this->limit = $limit;
return $this;
}
protected function parseLimit(ORM $orm): ORM {
if ($this->limit === null) {
return $orm;
}
return $orm->limit($this->limit);
}
protected ?int $offset;
public function offset(int $offset): Model {
$this->offset = $offset;
return $this;
}
protected function parseOffset(ORM $orm): ORM {
if ($this->offset === null) {
return $orm;
}
return $orm->offset($this->offset);
}
public function build(): ORM {
$orm = BaseModel::factory($this->class);
$methods = [
'columns',
'joins',
'conditions',
'groups',
'orders',
'limit',
'offset'
];
foreach ($methods as $m) {
$method = 'parse' . ucfirst($m);
$orm = $this->{$method}($orm);
}
return $orm;
}
public function one(?int $id = null): bool|ModelInterface {
$result = $this->build()->find_one($id);
if ($result !== false) {
$result->setFactory($this);
}
return $result;
}
public function many(): bool|array {
$results = $this->build()->find_many();
if ($results !== false) {
foreach ($results as &$result) {
$result->setFactory($this);
}
}
return $results;
}
public function array(): ?array {
$results = $this->many();
if ($results === false) {
return null;
}
return array_map(function($item) {
return $item->toArray();
}, $results);
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace Incoviba\API\Common\Middleware;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as Handler;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ResponseFactoryInterface as Factory;
use Incoviba\API\Common\Service\Auth as Service;
class Auth {
protected $service;
protected $factory;
protected $exceptions;
public function __construct(Service $service, Factory $factory, array $exception_routes) {
$this->service = $service;
$this->factory = $factory;
$this->exceptions = $exception_routes;
}
public function __invoke(Request $request, Handler $handler): Response {
$path = $request->getUri()->getPath();
if (in_array($path, $this->exceptions) or $this->service->isValid($request)) {
return $handler->handle($request);
}
$response = $this->factory->createResponse();
$response->getBody()->write(json_encode(['message' => 'Not authorized.']));
return $response
->withStatus(401) // unauthorized
->withHeader('content-type', 'application/json');
}
}

60
common/Service/Auth.php Normal file
View File

@ -0,0 +1,60 @@
<?php
namespace Incoviba\API\Common\Service;
use Psr\Http\Message\ServerRequestInterface as Request;
class Auth {
protected string $key;
public function __construct(string $key) {
$this->key = $key;
}
public function isValid(Request $request): bool {
$api_key = $this->getRequestKey($request);
if ($this->key == $api_key) {
return true;
}
return false;
}
protected function getRequestKey(Request $request) {
if ($request->hasHeader('Authorization')) {
return $this->getKeyFromHeader($request);
} elseif ($request->getParsedBody() !== null and in_array('API_KEY', $request->getParsedBody())) {
return $request->getParsedBody()['API_KEY'];
} elseif ($request->getQueryParams() !== null and in_array('API_KEY', array_keys($request->getQueryParams()))) {
return $request->getQueryParams()['API_KEY'];
}
return '';
}
protected function getKeyFromHeader(Request $request) {
$api_key = $request->getHeader('Authorization');
if (is_array($api_key)) {
$api_key = $api_key[0];
}
if (str_contains($api_key, 'Bearer')) {
$api_key = explode(' ', $api_key)[1];
}
return $api_key;
}
public function generate(int $length = 32, bool $removeSimilarCharacters = true): string {
$token = "";
try {
$bytesWithMargin = random_bytes($length*3);
$base64 = base64_encode($bytesWithMargin);
$purified = preg_replace("/[+=\/.]/", "", $base64);
if ($removeSimilarCharacters) {
$purified = preg_replace("/[I1l0Oo]/", "", $purified);
}
$token = substr($purified, 0, $length);
} catch (\Exception $e){
error_log(var_export($e, true));
}
return $token;
}
public function generateToken(): object {
$selector = bin2hex(\random_bytes(12));
$token = bin2hex(\random_bytes(20));
$full = "{$selector}:{$token}";
$token = password_hash($token, \PASSWORD_DEFAULT);
return (object) compact('selector', 'token', 'full');
}
}

14
common/Service/Format.php Normal file
View File

@ -0,0 +1,14 @@
<?php
namespace Incoviba\API\Common\Service;
class Format
{
public function uf(float $number)
{
return number_format($number, 2, ',', '.');
}
public function pesos(float $number)
{
return number_format($number, 0, ',', '.');
}
}

90
common/Service/Login.php Normal file
View File

@ -0,0 +1,90 @@
<?php
namespace Incoviba\API\Common\Service;
use Carbon\Carbon;
use Incoviba\Mapper\User as UserMapper;
use Incoviba\Mapper\Login as LoginMapper;
use Incoviba\Mapper\Config as ConfigMapper;
use Incoviba\Model\Auth\User;
class Login
{
protected UserMapper $userMapper;
protected LoginMapper $loginMapper;
protected ConfigMapper $configMapper;
public function __construct(UserMapper $userMapper, LoginMapper $loginMapper, ConfigMapper $configMapper)
{
$this->userMapper = $userMapper;
$this->loginMapper = $loginMapper;
$this->configMapper = $configMapper;
}
public function setToken(User $user, string $selector, string $token)
{
$this->logout($user);
$expiration = $this->configMapper->fetchByName('cookie_expiration_time');
$data = [
'user_id' => $user->id,
'time' => (new \DateTimeImmutable())->format('Y-m-d H:i:s '),
'selector' => $selector,
'token' => $token,
'status' => 1
];
$status = false;
try {
$login = $this->loginMapper->create($data);
$status = $this->loginMapper->save($login);
} catch (\PDOException $e) {
$login = false;
}
$output = [
'input' => $data,
'login' => $login,
'logged_in' => $status
];
if ($login !== false) {
$output['expires'] = $login->time->modify("+{$expiration->value} second")->getTimestamp();
}
return $output;
}
public function logout(User $user): bool
{
$logins = $this->validLogins($user);
if ($logins === false or count($logins) === 0) {
return true;
}
$bool = true;
foreach ($logins as $login) {
$login->status = false;
$bool &= $this->loginMapper->save($login);
}
return $bool;
}
public function validLogins(User $user): bool|array {
return $this->loginMapper->fetchActiveByUser($user->id);
}
public function validate($request): bool
{
list($selector, $token) = explode(':', $request->token);
$login = $this->loginMapper->fetchBySelector($selector);
if (!$login or !$login->status or !password_verify($token, $login->token)) {
return false;
}
$expiration = $this->configMapper->fetchByName('cookie_expiration_time');
if ((Carbon::createFromTimestamp($login->time->getTimestamp()))->diffInSeconds() > $expiration->value) {
$login->status = false;
$this->loginMapper->save($login);
return false;
}
return true;
}
public function getUser($request): User|bool
{
list($selector, $token) = explode(':', $request->token);
$login = $this->loginMapper->fetchBySelector($selector);
if (!$login or !$login->status) {
return false;
}
return $login->user;
}
}

28
composer.json Normal file
View File

@ -0,0 +1,28 @@
{
"name": "incoviba/api",
"type": "project",
"require": {
"slim/slim": "^4.9",
"php-di/slim-bridge": "^3.2",
"zeuxisoo/slim-whoops": "^0.7.3",
"slim/psr7": "^1.5",
"nesbot/carbon": "^2.54",
"doctrine/dbal": "^3.3"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"kint-php/kint": "^3.3"
},
"autoload": {
"psr-4": {
"Incoviba\\": "src/",
"Incoviba\\API\\Common\\": "common/"
}
},
"authors": [
{
"name": "Aldarien",
"email": "aldarien85@gmail.com"
}
]
}

66
docker-compose.yml Normal file
View File

@ -0,0 +1,66 @@
version: '3'
x-restart:
&restart
restart: unless-stopped
x-timezone:
&tz
TZ: America/Santiago
services:
proxy:
image: nginx:alpine
container_name: incoviba_proxy
<<: *restart
environment: *tz
ports:
- "8081:80"
volumes:
- ./:/code
- ./nginx.conf:/etc/nginx/conf.d/default.conf
networks:
- incoviba
api:
build:
dockerfile: Dockerfile
container_name: incoviba_api
<<: *restart
environment: *tz
env_file:
- .env
- .db.env
- .api.key
volumes:
- ./:/code
- ./php.ini:/usr/local/etc/php/conf.d/docker.ini
- ./logs/php/:/var/log/php/
networks:
- incoviba
db:
container_name: db
image: mariadb:latest
<<: *restart
environment: *tz
env_file: .db.env
volumes:
- incoviba_data:/var/lib/mysql
networks:
- incoviba
adminer:
container_name: adminer
image: adminer:latest
<<: *restart
environment: *tz
ports:
- "8083:8080"
env_file: .adminer.env
networks:
- incoviba
volumes:
incoviba_data: {}
networks:
incoviba:
external: true

36
nginx.conf Normal file
View File

@ -0,0 +1,36 @@
server {
listen 81;
server_name api;
index index.php;
error_log /var/log/nginx/api.error.log;
access_log /var/log/nginx/api.access.log;
root /app/api/public;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';
add_header 'Content-Type' 'application/json';
add_header 'Content-Length' 0;
return 204;
}
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
fastcgi_pass api:9000;
}
}

1945
php.ini Normal file

File diff suppressed because it is too large Load Diff

BIN
php.prod.ini Normal file

Binary file not shown.

12
public/index.php Normal file
View File

@ -0,0 +1,12 @@
<?php
$app = include_once implode(DIRECTORY_SEPARATOR, [
dirname(__FILE__, 2),
'setup',
'app.php'
]);
try {
$app->run();
} catch (Error | Exception $e) {
error_log($e);
echo json_encode(['message' => 'There was an error', 'error' => $e->getMessage(), 'errorCode' => $e->getCode()]);
}

View File

@ -0,0 +1,8 @@
<?php
use Incoviba\API\Common\Controller\Auth;
$app->post('/auth/login[/]', [Auth::class, 'login']);
$app->get('/auth/generate[/]', [Auth::class, 'generate']);
$app->post('/auth/validate[/]', [Auth::class, 'validate']);
$app->post('/auth/user[/]', [Auth::class, 'user']);
$app->post('/auth/logout[/]', [Auth::class, 'logout']);

View File

@ -0,0 +1,12 @@
<?php
use Incoviba\API\Common\Controller\Bancos;
$app->group('/bancos', function ($app) {
$app->post('/add[/]', [Bancos::class, 'add']);
$app->get('[/]', Bancos::class);
});
$app->group('/banco/{banco_id}', function ($app) {
$app->put('/edit[/]', [Bancos::class, 'edit']);
$app->delete('/delete[/]', [Bancos::class, 'delete']);
$app->get('[/]', [Bancos::class, 'show']);
});

View File

@ -0,0 +1,5 @@
<?php
use Incoviba\API\Common\Controller\Base;
$app->get('[/]', Base::class);
$app->get('/php/info[/]', [Base::class, 'info']);

View File

@ -0,0 +1,5 @@
<?php
use Incoviba\API\Common\Controller\Configs;
$app->get('/config/{config_name}', [Configs::class, 'get']);
$app->post('/config', [Configs::class, 'set']);

View File

@ -0,0 +1,13 @@
<?php
use Incoviba\API\Common\Controller\Inmobiliarias;
$app->group('/inmobiliarias', function ($app) {
$app->post('/add[/]', [Inmobiliarias::class, 'add']);
$app->get('[/]', Inmobiliarias::class);
});
$app->group('/inmobiliaria/{inmobiliaria_rut}', function ($app) {
$app->get('/proyectos[/]', [Inmobiliarias::class, 'proyectos']);
$app->put('/edit[/]', [Inmobiliarias::class, 'edit']);
$app->delete('/delete[/]', [Inmobiliarias::class, 'delete']);
$app->get('[/]', [Inmobiliarias::class, 'show']);
});

View File

@ -0,0 +1,12 @@
<?php
use Incoviba\API\Common\Controller\Propietarios;
$app->group('/propietarios', function ($app) {
$app->post('/add[/]', [Propietarios::class, 'add']);
$app->get('[/]', Propietarios::class);
});
$app->group('/propietario/{propietario_rut}', function ($app) {
$app->put('/edit[/]', [Propietarios::class, 'edit']);
$app->delete('/edit[/]', [Propietarios::class, 'delete']);
$app->get('[/]', [Propietarios::class, 'show']);
});

View File

@ -0,0 +1,28 @@
<?php
use Incoviba\API\Common\Controller\Proyectos;
use Incoviba\API\Common\Controller\Proyectos\Cuotas;
use Incoviba\API\Common\Controller\Proyectos\Cierres;
$app->group('/proyectos', function ($app) {
$app->post('/add[/]', [Proyectos::class, 'add']);
$app->group('/cuotas', function($app) {
$app->get('/hoy[/]', [Cuotas::class, 'hoy']);
$app->get('/mes[/]', [Cuotas::class, 'mes']);
$app->get('/pendientes[/]', [Cuotas::class, 'pendientes']);
});
$app->group('/cierres', function($app) {
$app->get('[/]', Cierres::class);
});
$app->get('[/]', Proyectos::class);
});
$app->group('/proyecto/{proyecto_id}', function ($app) {
$app->get('/ventas', [Proyectos::class, 'ventas']);
$app->get('/precios', [Proyectos::class, 'precios']);
$app->get('/inicio', [Proyectos::class, 'inicio']);
$app->get('/estado', [Proyectos::class, 'estado']);
$app->get('/progreso', [Proyectos::class, 'progreso']);
$app->get('/operadores', [Proyectos::class, 'operadores']);
$app->put('/edit[/]', [Proyectos::class, 'edit']);
$app->delete('/delete[/]', [Proyectos::class, 'delete']);
$app->get('[/]', [Proyectos::class, 'show']);
});

View File

@ -0,0 +1,16 @@
<?php
$folder = implode(DIRECTORY_SEPARATOR, [
__DIR__,
'tipos'
]);
if (file_exists($folder)) {
$app->group('/tipos', function ($app) use ($folder) {
$files = new DirectoryIterator($folder);
foreach ($files as $file) {
if ($file->isDir() or $file->getExtension() !== 'php') {
continue;
}
include_once $file->getRealPath();
}
});
}

View File

@ -0,0 +1,12 @@
<?php
use Incoviba\API\Common\Controller\Sociedades;
$app->group('/sociedades', function ($app) {
$app->post('/add[/]', [Sociedades::class, 'add']);
$app->get('[/]', Sociedades::class);
});
$app->group('/sociedad/{sociedad_id}', function ($app) {
$app->put('/edit[/]', [Sociedades::class, 'edit']);
$app->delete('/edit[/]', [Sociedades::class, 'delete']);
$app->get('[/]', [Sociedades::class, 'show']);
});

View File

@ -0,0 +1,12 @@
<?php
use Incoviba\API\Common\Controller\Ventas;
$app->group('/ventas', function ($app) {
$app->post('/add[/]', [Ventas::class, 'add']);
$app->get('[/]', Ventas::class);
});
$app->group('/venta/{venta_id}', function ($app) {
$app->put('/edit[/]', [Ventas::class, 'edit']);
$app->delete('/edit[/]', [Ventas::class, 'delete']);
$app->get('[/]', [Ventas::class, 'show']);
});

55
setup/app.php Normal file
View File

@ -0,0 +1,55 @@
<?php
use DI\ContainerBuilder as Builder;
use DI\Bridge\Slim\Bridge;
include_once 'composer.php';
$builder = new Builder();
$folders = [
'settings',
'setups'
];
foreach ($folders as $f) {
$folder = implode(DIRECTORY_SEPARATOR, [
__DIR__,
$f
]);
if (!file_exists($folder)) {
continue;
}
$files = new DirectoryIterator($folder);
foreach ($files as $file) {
if ($file->isDir()) {
continue;
}
$builder->addDefinitions($file->getRealPath());
}
}
$container = $builder->build();
$app = Bridge::create($container);
if ($app->getContainer()->has('base_url')) {
$app->setBasePath($app->getContainer()->get('base_url'));
}
$folder = implode(DIRECTORY_SEPARATOR, [
__DIR__,
'middlewares'
]);
if (file_exists($folder)) {
$files = new DirectoryIterator($folder);
foreach ($files as $file) {
if ($file->isDir() and $file->getExtension() != 'php') {
continue;
}
include_once $file->getRealPath();
}
}
$app->addRoutingMiddleware();
//include_once 'databases.php';
include_once 'router.php';
return $app;

6
setup/composer.php Normal file
View File

@ -0,0 +1,6 @@
<?php
include_once implode(DIRECTORY_SEPARATOR, [
dirname(__FILE__, 2),
'vendor',
'autoload.php'
]);

24
setup/databases.php Normal file
View File

@ -0,0 +1,24 @@
<?php
$database = $app->getContainer()->get('database');
foreach ($database->databases as $name => $settings) {
switch (strtolower($settings->engine)) {
case 'mysql':
$dsn = "mysql:host={$settings->host->name};dbname={$settings->name}" . (isset($settings->host->port) ? ';port=' . $settings->host->port : '');
Orm::configure([
'connection_string' => $dsn,
'username' => $settings->user->name,
'password' => $settings->user->password
], null, $name);
break;
}
if (isset($settings->logging) and $settings->logging) {
Orm::configure('logging', true, $name);
}
if (isset($settings->caching) and $settings->caching) {
Orm::configure('caching', true, $name);
}
}
if (isset($database->short_names) and $database->short_names) {
Orm::$short_table_names = true;
}

View File

@ -0,0 +1,4 @@
<?php
use Psr\Container\ContainerInterface as Container;
$app->add($app->getContainer()->get(Zeuxisoo\Whoops\Slim\WhoopsMiddleware::class));

View File

@ -0,0 +1,2 @@
<?php
$app->add($app->getContainer()->get(Incoviba\API\Common\Middleware\Auth::class));

9
setup/router.php Normal file
View File

@ -0,0 +1,9 @@
<?php
$folder = $app->getContainer()->get('folders')->routes;
$files = new DirectoryIterator($folder);
foreach ($files as $file) {
if ($file->isDir() or $file->getExtension() != 'php') {
continue;
}
include_once $file->getRealPath();
}

View File

@ -0,0 +1,5 @@
<?php
return [
'debug' => $_ENV['DEBUG'] ?? false,
'API_KEY' => $_ENV['API_KEY']
];

View File

@ -0,0 +1,25 @@
<?php
return [
'database' => function() {
$arr = [
'default' => (object) [
'engine' => 'mysql',
'driver' => 'pdo_mysql',
'host' => (object) [
'name' => $_ENV['MYSQL_HOST'] ?? 'db'
],
'user' => (object) [
'name' => $_ENV['MYSQL_USER'],
'password' => $_ENV['MYSQL_PASSWORD']
],
'name' => $_ENV['MYSQL_DATABASE'],
'logging' => true,
'caching' => true
]
];
return (object) [
'short_names' => true,
'databases' => $arr
];
}
];

View File

@ -0,0 +1,15 @@
<?php
return [
'folders' => function() {
$arr = ['base' => dirname(__FILE__, 3)];
$arr['resources'] = implode(DIRECTORY_SEPARATOR, [
$arr['base'],
'resources'
]);
$arr['routes'] = implode(DIRECTORY_SEPARATOR, [
$arr['resources'],
'routes'
]);
return (object) $arr;
}
];

11
setup/setups/01_env.php Normal file
View File

@ -0,0 +1,11 @@
<?php
use Psr\Container\ContainerInterface as Container;
return [
Zeuxisoo\Whoops\Slim\WhoopsMiddleware::class => function(Container $c) {
return new Zeuxisoo\Whoops\Slim\WhoopsMiddleware([
'enable' => $c->get('debug'),
'editor' => 'vscode'
]);
}
];

15
setup/setups/02_auth.php Normal file
View File

@ -0,0 +1,15 @@
<?php
use Psr\Container\ContainerInterface as Container;
return [
Incoviba\API\Common\Service\Auth::class => function(Container $c) {
return new Incoviba\API\Common\Service\Auth($c->get('API_KEY'));
},
Incoviba\API\Common\Middleware\Auth::class => function(Container $c) {
return new Incoviba\API\Common\Middleware\Auth(
$c->get(Incoviba\API\Common\Service\Auth::class),
$c->get(Slim\Psr7\Factory\ResponseFactory::class),
['/auth/generate']
);
}
];

View File

@ -0,0 +1,19 @@
<?php
use Psr\Container\ContainerInterface;
return [
\Doctrine\DBAL\Connection::class => function(ContainerInterface $container) {
$config = $container->get('database')->databases['default'];
$conn = [
'dbname' => $config->name,
'user' => $config->user->name,
'password' => $config->user->password,
'host' => $config->host->name,
'driver' => $config->driver
];
if (isset($config->host->port)) {
$conn['port'] = $config->host->port;
}
return \Doctrine\DBAL\DriverManager::getConnection($conn);
}
];

14
src/Admin/Config.php Normal file
View File

@ -0,0 +1,14 @@
<?php
namespace Incoviba\Admin;
use Incoviba\API\Common\Alias\Model;
/**
* @property int $id
* @property string $name
* @property string $value
*/
class Config extends Model {
public static $_table = 'configurations';
protected static $fields = ['name', 'value'];
}

48
src/Auth/Login.php Normal file
View File

@ -0,0 +1,48 @@
<?php
namespace Incoviba\Auth;
use Carbon\Carbon;
use DateTime;
use Incoviba\API\Common\Alias\Model;
use Incoviba\Admin\Config;
/**
* @property int $id
* @property int $user_id
* @property DateTime $time
* @property string $selector
* @property string $token
* @property int $status
*
*/
class Login extends Model {
public static $_table = 'logins';
protected static $fields = ['user_id', 'time', 'selector', 'token', 'status'];
protected $user;
public function user() {
if ($this->user === null) {
$this->user = $this->childOf(User::class, [Model::SELF_KEY => 'user_id']);
}
return $this->user;
}
public function time(DateTime $time = null) {
if ($time === null) {
return Carbon::parse($this->time);
}
$this->time = $time->format('Y-m-d H:m:s');
return null;
}
public function isValid() {
if ($this->status == 0) {
return false;
}
$expiration = $this->factory->find(Config::class)->where([['name', 'cookie_expiration_time']])->one();
if ($this->time()->diffInSeconds() > $expiration->value) {
$this->status = 0;
$this->save();
return false;
}
return true;
}
}

79
src/Auth/User.php Normal file
View File

@ -0,0 +1,79 @@
<?php
namespace Incoviba\Auth;
use Carbon\Carbon;
use Incoviba\Admin\Config;
use Incoviba\API\Common\Alias\Model;
/**
* @property int $id
* @property string $name
* @property string $password
* @property int $enabled
*/
class User extends Model {
public static $_table = 'users';
protected $logins;
public function logins() {
if ($this->logins === null) {
$this->logins = $this->parentOf(Login::class, [Model::CHILD_KEY => 'user_id']);
}
return $this->logins;
}
protected $login;
public function login() {
if ($this->login === null) {
$this->login = $this->factory->find(Login::class)
->where([['user_id', $this->id]])
->order([['column' => 'time', 'direction' => 'desc']])
->one(1);
}
return $this->login;
}
public function isIn(): bool {
return $this->login()->isValid();
}
public function validate($password): bool {
return password_verify($password, $this->password);
}
public function validLogins(): bool|array {
return $this->factory->find(Login::class)->where([['user_id', $this->id], ['status', 1]])->many();
}
public function logout() {
$logins = $this->validLogins();
if ($logins === false) {
return true;
}
$bool = true;
foreach ($logins as $login) {
$login->status = 0;
$bool &= $login->save();
}
return $bool;
}
public function setToken($selector, $token) {
$this->logout();
$expiration = $this->factory->find(Config::class)->where([['name', 'cookie_expiration_time']])->one();
$data = [
'user_id' => $this->id,
'time' => Carbon::now()->format('Y-m-d H:i:s '),
'selector' => $selector,
'token' => $token,
'status' => 1
];
$output = [
'input' => $data,
'login' => null,
'logged_in' => false
];
$login = Login::add($this->factory, $data);
$output['login'] = $login;
if ($login !== false and $login->is_new()) {
$output['logged_in'] = $login->save();
$output['expires'] = $login->time()->addSeconds($expiration->value)->timestamp;
}
return $output;
}
}

13
src/Common/Banco.php Normal file
View File

@ -0,0 +1,13 @@
<?php
namespace Incoviba\Common;
use Incoviba\API\Common\Alias\Model;
/**
* @property int $id
* @property string $nombre
*/
class Banco extends Model {
public static $_table = 'banco';
protected static $fields = ['nombre'];
}

14
src/Common/Comuna.php Normal file
View File

@ -0,0 +1,14 @@
<?php
namespace Incoviba\Common;
use Incoviba\API\Common\Alias\Model;
/**
* @property int $id
* @property string $descripcion
* @property Provincia $provincia
*/
class Comuna extends Model {
public static $_table = 'comuna';
protected static $fields = ['descripcion', 'provincia'];
}

24
src/Common/Direccion.php Normal file
View File

@ -0,0 +1,24 @@
<?php
namespace Incoviba\Common;
use Incoviba\API\Common\Alias\Model;
/**
* @property int $id
* @property string $calle
* @property int $numero
* @property string $extra
* @property Comuna $comuna
*/
class Direccion extends Model {
public static $_table = 'direccion';
protected static $fields = ['calle', 'numero', 'extra', 'comuna'];
protected ?Comuna $comuna_o;
public function comuna(): ?Comuna {
if ($this->comuna_o === null) {
$this->comuna_o = $this->childOf(Comuna::class, [Model::SELF_KEY => 'comuna']);
}
return $this->comuna_o;
}
}

29
src/Common/Provincia.php Normal file
View File

@ -0,0 +1,29 @@
<?php
namespace Incoviba\Common;
use Incoviba\API\Common\Alias\Model;
/**
* @property int $id
* @property string $descripcion
* @property Region $region
*/
class Provincia extends Model {
public static $_table = 'provincia';
protected static $fields = ['descripcion', 'region'];
protected ?Region $region_o;
public function region(): ?Region {
if ($this->region_o === null) {
$this->region_o = $this->childOf(Region::class, [Model::SELF_KEY => 'region']);
}
return $this->region_o;
}
protected ?array $comunas;
public function comunas(): ?array {
if ($this->comunas === null) {
$this->comunas = $this->parentOf(Comuna::class, [Model::CHILD_KEY => 'provincia']);
}
return $this->comunas;
}
}

23
src/Common/Region.php Normal file
View File

@ -0,0 +1,23 @@
<?php
namespace Incoviba\Common;
use Incoviba\API\Common\Alias\Model;
/**
* @property int $id
* @property string $descripcion
* @property string $numeral
* @property int $numeracion
*/
class Region extends Model {
public static $_table = 'region';
protected static $fields = ['descripcion', 'numeral', 'numeracion'];
protected ?array $provincias;
public function provincias(): ?array {
if ($this->provincias === null) {
$this->provincias = $this->parentOf(Provincia::class, [Model::CHILD_KEY => 'region']);
}
return $this->provincias;
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace Incoviba\Inmobiliaria;
use Incoviba\API\Common\Alias\Model;
use Incoviba\Common\Banco;
use Incoviba\Proyecto\Proyecto;
/**
* @property int $rut
* @property string $dv
* @property string $razon
* @property string $abreviacion
* @property string $cuenta
* @property Banco $banco
* @property TipoSociedad $sociedad
*/
class Inmobiliaria extends Model {
public static $_table = 'inmobiliaria';
public static $_id_column = ['rut'];
protected static $fields = ['rut', 'dv', 'razon', 'abreviacion', 'cuenta'];
protected $banco_o;
public function banco() {
if ($this->banco_o === null) {
$this->banco_o = $this->childOf(Banco::class, [Model::SELF_KEY => 'banco']);
}
return $this->banco_o;
}
protected $sociedad_o;
public function sociedad() {
if ($this->sociedad_o === null) {
$this->sociedad_o = $this->childOf(TipoSociedad::class, [Model::SELF_KEY => 'sociedad']);
}
return $this->sociedad_o;
}
protected $proyectos;
public function proyectos(): ?array {
if ($this->proyectos === null) {
$this->proyectos = $this->parentOf(Proyecto::class, [Model::CHILD_KEY => 'inmobiliaria', Model::SELF_KEY => 'rut']);
}
return $this->proyectos;
}
}

View File

@ -0,0 +1,9 @@
<?php
namespace Incoviba\Inmobiliaria;
use Incoviba\API\Common\Alias\Model;
class TipoSociedad extends Model {
public static $_table = 'tipo_sociedad';
protected static $fields = ['descripcion', 'abreviacion'];
}

26
src/Mapper/Agente.php Normal file
View File

@ -0,0 +1,26 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Proyecto\Agente as BaseModel;
class Agente extends Mapper
{
protected string $table = 'agente';
protected function load(bool|array $row, bool $lazy = false): BaseModel|bool
{
$model = new BaseModel();
$model->id = $row['id'];
$model->rut = $row['rut'];
$model->descripcion = $row['descripcion'];
$model->abreviacion = $row['abreviacion'];
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
}

25
src/Mapper/AgenteTipo.php Normal file
View File

@ -0,0 +1,25 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Proyecto\AgenteTipo as AgenteModel;
class AgenteTipo extends Mapper
{
protected string $table = 'agente_tipo';
protected function load(bool|array $row, bool $lazy = false): AgenteModel|bool
{
$model = new AgenteModel();
$model->id = $row['id'];
$model->agente = $this->getMapper(Agente::class)->fetchById($row['agente']);
$model->tipo = $this->getMapper(TipoAgente::class)->fetchById($row['tipo']);
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
}

25
src/Mapper/Banco.php Normal file
View File

@ -0,0 +1,25 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Common\Banco as BancoModel;
class Banco extends Mapper
{
protected string $table = 'banco';
protected function load(bool|array $row, bool $lazy = false): BancoModel|bool
{
if (!$row) return false;
$model = new BancoModel();
$model->id = $row['id'];
$model->nombre = $row['nombre'];
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
}

22
src/Mapper/BonoPie.php Normal file
View File

@ -0,0 +1,22 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Venta\BonoPie as BaseModel;
class BonoPie extends Mapper
{
protected string $table = 'bono_pie';
protected function load(bool|array $row, bool $lazy = false): BaseModel|bool
{
$model = new BaseModel();
$model->id = $row['id'];
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
}

38
src/Mapper/Cierre.php Normal file
View File

@ -0,0 +1,38 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Venta\Cierre as CierreModel;
class Cierre extends Mapper
{
protected string $table = 'cierre';
protected function load(bool|array $row, bool $lazy = false): CierreModel|bool
{
if (!$row) return false;
$model = new CierreModel();
$model->id = $row['id'];
$model->proyecto = $this->getMapper(Proyecto::class)->fetchById($row['proyecto']);
$model->precio = $row['precio'];
$model->fecha = new \DateTimeImmutable($row['fecha']);
$model->relacionado = $row['relacionado'] != 0;
$model->propietario = $row['propietario'] == 0 ? 0 : $this->getMapper(Propietario::class)->fetchById($row['propietario']);
$model->setFactory($this->factory);
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
public function fetchByProyecto(int $proyecto_id): array
{
$qb = $this->connection->createQueryBuilder()
->select('*')
->from($this->table)
->where('proyecto = ?');
return array_map([$this, 'load'], $this->connection->executeQuery($qb, [$proyecto_id])->fetchAllAssociative());
}
}

25
src/Mapper/Comuna.php Normal file
View File

@ -0,0 +1,25 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Common\Comuna as BaseModel;
class Comuna extends Mapper
{
protected string $table = 'comuna';
protected function load(bool|array $row, bool $lazy = false): BaseModel|bool
{
$model = new BaseModel();
$model->id = $row['id'];
$model->descripcion = $row['descripcion'];
// $model->provincia = $this->getMapper(Provincia::class)->fetchById($row['provincia']);
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
}

33
src/Mapper/Config.php Normal file
View File

@ -0,0 +1,33 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Admin\Config as ConfigModel;
use Incoviba\Model\Model;
class Config extends Mapper
{
protected string $table = 'configurations';
protected function load(array|bool $row, bool $lazy = false): ConfigModel|bool
{
if (!$row) return false;
$model = new ConfigModel();
$model->id = $row['id'];
$model->name = $row['name'];
$model->value = $row['value'];
return $model;
}
public function save(Model $model): bool
{
return true;
}
public function fetchByName(string $name): ConfigModel
{
$qb = $this->connection->createQueryBuilder()
->select('*')
->from($this->table)
->where('name = ?');
return $this->load(($this->connection->executeQuery($qb, [$name]))->fetchAssociative());
}
}

22
src/Mapper/Credito.php Normal file
View File

@ -0,0 +1,22 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Venta\Credito as CreditoModel;
class Credito extends Mapper
{
public string $table = 'credito';
protected function load(bool|array $row, bool $lazy = false): CreditoModel|bool
{
$credito = new CreditoModel();
$credito->id = $row['id'];
return $credito;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
}

93
src/Mapper/Cuota.php Normal file
View File

@ -0,0 +1,93 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Venta\Cuota as CuotaModel;
class Cuota extends Mapper
{
protected string $table = 'cuota';
protected function load(bool|array $row, bool $lazy = false): CuotaModel|bool
{
if (!$row) return false;
$model = new CuotaModel();
$model->id = $row['id'];
$model->pie = $this->getMapper(Pie::class)->fetchById($row['pie']);
$model->fecha = new \DateTimeImmutable($row['fecha']);
$model->valor = $row['valor_$'];
$model->estado = $row['estado'];
$model->banco = ($row['banco'] != '') ? $this->getMapper(Banco::class)->fetchById($row['banco']) : null;
$estado = new CuotaModel\Estado();
$estado->fecha_pago = ($row['fecha_pago']) ? new \DateTimeImmutable($row['fecha_pago']) : null;
$estado->abonado = $row['abonado'];
$estado->fecha_abono = ($row['fecha_abono']) ? new \DateTimeImmutable($row['fecha_abono']) : null;
$model->estado_cuota = $estado;
$model->uf = $row['uf'];
$model->pago = $this->getMapper(Pago::class)->fetchById($row['pago']);
$model->numero = $row['numero'];
return $model;
}
public function save(Model $model): bool
{
return true;
}
public function fetchByProyecto(int $proyecto_id): array
{
$qb = $this->connection->createQueryBuilder()
->select('a.*')
->from($this->table, 'a')
->innerJoin('a', 'venta', 'v', 'v.pie = a.pie')
->innerJoin('v', 'propiedad', 'p', 'p.id = v.propiedad')
->innerJoin('p', 'unidad', 'u', 'u.id = p.unidad_principal')
->innerJoin('u', 'proyecto_tipo_unidad', 'ptu', 'ptu.id = u.pt')
->where('ptu.proyecto = ?');
return array_map([$this, 'load'], $this->connection->executeQuery($qb, [$proyecto_id])->fetchAllAssociative());
}
public function fetchByProyectoAndFecha(int $proyecto_id, \DateTimeInterface $fecha): array
{
$qb = $this->connection->createQueryBuilder()
->select('a.*')
->from($this->table, 'a')
->innerJoin('a', 'venta', 'v', 'v.pie = a.pie')
->innerJoin('v', 'propiedad', 'p', 'p.id = v.propiedad')
->innerJoin('p', 'unidad', 'u', 'u.id = p.unidad_principal')
->innerJoin('u', 'proyecto_tipo_unidad', 'ptu', 'ptu.id = u.pt')
->innerJoin('a', 'pago', 'g', 'g.id = a.pago')
->where('ptu.proyecto = ?')
->andWhere('g.fecha = ?');
return array_map([$this, 'load'], $this->connection->executeQuery($qb, [$proyecto_id, $fecha->format('y-m-d')])->fetchAllAssociative());
}
public function fetchByProyectoAndMes(int $proyecto_id, \DateTimeInterface $mes): array
{
$qb = $this->connection->createQueryBuilder()
->select('a.*')
->from($this->table, 'a')
->innerJoin('a', 'venta', 'v', 'v.pie = a.pie')
->innerJoin('v', 'propiedad', 'p', 'p.id = v.propiedad')
->innerJoin('p', 'unidad', 'u', 'u.id = p.unidad_principal')
->innerJoin('u', 'proyecto_tipo_unidad', 'ptu', 'ptu.id = u.pt')
->innerJoin('a', 'pago', 'g', 'g.id = a.pago')
->where('ptu.proyecto = ?')
->andWhere('g.fecha BETWEEN ? AND ?');
return array_map([$this, 'load'], $this->connection->executeQuery($qb, [$proyecto_id, $mes->format('y-m-1'), $mes->format('y-m-t')])->fetchAllAssociative());
}
public function fetchByProyectoAndPendiente(int $proyecto_id): array
{
$qb = $this->connection->createQueryBuilder()
->select('a.*')
->from($this->table, 'a')
->innerJoin('a', 'venta', 'v', 'v.pie = a.pie')
->innerJoin('v', 'propiedad', 'p', 'p.id = v.propiedad')
->innerJoin('p', 'unidad', 'u', 'u.id = p.unidad_principal')
->innerJoin('u', 'proyecto_tipo_unidad', 'ptu', 'ptu.id = u.pt')
->innerJoin('a', 'pago', 'g', 'g.id = a.pago')
->leftJoin('g', '(SELECT e1.* FROM estado_pago e1 JOIN (SELECT MAX(id) AS id, pago FROM estado_pago GROUP BY pago) e0 ON e0.id = e1.id)', 'ep', 'ep.pago = g.id')
->where('ptu.proyecto = ?')
->andWhere('ep.estado < 1')
->andWhere('ep.estado >= 0');
return array_map([$this, 'load'], $this->connection->executeQuery($qb, [$proyecto_id])->fetchAllAssociative());
}
}

28
src/Mapper/Direccion.php Normal file
View File

@ -0,0 +1,28 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Common\Direccion as DireccionModel;
class Direccion extends Mapper
{
protected string $table = 'direccion';
protected function load(bool|array $row, bool $lazy = false): DireccionModel|bool
{
if (!$row) return false;
$model = new DireccionModel();
$model->id = $row['id'];
$model->calle = $row['calle'];
$model->numero = $row['numero'];
$model->extra = $row['extra'];
$model->comuna = $this->getMapper(Comuna::class)->fetchById($row['comuna']);
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
}

23
src/Mapper/Entrega.php Normal file
View File

@ -0,0 +1,23 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Venta\Entrega as BaseModel;
class Entrega extends Mapper
{
protected string $table = 'entrega';
protected function load(bool|array $row, bool $lazy = false): BaseModel|bool
{
$model = new BaseModel();
$model->id = $row['id'];
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
}

21
src/Mapper/Escritura.php Normal file
View File

@ -0,0 +1,21 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Venta\Escritura as EscrituraModel;
class Escritura extends Mapper
{
protected string $table = 'escritura';
protected function load(bool|array $row, bool $lazy = false): EscrituraModel|bool
{
$escritura = new EscrituraModel();
$escritura->id = $row['id'];
return $escritura;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Venta\EstadoCierre as EstadoModel;
class EstadoCierre extends Mapper
{
protected string $table = 'estado_cierre';
protected function load(bool|array $row, bool $lazy = false): EstadoModel|bool
{
if (!$row) return false;
$model = new EstadoModel();
$model->id = $row['id'];
$model->fecha = new \DateTimeImmutable($row['fecha']);
$model->tipo = $this->getMapper(TipoEstadoCierre::class)->fetchById($row['tipo']);
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
public function fetchLastByCierre(int $cierre_id)
{
$qb = $this->connection->createQueryBuilder()
->select('*')
->from($this->table)
->where('cierre = ?')
->orderBy('id', 'desc')
->setMaxResults(1);
return $this->load($this->connection->executeQuery($qb, [$cierre_id])->fetchAssociative(), true);
}
}

View File

@ -0,0 +1,45 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Venta\EstadoPrecio as EstadoModel;
class EstadoPrecio extends Mapper
{
protected string $table = 'estado_precio';
protected function load(bool|array $row, bool $lazy = false): EstadoModel|bool
{
if (!$row) return false;
$model = new EstadoModel();
$model->id = $row['id'];
$model->fecha = new \DateTimeImmutable($row['fecha']);
$model->estado = $this->getMapper(TipoEstadoPrecio::class)->fetchById($row['estado']);
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
public function fetchByPrecio(int $precio_id): array
{
$qb = $this->connection->createQueryBuilder()
->select('*')
->from($this->table)
->where('precio = ?');
return array_map([$this, 'load'], $this->connection->executeQuery($qb, [$precio_id])->fetchAllAssociative());
}
public function fetchLastByPrecio(int $precio_id): EstadoModel|bool
{
$qb = $this->connection->createQueryBuilder()
->select('*')
->from($this->table)
->where('precio = ?')
->setMaxResults(1)
->orderBy('id', 'desc');
return $this->load($this->connection->executeQuery($qb, [$precio_id])->fetchAssociative());
}
}

View File

@ -0,0 +1,55 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Proyecto\EstadoProyecto as BaseModel;
class EstadoProyecto extends Mapper
{
protected string $table = 'estado_proyecto';
protected function load(bool|array $row, bool $lazy = false): BaseModel|bool
{
$model = new BaseModel();
$model->id = $row['id'];
$model->proyecto = $this->getMapper(Proyecto::class)->fetchById($row['proyecto']);
$model->tipo = $this->getMapper(TipoEstadoProyecto::class)->fetchById($row['estado']);
$model->fecha = new \DateTimeImmutable($row['fecha']);
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
public function fetchByProyecto(int $proyecto_id): array
{
$qb = $this->connection->createQueryBuilder()
->select('*')
->from($this->table)
->where('proyecto = ?');
return array_map([$this, 'load'], $this->connection->executeQuery($qb, [$proyecto_id])->fetchAllAssociative());
}
public function fetchLastByProyecto(int $proyecto_id): BaseModel|bool
{
$qb = $this->connection->createQueryBuilder()
->select('*')
->from($this->table)
->where('proyecto = ?')
->setMaxResults(1)
->orderBy('id', 'desc');
return $this->load($this->connection->executeQuery($qb, [$proyecto_id])->fetchAssociative());
}
public function fetchFirstByProyecto(int $proyecto_id): BaseModel|bool
{
$qb = $this->connection->createQueryBuilder()
->select('*')
->from($this->table)
->where('proyecto = ?')
->setMaxResults(1)
->orderBy('id', 'asc');
return $this->load($this->connection->executeQuery($qb, [$proyecto_id])->fetchAssociative());
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Venta\EstadoVenta as BaseModel;
class EstadoVenta extends Mapper
{
protected string $table = 'estado_venta';
protected function load(bool|array $row, bool $lazy = false): BaseModel|bool
{
$model = new BaseModel();
$model->id = $row['id'];
$model->estado = $this->getMapper(TipoEstadoVenta::class)->fetchById($row['estado']);
$model->fecha = new \DateTimeImmutable($row['fecha']);
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
public function fetchByVenta(int $venta_id): array
{
$qb = $this->connection->createQueryBuilder()
->select('*')
->from($this->table)
->where('venta = ?');
return array_map([$this, 'load'], $this->connection->executeQuery($qb, [$venta_id])->fetchAllAssociative());
}
public function fetchLastByVenta(int $venta_id): BaseModel|bool
{
$qb = $this->connection->createQueryBuilder()
->select('*')
->from($this->table)
->where('venta = ?')
->setMaxResults(1)
->orderBy('id', 'desc');
return $this->load($this->connection->executeQuery($qb, [$venta_id])->fetchAssociative());
}
}

25
src/Mapper/Etapa.php Normal file
View File

@ -0,0 +1,25 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Proyecto\Etapa as BaseModel;
class Etapa extends Mapper
{
protected string $table = 'etapa_proyecto';
protected function load(bool|array $row, bool $lazy = false): BaseModel|bool
{
$model = new BaseModel();
$model->id = $row['id'];
$model->descripcion = $row['descripcion'];
$model->orden = $row['orden'];
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Inmobiliaria\Inmobiliaria as InmobiliariaModel;
class Inmobiliaria extends Mapper
{
protected string $id = 'rut';
protected string $table = 'inmobiliaria';
protected function load(bool|array $row, bool $lazy = false): InmobiliariaModel|bool
{
if (!$row) return false;
$model = new InmobiliariaModel();
$model->rut = $row['rut'];
$model->dv = $row['dv'];
$model->razon = $row['razon'];
$model->abreviacion = $row['abreviacion'];
$model->cuenta = $row['cuenta'];
$model->banco = ($row['banco']) ? $this->getMapper(Banco::class)->fetchById($row['banco']) : null;
$model->sociedad = $row['sociedad'];
return $model;
}
public function save(Model $model): bool
{
return false;
}
}

82
src/Mapper/Login.php Normal file
View File

@ -0,0 +1,82 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Auth\Login as LoginModel;
use Incoviba\Model\Model;
class Login extends Mapper
{
protected string $table = 'logins';
public function load(array|bool $row, bool $lazy = false): LoginModel|bool
{
if (!$row) return false;
$model = new LoginModel();
if (isset($row['id'])) {
$model->id = (int) $row['id'];
}
if (!$lazy) {
$model->user = $this->getMapper(User::class)->fetchById((int) $row['user_id']);
}
$model->time = new \DateTimeImmutable($row['time']);
$model->selector = $row['selector'];
$model->token = $row['token'];
$model->status = $row['status'] != 0;
return $model;
}
public function save(Model $model): bool
{
if (!isset($model->id)) {
$qb = $this->connection->createQueryBuilder()
->insert($this->table)
->values([
'user_id' => '?',
'time' => '?',
'selector' => '?',
'token' => '?',
'status' => '?'
]);
return $this->connection->executeStatement($qb, [
$model->user->id,
$model->time->format('Y-m-d H:i:s'),
$model->selector,
$model->token,
$model->status ? 1 : 0
]) > 0;
}
$qb = $this->connection->createQueryBuilder()
->update($this->table)
->set('user_id', '?')
->set('time', '?')
->set('selector', '?')
->set('token', '?')
->set('status', '?')
->where('id = ?');
return $this->connection->executeStatement($qb, [
$model->user->id,
$model->time->format('Y-m-d H:i:s'),
$model->selector,
$model->token,
$model->status ? 1 : 0,
$model->id
]) > 0;
}
public function fetchActiveByUser(int $user_id): array
{
$qb = $this->connection->createQueryBuilder()
->select('*')
->from($this->table)
->where('user_id = ?')
->andWhere('status = 1');
return array_map([$this, 'load'], $this->connection->executeQuery($qb, [$user_id])->fetchAllAssociative());
}
public function fetchBySelector(string $selector): LoginModel|bool
{
$qb = $this->connection->createQueryBuilder()
->select('*')
->from($this->table)
->where('selector = ?');
return $this->load($this->connection->executeQuery($qb, [$selector])->fetchAssociative());
}
}

80
src/Mapper/Mapper.php Normal file
View File

@ -0,0 +1,80 @@
<?php
namespace Incoviba\Mapper;
use Doctrine\DBAL\Connection;
use Incoviba\Model\Model;
use Psr\Container\ContainerInterface;
use Incoviba\API\Common\Factory\Mapper as Factory;
abstract class Mapper
{
protected Connection $connection;
protected array $mappers;
protected Factory $factory;
public function __construct(ContainerInterface $container)
{
$this->factory = $container->get(Factory::class);
$this->connection = $container->get(Connection::class);
}
protected string $id = 'id';
protected string $table = '';
public function fetch(string $name, array $params = [])
{
$method = 'fetch' . str_replace(' ', '', ucwords(str_replace('_', ' ', $name)));
if (method_exists($this, $method)) {
return call_user_func_array([$this, $method], $params);
}
throw new \InvalidArgumentException("Method {$method} not found in " . get_called_class());
}
public function fetchById(int $id): Model|bool
{
$qb = $this->connection->createQueryBuilder()
->select('*')
->from($this->table)
->where("{$this->id} = ?");
$row = $this->connection->executeQuery($qb, [$id])->fetchAssociative();
return $this->load($row);
}
public function fetchAll(): array
{
$qb = $this->connection->createQueryBuilder()
->select('*')
->from($this->table);
$row = $this->connection->executeQuery($qb)->fetchAllAssociative();
return array_map([$this, 'load'], $row);
}
public function getMapper(string $name): Mapper
{
if (!class_exists($name)) {
throw new \InvalidArgumentException("Mapper {$name} does not exist");
}
if (!isset($this->mappers)) {
$this->mappers = [];
}
if (!isset($this->mappers[$name])) {
$this->mappers[$name] = $this->factory->get($name);
}
return $this->mappers[$name];
}
public function create(array $data): Model|bool
{
$qb = $this->connection->createQueryBuilder()
->select('*')
->from($this->table);
foreach (array_keys($data) as $field) {
$qb = $qb->andWhere("{$field} = ?");
}
$row = ($this->connection->executeQuery($qb, array_values($data)))->fetchAssociative();
if (!$row) {
$row = $data;
}
return $this->load($row);
}
abstract protected function load(array|bool $row, bool $lazy = false): Model|bool;
abstract public function save(Model $model): bool;
}

32
src/Mapper/Pago.php Normal file
View File

@ -0,0 +1,32 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Venta\Pago as PagoModel;
class Pago extends Mapper
{
protected string $table = 'pago';
protected function load(bool|array $row, bool $lazy = false): PagoModel|bool
{
if (!$row) return false;
$model = new PagoModel();
$model->id = $row['id'];
$model->valor = $row['valor'];
$model->banco = ($row['banco']) ? $this->getMapper(Banco::class)->fetchById($row['banco']) : null;
$model->tipo = $this->getMapper(TipoPago::class)->fetchById($row['tipo']);
$model->identificador = $row['identificador'];
$model->fecha = new \DateTimeImmutable($row['fecha']);
$model->uf = $row['uf'];
$model->pagador = $row['pagador'];
$model->asociado = ($row['asociado']) ? $this->fetchById($row['asociado']) : null;
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
}

23
src/Mapper/Pie.php Normal file
View File

@ -0,0 +1,23 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Venta\Pie as PieModel;
class Pie extends Mapper
{
protected string $table = 'pie';
protected function load(bool|array $row, bool $lazy = false): PieModel|bool
{
if (!$row) return false;
$model = new PieModel();
$model->id = $row['id'];
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
}

43
src/Mapper/Precio.php Normal file
View File

@ -0,0 +1,43 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Venta\Precio as PrecioModel;
class Precio extends Mapper
{
protected string $table = 'precio';
protected function load(bool|array $row, bool $lazy = false): PrecioModel|bool
{
if (!$row) return false;
$model = new PrecioModel();
$model->id = $row['id'];
$model->valor = $row['valor'];
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
public function fetchByUnidad(int $unidad_id): array
{
$qb = $this->connection->createQueryBuilder()
->select('*')
->from($this->table)
->where('unidad = ?');
return array_map([$this, 'load'], $this->connection->executeQuery($qb, [$unidad_id])->fetchAllAssociative());
}
public function fetchLastByUnidad(int $unidad_id): PrecioModel|bool
{
$qb = $this->connection->createQueryBuilder()
->select('*')
->from($this->table)
->where('unidad = ?')
->orderBy('id', 'desc')
->setMaxResults(1);
return $this->load($this->connection->executeQuery($qb, [$unidad_id])->fetchAssociative());
}
}

22
src/Mapper/Promocion.php Normal file
View File

@ -0,0 +1,22 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Venta\Promocion as PromocionModel;
class Promocion extends Mapper
{
protected string $table = 'promocion';
protected function load(bool|array $row, bool $lazy = false): PromocionModel|bool
{
$model = new PromocionModel();
$model->id = $row['id'];
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
}

24
src/Mapper/Propiedad.php Normal file
View File

@ -0,0 +1,24 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Venta\Propiedad as PropiedadModel;
class Propiedad extends Mapper
{
protected string $table = 'propiedad';
protected function load(bool|array $row, bool $lazy = false): PropiedadModel|bool
{
$propiedad = new PropiedadModel();
$propiedad->id = $row['id'];
$propiedad->estado = $row['estado'] != 0;
$propiedad->unidades = $this->getMapper(Unidad::class)->fetchByPropiedad($row['id']);
return $propiedad;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Venta\Propietario as PropietarioModel;
class Propietario extends Mapper
{
protected string $id = 'rut';
protected string $table = 'propietario';
protected function load(bool|array $row, bool $lazy = false): PropietarioModel|bool
{
if (!$row) return false;
$model = new PropietarioModel();
$model->rut = $row['rut'];
$model->nombres = $row['nombres'];
$model->setApellidos($row['apellido_paterno'], $row['apellido_materno']);
$datos = new PropietarioModel\Datos();
$datos->sexo = $row['sexo'];
$datos->estado_civil = $row['estado_civil'];
$datos->profesion = $row['profesion'];
$datos->direccion = $row['direccion'] == 0 ? $this->getMapper(Direccion::class)->fetchById($row['direccion']) : 0;
$datos->telefono = $row['telefono'];
$datos->email = $row['email'];
$model->datos = $datos;
$model->repesentante = $row['representante'];
$model->otro = $row['otro'];
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
}

35
src/Mapper/Proyecto.php Normal file
View File

@ -0,0 +1,35 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Proyecto\Proyecto as ProyectoModel;
class Proyecto extends Mapper
{
protected string $table = 'proyecto';
protected function load(bool|array $row, bool $lazy = false): ProyectoModel|bool
{
if (!$row) return false;
$model = new ProyectoModel();
$model->id = $row['id'];
$model->inmobiliaria = $this->getMapper(Inmobiliaria::class)->fetchById($row['inmobiliaria']);
$model->descripcion = $row['descripcion'];
$model->direccion = $this->getMapper(Direccion::class)->fetchById($row['direccion']);
$model->valor_terreno = $row['valor_terreno'];
$superficie = new ProyectoModel\Superficie();
$superficie->terreno = $row['superficie_terreno'];
$superficie->bajo_nivel = $row['superficie_bajo_nivel'];
$superficie->sobre_nivel = $row['superficie_sobre_nivel'];
$model->superficie = $superficie;
$model->corredor = $row['corredor'];
$model->pisos = $row['pisos'];
$model->subterraneos = $row['subterraneos'];
return $model;
}
public function save(Model $model): bool
{
return true;
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Proyecto\ProyectoAgente as AgenteModel;
class ProyectoAgente extends Mapper
{
protected string $table = 'proyecto_agente';
protected function load(bool|array $row, bool $lazy = false): AgenteModel|bool
{
$model = new AgenteModel();
$model->id = $row['id'];
$model->proyecto = $this->getMapper(Proyecto::class)->fetchById($row['proyecto']);
$model->agenteTipo = $this->getMapper(AgenteTipo::class)->fetchById($row['agente']);
$model->fecha = new \DateTimeImmutable($row['fecha']);
$model->comision = $row['comision'];
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
public function fetchOperadoresVigenteByProyecto(int $proyecto_id): array
{
$qb = $this->connection->createQueryBuilder()
->select('pa.*')
->from($this->table, 'pa')
->innerJoin('pa', 'agente_tipo', 'at', 'at.id = pa.agente')
->innerJoin('at', 'tipo_agente', 'ta', 'ta.id = at.tipo')
->innerJoin('pa', '(SELECT e1.* FROM estado_proyecto_agente e1 JOIN (SELECT MAX(id) AS id, agente FROM estado_proyecto_agente GROUP BY agente) e0 ON e0.id = e1.id)', 'ep', 'ep.agente = pa.id')
->innerJoin('ep', 'tipo_estado_proyecto_agente', 'tep', 'tep.id = ep.tipo')
->where('pa.proyecto = ?')
->andWhere('ta.descripcion = ?')
->andWhere('tep.descripcion = ?');
return array_map([$this, 'load'], $this->connection->executeQuery($qb, [$proyecto_id, 'operador', 'vigente'])->fetchAllAssociative());
}
}

View File

@ -0,0 +1,43 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Proyecto\ProyectoTipoUnidad as TipoModel;
class ProyectoTipoUnidad extends Mapper
{
protected string $table = 'proyecto_tipo_unidad';
protected function load(bool|array $row, bool $lazy = false): TipoModel|bool
{
if (!$row) return false;
$model = new TipoModel();
$model->id = $row['id'];
$model->proyecto = $this->getMapper(Proyecto::class)->fetchById($row['proyecto']);
$model->tipo = $this->getMapper(TipoUnidad::class)->fetchById($row['tipo']);
$model->abreviacion = $row['abreviacion'];
$model->nombre = $row['nombre'];
$supreficie = new TipoModel\Superficie();
$supreficie->interior = $row['m2'];
$supreficie->logia = $row['logia'];
$supreficie->terraza = $row['terraza'];
$model->superficie = $supreficie;
$model->descripcion = $row['descripcion'];
$model->tipologias = $this->getMapper(TipoTipologia::class)->fetchByPTU($row['id']);
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
public function fetchByProyecto(int $proyecto_id): array
{
$qb = $this->connection->createQueryBuilder()
->select('*')
->from($this->table)
->where('proyecto = ?');
return array_map([$this, 'load'], $this->connection->executeQuery($qb, [$proyecto_id])->fetchAllAssociative());
}
}

22
src/Mapper/Subsidio.php Normal file
View File

@ -0,0 +1,22 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Venta\Subsidio as BaseModel;
class Subsidio extends Mapper
{
protected string $table = 'subsidio';
protected function load(bool|array $row, bool $lazy = false): BaseModel|bool
{
$model = new BaseModel();
$model->id = $row['id'];
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
}

24
src/Mapper/TipoAgente.php Normal file
View File

@ -0,0 +1,24 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Proyecto\TipoAgente as BaseModel;
class TipoAgente extends Mapper
{
protected string $table = 'tipo_agente';
protected function load(bool|array $row, bool $lazy = false): BaseModel|bool
{
$model = new BaseModel();
$model->id = $row['id'];
$model->descripcion = $row['descripcion'];
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Proyecto\TipoElemento as BaseModel;
class TipoElemento extends Mapper
{
protected string $table = 'tipo_elemento';
protected function load(bool|array $row, bool $lazy = false): BaseModel|bool
{
$model = new BaseModel();
$model->id = $row['id'];
$model->descripcion = $row['descripcion'];
$model->abreviacion = $row['abreviacion'];
$model->orden = $row['orden'];
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Venta\TipoEstadoCierre as TipoModel;
class TipoEstadoCierre extends Mapper
{
protected string $table = 'tipo_estado_cierre';
protected function load(bool|array $row, bool $lazy = false): TipoModel|bool
{
if (!$row) return false;
$model = new TipoModel();
$model->id = $row['id'];
$model->descripcion = $row['descripcion'];
$model->vigente = $row['vigente'] !== 0;
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Venta\TipoEstadoPrecio as TipoModel;
class TipoEstadoPrecio extends Mapper
{
protected string $table = 'tipo_estado_precio';
protected function load(bool|array $row, bool $lazy = false): TipoModel|bool
{
$model = new TipoModel();
$model->id = $row['id'];
$model->descripcion = $row['descripcion'];
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace Incoviba\Mapper;
use Incoviba\Model\Model;
use Incoviba\Model\Proyecto\TipoEstadoProyecto as BaseModel;
class TipoEstadoProyecto extends Mapper
{
protected string $table = 'tipo_estado_proyecto';
protected function load(bool|array $row, bool $lazy = false): BaseModel|bool
{
$model = new BaseModel();
$model->id = $row['id'];
$model->descripcion = $row['descripcion'];
$model->orden = $row['orden'];
$model->etapa = $this->getMapper(Etapa::class)->fetchById($row['etapa']);
return $model;
}
public function save(Model $model): bool
{
// TODO: Implement save() method.
}
}

Some files were not shown because too many files have changed in this diff Show More