Proveedores en movimientos
This commit is contained in:
@ -126,6 +126,20 @@ class Movimientos extends Ideal\Controller
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function findRut(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Service\Contabilidad\Movimiento $movimientoService, int $rut): ResponseInterface
|
||||
{
|
||||
$output = [
|
||||
'rut' => $rut,
|
||||
'model' => null,
|
||||
'success' => false
|
||||
];
|
||||
try {
|
||||
$output['model'] = $movimientoService->findRut($rut);
|
||||
$output['success'] = true;
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
|
||||
protected function movimientosToArray(array $movimientos): array
|
||||
{
|
||||
|
@ -1,36 +0,0 @@
|
||||
<?php
|
||||
namespace Incoviba\Model\Contabilidad\Movimiento;
|
||||
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Implement;
|
||||
use Incoviba\Model;
|
||||
|
||||
class Auxiliar extends Ideal\Model
|
||||
{
|
||||
public Model\Contabilidad\Movimiento $movimiento;
|
||||
public int $cargo;
|
||||
public int $abono;
|
||||
|
||||
protected ?Model\Contabilidad\Movimiento\Auxiliar\Detalle $detalles;
|
||||
public function getDetalles(): ?Model\Contabilidad\Movimiento\Auxiliar\Detalle
|
||||
{
|
||||
if (!isset($this->detalles)) {
|
||||
try {
|
||||
$this->detalles = $this->runFactory('detalles');
|
||||
} catch (Implement\Exception\EmptyResult) {
|
||||
$this->detalles = null;
|
||||
}
|
||||
}
|
||||
return $this->detalles;
|
||||
}
|
||||
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return array_merge(parent::jsonSerialize(), [
|
||||
'movimiento_id' => $this->movimiento->id,
|
||||
'cargo' => $this->cargo,
|
||||
'abono' => $this->abono,
|
||||
'detalles' => $this->getDetalles()
|
||||
]);
|
||||
}
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
<?php
|
||||
namespace Incoviba\Model\Contabilidad\Movimiento\Auxiliar;
|
||||
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Model;
|
||||
|
||||
class Detalle extends Ideal\Model
|
||||
{
|
||||
public Model\Contabilidad\Movimiento\Auxiliar $auxiliar;
|
||||
public ?Model\Contabilidad\CentroCosto $centroCosto;
|
||||
public ?int $rut;
|
||||
public ?string $digito;
|
||||
public ?string $nombre;
|
||||
public ?string $categoria;
|
||||
public ?string $detalle;
|
||||
|
||||
public function rut(): string
|
||||
{
|
||||
return $this->rut . '-' . $this->digito;
|
||||
}
|
||||
public function rutFormatted(): string
|
||||
{
|
||||
return number_format($this->rut, 0, ',', '.') . '-' . $this->digito;
|
||||
}
|
||||
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return [
|
||||
'auxiliar_id' => $this->auxiliar->id,
|
||||
'centro_costo' => $this->centroCosto,
|
||||
'rut' => $this->rut,
|
||||
'digito' => $this->digito,
|
||||
'nombre' => $this->nombre,
|
||||
'categoria' => $this->categoria,
|
||||
'detalle' => $this->detalle,
|
||||
'rutFormatted' => $this->rutFormatted(),
|
||||
];
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace Incoviba\Model\Contabilidad\Movimiento;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Model;
|
||||
|
||||
@ -24,6 +25,9 @@ class Detalle extends Ideal\Model
|
||||
return number_format($this->rut, 0, ',', '.') . '-' . $this->digito;
|
||||
}
|
||||
|
||||
public ?Define\Model $relacionado;
|
||||
public ?string $relacionadoType;
|
||||
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return [
|
||||
@ -35,7 +39,9 @@ class Detalle extends Ideal\Model
|
||||
'categoria' => $this->categoria,
|
||||
'detalle' => $this->detalle,
|
||||
'identificador' => $this->identificador,
|
||||
'rutFormatted' => $this->rutFormatted()
|
||||
'rutFormatted' => $this->rutFormatted(),
|
||||
'relacionado' => $this->relacionado ?? null,
|
||||
'relacionadoType' => $this->relacionadoType ?? null
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -1,62 +0,0 @@
|
||||
<?php
|
||||
namespace Incoviba\Repository\Contabilidad\Movimiento;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Implement;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
|
||||
class Auxiliar extends Ideal\Repository
|
||||
{
|
||||
public function __construct(Define\Connection $connection, protected Repository\Contabilidad\Movimiento $movimientoRepository)
|
||||
{
|
||||
parent::__construct($connection);
|
||||
$this->setTable('movimientos_auxiliares');
|
||||
}
|
||||
|
||||
public function create(?array $data = null): Model\Contabilidad\Movimiento\Auxiliar
|
||||
{
|
||||
$map = (new Implement\Repository\MapperParser(['cargo', 'abono']))
|
||||
->register('movimiento_id', (new Implement\Repository\Mapper())
|
||||
->setProperty('movimiento')
|
||||
->setFunction(function($data) {
|
||||
return $this->movimientoRepository->fetchById($data['movimiento_id']);
|
||||
}));
|
||||
return $this->parseData(new Model\Contabilidad\Movimiento\Auxiliar(), $data, $map);
|
||||
}
|
||||
public function save(Define\Model $model): Model\Contabilidad\Movimiento\Auxiliar
|
||||
{
|
||||
$model->id = $this->saveNew([
|
||||
'movimiento_id',
|
||||
'cargo',
|
||||
'abono'
|
||||
],[
|
||||
$model->movimiento->id,
|
||||
$model->cargo,
|
||||
$model->abono
|
||||
]);
|
||||
return $model;
|
||||
}
|
||||
public function edit(Define\Model $model, array $new_data): Model\Contabilidad\Movimiento\Auxiliar
|
||||
{
|
||||
return $this->update($model, [
|
||||
'movimiento_id',
|
||||
'cargo',
|
||||
'abono'
|
||||
], $new_data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws EmptyResult
|
||||
*/
|
||||
public function fetchByMovimiento(int $movimiento_id): array
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where('movimiento_id = :movimiento_id');
|
||||
return $this->fetchMany($query, ['movimiento_id' => $movimiento_id]);
|
||||
}
|
||||
}
|
@ -1,72 +0,0 @@
|
||||
<?php
|
||||
namespace Incoviba\Repository\Contabilidad\Movimiento\Auxiliar;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Implement;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
|
||||
class Detalle extends Ideal\Repository
|
||||
{
|
||||
public function __construct(Define\Connection $connection,
|
||||
protected Repository\Contabilidad\Movimiento\Auxiliar $auxiliarRepository,
|
||||
protected Repository\Contabilidad\CentroCosto $centroCostoRepository)
|
||||
{
|
||||
parent::__construct($connection);
|
||||
$this->setTable('auxiliar_detalles');
|
||||
}
|
||||
|
||||
public function create(?array $data = null): Model\Contabilidad\Movimiento\Auxiliar\Detalle
|
||||
{
|
||||
$map = (new Implement\Repository\MapperParser(['rut', 'digito', 'nombre', 'categoria', 'detalle']))
|
||||
->register('auxiliar_id', (new Implement\Repository\Mapper())
|
||||
->setProperty('auxiliar')
|
||||
->setFunction(function($data) {
|
||||
return $this->auxiliarRepository->fetchById($data['auxiliar_id']);
|
||||
}))
|
||||
->register('centro_costo_id', (new Implement\Repository\Mapper())
|
||||
->setProperty('centroCosto')
|
||||
->setFunction(function($data) {
|
||||
return $this->centroCostoRepository->fetchById($data['centro_costo_id']);
|
||||
})
|
||||
->setDefault(null));
|
||||
return $this->parseData(new Model\Contabilidad\Movimiento\Auxiliar\Detalle(), $data, $map);
|
||||
}
|
||||
public function save(Define\Model $model): Model\Contabilidad\Movimiento\Auxiliar\Detalle
|
||||
{
|
||||
$this->saveNew([
|
||||
'auxiliar_id',
|
||||
'rut',
|
||||
'digito',
|
||||
'nombre',
|
||||
'categoria',
|
||||
'detalle'
|
||||
], [
|
||||
$model->auxiliar->id,
|
||||
$model->rut,
|
||||
$model->digito,
|
||||
$model->nombre,
|
||||
$model->categoria,
|
||||
$model->detalle
|
||||
]);
|
||||
return $model;
|
||||
}
|
||||
public function edit(Define\Model $model, array $new_data): Model\Contabilidad\Movimiento\Auxiliar\Detalle
|
||||
{
|
||||
return $this->update($model, ['rut', 'digito', 'nombre', 'categoria', 'detalle'], $new_data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws EmptyResult
|
||||
*/
|
||||
public function fetchByAuxiliar(int $auxiliar_id): array
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where('auxiliar_id = :auxiliar_id');
|
||||
return $this->fetchMany($query, ['auxiliar_id' => $auxiliar_id]);
|
||||
}
|
||||
}
|
@ -20,15 +20,18 @@ class Proveedor extends Ideal\Repository
|
||||
public function create(?array $data = null): Model\Inmobiliaria\Proveedor
|
||||
{
|
||||
$map = (new Implement\Repository\MapperParser(['rut', 'digito', 'nombre', 'razon']))
|
||||
->register('contacto_rut', (new Implement\Repository\Mapper())->setProperty('contacto')->setFunction(function($data) {
|
||||
return $this->personaService->getById($data['contacto_rut']);
|
||||
}));
|
||||
->register('contacto_rut', (new Implement\Repository\Mapper())
|
||||
->setProperty('contacto')
|
||||
->setFunction(function($data) {
|
||||
return $this->personaService->getById($data['contacto_rut']);
|
||||
})
|
||||
->setDefault(null));
|
||||
return $this->parseData(new Model\Inmobiliaria\Proveedor(), $data, $map);
|
||||
}
|
||||
public function save(Define\Model $model): Model\Inmobiliaria\Proveedor
|
||||
{
|
||||
$this->saveNew(['rut', 'digito', 'nombre', 'razon', 'contacto_rut'], [
|
||||
$model->rut, $model->digito, $model->nombre, $model->razon, $model->contacto->rut
|
||||
$model->rut, $model->digito, $model->nombre, $model->razon, $model->contacto?->rut
|
||||
]);
|
||||
return $model;
|
||||
}
|
||||
|
@ -2,8 +2,10 @@
|
||||
namespace Incoviba\Service\Contabilidad;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Ideal\Service;
|
||||
use Incoviba\Common\Implement;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
use Psr\Log\LoggerInterface;
|
||||
@ -13,7 +15,7 @@ class Movimiento extends Service
|
||||
public function __construct(LoggerInterface $logger,
|
||||
protected Repository\Contabilidad\Movimiento $movimientoRepository,
|
||||
protected Repository\Contabilidad\Movimiento\Detalle $detalleRepository,
|
||||
protected Movimiento\Auxiliar $auxiliarService)
|
||||
protected Movimiento\Detalle $detalleService)
|
||||
{
|
||||
parent::__construct($logger);
|
||||
}
|
||||
@ -76,12 +78,20 @@ class Movimiento extends Service
|
||||
$this->movimientoRepository->remove($movimiento);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws EmptyResult
|
||||
*/
|
||||
public function findRut(int $rut): Define\Model
|
||||
{
|
||||
return $this->detalleService->findRut($rut);
|
||||
}
|
||||
|
||||
public function process(Model\Contabilidad\Movimiento $movimiento): Model\Contabilidad\Movimiento
|
||||
{
|
||||
$movimiento->addFactory('detalles', (new Implement\Repository\Factory())
|
||||
->setCallable(function(int $movimiento_id) {
|
||||
try {
|
||||
return $this->detalleRepository->fetchByMovimiento($movimiento_id);
|
||||
return $this->detalleService->getByMovimiento($movimiento_id);
|
||||
} catch (Implement\Exception\EmptyResult) {
|
||||
return null;
|
||||
}
|
||||
|
@ -1,42 +0,0 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Contabilidad\Movimiento;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Implement;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Service;
|
||||
|
||||
class Auxiliar extends Ideal\Service
|
||||
{
|
||||
public function __construct(LoggerInterface $logger,
|
||||
protected Repository\Contabilidad\Movimiento\Auxiliar $auxiliarRepository,
|
||||
protected Repository\Contabilidad\Movimiento\Auxiliar\Detalle $detalleRepository)
|
||||
{
|
||||
parent::__construct($logger);
|
||||
}
|
||||
|
||||
public function getByMovimiento(int $movimiento_id): ?array
|
||||
{
|
||||
try {
|
||||
return array_map([$this, 'process'], $this->auxiliarRepository->fetchByMovimiento($movimiento_id));
|
||||
} catch (Implement\Exception\EmptyResult) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected function process(Model\Contabilidad\Movimiento\Auxiliar $auxiliar): Model\Contabilidad\Movimiento\Auxiliar
|
||||
{
|
||||
$auxiliar->addFactory('detalles', (new Implement\Repository\Factory())
|
||||
->setCallable(function(int $auxiliar_id) {
|
||||
try {
|
||||
return $this->detalleRepository->fetchByAuxiliar($auxiliar_id);
|
||||
} catch (Implement\Exception\EmptyResult) {
|
||||
return null;
|
||||
}
|
||||
})
|
||||
->setArgs(['auxiliar_id' => $auxiliar->id]));
|
||||
return $auxiliar;
|
||||
}
|
||||
}
|
63
app/src/Service/Contabilidad/Movimiento/Detalle.php
Normal file
63
app/src/Service/Contabilidad/Movimiento/Detalle.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Contabilidad\Movimiento;
|
||||
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Implement;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Model;
|
||||
|
||||
class Detalle extends Ideal\Service
|
||||
{
|
||||
public function __construct(LoggerInterface $logger,
|
||||
protected Repository\Contabilidad\Movimiento\Detalle $detalleRepository,
|
||||
protected Repository\Venta\Propietario $propietarioRepository,
|
||||
protected Repository\Inmobiliaria\Proveedor $proveedorRepository,
|
||||
protected Repository\Inmobiliaria $inmobiliariaRepository)
|
||||
{
|
||||
parent::__construct($logger);
|
||||
}
|
||||
|
||||
public function getByMovimiento(int $movimiento_id): ?Model\Contabilidad\Movimiento\Detalle
|
||||
{
|
||||
try {
|
||||
return $this->process($this->detalleRepository->fetchByMovimiento($movimiento_id));
|
||||
} catch (Implement\Exception\EmptyResult) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws EmptyResult
|
||||
*/
|
||||
public function findRut(int $rut): Define\Model
|
||||
{
|
||||
try {
|
||||
return $this->inmobiliariaRepository->fetchById($rut);
|
||||
} catch (Implement\Exception\EmptyResult) {}
|
||||
try {
|
||||
return $this->proveedorRepository->fetchById($rut);
|
||||
} catch (Implement\Exception\EmptyResult) {}
|
||||
try {
|
||||
return $this->propietarioRepository->fetchById($rut);
|
||||
} catch (Implement\Exception\EmptyResult) {}
|
||||
|
||||
throw new Implement\Exception\EmptyResult("{$rut} no encontrado.");
|
||||
}
|
||||
|
||||
protected function process(Model\Contabilidad\Movimiento\Detalle $detalle): Model\Contabilidad\Movimiento\Detalle
|
||||
{
|
||||
if (empty($detalle->rut)) {
|
||||
return $detalle;
|
||||
}
|
||||
|
||||
try {
|
||||
$detalle->relacionado = $this->findRut($detalle->rut);
|
||||
$detalle->relacionadoType = strtolower(last(explode('\\', get_class($detalle->relacionado))));
|
||||
} catch (Implement\Exception\EmptyResult) {}
|
||||
|
||||
return $detalle;
|
||||
}
|
||||
}
|
@ -42,14 +42,16 @@ class Proveedor extends Ideal\Service
|
||||
$filteredData = $this->proveedorRepository->filterData($data);
|
||||
try {
|
||||
$proveedor = $this->process($this->proveedorRepository->fetchByNombre($filteredData['nombre']));
|
||||
if ($proveedor->contacto->rut !== $data['contacto']['rut']) {
|
||||
if (!empty($data['contacto']['rut']) and $proveedor->contacto->rut !== $data['contacto']['rut']) {
|
||||
$contacto = $this->contactoService->add($data['contacto']);
|
||||
return $this->proveedorRepository->edit($proveedor, ['contacto_rut' => $contacto->rut]);
|
||||
}
|
||||
return $proveedor;
|
||||
} catch (Implement\Exception\EmptyResult) {
|
||||
$contacto = $this->contactoService->add($data['contacto']);
|
||||
$filteredData['contacto_rut'] = $contacto->rut;
|
||||
if (!empty($data['contacto']['rut'])) {
|
||||
$contacto = $this->contactoService->add($data['contacto']);
|
||||
$filteredData['contacto_rut'] = $contacto->rut;
|
||||
}
|
||||
$proveedor = $this->proveedorRepository->create($filteredData);
|
||||
return $this->process($this->proveedorRepository->save($proveedor));
|
||||
}
|
||||
@ -61,12 +63,15 @@ class Proveedor extends Ideal\Service
|
||||
*/
|
||||
public function edit(Model\Inmobiliaria\Proveedor $proveedor, array $data): Model\Inmobiliaria\Proveedor
|
||||
{
|
||||
$filteredData = $this->proveedorRepository->filterData($data);
|
||||
try {
|
||||
if ($proveedor->contacto->rut !== $data['contacto']['rut']) {
|
||||
if (!empty($data['contacto']['rut'])) {
|
||||
$contacto = $this->contactoService->getById($data['contacto']['rut']);
|
||||
$this->contactoService->edit($contacto, $data['contacto']);
|
||||
$contacto = $this->contactoService->edit($contacto, $data['contacto']);
|
||||
$data['contacto_rut'] = $contacto->rut;
|
||||
} elseif ($proveedor->contacto !== null) {
|
||||
$data['contacto_rut'] = $proveedor->contacto->rut;
|
||||
}
|
||||
$filteredData = $this->proveedorRepository->filterData($data);
|
||||
return $this->process($this->proveedorRepository->edit($proveedor, $filteredData));
|
||||
} catch (Implement\Exception\EmptyResult) {
|
||||
return $proveedor;
|
||||
|
Reference in New Issue
Block a user