Agregar Pie

This commit is contained in:
Juan Pablo Vial
2025-03-26 16:41:54 -03:00
parent 4ce83fb270
commit db36549699
8 changed files with 195 additions and 44 deletions

View File

@ -23,6 +23,7 @@ $app->group('/venta/{venta_id:[0-9]+}', function($app) {
$app->get('[/]', [Ventas::class, 'propiedad']);
});
$app->group('/pie', function($app) {
$app->get('/add[/]', [Ventas\Pies::class, 'add']);
$app->group('/cuotas', function($app) {
$app->get('[/]', [Ventas::class, 'cuotas']);
});

View File

@ -54,6 +54,9 @@ $app->group('/venta/{venta_id}', function($app) {
$app->group('/propietario', function($app) {
$app->put('/edit[/]', [Ventas::class, 'propietario']);
});
$app->group('/pie', function($app) {
$app->post('/add[/]', [Ventas\Pies::class, 'add']);
});
$app->post('[/]', [Ventas::class, 'edit']);
$app->get('[/]', [Ventas::class, 'get']);
});

View File

@ -0,0 +1,50 @@
@extends('ventas.base')
@section('venta_subtitle')
Pie
@endsection
@section('venta_content')
<div class="ui basic compact segment">
Valor Promesa: {{ $format->ufs($venta->valor) }} <br />
10% {{ $format->ufs($venta->valor * 0.1) }}
</div>
<form class="ui form" id="add_pie">
<input type="hidden" name="venta" value="{{ $venta->id }}" />
<input type="hidden" name="fecha" value="{{ $venta->fecha->format('Y-m-d') }}" />
<div class="three wide field">
<label for="valor">Valor</label>
<div class="ui right labeled input">
<input type="text" name="valor" id="valor" value="{{ round($venta->valor * 0.1, 2) }}" />
<div class="ui basic label">UF</div>
</div>
</div>
<div class="three wide field">
<label for="cuotas"># Cuotas</label>
<input type="number" name="cuotas" id="cuotas" />
</div>
<button class="ui button">Agregar</button>
</form>
@endsection
@push('page_scripts')
<script>
$(document).ready(() => {
$('#add_pie').submit(event => {
event.preventDefault()
const data = new FormData(event.currentTarget)
return fetchAPI('{{ $urls->api }}/venta/{{ $venta->id }}/pie/add', {method: 'post', body: data}).then(response => {
if (response.ok) {
return response.json()
}
}).then(json => {
if (json.success) {
window.location = '{{$urls->base}}/venta/{{$venta->id}}'
return true
}
return false
})
})
})
</script>
@endpush

View File

@ -1,42 +1,52 @@
@if ($pie !== null)
<tr>
<td>
Pie
<sub>
<a href="{{$urls->base}}/venta/{{$venta->id}}/pie">
<i class="edit icon"></i>
</a>
</sub>
</td>
<td></td>
<td class="right aligned">{{$format->ufs($pie->valor)}}</td>
<td class="right aligned">{{$format->pesos($pie->valor * $pie->uf)}}</td>
<td class="right aligned">Cuotas</td>
<td>
<a href="{{$urls->base}}/venta/{{$venta->id}}/pie/cuotas">
<span data-tooltip="Pagadas">{{count($pie->cuotas(true, true))}}</span>/{{$pie->cuotas}}
</a>
@if (count($pie->cuotas(vigentes: true)) < $pie->cuotas)
<a href="{{$urls->base}}/ventas/pie/{{$pie->id}}/cuotas/add">
@if ($pie !== null)
<sub>
<a href="{{$urls->base}}/venta/{{$venta->id}}/pie">
<i class="edit icon"></i>
</a>
</sub>
@else
<a href="{{$urls->base}}/venta/{{$venta->id}}/pie/add">
<i class="plus icon"></i>
</a>
@endif
</td>
</tr>
<tr>
<td>Pagado</td>
<td></td>
<td class="right aligned">{{$format->ufs($pie->pagado())}}</td>
<td class="right aligned">{{$format->pesos($pie->pagado('pesos'))}}</td>
<td colspan="2"></td>
</tr>
@if ($pie->reajuste)
<tr>
<td>Reajuste</td>
<td></td>
<td class="right aligned">{{$format->ufs($pie->reajuste->valor())}}</td>
<td class="right aligned">{{$format->pesos($pie->reajuste->valor)}}</td>
<td colspan="2"></td>
@if ($pie !== null)
<td class="right aligned">{{$format->ufs($pie->valor)}}</td>
<td class="right aligned">{{$format->pesos($pie->valor * $pie->uf)}}</td>
<td class="right aligned">Cuotas</td>
<td>
<a href="{{$urls->base}}/venta/{{$venta->id}}/pie/cuotas">
<span data-tooltip="Pagadas">{{count($pie->cuotas(true, true))}}</span>/{{$pie->cuotas}}
</a>
@if (count($pie->cuotas(vigentes: true)) < $pie->cuotas)
<a href="{{$urls->base}}/ventas/pie/{{$pie->id}}/cuotas/add">
<i class="plus icon"></i>
</a>
@endif
</td>
@else
<td colspan="4"></td>
@endif
</tr>
@endif
@if ($pie !== null)
<tr>
<td>Pagado</td>
<td></td>
<td class="right aligned">{{$format->ufs($pie->pagado())}}</td>
<td class="right aligned">{{$format->pesos($pie->pagado('pesos'))}}</td>
<td colspan="2"></td>
</tr>
@if ($pie->reajuste)
<tr>
<td>Reajuste</td>
<td></td>
<td class="right aligned">{{$format->ufs($pie->reajuste->valor())}}</td>
<td class="right aligned">{{$format->pesos($pie->reajuste->valor)}}</td>
<td colspan="2"></td>
</tr>
@endif
@endif

View File

@ -5,7 +5,8 @@ use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Incoviba\Controller\API\{withJson, emptyBody};
use Incoviba\Controller\withRedis;
use Incoviba\Common\Implement\Exception\{EmptyRedis, EmptyResult};
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Exception\ServiceAction\{Create, Read, Update};
use Incoviba\Service;
class Pies
@ -27,4 +28,35 @@ class Pies
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Venta $ventaService,
Service\Venta\Pie $pieService, int $venta_id): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'venta_id' => $venta_id,
'input' => $input,
'pie' => null,
'success' => false
];
try {
$venta = $ventaService->getById($venta_id);
} catch (Read $exception) {
return $this->withError($response, $exception);
}
try {
$pie = $pieService->add($input);
} catch (Create $exception) {
return $this->withError($response, $exception);
}
try {
$ventaService->edit($venta, ['pie' => $pie->id]);
$output['pie'] = $pie;
$output['success'] = true;
} catch (Update $exception) {
return $this->withError($response, $exception);
}
return $this->withJson($response, $output);
}
}

View File

@ -1,12 +1,12 @@
<?php
namespace Incoviba\Controller\Ventas;
use Incoviba\Common\Alias\View;
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Alias\View;
use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Repository;
use Incoviba\Service;
class Pies
{
@ -21,4 +21,13 @@ class Pies
$estados = $tipoEstadoPagoRepository->fetchAll('descripcion');
return $view->render($response, 'ventas.pies.cuotas', compact('pie', 'venta', 'bancos', 'estados', 'ventaRepository'));
}
public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Venta $ventaService,
View $view, int $venta_id): ResponseInterface
{
$venta = null;
try {
$venta = $ventaService->getById($venta_id);
} catch (Read) {}
return $view->render($response, 'ventas.pies.add', compact('venta'));
}
}

View File

@ -4,6 +4,7 @@ namespace Incoviba\Service;
use Exception;
use DateTimeImmutable;
use DateMalformedStringException;
use Incoviba\Exception\ServiceAction\{Read, Update};
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal\Service;
use Incoviba\Common\Implement;
@ -36,9 +37,18 @@ class Venta extends Service
parent::__construct($logger);
}
/**
* @param int $venta_id
* @return Model\Venta
* @throws Read
*/
public function getById(int $venta_id): Model\Venta
{
return $this->process($this->ventaRepository->fetchById($venta_id));
try {
return $this->process($this->ventaRepository->fetchById($venta_id));
} catch (Implement\Exception\EmptyResult $exception) {
throw new Read(__CLASS__, $exception);
}
}
public function getByProyecto(int $proyecto_id): array
{
@ -285,6 +295,22 @@ class Venta extends Service
return $this->formaPagoService->add($data);
}
/**
* @param Model\Venta $venta
* @param array $data
* @return Model\Venta
* @throws Update
*/
public function edit(Model\Venta $venta, array $data): Model\Venta
{
try {
$filteredData = $this->ventaRepository->filterData($data);
return $this->ventaRepository->edit($venta, $filteredData);
} catch (Implement\Exception\EmptyResult $exception) {
throw new Update(__CLASS__, $exception);
}
}
protected function addEstado(Model\Venta $venta, Model\Venta\TipoEstadoVenta $tipoEstadoVenta, array $data): void
{
try {

View File

@ -1,17 +1,22 @@
<?php
namespace Incoviba\Service\Venta;
use PDOException;
use DateTimeImmutable;
use DateMalformedStringException;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Repository;
use Incoviba\Exception\ServiceAction\{Create, Read};
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service\UF;
class Pie
{
public function __construct(
protected Repository\Venta\Pie $pieRepository,
protected Cuota $cuotaService,
protected Pago $pagoService
protected Pago $pagoService,
protected UF $ufService
) {}
public function getById(int $pie_id): Model\Venta\Pie
@ -27,11 +32,26 @@ class Pie
}
}
/**
* @param array $data
* @return Model\Venta\Pie
* @throws Create
*/
public function add(array $data): Model\Venta\Pie
{
$filteredData = $this->pieRepository->filterData($data);
$pie = $this->pieRepository->create($filteredData);
return $this->pieRepository->save($pie);
try {
$filteredData = $this->pieRepository->filterData($data);
if (!isset($filteredData['uf'])) {
try {
$date = new DateTimeImmutable($filteredData['fecha']);
$filteredData['uf'] = $this->ufService->get($date);
} catch (DateMalformedStringException) {}
}
$pie = $this->pieRepository->create($filteredData);
return $this->pieRepository->save($pie);
} catch (PDOException $exception) {
throw new Create(__CLASS__, $exception);
}
}
public function addCuota(array $data): Model\Venta\Cuota
{