DAPs API
This commit is contained in:
35
app/src/Controller/API/Contabilidad/DAPs.php
Normal file
35
app/src/Controller/API/Contabilidad/DAPs.php
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
namespace Incoviba\Controller\API\Contabilidad;
|
||||||
|
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Incoviba\Common\Ideal;
|
||||||
|
use Incoviba\Common\Implement;
|
||||||
|
use Incoviba\Controller\API\withJson;
|
||||||
|
use Incoviba\Repository;
|
||||||
|
|
||||||
|
class DAPs extends Ideal\Controller
|
||||||
|
{
|
||||||
|
use withJson;
|
||||||
|
|
||||||
|
public function inmobiliaria(ServerRequestInterface $request, ResponseInterface $response,
|
||||||
|
Repository\Inmobiliaria $inmobiliariaRepository,
|
||||||
|
Repository\Banco $bancoRepository,
|
||||||
|
Repository\Inmobiliaria\Cuenta $cuentaRepository,
|
||||||
|
Repository\DAP $dapRepository,
|
||||||
|
int $inmobiliaria_rut): ResponseInterface
|
||||||
|
{
|
||||||
|
$output = [
|
||||||
|
'inmobiliaria_rut' => $inmobiliaria_rut,
|
||||||
|
'daps' => []
|
||||||
|
];
|
||||||
|
try {
|
||||||
|
$inmobiliaria = $inmobiliariaRepository->fetchById($inmobiliaria_rut);
|
||||||
|
$cuentas = $cuentaRepository->fetchByInmobiliaria($inmobiliaria->rut);
|
||||||
|
foreach ($cuentas as $cuenta) {
|
||||||
|
$daps = $dapRepository->fetchByCuenta($cuenta->id);
|
||||||
|
}
|
||||||
|
} catch (Implement\Exception\EmptyResult) {}
|
||||||
|
return $this->withJson($response, $output);
|
||||||
|
}
|
||||||
|
}
|
36
app/src/Model/DAP.php
Normal file
36
app/src/Model/DAP.php
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
namespace Incoviba\Model;
|
||||||
|
|
||||||
|
use DateTimeInterface;
|
||||||
|
use Incoviba\Common\Ideal;
|
||||||
|
|
||||||
|
class DAP extends Ideal\Model
|
||||||
|
{
|
||||||
|
public Inmobiliaria\Cuenta $cuenta;
|
||||||
|
public int $capital;
|
||||||
|
public int $futuro;
|
||||||
|
public DateTimeInterface $inicio;
|
||||||
|
public DateTimeInterface $termino;
|
||||||
|
|
||||||
|
public function periodo(): int
|
||||||
|
{
|
||||||
|
return $this->termino->diff($this->inicio)->days;
|
||||||
|
}
|
||||||
|
public function interes(): float
|
||||||
|
{
|
||||||
|
return ($this->futuro - $this->capital) / $this->capital / $this->periodo() * 365;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function jsonSerialize(): mixed
|
||||||
|
{
|
||||||
|
return array_merge(parent::jsonSerialize(), [
|
||||||
|
'cuenta_id' => $this->cuenta->id,
|
||||||
|
'capital' => $this->capital,
|
||||||
|
'futuro' => $this->futuro,
|
||||||
|
'inicio' => $this->inicio->format('Y-m-d'),
|
||||||
|
'termino' => $this->termino->format('Y-m-d'),
|
||||||
|
'periodo' => $this->periodo(),
|
||||||
|
'interes' => $this->interes()
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
54
app/src/Repository/DAP.php
Normal file
54
app/src/Repository/DAP.php
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
namespace Incoviba\Repository;
|
||||||
|
|
||||||
|
use Incoviba\Common\Define;
|
||||||
|
use Incoviba\Common\Ideal;
|
||||||
|
use Incoviba\Common\Implement;
|
||||||
|
use Incoviba\Repository;
|
||||||
|
use Incoviba\Model;
|
||||||
|
|
||||||
|
class DAP extends Ideal\Repository
|
||||||
|
{
|
||||||
|
public function __construct(Define\Connection $connection, protected Repository\Inmobiliaria\Cuenta $cuentaRepository)
|
||||||
|
{
|
||||||
|
parent::__construct($connection);
|
||||||
|
$this->setTable('depositos');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function create(?array $data = null): Model\DAP
|
||||||
|
{
|
||||||
|
$map = (new Implement\Repository\MapperParser(['capital', 'futuro']))
|
||||||
|
->register('cuenta_id', (new Implement\Repository\Mapper())
|
||||||
|
->setProperty('cuenta')
|
||||||
|
->setFunction(function(array $data) {
|
||||||
|
return $this->cuentaRepository->fetchById($data['cuenta_id']);
|
||||||
|
}))
|
||||||
|
->register('inicio', new Implement\Repository\Mapper\DateTime('inicio'))
|
||||||
|
->register('termino', new Implement\Repository\Mapper\DateTime('termino'));
|
||||||
|
return $this->parseData(new Model\DAP(), $data, $map);
|
||||||
|
}
|
||||||
|
public function save(Define\Model $model): Model\DAP
|
||||||
|
{
|
||||||
|
$model->id = $this->saveNew([
|
||||||
|
'cuenta_id', 'capital', 'futuro', 'inicio', 'termino'
|
||||||
|
], [
|
||||||
|
$model->cuenta->id, $model->capital, $model->futuro,
|
||||||
|
$model->inicio->format('Y-m-d'), $model->termino->format('Y-m-d')
|
||||||
|
]);
|
||||||
|
return $model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function edit(Define\Model $model, array $new_data): Model\DAP
|
||||||
|
{
|
||||||
|
return $this->update($model, ['cuenta_id', 'capital', 'futuro', 'inicio', 'termino'], $new_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function fetchByCuenta(int $cuenta_id): array
|
||||||
|
{
|
||||||
|
$query = $this->connection->getQueryBuilder()
|
||||||
|
->select()
|
||||||
|
->from($this->getTable())
|
||||||
|
->where('cuenta_id = ?');
|
||||||
|
return $this->fetchMany($query, [$cuenta_id]);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user