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

@ -161,4 +161,9 @@ abstract class Repository implements Define\Repository
} }
return $results; return $results;
} }
public function filterData(array $data): array
{
return $data;
}
} }

View File

@ -6,7 +6,7 @@ use Throwable;
class EmptyResult extends Exception class EmptyResult extends Exception
{ {
public function __construct(string $query, ?Throwable $previous = null) public function __construct(public string $query, ?Throwable $previous = null)
{ {
$message = "Empty results for {$query}"; $message = "Empty results for {$query}";
$code = 700; $code = 700;

View File

@ -14,51 +14,23 @@ class Escrituras
use withJson; use withJson;
public function add(ServerRequestInterface $request, ResponseInterface $response, public function add(ServerRequestInterface $request, ResponseInterface $response,
Repository\Venta $ventaRepository, Service\Venta $ventaService, Service\Venta\Escritura $escrituraService, int $venta_id): ResponseInterface
Repository\Venta\Escritura $escrituraRepository, Service\Venta\Pago $pagoService,
Service\UF $ufService, int $venta_id): ResponseInterface
{ {
$body = $request->getParsedBody(); $body = $request->getParsedBody();
$output = [ $output = [
'venta_id' => $venta_id, 'venta_id' => $venta_id,
'input' => $body, 'input' => $body,
'escritura' => null,
'status' => false 'status' => false
]; ];
try { try {
$venta = $ventaService->getById($venta_id); $output['escritura'] = $escrituraService->add($venta_id, $body);
if (isset($venta->formaPago()->escritura)) {
throw new EmptyResult('');
}
$fecha = new DateTimeImmutable($body['fecha']);
$uf = $ufService->get($fecha);
$valor = $body['valor'];
if (str_contains($valor, ',')) {
$valor = str_replace(['.', ','], ['', '.'], $valor);
}
$valor = ((float) $valor) * (($body['uf']) ? $uf : 1);
$data = [
'fecha' => $fecha->format('Y-m-d'),
'valor' => $valor,
'banco' => $body['banco'],
'tipo' => $body['tipo'],
'uf' => $uf
];
$pago = $pagoService->add($data);
$data = [
'valor' => $valor,
'fecha' => $fecha->format('Y-m-d'),
'uf' => $uf,
'pago' => $pago->id
];
$escritura = $escrituraRepository->create($data);
$escrituraRepository->save($escritura);
$ventaRepository->edit($venta, ['escritura' => $escritura->id]);
$output['status'] = true; $output['status'] = true;
} catch (EmptyResult) {} } catch (EmptyResult) {}
return $this->withJson($response, $output); return $this->withJson($response, $output);
} }
public function edit(ServerRequestInterface $request, ResponseInterface $response, public function edit(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta $ventaService, Repository\Venta\EstadoVenta $estadoVentaRepository, Service\Venta\Escritura $escrituraService,
int $venta_id): ResponseInterface int $venta_id): ResponseInterface
{ {
$body = $request->getParsedBody(); $body = $request->getParsedBody();
@ -68,13 +40,7 @@ class Escrituras
'edited' => false 'edited' => false
]; ];
try { try {
$venta = $ventaService->getById($venta_id); $escrituraService->edit($venta_id, $body);
$estado = $venta->currentEstado();
if (!in_array($estado->tipoEstadoVenta->descripcion, ['escriturando', 'firmado por inmobiliaria'])) {
throw new EmptyResult('');
}
$body['fecha'] = (new DateTimeImmutable($body['fecha']))->format('Y-m-d');
$estadoVentaRepository->edit($estado, $body);
$output['edited'] = true; $output['edited'] = true;
} catch (EmptyResult) {} } catch (EmptyResult) {}
return $this->withJson($response, $output); return $this->withJson($response, $output);

View File

@ -34,8 +34,7 @@ class Facturacion extends Ideal\Service
return $this->withJson($response, $output); return $this->withJson($response, $output);
} }
public function ventas(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService, public function ventas(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
Service\Venta $ventaService, Service\Proyecto\Terreno $terrenoService, Service\UF $ufService, Service\Venta $ventaService): ResponseInterface
Service\IPC $ipcService): ResponseInterface
{ {
$input = $request->getParsedBody(); $input = $request->getParsedBody();
$output = [ $output = [

View File

@ -1,7 +1,9 @@
<?php <?php
namespace Incoviba\Controller\API\Ventas; namespace Incoviba\Controller\API\Ventas;
use Exception;
use DateTimeImmutable; use DateTimeImmutable;
use Incoviba\Model\Venta\Pago;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Implement\Exception\EmptyResult; use Incoviba\Common\Implement\Exception\EmptyResult;
@ -26,6 +28,15 @@ class Pagos
} catch (EmptyResult) {} } catch (EmptyResult) {}
return $this->withJson($response, $output); return $this->withJson($response, $output);
} }
/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param Repository\Venta\Pago $pagoRepository
* @param int $pago_id
* @return ResponseInterface
* @throws Exception
*/
public function edit(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Pago $pagoRepository, int $pago_id): ResponseInterface public function edit(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Pago $pagoRepository, int $pago_id): ResponseInterface
{ {
$body = $request->getParsedBody(); $body = $request->getParsedBody();
@ -44,6 +55,16 @@ class Pagos
} catch (EmptyResult) {} } catch (EmptyResult) {}
return $this->withJson($response, $output); return $this->withJson($response, $output);
} }
/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param Service\Venta\Pago $pagoService
* @param Service\Format $formatService
* @param int $pago_id
* @return ResponseInterface
* @throws Exception
*/
public function depositar(ServerRequestInterface $request, ResponseInterface $response, public function depositar(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\Pago $pagoService, Service\Format $formatService, int $pago_id): ResponseInterface Service\Venta\Pago $pagoService, Service\Format $formatService, int $pago_id): ResponseInterface
{ {
@ -63,6 +84,16 @@ class Pagos
} catch (EmptyResult) {} } catch (EmptyResult) {}
return $this->withJson($response, $output); return $this->withJson($response, $output);
} }
/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param Service\Venta\Pago $pagoService
* @param Service\Format $formatService
* @param int $pago_id
* @return ResponseInterface
* @throws Exception
*/
public function abonar(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService, public function abonar(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService,
Service\Format $formatService, int $pago_id): ResponseInterface Service\Format $formatService, int $pago_id): ResponseInterface
{ {
@ -83,6 +114,15 @@ class Pagos
} catch (EmptyResult) {} } catch (EmptyResult) {}
return $this->withJson($response, $output); return $this->withJson($response, $output);
} }
/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param Service\Venta\Pago $pagoService
* @param int $pago_id
* @return ResponseInterface
* @throws Exception
*/
public function devolver(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService, int $pago_id): ResponseInterface public function devolver(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService, int $pago_id): ResponseInterface
{ {
$body = $request->getParsedBody(); $body = $request->getParsedBody();
@ -102,34 +142,49 @@ class Pagos
public function para_pendientes(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService): ResponseInterface public function para_pendientes(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService): ResponseInterface
{ {
$pagos = $pagoService->getPendientes(); $pagos = $pagoService->getPendientes();
$pagos_pendientes = []; $pagos_pendientes = array_map(function(Pago $pago) {
return [
'id' => $pago->id
];
}, $pagos);
/*$pagos_pendientes = [];
foreach ($pagos as $pago) { foreach ($pagos as $pago) {
$pagos_pendientes []= [ $pagos_pendientes []= [
'id' => $pago->id 'id' => $pago->id
]; ];
} }*/
return $this->withJson($response, ['pagos' => $pagos_pendientes, 'total' => count($pagos_pendientes)]); return $this->withJson($response, ['pagos' => $pagos_pendientes, 'total' => count($pagos_pendientes)]);
} }
public function para_abonar(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService): ResponseInterface public function para_abonar(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService): ResponseInterface
{ {
$pagos = $pagoService->getDepositados(); $pagos = $pagoService->getDepositados();
$pagos_depositados = []; $pagos_depositados = array_map(function(Pago $pago) {
return [
'id' => $pago->id
];
}, $pagos);
/*$pagos_depositados = [];
foreach ($pagos as $pago) { foreach ($pagos as $pago) {
$pagos_depositados []= [ $pagos_depositados []= [
'id' => $pago->id 'id' => $pago->id
]; ];
} }*/
return $this->withJson($response, ['pagos' => $pagos_depositados, 'total' => count($pagos_depositados)]); return $this->withJson($response, ['pagos' => $pagos_depositados, 'total' => count($pagos_depositados)]);
} }
public function rebotes(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService): ResponseInterface public function rebotes(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService): ResponseInterface
{ {
$pagos = $pagoService->getRebotes(); $pagos = $pagoService->getRebotes();
$rebotes = []; $rebotes = array_map(function(Pago $pago) {
return [
'id' => $pago->id
];
}, $pagos);
/*$rebotes = [];
foreach ($pagos as $pago) { foreach ($pagos as $pago) {
$rebotes []= [ $rebotes []= [
'id' => $pago->id 'id' => $pago->id
]; ];
} }*/
return $this->withJson($response, ['pagos' => $rebotes, 'total' => count($rebotes)]); return $this->withJson($response, ['pagos' => $rebotes, 'total' => count($rebotes)]);
} }

View File

@ -34,7 +34,10 @@ class Unidades
} }
return $this->withJson($response, $output); return $this->withJson($response, $output);
} }
public function disponibles(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Unidad $unidadRepository, Repository\Proyecto\TipoUnidad $tipoUnidadRepository, Service\Redis $redisService): ResponseInterface public function disponibles(ServerRequestInterface $request, ResponseInterface $response,
Repository\Venta\Unidad $unidadRepository,
Repository\Proyecto\TipoUnidad $tipoUnidadRepository,
Service\Redis $redisService): ResponseInterface
{ {
$body = $request->getBody(); $body = $request->getBody();
$json = json_decode($body->getContents()); $json = json_decode($body->getContents());
@ -83,7 +86,10 @@ class Unidades
} }
return $this->withJson($response, $output); return $this->withJson($response, $output);
} }
public function prorrateo(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Unidad $unidadRepository, int $unidad_id): ResponseInterface public function prorrateo(ServerRequestInterface $request, ResponseInterface $response,
Repository\Venta\Unidad $unidadRepository,
Service\Venta\Unidad $unidadService,
int $unidad_id): ResponseInterface
{ {
$body = $request->getParsedBody(); $body = $request->getParsedBody();
$output = [ $output = [
@ -92,8 +98,7 @@ class Unidades
'edited' => false 'edited' => false
]; ];
try { try {
$unidad = $unidadRepository->fetchById($unidad_id); $unidadService->editProrrateo($unidad_id, $body);
$unidadRepository->editProrrateo($unidad, $body);
$output['edited'] = true; $output['edited'] = true;
} catch (EmptyResult) {} } catch (EmptyResult) {}
return $this->withJson($response, $output); return $this->withJson($response, $output);

View File

@ -1,6 +1,7 @@
<?php <?php
namespace Incoviba\Controller\Ventas; namespace Incoviba\Controller\Ventas;
use Exception;
use DateInterval; use DateInterval;
use DateTimeImmutable; use DateTimeImmutable;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
@ -20,10 +21,14 @@ class Facturacion extends Ideal\Controller
} }
return $view->render($response, 'ventas.facturacion', compact('proyectos')); return $view->render($response, 'ventas.facturacion', compact('proyectos'));
} }
/**
* @throws Exception
*/
public function show(ServerRequestInterface $request, ResponseInterface $response, View $view, public function show(ServerRequestInterface $request, ResponseInterface $response, View $view,
Service\Venta $ventaService, Service\Proyecto\Terreno $terrenoService, Service\Venta $ventaService, Service\Proyecto\Terreno $terrenoService,
Service\IPC $ipcService, Service\UF $ufService, Service\IPC $ipcService, Service\UF $ufService,
int $venta_id): ResponseInterface int $venta_id): ResponseInterface
{ {
$venta = $ventaService->getById($venta_id); $venta = $ventaService->getById($venta_id);
$uf = $ufService->get($venta->currentEstado()->fecha); $uf = $ufService->get($venta->currentEstado()->fecha);

View File

@ -0,0 +1,19 @@
<?php
namespace Incoviba\Model\Venta\Unidad;
use Incoviba\Common\Ideal;
use Incoviba\Model;
class Prorrateo extends Ideal\Model
{
public Model\Venta\Unidad $unidad;
public float $prorrateo;
public function jsonSerialize(): mixed
{
return [
'unidad_id' => $this->unidad->id,
'prorrateo' => $this->prorrateo,
];
}
}

View File

@ -203,4 +203,8 @@ GROUP BY a.`id`";
->where('pago = ?'); ->where('pago = ?');
return $this->fetchOne($query, [$pago_id]); return $this->fetchOne($query, [$pago_id]);
} }
public function filterData(array $data): array
{
return array_intersect_key($data, array_fill_keys(['pie', 'fecha', 'valor', 'estado', 'banco', 'fecha_pago', 'abonado', 'fecha_abonado', 'uf', 'pago', 'numero'], 0));
}
} }

View File

@ -117,4 +117,8 @@ WHERE venta_id = ?";
->where('venta.id = ?'); ->where('venta.id = ?');
return $this->fetchOne($query, [$venta_id]); return $this->fetchOne($query, [$venta_id]);
} }
public function filterData(array $data): array
{
return array_intersect_key($data, array_fill_keys(['valor', 'banco', 'tipo', 'identificador', 'fecha', 'uf', 'pagador', 'asociado'], 0));
}
} }

View File

@ -81,4 +81,8 @@ class Pie extends Ideal\Repository
->where('venta.id = ?'); ->where('venta.id = ?');
return $this->fetchOne($query, [$venta_id]); return $this->fetchOne($query, [$venta_id]);
} }
public function filterData(array $data): array
{
return array_intersect_key($data, array_fill_keys(['fecha', 'valor', 'uf', 'cuotas', 'asociado', 'reajuste'], 0));
}
} }

View File

@ -39,40 +39,6 @@ class Unidad extends Ideal\Repository
{ {
return $this->update($model, ['subtipo', 'piso', 'descripcion', 'orientacion', 'pt'], $new_data); return $this->update($model, ['subtipo', 'piso', 'descripcion', 'orientacion', 'pt'], $new_data);
} }
public function editProrrateo(Model\Venta\Unidad $model, array $new_data): Model\Venta\Unidad
{
try {
$query = $this->connection->getQueryBuilder()
->select('prorrateo')
->from('unidad_prorrateo')
->where('unidad_id = ?');
$result = $this->connection->execute($query, [$model->id])->fetch(PDO::FETCH_ASSOC);
if ($result === false) {
throw new Implement\Exception\EmptyResult($query);
}
if ($new_data['prorrateo'] !== $result['prorrateo']) {
$query = $this->connection->getQueryBuilder()
->update('unidad_prorrateo')
->set('prorrateo = ?')
->where('unidad_id = ?');
$this->connection->execute($query, [$new_data['prorrateo'], $model->id]);
$model->prorrateo = $new_data['prorrateo'];
}
} catch (PDOException | Implement\Exception\EmptyResult) {
$query = $this->connection->getQueryBuilder()
->insert()
->into('unidad_prorrateo')
->columns(['unidad_id', 'prorrateo'])
->values(['?', '?']);
try {
$this->connection->execute($query, [$model->id, $new_data['prorrateo']]);
$model->prorrateo = $new_data['prorrateo'];
} catch (PDOException) {
throw new Implement\Exception\EmptyResult($query);
}
}
return $model;
}
public function fetchById(int $id): Model\Venta\Unidad public function fetchById(int $id): Model\Venta\Unidad
{ {

View File

@ -0,0 +1,54 @@
<?php
namespace Incoviba\Repository\Venta\Unidad;
use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement;
use Incoviba\Repository;
use Incoviba\Model;
class Prorrateo extends Ideal\Repository
{
public function __construct(Define\Connection $connection, protected Repository\Venta\Unidad $unidadRepository)
{
parent::__construct($connection);
$this->setTable('unidad_prorrateo');
}
public function create(?array $data = null): Model\Venta\Unidad\Prorrateo
{
$map = (new Implement\Repository\MapperParser(['prorrateo']))
->register('unidad_id', (new Implement\Repository\Mapper())
->setProperty('unidad')
->setFunction(function($data) {
return $this->unidadRepository->fetchById($data['unidad_id']);
}));
return $this->parseData(new Model\Venta\Unidad\Prorrateo(), $data, $map);
}
public function save(Define\Model $model): Model\Venta\Unidad\Prorrateo
{
$model->id = $this->saveNew(
['unidad_id', 'prorrateo'],
[$model->unidad->id, $model->prorrateo]
);
return $model;
}
public function edit(Define\Model $model, array $new_data): Model\Venta\Unidad\Prorrateo
{
return $this->update($model, ['unidad_id', 'prorrateo'], $new_data);
}
public function fetchByUnidad(int $unidad_id): Model\Venta\Unidad\Prorrateo
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('unidad_id = ?');
return $this->fetchOne($query, [$unidad_id]);
}
public function filterData(array $data): array
{
return array_intersect_key($data, array_flip(['prorrateo']));
}
}

View File

@ -2,6 +2,7 @@
namespace Incoviba\Service\Informe; namespace Incoviba\Service\Informe;
use PhpOffice\PhpSpreadsheet; use PhpOffice\PhpSpreadsheet;
use PhpOffice\PhpSpreadsheet\Exception;
use Incoviba\Common\Define; use Incoviba\Common\Define;
class Excel implements Define\Informe class Excel implements Define\Informe
@ -40,6 +41,10 @@ class Excel implements Define\Informe
return $this; return $this;
} }
/**
* @throws Exception
* @throws PhpSpreadsheet\Writer\Exception
*/
public function build(): void public function build(): void
{ {
$spreadsheet = new PhpSpreadsheet\Spreadsheet(); $spreadsheet = new PhpSpreadsheet\Spreadsheet();

View File

@ -1,6 +1,7 @@
<?php <?php
namespace Incoviba\Service\Money; namespace Incoviba\Service\Money;
use Exception;
use DateTimeInterface; use DateTimeInterface;
use DateTimeImmutable; use DateTimeImmutable;
use DateInterval; use DateInterval;
@ -14,6 +15,10 @@ class Ine implements Provider
protected string $uri = 'https://api-calculadora.ine.cl/ServiciosCalculadoraVariacion'; protected string $uri = 'https://api-calculadora.ine.cl/ServiciosCalculadoraVariacion';
public function __construct(protected ClientInterface $client) {} public function __construct(protected ClientInterface $client) {}
/**
* @throws EmptyResponse
* @throws Exception
*/
public function get(string $money_symbol, DateTimeInterface $dateTime): float public function get(string $money_symbol, DateTimeInterface $dateTime): float
{ {
$end = new DateTimeImmutable($dateTime->format('Y-m-1')); $end = new DateTimeImmutable($dateTime->format('Y-m-1'));
@ -21,6 +26,9 @@ class Ine implements Provider
return $this->getVar($start, $end); return $this->getVar($start, $end);
} }
/**
* @throws EmptyResponse
*/
public function getVar(DateTimeInterface $start, DateTimeInterface $end): float public function getVar(DateTimeInterface $start, DateTimeInterface $end): float
{ {
$base = 1000000000; $base = 1000000000;
@ -33,9 +41,10 @@ class Ine implements Provider
]; ];
$request_uri = implode('?', [ $request_uri = implode('?', [
$this->uri, $this->uri,
implode('&', array_map(function($val, $key) { http_build_query($request_query),
/*implode('&', array_map(function($val, $key) {
return "{$key}={$val}"; return "{$key}={$val}";
}, $request_query, array_keys($request_query))) }, $request_query, array_keys($request_query)))*/
]); ]);
try { try {
$response = $this->client->get($request_uri); $response = $this->client->get($request_uri);
@ -44,6 +53,9 @@ class Ine implements Provider
} }
$body = $response->getBody(); $body = $response->getBody();
$json = json_decode($body->getContents()); $json = json_decode($body->getContents());
if (empty($json)) {
throw new EmptyResponse($request_uri);
}
return ((int) str_replace('.', '', $json[0]->valorajustado) - $base) / $base; return ((int) str_replace('.', '', $json[0]->valorajustado) - $base) / $base;
} }
} }

View File

@ -15,6 +15,9 @@ class MiIndicador implements Provider
public function __construct(protected ClientInterface $client) {} public function __construct(protected ClientInterface $client) {}
/**
* @throws EmptyResponse
*/
public function get(string $money_symbol, DateTimeInterface $dateTime): float public function get(string $money_symbol, DateTimeInterface $dateTime): float
{ {
$request_uri = "{$money_symbol}/{$dateTime->format('d-m-Y')}"; $request_uri = "{$money_symbol}/{$dateTime->format('d-m-Y')}";
@ -31,7 +34,7 @@ class MiIndicador implements Provider
$body = $response->getBody(); $body = $response->getBody();
$json = json_decode($body->getContents()); $json = json_decode($body->getContents());
if ($json->codigo !== $money_symbol or count($json->serie) === 0) { if (empty($json) or $json->codigo !== $money_symbol or count($json->serie) === 0) {
throw new EmptyResponse($request_uri); throw new EmptyResponse($request_uri);
} }
return $json->serie[0]->valor; return $json->serie[0]->valor;

View File

@ -1,8 +1,12 @@
<?php <?php
namespace Incoviba\Service\Proyecto; namespace Incoviba\Service\Proyecto;
use Exception;
use DateTimeImmutable; use DateTimeImmutable;
use DateInterval; use DateInterval;
use Incoviba\Common\Implement\Exception\EmptyResponse;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Common\Implement\Exception\HttpResponse;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal; use Incoviba\Common\Ideal;
use Incoviba\Common\Implement; use Incoviba\Common\Implement;
@ -18,6 +22,11 @@ class Terreno extends Ideal\Service
parent::__construct($logger); parent::__construct($logger);
} }
/**
* @param int $proyecto_id
* @return Model\Proyecto\Terreno|null
* @throws Exception
*/
public function valor(int $proyecto_id): ?Model\Proyecto\Terreno public function valor(int $proyecto_id): ?Model\Proyecto\Terreno
{ {
$terreno = null; $terreno = null;
@ -42,6 +51,12 @@ class Terreno extends Ideal\Service
return $terreno; return $terreno;
} }
/**
* @throws EmptyResponse
* @throws HttpResponse
* @throws EmptyResult
* @throws Exception
*/
protected function getValorContable(Model\Proyecto $proyecto, DateTimeImmutable $lastDecember): Model\Proyecto\Terreno protected function getValorContable(Model\Proyecto $proyecto, DateTimeImmutable $lastDecember): Model\Proyecto\Terreno
{ {
$cuentaNubox = $this->nuboxService->getCuenta($proyecto->inmobiliaria()->rut, 'Terrenos'); $cuentaNubox = $this->nuboxService->getCuenta($proyecto->inmobiliaria()->rut, 'Terrenos');
@ -57,6 +72,9 @@ class Terreno extends Ideal\Service
return $proyecto->terreno; return $proyecto->terreno;
} }
/**
* @throws Exception
*/
protected function getValorReajustado(Model\Proyecto $proyecto): ?Model\Proyecto\Terreno protected function getValorReajustado(Model\Proyecto $proyecto): ?Model\Proyecto\Terreno
{ {
$novPrevTerreno = new DateTimeImmutable($proyecto->terreno->fecha->format('m') < 12 ? $proyecto->terreno->fecha->sub(new DateInterval('P1Y'))->format('Y-11-1') : $proyecto->terreno->fecha->format('Y-11-1')); $novPrevTerreno = new DateTimeImmutable($proyecto->terreno->fecha->format('m') < 12 ? $proyecto->terreno->fecha->sub(new DateInterval('P1Y'))->format('Y-11-1') : $proyecto->terreno->fecha->format('Y-11-1'));

13
app/src/Service/Valor.php Normal file
View File

@ -0,0 +1,13 @@
<?php
namespace Incoviba\Service;
class Valor
{
public function clean(string|float|int $value): float
{
if ((float) $value == $value) {
return (float) $value;
}
return (float) str_replace(['.', ','], ['', '.'], $value);
}
}

View File

@ -1,6 +1,7 @@
<?php <?php
namespace Incoviba\Service; namespace Incoviba\Service;
use Exception;
use DateTimeImmutable; use DateTimeImmutable;
use DateInterval; use DateInterval;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -29,7 +30,8 @@ class Venta extends Service
protected Venta\BonoPie $bonoPieService, protected Venta\BonoPie $bonoPieService,
protected Venta\Pago $pagoService, protected Venta\Pago $pagoService,
protected Proyecto\Terreno $terrenoService, protected Proyecto\Terreno $terrenoService,
protected Money $moneyService protected Money $moneyService,
protected Valor $valorService
) { ) {
parent::__construct($logger); parent::__construct($logger);
} }
@ -136,6 +138,9 @@ class Venta extends Service
return $venta; return $venta;
} }
/**
* @throws Exception
*/
public function add(array $data): Model\Venta public function add(array $data): Model\Venta
{ {
$fecha = new DateTimeImmutable($data['fecha_venta']); $fecha = new DateTimeImmutable($data['fecha_venta']);
@ -147,7 +152,7 @@ class Venta extends Service
'propietario' => $propietario->rut, 'propietario' => $propietario->rut,
'propiedad' => $propiedad->id, 'propiedad' => $propiedad->id,
'fecha' => $fecha->format('Y-m-d'), 'fecha' => $fecha->format('Y-m-d'),
'valor_uf' => $this->cleanValue($data['valor']), 'valor_uf' => $this->valorService->clean($data['valor']),
'fecha_ingreso' => (new DateTimeImmutable())->format('Y-m-d'), 'fecha_ingreso' => (new DateTimeImmutable())->format('Y-m-d'),
'uf' => $data['uf'] 'uf' => $data['uf']
]; ];
@ -279,6 +284,10 @@ class Venta extends Service
{ {
return $this->formaPagoService->add($data); return $this->formaPagoService->add($data);
} }
/**
* @throws Exception
*/
protected function addEstado(Model\Venta $venta, Model\Venta\TipoEstadoVenta $tipoEstadoVenta, array $data): void protected function addEstado(Model\Venta $venta, Model\Venta\TipoEstadoVenta $tipoEstadoVenta, array $data): void
{ {
$fecha = new DateTimeImmutable($data['fecha']); $fecha = new DateTimeImmutable($data['fecha']);
@ -290,13 +299,6 @@ class Venta extends Service
$estado = $this->estadoVentaRepository->create($estadoData); $estado = $this->estadoVentaRepository->create($estadoData);
$this->estadoVentaRepository->save($estado); $this->estadoVentaRepository->save($estado);
} }
protected function cleanValue($value): float
{
if ((float) $value == $value) {
return (float) $value;
}
return (float) str_replace(['.', ','], ['', '.'], $value);
}
public function escriturar(Model\Venta $venta, array $data): bool public function escriturar(Model\Venta $venta, array $data): bool
{ {
@ -340,21 +342,29 @@ class Venta extends Service
} }
return true; return true;
} }
/**
* @throws Exception
*/
protected function reajustarEscritura(Model\Venta $venta, array $data): void protected function reajustarEscritura(Model\Venta $venta, array $data): void
{ {
$fecha = new DateTimeImmutable($data['fecha_reajuste']); $fecha = new DateTimeImmutable($data['fecha_reajuste']);
$reajusteData = [ $reajusteData = [
'valor' => $data['valor_reajuste'], 'valor' => $this->valorService->clean($data['valor_reajuste']),
'fecha' => $fecha->format('Y-m-d') 'fecha' => $fecha->format('Y-m-d')
]; ];
$pie = $venta->formaPago()->pie; $pie = $venta->formaPago()->pie;
$this->pieService->reajustar($pie, $reajusteData); $this->pieService->reajustar($pie, $reajusteData);
} }
/**
* @throws Exception
*/
protected function abonoEscritura(Model\Venta $venta, array $data): void protected function abonoEscritura(Model\Venta $venta, array $data): void
{ {
$fecha = new DateTimeImmutable($data['fecha_pago']); $fecha = new DateTimeImmutable($data['fecha_pago']);
$uf = $this->moneyService->getUF($fecha); $uf = $this->moneyService->getUF($fecha);
$valor = $data['valor_pago_ufs'] !== '' ? $data['valor_pago_ufs'] * $uf : $data['valor_pago_pesos']; $valor = $data['valor_pago_ufs'] !== '' ? $this->valorService->clean($data['valor_pago_ufs']) * $uf : $this->valorService->clean($data['valor_pago_pesos']);
$pagoData = [ $pagoData = [
'valor' => $valor, 'valor' => $valor,
'fecha' => $fecha->format('Y-m-d'), 'fecha' => $fecha->format('Y-m-d'),
@ -371,24 +381,32 @@ class Venta extends Service
$escritura = $this->escrituraRepository->save($escritura); $escritura = $this->escrituraRepository->save($escritura);
$this->ventaRepository->edit($venta, ['escritura' => $escritura->id]); $this->ventaRepository->edit($venta, ['escritura' => $escritura->id]);
} }
/**
* @throws Exception
*/
protected function subsidioEscritura(Model\Venta $venta, array $data): void protected function subsidioEscritura(Model\Venta $venta, array $data): void
{ {
$fecha = new DateTimeImmutable($data['fecha']); $fecha = new DateTimeImmutable($data['fecha']);
$uf = $this->moneyService->getUF($fecha); $uf = $this->moneyService->getUF($fecha);
$subsidioData = [ $subsidioData = [
'fecha_venta' => $fecha->format('Y-m-d'), 'fecha_venta' => $fecha->format('Y-m-d'),
'ahorro' => $data['valor_ahorro'], 'ahorro' => $this->valorService->clean($data['valor_ahorro']),
'subsidio' => $data['valor_subsidio'], 'subsidio' => $this->valorService->clean($data['valor_subsidio']),
'uf' => $uf 'uf' => $uf
]; ];
$subsidio = $this->addSubsidio($subsidioData); $subsidio = $this->addSubsidio($subsidioData);
$this->ventaRepository->edit($venta, ['subsidio' => $subsidio->id]); $this->ventaRepository->edit($venta, ['subsidio' => $subsidio->id]);
} }
/**
* @throws Exception
*/
protected function editCredito(Model\Venta $venta, array $data): void protected function editCredito(Model\Venta $venta, array $data): void
{ {
$fecha = new DateTimeImmutable($data['fecha']); $fecha = new DateTimeImmutable($data['fecha']);
$uf = $this->moneyService->getUF($fecha); $uf = $this->moneyService->getUF($fecha);
$valor = $data['valor_credito'] * $uf; $valor = $this->valorService->clean($data['valor_credito']) * $uf;
if ($venta->formaPago()->credito === null) { if ($venta->formaPago()->credito === null) {
$pagoData = [ $pagoData = [
'valor' => $valor, 'valor' => $valor,
@ -425,7 +443,7 @@ class Venta extends Service
{ {
try { try {
if ($this->validarData($data, ['fecha', 'devolucion'])) { if ($this->validarData($data, ['fecha', 'devolucion'])) {
$pago = $this->pagoService->add(['fecha' => $data['fecha'], 'valor' => str_replace(['.', ','], ['', '.'], $data['devolucion'])]); $pago = $this->pagoService->add(['fecha' => $data['fecha'], 'valor' => $this->valorService->clean($data['devolucion'])]);
$this->ventaRepository->edit($venta, ['resciliacion' => $pago->id]); $this->ventaRepository->edit($venta, ['resciliacion' => $pago->id]);
} }
$tipoEstado = $this->tipoEstadoVentaRepository->fetchByDescripcion('desistida'); $tipoEstado = $this->tipoEstadoVentaRepository->fetchByDescripcion('desistida');

View File

@ -10,7 +10,9 @@ class BonoPie
public function add(array $data): Model\Venta\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 public function getByVenta(int $venta_id): Model\Venta\BonoPie
{ {

View File

@ -1,23 +1,25 @@
<?php <?php
namespace Incoviba\Service\Venta; namespace Incoviba\Service\Venta;
use Exception;
use DateTimeImmutable; use DateTimeImmutable;
use Incoviba\Common\Ideal\Service; use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal;
use Incoviba\Repository; use Incoviba\Repository;
use Incoviba\Model; use Incoviba\Model;
use Incoviba\Service\Money; use Incoviba\Service;
use Psr\Log\LoggerInterface;
class Credito extends Service class Credito extends Ideal\Service
{ {
public function __construct( public function __construct(
LoggerInterface $logger, LoggerInterface $logger,
protected Repository\Venta\Credito $creditoRepository, protected Repository\Venta\Credito $creditoRepository,
protected Repository\Venta\Pago $pagoRepository, protected Pago $pagoService,
protected Repository\Venta\TipoPago $tipoPagoRepository, protected Repository\Venta\TipoPago $tipoPagoRepository,
protected Repository\Venta\EstadoPago $estadoPagoRepository, protected Repository\Venta\EstadoPago $estadoPagoRepository,
protected Repository\Venta\TipoEstadoPago $tipoEstadoPagoRepository, protected Repository\Venta\TipoEstadoPago $tipoEstadoPagoRepository,
protected Money $moneyService protected Service\Money $moneyService,
protected Service\Valor $valorService
) { ) {
parent::__construct($logger); parent::__construct($logger);
} }
@ -27,19 +29,32 @@ class Credito extends Service
return $this->creditoRepository->fetchByVenta($venta_id); return $this->creditoRepository->fetchByVenta($venta_id);
} }
/**
* @throws Exception
*/
public function add(array $data): Model\Venta\Credito public function add(array $data): Model\Venta\Credito
{ {
$fecha = new DateTimeImmutable($data['fecha']); $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'); $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([ $credito = $this->creditoRepository->create([
'valor' => $data['valor'], 'valor' => $valor,
'fecha' => $fecha->format('Y-m-d'), 'fecha' => $fecha->format('Y-m-d'),
'pago' => $pago->id 'pago' => $pago->id
]); ]);
return $this->creditoRepository->save($credito); return $this->creditoRepository->save($credito);
} }
/**
* @throws Exception
*/
public function edit(Model\Venta\Credito $credito, array $data): Model\Venta\Credito public function edit(Model\Venta\Credito $credito, array $data): Model\Venta\Credito
{ {
$uf = $this->moneyService->getUF($credito->pago->fecha); $uf = $this->moneyService->getUF($credito->pago->fecha);
@ -50,7 +65,8 @@ class Credito extends Service
$data['uf'] = $uf; $data['uf'] = $uf;
} }
if (array_key_exists('valor', $data)) { 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([ $filteredData = array_intersect_key($data, array_fill_keys([
'fecha', 'fecha',
@ -58,21 +74,12 @@ class Credito extends Service
'valor', 'valor',
'banco' 'banco'
], 0)); ], 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); 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); $filtered_data = array_intersect_key($data, $fields);
$pago_data = array_merge($filtered_data, ['tipo' => $tipoPago->id]); $pago_data = array_merge($filtered_data, ['tipo' => $tipoPago->id]);
$pago = $this->pagoService->add($pago_data); $pago = $this->pagoService->add($pago_data);
$data['pago'] = $pago->id; $data['pago'] = $pago->id;
$data['estado'] = $pago->currentEstado->tipoEstadoPago->id; $data['estado'] = $pago->currentEstado->tipoEstadoPago->id;
$fields = array_fill_keys([ $filtered_data = $this->cuotaRepository->filterData($data);
'pie',
'fecha',
'valor',
'estado',
'banco',
'uf',
'pago',
'numero'
], 0);
$filtered_data = array_intersect_key($data, $fields);
$mapped_data = $filtered_data; $mapped_data = $filtered_data;
$mapped_data['valor_$'] = $mapped_data['valor']; $mapped_data['valor_$'] = $mapped_data['valor'];
unset($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 <?php
namespace Incoviba\Service\Venta; namespace Incoviba\Service\Venta;
use Incoviba\Service\Valor;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Incoviba\Common\Implement; use Incoviba\Common\Implement;
use Incoviba\Common\Ideal; use Incoviba\Common\Ideal;
@ -15,7 +16,8 @@ class FormaPago extends Ideal\Service
protected Credito $creditoService, protected Credito $creditoService,
protected Repository\Venta\Escritura $escrituraRepository, protected Repository\Venta\Escritura $escrituraRepository,
protected Subsidio $subsidioService, protected Subsidio $subsidioService,
protected Pago $pagoService) protected Pago $pagoService,
protected Valor $valorService)
{ {
parent::__construct($logger); parent::__construct($logger);
} }
@ -83,7 +85,7 @@ class FormaPago extends Ideal\Service
'cuotas', 'cuotas',
'uf' 'uf'
], $filtered_data); ], $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); return $this->pieService->add($mapped_data);
} }
protected function addSubsidio(array $data): Model\Venta\Subsidio protected function addSubsidio(array $data): Model\Venta\Subsidio
@ -101,6 +103,8 @@ class FormaPago extends Ideal\Service
'subsidio', 'subsidio',
'uf' 'uf'
], $filtered_data); ], $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); return $this->subsidioService->add($mapped_data);
} }
protected function addCredito(array $data): Model\Venta\Credito protected function addCredito(array $data): Model\Venta\Credito
@ -116,6 +120,7 @@ class FormaPago extends Ideal\Service
'valor', 'valor',
'uf' 'uf'
], $filtered_data); ], $filtered_data);
$mapped_data['valor'] = $this->valorService->clean($mapped_data['valor']);
return $this->creditoService->add($mapped_data); return $this->creditoService->add($mapped_data);
} }
protected function addBonoPie(array $data): Model\Venta\BonoPie protected function addBonoPie(array $data): Model\Venta\BonoPie
@ -129,14 +134,7 @@ class FormaPago extends Ideal\Service
'fecha', 'fecha',
'valor' 'valor'
], $filtered_data); ], $filtered_data);
$mapped_data['valor'] = $this->valorService->clean($mapped_data['valor']);
return $this->bonoPieService->add($mapped_data); 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 <?php
namespace Incoviba\Service\Venta; namespace Incoviba\Service\Venta;
use Exception;
use DateTimeInterface; use DateTimeInterface;
use DateTimeImmutable; use DateTimeImmutable;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Service\Money;
use PDOException; use PDOException;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Repository; use Incoviba\Repository;
use Incoviba\Model; use Incoviba\Model;
use Incoviba\Service; use Incoviba\Service;
@ -16,7 +16,8 @@ class Pago
protected Repository\Venta\Pago $pagoRepository, protected Repository\Venta\Pago $pagoRepository,
protected Repository\Venta\EstadoPago $estadoPagoRepository, protected Repository\Venta\EstadoPago $estadoPagoRepository,
protected Repository\Venta\TipoEstadoPago $tipoEstadoPagoRepository, 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 public function depositar(Model\Venta\Pago $pago, DateTimeInterface $fecha): bool
@ -115,24 +116,19 @@ class Pago
return $this->process($this->pagoRepository->fetchDevolucionByVenta($venta_id)); return $this->process($this->pagoRepository->fetchDevolucionByVenta($venta_id));
} }
/**
* @throws \Exception
*/
public function add(array $data): Model\Venta\Pago public function add(array $data): Model\Venta\Pago
{ {
if (!isset($data['uf'])) { if (!isset($data['uf'])) {
$data['uf'] = $this->ufService->get(new DateTimeImmutable($data['fecha'])); $data['uf'] = $this->ufService->get(new DateTimeImmutable($data['fecha']));
} }
$fields = array_fill_keys([ $filtered_data = $this->pagoRepository->filterData($data);
'valor', $filtered_data['valor'] = round($this->valorService->clean($filtered_data['valor']));
'banco',
'tipo',
'identificador',
'fecha',
'uf',
'pagador',
'asociado'
], 0);
$filtered_data = array_intersect_key($data, $fields);
$pago = $this->pagoRepository->create($filtered_data); $pago = $this->pagoRepository->create($filtered_data);
$pago = $this->pagoRepository->save($pago); $pago = $this->pagoRepository->save($pago);
$tipoEstado = $this->tipoEstadoPagoRepository->fetchByDescripcion('no pagado'); $tipoEstado = $this->tipoEstadoPagoRepository->fetchByDescripcion('no pagado');
$estado = $this->estadoPagoRepository->create([ $estado = $this->estadoPagoRepository->create([
'pago' => $pago->id, 'pago' => $pago->id,
@ -143,6 +139,24 @@ class Pago
$pago->currentEstado = $estado; $pago->currentEstado = $estado;
return $pago; 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 public function delete(Model\Venta\Pago $pago): bool
{ {
try { try {

View File

@ -25,7 +25,8 @@ class Pie
public function add(array $data): Model\Venta\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); return $this->pieRepository->save($pie);
} }
public function addCuota(array $data): Model\Venta\Cuota 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 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 public function reajustar(Model\Venta\Pie $pie, array $data): Model\Venta\Pie
{ {

View File

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