Limpieza de input de valor y filtro de datos a nivel Repo
This commit is contained in:
70
app/src/Service/Venta/Escritura.php
Normal file
70
app/src/Service/Venta/Escritura.php
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user