From 2d4f48f3a9207f6a2c207ec53e08a2063d4f51c8 Mon Sep 17 00:00:00 2001 From: Aldarien Date: Tue, 10 Aug 2021 15:48:33 -0400 Subject: [PATCH] API --- api/.gitignore | 3 + api/PHP.Dockerfile | 5 ++ api/common/Controller/Base.php | 14 +++++ api/common/Controller/Operadores.php | 52 ++++++++++++++++ api/common/Controller/Proyectos.php | 38 ++++++++++++ api/common/Controller/Ventas.php | 31 ++++++++++ api/common/Define/Controller/Json.php | 13 ++++ api/common/Middleware/Cors.php | 24 ++++++++ api/composer.json | 28 +++++++++ api/nginx.conf | 27 +++++++++ api/phpunit.xml | 26 ++++++++ api/public/index.php | 7 +++ api/resources/routes/02_operadores.php | 13 ++++ api/resources/routes/02_proyectos.php | 12 ++++ api/resources/routes/02_ventas.php | 12 ++++ api/resources/routes/99_base.php | 4 ++ api/setup/app.php | 44 ++++++++++++++ api/setup/composer.php | 6 ++ api/setup/databases.php | 30 +++++++++ api/setup/middlewares/01_base.php | 3 + api/setup/middlewares/02_error.php | 2 + api/setup/router.php | 9 +++ api/setup/settings/01_env.php | 34 +++++++++++ api/src/Agente.php | 16 +++++ api/src/AgenteTipo.php | 30 +++++++++ api/src/EstadoVenta.php | 23 +++++++ api/src/FacturaOperador.php | 24 ++++++++ api/src/FacturaUnidad.php | 29 +++++++++ api/src/Inmobiliaria.php | 8 +++ api/src/Operador.php | 84 ++++++++++++++++++++++++++ api/src/Propiedad.php | 16 +++++ api/src/PropiedadUnidad.php | 16 +++++ api/src/Proyecto.php | 49 +++++++++++++++ api/src/ProyectoAgente.php | 30 +++++++++ api/src/ProyectoTipoUnidad.php | 16 +++++ api/src/TipoAgente.php | 16 +++++ api/src/TipoEstadoVenta.php | 8 +++ api/src/Unidad.php | 16 +++++ api/src/Venta.php | 46 ++++++++++++++ 39 files changed, 864 insertions(+) create mode 100644 api/.gitignore create mode 100644 api/PHP.Dockerfile create mode 100644 api/common/Controller/Base.php create mode 100644 api/common/Controller/Operadores.php create mode 100644 api/common/Controller/Proyectos.php create mode 100644 api/common/Controller/Ventas.php create mode 100644 api/common/Define/Controller/Json.php create mode 100644 api/common/Middleware/Cors.php create mode 100644 api/composer.json create mode 100644 api/nginx.conf create mode 100644 api/phpunit.xml create mode 100644 api/public/index.php create mode 100644 api/resources/routes/02_operadores.php create mode 100644 api/resources/routes/02_proyectos.php create mode 100644 api/resources/routes/02_ventas.php create mode 100644 api/resources/routes/99_base.php create mode 100644 api/setup/app.php create mode 100644 api/setup/composer.php create mode 100644 api/setup/databases.php create mode 100644 api/setup/middlewares/01_base.php create mode 100644 api/setup/middlewares/02_error.php create mode 100644 api/setup/router.php create mode 100644 api/setup/settings/01_env.php create mode 100644 api/src/Agente.php create mode 100644 api/src/AgenteTipo.php create mode 100644 api/src/EstadoVenta.php create mode 100644 api/src/FacturaOperador.php create mode 100644 api/src/FacturaUnidad.php create mode 100644 api/src/Inmobiliaria.php create mode 100644 api/src/Operador.php create mode 100644 api/src/Propiedad.php create mode 100644 api/src/PropiedadUnidad.php create mode 100644 api/src/Proyecto.php create mode 100644 api/src/ProyectoAgente.php create mode 100644 api/src/ProyectoTipoUnidad.php create mode 100644 api/src/TipoAgente.php create mode 100644 api/src/TipoEstadoVenta.php create mode 100644 api/src/Unidad.php create mode 100644 api/src/Venta.php diff --git a/api/.gitignore b/api/.gitignore new file mode 100644 index 0000000..351813b --- /dev/null +++ b/api/.gitignore @@ -0,0 +1,3 @@ +# Composer +**/vendor/ +**/composer.lock diff --git a/api/PHP.Dockerfile b/api/PHP.Dockerfile new file mode 100644 index 0000000..cabee9b --- /dev/null +++ b/api/PHP.Dockerfile @@ -0,0 +1,5 @@ +FROM php:7.4-fpm + +RUN docker-php-ext-install pdo pdo_mysql + +WORKDIR /app diff --git a/api/common/Controller/Base.php b/api/common/Controller/Base.php new file mode 100644 index 0000000..d34d823 --- /dev/null +++ b/api/common/Controller/Base.php @@ -0,0 +1,14 @@ +withJson($response, []); + } +} diff --git a/api/common/Controller/Operadores.php b/api/common/Controller/Operadores.php new file mode 100644 index 0000000..c6659b5 --- /dev/null +++ b/api/common/Controller/Operadores.php @@ -0,0 +1,52 @@ +where('descripcion', 'operador')->find_one(); + $ids = array_map(function($item) { + return $item->agente; + }, $tipo->agente_tipos()); + $operadores = []; + foreach ($ids as $id) { + $operadores []= Model::factory(Operador::class)->find_one($id); + } + usort($operadores, function($a, $b) { + return strcmp($a->abreviacion, $b->abreviacion); + }); + array_walk($operadores, function(&$item) { + $item = $item->as_array(); + }); + $output = compact('operadores'); + return $this->withJson($response, $output); + } + public function show(Request $request, Response $response, $id_operador): Response { + $operador = Model::factory(Operador::class)->find_one($id_operador); + $output = ['operador' => $operador->as_array()]; + return $this->withJson($response, $output); + } + public function add(Request $request, Response $response): Response { + $post = $request->getParsedBody(); + $output = Operador::add($post); + return $this->withJson($response, $output); + } + public function ventas(Request $request, Response $response, $id_operador): Response { + $operador = Model::factory(Operador::class)->find_one($id_operador); + $output = [ + 'operador' => $operador->as_array(), + 'ventas' => array_map(function($item) { + return $item->as_array(); + }, $operador->ventas()) + ]; + return $this->withJson($response, $output); + } +} diff --git a/api/common/Controller/Proyectos.php b/api/common/Controller/Proyectos.php new file mode 100644 index 0000000..937994d --- /dev/null +++ b/api/common/Controller/Proyectos.php @@ -0,0 +1,38 @@ +order_by_asc('descripcion')->find_many(); + array_walk($proyectos, function(&$item) { + $item = $item->as_array(); + }); + $output = compact('proyectos'); + return $this->withJson($response, $output); + } + public function show(Request $request, Response $response, $id_proyecto): Response { + $proyecto = Model::factory(Proyecto::class)->find_one($id_proyecto); + $output = ['proyecto' => $proyecto->as_array()]; + return $this->withJson($response, $output); + } + public function ventas(Request $request, Response $response, $id_proyecto): Response { + $proyecto = Model::factory(Proyecto::class)->find_one($id_proyecto); + $output = [ + 'proyecto' => $proyecto->as_array(), + 'ventas' => array_map(function($item) { + if ($item) { + return $item->as_array(); + } + }, $proyecto->ventas()) + ]; + return $this->withJson($response, $output); + } +} diff --git a/api/common/Controller/Ventas.php b/api/common/Controller/Ventas.php new file mode 100644 index 0000000..49aad16 --- /dev/null +++ b/api/common/Controller/Ventas.php @@ -0,0 +1,31 @@ +find_array(); + $output = compact('ventas'); + return $this->withJson($response, $output); + } + public function show(Request $request, Response $response, $id_venta): Response { + $venta = Model::factory(Venta::class)->find_one($id_venta); + $output = ['venta' => $venta->as_array()]; + return $this->withJson($response, $output); + } + public function operador(Request $request, Response $response, $id_venta): Response { + $venta = Model::factory(Venta::class)->find_one($id_venta); + $output = [ + 'venta' => $venta->as_array(), + 'operador' => $venta->operador() ? $venta->operador()->as_array() : null + ]; + return $this->withJson($response, $output); + } +} diff --git a/api/common/Define/Controller/Json.php b/api/common/Define/Controller/Json.php new file mode 100644 index 0000000..1fb9311 --- /dev/null +++ b/api/common/Define/Controller/Json.php @@ -0,0 +1,13 @@ +getBody()->write(json_encode($data)); + return $response + ->withHeader('Content-Type', 'application/json') + ->withStatus($status); + } +} diff --git a/api/common/Middleware/Cors.php b/api/common/Middleware/Cors.php new file mode 100644 index 0000000..b845d36 --- /dev/null +++ b/api/common/Middleware/Cors.php @@ -0,0 +1,24 @@ +getRoutingResults(); + $methods = $routingResults->getAllowedMethods(); + $requestHeaders = $request->getHeaderLine('Access-Control-Request-Headers'); + + $response = $handler->handle($request); + + $response = $response->withHeader('Access-Control-Allow-Origin', '*'); + $response = $response->withHeader('Access-Control-Allow-Methods', implode(',', $methods)); + $response = $response->withHeader('Access-Control-Allow-Headers', $requestHeaders); + + return $response; + } +} diff --git a/api/composer.json b/api/composer.json new file mode 100644 index 0000000..d592361 --- /dev/null +++ b/api/composer.json @@ -0,0 +1,28 @@ +{ + "name": "incoviba/operadores", + "description": "Modulo de Operadores para Incoviba", + "type": "project", + "require": { + "php-di/slim-bridge": "^3.1", + "nyholm/psr7": "^1.4", + "nyholm/psr7-server": "^1.0", + "zeuxisoo/slim-whoops": "^0.7.3", + "j4mie/paris": "^1.5" + }, + "require-dev": { + "phpunit/phpunit": "^9.5", + "kint-php/kint": "^3.3" + }, + "autoload": { + "psr-4": { + "Incoviba\\Common\\": "common/", + "Incoviba\\": "src/" + } + }, + "authors": [ + { + "name": "Aldarien", + "email": "aldarien85@gmail.com" + } + ] +} diff --git a/api/nginx.conf b/api/nginx.conf new file mode 100644 index 0000000..9f1a5ca --- /dev/null +++ b/api/nginx.conf @@ -0,0 +1,27 @@ +server { + listen 80; + index index.php index.html index.htm; + # server_name php-docker.local; + # error_log /var/log/nginx/error.log; + # access_log /var/log/nginx/access.log; + root /app/public; + + location / { + try_files $uri $uri/ /index.php?$query_string; + + add_header 'Access-Control-Allow-Origin' '*' always; + add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH' always; + add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always; + add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always; + } + + location ~ \.php$ { + try_files $uri =404; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_pass backend:9000; + fastcgi_index index.php; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + } +} diff --git a/api/phpunit.xml b/api/phpunit.xml new file mode 100644 index 0000000..2414c3f --- /dev/null +++ b/api/phpunit.xml @@ -0,0 +1,26 @@ + + + + + tests + + + + + + src + + + diff --git a/api/public/index.php b/api/public/index.php new file mode 100644 index 0000000..e6d2557 --- /dev/null +++ b/api/public/index.php @@ -0,0 +1,7 @@ +run(); diff --git a/api/resources/routes/02_operadores.php b/api/resources/routes/02_operadores.php new file mode 100644 index 0000000..6214116 --- /dev/null +++ b/api/resources/routes/02_operadores.php @@ -0,0 +1,13 @@ +group('/operadores', function($app) { + $app->post('/add[/]', [Operadores::class, 'add']); + $app->get('[/]', Operadores::class); +}); +$app->group('/operador/{id_operador}', function($app) { + $app->group('/ventas', function($app) { + $app->get('[/]', [Operadores::class, 'ventas']); + }); + $app->get('[/]', [Operadores::class, 'show']); +}); diff --git a/api/resources/routes/02_proyectos.php b/api/resources/routes/02_proyectos.php new file mode 100644 index 0000000..298c190 --- /dev/null +++ b/api/resources/routes/02_proyectos.php @@ -0,0 +1,12 @@ +group('/proyectos', function($app) { + $app->get('[/]', Proyectos::class); +}); +$app->group('/proyecto/{id_proyecto}', function($app) { + $app->group('/ventas', function($app) { + $app->get('[/]', [Proyectos::class, 'ventas']); + }); + $app->get('[/]', [Proyectos::class, 'show']); +}); diff --git a/api/resources/routes/02_ventas.php b/api/resources/routes/02_ventas.php new file mode 100644 index 0000000..7dab3b6 --- /dev/null +++ b/api/resources/routes/02_ventas.php @@ -0,0 +1,12 @@ +group('/ventas', function($app) { + $app->get('[/]', Ventas::class); +}); +$app->group('/venta/{id_venta}', function($app) { + $app->group('/operador', function($app) { + $app->get('[/]', [Ventas::class, 'operador']); + }); + $app->get('[/]', [Ventas::class, 'show']); +}); diff --git a/api/resources/routes/99_base.php b/api/resources/routes/99_base.php new file mode 100644 index 0000000..66c3d0c --- /dev/null +++ b/api/resources/routes/99_base.php @@ -0,0 +1,4 @@ +get('[/]', Base::class); diff --git a/api/setup/app.php b/api/setup/app.php new file mode 100644 index 0000000..42236eb --- /dev/null +++ b/api/setup/app.php @@ -0,0 +1,44 @@ +isDir()) { + continue; + } + $builder->addDefinitions($file->getRealPath()); + } +} + +$container = $builder->build(); +#!d($container->getKnownEntryNames()); +$app = Bridge::create($container); + +$app->addBodyParsingMiddleware(); + +$folder = implode(DIRECTORY_SEPARATOR, [__DIR__, 'middlewares']); +if (file_exists($folder)) { + $files = new DirectoryIterator($folder); + foreach ($files as $file) { + if ($file->isDir()) { + continue; + } + include_once $file->getRealPath(); + } +} + +include_once 'databases.php'; +include_once 'router.php'; diff --git a/api/setup/composer.php b/api/setup/composer.php new file mode 100644 index 0000000..c8fb135 --- /dev/null +++ b/api/setup/composer.php @@ -0,0 +1,6 @@ +getContainer()->get('databases'); +foreach ($databases->databases as $name => $settings) { + switch ($settings->engine) { + case 'mysql': { + $dsn = ["mysql:host={$settings->host->name}"]; + if (isset($settings->host->port)) { + $dsn []= "port={$settings->host->port}"; + } + $dsn []= "dbname={$settings->name}"; + $dsn = implode(';', $dsn); + $config = [ + 'connection_string' => $dsn, + 'username' => $settings->user->name, + 'password' => $settings->user->password + ]; + Model::configure($config, null, $name); + Model::set_db(new PDO($dsn, $settings->user->name, $settings->user->password), $name); + break; + } + case 'sqlite': { + $dsn = "sqlite:{$settings->database}"; + Model::configure($dsn, null, $name); + break; + } + } +} +if (isset($databases->short_names) and $databases->short_names) { + Model::$short_table_names = true; +} diff --git a/api/setup/middlewares/01_base.php b/api/setup/middlewares/01_base.php new file mode 100644 index 0000000..c1b6d44 --- /dev/null +++ b/api/setup/middlewares/01_base.php @@ -0,0 +1,3 @@ +add(new Incoviba\Common\Middleware\Cors()); +$app->addRoutingMiddleware(); diff --git a/api/setup/middlewares/02_error.php b/api/setup/middlewares/02_error.php new file mode 100644 index 0000000..94c3ce7 --- /dev/null +++ b/api/setup/middlewares/02_error.php @@ -0,0 +1,2 @@ +add(new Zeuxisoo\Whoops\Slim\WhoopsMiddleware()); diff --git a/api/setup/router.php b/api/setup/router.php new file mode 100644 index 0000000..22be408 --- /dev/null +++ b/api/setup/router.php @@ -0,0 +1,9 @@ +getContainer()->get('folders')->routes; +$files = new DirectoryIterator($folder); +foreach ($files as $file) { + if ($file->isDir() or $file->getExtension() !== 'php') { + continue; + } + include_once $file->getRealPath(); +} diff --git a/api/setup/settings/01_env.php b/api/setup/settings/01_env.php new file mode 100644 index 0000000..9c8d337 --- /dev/null +++ b/api/setup/settings/01_env.php @@ -0,0 +1,34 @@ + function() { + $arr = [ + 'databases' => [ + ORM::DEFAULT_CONNECTION => (object) [ + 'engine' => 'mysql', + 'name' => $_ENV['MYSQL_DATABASE'], + 'host' => (object) [ + 'name' => 'db' + ], + 'user' => (object) [ + 'name' => $_ENV['MYSQL_USER'], + 'password' => $_ENV['MYSQL_PASSWORD'] + ] + ] + ], + 'short_names' => true + ]; + return (object) $arr; + }, + 'folders' => function() { + $arr = ['base' => dirname(__DIR__, 2)]; + $arr['resources'] = implode(DIRECTORY_SEPARATOR, [ + $arr['base'], + 'resources' + ]); + $arr['routes'] = implode(DIRECTORY_SEPARATOR, [ + $arr['resources'], + 'routes' + ]); + return (object) $arr; + } +]; diff --git a/api/src/Agente.php b/api/src/Agente.php new file mode 100644 index 0000000..b06d918 --- /dev/null +++ b/api/src/Agente.php @@ -0,0 +1,16 @@ +agente_tipos === null) { + $this->agente_tipos = $this->has_many(AgenteTipo::class, 'agente')->find_many(); + } + return $this->agente_tipos; + } +} diff --git a/api/src/AgenteTipo.php b/api/src/AgenteTipo.php new file mode 100644 index 0000000..cd3f6cc --- /dev/null +++ b/api/src/AgenteTipo.php @@ -0,0 +1,30 @@ +proyecto_agentes === null) { + $this->proyecto_agentes = $this->has_many(ProyectoAgente::class, 'agente')->find_many(); + } + return $this->proyecto_agentes; + } + protected $agente_obj; + public function agente() { + if ($this->agente_obj === null) { + $this->agente_obj = $this->belongs_to(Agente::class, 'agente')->find_one(); + } + return $this->agente_obj; + } + protected $tipo_obj; + public function tipo() { + if ($this->tipo_obj === null) { + $this->tipo_obj = $this->belongs_to(TipoAgente::class, 'tipo')->find_one(); + } + return $this->tipo_obj; + } +} diff --git a/api/src/EstadoVenta.php b/api/src/EstadoVenta.php new file mode 100644 index 0000000..00c05ec --- /dev/null +++ b/api/src/EstadoVenta.php @@ -0,0 +1,23 @@ +tipo === null) { + $this->tipo = $this->belongs_to(TipoEstadoVenta::class, 'estado')->find_one(); + } + return $this->tipo; + } + protected $venta_obj; + public function venta() { + if ($this->venta_obj === null) { + $this->venta_obj = $this->belongs_to(Venta::class, 'venta')->find_one(); + } + return $this->venta_obj; + } +} diff --git a/api/src/FacturaOperador.php b/api/src/FacturaOperador.php new file mode 100644 index 0000000..591b590 --- /dev/null +++ b/api/src/FacturaOperador.php @@ -0,0 +1,24 @@ +operador === null) { + $this->operador = $this->belongs_to(Operador::class, 'operador_id')->find_one(); + } + return $this->operador; + } +} diff --git a/api/src/FacturaUnidad.php b/api/src/FacturaUnidad.php new file mode 100644 index 0000000..a8cfd17 --- /dev/null +++ b/api/src/FacturaUnidad.php @@ -0,0 +1,29 @@ +factura === null) { + $this->factura = $this->belongs_to(FacturaOperador::class, 'factura_id')->find_one(); + } + return $this->factura; + } + protected $unidad; + public function unidad() { + if ($this->unidad === null) { + $this->unidad = $this->belongs_to(Unidad::class, 'unidad_id')->find_one(); + } + return $this->unidad; + } +} diff --git a/api/src/Inmobiliaria.php b/api/src/Inmobiliaria.php new file mode 100644 index 0000000..9040baa --- /dev/null +++ b/api/src/Inmobiliaria.php @@ -0,0 +1,8 @@ +ventas === null) { + $ventas = []; + foreach ($this->agente_tipos() as $at) { + foreach ($at->proyecto_agentes() as $pa) { + $ventas = array_merge($ventas, $pa->ventas()); + } + } + $this->ventas = $ventas; + } + return $this->ventas; + } + protected $comisiones; + public function comisiones() { + if ($this->comisiones === null) { + $comisiones = []; + foreach ($this->agente_tipos() as $at) { + foreach ($at->proyecto_agentes() as $pa) { + $comisiones []= (object) ['proyecto' => (object) $pa->proyecto()->as_array(), 'comision' => $pa->comision]; + } + } + $this->comisiones = $comisiones; + } + return $this->comisiones; + } + + public static function add($data) { + $fields = [ + 'rut', + 'descripcion', + 'abreviacion', + 'representante', + 'telefono', + 'correo' + ]; + $input = array_intersect_key($data, array_combine($fields, $fields)); + $tipo = Model::factory(TipoAgente::class)->where('descripcion', 'operador')->find_one(); + $input['tipo'] = $tipo->id; + $validate = [ + 'rut', + 'descripcion', + 'abreviacion' + ]; + $found = false; + foreach ($validate as $val) { + $operador = Model::factory(Operador::class)->where($val, $input[$val])->find_one(); + if ($operador) { + $found = true; + break; + } + } + $created = false; + if (!$found) { + $operador = Operador::create($input); + $created = $operador->save(); + $input = [ + 'agente' => $operador->id, + 'tipo' => $tipo->id + ]; + $at = AgenteTipo::create($input); + $at->save(); + } + $output = [ + 'input' => $data, + 'operador' => $operador->as_array(), + 'new' => $found, + 'created' => $created + ]; + return $output; + } + + public function as_array() { + $arr = parent::as_array(); + $arr['comisiones'] = $this->comisiones(); + return $arr; + } +} diff --git a/api/src/Propiedad.php b/api/src/Propiedad.php new file mode 100644 index 0000000..7c1c1b6 --- /dev/null +++ b/api/src/Propiedad.php @@ -0,0 +1,16 @@ +venta === null) { + $this->venta = $this->has_one(Venta::class, 'propiedad')->find_one(); + } + return $this->venta; + } +} diff --git a/api/src/PropiedadUnidad.php b/api/src/PropiedadUnidad.php new file mode 100644 index 0000000..cdfe646 --- /dev/null +++ b/api/src/PropiedadUnidad.php @@ -0,0 +1,16 @@ +propiedad_obj === null) { + $this->propiedad_obj = $this->belongs_to(Propiedad::class, 'propiedad')->find_one(); + } + return $this->propiedad_obj; + } +} diff --git a/api/src/Proyecto.php b/api/src/Proyecto.php new file mode 100644 index 0000000..24c41e7 --- /dev/null +++ b/api/src/Proyecto.php @@ -0,0 +1,49 @@ +inmobiliaria_obj === null) { + $this->inmobiliaria_obj = $this->belongs_to(Inmobiliaria::class, 'inmobiliaria', 'rut')->find_one(); + } + return $this->inmobiliaria_obj; + } + + protected $ventas; + public function ventas() { + if ($this->ventas === null) { + $ventas = []; + foreach ($this->proyecto_tipo_unidades() as $pu) { + foreach ($pu->unidades() as $u) { + if (!$u->propiedad_unidad() or $u->propiedad_unidad()->principal != 1 or !$u->propiedad_unidad()->propiedad() or !$u->propiedad_unidad()->propiedad()->venta()) { + continue; + } + $ventas = array_merge($ventas, [$u->propiedad_unidad()->propiedad()->venta()]); + } + } + $ventas = array_filter($ventas, function($item) { + return ($item->activa()); + }); + $this->ventas = $ventas; + } + return $this->ventas; + } + protected $proyecto_tipo_unidades; + public function proyecto_tipo_unidades() { + if ($this->proyecto_tipo_unidades === null) { + $this->proyecto_tipo_unidades = $this->has_many(ProyectoTipoUnidad::class, 'proyecto')->find_many(); + } + return $this->proyecto_tipo_unidades; + } + + public function as_array() { + $arr = parent::as_array(); + $arr['inmobiliaria'] = $this->inmobiliaria()->as_array(); + return $arr; + } +} diff --git a/api/src/ProyectoAgente.php b/api/src/ProyectoAgente.php new file mode 100644 index 0000000..7c00983 --- /dev/null +++ b/api/src/ProyectoAgente.php @@ -0,0 +1,30 @@ +ventas === null) { + $this->ventas = $this->has_many(Venta::class, 'agente')->find_many(); + } + return $this->ventas; + } + protected $agente_tipo; + public function agente_tipo() { + if ($this->agente_tipo === null) { + $this->agente_tipo = $this->belongs_to(AgenteTipo::class, 'agente')->find_one(); + } + return $this->agente_tipo; + } + protected $proyecto_obj; + public function proyecto() { + if ($this->proyecto_obj === null) { + $this->proyecto_obj = $this->belongs_to(Proyecto::class, 'proyecto')->find_one(); + } + return $this->proyecto_obj; + } +} diff --git a/api/src/ProyectoTipoUnidad.php b/api/src/ProyectoTipoUnidad.php new file mode 100644 index 0000000..019f56d --- /dev/null +++ b/api/src/ProyectoTipoUnidad.php @@ -0,0 +1,16 @@ +unidades === null) { + $this->unidades = $this->has_many(Unidad::class, 'pt')->find_many(); + } + return $this->unidades; + } +} diff --git a/api/src/TipoAgente.php b/api/src/TipoAgente.php new file mode 100644 index 0000000..2e56ed8 --- /dev/null +++ b/api/src/TipoAgente.php @@ -0,0 +1,16 @@ +agente_tipos === null) { + $this->agente_tipos = $this->has_many(AgenteTipo::class, 'tipo')->find_many(); + } + return $this->agente_tipos; + } +} diff --git a/api/src/TipoEstadoVenta.php b/api/src/TipoEstadoVenta.php new file mode 100644 index 0000000..ef4623c --- /dev/null +++ b/api/src/TipoEstadoVenta.php @@ -0,0 +1,8 @@ +propiedad_unidad === null) { + $this->propiedad_unidad = $this->has_one(PropiedadUnidad::class, 'unidad')->find_one(); + } + return $this->propiedad_unidad; + } +} diff --git a/api/src/Venta.php b/api/src/Venta.php new file mode 100644 index 0000000..6556631 --- /dev/null +++ b/api/src/Venta.php @@ -0,0 +1,46 @@ +operador === null) { + $pa = $this->proyecto_agente(); + if ($pa) { + $id = $this->proyecto_agente()->agente_tipo()->agente()->id; + $this->operador = Model::factory(Operador::class)->find_one($id); + } + } + return $this->operador; + } + protected $proyecto_agente; + public function proyecto_agente() { + if ($this->proyecto_agente === null) { + $this->proyecto_agente = $this->belongs_to(ProyectoAgente::class, 'agente')->find_one(); + } + return $this->proyecto_agente; + } + protected $estados; + public function estados() { + if ($this->estados === null) { + $this->estados = $this->has_many(EstadoVenta::class, 'venta')->order_by_asc('fecha', 'id')->find_many(); + } + return $this->estados; + } + protected $activa; + public function activa() { + if ($this->activa === null) { + $this->activa = false; + foreach ($this->estados() as $estado) { + if ($estado->tipo()->activa == 1) { + $this->activa = true; + } + } + } + return $this->activa; + } +}