This commit is contained in:
2021-08-16 22:13:08 -04:00
parent 740de41139
commit 49374254e4
24 changed files with 466 additions and 58 deletions

View File

@ -0,0 +1,44 @@
<?php
namespace Incoviba\Common\Controller;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use \Model;
use Incoviba\Common\Define\Controller\Json;
use Incoviba\FacturaProyectoOperador;
class Facturas {
use Json;
public function __invoke() {
}
public function proyecto_operador(Request $request, Response $response): Response {
$post = $request->getParsedBody();
$facturas = Model::factory(FacturaProyectoOperador::class)
->where('proyecto_id', $post['proyecto_id'])
->where('operador_id', $post['operador_id'])
->find_many();
$output = [
'facturas' => $facturas ? array_map(function($item) {
return $item->as_array();
}, $facturas) : null
];
return $this->withJson($response, $output);
}
public function add(Request $request, Response $response): Response {
$post = $request->getParsedBody();
$output = FacturaProyectoOperador::add($post);
return $this->withJson($response, $output);
}
public function ventas(Request $request, Response $response, $id_factura): Response {
$factura = Model::factory(FacturaProyectoOperador::class)->find_one($id_factura);
$output = [
'factura' => $factura->as_array(),
'ventas' => array_map(function($item) {
return $item->as_array();
}, $factura->ventas())
];
return $this->withJson($response, $output);
}
}

View File

@ -31,7 +31,10 @@ class Operadores {
}
public function show(Request $request, Response $response, $id_operador): Response {
$operador = Model::factory(Operador::class)->find_one($id_operador);
$output = ['operador' => $operador->as_array()];
$output = ['operador' => null];
if ($operador) {
$output = ['operador' => $operador->as_array()];
}
return $this->withJson($response, $output);
}
public function add(Request $request, Response $response): Response {
@ -49,4 +52,10 @@ class Operadores {
];
return $this->withJson($response, $output);
}
public function add_proyecto(Request $request, Response $response, $id_operador): Response {
$operador = Model::factory(Operador::class)->find_one($id_operador);
$post = $request->getParsedBody();
$output = $operador->addProyecto($post);
return $this->withJson($response, $output);
}
}

View File

@ -20,18 +20,37 @@ class Proyectos {
}
public function show(Request $request, Response $response, $id_proyecto): Response {
$proyecto = Model::factory(Proyecto::class)->find_one($id_proyecto);
$output = ['proyecto' => $proyecto->as_array()];
$output = ['proyecto' => null];
if ($proyecto) {
$output = ['proyecto' => $proyecto->as_array()];
}
return $this->withJson($response, $output);
}
public function ventas(Request $request, Response $response, $id_proyecto): Response {
$proyecto = Model::factory(Proyecto::class)->find_one($id_proyecto);
$ventas = $proyecto->ventas();
usort($ventas, function($a, $b) {
return strcmp(str_pad($a->propiedad()->unidades()[0]->descripcion, 4, '0', \STR_PAD_LEFT), str_pad($b->propiedad()->unidades()[0]->descripcion, 4, '0', \STR_PAD_LEFT));
});
$output = [
'proyecto' => $proyecto->as_array(),
'ventas' => array_map(function($item) {
if ($item) {
return $item->as_array();
}
}, $proyecto->ventas())
}, $ventas)
];
return $this->withJson($response, $output);
}
public function operadores(Request $request, Response $response, $id_proyecto): Response {
$proyecto = Model::factory(Proyecto::class)->find_one($id_proyecto);
$output = [
'proyecto' => $proyecto->as_array(),
'operadores' => $proyecto->operadores() ? array_map(function($item) {
if ($item) {
return $item->as_array();
}
}, $proyecto->operadores()) : null
];
return $this->withJson($response, $output);
}

View File

@ -0,0 +1,23 @@
<?php
namespace Incoviba\Common\Controller;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use \Model;
use Incoviba\Common\Define\Controller\Json;
use Incoviba\Unidad;
class Unidades {
use Json;
public function facturas(Request $request, Response $response, $id_unidad): Response {
$unidad = Model::factory(Unidad::class)->find_one($id_unidad);
$output = [
'unidad' => $unidad->as_array(),
'facturas' => ($unidad->facturas()) ? array_map(function($item) {
return $item->as_array();
}, $unidad->facturas()) : null
];
return $this->withJson($response, $output);
}
}

View File

@ -7,7 +7,8 @@
"nyholm/psr7": "^1.4",
"nyholm/psr7-server": "^1.0",
"zeuxisoo/slim-whoops": "^0.7.3",
"j4mie/paris": "^1.5"
"j4mie/paris": "^1.5",
"nesbot/carbon": "^2.51"
},
"require-dev": {
"phpunit/phpunit": "^9.5",

View File

@ -0,0 +1,15 @@
<?php
use Incoviba\Common\Controller\Facturas;
$app->group('/facturas', function($app) {
$app->group('/add', function($app) {
$app->post('[/]', [Facturas::class, 'add']);
});
$app->post('[/]', [Facturas::class, 'proyecto_operador']);
$app->get('[/]', Facturas::class);
});
$app->group('/factura/{id_factura}', function($app) {
$app->group('/ventas', function($app) {
$app->get('[/]', [Facturas::class, 'ventas']);
});
});

View File

@ -9,5 +9,9 @@ $app->group('/operador/{id_operador}', function($app) {
$app->group('/ventas', function($app) {
$app->get('[/]', [Operadores::class, 'ventas']);
});
$app->group('/proyectos', function($app) {
$app->post('/add[/]', [Operadores::class, 'add_proyecto']);
$app->get('[/]', [Operadores::class, 'proyectos']);
});
$app->get('[/]', [Operadores::class, 'show']);
});

View File

@ -8,5 +8,8 @@ $app->group('/proyecto/{id_proyecto}', function($app) {
$app->group('/ventas', function($app) {
$app->get('[/]', [Proyectos::class, 'ventas']);
});
$app->group('/operadores', function($app) {
$app->get('[/]', [Proyectos::class, 'operadores']);
});
$app->get('[/]', [Proyectos::class, 'show']);
});

View File

@ -0,0 +1,8 @@
<?php
use Incoviba\Common\Controller\Unidades;
$app->group('/unidad/{id_unidad}', function($app) {
$app->group('/facturas', function($app) {
$app->get('[/]', [Unidades::class, 'facturas']);
});
});

View File

@ -13,4 +13,12 @@ class Agente extends Model {
}
return $this->agente_tipos;
}
public function findAgenteTipo(string $tipo) {
foreach ($this->agente_tipos() as $at) {
if ($at->tipo()->descripcion == $tipo) {
return $at;
}
}
return false;
}
}

View File

@ -1,24 +0,0 @@
<?php
namespace Incoviba;
use \Model;
/**
* @property int $id
* @property Operador $operador_id
* @property int $factura
* @property int $valor_neto
* @property int $iva
* @property int $valor_total
*/
class FacturaOperador extends Model {
public static $_table = 'factura_operador';
protected $operador;
public function operador() {
if ($this->operador === null) {
$this->operador = $this->belongs_to(Operador::class, 'operador_id')->find_one();
}
return $this->operador;
}
}

View File

@ -0,0 +1,113 @@
<?php
namespace Incoviba;
use Carbon\Carbon;
use \Model;
/**
* @property int $id
* @property Proyecto $proyecto_id
* @property Operador $operador_id
* @property int $factura
* @property double $valor_uf
* @property int $valor_neto
* @property int $iva
*/
class FacturaProyectoOperador extends Model {
public static $_table = 'factura_proyecto_operador';
protected $proyecto;
public function proyecto() {
if ($this->proyecto === null) {
$this->proyecto = $this->belongs_to(Proyecto::class, 'proyecto_id')->find_one();
}
return $this->proyecto;
}
protected $operador;
public function operador() {
if ($this->operador === null) {
$this->operador = $this->belongs_to(Operador::class, 'operador_id')->find_one();
}
return $this->operador;
}
public function fecha(\DateTime $fecha = null) {
if ($fecha === null) {
return Carbon::parse($this->fecha);
}
$this->fecha = $fecha->format('Y-m-d');
}
public function valor_total() {
return $this->valor_neto + $this->iva;
}
protected $ventas;
public function ventas() {
if ($this->ventas === null) {
$this->ventas = $this->has_many(FacturaVenta::class, 'factura_id')->find_many();
}
return $this->ventas;
}
public static function add($data) {
$fields = [
'proyecto_id',
'operador_id',
'factura',
'valor_uf',
'valor_neto',
'iva'
];
$input = array_intersect_key($data, array_combine($fields, $fields));
$validate = [
'proyecto_id',
'operador_id',
'factura'
];
$orm = Model::factory(FacturaProyectoOperador::class);
foreach ($validate as $field) {
$orm = $orm->where($field, $input[$field]);
}
$factura = $orm->find_one();
$created = false;
$found = true;
if (!$factura) {
$found = false;
$factura = FacturaProyectoOperador::create($input);
$created = $factura->save();
}
$output = [
'input' => $data,
'factura' => $factura->as_array(),
'new' => !$found,
'created' => $created
];
return $output;
}
public function as_array() {
$arr = parent::as_array();
$arr['proyecto'] = $this->proyecto()->as_array();
$arr['operador'] = $this->operador()->as_array();
$arr['fecha'] = (object) [
'valor' => $this->fecha,
'formateada' => $this->fecha()->format('d-m-Y')
];
$arr['valor_uf'] = (object) [
'valor' => $this->valor_uf,
'formateado' => number_format($this->valor_uf, 2, ',', '.') . ' UF'
];
$arr['valor_neto'] = (object) [
'valor' => $this->valor_neto,
'formateado' => '$ ' . number_format($this->valor_neto, 0, ',', '.')
];
$arr['iva'] = (object) [
'valor' => $this->iva,
'formateado' => '$ ' . number_format($this->iva, 0, ',', '.')
];
$arr['valor_total'] = (object) [
'valor' => $this->valor_total(),
'formateado' => '$ ' . number_format($this->valor_total(), 0, ',', '.')
];
return $arr;
}
}

View File

@ -1,29 +0,0 @@
<?php
namespace Incoviba;
use \Model;
/**
* @property int $id
* @property FacturaOperador $factura_id
* @property Unidad $unidad_id
* @property double $valor
*/
class FacturaUnidad extends Model {
public static $_table = 'factura_unidad';
protected $factura;
public function factura() {
if ($this->factura === null) {
$this->factura = $this->belongs_to(FacturaOperador::class, 'factura_id')->find_one();
}
return $this->factura;
}
protected $unidad;
public function unidad() {
if ($this->unidad === null) {
$this->unidad = $this->belongs_to(Unidad::class, 'unidad_id')->find_one();
}
return $this->unidad;
}
}

40
api/src/FacturaVenta.php Normal file
View File

@ -0,0 +1,40 @@
<?php
namespace Incoviba;
use \Model;
/**
* @property int $id
* @property FacturaOperador $factura_id
* @property Venta $venta_id
* @property double $valor
*/
class FacturaVenta extends Model {
public static $_table = 'factura_venta';
protected $factura;
public function factura() {
if ($this->factura === null) {
$this->factura = $this->belongs_to(FacturaProyectoOperador::class, 'factura_id')->find_one();
}
return $this->factura;
}
protected $venta;
public function venta() {
if ($this->venta === null) {
$this->venta = $this->belongs_to(Venta::class, 'venta_id')->find_one();
}
return $this->venta;
}
public function as_array() {
$arr = parent::as_array();
$arr['factura'] = $this->factura()->as_array();
$arr['venta'] = $this->venta()->as_array();
$arr['valor'] = (object) [
'valor' => $this->valor,
'formateado' => number_format($this->valor, 2, ',', '.') . ' UF'
];
return $arr;
}
}

View File

@ -70,11 +70,19 @@ class Operador extends Agente {
$output = [
'input' => $data,
'operador' => $operador->as_array(),
'new' => $found,
'new' => !$found,
'created' => $created
];
return $output;
}
public function addProyecto($data) {
$data['agente'] = $this->findAgenteTipo('operador')->id;
$pa = ProyectoAgente::add($data);
return [
'operador' => $this->as_array(),
'proyecto_agente' => $pa
];
}
public function as_array() {
$arr = parent::as_array();

View File

@ -13,4 +13,33 @@ class Propiedad extends Model {
}
return $this->venta;
}
protected $unidades;
public function unidades() {
if ($this->unidades === null) {
$pus = $this->has_many(PropiedadUnidad::class, 'propiedad')->find_many();
usort($pus, function($a, $b) {
$p = $a->principal - $b->principal;
if ($p == 0) {
$t = $a->unidad()->tipo - $b->unidad()->tipo;
if ($t == 0) {
return $a->unidad()->descripcion - $b->unidad()->descripcion;
}
return $t;
}
return -$p;
});
$this->unidades = array_map(function($item) {
return $item->unidad();
}, $pus);
}
return $this->unidades;
}
public function as_array() {
$arr = parent::as_array();
$arr['unidades'] = array_map(function($item) {
return $item->as_array();
}, $this->unidades());
return $arr;
}
}

View File

@ -13,4 +13,11 @@ class PropiedadUnidad extends Model {
}
return $this->propiedad_obj;
}
protected $unidad_obj;
public function unidad() {
if ($this->unidad_obj === null) {
$this->unidad_obj = $this->belongs_to(Unidad::class, 'unidad')->find_one();
}
return $this->unidad_obj;
}
}

31
api/src/Propietario.php Normal file
View File

@ -0,0 +1,31 @@
<?php
namespace Incoviba;
use \Model;
class Propietario extends Model {
public static $_table = 'propietario';
public static $_id_columns = ['rut'];
protected $venta;
public function venta() {
if ($this->venta === null) {
$this->venta = $this->has_one(Venta::class, 'propietario', 'rut')->find_one();
}
return $this->venta;
}
public function nombreCompleto() {
return implode(' ', [
$this->nombres,
$this->apellido_paterno,
$this->apellido_materno
]);
}
public function as_array() {
$arr = parent::as_array();
$arr['nombre_completo'] = $this->nombreCompleto();
return $arr;
}
}

View File

@ -41,6 +41,20 @@ class Proyecto extends Model {
return $this->proyecto_tipo_unidades;
}
protected $operadores;
public function operadores() {
if ($this->operadores === null) {
$pas = $this->has_many(ProyectoAgente::class, 'proyecto')->find_many();
$operadores = [];
foreach ($pas as $pa) {
$id = $pa->agente_tipo()->agente()->id;
$operadores []= Model::factory(Operador::class)->find_one($id);
}
$this->operadores = $operadores;
}
return $this->operadores;
}
public function as_array() {
$arr = parent::as_array();
$arr['inmobiliaria'] = $this->inmobiliaria()->as_array();

View File

@ -27,4 +27,25 @@ class ProyectoAgente extends Model {
}
return $this->proyecto_obj;
}
public static function add($data) {
$fields = ['agente', 'proyecto', 'fecha', 'comision'];
$input = array_intersect_key($data, array_combine($fields, $fields));
$pa = Model::factory(ProyectoAgente::class)
->where('proyecto', $input['proyecto'])
->where('agente', $input['agente'])
->where('fecha', $input['fecha'])
->find_one();
$created = false;
if (!$pa) {
$pa = ProyectoAgente::create($input);
$created = $pa->save();
}
$output = [
'input' => $input,
'proyecto_agente' => $pa->as_array(),
'created' => $created
];
return $output;
}
}

View File

@ -13,4 +13,12 @@ class ProyectoTipoUnidad extends Model {
}
return $this->unidades;
}
protected $proyecto_obj;
public function proyecto() {
if ($this->proyecto_obj === null) {
$this->proyecto_obj = $this->belongs_to(Proyecto::class, 'proyecto')->find_one();
}
return $this->proyecto_obj;
}
}

View File

@ -13,4 +13,18 @@ class Unidad extends Model {
}
return $this->propiedad_unidad;
}
protected $proyecto_tipo_unidad;
public function proyecto_tipo_unidad() {
if ($this->proyecto_tipo_unidad === null) {
$this->proyecto_tipo_unidad = $this->belongs_to(ProyectoTipoUnidad::class, 'pt')->find_one();
}
return $this->proyecto_tipo_unidad;
}
protected $facturas;
public function facturas() {
if ($this->facturas === null) {
$this->facturas = $this->has_many(FacturaUnidad::class, 'unidad_id')->find_many();
}
return $this->facturas;
}
}

View File

@ -43,4 +43,46 @@ class Venta extends Model {
}
return $this->activa;
}
protected $propietario_obj;
public function propietario() {
if ($this->propietario_obj === null) {
$this->propietario_obj = $this->belongs_to(Propietario::class, 'propietario', 'rut')->find_one();
}
return $this->propietario_obj;
}
protected $propiedad_obj;
public function propiedad() {
if ($this->propiedad_obj === null) {
$this->propiedad_obj = $this->belongs_to(Propiedad::class, 'propiedad')->find_one();
}
return $this->propiedad_obj;
}
protected $comision;
public function comision() {
if ($this->comision === null) {
$proyecto_id = $this->propiedad()->unidades()[0]->proyecto_tipo_unidad()->proyecto()->id;
$comision = array_values(array_filter($this->operador()->comisiones(), function($item) use ($proyecto_id) {
return ($item->proyecto->id == $proyecto_id);
}));
$comision = $comision[0]->comision;
$this->comision = (object) [
'valor' => $comision / 100,
'total' => $comision / 100 * $this->valor_uf
];
}
return $this->comision;
}
public function as_array() {
$arr = parent::as_array();
$arr['propietario'] = $this->propietario()->as_array();
$arr['propiedad'] = $this->propiedad()->as_array();
$arr['valor'] = '$ ' . number_format($this->valor_uf, 2, ',', '.');
if ($this->operador()) {
$arr['operador'] = $this->operador()->as_array();
$arr['comision'] = (array) $this->comision();
$arr['comision']['formateada'] = '$ ' . number_format($this->comision()->total, 2, ',', '.');
}
return $arr;
}
}