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