Centros de Costos

This commit is contained in:
2024-01-09 23:35:35 -03:00
parent 74b3bb42ea
commit a66b549a8c
18 changed files with 725 additions and 0 deletions

View File

@ -0,0 +1,62 @@
<?php
namespace Incoviba\Controller\API;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Repository;
class CentrosCostos
{
use withJson;
public function add(ServerRequestInterface $request, ResponseInterface $response,
Repository\CentroCosto $centroCostoRepository): ResponseInterface
{
$body = $request->getParsedBody();
$output = [
'input' => $body,
'added' => false
];
try {
$centroCosto = $centroCostoRepository->create($body);
$centroCosto->id = $body['id'];
$centroCostoRepository->save($centroCosto);
$output['added'] = true;
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
public function edit(ServerRequestInterface $request, ResponseInterface $response,
Repository\CentroCosto $centroCostoRepository, int $centro_costo_id): ResponseInterface
{
$body = $request->getParsedBody();
$output = [
'centro_costo_id' => $centro_costo_id,
'input' => $body,
'edited' => false
];
try {
$centroCosto = $centroCostoRepository->fetchById($centro_costo_id);
if ($body['tipo_cuenta_id'] === '') {
$body['tipo_cuenta_id'] = null;
}
$centroCostoRepository->edit($centroCosto, $body);
$output['edited'] = true;
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
public function remove(ServerRequestInterface $request, ResponseInterface $response,
Repository\CentroCosto $centroCostoRepository, int $centro_costo_id): ResponseInterface
{
$output = [
'centro_costo_id' => $centro_costo_id,
'removed' => false
];
try {
$centroCosto = $centroCostoRepository->fetchById($centro_costo_id);
$centroCostoRepository->remove($centroCosto);
$output['removed'] = true;
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace Incoviba\Controller;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Repository;
use Incoviba\Common\Alias\View;
class CentrosCostos
{
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, View $view,
Repository\CentroCosto $centroCostoRepository,
Repository\TipoCentro $tipoCentroRepository,
Repository\CategoriaCentro $categoriaCentroRepository,
Repository\TipoCuenta $tipoCuentaRepository): ResponseInterface
{
$centrosCostos = $centroCostoRepository->fetchAll();
$tiposCentros = $tipoCentroRepository->fetchAll();
$categorias = $categoriaCentroRepository->fetchAll('descripcion');
$tiposCuentas = $tipoCuentaRepository->fetchAll();
return $view->render($response, 'contabilidad.centros_costos', compact('centrosCostos',
'tiposCentros', 'categorias', 'tiposCuentas'));
}
}

View File

@ -0,0 +1,5 @@
<?php
namespace Incoviba\Model;
class CategoriaCentro extends Tipo
{}

View File

@ -0,0 +1,24 @@
<?php
namespace Incoviba\Model;
use Incoviba\Common\Ideal;
class CentroCosto extends Ideal\Model
{
public TipoCentro $tipoCentro;
public CategoriaCentro $categoria;
public ?TipoCuenta $tipoCuenta;
public string $cuentaContable;
public string $descripcion;
public function jsonSerialize(): mixed
{
return array_map(parent::jsonSerialize(), [
'tipo_centro' => $this->tipoCentro,
'categoria' => $this->categoria,
'tipo_cuenta' => $this->tipoCuenta,
'cuenta_contable' => $this->cuentaContable,
'descripcion' => $this->descripcion
]);
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace Incoviba\Model;
use Incoviba\Common\Ideal;
class PagoCentroCosto extends Ideal\Model
{
public Venta\Pago $pago;
public CentroCosto $centroCosto;
public function jsonSerialize(): mixed
{
return array_merge(parent::jsonSerialize(), [
'pago' => $this->pago,
'centro_costo' => $this->centroCosto
]);
}
}

View File

@ -0,0 +1,5 @@
<?php
namespace Incoviba\Model;
class TipoCentro extends Tipo
{}

View File

@ -0,0 +1,5 @@
<?php
namespace Incoviba\Model;
class TipoCuenta extends Tipo
{}

View File

@ -0,0 +1,19 @@
<?php
namespace Incoviba\Repository;
use Incoviba\Common\Define;
use Incoviba\Model;
class CategoriaCentro extends Tipo
{
public function __construct(Define\Connection $connection)
{
parent::__construct($connection);
$this->setTable('categorias_centros_costos');
}
protected function getBlank(): Define\Model
{
return new Model\CategoriaCentro();
}
}

View File

@ -0,0 +1,81 @@
<?php
namespace Incoviba\Repository;
use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement\Repository\Mapper;
use Incoviba\Common\Implement\Repository\MapperParser;
use Incoviba\Repository;
use Incoviba\Model;
class CentroCosto extends Ideal\Repository
{
public function __construct(Define\Connection $connection, protected TipoCentro $tipoCentroRepository,
protected CategoriaCentro $categoriaCentroRepository,
protected TipoCuenta $tipoCuentaRepository)
{
parent::__construct($connection);
$this->setTable('centros_costos');
}
public function create(?array $data = null): Model\CentroCosto
{
$map = (new MapperParser(['descripcion']))
->register('tipo_centro_id', (new Mapper())
->setProperty('tipoCentro')
->setFunction(function(array $data) {
return $this->tipoCentroRepository->fetchById($data['tipo_centro_id']);
}))
->register('categoria_id', (new Mapper())
->setProperty('categoria')
->setFunction(function(array $data) {
return $this->categoriaCentroRepository->fetchById($data['categoria_id']);
}))
->register('tipo_cuenta', (new Mapper())
->setProperty('tipoCuenta')
->setFunction(function(array $data) {
return $this->tipoCuentaRepository->fetchById($data['tipo_cuenta_id']);
})
->setDefault(null))
->register('cuenta_contable', (new Mapper())
->setProperty('cuentaContable'));
return $this->parseData(new Model\CentroCosto(), $data, $map);
}
public function save(Define\Model $model): Model\CentroCosto
{
$this->saveNew(
['id', 'tipo_centro_id', 'categoria_id', 'tipo_cuenta_id', 'cuenta_contable', 'descripcion'],
[$model->id, $model->tipoCentro->id, $model->categoria->id, $model->tipoCuenta?->id, $model->cuentaContable, $model->descripcion]
);
return $model;
}
public function edit(Define\Model $model, array $new_data): Model\CentroCosto
{
return $this->update($model, ['tipo_centro_id', 'categoria_id', 'tipo_cuenta_id', 'cuenta_contable', 'descripcion'], $new_data);
}
public function fetchByDescripcion(string $descripcion): Model\CentroCosto
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('descripcion LIKE ?');
return $this->fetchOne($query, [$descripcion]);
}
public function fetchByTipoCuenta(string $tipo_cuenta): array
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('tipo_cuenta_id LIKE ?');
return $this->fetchMany($query, [$tipo_cuenta]);
}
public function fetchByCategoria(string $categoria): array
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('categoria_id LIKE ?');
return $this->fetchMany($query, [$categoria]);
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace Incoviba\Repository;
use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement;
use Incoviba\Model;
use Incoviba\Repository;
class PagoCentroCosto extends Ideal\Repository
{
public function __construct(Define\Connection $connection, protected Repository\Venta\Pago $pagoRepository, protected CentroCosto $centroCostoRepository)
{
parent::__construct($connection);
$this->setTable('pagos_centros_costos');
}
public function create(?array $data = null): Model\PagoCentroCosto
{
$map = (new Implement\Repository\MapperParser())
->register('pago_id', (new Implement\Repository\Mapper())
->setProperty('pago')
->setFunction(function(array $data) {
return $this->pagoRepository->fetchById($data['pago_id']);
}))
->register('centro_costo_id', (new Implement\Repository\Mapper())
->setProperty('centroCosto')
->setFunction(function(array $data) {
return $this->centroCostoRepository->fetchById($data['centro_costo_id']);
}));
return $this->parseData(new Model\PagoCentroCosto(), $data, $map);
}
public function save(Define\Model $model): Model\PagoCentroCosto
{
$model->id = $this->saveNew(['pago_id', 'centro_costo_id'], [$model->pago->id, $model->centroCosto->id]);
return $model;
}
public function edit(Define\Model $model, array $new_data): Model\PagoCentroCosto
{
return $this->update($model, ['pago_id', 'centro_costo_id'], $new_data);
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace Incoviba\Repository;
use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement;
abstract class Tipo extends Ideal\Repository
{
abstract protected function getBlank(): Define\Model;
public function create(?array $data = null): Define\Model
{
$map = new Implement\Repository\MapperParser(['descripcion']);
return $this->parseData($this->getBlank(), $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);
}
public function fetchByDescripcion(string $descripcion): Define\Model
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('descripcion LIKE ?');
return $this->fetchOne($query, [$descripcion]);
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace Incoviba\Repository;
use Incoviba\Common\Define;
use Incoviba\Model;
class TipoCentro extends Tipo
{
public function __construct(Define\Connection $connection)
{
parent::__construct($connection);
$this->setTable('tipos_centros_costos');
}
protected function getBlank(): Define\Model
{
return new Model\TipoCentro();
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace Incoviba\Repository;
use Incoviba\Common\Define;
use Incoviba\Model;
class TipoCuenta extends Tipo
{
public function __construct(Define\Connection $connection)
{
parent::__construct($connection);
$this->setTable('tipos_cuentas_costos');
}
protected function getBlank(): Define\Model
{
return new Model\TipoCuenta();
}
}