FormaPago Service

This commit is contained in:
Juan Pablo Vial
2024-03-13 14:38:44 -03:00
parent c7dd309185
commit 98953cce42
21 changed files with 261 additions and 71 deletions

View File

@ -32,46 +32,3 @@ $showPropietario = true;
@include('ventas.show.comentarios')
</div>
@endsection
{{--@section('page_content')
<div class="ui container">
<div class="ui two column grid">
<h1 class="four wide column header">
<div class="content">
<div class="ui dividing sub header">{{$venta->proyecto()->descripcion}}</div>
{{$venta->propiedad()->summary()}}
</div>
</h1>
<div class="right floated column">
@include('ventas.show.propietario')
</div>
</div>
<br />
<div class="ui fitted basic mini segment">
@if ($venta->currentEstado()->tipoEstadoVenta->activa)
<a href="{{$urls->base}}/venta/{{$venta->id}}/desistir">
Desistir <i class="minus icon"></i>
</a>
<a href="{{$urls->base}}/venta/{{$venta->id}}/ceder">
Ceder <i clasS="right chevron icon"></i>
</a>
@else
<div class="ui red icon label">
<i class="ban icon"></i>
{{ucwords($venta->currentEstado()->tipoEstadoVenta->descripcion)}}
(<a href="{{$urls->base}}/venta/{{$venta->id}}/desistida">
{{$format->pesos($venta->resciliacion()->valor)}}
</a>)
</div>
@endif
</div>
<div class="ui segments">
@include('ventas.show.propiedad')
@include('ventas.show.detalle')
@include('ventas.show.forma_pago', ['formaPago' => $venta->formaPago()])
@include('ventas.show.escritura')
@include('ventas.show.entrega')
@include('ventas.show.comentarios')
</div>
</div>
@endsection--}}

View File

@ -3,14 +3,14 @@
</div>
<div class="ui segment">
<table class="ui very basic fluid table">
@include('ventas.show.forma_pago.pie', ['pie' => $formaPago->pie])
@include('ventas.show.forma_pago.escritura', ['escritura' => $formaPago->escritura])
@include('ventas.show.forma_pago.anticipo', ['anticipo' => ['uf' => $formaPago->anticipo(), 'pesos' => $formaPago->anticipo('pesos')]])
@include('ventas.show.forma_pago.bono_pie', ['bonoPie' => $formaPago->bonoPie])
@include('ventas.show.forma_pago.subsidio', ['subsidio' => $formaPago->subsidio])
@include('ventas.show.forma_pago.credito', ['credito' => $formaPago->credito])
@include('ventas.show.forma_pago.pie', ['pie' => $formaPago?->pie])
@include('ventas.show.forma_pago.escritura', ['escritura' => $formaPago?->escritura])
@include('ventas.show.forma_pago.anticipo', ['anticipo' => ['uf' => $formaPago?->anticipo(), 'pesos' => $formaPago?->anticipo('pesos')]])
@include('ventas.show.forma_pago.bono_pie', ['bonoPie' => $formaPago?->bonoPie])
@include('ventas.show.forma_pago.subsidio', ['subsidio' => $formaPago?->subsidio])
@include('ventas.show.forma_pago.credito', ['credito' => $formaPago?->credito])
@include('ventas.show.forma_pago.total')
@include('ventas.show.forma_pago.devolucion', ['devolucion' => $formaPago->devolucion])
@include('ventas.show.forma_pago.devolucion', ['devolucion' => $formaPago?->devolucion])
</table>
</div>
<div id="pago_modal" class="ui modal">

View File

@ -13,7 +13,7 @@ class Login
public function form(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Login $service): ResponseInterface
{
if ($service->isIn()) {
return $response->withStatus(301)->withHeader('Location', $view->get('urls')->base);
return $response->withStatus(302)->withHeader('Location', $view->get('urls')->base);
}
return $view->render($response, 'login.form');
}

View File

@ -32,8 +32,13 @@ class Ventas
public function show(ServerRequestInterface $request, ResponseInterface $response, View $view,
Service\Venta $service, int $venta_id): ResponseInterface
{
$venta = $service->getById($venta_id);
return $view->render($response, 'ventas.show', compact('venta'));
try {
$venta = $service->getById($venta_id);
return $view->render($response, 'ventas.show', compact('venta'));
} catch (EmptyResult) {
$response = $response->withStatus(404);
return $view->render($response, 'not_found');
}
}
public function showUnidad(ServerRequestInterface $request, ResponseInterface $response, View $view,
Service\Venta $service, string $proyecto_nombre, int $unidad_descripcion): ResponseInterface

View File

@ -6,8 +6,8 @@ use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Incoviba\Service;
use Psr\Log\LoggerInterface;
use Incoviba\Service;
class Authentication
{
@ -27,7 +27,11 @@ class Authentication
$response = $this->responseFactory->createResponse(301, 'Not logged in')
->withHeader('Referer', (string) $request->getUri())
->withHeader('X-Redirected-URI', (string) $request->getUri());
return $this->view->render($response, 'login.form', ['redirect_uri' => (string) $request->getUri()]);
$url = "{$request->getUri()}";
if (str_ends_with($url, '/login')) {
$url = str_replace('/login', '', $url);
}
return $this->view->render($response, 'login.form', ['redirect_uri' => $url]);
}
protected function isValid(ServerRequestInterface $request): bool

View File

@ -4,13 +4,14 @@ namespace Incoviba\Model;
use DateTimeInterface;
use Incoviba\Common\Ideal;
use Incoviba\Controller\Ventas;
use Incoviba\Model\Venta\FormaPago;
use Incoviba\Model\Venta\Pago;
class Venta extends Ideal\Model
{
protected Venta\Propietario $propietario;
protected Venta\Propiedad $propiedad;
protected Venta\FormaPago $formaPago;
protected ?Venta\FormaPago $formaPago;
public DateTimeInterface $fecha;
public DateTimeInterface $fechaIngreso;
public float $valor;
@ -36,7 +37,7 @@ class Venta extends Ideal\Model
}
return $this->propiedad;
}
public function formaPago(): Venta\FormaPago
public function formaPago(): ?Venta\FormaPago
{
if (!isset($this->formaPago)) {
$this->formaPago = $this->runFactory('formaPago');
@ -104,7 +105,7 @@ class Venta extends Ideal\Model
return array_merge(parent::jsonSerialize(), [
'propietario' => $this->propietario(),
'propiedad' => $this->propiedad(),
'forma_pago' => $this->formaPago()->ids(),
'forma_pago' => $this->formaPago()?->ids(),
'fecha' => $this->fecha->format('Y-m-d'),
'fecha_ingreso' => $this->fechaIngreso->format('Y-m-d'),
'valor' => $this->valor,

View File

@ -5,12 +5,12 @@ use JsonSerializable;
class FormaPago implements JsonSerializable
{
public ?Pie $pie;
public ?Escritura $escritura;
public ?BonoPie $bonoPie;
public ?Subsidio $subsidio;
public ?Credito $credito;
public ?Pago $devolucion;
public ?Pie $pie = null;
public ?Escritura $escritura = null;
public ?BonoPie $bonoPie = null;
public ?Subsidio $subsidio = null;
public ?Credito $credito = null;
public ?Pago $devolucion = null;
public function anticipo(string $moneda = Pago::UF): float
{

View File

@ -38,7 +38,7 @@ class Venta extends Ideal\Repository
->setFactory((new Implement\Repository\Factory())
->setCallable([$this->propiedadRepository, 'fetchById'])
->setArgs([$data['propiedad']])))
->register('pie', (new Implement\Repository\Mapper())
/*->register('pie', (new Implement\Repository\Mapper())
->setProperty('formaPago')
->setFactory((new Implement\Repository\Factory())
->setCallable(function($repositories, $data) {
@ -84,7 +84,7 @@ class Venta extends Ideal\Repository
'escrituraRepository' => $this->escrituraRepository,
'subsidioRepository' => $this->subsidioRepository,
'pagoService' => $this->pagoService
], $data])))
], $data])))*/
/*->register('escriturado', (new Implement\Repository\Mapper())
->setFunction(function($data) {
return $data['escritura'] !== null;

View File

@ -52,4 +52,13 @@ class BonoPie extends Ideal\Repository
->where('pago = ?');
return $this->fetchOne($query, [$pago_id]);
}
public function fetchByVenta(int $venta_id): Model\Venta\BonoPie
{
$query = $this->connection->getQueryBuilder()
->select('a.*')
->from("{$this->getTable()} a")
->joined('JOIN venta ON venta.bono_pie = a.id')
->where('venta.id = ?');
return $this->fetchOne($query, [$venta_id]);
}
}

View File

@ -53,4 +53,13 @@ class Credito extends Ideal\Repository
->where('pago = ?');
return $this->fetchOne($query, [$pago_id]);
}
public function fetchByVenta(int $venta_id): Model\Venta\Credito
{
$query = $this->connection->getQueryBuilder()
->select('a.*')
->from("{$this->getTable()} a")
->joined('JOIN venta ON venta.credito = a.id')
->where('venta.id = ?');
return $this->fetchOne($query, [$venta_id]);
}
}

View File

@ -55,4 +55,13 @@ class Escritura extends Ideal\Repository
->where('pago = ?');
return $this->fetchOne($query, [$pago_id]);
}
public function fetchByVenta(int $venta_id): Model\Venta\Escritura
{
$query = $this->connection->getQueryBuilder()
->select('a.*')
->from("{$this->getTable()} a")
->joined('JOIN venta ON venta.escritura = a.id')
->where('venta.id = ?');
return $this->fetchOne($query, [$venta_id]);
}
}

View File

@ -108,4 +108,13 @@ WHERE venta_id = ?";
->where('valor = ? OR ROUND(valor/uf, 3) = ?');
return $this->fetchMany($query, [$value, $value]);
}
public function fetchDevolucionByVenta(int $venta_id): Model\Venta\Pago
{
$query = $this->connection->getQueryBuilder()
->select('a.*')
->from("{$this->getTable()} a")
->joined('JOIN venta ON venta.devolucion = a.id')
->where('venta.id = ?');
return $this->fetchOne($query, [$venta_id]);
}
}

View File

@ -72,4 +72,13 @@ class Pie extends Ideal\Repository
->where('reajuste = ?');
return $this->fetchOne($query, [$reajuste_id]);
}
public function fetchByVenta(int $venta_id): Model\Venta\Pie
{
$query = $this->connection->getQueryBuilder()
->select('a.*')
->from("{$this->getTable()} a")
->joined('JOIN venta ON venta.pie = a.id')
->where('venta.id = ?');
return $this->fetchOne($query, [$venta_id]);
}
}

View File

@ -49,4 +49,13 @@ class Subsidio extends Ideal\Repository
->where('subsidio = ? OR pago = ?');
return $this->fetchOne($query, [$pago_id, $pago_id]);
}
public function fetchByVenta(int $venta_id): Model\Venta\Subsidio
{
$query = $this->connection->getQueryBuilder()
->select('a.*')
->from("{$this->getTable()} a")
->joined('JOIN venta ON venta.subsidio = a.id')
->where('venta.id = ?');
return $this->fetchOne($query, [$venta_id]);
}
}

View File

@ -6,7 +6,6 @@ use Incoviba\Common\Ideal\Service;
use Incoviba\Common\Implement;
use Incoviba\Repository;
use Incoviba\Model;
use PhpParser\Node\Expr\AssignOp\Mod;
use Psr\Log\LoggerInterface;
class Venta extends Service
@ -20,6 +19,7 @@ class Venta extends Service
protected Repository\Venta\Escritura $escrituraRepository,
protected Repository\Venta\Pago $pagoRepository,
protected Repository\Venta\EstadoPago $estadoPagoRepository,
protected Venta\FormaPago $formaPagoService,
protected Venta\Propietario $propietarioService,
protected Venta\Propiedad $propiedadService,
protected Venta\Pie $pieService,
@ -82,6 +82,9 @@ class Venta extends Service
protected function process(Model\Venta $venta): Model\Venta
{
$venta->addFactory('formaPago', (new Implement\Repository\Factory())
->setCallable([$this->formaPagoService, 'getByVenta'])
->setArgs(['venta_id' => $venta->id]));
$venta->addFactory('estados', (new Implement\Repository\Factory())
->setCallable([$this->estadoVentaRepository, 'fetchByVenta'])
->setArgs([$venta->id]));
@ -231,7 +234,8 @@ class Venta extends Service
}
protected function addFormaPago(array $data): Model\Venta\FormaPago
{
$fields = [
return $this->formaPagoService->add($data);
/*$fields = [
'pie',
'subsidio',
'credito',
@ -245,9 +249,9 @@ class Venta extends Service
$forma_pago->{$name} = $obj;
}
}
return $forma_pago;
return $forma_pago;*/
}
protected function addPie(array $data): Model\Venta\Pie
/*protected function addPie(array $data): Model\Venta\Pie
{
$fields = array_fill_keys([
'fecha_venta',
@ -309,7 +313,7 @@ class Venta extends Service
'valor'
], $filtered_data);
return $this->bonoPieService->add($mapped_data);
}
}*/
protected function addEstado(Model\Venta $venta, Model\Venta\TipoEstadoVenta $tipoEstadoVenta, array $data): void
{
$fecha = new DateTimeImmutable($data['fecha']);

View File

@ -12,4 +12,8 @@ class BonoPie
{
return new Model\Venta\BonoPie();
}
public function getByVenta(int $venta_id): Model\Venta\BonoPie
{
return $this->bonoPieRepository->fetchByVenta($venta_id);
}
}

View File

@ -22,6 +22,11 @@ class Credito extends Service
parent::__construct($logger);
}
public function getByVenta(int $venta_id): Model\Venta\Credito
{
return $this->creditoRepository->fetchByVenta($venta_id);
}
public function add(array $data): Model\Venta\Credito
{
$fecha = new DateTimeImmutable($data['fecha']);

View File

@ -0,0 +1,143 @@
<?php
namespace Incoviba\Service\Venta;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Implement;
use Incoviba\Common\Ideal;
use Incoviba\Model;
use Incoviba\Repository;
class FormaPago extends Ideal\Service
{
public function __construct(LoggerInterface $logger,
protected Pie $pieService,
protected BonoPie $bonoPieService,
protected Credito $creditoService,
protected Repository\Venta\Escritura $escrituraRepository,
protected Subsidio $subsidioService,
protected Pago $pagoService)
{
parent::__construct($logger);
}
public function getByVenta(int $venta_id): Model\Venta\FormaPago
{
$formaPago = new Model\Venta\FormaPago();
try {
$formaPago->pie = $this->pieService->getByVenta($venta_id);
} catch (Implement\Exception\EmptyResult) {}
try {
$formaPago->bonoPie = $this->bonoPieService->getByVenta($venta_id);
} catch (Implement\Exception\EmptyResult) {}
try {
$formaPago->credito = $this->creditoService->getByVenta($venta_id);
} catch (Implement\Exception\EmptyResult) {}
try {
$formaPago->escritura = $this->escrituraRepository->fetchByVenta($venta_id);
} catch (Implement\Exception\EmptyResult) {}
try {
$formaPago->subsidio = $this->subsidioService->getByVenta($venta_id);
} catch (Implement\Exception\EmptyResult) {}
try {
$formaPago->devolucion = $this->pagoService->getDevolucionByVenta($venta_id);
} catch (Implement\Exception\EmptyResult) {}
return $formaPago;
}
public function add(array $data): Model\Venta\FormaPago
{
$fields = [
'pie',
'subsidio',
'credito',
'bono_pie'
];
$forma_pago = new Model\Venta\FormaPago();
foreach ($fields as $name) {
if (isset($data["has_{$name}"])) {
try {
$method = 'add' . str_replace(' ', '', ucwords(str_replace('_', ' ', $name)));
$obj = $this->{$method}($data);
$forma_pago->{$name} = $obj;
} catch (\Error $error) {
$this->logger->critical($error);
}
}
}
return $forma_pago;
}
protected function addPie(array $data): Model\Venta\Pie
{
$fields = array_fill_keys([
'fecha_venta',
'pie',
'cuotas',
'uf'
], 0);
$filtered_data = array_intersect_key($data, $fields);
$this->logger->critical(var_export($filtered_data,true));
$mapped_data = array_combine([
'fecha',
'valor',
'cuotas',
'uf'
], $filtered_data);
$mapped_data['valor'] = $this->cleanValue($mapped_data['valor']);
return $this->pieService->add($mapped_data);
}
protected function addSubsidio(array $data): Model\Venta\Subsidio
{
$fields = array_fill_keys([
'fecha_venta',
'ahorro',
'subsidio',
'uf'
], 0);
$filtered_data = array_intersect_key($data, $fields);
$mapped_data = array_combine([
'fecha',
'ahorro',
'subsidio',
'uf'
], $filtered_data);
return $this->subsidioService->add($mapped_data);
}
protected function addCredito(array $data): Model\Venta\Credito
{
$fields = array_fill_keys([
'fecha_venta',
'credito',
'uf'
], 0);
$filtered_data = array_intersect_key($data, $fields);
$mapped_data = array_combine([
'fecha',
'valor',
'uf'
], $filtered_data);
return $this->creditoService->add($mapped_data);
}
protected function addBonoPie(array $data): Model\Venta\BonoPie
{
$fields = array_fill_keys([
'fecha_venta',
'bono_pie'
], 0);
$filtered_data = array_intersect_key($data, $fields);
$mapped_data = array_combine([
'fecha',
'valor'
], $filtered_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

@ -109,6 +109,10 @@ class Pago
{
return [];
}
public function getDevolucionByVenta(int $venta_id): Model\Venta\Pago
{
return $this->process($this->pagoRepository->fetchDevolucionByVenta($venta_id));
}
public function add(array $data): Model\Venta\Pago
{

View File

@ -17,6 +17,10 @@ class Pie
{
return $this->process($this->pieRepository->fetchById($pie_id));
}
public function getByVenta(int $venta_id): Model\Venta\Pie
{
return $this->process($this->pieRepository->fetchByVenta($venta_id));
}
public function add(array $data): Model\Venta\Pie
{

View File

@ -17,6 +17,11 @@ class Subsidio
protected Service\Money $moneyService
) {}
public function getByVenta(int $venta_id): Model\Venta\Subsidio
{
return $this->subsidioRepository->fetchByVenta($venta_id);
}
public function add(array $data): Model\Venta\Subsidio
{
$fecha = new DateTimeImmutable($data['fecha']);