88 lines
3.5 KiB
PHP
88 lines
3.5 KiB
PHP
<?php
|
|
namespace Incoviba\Repository\Contabilidad;
|
|
|
|
use Incoviba\Common\Define;
|
|
use Incoviba\Common\Ideal;
|
|
use Incoviba\Common\Implement\Repository\Mapper;
|
|
use Incoviba\Common\Implement\Repository\MapperParser;
|
|
use Incoviba\Model;
|
|
use Incoviba\Repository\TipoCuenta;
|
|
|
|
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\Contabilidad\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_id', (new Mapper())
|
|
->setProperty('tipoCuenta')
|
|
->setFunction(function(array $data) {
|
|
if (empty($data['tipo_cuenta_id'])) return null;
|
|
return $this->tipoCuentaRepository->fetchById($data['tipo_cuenta_id']);
|
|
})
|
|
->setDefault(null))
|
|
->register('cuenta_contable', (new Mapper())
|
|
->setProperty('cuentaContable'));
|
|
return $this->parseData(new Model\Contabilidad\CentroCosto(), $data, $map);
|
|
}
|
|
public function save(Define\Model $model): Model\Contabilidad\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\Contabilidad\CentroCosto
|
|
{
|
|
return $this->update($model, ['tipo_centro_id', 'categoria_id', 'tipo_cuenta_id', 'cuenta_contable', 'descripcion'], $new_data);
|
|
}
|
|
|
|
/**
|
|
* @param string $descripcion
|
|
* @return Model\Contabilidad\CentroCosto
|
|
* @throws \Incoviba\Common\Implement\Exception\EmptyResult
|
|
*/
|
|
public function fetchByDescripcion(string $descripcion): Model\Contabilidad\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]);
|
|
}
|
|
}
|