Limpieza de input de valor y filtro de datos a nivel Repo

This commit is contained in:
Juan Pablo Vial
2024-07-03 15:13:13 -04:00
parent d5b9be0196
commit d68eba5697
28 changed files with 436 additions and 189 deletions

View File

@ -10,7 +10,9 @@ class BonoPie
public function add(array $data): Model\Venta\BonoPie
{
return new Model\Venta\BonoPie();
$filteredData = $this->bonoPieRepository->filterData($data);
$bono = $this->bonoPieRepository->create($filteredData);
return $this->bonoPieRepository->save($bono);
}
public function getByVenta(int $venta_id): Model\Venta\BonoPie
{

View File

@ -1,23 +1,25 @@
<?php
namespace Incoviba\Service\Venta;
use Exception;
use DateTimeImmutable;
use Incoviba\Common\Ideal\Service;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal;
use Incoviba\Repository;
use Incoviba\Model;
use Incoviba\Service\Money;
use Psr\Log\LoggerInterface;
use Incoviba\Service;
class Credito extends Service
class Credito extends Ideal\Service
{
public function __construct(
LoggerInterface $logger,
protected Repository\Venta\Credito $creditoRepository,
protected Repository\Venta\Pago $pagoRepository,
protected Pago $pagoService,
protected Repository\Venta\TipoPago $tipoPagoRepository,
protected Repository\Venta\EstadoPago $estadoPagoRepository,
protected Repository\Venta\TipoEstadoPago $tipoEstadoPagoRepository,
protected Money $moneyService
protected Service\Money $moneyService,
protected Service\Valor $valorService
) {
parent::__construct($logger);
}
@ -27,19 +29,32 @@ class Credito extends Service
return $this->creditoRepository->fetchByVenta($venta_id);
}
/**
* @throws Exception
*/
public function add(array $data): Model\Venta\Credito
{
$fecha = new DateTimeImmutable($data['fecha']);
$uf = $data['uf'] ?? $this->moneyService->getUF($fecha);
$uf = $this->valorService->clean($data['uf']) ?? $this->moneyService->getUF($fecha);
$tipoPago = $this->tipoPagoRepository->fetchByDescripcion('carta de resguardo');
$pago = $this->addPago(['fecha' => $fecha->format('Y-m-d'), 'valor' => $data['valor'] * $uf, 'uf' => $uf, 'tipo' => $tipoPago->id]);
$valor = $this->valorService->clean($data['valor']);
$pago = $this->pagoService->add([
'fecha' => $fecha->format('Y-m-d'),
'valor' => $valor * $uf,
'uf' => $uf,
'tipo' => $tipoPago->id
]);
$credito = $this->creditoRepository->create([
'valor' => $data['valor'],
'valor' => $valor,
'fecha' => $fecha->format('Y-m-d'),
'pago' => $pago->id
]);
return $this->creditoRepository->save($credito);
}
/**
* @throws Exception
*/
public function edit(Model\Venta\Credito $credito, array $data): Model\Venta\Credito
{
$uf = $this->moneyService->getUF($credito->pago->fecha);
@ -50,7 +65,8 @@ class Credito extends Service
$data['uf'] = $uf;
}
if (array_key_exists('valor', $data)) {
$data['valor'] = round(((float) $data['valor']) * $uf);
$data['valor'] = $this->valorService->clean($data['valor']);
$valorPago = round($data['valor'] * $uf);
}
$filteredData = array_intersect_key($data, array_fill_keys([
'fecha',
@ -58,21 +74,12 @@ class Credito extends Service
'valor',
'banco'
], 0));
$credito->pago = $this->pagoRepository->edit($credito->pago, $filteredData);
$filteredDataPago = $filteredData;
if (isset($valorPago)) {
$filteredDataPago['valor'] = $valorPago;
}
$credito->pago = $this->pagoService->edit($credito->pago, $filteredDataPago);
return $this->creditoRepository->edit($credito, $filteredData);
}
protected function addPago(array $data): Model\Venta\Pago
{
$pago = $this->pagoRepository->create($data);
$pago = $this->pagoRepository->save($pago);
$tipoEstado = $this->tipoEstadoPagoRepository->fetchByDescripcion('no pagado');
$data = [
'pago' => $pago->id,
'fecha' => $pago->fecha->format('Y-m-d'),
'estado' => $tipoEstado->id
];
$estado = $this->estadoPagoRepository->create($data);
$this->estadoPagoRepository->save($estado);
return $pago;
}
}

View File

@ -98,19 +98,10 @@ class Cuota
$filtered_data = array_intersect_key($data, $fields);
$pago_data = array_merge($filtered_data, ['tipo' => $tipoPago->id]);
$pago = $this->pagoService->add($pago_data);
$data['pago'] = $pago->id;
$data['estado'] = $pago->currentEstado->tipoEstadoPago->id;
$fields = array_fill_keys([
'pie',
'fecha',
'valor',
'estado',
'banco',
'uf',
'pago',
'numero'
], 0);
$filtered_data = array_intersect_key($data, $fields);
$filtered_data = $this->cuotaRepository->filterData($data);
$mapped_data = $filtered_data;
$mapped_data['valor_$'] = $mapped_data['valor'];
unset($mapped_data['valor']);

View File

@ -0,0 +1,70 @@
<?php
namespace Incoviba\Service\Venta;
use Exception;
use DateTimeImmutable;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Repository;
use Incoviba\Service;
use Incoviba\Model;
class Escritura extends Ideal\Service
{
public function __construct(LoggerInterface $logger, protected Repository\Venta $ventaRepository,
protected Service\Venta $ventaService,
protected Repository\Venta\Escritura $escrituraRepository,
protected Repository\Venta\EstadoVenta $estadoVentaRepository,
protected Service\Venta\Pago $pagoService, protected Service\UF $ufService,
protected Service\Valor $valorService)
{
parent::__construct($logger);
}
/**
* @throws EmptyResult
* @throws Exception
*/
public function add(int $venta_id, array $data): Model\Venta\Escritura
{
$venta = $this->ventaService->getById($venta_id);
if (isset($venta->formaPago()->escritura)) {
throw new EmptyResult('');
}
$fecha = new DateTimeImmutable($data['fecha']);
$uf = $this->ufService->get($fecha);
$valor = $this->valorService->clean($data['valor']) * (($data['uf']) ? $uf : 1);
$pago = $this->pagoService->add([
'fecha' => $fecha->format('Y-m-d'),
'valor' => $valor,
'banco' => $data['banco'],
'tipo' => $data['tipo'],
'uf' => $uf
]);
$escritura = $this->escrituraRepository->create([
'valor' => $valor,
'fecha' => $fecha->format('Y-m-d'),
'uf' => $uf,
'pago' => $pago->id
]);
$escritura = $this->escrituraRepository->save($escritura);
$this->ventaRepository->edit($venta, ['escritura' => $escritura->id]);
return $escritura;
}
/**
* @throws EmptyResult
* @throws Exception
*/
public function edit(int $venta_id, array $data): Model\Venta\EstadoVenta
{
$venta = $this->ventaService->getById($venta_id);
$estado = $venta->currentEstado();
if (!in_array($estado->tipoEstadoVenta->descripcion, ['escriturando', 'firmado por inmobiliaria'])) {
throw new EmptyResult('');
}
$data['fecha'] = (new DateTimeImmutable($data['fecha']))->format('Y-m-d');
return $this->estadoVentaRepository->edit($estado, $data);
}
}

View File

@ -1,6 +1,7 @@
<?php
namespace Incoviba\Service\Venta;
use Incoviba\Service\Valor;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Implement;
use Incoviba\Common\Ideal;
@ -15,7 +16,8 @@ class FormaPago extends Ideal\Service
protected Credito $creditoService,
protected Repository\Venta\Escritura $escrituraRepository,
protected Subsidio $subsidioService,
protected Pago $pagoService)
protected Pago $pagoService,
protected Valor $valorService)
{
parent::__construct($logger);
}
@ -83,7 +85,7 @@ class FormaPago extends Ideal\Service
'cuotas',
'uf'
], $filtered_data);
$mapped_data['valor'] = $this->cleanValue($mapped_data['valor']);
$mapped_data['valor'] = $this->valorService->clean($mapped_data['valor']);
return $this->pieService->add($mapped_data);
}
protected function addSubsidio(array $data): Model\Venta\Subsidio
@ -101,6 +103,8 @@ class FormaPago extends Ideal\Service
'subsidio',
'uf'
], $filtered_data);
$mapped_data['ahorro'] = $this->valorService->clean($mapped_data['ahorro']);
$mapped_data['subsidio'] = $this->valorService->clean($mapped_data['subsidio']);
return $this->subsidioService->add($mapped_data);
}
protected function addCredito(array $data): Model\Venta\Credito
@ -116,6 +120,7 @@ class FormaPago extends Ideal\Service
'valor',
'uf'
], $filtered_data);
$mapped_data['valor'] = $this->valorService->clean($mapped_data['valor']);
return $this->creditoService->add($mapped_data);
}
protected function addBonoPie(array $data): Model\Venta\BonoPie
@ -129,14 +134,7 @@ class FormaPago extends Ideal\Service
'fecha',
'valor'
], $filtered_data);
$mapped_data['valor'] = $this->valorService->clean($mapped_data['valor']);
return $this->bonoPieService->add($mapped_data);
}
protected function cleanValue($value): float
{
if ((float) $value == $value) {
return (float) $value;
}
return (float) str_replace(['.', ','], ['', '.'], $value);
}
}

View File

@ -1,11 +1,11 @@
<?php
namespace Incoviba\Service\Venta;
use Exception;
use DateTimeInterface;
use DateTimeImmutable;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Service\Money;
use PDOException;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Repository;
use Incoviba\Model;
use Incoviba\Service;
@ -16,7 +16,8 @@ class Pago
protected Repository\Venta\Pago $pagoRepository,
protected Repository\Venta\EstadoPago $estadoPagoRepository,
protected Repository\Venta\TipoEstadoPago $tipoEstadoPagoRepository,
protected Service\UF $ufService
protected Service\UF $ufService,
protected Service\Valor $valorService
) {}
public function depositar(Model\Venta\Pago $pago, DateTimeInterface $fecha): bool
@ -115,24 +116,19 @@ class Pago
return $this->process($this->pagoRepository->fetchDevolucionByVenta($venta_id));
}
/**
* @throws \Exception
*/
public function add(array $data): Model\Venta\Pago
{
if (!isset($data['uf'])) {
$data['uf'] = $this->ufService->get(new DateTimeImmutable($data['fecha']));
}
$fields = array_fill_keys([
'valor',
'banco',
'tipo',
'identificador',
'fecha',
'uf',
'pagador',
'asociado'
], 0);
$filtered_data = array_intersect_key($data, $fields);
$filtered_data = $this->pagoRepository->filterData($data);
$filtered_data['valor'] = round($this->valorService->clean($filtered_data['valor']));
$pago = $this->pagoRepository->create($filtered_data);
$pago = $this->pagoRepository->save($pago);
$tipoEstado = $this->tipoEstadoPagoRepository->fetchByDescripcion('no pagado');
$estado = $this->estadoPagoRepository->create([
'pago' => $pago->id,
@ -143,6 +139,24 @@ class Pago
$pago->currentEstado = $estado;
return $pago;
}
/**
* @throws Exception
*/
public function edit(Model\Venta\Pago $pago, array $data): Model\Venta\Pago
{
if (array_key_exists('fecha', $data)) {
$data['fecha'] = (new DateTimeImmutable($data['fecha']))->format('Y-m-d');
}
if (array_key_exists('uf', $data)) {
$data['uf'] = $this->ufService->get(new DateTimeImmutable($data['fecha']));
}
$filteredData = $this->pagoRepository->filterData($data);
$filteredData['valor'] = round($this->valorService->clean($filteredData['valor']));
$pago = $this->pagoRepository->edit($pago, $filteredData);
$pago->currentEstado = $this->estadoPagoRepository->fetchCurrentByPago($pago->id);
return $pago;
}
public function delete(Model\Venta\Pago $pago): bool
{
try {

View File

@ -25,7 +25,8 @@ class Pie
public function add(array $data): Model\Venta\Pie
{
$pie = $this->pieRepository->create($data);
$filteredData = $this->pieRepository->filterData($data);
$pie = $this->pieRepository->create($filteredData);
return $this->pieRepository->save($pie);
}
public function addCuota(array $data): Model\Venta\Cuota
@ -34,7 +35,8 @@ class Pie
}
public function edit(Model\Venta\Pie $pie, array $data): Model\Venta\Pie
{
return $this->pieRepository->edit($pie, $data);
$filteredData = $this->pieRepository->filterData($data);
return $this->pieRepository->edit($pie, $filteredData);
}
public function reajustar(Model\Venta\Pie $pie, array $data): Model\Venta\Pie
{

View File

@ -1,6 +1,7 @@
<?php
namespace Incoviba\Service\Venta;
use Exception;
use DateTimeImmutable;
use Incoviba\Repository;
use Incoviba\Model;
@ -10,11 +11,12 @@ class Subsidio
{
public function __construct(
protected Repository\Venta\Subsidio $subsidioRepository,
protected Repository\Venta\Pago $pagoRepository,
protected Pago $pagoService,
protected Repository\Venta\TipoPago $tipoPagoRepository,
protected Repository\Venta\EstadoPago $estadoPagoRepository,
protected Repository\Venta\TipoEstadoPago $tipoEstadoPagoRepository,
protected Service\Money $moneyService
protected Service\Money $moneyService,
protected Service\Valor $valorService
) {}
public function getByVenta(int $venta_id): Model\Venta\Subsidio
@ -22,28 +24,17 @@ class Subsidio
return $this->subsidioRepository->fetchByVenta($venta_id);
}
/**
* @throws Exception
*/
public function add(array $data): Model\Venta\Subsidio
{
$fecha = new DateTimeImmutable($data['fecha']);
$uf = $data['uf'] ?? $this->moneyService->getUF($fecha);
$tipoPago = $this->tipoPagoRepository->fetchByDescripcion('vale vista');
$ahorro = $this->addPago(['fecha' => $fecha->format('Y-m-d'), 'valor' => $data['ahorro'] * $uf, 'uf' => $uf, 'tipo' => $tipoPago->id]);
$subsidio = $this->addPago(['fecha' => $fecha->format('Y-m-d'), 'valor' => $data['subsidio'] * $uf, 'uf' => $uf, 'tipo' => $tipoPago->id]);
$subsidio = $this->subsidioRepository->create(['pago' => $ahorro->id, 'subsidio' => $subsidio->id]);
$ahorro = $this->pagoService->add(['fecha' => $fecha->format('Y-m-d'), 'valor' => $this->valorService->clean($data['ahorro']) * $uf, 'uf' => $uf, 'tipo' => $tipoPago->id]);
$subsidioPago = $this->pagoService->add(['fecha' => $fecha->format('Y-m-d'), 'valor' => $this->valorService->clean($data['subsidio']) * $uf, 'uf' => $uf, 'tipo' => $tipoPago->id]);
$subsidio = $this->subsidioRepository->create(['pago' => $ahorro->id, 'subsidio' => $subsidioPago->id]);
return $this->subsidioRepository->save($subsidio);
}
protected function addPago(array $data): Model\Venta\Pago
{
$pago = $this->pagoRepository->create($data);
$pago = $this->pagoRepository->save($pago);
$tipoEstado = $this->tipoEstadoPagoRepository->fetchByDescripcion('no pagado');
$data = [
'pago' => $pago->id,
'fecha' => $pago->fecha->format('Y-m-d'),
'estado' => $tipoEstado->id
];
$estado = $this->estadoPagoRepository->create($data);
$this->estadoPagoRepository->save($estado);
return $pago;
}
}

View File

@ -10,6 +10,7 @@ class Unidad
{
public function __construct(
protected Repository\Venta\Unidad $unidadRepository,
protected Repository\Venta\Unidad\Prorrateo $unidadProrrateoRepository,
protected Precio $precioService
) {}
@ -38,6 +39,22 @@ class Unidad
return $this->unidadRepository->fetchByIdForSearch($unidad_id);
}
public function editProrrateo(int $unidad_id, array $new_data): Model\Venta\Unidad
{
$model = $this->unidadRepository->fetchById($unidad_id);
$filteredData = $this->unidadProrrateoRepository->filterData($new_data);
try {
$prorrateo = $this->unidadProrrateoRepository->fetchByUnidad($model->id);
$prorrateo = $this->unidadProrrateoRepository->edit($prorrateo, $filteredData);
} catch (PDOException | EmptyResult) {
$filteredData['unidad_id'] = $model->id;
$prorrateo = $this->unidadProrrateoRepository->create($filteredData);
$this->unidadProrrateoRepository->save($prorrateo);
}
$model->prorrateo = $prorrateo->prorrateo;
return $model;
}
protected function process($unidad): Model\Venta\Unidad
{
try {