83 lines
3.0 KiB
PHP
83 lines
3.0 KiB
PHP
<?php
|
|
namespace Incoviba\Service\Venta;
|
|
|
|
use Exception;
|
|
use DateTimeImmutable;
|
|
use DateMalformedStringException;
|
|
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
|
|
*/
|
|
public function edit(int $venta_id, array $data): Model\Venta\Escritura
|
|
{
|
|
$venta = $this->ventaService->getById($venta_id);
|
|
$estado = $venta->currentEstado();
|
|
if (!in_array($estado->tipoEstadoVenta->descripcion, ['escriturando', 'firmado por inmobiliaria'])) {
|
|
throw new EmptyResult('');
|
|
}
|
|
try {
|
|
$data['fecha'] = (new DateTimeImmutable($data['fecha']))->format('Y-m-d');
|
|
} catch (DateMalformedStringException) {
|
|
unset($data['fecha']);
|
|
}
|
|
|
|
$escritura = $venta->formaPago()->escritura;
|
|
$pagoData = array_intersect_key($data, array_flip(['valor', 'fecha']));
|
|
$pago = $escritura->pago;
|
|
$this->escrituraRepository->edit($escritura, $pagoData);
|
|
$this->pagoService->edit($pago, $pagoData);
|
|
$this->pagoService->updateEstado($pago, $data);
|
|
|
|
return $this->escrituraRepository->fetchById($escritura->id);
|
|
}
|
|
}
|