44 lines
1.5 KiB
PHP
44 lines
1.5 KiB
PHP
![]() |
<?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;
|
||
|
}
|
||
|
}
|