Movimiento + Detalles
This commit is contained in:
50
app/src/Controller/API/Contabilidad/Movimientos.php
Normal file
50
app/src/Controller/API/Contabilidad/Movimientos.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
namespace Incoviba\Controller\API\Contabilidad;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Controller\API\withJson;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Service;
|
||||
|
||||
class Movimientos extends Ideal\Controller
|
||||
{
|
||||
use withJson;
|
||||
|
||||
public function detalles(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Service\Movimiento $movimientoService,
|
||||
Repository\CentroCosto $centroCostoRepository, int $movimiento_id): ResponseInterface
|
||||
{
|
||||
$body = $request->getParsedBody();
|
||||
$output = [
|
||||
'movimiento_id' => $movimiento_id,
|
||||
'input' => $body,
|
||||
'status' => false,
|
||||
'movimiento' => null,
|
||||
'centro' => null,
|
||||
'detalle' => ''
|
||||
];
|
||||
try {
|
||||
$movimiento = $movimientoService->getById($movimiento_id);
|
||||
$output['movimiento'] = $movimiento;
|
||||
$data = [];
|
||||
if (isset($body['centro_id'])) {
|
||||
$centro = $centroCostoRepository->fetchById($body['centro_id']);
|
||||
$data['centro_costo_id'] = $centro->id;
|
||||
}
|
||||
if (isset($body['detalle'])) {
|
||||
$data['detalle'] = $body['detalle'];
|
||||
}
|
||||
$movimientoService->setDetalles($movimiento, $data);
|
||||
if (isset($body['centro_id'])) {
|
||||
$output['centro'] = $centro;
|
||||
}
|
||||
if (isset($body['detalle'])) {
|
||||
$output['detalle'] = $body['detalle'];
|
||||
}
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
@ -18,7 +18,8 @@ class Contabilidad extends Controller
|
||||
|
||||
public function diaria(ServerRequestInterface $request, ResponseInterface $response, View $view,
|
||||
Service\Redis $redisService,
|
||||
Repository\Inmobiliaria $inmobiliariaRepository): ResponseInterface
|
||||
Repository\Inmobiliaria $inmobiliariaRepository,
|
||||
Repository\CentroCosto $centroCostoRepository): ResponseInterface
|
||||
{
|
||||
$redisKey = 'inmobiliarias';
|
||||
$inmobiliarias = [];
|
||||
@ -30,7 +31,8 @@ class Contabilidad extends Controller
|
||||
$this->saveRedis($redisService, $redisKey, $inmobiliarias, 30 * 24 * 60 * 60);
|
||||
} catch (EmptyResult) {}
|
||||
}
|
||||
return $view->render($response, 'contabilidad.cartolas.diaria', compact('inmobiliarias'));
|
||||
$centrosCostos = $centroCostoRepository->fetchAll();
|
||||
return $view->render($response, 'contabilidad.cartolas.diaria', compact('inmobiliarias', 'centrosCostos'));
|
||||
}
|
||||
public function depositos(ServerRequestInterface $request, ResponseInterface $response, View $view,
|
||||
Service\Redis $redisService,
|
||||
|
@ -13,7 +13,7 @@ class CentroCosto extends Ideal\Model
|
||||
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return array_map(parent::jsonSerialize(), [
|
||||
return array_merge(parent::jsonSerialize(), [
|
||||
'tipo_centro' => $this->tipoCentro,
|
||||
'categoria' => $this->categoria,
|
||||
'tipo_cuenta' => $this->tipoCuenta,
|
||||
|
@ -3,6 +3,7 @@ namespace Incoviba\Model;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Model\Movimiento\Detalle;
|
||||
|
||||
class Movimiento extends Ideal\Model
|
||||
{
|
||||
@ -14,6 +15,15 @@ class Movimiento extends Ideal\Model
|
||||
public int $abono;
|
||||
public int $saldo;
|
||||
|
||||
protected ?Detalle $detalles;
|
||||
public function getDetalles(): ?Detalle
|
||||
{
|
||||
if (!isset($this->detalles)) {
|
||||
$this->detalles = $this->runFactory('detalles');
|
||||
}
|
||||
return $this->detalles;
|
||||
}
|
||||
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return array_merge(parent::jsonSerialize(), [
|
||||
@ -23,7 +33,8 @@ class Movimiento extends Ideal\Model
|
||||
'documento' => $this->documento,
|
||||
'cargo' => $this->cargo,
|
||||
'abono' => $this->abono,
|
||||
'saldo' => $this->saldo
|
||||
'saldo' => $this->saldo,
|
||||
'detalles' => $this->getDetalles() ?? null
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
21
app/src/Model/Movimiento/Detalle.php
Normal file
21
app/src/Model/Movimiento/Detalle.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
namespace Incoviba\Model\Movimiento;
|
||||
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Model;
|
||||
|
||||
class Detalle extends Ideal\Model
|
||||
{
|
||||
public Model\Movimiento $movimiento;
|
||||
public ?Model\CentroCosto $centroCosto;
|
||||
public ?string $detalle;
|
||||
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return [
|
||||
'movimiento_id' => $this->movimiento->id,
|
||||
'centro_costo' => $this->centroCosto,
|
||||
'detalle' => $this->detalle
|
||||
];
|
||||
}
|
||||
}
|
60
app/src/Repository/Movimiento/Detalle.php
Normal file
60
app/src/Repository/Movimiento/Detalle.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
namespace Incoviba\Repository\Movimiento;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Implement;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
|
||||
class Detalle extends Ideal\Repository
|
||||
{
|
||||
public function __construct(Define\Connection $connection, protected Repository\Movimiento $movimientoRepository,
|
||||
protected Repository\CentroCosto $centroCostoRepository)
|
||||
{
|
||||
parent::__construct($connection);
|
||||
$this->setTable('movimientos_detalles');
|
||||
}
|
||||
|
||||
public function create(?array $data = null): Model\Movimiento\Detalle
|
||||
{
|
||||
$map = (new Implement\Repository\MapperParser(['detalle']))
|
||||
->register('movimiento_id', (new Implement\Repository\Mapper())
|
||||
->setProperty('movimiento')
|
||||
->setFunction(function(array $data) {
|
||||
return $this->movimientoRepository->fetchById($data['movimiento_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\Movimiento\Detalle(), $data, $map);
|
||||
}
|
||||
public function save(Define\Model $model): Model\Movimiento\Detalle
|
||||
{
|
||||
$this->saveNew(
|
||||
['movimiento_id', 'centro_costo_id', 'detalle'],
|
||||
[$model->movimiento->id, $model->centroCosto->id, $model->detalle]
|
||||
);
|
||||
return $model;
|
||||
}
|
||||
public function edit(Define\Model $model, array $new_data): Model\Movimiento\Detalle
|
||||
{
|
||||
return $this->update($model, ['movimiento_id', 'centro_costo_id', 'detalle'], $new_data);
|
||||
}
|
||||
|
||||
public function fetchByMovimiento(int $movimiento_id): Model\Movimiento\Detalle
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where('movimiento_id = ?');
|
||||
return $this->fetchOne($query, [$movimiento_id]);
|
||||
}
|
||||
|
||||
protected function getKey(): string
|
||||
{
|
||||
return 'movimiento_id';
|
||||
}
|
||||
}
|
@ -21,6 +21,7 @@ class Cartola extends Service
|
||||
protected Repository\Inmobiliaria $inmobiliariaRepository,
|
||||
protected Repository\Inmobiliaria\Cuenta $cuentaRepository,
|
||||
protected Repository\Movimiento $movimientoRepository,
|
||||
protected Movimiento $movimientoService,
|
||||
protected Repository\Cartola $cartolaRepository) {
|
||||
parent::__construct($logger);
|
||||
}
|
||||
@ -51,6 +52,7 @@ class Cartola extends Service
|
||||
$movimientos = [];
|
||||
foreach ($ms as $m) {
|
||||
$movimiento = $this->buildMovimiento($cuenta, $m);
|
||||
$movimiento = $this->movimientoService->process($movimiento);
|
||||
|
||||
if ($movimiento->fecha->getTimestamp() === $fecha->getTimestamp()) {
|
||||
$movimientos []= $movimiento;
|
||||
@ -78,6 +80,7 @@ class Cartola extends Service
|
||||
$dataMovimiento['fecha'] = $fecha->format('Y-m-d');
|
||||
$dataMovimiento['documento'] = '';
|
||||
$movimiento = $this->buildMovimiento($cuenta, $dataMovimiento);
|
||||
$movimiento = $this->movimientoService->process($movimiento);
|
||||
|
||||
$movimientos []= $movimiento;
|
||||
$cartolaData['cargos'] += $movimiento->cargo;
|
||||
|
43
app/src/Service/Movimiento.php
Normal file
43
app/src/Service/Movimiento.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
namespace Incoviba\Service;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Incoviba\Common\Ideal\Service;
|
||||
use Incoviba\Common\Implement;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Model;
|
||||
|
||||
class Movimiento extends Service
|
||||
{
|
||||
public function __construct(LoggerInterface $logger, protected Repository\Movimiento $movimientoRepository,
|
||||
protected Repository\Movimiento\Detalle $detalleRepository)
|
||||
{
|
||||
parent::__construct($logger);
|
||||
}
|
||||
|
||||
public function getById(int $movimiento_id): Model\Movimiento
|
||||
{
|
||||
return $this->process($this->movimientoRepository->fetchById($movimiento_id));
|
||||
}
|
||||
public function setDetalles(Model\Movimiento $movimiento, array $data): Model\Movimiento
|
||||
{
|
||||
try {
|
||||
$detalles = $this->detalleRepository->fetchByMovimiento($movimiento->id);
|
||||
$this->detalleRepository->edit($detalles, $data);
|
||||
} catch (Implement\Exception\EmptyResult) {
|
||||
$data['movimiento_id'] = $movimiento->id;
|
||||
$detalles = $this->detalleRepository->create($data);
|
||||
$this->detalleRepository->save($detalles);
|
||||
}
|
||||
return $movimiento;
|
||||
}
|
||||
|
||||
public function process(Model\Movimiento $movimiento): Model\Movimiento
|
||||
{
|
||||
$movimiento->addFactory('detalles', (new Implement\Repository\Factory())->setCallable(function(int $movimiento_id) {
|
||||
return $this->detalleRepository->fetchByMovimiento($movimiento_id);
|
||||
})->setArgs(['movimiento_id' => $movimiento->id]));
|
||||
return $movimiento;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user