From 59825259b6e179585f12fb44dc8d1af83e7c6803 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Tue, 8 Aug 2023 23:53:49 -0400 Subject: [PATCH] Implemented repository mapper, and venta show --- app/common/Define/Repository/Factory.php | 9 + app/common/Define/Repository/Mapper.php | 15 ++ app/common/Define/Repository/MapperParser.php | 10 + app/common/Ideal/Model.php | 13 + app/common/Ideal/Repository.php | 57 +++-- app/common/Implement/Repository/Factory.php | 27 +++ app/common/Implement/Repository/Mapper.php | 76 ++++++ .../Implement/Repository/Mapper/Boolean.php | 17 ++ .../Implement/Repository/Mapper/DateTime.php | 18 ++ .../Implement/Repository/MapperParser.php | 41 ++++ app/resources/routes/04_ventas.php | 13 + app/resources/routes/api/direcciones.php | 11 + app/resources/routes/api/ventas.php | 4 + app/resources/routes/api/ventas/pagos.php | 7 + app/resources/routes/ventas/propietarios.php | 6 + app/resources/views/ventas/edit.blade.php | 94 +++++++ app/resources/views/ventas/list.blade.php | 29 ++- .../views/ventas/propiedades/edit.blade.php | 104 ++++++++ .../views/ventas/propietarios/edit.blade.php | 226 +++++++++++++++++ .../views/ventas/propietarios/show.blade.php | 7 + app/resources/views/ventas/show.blade.php | 40 +++ .../views/ventas/show/comentarios.blade.php | 88 +++++++ .../views/ventas/show/detalle.blade.php | 38 +++ .../views/ventas/show/entrega.blade.php | 11 + .../views/ventas/show/escritura.blade.php | 22 ++ .../views/ventas/show/forma_pago.blade.php | 229 ++++++++++++++++++ .../ventas/show/forma_pago/anticipo.blade.php | 7 + .../ventas/show/forma_pago/bono_pie.blade.php | 22 ++ .../ventas/show/forma_pago/credito.blade.php | 40 +++ .../show/forma_pago/devolucion.blade.php | 0 .../show/forma_pago/escritura.blade.php | 34 +++ .../ventas/show/forma_pago/pie.blade.php | 31 +++ .../ventas/show/forma_pago/subsidio.blade.php | 32 +++ .../ventas/show/forma_pago/total.blade.php | 28 +++ .../views/ventas/show/propiedad.blade.php | 56 +++++ .../views/ventas/show/propietario.blade.php | 15 ++ app/src/Controller/Direcciones.php | 39 +++ app/src/Controller/Proyectos.php | 10 +- app/src/Controller/Ventas.php | 85 ++++++- app/src/Controller/Ventas/Cierres.php | 14 +- app/src/Controller/Ventas/Comentarios.php | 22 ++ app/src/Controller/Ventas/Cuotas.php | 10 +- app/src/Controller/Ventas/Pagos.php | 26 ++ app/src/Controller/Ventas/Precios.php | 16 +- app/src/Controller/Ventas/Propietarios.php | 15 ++ app/src/Model/Comuna.php | 2 +- app/src/Model/Direccion.php | 10 +- app/src/Model/Proyecto.php | 23 +- app/src/Model/Proyecto/Elemento.php | 20 ++ app/src/Model/Proyecto/ProyectoTipoUnidad.php | 22 +- app/src/Model/Proyecto/TipoTipologia.php | 8 + app/src/Model/Proyecto/Tipologia.php | 20 ++ app/src/Model/Venta.php | 87 ++++++- app/src/Model/Venta/BonoPie.php | 10 + app/src/Model/Venta/Comentario.php | 21 ++ app/src/Model/Venta/Credito.php | 11 +- app/src/Model/Venta/Escritura.php | 14 +- app/src/Model/Venta/FormaPago.php | 53 +++- app/src/Model/Venta/Pago.php | 11 + app/src/Model/Venta/Pie.php | 18 ++ app/src/Model/Venta/Propiedad.php | 8 + app/src/Model/Venta/Unidad.php | 28 +++ app/src/Repository/Banco.php | 5 +- app/src/Repository/Comuna.php | 21 +- app/src/Repository/Direccion.php | 19 +- app/src/Repository/Inmobiliaria.php | 26 +- .../Repository/Inmobiliaria/TipoSociedad.php | 6 +- app/src/Repository/Login.php | 28 +-- app/src/Repository/Provincia.php | 12 +- app/src/Repository/Proyecto.php | 47 ++-- app/src/Repository/Proyecto/Elemento.php | 34 +++ .../Proyecto/ProyectoTipoUnidad.php | 30 +-- app/src/Repository/Proyecto/TipoTipologia.php | 34 +++ app/src/Repository/Proyecto/Tipologia.php | 58 +++++ app/src/Repository/Region.php | 7 +- app/src/Repository/User.php | 12 +- app/src/Repository/Venta.php | 200 ++++++++------- app/src/Repository/Venta/BonoPie.php | 11 +- app/src/Repository/Venta/Cierre.php | 34 +-- app/src/Repository/Venta/Comentario.php | 43 ++++ app/src/Repository/Venta/Credito.php | 16 +- app/src/Repository/Venta/Cuota.php | 95 +++----- app/src/Repository/Venta/Entrega.php | 5 +- app/src/Repository/Venta/Escritura.php | 20 +- app/src/Repository/Venta/EstadoCierre.php | 26 +- app/src/Repository/Venta/EstadoPago.php | 34 +-- app/src/Repository/Venta/EstadoPrecio.php | 26 +- app/src/Repository/Venta/EstadoVenta.php | 26 +- app/src/Repository/Venta/Pago.php | 41 ++-- app/src/Repository/Venta/Pie.php | 28 +-- app/src/Repository/Venta/Precio.php | 24 +- app/src/Repository/Venta/Propiedad.php | 24 +- app/src/Repository/Venta/Propietario.php | 41 ++-- app/src/Repository/Venta/Subsidio.php | 3 +- app/src/Repository/Venta/TipoEstadoCierre.php | 11 +- app/src/Repository/Venta/TipoEstadoPago.php | 4 +- app/src/Repository/Venta/TipoEstadoPrecio.php | 5 +- app/src/Repository/Venta/TipoEstadoVenta.php | 11 +- app/src/Repository/Venta/TipoPago.php | 5 +- app/src/Repository/Venta/TipoUnidad.php | 7 +- app/src/Repository/Venta/TipoValorCierre.php | 5 +- app/src/Repository/Venta/Unidad.php | 47 ++-- app/src/Repository/Venta/ValorCierre.php | 21 +- app/src/Service/Format.php | 10 +- .../Service/Proyecto/ProyectoTipoUnidad.php | 22 ++ app/src/Service/Venta.php | 19 ++ app/src/Service/{Ventas => Venta}/Cierre.php | 2 +- app/src/Service/{Ventas => Venta}/Pago.php | 9 +- app/src/Service/Venta/Pie.php | 20 ++ app/src/Service/{Ventas => Venta}/Precio.php | 15 +- app/src/Service/Venta/Unidad.php | 49 ++++ 111 files changed, 2766 insertions(+), 612 deletions(-) create mode 100644 app/common/Define/Repository/Factory.php create mode 100644 app/common/Define/Repository/Mapper.php create mode 100644 app/common/Define/Repository/MapperParser.php create mode 100644 app/common/Implement/Repository/Factory.php create mode 100644 app/common/Implement/Repository/Mapper.php create mode 100644 app/common/Implement/Repository/Mapper/Boolean.php create mode 100644 app/common/Implement/Repository/Mapper/DateTime.php create mode 100644 app/common/Implement/Repository/MapperParser.php create mode 100644 app/resources/routes/api/direcciones.php create mode 100644 app/resources/routes/api/ventas/pagos.php create mode 100644 app/resources/routes/ventas/propietarios.php create mode 100644 app/resources/views/ventas/edit.blade.php create mode 100644 app/resources/views/ventas/propiedades/edit.blade.php create mode 100644 app/resources/views/ventas/propietarios/edit.blade.php create mode 100644 app/resources/views/ventas/propietarios/show.blade.php create mode 100644 app/resources/views/ventas/show.blade.php create mode 100644 app/resources/views/ventas/show/comentarios.blade.php create mode 100644 app/resources/views/ventas/show/detalle.blade.php create mode 100644 app/resources/views/ventas/show/entrega.blade.php create mode 100644 app/resources/views/ventas/show/escritura.blade.php create mode 100644 app/resources/views/ventas/show/forma_pago.blade.php create mode 100644 app/resources/views/ventas/show/forma_pago/anticipo.blade.php create mode 100644 app/resources/views/ventas/show/forma_pago/bono_pie.blade.php create mode 100644 app/resources/views/ventas/show/forma_pago/credito.blade.php create mode 100644 app/resources/views/ventas/show/forma_pago/devolucion.blade.php create mode 100644 app/resources/views/ventas/show/forma_pago/escritura.blade.php create mode 100644 app/resources/views/ventas/show/forma_pago/pie.blade.php create mode 100644 app/resources/views/ventas/show/forma_pago/subsidio.blade.php create mode 100644 app/resources/views/ventas/show/forma_pago/total.blade.php create mode 100644 app/resources/views/ventas/show/propiedad.blade.php create mode 100644 app/resources/views/ventas/show/propietario.blade.php create mode 100644 app/src/Controller/Direcciones.php create mode 100644 app/src/Controller/Ventas/Comentarios.php create mode 100644 app/src/Controller/Ventas/Pagos.php create mode 100644 app/src/Controller/Ventas/Propietarios.php create mode 100644 app/src/Model/Proyecto/Elemento.php create mode 100644 app/src/Model/Proyecto/TipoTipologia.php create mode 100644 app/src/Model/Proyecto/Tipologia.php create mode 100644 app/src/Model/Venta/Comentario.php create mode 100644 app/src/Repository/Proyecto/Elemento.php create mode 100644 app/src/Repository/Proyecto/TipoTipologia.php create mode 100644 app/src/Repository/Proyecto/Tipologia.php create mode 100644 app/src/Repository/Venta/Comentario.php create mode 100644 app/src/Service/Proyecto/ProyectoTipoUnidad.php rename app/src/Service/{Ventas => Venta}/Cierre.php (97%) rename app/src/Service/{Ventas => Venta}/Pago.php (72%) create mode 100644 app/src/Service/Venta/Pie.php rename app/src/Service/{Ventas => Venta}/Precio.php (59%) create mode 100644 app/src/Service/Venta/Unidad.php diff --git a/app/common/Define/Repository/Factory.php b/app/common/Define/Repository/Factory.php new file mode 100644 index 0000000..a60afef --- /dev/null +++ b/app/common/Define/Repository/Factory.php @@ -0,0 +1,9 @@ +factories[$property] = $factory; + return $this; + } + + protected function runFactory(string $property): mixed + { + return $this->factories[$property]->run(); + } + public function jsonSerialize(): mixed { return [ diff --git a/app/common/Ideal/Repository.php b/app/common/Ideal/Repository.php index 9b4acc7..daf2139 100644 --- a/app/common/Ideal/Repository.php +++ b/app/common/Ideal/Repository.php @@ -1,10 +1,11 @@ create($data_row); $model->{$this->getKey()} = $data_row[$this->getKey()]; return $model; } - public function remove(Model $model): void + public function remove(Define\Model $model): void { $query = "DELETE FROM `{$this->getTable()}` WHERE `{$this->getKey()}` = ?"; $this->connection->execute($query, [$model->getId()]); @@ -49,26 +50,50 @@ abstract class Repository implements Define\Repository { return 'id'; } - protected function parseData(Define\Model $model, ?array $data, array $data_map): Define\Model + protected function parseData(Define\Model $model, ?array $data, Implement\Repository\MapperParser $data_map): Define\Model { if ($data === null) { return $model; } - foreach ($data_map as $column => $settings) { - if (isset($data[$column])) { - $property = $column; - if (isset($settings['property'])) { - $property = $settings['property']; - } - $value = $data[$column]; - if (isset($settings['function'])) { - $value = $settings['function']($data); - } - $model->{$property} = $value; + foreach ($data_map->getColumns() as $column) { + if (!$data_map->hasMapper($column)) { + $this->parsePlainColumn($model, $column, $data); + continue; } + + $settings = $data_map->getMapper($column); + if ($settings->parse($model, $column, $data)) { + continue; + } + $property = $column; + if ($settings->hasProperty()) { + $property = $settings->property; + } + $this->setDefault($model, $property); } return $model; } + protected function parsePlainColumn(Define\Model &$model, string $column, ?array $data): void + { + $property = $column; + if (isset($data[$column])) { + $model->{$property} = $data[$column]; + return; + } + $this->setDefault($model, $property); + } + protected function setDefault(Define\Model &$model, string $property): void + { + $prop = new ReflectionProperty($model, $property); + $type = $prop->getType()->getName(); + $value = match ($type) { + 'int' => 0, + 'float' => 0.0, + 'string' => '', + default => null, + }; + $model->{$property} = $value; + } protected function saveNew(array $columns, array $values): int { $columns_string = implode(', ', array_map(function($column) {return "`{$column}`";}, $columns)); diff --git a/app/common/Implement/Repository/Factory.php b/app/common/Implement/Repository/Factory.php new file mode 100644 index 0000000..00fd799 --- /dev/null +++ b/app/common/Implement/Repository/Factory.php @@ -0,0 +1,27 @@ +callable = $callable(...); + return $this; + } + public function setArgs(array $args): Define\Repository\Factory + { + $this->args = $args; + return $this; + } + + public function run(): mixed + { + return call_user_func_array($this->callable, $this->args); + } +} diff --git a/app/common/Implement/Repository/Mapper.php b/app/common/Implement/Repository/Mapper.php new file mode 100644 index 0000000..d54d344 --- /dev/null +++ b/app/common/Implement/Repository/Mapper.php @@ -0,0 +1,76 @@ +property = $property; + return $this; + } + public function setFunction(callable $function): Define\Repository\Mapper + { + $this->function = $function(...); + return $this; + } + public function setFactory(Define\Repository\Factory $factory): Define\Repository\Mapper + { + $this->factory = $factory; + return $this; + } + public function setDefault(mixed $value): Define\Repository\Mapper + { + $this->default = $value; + return $this; + } + + public function hasProperty(): bool + { + return isset($this->property); + } + public function hasFunction(): bool + { + return isset($this->function); + } + public function hasFactory(): bool + { + return isset($this->factory); + } + public function hasDefault(): bool + { + return isset($this->default); + } + + public function parse(Define\Model &$model, string $column, ?array $data): bool + { + $property = $column; + if ($this->hasProperty()) { + $property = $this->property; + } + if (isset($data[$column])) { + if ($this->hasFactory()) { + $model->addFactory($property, $this->factory); + return true; + } + $value = $data[$column]; + if ($this->hasFunction()) { + $value = ($this->function)($data); + } + $model->{$property} = $value; + return true; + } + if ($this->hasDefault()) { + $model->{$property} = $this->default; + return true; + } + return false; + } +} diff --git a/app/common/Implement/Repository/Mapper/Boolean.php b/app/common/Implement/Repository/Mapper/Boolean.php new file mode 100644 index 0000000..a0eab27 --- /dev/null +++ b/app/common/Implement/Repository/Mapper/Boolean.php @@ -0,0 +1,17 @@ +setFunction(function($data) use ($column) { + return $data[$column] !== 0; + }); + if ($property !== null) { + $this->setProperty($property); + } + } +} diff --git a/app/common/Implement/Repository/Mapper/DateTime.php b/app/common/Implement/Repository/Mapper/DateTime.php new file mode 100644 index 0000000..85ad2f1 --- /dev/null +++ b/app/common/Implement/Repository/Mapper/DateTime.php @@ -0,0 +1,18 @@ +setFunction(function($data) use ($column) { + return new DateTimeImmutable($data[$column]); + }); + if ($property !== null) { + $this->setProperty($property); + } + } +} diff --git a/app/common/Implement/Repository/MapperParser.php b/app/common/Implement/Repository/MapperParser.php new file mode 100644 index 0000000..d93e078 --- /dev/null +++ b/app/common/Implement/Repository/MapperParser.php @@ -0,0 +1,41 @@ +register($column); + } + } + } + + protected array $maps; + + public function register(string $column, ?Mapper $mapper = null): Define\Repository\MapperParser + { + if ($mapper !== null) { + $this->maps[$column] = $mapper; + return $this; + } + $this->maps[$column] = []; + return $this; + } + public function getColumns(): array + { + return array_keys($this->maps); + } + public function hasMapper(string $column): bool + { + return is_a($this->maps[$column], Define\Repository\Mapper::class); + } + public function getMapper(string $column): Mapper + { + return $this->maps[$column]; + } +} diff --git a/app/resources/routes/04_ventas.php b/app/resources/routes/04_ventas.php index d3eeeea..5e522cf 100644 --- a/app/resources/routes/04_ventas.php +++ b/app/resources/routes/04_ventas.php @@ -11,3 +11,16 @@ $app->group('/ventas', function($app) { } $app->get('[/]', Ventas::class); }); +$app->group('/venta/{proyecto_nombre:[A-za-zÑñ\+\ %0-9]+}/{unidad_descripcion:[0-9]+}', function($app) { + $app->get('[/]', [Ventas::class, 'showUnidad']); +}); +$app->group('/venta/{venta_id:[0-9]+}', function($app) { + $app->group('/propietario', function($app) { + $app->get('[/]', [Ventas::class, 'propietario']); + }); + $app->group('/propiedad', function($app) { + $app->get('[/]', [Ventas::class, 'propiedad']); + }); + $app->get('/edit[/]', [Ventas::class, 'edit']); + $app->get('[/]', [Ventas::class, 'show']); +}); diff --git a/app/resources/routes/api/direcciones.php b/app/resources/routes/api/direcciones.php new file mode 100644 index 0000000..5f2d740 --- /dev/null +++ b/app/resources/routes/api/direcciones.php @@ -0,0 +1,11 @@ +group('/direcciones', function($app) { + $app->group('/region/{region_id:[0-9]+}', function($app) { + $app->get('/comunas[/]', [Direcciones::class, 'comunas']); + }); + $app->group('/comunas', function($app) { + $app->post('/find[/]', [Direcciones::class, 'findComunas']); + }); +}); diff --git a/app/resources/routes/api/ventas.php b/app/resources/routes/api/ventas.php index 90ba335..21edd73 100644 --- a/app/resources/routes/api/ventas.php +++ b/app/resources/routes/api/ventas.php @@ -14,3 +14,7 @@ $app->group('/ventas', function($app) { } $app->post('[/]', [Ventas::class, 'proyecto']); }); +$app->group('/venta/{venta_id}', function($app) { + $app->get('/comentarios', [Ventas::class, 'comentarios']); + $app->get('[/]', [Ventas::class, 'get']); +}); diff --git a/app/resources/routes/api/ventas/pagos.php b/app/resources/routes/api/ventas/pagos.php new file mode 100644 index 0000000..dd33bf3 --- /dev/null +++ b/app/resources/routes/api/ventas/pagos.php @@ -0,0 +1,7 @@ +group('/pago/{pago_id:[0-9]+}', function($app) { + $app->put('/depositar[/]', [Pagos::class, 'depositar']); + $app->put('/abonar[/]', [Pagos::class, 'abonar']); +}); diff --git a/app/resources/routes/ventas/propietarios.php b/app/resources/routes/ventas/propietarios.php new file mode 100644 index 0000000..bd0929f --- /dev/null +++ b/app/resources/routes/ventas/propietarios.php @@ -0,0 +1,6 @@ +group('/propietario/{propietario_rut:[0-9]+}', function($app) { + $app->get('[/]', [Propietarios::class, 'show']); +}); \ No newline at end of file diff --git a/app/resources/views/ventas/edit.blade.php b/app/resources/views/ventas/edit.blade.php new file mode 100644 index 0000000..ebc7387 --- /dev/null +++ b/app/resources/views/ventas/edit.blade.php @@ -0,0 +1,94 @@ +@extends('layout.base') + +@section('page_content') +
+

Editar Venta

+
+
+ +
+ +
UF
+
+
+
+ +
+
+ + +
+
+
+ +
+
+@endsection + +@push('page_scripts') + +@endpush diff --git a/app/resources/views/ventas/list.blade.php b/app/resources/views/ventas/list.blade.php index d92a732..04261be 100644 --- a/app/resources/views/ventas/list.blade.php +++ b/app/resources/views/ventas/list.blade.php @@ -87,6 +87,7 @@ id: 0, proyecto: '', proyectos: JSON.parse('{!! json_encode($proyectos) !!}'), + venta_ids: [], ventas: [] }, loading: { @@ -100,6 +101,7 @@ get: function() { return { ventas: proyecto_id => { + this.data.venta_ids = [] this.data.ventas = [] return fetch('{{$urls->api}}/ventas', {method: 'post', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({proyecto_id})} @@ -110,14 +112,31 @@ } }).then(data => { if (data.total > 0) { - this.data.id = data.ventas[0].propiedad.departamentos[0].proyecto_tipo_unidad.proyecto.id - this.data.proyecto = data.ventas[0].propiedad.departamentos[0].proyecto_tipo_unidad.proyecto.descripcion - data.ventas.forEach(venta => { - this.add().venta(venta) + this.data.id = data.proyecto.id + this.data.proyecto = data.proyecto.descripcion + this.data.venta_ids = data.ventas + const promises = [] + data.ventas.forEach(venta_id => { + promises.push(this.get().venta(venta_id)) + }) + Promise.all(promises).then(() => { + this.draw().ventas() }) - this.draw().ventas() } }) + }, + venta: venta_id => { + return fetch('{{$urls->api}}/venta/' + venta_id).then(response => { + if (response.ok) { + return response.json() + } + }).then(data => { + if (typeof data.venta === 'undefined') { + console.error(venta_id, data.error) + return + } + this.add().venta(data.venta) + }) } } }, diff --git a/app/resources/views/ventas/propiedades/edit.blade.php b/app/resources/views/ventas/propiedades/edit.blade.php new file mode 100644 index 0000000..31ebaa6 --- /dev/null +++ b/app/resources/views/ventas/propiedades/edit.blade.php @@ -0,0 +1,104 @@ +@extends('layout.base') + +@section('page_content') +
+

Editar Propiedad - {{$proyecto->descripcion}} {{$propiedad->summary()}}

+ + + + + + + + + + @foreach($propiedad->unidades as $unidad) + + + + + + @endforeach + +
TipoUnidad + +
{{ucwords($unidad->proyectoTipoUnidad->tipoUnidad->descripcion)}}{{$unidad->descripcion}} + +
+
+ +@endsection + +@push('page_scripts') + +@endpush diff --git a/app/resources/views/ventas/propietarios/edit.blade.php b/app/resources/views/ventas/propietarios/edit.blade.php new file mode 100644 index 0000000..5b6438d --- /dev/null +++ b/app/resources/views/ventas/propietarios/edit.blade.php @@ -0,0 +1,226 @@ +@extends('layout.base') + +@section('page_title') +Editar Propietario +@endsection + +@section('page_content') +
+

Editar Propietario

+
+ +
+ + {{$propietario->rut()}} +
+
+
+ + +
+
+ + +
+
+ + +
+
+
+
+ + +
+
+ + +
+
+ + +
+
+
+
+ + +
+
+ + +
+
+ +
+
+@endsection + +@push('page_scripts') + +@endpush diff --git a/app/resources/views/ventas/propietarios/show.blade.php b/app/resources/views/ventas/propietarios/show.blade.php new file mode 100644 index 0000000..db1ce61 --- /dev/null +++ b/app/resources/views/ventas/propietarios/show.blade.php @@ -0,0 +1,7 @@ +@extends('layout.base') + +@section('page_content') +
+ {{$propietario->nombreCompleto()}} +
+@endsection \ No newline at end of file diff --git a/app/resources/views/ventas/show.blade.php b/app/resources/views/ventas/show.blade.php new file mode 100644 index 0000000..f60a53a --- /dev/null +++ b/app/resources/views/ventas/show.blade.php @@ -0,0 +1,40 @@ +@extends('layout.base') + +@section('page_title') + Venta {{$venta->proyecto()->descripcion}} {{$venta->propiedad()->summary()}} +@endsection + +@section('page_content') +
+
+

+
+
{{$venta->proyecto()->descripcion}}
+ {{$venta->propiedad()->summary()}} +
+

+
+ @include('ventas.show.propietario') +
+
+
+
+ @if ($venta->currentEstado()->tipoEstadoVenta->activa) + + Desistir + + + Ceder + + @endif +
+
+ @include('ventas.show.propiedad') + @include('ventas.show.detalle') + @include('ventas.show.forma_pago', ['formaPago' => $venta->formaPago()]) + @include('ventas.show.escritura') + @include('ventas.show.entrega') + @include('ventas.show.comentarios') +
+
+@endsection diff --git a/app/resources/views/ventas/show/comentarios.blade.php b/app/resources/views/ventas/show/comentarios.blade.php new file mode 100644 index 0000000..8dabe09 --- /dev/null +++ b/app/resources/views/ventas/show/comentarios.blade.php @@ -0,0 +1,88 @@ +
+
+ COMENTARIOS +
+
+ + + +
+
+
+ + +
+
+ +@push('page_scripts') + +@endpush diff --git a/app/resources/views/ventas/show/detalle.blade.php b/app/resources/views/ventas/show/detalle.blade.php new file mode 100644 index 0000000..e928812 --- /dev/null +++ b/app/resources/views/ventas/show/detalle.blade.php @@ -0,0 +1,38 @@ +
+
+ VENTA +
+
+ + + +
+
+
+ + + + + + + + + + + + + + + + + + + +
Valor PromesaValor UtilUF/m²Comisión + Fecha Promesa
+ Fecha Ingreso +
{{$format->ufs($venta->valor)}}{{$format->ufs($venta->util())}}{{$format->number($venta->util() / $venta->propiedad()->vendible(), 2)}} UF/m²0,00 UF (0,00%) + {{$venta->fecha->format('d-m-Y')}}
+ {{$venta->fechaIngreso->format('d-m-Y')}} +
+
diff --git a/app/resources/views/ventas/show/entrega.blade.php b/app/resources/views/ventas/show/entrega.blade.php new file mode 100644 index 0000000..ce51587 --- /dev/null +++ b/app/resources/views/ventas/show/entrega.blade.php @@ -0,0 +1,11 @@ +
+ ENTREGA +
+
+ @if ($venta->entrega() !== null) + @else + + No + + @endif +
diff --git a/app/resources/views/ventas/show/escritura.blade.php b/app/resources/views/ventas/show/escritura.blade.php new file mode 100644 index 0000000..5fc371d --- /dev/null +++ b/app/resources/views/ventas/show/escritura.blade.php @@ -0,0 +1,22 @@ +
+ ESCRITURA +
+@if ($venta->formaPago()->escritura !== null) +
+ Escriturado {{$venta->formaPago()->escritura->fecha->format('d-m-Y')}} + + Informe + + +
+ + Firmar + +
+@else +
+ + Escriturar + +
+@endif diff --git a/app/resources/views/ventas/show/forma_pago.blade.php b/app/resources/views/ventas/show/forma_pago.blade.php new file mode 100644 index 0000000..f9f90b2 --- /dev/null +++ b/app/resources/views/ventas/show/forma_pago.blade.php @@ -0,0 +1,229 @@ +
+ FORMA DE PAGO +
+
+ + @include('ventas.show.forma_pago.pie', ['pie' => $formaPago->pie]) + @include('ventas.show.forma_pago.escritura', ['escritura' => $formaPago->escritura]) + @include('ventas.show.forma_pago.anticipo', ['anticipo' => ['uf' => $formaPago->anticipo(), 'pesos' => $formaPago->anticipo('pesos')]]) + @include('ventas.show.forma_pago.bono_pie', ['bonoPie' => $formaPago->bonoPie]) + @include('ventas.show.forma_pago.subsidio', ['subsidio' => $formaPago->subsidio]) + @include('ventas.show.forma_pago.credito', ['credito' => $formaPago->credito]) + @include('ventas.show.forma_pago.total') + @include('ventas.show.forma_pago.devolucion', ['devolucion' => $formaPago->devolucion]) +
+
+ + +@push('page_scripts') + +@endpush diff --git a/app/resources/views/ventas/show/forma_pago/anticipo.blade.php b/app/resources/views/ventas/show/forma_pago/anticipo.blade.php new file mode 100644 index 0000000..d9f3095 --- /dev/null +++ b/app/resources/views/ventas/show/forma_pago/anticipo.blade.php @@ -0,0 +1,7 @@ + + Anticipo + + {{$format->ufs($anticipo['uf'])}} + {{$format->pesos($anticipo['pesos'])}} + + diff --git a/app/resources/views/ventas/show/forma_pago/bono_pie.blade.php b/app/resources/views/ventas/show/forma_pago/bono_pie.blade.php new file mode 100644 index 0000000..06022e3 --- /dev/null +++ b/app/resources/views/ventas/show/forma_pago/bono_pie.blade.php @@ -0,0 +1,22 @@ + + + Bono Pie + @if ($bonoPie !== null) + + + + @else + + + + @endif + + @if ($bonoPie !== null) + + {{$format->ufs($bonoPie->pago->valor())}} + {{$format->pesos($bonoPie->pago->valor)}} + + @else + + @endif + diff --git a/app/resources/views/ventas/show/forma_pago/credito.blade.php b/app/resources/views/ventas/show/forma_pago/credito.blade.php new file mode 100644 index 0000000..a0fb0b5 --- /dev/null +++ b/app/resources/views/ventas/show/forma_pago/credito.blade.php @@ -0,0 +1,40 @@ + + + Crédito + @if ($credito !== null) + + + + @else + + + + @endif + + @if ($credito !== null) + + + {{$format->ufs($credito->pago->valor())}} + + + {{$format->pesos($credito->pago->valor)}} + + + {{$credito->pago->currentEstado->fecha->format('d-m-Y')}} + @if ($credito->pago->currentEstado->tipoEstadoPago->descripcion === 'no pagado') + + + + @elseif($credito->pago->currentEstado->tipoEstadoPago->descripcion === 'depositado') + + + + @endif + + + Banco: {{$credito->pago->banco?->nombre}} + + @else + + @endif + diff --git a/app/resources/views/ventas/show/forma_pago/devolucion.blade.php b/app/resources/views/ventas/show/forma_pago/devolucion.blade.php new file mode 100644 index 0000000..e69de29 diff --git a/app/resources/views/ventas/show/forma_pago/escritura.blade.php b/app/resources/views/ventas/show/forma_pago/escritura.blade.php new file mode 100644 index 0000000..6d329f1 --- /dev/null +++ b/app/resources/views/ventas/show/forma_pago/escritura.blade.php @@ -0,0 +1,34 @@ + + + Escritura + @if ($escritura !== null) + + + + @else + + + + @endif + + @if ($escritura !== null) + + {{$format->ufs($escritura->pago->valor())}} + {{$format->pesos($escritura->pago->valor)}} + + {{$escritura->pago->currentEstado->fecha->format('d-m-Y')}} + @if ($escritura->pago->currentEstado->tipoEstadoPago->descripcion === 'no pagado') + + + + @elseif ($escritura->pago->currentEstado->tipoEstadoPago->descripcion === 'depositado') + + + + @endif + + + @else + + @endif + diff --git a/app/resources/views/ventas/show/forma_pago/pie.blade.php b/app/resources/views/ventas/show/forma_pago/pie.blade.php new file mode 100644 index 0000000..5b6a751 --- /dev/null +++ b/app/resources/views/ventas/show/forma_pago/pie.blade.php @@ -0,0 +1,31 @@ +@if ($pie !== null) + + + Pie + + + + + + {{$format->ufs($pie->valor)}} + {{$format->pesos($pie->valor * $pie->uf)}} + Cuotas + + + {{count($pie->cuotas(true))}}/{{$pie->cuotas}} + + @if (count($pie->cuotas()) < $pie->cuotas) + + + + @endif + + + + Pagado + + {{$format->ufs($pie->pagado())}} + {{$format->pesos($pie->pagado('pesos'))}} + + +@endif diff --git a/app/resources/views/ventas/show/forma_pago/subsidio.blade.php b/app/resources/views/ventas/show/forma_pago/subsidio.blade.php new file mode 100644 index 0000000..1f0dab4 --- /dev/null +++ b/app/resources/views/ventas/show/forma_pago/subsidio.blade.php @@ -0,0 +1,32 @@ + + + Subsidio + @if ($subsidio !== null) + + + + @else + + + + @endif + + @if ($subsidio !== null) + Ahorro + {{$format->ufs($subsidio->ahorro->valor())}} + {{$format->pesos($subsidio->ahorro->valor)}} + + + + Subsidio + {{$format->ufs($subsidio->ahorro->valor())}} + {{$format->pesos($subsidio->ahorro->valor)}} + + @else + + @endif + diff --git a/app/resources/views/ventas/show/forma_pago/total.blade.php b/app/resources/views/ventas/show/forma_pago/total.blade.php new file mode 100644 index 0000000..06d3ac9 --- /dev/null +++ b/app/resources/views/ventas/show/forma_pago/total.blade.php @@ -0,0 +1,28 @@ + + + + Total + + + + + + {{$format->ufs($formaPago->total())}} + + + + + {{$format->pesos($formaPago->total('pesos'))}} + + + + @if ($venta->saldo() / $venta->valor > 0.01) + + Δ + {{$format->ufs($venta->saldo())}} + ({{$format->number($venta->saldo() / $venta->valor * 100, 2)}} %) + + @endif + + + diff --git a/app/resources/views/ventas/show/propiedad.blade.php b/app/resources/views/ventas/show/propiedad.blade.php new file mode 100644 index 0000000..8d6208c --- /dev/null +++ b/app/resources/views/ventas/show/propiedad.blade.php @@ -0,0 +1,56 @@ +
+
+ PROPIEDAD +
+
+ + + +
+
+
+ + + + + + + + + + + + + @foreach($venta->propiedad()->unidades as $unidad) + + + + + + + + + + @endforeach + +
UnidadPisoMetros vendiblesPrecioUF/m²Orientacion
+ {{ucwords($unidad->proyectoTipoUnidad->tipoUnidad->descripcion)}} + @if ($unidad->proyectoTipoUnidad->tipoUnidad->descripcion === 'departamento') + {{$unidad->proyectoTipoUnidad->abreviacion}} + @endif + + {{$unidad->descripcion}} + + {{$unidad->piso}} + + {{$format->number($unidad->proyectoTipoUnidad->vendible(), 2)}} m² + + {{$format->ufs($unidad->precio($venta->fecha)->valor)}} + + @if ($unidad->proyectoTipoUnidad->tipoUnidad->descripcion === 'departamento') + {{$format->number($unidad->precio($venta->fecha)->valor / $unidad->proyectoTipoUnidad->vendible(), 2)}} UF/m² + @endif + + {{$unidad->orientacion}} +
+
diff --git a/app/resources/views/ventas/show/propietario.blade.php b/app/resources/views/ventas/show/propietario.blade.php new file mode 100644 index 0000000..6a7ac45 --- /dev/null +++ b/app/resources/views/ventas/show/propietario.blade.php @@ -0,0 +1,15 @@ +
+
+ + + + + {{$venta->propietario()->nombreCompleto()}} + + + {{$venta->propietario()->rut()}} +
+ {{$venta->propietario()->datos->direccion}} +
+
+
diff --git a/app/src/Controller/Direcciones.php b/app/src/Controller/Direcciones.php new file mode 100644 index 0000000..18024bf --- /dev/null +++ b/app/src/Controller/Direcciones.php @@ -0,0 +1,39 @@ +fetchByRegion($region_id); + $comunas = []; + foreach($temp_provincias as $provincia) { + $temp_comunas = $comunaRepository->fetchByProvincia($provincia->id); + $comunas = array_merge($comunas, $temp_comunas); + } + usort($comunas, function(Model\Comuna $a, Model\Comuna $b) { + return strcoll($a->descripcion, $b->descripcion); + }); + $response->getBody()->write(json_encode(['comunas' => $comunas, 'total' => count($comunas)])); + return $response->withHeader('Content-Type', 'application/json'); + } + public function findComunas(ServerRequestInterface $request, ResponseInterface $response, Repository\Comuna $comunaRepository): ResponseInterface + { + $body = $request->getBody(); + $json = json_decode($body->getContents()); + $output = ['total' => 0]; + try { + $comunas = $comunaRepository->fetchByDireccion($json->direccion); + $output['comunas'] = $comunas; + $output['total'] = count($comunas); + } catch (EmptyResult) {} + $response->getBody()->write(json_encode($output)); + return $response->withHeader('Content-Type', 'application/json'); + } +} diff --git a/app/src/Controller/Proyectos.php b/app/src/Controller/Proyectos.php index 9ef8c47..b54dadb 100644 --- a/app/src/Controller/Proyectos.php +++ b/app/src/Controller/Proyectos.php @@ -1,6 +1,7 @@ fetchAllActive(); - $response->getBody()->write(json_encode(['proyectos' => $proyectos, 'total' => count($proyectos)])); + $output = ['total' => 0]; + try { + $proyectos = $proyectoRepository->fetchAllActive(); + $output['proyectos'] = $proyectos; + $output['total'] = count($proyectos); + } catch (EmptyResult) {} + $response->getBody()->write(json_encode($output)); return $response->withHeader('Content-Type', 'application/json'); } } diff --git a/app/src/Controller/Ventas.php b/app/src/Controller/Ventas.php index 6a2d55b..e74e222 100644 --- a/app/src/Controller/Ventas.php +++ b/app/src/Controller/Ventas.php @@ -1,11 +1,14 @@ $proyecto->id, 'descripcion' => $proyecto->descripcion];}, $proyectoService->getVendibles()); return $view->render($response, 'ventas.list', compact('proyectos')); } - public function proyecto(ServerRequestInterface $request, ResponseInterface $response, Service\Venta $service): ResponseInterface + public function show(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Venta $service, int $venta_id): ResponseInterface + { + $venta = $service->getById($venta_id); + return $view->render($response, 'ventas.show', compact('venta')); + } + public function showUnidad(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Venta $service, string $proyecto_nombre, int $unidad_descripcion): ResponseInterface + { + $proyecto_nombre = urldecode($proyecto_nombre); + $venta = $service->getByProyectoAndUnidad($proyecto_nombre, $unidad_descripcion); + return $view->render($response, 'ventas.show', compact('venta')); + } + public function proyecto(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta $service): ResponseInterface { $body = $request->getBody(); $json = json_decode($body->getContents()); $proyecto_id = $json->proyecto_id; - $ventas = $service->getByProyecto($proyecto_id); - $response->getBody()->write(json_encode(['ventas' => $ventas, 'total' => count($ventas)])); + $output = [ + 'proyecto' => [ + 'id' => $proyecto_id + ], + 'total' => 0 + ]; + try { + $ventas = $service->fetchByProyecto($proyecto_id); + $output['ventas'] = array_map(function(Model\Venta $venta) {return $venta->id;}, $ventas); + $output['proyecto']['descripcion'] = $ventas[0]->proyecto()->descripcion; + $output['total'] = count($ventas); + } catch (EmptyResult) {} + $response->getBody()->write(json_encode($output)); + return $response->withHeader('Content-Type', 'application/json'); + } + public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Venta $service, int $venta_id): ResponseInterface + { + try { + $venta = $service->getById($venta_id); + $response->getBody()->write(json_encode(compact('venta'))); + } catch (EmptyResult $exception) { + $response->getBody()->write(json_encode(['error' => [ + 'code' => $exception->getCode(), + 'message' => str_replace([PHP_EOL, "\r"], [' ', ''], $exception->getMessage()) + ]])); + } + return $response->withHeader('Content-Type', 'application/json'); + } + public function edit(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Venta $service, int $venta_id): ResponseInterface + { + $venta = $service->getById($venta_id); + return $view->render($response, 'ventas.edit', compact('venta')); + } + public function propietario(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Venta $service, Repository\Region $regionRepository, int $venta_id): ResponseInterface + { + $venta = $service->getById($venta_id); + $propietario = $venta->propietario(); + $regiones = $regionRepository->fetchAll(); + usort($regiones, function(Model\Region $a, Model\Region $b) { + return $a->numeracion - $b->numeracion; + }); + return $view->render($response, 'ventas.propietarios.edit', compact('propietario', 'venta_id', 'regiones')); + } + public function propiedad(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Venta $service, Service\Venta\Unidad $unidadService, int $venta_id): ResponseInterface + { + $venta = $service->getById($venta_id); + $propiedad = $venta->propiedad(); + $proyecto = $venta->proyecto(); + $unidades = $unidadService->getDisponiblesByProyecto($proyecto->id); + $tiposUnidades = []; + foreach ($unidades as $unidad) { + if (!in_array($unidad->proyectoTipoUnidad->tipoUnidad, $tiposUnidades)) { + $tiposUnidades []= $unidad->proyectoTipoUnidad->tipoUnidad; + } + } + return $view->render($response, 'ventas.propiedades.edit', compact('propiedad', 'proyecto', 'tiposUnidades', 'unidades', 'venta_id')); + } + public function comentarios(ServerRequestInterface $request, ResponseInterface $response, Service\Venta $service, Repository\Venta\Comentario $comentarioRepository, int $venta_id): ResponseInterface + { + $venta = $service->getById($venta_id); + $output = ['total' => 0]; + try { + $comentarios = $comentarioRepository->fetchByVenta($venta->id); + $output['total'] = count($comentarios); + $output['comentarios'] = $comentarios; + } catch (EmptyResult) {} + $response->getBody()->write(json_encode($output)); return $response->withHeader('Content-Type', 'application/json'); } } diff --git a/app/src/Controller/Ventas/Cierres.php b/app/src/Controller/Ventas/Cierres.php index 8cfde24..ed92eca 100644 --- a/app/src/Controller/Ventas/Cierres.php +++ b/app/src/Controller/Ventas/Cierres.php @@ -1,6 +1,7 @@ render($response, 'ventas.cierres.list'); } - public function show(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Ventas\Cierre $service, int $cierre_id): ResponseInterface + public function show(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Venta\Cierre $service, int $cierre_id): ResponseInterface { $cierre = $service->getById($cierre_id); return $view->render($response, 'ventas.cierres.show', compact('cierre')); } - public function proyecto(ServerRequestInterface $request, ResponseInterface $response, Service\Ventas\Cierre $service): ResponseInterface + public function proyecto(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Cierre $service): ResponseInterface { $body = $request->getBody(); $json = json_decode($body->getContents()); $proyecto_id = $json->proyecto_id; - $cierres = $service->getByProyecto($proyecto_id); - $response->getBody()->write(json_encode(['cierres' => $cierres, 'total' => count($cierres)])); + $output = ['total' => 0]; + try { + $cierres = $service->getByProyecto($proyecto_id); + $output['cierres'] = $cierres; + $output['total'] = count($cierres); + } catch (EmptyResult) {} + $response->getBody()->write(json_encode($output)); return $response->withHeader('Content-Type', 'application/json'); } } diff --git a/app/src/Controller/Ventas/Comentarios.php b/app/src/Controller/Ventas/Comentarios.php new file mode 100644 index 0000000..8d39525 --- /dev/null +++ b/app/src/Controller/Ventas/Comentarios.php @@ -0,0 +1,22 @@ + 0]; + try { + $comentarios = $comentarioRepository->fetchAll(); + $output['comentarios'] = $comentarios; + $output['total'] = count($comentarios); + } catch (EmptyResult) {} + $response->getBody()->write(json_encode($output)); + return $response->withHeader('Content-Type', 'application/json'); + } +} diff --git a/app/src/Controller/Ventas/Cuotas.php b/app/src/Controller/Ventas/Cuotas.php index 87960c4..313dff8 100644 --- a/app/src/Controller/Ventas/Cuotas.php +++ b/app/src/Controller/Ventas/Cuotas.php @@ -3,6 +3,7 @@ namespace Incoviba\Controller\Ventas; use DateTimeImmutable; use DateInterval; +use Incoviba\Common\Implement\Exception\EmptyResult; use IntlDateFormatter; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -44,16 +45,19 @@ class Cuotas } return $view->render($response, 'ventas.cuotas.pendientes', compact('cuotas_pendientes')); } - public function depositar(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Cuota $cuotaRepository, Service\Ventas\Pago $pagoService): ResponseInterface + public function depositar(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Cuota $cuotaRepository, Service\Venta\Pago $pagoService): ResponseInterface { $body = $request->getBody(); $json = json_decode($body->getContents()); $cuota_id = $json->cuota_id; - $cuota = $cuotaRepository->fetchById($cuota_id); $output = [ 'cuota_id' => $cuota_id, - 'depositada' => $pagoService->depositar($cuota->pago) + 'depositada' => false ]; + try{ + $cuota = $cuotaRepository->fetchById($cuota_id); + $output['depositada'] = $pagoService->depositar($cuota->pago); + } catch (EmptyResult) {} $response->getBody()->write(json_encode($output)); return $response->withHeader('Content-Type', 'application/json'); } diff --git a/app/src/Controller/Ventas/Pagos.php b/app/src/Controller/Ventas/Pagos.php new file mode 100644 index 0000000..6d93342 --- /dev/null +++ b/app/src/Controller/Ventas/Pagos.php @@ -0,0 +1,26 @@ +getBody(); + $json = json_decode($body->getContents()); + $date = new DateTimeImmutable($json->fecha); + $response->getBody()->write(json_encode(['fecha' => $date->format('d-m-Y'), 'message' => 'Not implemented'])); + return $response->withHeader('Content-Type', 'application/json'); + } + public function abonar(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface + { + $body = $request->getBody(); + $json = json_decode($body->getContents()); + $date = new DateTimeImmutable($json->fecha); + $response->getBody()->write(json_encode(['fecha' => $date->format('d-m-Y'), 'message' => 'Not implemented'])); + return $response->withHeader('Content-Type', 'application/json'); + } +} diff --git a/app/src/Controller/Ventas/Precios.php b/app/src/Controller/Ventas/Precios.php index 56c4416..6a96459 100644 --- a/app/src/Controller/Ventas/Precios.php +++ b/app/src/Controller/Ventas/Precios.php @@ -1,6 +1,7 @@ $proyecto->id, 'descripcion' => $proyecto->descripcion];}, $proyectoService->getVendibles()); return $view->render($response, 'ventas.precios.list', compact('proyectos')); } - public function proyecto(ServerRequestInterface $request, ResponseInterface $response, Service\Ventas\Precio $precioService): ResponseInterface + public function proyecto(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Precio $precioService): ResponseInterface { $body = $request->getBody(); $json = json_decode($body->getContents()); $proyecto_id = $json->proyecto_id; - $precios = $precioService->getByProyecto($proyecto_id); - $response->getBody()->write(json_encode(['precios' => $precios, 'total' => count($precios)])); + $output = ['total' => 0]; + try { + $precios = $precioService->getByProyecto($proyecto_id); + $output['precios'] = $precios; + $output['total'] = count($precios); + } catch (EmptyResult) {} + $response->getBody()->write(json_encode($output)); return $response->withHeader('Content-Type', 'application/json'); } - public function unidad(ServerRequestInterface $request, ResponseInterface $response, Service\Ventas\Precio $precioService, int $unidad_id): ResponseInterface + public function unidad(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Precio $precioService, int $unidad_id): ResponseInterface { - $precio = $precioService->getByUnidad($unidad_id); + $precio = $precioService->getVigenteByUnidad($unidad_id); $response->getBody()->write(json_encode(['precio' => $precio])); return $response->withHeader('Content-Type', 'application/json'); } diff --git a/app/src/Controller/Ventas/Propietarios.php b/app/src/Controller/Ventas/Propietarios.php new file mode 100644 index 0000000..6d119fc --- /dev/null +++ b/app/src/Controller/Ventas/Propietarios.php @@ -0,0 +1,15 @@ +fetchById($propietario_rut); + return $view->render($response, 'ventas.propietarios.show', compact('propietario')); + } +} \ No newline at end of file diff --git a/app/src/Model/Comuna.php b/app/src/Model/Comuna.php index 6739cb7..057bdf1 100644 --- a/app/src/Model/Comuna.php +++ b/app/src/Model/Comuna.php @@ -17,6 +17,6 @@ class Comuna extends Model } public function __toString(): string { - return implode(', ', [$this->descripcion, '' . $this->provincia]); + return $this->descripcion; } } diff --git a/app/src/Model/Direccion.php b/app/src/Model/Direccion.php index 6ed67af..92b8e05 100644 --- a/app/src/Model/Direccion.php +++ b/app/src/Model/Direccion.php @@ -10,6 +10,14 @@ class Direccion extends Model public string $extra; public Comuna $comuna; + public function full(): string + { + return implode(', ', [ + "{$this}", + $this->comuna->provincia + ]); + } + public function jsonSerialize(): mixed { return array_merge(parent::jsonSerialize(), [ @@ -30,7 +38,7 @@ class Direccion extends Model if ($this->extra !== '') { $array[]= $this->extra; } - $array []= '' . $this->comuna; + $array []= $this->comuna; return implode(', ', $array); } } diff --git a/app/src/Model/Proyecto.php b/app/src/Model/Proyecto.php index 3b1d12a..633e9e7 100644 --- a/app/src/Model/Proyecto.php +++ b/app/src/Model/Proyecto.php @@ -5,21 +5,36 @@ use Incoviba\Common\Ideal; class Proyecto extends Ideal\Model { - public Inmobiliaria $inmobiliaria; + protected Inmobiliaria $inmobiliaria; public string $descripcion; - public Direccion $direccion; + protected Direccion $direccion; public Proyecto\Terreno $terreno; public Proyecto\Superficie $superficie; public float $corredor; public int $pisos; public int $subterraneos; + public function inmobiliaria(): Inmobiliaria + { + if (!isset($this->inmobiliaria)) { + $this->inmobiliaria = $this->runFactory('inmobiliaria'); + } + return $this->inmobiliaria; + } + public function direccion(): Direccion + { + if (!isset($this->direccion)) { + $this->direccion = $this->runFactory('direccion'); + } + return $this->direccion; + } + public function jsonSerialize(): mixed { return array_merge(parent::jsonSerialize(), [ - 'inmobiliaria' => $this->inmobiliaria, + 'inmobiliaria' => $this->inmobiliaria(), 'descripcion' => $this->descripcion, - 'direccion' => $this->direccion, + 'direccion' => $this->direccion(), 'terreno' => $this->terreno, 'superficie' => $this->superficie, 'corredor' => $this->corredor, diff --git a/app/src/Model/Proyecto/Elemento.php b/app/src/Model/Proyecto/Elemento.php new file mode 100644 index 0000000..9a015ff --- /dev/null +++ b/app/src/Model/Proyecto/Elemento.php @@ -0,0 +1,20 @@ + $this->descripcion, + 'abreviacion' => $this->abreviacion, + 'orden' => $this->orden + ]); + } +} diff --git a/app/src/Model/Proyecto/ProyectoTipoUnidad.php b/app/src/Model/Proyecto/ProyectoTipoUnidad.php index a755bb7..e05f9d9 100644 --- a/app/src/Model/Proyecto/ProyectoTipoUnidad.php +++ b/app/src/Model/Proyecto/ProyectoTipoUnidad.php @@ -15,6 +15,8 @@ class ProyectoTipoUnidad extends Ideal\Model public float $terraza; public string $descripcion; + public array $tipologias; + public function superficie(): float { return array_reduce([$this->util, $this->logia, $this->terraza], function($sum, $item) {return $sum + $item;}, 0); @@ -23,6 +25,23 @@ class ProyectoTipoUnidad extends Ideal\Model { return array_reduce([$this->util, $this->logia, $this->terraza / 2], function($sum, $item) {return $sum + $item;}, 0); } + public function tipologia(): string + { + if (isset($this->tipologias) and count($this->tipologias) > 0) { + $temp = [...$this->tipologias]; + usort($temp, function(Tipologia $a, Tipologia $b) { + $el = $a->elemento->orden - $b->elemento->orden; + if ($el === 0) { + return strcmp($a->elemento->abreviacion, $b->elemento->abreviacion); + } + return $el; + }); + return implode('/', array_map(function(Tipologia $tipologia) { + return $tipologia->cantidad . $tipologia->elemento->abreviacion; + }, $temp)); + } + return $this->abreviacion; + } public function jsonSerialize(): mixed { @@ -36,7 +55,8 @@ class ProyectoTipoUnidad extends Ideal\Model 'terraza' => $this->terraza, 'superficie' => $this->superficie(), 'vendible' => $this->vendible(), - 'descripcion' => $this->descripcion + 'descripcion' => $this->descripcion, + 'tipologia' => $this->tipologia() ]); } } diff --git a/app/src/Model/Proyecto/TipoTipologia.php b/app/src/Model/Proyecto/TipoTipologia.php new file mode 100644 index 0000000..39a3649 --- /dev/null +++ b/app/src/Model/Proyecto/TipoTipologia.php @@ -0,0 +1,8 @@ + $this->cantidad, + 'elemento' => $this->elemento + ]); + } +} diff --git a/app/src/Model/Venta.php b/app/src/Model/Venta.php index ea9ec91..e648c59 100644 --- a/app/src/Model/Venta.php +++ b/app/src/Model/Venta.php @@ -3,32 +3,103 @@ namespace Incoviba\Model; use DateTimeInterface; use Incoviba\Common\Ideal; +use Incoviba\Controller\Ventas; class Venta extends Ideal\Model { - public Venta\Propietario $propietario; - public Venta\Propiedad $propiedad; - public Venta\FormaPago $formaPago; + protected Venta\Propietario $propietario; + protected Venta\Propiedad $propiedad; + protected Venta\FormaPago $formaPago; public DateTimeInterface $fecha; public DateTimeInterface $fechaIngreso; public float $valor; public bool $relacionado; + protected ?Venta\Entrega $entrega; public array $estados; public Venta\EstadoVenta $currentEstado; + public function propietario(): Venta\Propietario + { + if (!isset($this->propietario)) { + $this->propietario = $this->runFactory('propietario'); + } + return $this->propietario; + } + public function propiedad(): Venta\Propiedad + { + if (!isset($this->propiedad)) { + $this->propiedad = $this->runFactory('propiedad'); + } + return $this->propiedad; + } + public function formaPago(): Venta\FormaPago + { + if (!isset($this->formaPago)) { + $this->formaPago = $this->runFactory('formaPago'); + } + return $this->formaPago; + } + public function entrega(): ?Venta\Entrega + { + if (!isset($this->entrega)) { + $this->entrega = $this->runFactory('entrega'); + } + return $this->entrega; + } + + public function estados(): array + { + if (!isset($this->estados)) { + $this->estados = $this->runFactory('estados'); + } + return $this->estados; + } + public function currentEstado(): ?Venta\EstadoVenta + { + if (!isset($this->currentEstado)) { + $this->currentEstado = $this->runFactory('currentEstado'); + } + return $this->currentEstado; + } + + public function proyecto(): Proyecto + { + return $this->propiedad()->departamentos()[0]->proyectoTipoUnidad->proyecto; + } + + protected float $valor_util; + public function util(): float + { + if (!isset($this->valor_util)) { + $sum = $this->valor; + $sum -= array_reduce($this->propiedad()->estacionamientos(), function(float $sum, Venta\Unidad $unidad) { + return $unidad->precio($this->fecha)->valor; + }, 0); + $sum -= array_reduce($this->propiedad()->bodegas(), function(float $sum, Venta\Unidad $unidad) { + return $unidad->precio($this->fecha)->valor; + }, 0); + $this->valor_util = $sum; + } + return $this->valor_util; + } + public function saldo(): float + { + return $this->valor - $this->formaPago->total(); + } + public function jsonSerialize(): mixed { return array_merge(parent::jsonSerialize(), [ - 'propietario' => $this->propietario, - 'propiedad' => $this->propiedad, - 'forma_pago' => $this->formaPago, + 'propietario' => $this->propietario(), + 'propiedad' => $this->propiedad(), + 'forma_pago' => $this->formaPago()->ids(), 'fecha' => $this->fecha->format('Y-m-d'), 'fecha_ingreso' => $this->fechaIngreso->format('Y-m-d'), 'valor' => $this->valor, 'relacionado' => $this->relacionado, - 'estados' => $this->estados, - 'current_estado' => $this->currentEstado + 'estados' => array_map(function(Venta\EstadoVenta $estado) {return $estado->id;}, $this->estados()), + 'current_estado' => $this->currentEstado() ]); } } diff --git a/app/src/Model/Venta/BonoPie.php b/app/src/Model/Venta/BonoPie.php index a94d24f..02f1666 100644 --- a/app/src/Model/Venta/BonoPie.php +++ b/app/src/Model/Venta/BonoPie.php @@ -5,4 +5,14 @@ use Incoviba\Common\Ideal; class BonoPie extends Ideal\Model { + public float $valor; + public Pago $pago; + + public function jsonSerialize(): mixed + { + return array_merge(parent::jsonSerialize(), [ + 'valor' => $this->valor, + 'pago' => $this->pago + ]); + } } diff --git a/app/src/Model/Venta/Comentario.php b/app/src/Model/Venta/Comentario.php new file mode 100644 index 0000000..55910bb --- /dev/null +++ b/app/src/Model/Venta/Comentario.php @@ -0,0 +1,21 @@ + $this->fecha->format('Y-m-d'), + 'texto' => $this->texto, + 'activo' => $this->activo + ]); + } +} diff --git a/app/src/Model/Venta/Credito.php b/app/src/Model/Venta/Credito.php index 8dea8ef..e1f8a3a 100644 --- a/app/src/Model/Venta/Credito.php +++ b/app/src/Model/Venta/Credito.php @@ -4,4 +4,13 @@ namespace Incoviba\Model\Venta; use Incoviba\Common\Ideal; class Credito extends Ideal\Model -{} +{ + public Pago $pago; + + public function jsonSerialize(): mixed + { + return array_merge(parent::jsonSerialize(), [ + 'pago' => $this->pago + ]); + } +} diff --git a/app/src/Model/Venta/Escritura.php b/app/src/Model/Venta/Escritura.php index 56b0a48..035fe09 100644 --- a/app/src/Model/Venta/Escritura.php +++ b/app/src/Model/Venta/Escritura.php @@ -1,7 +1,19 @@ $this->pago, + 'fecha' => $this->fecha->format('Y-m-d') + ]); + } +} diff --git a/app/src/Model/Venta/FormaPago.php b/app/src/Model/Venta/FormaPago.php index 0c44d2d..497ce05 100644 --- a/app/src/Model/Venta/FormaPago.php +++ b/app/src/Model/Venta/FormaPago.php @@ -6,19 +6,62 @@ use JsonSerializable; class FormaPago implements JsonSerializable { public ?Pie $pie; - public ?BonoPie $bonoPie; - public ?Credito $credito; public ?Escritura $escritura; + public ?BonoPie $bonoPie; public ?Subsidio $subsidio; + public ?Credito $credito; + public ?Pago $devolucion; + + public function anticipo(string $moneda = Pago::UF): float + { + $sum = 0; + if ($this->pie !== null) { + $sum += $this->pie->pagado($moneda); + if (isset($this->pie->reajuste) and $this->pie->reajuste !== null) { + $sum += $this->pie->reajuste->valor($moneda); + } + } + if ($this->escritura !== null) { + $sum += $this->escritura->pago->valor($moneda); + } + return $sum; + } + public function total(string $moneda = Pago::UF): float + { + $sum = $this->anticipo($moneda); + if (isset($this->bonoPie)) { + $sum += $this->bonoPie->pago->valor($moneda); + } + if (isset($this->subsidio)) { + $sum += $this->subsidio->ahorro->valor($moneda); + $sum += $this->subsidio->subsidio->valor($moneda); + } + if (isset($this->credito)) { + $sum += $this->credito->pago->valor($moneda); + } + return $sum; + } + public function ids(): array + { + return [ + 'pie_id' => $this->pie?->id, + 'escritura_id' => $this->escritura?->id, + 'bono_pie_id' => $this->bonoPie?->id, + 'credito_id' => $this->credito?->id, + 'subsidio_id' => $this->subsidio?->id, + 'devolucion_id' => $this->devolucion?->id + ]; + } public function jsonSerialize(): mixed { return [ 'pie' => $this->pie ?? null, - 'bono_pie' => $this->bonoPie ?? null, - 'credito' => $this->credito ?? null, 'escritura' => $this->escritura ?? null, - 'subsidio' => $this->subsidio ?? null + 'bono_pie' => $this->bonoPie ?? null, + 'subsidio' => $this->subsidio ?? null, + 'credito' => $this->credito ?? null, + 'devolucion' => $this->devolucion ?? null ]; } } diff --git a/app/src/Model/Venta/Pago.php b/app/src/Model/Venta/Pago.php index 08f5da9..9b2e1ba 100644 --- a/app/src/Model/Venta/Pago.php +++ b/app/src/Model/Venta/Pago.php @@ -7,6 +7,9 @@ use Incoviba\Model\Banco; class Pago extends Model { + const UF = 'uf'; + const PESOS = 'pesos'; + public float $valor; public ?Banco $banco; public ?TipoPago $tipoPago; @@ -16,6 +19,14 @@ class Pago extends Model public ?string $pagador; public ?Pago $asociado; + public array $estados; + public EstadoPago $currentEstado; + + public function valor(string $moneda = Pago::UF): float + { + return $this->valor / (($moneda === Pago::UF) ? ($this->uf > 0 ? $this->uf : 1) : 1); + } + public function jsonSerialize(): mixed { return array_merge(parent::jsonSerialize(), [ diff --git a/app/src/Model/Venta/Pie.php b/app/src/Model/Venta/Pie.php index 3a1f4ad..0931ee9 100644 --- a/app/src/Model/Venta/Pie.php +++ b/app/src/Model/Venta/Pie.php @@ -13,6 +13,24 @@ class Pie extends Model public ?Pie $asociado; public ?Pago $reajuste; + public array $cuotasArray; + public function cuotas(bool $pagadas = false): array + { + if (!$pagadas) { + return $this->cuotasArray; + } + return array_filter($this->cuotasArray, function(Cuota $cuota) { + return $cuota->pago->currentEstado->tipoEstadoPago->descripcion !== 'no pagado'; + }); + } + + public function pagado(string $moneda = Pago::UF): float + { + return array_reduce($this->cuotas(true), function(float $sum, Cuota $cuota) use ($moneda) { + return $sum + $cuota->pago->valor($moneda); + }, 0); + } + public function jsonSerialize(): mixed { return array_merge(parent::jsonSerialize(), [ diff --git a/app/src/Model/Venta/Propiedad.php b/app/src/Model/Venta/Propiedad.php index c5c6eef..0e18cea 100644 --- a/app/src/Model/Venta/Propiedad.php +++ b/app/src/Model/Venta/Propiedad.php @@ -20,6 +20,14 @@ class Propiedad extends Ideal\Model return array_filter($this->unidades, function(Unidad $unidad) {return $unidad->proyectoTipoUnidad->tipoUnidad->descripcion === 'bodega';}); } + protected float $vendible; + public function vendible(): float + { + return array_reduce($this->departamentos(), function(float $sum, Unidad $unidad) { + return $sum + $unidad->proyectoTipoUnidad->vendible(); + }, 0); + } + public function summary(): string { return implode(' - ', array_merge( diff --git a/app/src/Model/Venta/Unidad.php b/app/src/Model/Venta/Unidad.php index fc2da77..6d66454 100644 --- a/app/src/Model/Venta/Unidad.php +++ b/app/src/Model/Venta/Unidad.php @@ -1,6 +1,7 @@ $this->currentPrecio->current->fecha) { + return $this->currentPrecio; + } + $precio = array_reduce(array_filter($this->precios, function(Precio $precio) use ($dateTime) { + return $dateTime > $precio->current->fecha; + }), function(?Precio $max, Precio $precio) { + if ($max === null) { + return $precio; + } + return $max->current->fecha > $precio->current->fecha ? $max : $precio; + }); + if ($precio === null) { + $precio = array_reduce(array_filter($this->precios, function(Precio $precio) use ($dateTime) { + return $dateTime < $precio->current->fecha; + }), function(?Precio $min, Precio $precio) { + if ($min === null) { + return $precio; + } + return $min->current->fecha < $precio->current->fecha ? $min : $precio; + }); + } + + return $precio; + } + public function jsonSerialize(): mixed { return array_merge(parent::jsonSerialize(), [ diff --git a/app/src/Repository/Banco.php b/app/src/Repository/Banco.php index 7a35492..00afc45 100644 --- a/app/src/Repository/Banco.php +++ b/app/src/Repository/Banco.php @@ -3,6 +3,7 @@ namespace Incoviba\Repository; use Incoviba\Common\Define; use Incoviba\Common\Ideal; +use Incoviba\Common\Implement; use Incoviba\Model; class Banco extends Ideal\Repository @@ -15,9 +16,7 @@ class Banco extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'nombre' => [] - ]; + $map = new Implement\Repository\MapperParser(['nombre']); return $this->parseData(new Model\Banco(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/Comuna.php b/app/src/Repository/Comuna.php index ddb54e1..c7567ec 100644 --- a/app/src/Repository/Comuna.php +++ b/app/src/Repository/Comuna.php @@ -3,6 +3,7 @@ namespace Incoviba\Repository; use Incoviba\Common\Define; use Incoviba\Common\Ideal; +use Incoviba\Common\Implement; use Incoviba\Model; class Comuna extends Ideal\Repository @@ -15,14 +16,10 @@ class Comuna extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'descripcion' => [], - 'provincia' => [ - 'function' => function($data) { - return $this->provinciaRepository->fetchById($data['provincia']); - } - ] - ]; + $map = (new Implement\Repository\MapperParser(['descripcion'])) + ->register('provincia', (new Implement\Repository\Mapper())->setFunction(function($data) { + return $this->provinciaRepository->fetchById($data['provincia']); + })); return $this->parseData(new Model\Comuna(), $data, $map); } public function save(Define\Model $model): Define\Model @@ -48,4 +45,12 @@ class Comuna extends Ideal\Repository $query = "SELECT * FROM `{$this->getTable()}` WHERE `provincia` = ?"; return $this->fetchMany($query, [$provincia_id]); } + public function fetchByDireccion(string $direccion): array + { + $query = "SELECT a.* +FROM `{$this->getTable()}` a + JOIN `direccion` ON `direccion`.`comuna` = a.`id` +WHERE TRIM(CONCAT_WS(' ', `direccion`.`calle`, `direccion`.`numero`, `direccion`.`extra`)) LIKE ?"; + return $this->fetchMany($query, ["%{$direccion}%"]); + } } diff --git a/app/src/Repository/Direccion.php b/app/src/Repository/Direccion.php index 87bd955..44d3508 100644 --- a/app/src/Repository/Direccion.php +++ b/app/src/Repository/Direccion.php @@ -2,10 +2,11 @@ namespace Incoviba\Repository; use Incoviba\Common\Define; -use Incoviba\Common\Ideal\Repository; +use Incoviba\Common\Ideal; +use Incoviba\Common\Implement; use Incoviba\Model; -class Direccion extends Repository +class Direccion extends Ideal\Repository { public function __construct(Define\Connection $connection, protected Comuna $comunaRepository) { @@ -15,16 +16,10 @@ class Direccion extends Repository public function create(?array $data = null): Define\Model { - $map = [ - 'calle' => [], - 'numero' => [], - 'extra' => [], - 'comuna' => [ - 'function' => function($data) { - return $this->comunaRepository->fetchById($data['comuna']); - } - ] - ]; + $map = (new Implement\Repository\MapperParser(['calle', 'numero', 'extra'])) + ->register('comuna', (new Implement\Repository\Mapper())->setFunction(function($data) { + return $this->comunaRepository->fetchById($data['comuna']); + })); return $this->parseData(new Model\Direccion(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/Inmobiliaria.php b/app/src/Repository/Inmobiliaria.php index 7ae90c6..bd5d99c 100644 --- a/app/src/Repository/Inmobiliaria.php +++ b/app/src/Repository/Inmobiliaria.php @@ -1,8 +1,9 @@ [], - 'razon' => [], - 'abreviacion' => [], - 'cuenta' => [], - 'banco' => [ - 'function' => function($data) { + $map = (new Implement\Repository\MapperParser(['db', 'razon', 'abreviacion', 'cuenta'])) + ->register('banco', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { return $this->bancoRepository->fetchById($data['banco']); - } - ], - 'sociedad' => [ - 'property' => 'tipoSociedad', - 'function' => function($data) { + })) + ->register('sociedad', (new Implement\Repository\Mapper()) + ->setProperty('tipoSociedad') + ->setFunction(function($data) { return $this->tipoSociedadRepository->fetchById($data['sociedad']); - } - ] - ]; + })); return $this->parseData(new Model\Inmobiliaria(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/Inmobiliaria/TipoSociedad.php b/app/src/Repository/Inmobiliaria/TipoSociedad.php index 0d2a05f..7fa573d 100644 --- a/app/src/Repository/Inmobiliaria/TipoSociedad.php +++ b/app/src/Repository/Inmobiliaria/TipoSociedad.php @@ -3,6 +3,7 @@ namespace Incoviba\Repository\Inmobiliaria; use Incoviba\Common\Ideal; use Incoviba\Common\Define; +use Incoviba\Common\Implement; use Incoviba\Model; class TipoSociedad extends Ideal\Repository @@ -15,10 +16,7 @@ class TipoSociedad extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'descripcion' => [], - 'abreviacion' => [] - ]; + $map = (new Implement\Repository\MapperParser(['descripcion', 'abreviacion'])); return $this->parseData(new Model\Inmobiliaria\TipoSociedad(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/Login.php b/app/src/Repository/Login.php index 0cfa3e4..e9e872b 100644 --- a/app/src/Repository/Login.php +++ b/app/src/Repository/Login.php @@ -4,6 +4,7 @@ namespace Incoviba\Repository; use DateTimeImmutable; use Incoviba\Common\Define; use Incoviba\Common\Ideal; +use Incoviba\Common\Implement; use Incoviba\Model; class Login extends Ideal\Repository @@ -17,27 +18,14 @@ class Login extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'user_id' => [ - 'property' => 'user', - 'function' => function($data) { + $map = (new Implement\Repository\MapperParser(['selector', 'token'])) + ->register('user_id', (new Implement\Repository\Mapper()) + ->setProperty('user') + ->setFunction(function($data) { return $this->userRepository->fetchById($data['user_id']); - } - ], - 'selector' => [], - 'token' => [], - 'time' => [ - 'property' => 'dateTime', - 'function' => function($data) { - return new DateTimeImmutable($data['time']); - } - ], - 'status' => [ - 'function' => function($data) { - return $data['status'] != 0; - } - ] - ]; + })) + ->register('time', new Implement\Repository\Mapper\DateTime('time', 'dateTime')) + ->register('status', new Implement\Repository\Mapper\Boolean('status')); return $this->parseData(new Model\Login(), $data, $map); } diff --git a/app/src/Repository/Provincia.php b/app/src/Repository/Provincia.php index 3e8406e..7f95a77 100644 --- a/app/src/Repository/Provincia.php +++ b/app/src/Repository/Provincia.php @@ -3,6 +3,7 @@ namespace Incoviba\Repository; use Incoviba\Common\Define; use Incoviba\Common\Ideal; +use Incoviba\Common\Implement; use Incoviba\Model; class Provincia extends Ideal\Repository @@ -15,14 +16,11 @@ class Provincia extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'descripcion' => [], - 'region' => [ - 'function' => function($data) { + $map = (new Implement\Repository\MapperParser(['descripcion'])) + ->register('region', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { return $this->regionRepository->fetchById($data['region']); - } - ] - ]; + })); return $this->parseData(new Model\Provincia(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/Proyecto.php b/app/src/Repository/Proyecto.php index 0c8d4e8..b8e6dff 100644 --- a/app/src/Repository/Proyecto.php +++ b/app/src/Repository/Proyecto.php @@ -3,8 +3,8 @@ namespace Incoviba\Repository; use Incoviba\Common\Define; use Incoviba\Common\Ideal; +use Incoviba\Common\Implement; use Incoviba\Model; -use Incoviba\Repository; class Proyecto extends Ideal\Repository { @@ -16,40 +16,31 @@ class Proyecto extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'inmobiliaria' => [ - 'function' => function($data) { - return $this->inmobiliariaRepository->fetchById($data['inmobiliaria']); - } - ], - 'descripcion' => [], - 'direccion' => [ - 'function' => function($data) { - return $this->direccionRepository->fetchById($data['direccion']); - } - ], - 'superficie_terreno' => [ - 'property' => 'terreno', - 'function' => function($data) { + $map = (new Implement\Repository\MapperParser(['descripcion', 'corredor', 'pisos', 'subterraneos'])) + ->register('inmobiliaria', (new Implement\Repository\Mapper()) + ->setFactory((new Implement\Repository\Factory()) + ->setCallable([$this->inmobiliariaRepository, 'fetchById']) + ->setArgs([$data['inmobiliaria']]))) + ->register('direccion', (new Implement\Repository\Mapper()) + ->setFactory((new Implement\Repository\Factory()) + ->setCallable([$this->inmobiliariaRepository, 'fetchById']) + ->setArgs([$data['inmobiliaria']]))) + ->register('superficie_terreno', (new Implement\Repository\Mapper()) + ->setProperty('terreno') + ->setFunction(function($data) { $terreno = new Model\Proyecto\Terreno(); $terreno->superficie = $data['superficie_terreno']; $terreno->valor = $data['valor_terreno']; return $terreno; - } - ], - 'superficie_sobre_nivel' => [ - 'property' => 'superficie', - 'function' => function($data) { + })) + ->register('superficie_sobre_nivel', (new Implement\Repository\Mapper()) + ->setProperty('superficie') + ->setFunction(function($data) { $superficie = new Model\Proyecto\Superficie(); $superficie->sobre_nivel = $data['superficie_sobre_nivel']; $superficie->bajo_nivel = $data['superficie_bajo_nivel']; return $superficie; - } - ], - 'corredor' => [], - 'pisos' => [], - 'subterraneos' => [] - ]; + })); return $this->parseData(new Model\Proyecto(), $data, $map); } public function save(Define\Model $model): Define\Model @@ -57,7 +48,7 @@ class Proyecto extends Ideal\Repository $model->id = $this->saveNew( ['inmobiliaria', 'descripcion', 'direccion', 'superficie_terreno', 'valor_terreno', 'corredor', 'superficie_sobre_nivel', 'superficie_bajo_nivel', 'pisos', 'subterraneos'], - [$model->inmobiliaria->rut, $model->descripcion, $model->direccion->id, $model->terreno->superficie, + [$model->inmobiliaria()->rut, $model->descripcion, $model->direccion()->id, $model->terreno->superficie, $model->terreno->valor, $model->corredor, $model->superficie->sobre_nivel, $model->superficie->bajo_nivel, $model->pisos, $model->subterraneos] ); diff --git a/app/src/Repository/Proyecto/Elemento.php b/app/src/Repository/Proyecto/Elemento.php new file mode 100644 index 0000000..119fb6f --- /dev/null +++ b/app/src/Repository/Proyecto/Elemento.php @@ -0,0 +1,34 @@ +setTable('tipo_elemento'); + } + + public function create(?array $data = null): Define\Model + { + $map = (new Implement\Repository\MapperParser(['descripcion', 'abreviacion', 'orden'])); + return $this->parseData(new Model\Proyecto\Elemento(), $data, $map); + } + public function save(Define\Model $model): Define\Model + { + $model->id = $this->saveNew( + ['descripcion', 'abreviacion', 'orden'], + [$model->descripcion, $model->abreviacion, $model->orden] + ); + return $model; + } + public function edit(Define\Model $model, array $new_data): Define\Model + { + return $this->update($model, ['descripcion', 'abreviacion', 'orden'], $new_data); + } +} diff --git a/app/src/Repository/Proyecto/ProyectoTipoUnidad.php b/app/src/Repository/Proyecto/ProyectoTipoUnidad.php index 1500f26..4beb4ac 100644 --- a/app/src/Repository/Proyecto/ProyectoTipoUnidad.php +++ b/app/src/Repository/Proyecto/ProyectoTipoUnidad.php @@ -3,6 +3,7 @@ namespace Incoviba\Repository\Proyecto; use Incoviba\Common\Define; use Incoviba\Common\Ideal; +use Incoviba\Common\Implement; use Incoviba\Model; use Incoviba\Repository; @@ -16,27 +17,18 @@ class ProyectoTipoUnidad extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'proyecto' => [ - 'function' => function($data) { + $map = (new Implement\Repository\MapperParser(['nombre', 'abreviacion', 'logia', 'terraza', 'descripcion'])) + ->register('proyecto', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { return $this->proyectoRepository->fetchById($data['proyecto']); - } - ], - 'tipo' => [ - 'property' => 'tipoUnidad', - 'function' => function($data) { + })) + ->register('tipo', (new Implement\Repository\Mapper()) + ->setProperty('tipoUnidad') + ->setFunction(function($data) { return $this->tipoUnidadRepository->fetchById($data['tipo']); - } - ], - 'nombre' => [], - 'abreviacion' => [], - 'm2' => [ - 'property' => 'util' - ], - 'logia' => [], - 'terraza' => [], - 'descripcion' => [] - ]; + })) + ->register('m2', (new Implement\Repository\Mapper()) + ->setProperty('util')); return $this->parseData(new Model\Proyecto\ProyectoTipoUnidad(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/Proyecto/TipoTipologia.php b/app/src/Repository/Proyecto/TipoTipologia.php new file mode 100644 index 0000000..3bf1a9a --- /dev/null +++ b/app/src/Repository/Proyecto/TipoTipologia.php @@ -0,0 +1,34 @@ +setTable('tipologia'); + } + + public function create(?array $data = null): Define\Model + { + $map = (new Implement\Repository\MapperParser(['descripcion'])); + return $this->parseData(new Model\Proyecto\TipoTipologia(), $data, $map); + } + public function save(Define\Model $model): Define\Model + { + $model->id = $this->saveNew( + ['descripcion'], + [$model->descripcion] + ); + return $model; + } + public function edit(Define\Model $model, array $new_data): Define\Model + { + return $this->update($model, ['descripcion'], $new_data); + } +} diff --git a/app/src/Repository/Proyecto/Tipologia.php b/app/src/Repository/Proyecto/Tipologia.php new file mode 100644 index 0000000..3db6ed4 --- /dev/null +++ b/app/src/Repository/Proyecto/Tipologia.php @@ -0,0 +1,58 @@ +setTable('tipo_tipologia'); + } + + public function create(?array $data = null): Define\Model + { + $map = (new Implement\Repository\MapperParser(['cantidad'])) + ->register('tipo', (new Implement\Repository\Mapper()) + ->setProperty('proyectoTipoUnidad') + ->setFunction(function($data) { + return $this->proyectoTipoUnidadRepository->fetchById($data['tipo']); + })) + ->register('tipologia', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { + return $this->tipoTipologiaRepository->fetchById($data['tipologia']); + })) + ->register('elemento', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { + return $this->elementoRepository->fetchById($data['elemento']); + })); + return $this->parseData(new Model\Proyecto\Tipologia(), $data, $map); + } + public function save(Define\Model $model): Define\Model + { + $model->id = $this->saveNew( + ['tipo', 'tipologia', 'cantidad', 'elemento'], + [$model->proyectoTipoUnidad->id, $model->tipoTipologia->id, $model->cantidad, $model->elemento->id] + ); + return $model; + } + public function edit(Define\Model $model, array $new_data): Define\Model + { + return $this->update($model, ['tipo', 'tipologia', 'cantidad', 'elemento'], $new_data); + } + + public function fetchByProyectoTipoUnidad(int $proyecto_tipo_unidad_id): array + { + $query = "SELECT * FROM `{$this->getTable()}` WHERE `tipo` = ?"; + return $this->fetchMany($query, [$proyecto_tipo_unidad_id]); + } +} diff --git a/app/src/Repository/Region.php b/app/src/Repository/Region.php index 34287e6..3932ea3 100644 --- a/app/src/Repository/Region.php +++ b/app/src/Repository/Region.php @@ -3,6 +3,7 @@ namespace Incoviba\Repository; use Incoviba\Common\Define; use Incoviba\Common\Ideal; +use Incoviba\Common\Implement; use Incoviba\Model; class Region extends Ideal\Repository @@ -15,11 +16,7 @@ class Region extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'descripcion' => [], - 'numeral' => [], - 'numeracion' => [] - ]; + $map = new Implement\Repository\MapperParser(['descripcion', 'numeral', 'numeracion']); return $this->parseData(new Model\Region(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/User.php b/app/src/Repository/User.php index 97ae151..4211bc6 100644 --- a/app/src/Repository/User.php +++ b/app/src/Repository/User.php @@ -3,6 +3,7 @@ namespace Incoviba\Repository; use Incoviba\Common\Define; use Incoviba\Common\Ideal; +use Incoviba\Common\Implement; use Incoviba\Model; class User extends Ideal\Repository @@ -15,15 +16,8 @@ class User extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'name' => [], - 'password' => [], - 'enabled' => [ - 'function' => function($data) { - return $data['enabled'] != 0; - } - ] - ]; + $map = (new Implement\Repository\MapperParser(['name', 'password'])) + ->register('enabled', new Implement\Repository\Mapper\Boolean('enabled')); return $this->parseData(new Model\User(), $data, $map); } diff --git a/app/src/Repository/Venta.php b/app/src/Repository/Venta.php index 36338d0..f594e6f 100644 --- a/app/src/Repository/Venta.php +++ b/app/src/Repository/Venta.php @@ -4,7 +4,9 @@ namespace Incoviba\Repository; use DateTimeImmutable; use Incoviba\Common\Ideal; use Incoviba\Common\Define; +use Incoviba\Common\Implement; use Incoviba\Model; +use Incoviba\Service; class Venta extends Ideal\Repository { @@ -12,13 +14,13 @@ class Venta extends Ideal\Repository Define\Connection $connection, protected Venta\Propietario $propietarioRepository, protected Venta\Propiedad $propiedadRepository, - protected Venta\Pie $pieRepository, + protected Service\Venta\Pie $pieService, protected Venta\BonoPie $bonoPieRepository, protected Venta\Credito $creditoRepository, protected Venta\Escritura $escrituraRepository, protected Venta\Subsidio $subsidioRepository, protected Venta\Entrega $entregaRepository, - protected Venta\Pago $pagoRepository + protected Service\Venta\Pago $pagoService ) { parent::__construct($connection); @@ -27,95 +29,96 @@ class Venta extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'propietario' => [ - 'function' => function($data) { - return $this->propietarioRepository->fetchById($data['propietario']); - } - ], - 'propiedad' => [ - 'function' => function($data) { - return $this->propiedadRepository->fetchById($data['propiedad']); - } - ], - 'pie' => [ - 'property' => 'formaPago', - 'function' => function($data) { - $fp = new Model\Venta\FormaPago(); - $map = [ - 'pie' => [ - 'repository' => $this->pieRepository - ], - 'bono_pie' => [ - 'property' => 'bonoPie', - 'repository' => $this->bonoPieRepository - ], - 'credito' => [ - 'repository' => $this->creditoRepository - ], - 'escritura' => [ - 'repository' => $this->escrituraRepository - ], - 'subsidio' => [ - 'repository' => $this->subsidioRepository - ] - ]; - foreach ($map as $column => $settings) { - if ($data[$column] !== null and $data[$column] !== 0) { - $fp->{$settings['property'] ?? $column} = $settings['repository']->fetchById($data[$column]); + $map = (new Implement\Repository\MapperParser()) + ->register('propietario', (new Implement\Repository\Mapper()) + ->setFactory((new Implement\Repository\Factory()) + ->setCallable([$this->propietarioRepository, 'fetchById']) + ->setArgs([$data['propietario']]))) + ->register('propiedad', (new Implement\Repository\Mapper()) + ->setFactory((new Implement\Repository\Factory()) + ->setCallable([$this->propiedadRepository, 'fetchById']) + ->setArgs([$data['propiedad']]))) + ->register('pie', (new Implement\Repository\Mapper()) + ->setProperty('formaPago') + ->setFactory((new Implement\Repository\Factory()) + ->setCallable(function($repositories, $data) { + $fp = new Model\Venta\FormaPago(); + $map = [ + 'pie' => [ + 'service' => $repositories->pieService + ], + 'bono_pie' => [ + 'property' => 'bonoPie', + 'repository' => $repositories->bonoPieRepository + ], + 'credito' => [ + 'repository' => $repositories->creditoRepository + ], + 'escritura' => [ + 'repository' => $repositories->escrituraRepository + ], + 'subsidio' => [ + 'repository' => $repositories->subsidioRepository + ], + 'devolucion' => [ + 'service' => $repositories->pagoService + ] + ]; + foreach ($map as $column => $settings) { + if ($data[$column] !== null and $data[$column] !== 0) { + if (isset($settings['repository'])) { + $fp->{$settings['property'] ?? $column} = $settings['repository']->fetchById($data[$column]); + continue; + } + $fp->{$settings['property'] ?? $column} = $settings['service']->getById($data[$column]); + continue; + } + $fp->{$settings['property'] ?? $column} = null; } - } - return $fp; - } - ], - 'escriturado' => [ - 'function' => function($data) { + return $fp; + }) + ->setArgs([(object) [ + 'pieService' => $this->pieService, + 'bonoPieRepository' => $this->bonoPieRepository, + 'creditoRepository' => $this->creditoRepository, + 'escrituraRepository' => $this->escrituraRepository, + 'subsidioRepository' => $this->subsidioRepository, + 'pagoService' => $this->pagoService + ], $data]))) + /*->register('escriturado', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { return $data['escritura'] !== null; - } - ], - 'entrega' => [ - 'function' => function($data) { - if ($data['entrega'] !== null and $data['entrega'] !== 0) { - return $this->entregaRepository->fetchById($data['entrega']); - } - } - ], - 'entregado' => [ - 'function' => function($data) { - if ($data['entrega'] !== null and $data['entrega'] !== 0) { - return $data['entrega'] !== null; - } - } - ], - 'fecha' => [ - 'function' => function($data) { - return new DateTimeImmutable($data['fecha']); - } - ], - 'valor_uf' => [ - 'property' => 'valor' - ], - //'estado' => [], - 'fecha_ingreso' => [ - 'property' => 'fechaIngreso', - 'function' => function($data) { - return new DateTimeImmutable($data['fecha_ingreso']); - } - ], - /*'avalchile' => [ - - ],*/ - //'agente', - //'uf', - 'relacionado' => [ - 'function' => function($data) { - return $data['relacionado'] !== 0; - } - ], - //'promocion', - //'resciliacion', - //'devolucion' - ]; + }))*/ + ->register('entrega', (new Implement\Repository\Mapper()) + ->setFactory((new Implement\Repository\Factory()) + ->setCallable(function($entrega_id) { + if ($entrega_id !== null and $entrega_id !== 0) { + return $this->entregaRepository->fetchById($entrega_id); + } + return null; + }) + ->setArgs([$data['entrega']]))) + /*->register('entregado', (new Implement\Repository\Mapper()) + ->setFactory((new Implement\Repository\Factory()) + ->setCallable(function($entrega_id) { + if ($entrega_id !== null and $entrega_id !== 0) { + return $entrega_id != null; + } + return false; + }) + ->setArgs([$data['entrega']])))*/ + ->register('fecha', new Implement\Repository\Mapper\DateTime('fecha')) + ->register('valor_uf', (new Implement\Repository\Mapper()) + ->setProperty('valor')) + //->register('estado') + ->register('fecha_ingreso', new Implement\Repository\Mapper\DateTime('fecha_ingreso', 'fechaIngreso')) + //->register('avalchile') + //->register('agente') + //->register('uf') + ->register('relacionado', new Implement\Repository\Mapper\Boolean('relacionado')); + //->register('promocion') + //->register('resciliacion') + //->register('devolucion'); return $this->parseData(new Model\Venta(), $data, $map); } public function save(Define\Model $model): Define\Model @@ -124,9 +127,9 @@ class Venta extends Ideal\Repository ['propietario', 'propiedad', 'pie', 'bono_pie', 'credito', 'escritura', 'subsidio', 'escriturado', 'entrega', 'entregado', 'fecha', 'valor_uf', 'estado', 'fecha_ingreso', 'avalchile', 'agente', 'uf', 'relacionado', 'promocion', 'resciliacion', 'devolucion'], - [$model->propietario->rut, $model->propiedad->id, $model->formaPago->Pie?->id, $model->formaPago->bonoPie?->id, - $model->formaPago->credito?->id, $model->formaPago->escritura?->id, $model->formaPago->subsidio?->id, - $model->formaPago->escritura !== null ? 1 : 0, null, 0, $model->fecha->format('Y-m-d'), $model->valor, + [$model->propietario->rut, $model->propiedad()->id, $model->formaPago()->Pie?->id, $model->formaPago()->bonoPie?->id, + $model->formaPago()->credito?->id, $model->formaPago()->escritura?->id, $model->formaPago()->subsidio?->id, + $model->formaPago()->escritura !== null ? 1 : 0, null, 0, $model->fecha->format('Y-m-d'), $model->valor, $model->currentEstado->vigente ? 1 : 0, $model->fechaIngreso->format('Y-m-d'), '', null, 0, $model->relacionado ? 1 : 0, null, null, null] ); @@ -152,4 +155,17 @@ WHERE ptu.`proyecto` = ? AND tev.`activa` GROUP BY a.`id`"; return $this->fetchMany($query, [$proyecto_id]); } + public function fetchByProyectoAndUnidad(string $proyecto_nombre, int $unidad_descripcion): Define\Model + { + $query = "SELECT a.* +FROM `{$this->getTable()}` a + JOIN `propiedad_unidad` pu ON pu.`propiedad` = a.`propiedad` + JOIN `unidad` ON `unidad`.`id` = pu.`unidad` AND pu.`principal` = 1 + JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = `unidad`.`pt` + JOIN `proyecto` ON `proyecto`.`id` = ptu.`proyecto` + JOIN (SELECT e1.* FROM `estado_venta` e1 JOIN (SELECT MAX(`id`) AS 'id', `venta` FROM `estado_venta` GROUP BY `venta`) e0 ON e0.`id` = e1.`id`) ev ON ev.`venta` = a.`id` + JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado` +WHERE `proyecto`.`descripcion` = ? AND `unidad`.`descripcion` = ? AND tev.`activa`"; + return $this->fetchOne($query, [$proyecto_nombre, $unidad_descripcion]); + } } diff --git a/app/src/Repository/Venta/BonoPie.php b/app/src/Repository/Venta/BonoPie.php index 4f5a9bd..02332b8 100644 --- a/app/src/Repository/Venta/BonoPie.php +++ b/app/src/Repository/Venta/BonoPie.php @@ -3,6 +3,7 @@ namespace Incoviba\Repository\Venta; use Incoviba\Common\Ideal; use Incoviba\Common\Define; +use Incoviba\Common\Implement; use Incoviba\Model; class BonoPie extends Ideal\Repository @@ -15,13 +16,11 @@ class BonoPie extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'pago' => [ - 'function' => function($data) { + $map = (new Implement\Repository\MapperParser()) + ->register('pago', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { return $this->pagoRepository->fetchById($data['pago']); - } - ] - ]; + })); return $this->parseData(new Model\Venta\BonoPie(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/Venta/Cierre.php b/app/src/Repository/Venta/Cierre.php index 6de51f9..02dba77 100644 --- a/app/src/Repository/Venta/Cierre.php +++ b/app/src/Repository/Venta/Cierre.php @@ -1,13 +1,14 @@ [ - 'function' => function($data) { + $map = (new Implement\Repository\MapperParser(['precio'])) + ->register('proyecto', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { return $this->proyectoRepository->fetchById($data['proyecto']); - } - ], - 'precio' => [], - 'fecha' => [ - 'property' => 'dateTime', - 'function' => function($data) { - return new DateTimeImmutable($data['fecha']); - } - ], - 'relacionado' => [ - 'function' => function($data) { - return $data['relacionado'] !== 0; - } - ], - 'propietario' => [ - 'function' => function($data) { + })) + ->register('fecha', new Implement\Repository\Mapper\DateTime('fecha', 'dateTime')) + ->register('relacionado', new Implement\Repository\Mapper\Boolean('relacionado')) + ->register('propietario', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { return $this->propietarioRepository->fetchById($data['propietario']); - } - ] - ]; + })); return $this->parseData(new Model\Venta\Cierre(), $data, $map); } diff --git a/app/src/Repository/Venta/Comentario.php b/app/src/Repository/Venta/Comentario.php new file mode 100644 index 0000000..9197264 --- /dev/null +++ b/app/src/Repository/Venta/Comentario.php @@ -0,0 +1,43 @@ +setTable('comentario'); + } + + public function create(?array $data = null): Define\Model + { + $map = (new Implement\Repository\MapperParser(['texto'])) + ->register('fecha', new Implement\Repository\Mapper\DateTime('fecha')) + ->register('estado', new Implement\Repository\Mapper\Boolean('estado', 'activo')); + return $this->parseData(new Model\Venta\Comentario(), $data, $map); + } + public function save(Define\Model $model): Define\Model + { + $model->id = $this->saveNew( + ['fecha', 'texto', 'estado'], + [$model->fecha->format('Y-m-d'), $model->texto, $model->activo ? 1 : 0] + ); + return $model; + } + public function edit(Define\Model $model, array $new_data): Define\Model + { + return $this->update($model, ['fecha', 'texto', 'estado'], $new_data); + } + + public function fetchByVenta(int $venta_id): array + { + $query = "SELECT * FROM `{$this->getTable()}` WHERE `venta` = ? AND `estado` = 1 ORDER BY `fecha` DESC"; + return $this->fetchMany($query, [$venta_id]); + } +} diff --git a/app/src/Repository/Venta/Credito.php b/app/src/Repository/Venta/Credito.php index ed3cd9d..b5529c7 100644 --- a/app/src/Repository/Venta/Credito.php +++ b/app/src/Repository/Venta/Credito.php @@ -3,11 +3,13 @@ namespace Incoviba\Repository\Venta; use Incoviba\Common\Ideal; use Incoviba\Common\Define; +use Incoviba\Common\Implement; use Incoviba\Model; +use Incoviba\Service; class Credito extends Ideal\Repository { - public function __construct(Define\Connection $connection, protected Pago $pagoRepository) + public function __construct(Define\Connection $connection, protected Service\Venta\Pago $pagoService) { parent::__construct($connection); $this->setTable('credito'); @@ -15,13 +17,11 @@ class Credito extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'pago' => [ - 'function' => function($data) { - return $this->pagoRepository->fetchById($data['pago']); - } - ] - ]; + $map = (new Implement\Repository\MapperParser()) + ->register('pago', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { + return $this->pagoService->getById($data['pago']); + })); return $this->parseData(new Model\Venta\Credito(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/Venta/Cuota.php b/app/src/Repository/Venta/Cuota.php index 341d554..8dc4c0c 100644 --- a/app/src/Repository/Venta/Cuota.php +++ b/app/src/Repository/Venta/Cuota.php @@ -1,17 +1,23 @@ setTable('cuota'); @@ -19,68 +25,33 @@ class Cuota extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'pie' => [ - 'function' => function($data) { + $map = (new Implement\Repository\MapperParser(['valor', 'uf', 'numero'])) + ->register('pie', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { return $this->pieRepository->fetchById($data['pie']); - } - ], - 'fecha' => [ - 'function' => function($data) { - return new DateTimeImmutable($data['fecha']); - } - ], - 'valor' => [], - 'estado' => [ - 'function' => function($data) { - return $data['estado'] !== 0; - } - ], - 'banco' => [ - 'function' => function($data) { + })) + ->register('fecha', new Implement\Repository\Mapper\DateTime('fecha')) + ->register('estado', new Implement\Repository\Mapper\Boolean('estado')) + ->register('banco', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { if ($data['banco'] === null or $data['banco'] === '') { return null; } return $this->bancoRepository->fetchById($data['banco']); - } - ], - 'fecha_pago' => [ - 'property' => 'fechaPago', - 'function' => function($data) { - if ($data['fecha_pago'] === null) { - return null; - } - return new DateTimeImmutable($data['fecha_pago']); - } - ], - 'abonado' => [ - 'function' => function($data) { - if ($data['abonado'] === null) { - return null; - } - return $data['abonado'] !== 0; - } - ], - 'fecha_abonado' => [ - 'property' => 'fechaAbonado', - 'function' => function($data) { - if ($data['fecha_abonado'] === null) { - return null; - } - return new DateTimeImmutable($data['fecha_abonado']); - } - ], - 'uf' => [], - 'pago' => [ - 'function' => function($data) { + })) + ->register('fecha_pago', (new Implement\Repository\Mapper\DateTime('fecha_pago', 'fechaPago')) + ->setDefault(null)) + ->register('abonado', (new Implement\Repository\Mapper\Boolean('abonado')) + ->setDefault(null)) + ->register('fecha_abonado', (new Implement\Repository\Mapper\DateTime('fecha_abonado', 'fechaAbonado')) + ->setDefault(null)) + ->register('pago', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { if ($data['pago'] === null) { return null; } - return $this->pagoRepository->fetchById($data['pago']); - } - ], - 'numero' => [] - ]; + return $this->pagoService->getById($data['pago']); + })); return $this->parseData(new Model\Venta\Cuota(), $data, $map); } @@ -158,4 +129,14 @@ GROUP BY `pago`.`fecha`, `proyecto`.`descripcion` ORDER BY `pago`.`fecha`, `proyecto`.`descripcion`"; return $this->fetchAsArray($query); } + public function fetchVigenteByPie(int $pie_id): array + { + $query = "SELECT a.* +FROM `{$this->getTable()}` a + JOIN `pago` ON `pago`.`id` = a.`pago` + JOIN (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 ON ep.`pago` = `pago`.`id` + JOIN `tipo_estado_pago` tep ON tep.`id` = ep.`estado` +WHERE a.`pie` = ? AND tep.`active` = 1"; + return $this->fetchMany($query, [$pie_id]); + } } diff --git a/app/src/Repository/Venta/Entrega.php b/app/src/Repository/Venta/Entrega.php index e91935f..0859eeb 100644 --- a/app/src/Repository/Venta/Entrega.php +++ b/app/src/Repository/Venta/Entrega.php @@ -3,6 +3,7 @@ namespace Incoviba\Repository\Venta; use Incoviba\Common\Ideal; use Incoviba\Common\Define; +use Incoviba\Common\Implement; use Incoviba\Model; class Entrega extends Ideal\Repository @@ -15,8 +16,8 @@ class Entrega extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = ['fecha', 'fondo_operacion', 'fondo_reserva', 'fecha_fondo_operacion', 'fecha_fondo_reserva', - 'pago_operacion', 'pago_reserva']; + $map = new Implement\Repository\MapperParser(['fecha', 'fondo_operacion', 'fondo_reserva', 'fecha_fondo_operacion', 'fecha_fondo_reserva', + 'pago_operacion', 'pago_reserva']); return $this->parseData(new Model\Venta\Entrega(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/Venta/Escritura.php b/app/src/Repository/Venta/Escritura.php index 730a991..2a44ce1 100644 --- a/app/src/Repository/Venta/Escritura.php +++ b/app/src/Repository/Venta/Escritura.php @@ -1,13 +1,16 @@ setTable('escritura'); @@ -15,20 +18,19 @@ class Escritura extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'pago' => [ - 'function' => function($data) { - return $this->pagoRepository->fetchById($data['pago']); - } - ] - ]; + $map = (new Implement\Repository\MapperParser()) + ->register('pago', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { + return $this->pagoService->getById($data['pago']); + })) + ->register('fecha', new Implement\Repository\Mapper\DateTime('fecha')); return $this->parseData(new Model\Venta\Escritura(), $data, $map); } public function save(Define\Model $model): Define\Model { $model->id = $this->saveNew( ['valor', 'fecha', 'uf', 'abonado', 'fecha_abono', 'pago'], - [$model->pago->valor, $model->pago->fecha->format('Y-m-d'), $model->pago->uf, null, null, $model->pago->id] + [$model->pago->valor, $model->fecha->format('Y-m-d'), $model->pago->uf, null, null, $model->pago->id] ); return $model; } diff --git a/app/src/Repository/Venta/EstadoCierre.php b/app/src/Repository/Venta/EstadoCierre.php index ed82284..2b183a0 100644 --- a/app/src/Repository/Venta/EstadoCierre.php +++ b/app/src/Repository/Venta/EstadoCierre.php @@ -4,6 +4,7 @@ namespace Incoviba\Repository\Venta; use DateTimeImmutable; use Incoviba\Common\Ideal; use Incoviba\Common\Define; +use Incoviba\Common\Implement; use Incoviba\Model; use Incoviba\Repository; @@ -17,24 +18,17 @@ class EstadoCierre extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'cierre' => [ - 'function' => function($data) { + $map = (new Implement\Repository\MapperParser()) + ->register('cierre', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { return $this->cierreRepository->fetchById($data['cierre']); - } - ], - 'tipo' => [ - 'property' => 'tipoEstadoCierre', - 'function' => function($data) { + })) + ->register('tipo', (new Implement\Repository\Mapper()) + ->setProperty('tipoEstadoCierre') + ->setFunction(function($data) { return $this->tipoEstadoCierreRepository->fetchById($data['tipo']); - } - ], - 'fecha' => [ - 'function' => function($data) { - return new DateTimeImmutable($data['fecha']); - } - ] - ]; + })) + ->register('fecha', new Implement\Repository\Mapper\DateTime('fecha')); return $this->parseData(new Model\Venta\EstadoCierre(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/Venta/EstadoPago.php b/app/src/Repository/Venta/EstadoPago.php index 406647f..8fb1c78 100644 --- a/app/src/Repository/Venta/EstadoPago.php +++ b/app/src/Repository/Venta/EstadoPago.php @@ -4,6 +4,7 @@ namespace Incoviba\Repository\Venta; use DateTimeImmutable; use Incoviba\Common\Define; use Incoviba\Common\Ideal; +use Incoviba\Common\Implement; use Incoviba\Model; use Incoviba\Repository; @@ -19,24 +20,17 @@ class EstadoPago extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'pago' => [ - 'function' => function($data) { + $map = (new Implement\Repository\MapperParser()) + ->register('pago', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { return $this->pagoRepository->fetchById($data['pago']); - } - ], - 'estado' => [ - 'property' => 'tipoEstadoPago', - 'function' => function($data) { + })) + ->register('estado', (new Implement\Repository\Mapper()) + ->setProperty('tipoEstadoPago') + ->setFunction(function($data) { return $this->tipoEstadoPagoRepository->fetchById($data['estado']); - } - ], - 'fecha' => [ - 'function' => function($data) { - return new DateTimeImmutable($data['fecha']); - } - ] - ]; + })) + ->register('fecha', new Implement\Repository\Mapper\DateTime('fecha')); return $this->parseData(new Model\Venta\EstadoPago(), $data, $map); } @@ -59,6 +53,14 @@ class EstadoPago extends Ideal\Repository $query = "SELECT * FROM `{$this->getTable()}` WHERE `pago` = ?"; return $this->fetchMany($query, [$pago_id]); } + public function fetchCurrentByPago(int $pago_id): Define\Model + { + $query = "SELECT a.* +FROM `{$this->getTable()}` a + JOIN (SELECT MAX(`id`) AS 'id', `pago` FROM `{$this->getTable()}` GROUP BY `pago`) e0 ON e0.`id` = a.`id` +WHERE a.`pago` = ?"; + return $this->fetchOne($query, [$pago_id]); + } public function fetchByPagoAndEstado(int $pago_id, int $estado_id): Define\Model { $query = "SELECT * FROM `{$this->getTable()}` WHERE `pago` = ? AND `estado` = ?"; diff --git a/app/src/Repository/Venta/EstadoPrecio.php b/app/src/Repository/Venta/EstadoPrecio.php index c223e30..e1e0858 100644 --- a/app/src/Repository/Venta/EstadoPrecio.php +++ b/app/src/Repository/Venta/EstadoPrecio.php @@ -4,6 +4,7 @@ namespace Incoviba\Repository\Venta; use DateTimeImmutable; use Incoviba\Common\Ideal; use Incoviba\Common\Define; +use Incoviba\Common\Implement; use Incoviba\Model; use Incoviba\Repository; @@ -17,24 +18,17 @@ class EstadoPrecio extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'precio' => [ - 'function' => function($data) { + $map = (new Implement\Repository\MapperParser()) + ->register('precio', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { return $this->precioRepository->fetchById($data['precio']); - } - ], - 'estado' => [ - 'property' => 'tipoEstadoPrecio', - 'function' => function($data) { + })) + ->register('estado', (new Implement\Repository\Mapper()) + ->setProperty('tipoEstadoPrecio') + ->setFunction(function($data) { return $this->tipoEstadoPrecioRepository->fetchById($data['estado']); - } - ], - 'fecha' => [ - 'function' => function($data) { - return new DateTimeImmutable($data['fecha']); - } - ] - ]; + })) + ->register('fecha', new Implement\Repository\Mapper\DateTime('fecha')); return $this->parseData(new Model\Venta\EstadoPrecio(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/Venta/EstadoVenta.php b/app/src/Repository/Venta/EstadoVenta.php index 15143d3..d0637fc 100644 --- a/app/src/Repository/Venta/EstadoVenta.php +++ b/app/src/Repository/Venta/EstadoVenta.php @@ -4,6 +4,7 @@ namespace Incoviba\Repository\Venta; use DateTimeImmutable; use Incoviba\Common\Ideal; use Incoviba\Common\Define; +use Incoviba\Common\Implement; use Incoviba\Model; use Incoviba\Repository; @@ -18,24 +19,17 @@ class EstadoVenta extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'venta' => [ - 'function' => function($data) { + $map = (new Implement\Repository\MapperParser()) + ->register('venta', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { return $this->ventaRepository->fetchById($data['venta']); - } - ], - 'estado' => [ - 'property' => 'tipoEstadoVenta', - 'function' => function($data) { + })) + ->register('estado', (new Implement\Repository\Mapper()) + ->setProperty('tipoEstadoVenta') + ->setFunction(function($data) { return $this->tipoEstadoVentaRepository->fetchById($data['estado']); - } - ], - 'fecha' => [ - 'function' => function($data) { - return new DateTimeImmutable($data['fecha']); - } - ] - ]; + })) + ->register('fecha', new Implement\Repository\Mapper\DateTime('fecha')); return $this->parseData(new Model\Venta\EstadoVenta(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/Venta/Pago.php b/app/src/Repository/Venta/Pago.php index ec9b6e6..c164417 100644 --- a/app/src/Repository/Venta/Pago.php +++ b/app/src/Repository/Venta/Pago.php @@ -4,6 +4,7 @@ namespace Incoviba\Repository\Venta; use DateTimeImmutable; use Incoviba\Common\Define; use Incoviba\Common\Ideal; +use Incoviba\Common\Implement; use Incoviba\Model; use Incoviba\Repository; @@ -17,45 +18,31 @@ class Pago extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'valor' => [], - 'banco' => [ - 'function' => function($data) { + $map = (new Implement\Repository\MapperParser(['valor', 'identificador', 'uf', 'pagador'])) + ->register('banco', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { if ($data['banco'] === null or $data['banco'] === 0) { return null; } return $this->bancoRepository->fetchById($data['banco']); - } - ], - 'tipo' => [ - 'property' => 'tipoPago', - 'function' => function($data) { + })) + ->register('tipo', (new Implement\Repository\Mapper()) + ->setProperty('tipoPago') + ->setFunction(function($data) { if ($data['tipo'] === null) { return null; } return $this->tipoPagoRepository->fetchById($data['tipo']); - } - ], - 'identificador' => [], - 'fecha' => [ - 'function' => function($data) { - if ($data['fecha'] === null) { - return null; - } - return new DateTimeImmutable($data['fecha']); - } - ], - 'uf' => [], - 'pagador' => [], - 'asociado' => [ - 'function' => function($data) { + })) + ->register('fecha', (new Implement\Repository\Mapper\DateTime('fecha')) + ->setDefault(null)) + ->register('asociado', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { if ($data['asociado'] === null) { return null; } return $this->fetchById($data['asociado']); - } - ] - ]; + })); return $this->parseData(new Model\Venta\Pago(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/Venta/Pie.php b/app/src/Repository/Venta/Pie.php index 37b8d2c..6d66b85 100644 --- a/app/src/Repository/Venta/Pie.php +++ b/app/src/Repository/Venta/Pie.php @@ -4,8 +4,8 @@ namespace Incoviba\Repository\Venta; use DateTimeImmutable; use Incoviba\Common\Define; use Incoviba\Common\Ideal; +use Incoviba\Common\Implement; use Incoviba\Model; -use Incoviba\Repository; class Pie extends Ideal\Repository { @@ -17,32 +17,22 @@ class Pie extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'fecha' => [ - 'function' => function($data) { - return new DateTimeImmutable($data['fecha']); - } - ], - 'valor' => [], - 'uf' => [], - 'cuotas' => [], - 'asociado' => [ - 'function' => function($data) { + $map = (new Implement\Repository\MapperParser(['valor', 'uf', 'cuotas'])) + ->register('fecha', new Implement\Repository\Mapper\DateTime('fecha')) + ->register('asociado', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { if ($data['asociado'] === null or $data['asociado'] === 0) { return null; } return $this->fetchById($data['asociado']); - } - ], - 'reajuste' => [ - 'function' => function($data) { + })) + ->register('reajuste', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { if ($data['reajuste'] === null or $data['reajuste'] === 0) { return null; } return $this->pagoRepository->fetchById($data['reajuste']); - } - ] - ]; + })); return $this->parseData(new Model\Venta\Pie(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/Venta/Precio.php b/app/src/Repository/Venta/Precio.php index 3373c84..f569afe 100644 --- a/app/src/Repository/Venta/Precio.php +++ b/app/src/Repository/Venta/Precio.php @@ -1,9 +1,9 @@ [ - 'function' => function($data) { + $map = (new Implement\Repository\MapperParser(['valor'])) + ->register('unidad', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { return $this->unidadRepository->fetchById($data['unidad']); - } - ], - 'valor' => [] - ]; + })); return $this->parseData(new Model\Venta\Precio(), $data, $map); } public function save(Define\Model $model): Define\Model @@ -53,7 +50,16 @@ WHERE ptu.`proyecto` = ? AND tep.`descripcion` = 'vigente' ORDER BY tu.`orden`, ptu.`nombre`, `unidad`.`subtipo`, LPAD(`unidad`.`descripcion`, 4, '0')"; return $this->fetchMany($query, [$proyecto_id]); } - public function fetchByUnidad(int $unidad_id): Define\Model + public function fetchByUnidad(int $unidad_id): array + { + $query = "SELECT a.* +FROM `{$this->getTable()}` a + JOIN (SELECT e1.* FROM `estado_precio` e1 JOIN (SELECT MAX(`id`) AS 'id', `precio` FROM `estado_precio` GROUP BY `precio`) e0 ON e0.`id` = e1.`id`) ep ON ep.`precio` = a.`id` + JOIN `tipo_estado_precio` tep ON tep.`id` = ep.`estado` +WHERE `unidad` = ?"; + return $this->fetchMany($query, [$unidad_id]); + } + public function fetchVigenteByUnidad(int $unidad_id): Define\Model { $query = "SELECT a.* FROM `{$this->getTable()}` a diff --git a/app/src/Repository/Venta/Propiedad.php b/app/src/Repository/Venta/Propiedad.php index 36baa7e..173da2e 100644 --- a/app/src/Repository/Venta/Propiedad.php +++ b/app/src/Repository/Venta/Propiedad.php @@ -3,11 +3,13 @@ namespace Incoviba\Repository\Venta; use Incoviba\Common\Ideal; use Incoviba\Common\Define; +use Incoviba\Common\Implement; use Incoviba\Model; +use Incoviba\Service; class Propiedad extends Ideal\Repository { - public function __construct(Define\Connection $connection, protected Unidad $unidadRepository) + public function __construct(Define\Connection $connection, protected Service\Venta\Unidad $unidadService) { parent::__construct($connection); $this->setTable('propiedad'); @@ -15,19 +17,13 @@ class Propiedad extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'unidad_principal' => [ - 'property' => 'unidades', - 'function' => function($data) { - return $this->unidadRepository->fetchByPropiedad($data['id']); - } - ], - 'estado' => [ - 'function' => function($data) { - return true; - } - ] - ]; + $map = (new Implement\Repository\MapperParser()) + ->register('unidad_principal', (new Implement\Repository\Mapper()) + ->setProperty('unidades') + ->setFunction(function($data) { + return $this->unidadService->getByPropiedad($data['id']); + })) + ->register('estado', new Implement\Repository\Mapper\Boolean('estado')); return $this->parseData(new Model\Venta\Propiedad(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/Venta/Propietario.php b/app/src/Repository/Venta/Propietario.php index 8e0dce6..eacc4a7 100644 --- a/app/src/Repository/Venta/Propietario.php +++ b/app/src/Repository/Venta/Propietario.php @@ -3,6 +3,7 @@ namespace Incoviba\Repository\Venta; use Incoviba\Common\Define; use Incoviba\Common\Ideal; +use Incoviba\Common\Implement; use Incoviba\Model; use Incoviba\Repository; @@ -21,12 +22,10 @@ class Propietario extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'dv' => [], - 'nombres' => [], - 'apellido_paterno' => [ - 'property' => 'apellidos', - 'function' => function($data) { + $map = (new Implement\Repository\MapperParser(['dv', 'nombres'])) + ->register('apellido_paterno', (new Implement\Repository\Mapper()) + ->setProperty('apellidos') + ->setFunction(function($data) { $arr = [ 'paterno' => $data['apellido_paterno'] ]; @@ -34,35 +33,25 @@ class Propietario extends Ideal\Repository $arr['materno'] = $data['apellido_materno']; } return $arr; - } - ], - 'direccion' => [ - 'property' => 'datos', - 'function' => function($data) { + })) + ->register('direccion', (new Implement\Repository\Mapper()) + ->setProperty('datos') + ->setFunction(function($data) { $datos = new Model\Venta\Datos(); if ($data['direccion'] !== null and $data['direccion'] !== 0) { $datos->direccion = $this->direccionRepository->fetchById($data['direccion']); } return $datos; - } - ], - 'representante' => [ - 'function' => function($data) { + })) + ->register('representante', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { if ($data['representante'] === null or $data['representante'] === 0) { return null; } return $this->fetchById($data['representante']); - } - ], - 'otro' => [ - 'function' => function($data) { - if ($data['otro'] === null) { - return null; - } - return $data['otro'] !== 0; - } - ] - ]; + })) + ->register('otro', (new Implement\Repository\Mapper\Boolean('otro')) + ->setDefault(null)); return $this->parseData(new Model\Venta\Propietario(), $data, $map); } diff --git a/app/src/Repository/Venta/Subsidio.php b/app/src/Repository/Venta/Subsidio.php index 60e0b88..12af4e2 100644 --- a/app/src/Repository/Venta/Subsidio.php +++ b/app/src/Repository/Venta/Subsidio.php @@ -3,6 +3,7 @@ namespace Incoviba\Repository\Venta; use Incoviba\Common\Ideal; use Incoviba\Common\Define; +use Incoviba\Common\Implement; use Incoviba\Model; class Subsidio extends Ideal\Repository @@ -15,7 +16,7 @@ class Subsidio extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = ['pago', 'subsidio']; + $map = new Implement\Repository\MapperParser(['pago', 'subsidio']); return $this->parseData(new Model\Venta\Subsidio(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/Venta/TipoEstadoCierre.php b/app/src/Repository/Venta/TipoEstadoCierre.php index 7883c82..2264d4b 100644 --- a/app/src/Repository/Venta/TipoEstadoCierre.php +++ b/app/src/Repository/Venta/TipoEstadoCierre.php @@ -3,6 +3,7 @@ namespace Incoviba\Repository\Venta; use Incoviba\Common\Ideal; use Incoviba\Common\Define; +use Incoviba\Common\Implement; use Incoviba\Model; class TipoEstadoCierre extends Ideal\Repository @@ -15,14 +16,8 @@ class TipoEstadoCierre extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'descripcion' => [], - 'vigente' => [ - 'function' => function($data) { - return $data['vigente'] != 0; - } - ] - ]; + $map = (new Implement\Repository\MapperParser(['descripcion'])) + ->register('vigente', new Implement\Repository\Mapper\Boolean('vigente')); return $this->parseData(new Model\Venta\TipoEstadoCierre(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/Venta/TipoEstadoPago.php b/app/src/Repository/Venta/TipoEstadoPago.php index 80f8a0b..2b166eb 100644 --- a/app/src/Repository/Venta/TipoEstadoPago.php +++ b/app/src/Repository/Venta/TipoEstadoPago.php @@ -3,7 +3,7 @@ namespace Incoviba\Repository\Venta; use Incoviba\Common\Define; use Incoviba\Common\Ideal; -use Incoviba\Repository; +use Incoviba\Common\Implement; use Incoviba\Model; class TipoEstadoPago extends Ideal\Repository @@ -16,7 +16,7 @@ class TipoEstadoPago extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = ['descripcion' => []]; + $map = new Implement\Repository\MapperParser(['descripcion']); return $this->parseData(new Model\Venta\TipoEstadoPago(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/Venta/TipoEstadoPrecio.php b/app/src/Repository/Venta/TipoEstadoPrecio.php index 1955b2e..a684168 100644 --- a/app/src/Repository/Venta/TipoEstadoPrecio.php +++ b/app/src/Repository/Venta/TipoEstadoPrecio.php @@ -3,6 +3,7 @@ namespace Incoviba\Repository\Venta; use Incoviba\Common\Ideal; use Incoviba\Common\Define; +use Incoviba\Common\Implement; use Incoviba\Model; class TipoEstadoPrecio extends Ideal\Repository @@ -15,9 +16,7 @@ class TipoEstadoPrecio extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'descripcion' => [] - ]; + $map = new Implement\Repository\MapperParser(['descripcion']); return $this->parseData(new Model\Venta\TipoEstadoPrecio(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/Venta/TipoEstadoVenta.php b/app/src/Repository/Venta/TipoEstadoVenta.php index 5433978..7b07332 100644 --- a/app/src/Repository/Venta/TipoEstadoVenta.php +++ b/app/src/Repository/Venta/TipoEstadoVenta.php @@ -3,6 +3,7 @@ namespace Incoviba\Repository\Venta; use Incoviba\Common\Ideal; use Incoviba\Common\Define; +use Incoviba\Common\Implement; use Incoviba\Model; class TipoEstadoVenta extends Ideal\Repository @@ -15,14 +16,8 @@ class TipoEstadoVenta extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'descripcion' => [], - 'activa' => [ - 'function' => function($data) { - return $data['activa'] !== 0; - } - ] - ]; + $map = (new Implement\Repository\MapperParser(['descripcion'])) + ->register('activa', new Implement\Repository\Mapper\Boolean('activa')); return $this->parseData(new Model\Venta\TipoEstadoVenta(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/Venta/TipoPago.php b/app/src/Repository/Venta/TipoPago.php index dda6865..bcb8e52 100644 --- a/app/src/Repository/Venta/TipoPago.php +++ b/app/src/Repository/Venta/TipoPago.php @@ -3,6 +3,7 @@ namespace Incoviba\Repository\Venta; use Incoviba\Common\Define; use Incoviba\Common\Ideal; +use Incoviba\Common\Implement; use Incoviba\Model; class TipoPago extends Ideal\Repository @@ -15,9 +16,7 @@ class TipoPago extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'descripcion' => [] - ]; + $map = new Implement\Repository\MapperParser(['descripcion']); return $this->parseData(new Model\Venta\TipoPago(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/Venta/TipoUnidad.php b/app/src/Repository/Venta/TipoUnidad.php index 13474bb..035a9a2 100644 --- a/app/src/Repository/Venta/TipoUnidad.php +++ b/app/src/Repository/Venta/TipoUnidad.php @@ -3,8 +3,8 @@ namespace Incoviba\Repository\Venta; use Incoviba\Common\Ideal; use Incoviba\Common\Define; +use Incoviba\Common\Implement; use Incoviba\Model; -use Incoviba\Repository; class TipoUnidad extends Ideal\Repository { @@ -16,10 +16,7 @@ class TipoUnidad extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'descripcion' => [], - 'orden' => [] - ]; + $map = new Implement\Repository\MapperParser(['descripcion', 'orden']); return $this->parseData(new Model\Venta\TipoUnidad(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/Venta/TipoValorCierre.php b/app/src/Repository/Venta/TipoValorCierre.php index 0f6cef0..4237a6d 100644 --- a/app/src/Repository/Venta/TipoValorCierre.php +++ b/app/src/Repository/Venta/TipoValorCierre.php @@ -3,6 +3,7 @@ namespace Incoviba\Repository\Venta; use Incoviba\Common\Ideal; use Incoviba\Common\Define; +use Incoviba\Common\Implement; use Incoviba\Model; class TipoValorCierre extends Ideal\Repository @@ -15,9 +16,7 @@ class TipoValorCierre extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'descripcion' => [] - ]; + $map = new Implement\Repository\MapperParser(['descripcion']); return $this->parseData(new Model\Venta\TipoValorCierre(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Repository/Venta/Unidad.php b/app/src/Repository/Venta/Unidad.php index d76f07f..6f1b523 100644 --- a/app/src/Repository/Venta/Unidad.php +++ b/app/src/Repository/Venta/Unidad.php @@ -3,12 +3,13 @@ namespace Incoviba\Repository\Venta; use Incoviba\Common\Ideal; use Incoviba\Common\Define; +use Incoviba\Common\Implement; use Incoviba\Model; -use Incoviba\Repository; +use Incoviba\Service; class Unidad extends Ideal\Repository { - public function __construct(Define\Connection $connection, protected Repository\Proyecto\ProyectoTipoUnidad $proyectoTipoUnidadRepository) + public function __construct(Define\Connection $connection, protected Service\Proyecto\ProyectoTipoUnidad $proyectoTipoUnidadService) { parent::__construct($connection); $this->setTable('unidad'); @@ -16,18 +17,12 @@ class Unidad extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'subtipo' => [], - 'piso' => [], - 'descripcion' => [], - 'orientacion' => [], - 'pt' => [ - 'property' => 'proyectoTipoUnidad', - 'function' => function($data) { - return $this->proyectoTipoUnidadRepository->fetchById($data['pt']); - } - ] - ]; + $map = (new Implement\Repository\MapperParser(['subtipo', 'piso', 'descripcion', 'orientacion'])) + ->register('pt', (new Implement\Repository\Mapper()) + ->setProperty('proyectoTipoUnidad') + ->setFunction(function($data) { + return $this->proyectoTipoUnidadService->getById($data['pt']); + })); return $this->parseData(new Model\Venta\Unidad(), $data, $map); } public function save(Define\Model $model): Define\Model @@ -64,4 +59,28 @@ GROUP BY a.`id` ORDER BY tu.`orden`, LPAD(a.`descripcion`, 4, '0')"; return $this->fetchMany($query, [$cierre_id]); } + public function fetchByProyecto(int $proyecto_id): array + { + $query = "SELECT a.* +FROM `{$this->getTable()}` a + JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = a.`pt` + JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo` +WHERE ptu.`proyecto` = ? +ORDER BY tu.`orden`"; + return $this->fetchMany($query, [$proyecto_id]); + } + public function fetchDisponiblesByProyecto(int $proyecto_id): array + { + $query = "SELECT DISTINCT a.* +FROM `{$this->getTable()}` a + JOIN `proyecto_tipo_unidad` ptu ON ptu.`id` = a.`pt` + JOIN `tipo_unidad` tu ON tu.`id` = ptu.`tipo` + LEFT OUTER JOIN `propiedad_unidad` pu ON pu.`unidad` = a.`id` + LEFT OUTER JOIN `venta` ON `venta`.`propiedad` = `pu`.`propiedad` + LEFT OUTER JOIN (SELECT ev1.* FROM `estado_venta` ev1 JOIN (SELECT MAX(`id`) as 'id', `venta` FROM `estado_venta`) ev0 ON ev0.`id` = ev1.`id`) ev ON ev.`venta` = `venta`.`id` + LEFT OUTER JOIN `tipo_estado_venta` tev ON tev.`id` = ev.`estado` +WHERE ptu.`proyecto` = ? AND (pu.`id` IS NULL OR `venta`.`id` IS NULL OR tev.`activa` = 0) +ORDER BY tu.`orden`"; + return $this->fetchMany($query, [$proyecto_id]); + } } diff --git a/app/src/Repository/Venta/ValorCierre.php b/app/src/Repository/Venta/ValorCierre.php index 801431c..5997e1b 100644 --- a/app/src/Repository/Venta/ValorCierre.php +++ b/app/src/Repository/Venta/ValorCierre.php @@ -3,6 +3,7 @@ namespace Incoviba\Repository\Venta; use Incoviba\Common\Ideal; use Incoviba\Common\Define; +use Incoviba\Common\Implement; use Incoviba\Model; use Incoviba\Repository; @@ -18,20 +19,16 @@ class ValorCierre extends Ideal\Repository public function create(?array $data = null): Define\Model { - $map = [ - 'cierre' => [ - 'function' => function($data) { + $map = (new Implement\Repository\MapperParser(['valor'])) + ->register('cierre', (new Implement\Repository\Mapper()) + ->setFunction(function($data) { return $this->cierreRepository->fetchById($data['cierre']); - } - ], - 'tipo' => [ - 'property' => 'tipoValorCierre', - 'function' => function($data) { + })) + ->register('tipo', (new Implement\Repository\Mapper()) + ->setProperty('tipoValorCierre') + ->setFunction(function($data) { return $this->tipoValorCierreRepository->fetchById($data['tipo']); - } - ], - 'valor' => [] - ]; + })); return $this->parseData(new Model\Venta\ValorCierre(), $data, $map); } public function save(Define\Model $model): Define\Model diff --git a/app/src/Service/Format.php b/app/src/Service/Format.php index ec9177b..2af1a3b 100644 --- a/app/src/Service/Format.php +++ b/app/src/Service/Format.php @@ -17,8 +17,16 @@ class Format return $formatter->format($date); } + public function number(float|string $number, int $decimal_places = 0): string + { + return number_format($number, $decimal_places, ',', '.'); + } public function pesos(string $valor): string { - return '$' . number_format($valor, 0, ',', '.'); + return '$ ' . $this->number($valor); + } + public function ufs(string $valor): string + { + return $this->number($valor, 2) . ' UF'; } } diff --git a/app/src/Service/Proyecto/ProyectoTipoUnidad.php b/app/src/Service/Proyecto/ProyectoTipoUnidad.php new file mode 100644 index 0000000..3804b25 --- /dev/null +++ b/app/src/Service/Proyecto/ProyectoTipoUnidad.php @@ -0,0 +1,22 @@ +proyectoTipoUnidadRepository->fetchById($proyecto_tipo_unidad_id); + if ($ptu->tipoUnidad->descripcion === 'departamento') { + $ptu->tipologias = $this->tipologiaRepository->fetchByProyectoTipoUnidad($proyecto_tipo_unidad_id); + } + return $ptu; + } +} diff --git a/app/src/Service/Venta.php b/app/src/Service/Venta.php index 886804c..ec6b6b9 100644 --- a/app/src/Service/Venta.php +++ b/app/src/Service/Venta.php @@ -1,7 +1,9 @@ ventaRepository->fetchById($venta_id)) + ->addFactory('estados', (new Implement\Repository\Factory()) + ->setCallable([$this->estadoVentaRepository, 'fetchByVenta']) + ->setArgs([$venta_id])) + ->addFactory('currentEstado', (new Implement\Repository\Factory()) + ->setCallable([$this->estadoVentaRepository, 'fetchCurrentByVenta']) + ->setArgs([$venta_id])); + } public function getByProyecto(int $proyecto_id): array { $ventas = $this->ventaRepository->fetchByProyecto($proyecto_id); @@ -19,4 +31,11 @@ class Venta } return $ventas; } + public function getByProyectoAndUnidad(string $proyecto_nombre, int $unidad_descripcion): Model\Venta + { + $venta = $this->ventaRepository->fetchByProyectoAndUnidad($proyecto_nombre, $unidad_descripcion); + $venta->addFactory('estados', ['callable' => [$this->estadoVentaRepository, 'fetchByVenta'], 'args' => [$venta->id]]); + $venta->addFactory('currentEstado', ['callable' => [$this->estadoVentaRepository, 'fetchCurrentByVenta'], 'args' => [$venta->id]]); + return $venta; + } } diff --git a/app/src/Service/Ventas/Cierre.php b/app/src/Service/Venta/Cierre.php similarity index 97% rename from app/src/Service/Ventas/Cierre.php rename to app/src/Service/Venta/Cierre.php index fbe750e..02c921d 100644 --- a/app/src/Service/Ventas/Cierre.php +++ b/app/src/Service/Venta/Cierre.php @@ -1,5 +1,5 @@ pagoRepository->fetchById($pago_id); + $pago->estados = $this->estadoPagoRepository->fetchByPago($pago_id); + $pago->currentEstado = $this->estadoPagoRepository->fetchCurrentByPago($pago_id); + return $pago; + } } diff --git a/app/src/Service/Venta/Pie.php b/app/src/Service/Venta/Pie.php new file mode 100644 index 0000000..0455d0a --- /dev/null +++ b/app/src/Service/Venta/Pie.php @@ -0,0 +1,20 @@ +pieRepository->fetchById($pie_id); + $pie->cuotasArray = $this->cuotaRepository->fetchVigenteByPie($pie_id); + return $pie; + } +} diff --git a/app/src/Service/Ventas/Precio.php b/app/src/Service/Venta/Precio.php similarity index 59% rename from app/src/Service/Ventas/Precio.php rename to app/src/Service/Venta/Precio.php index 34c2ebc..d6ec40a 100644 --- a/app/src/Service/Ventas/Precio.php +++ b/app/src/Service/Venta/Precio.php @@ -1,5 +1,5 @@ precioRepository->fetchByUnidad($unidad_id); + $precio = $this->precioRepository->fetchVigenteByUnidad($unidad_id); $precio->estados = $this->estadoPrecioRepository->fetchByPrecio($precio->id); $precio->current = $this->estadoPrecioRepository->fetchCurrentByPrecio($precio->id); return $precio; } + public function getByUnidad(int $unidad_id): array + { + $precios = $this->precioRepository->fetchByUnidad($unidad_id); + foreach ($precios as &$precio) { + $precio->estados = $this->estadoPrecioRepository->fetchByPrecio($precio->id); + $precio->current = $this->estadoPrecioRepository->fetchCurrentByPrecio($precio->id); + } + return $precios; + } } diff --git a/app/src/Service/Venta/Unidad.php b/app/src/Service/Venta/Unidad.php new file mode 100644 index 0000000..b690a2f --- /dev/null +++ b/app/src/Service/Venta/Unidad.php @@ -0,0 +1,49 @@ +unidadRepository->fetchById($unidad_id); + $this->fillPrecios($unidad); + return $unidad; + } + public function getByPropiedad(int $propiedad_id): array + { + $unidades = $this->unidadRepository->fetchByPropiedad($propiedad_id); + array_walk($unidades, [$this, 'fillPrecios']); + return $unidades; + } + public function getByCierre(int $cierre_id): array + { + $unidades = $this->unidadRepository->fetchByCierre($cierre_id); + array_walk($unidades, [$this, 'fillPrecios']); + return $unidades; + } + public function getDisponiblesByProyecto(int $proyecto_id): array + { + $unidades = $this->unidadRepository->fetchDisponiblesByProyecto($proyecto_id); + //array_walk($unidades, [$this, 'fillPrecios']); + return $unidades; + } + + protected function fillPrecios(&$unidad): void + { + try { + $unidad->precios = $this->precioService->getByUnidad($unidad->id); + $unidad->currentPrecio = $this->precioService->getVigenteByUnidad($unidad->id); + } catch (EmptyResult) { + } + } +}