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

@ -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;
}
}