Agregar Bono Pie

This commit is contained in:
Juan Pablo Vial
2024-11-18 23:25:20 -03:00
parent 1e1994264e
commit cb2edb1543
9 changed files with 156 additions and 4 deletions

View File

@ -28,6 +28,9 @@ $app->group('/venta/{venta_id:[0-9]+}', function($app) {
});
$app->get('[/]', [Ventas::class, 'pie']);
});
$app->group('/bono_pie', function($app) {
$app->get('/add[/]', [Ventas\Bonos::class, 'add']);
});
$app->group('/escritura', function($app) {
$app->get('/add[/]', [Ventas\Escrituras::class, 'add']);
});

View File

@ -31,6 +31,9 @@ $app->group('/venta/{venta_id}', function($app) {
$app->post('/add[/]', [Ventas::class, 'addComentario']);
$app->get('[/]', [Ventas::class, 'comentarios']);
});
$app->group('/bono_pie', function($app) {
$app->post('/add[/]', [Ventas\Bonos::class, 'add']);
});
$app->group('/escritura', function($app) {
$app->post('/add[/]', [Ventas\Escrituras::class, 'add']);
});

View File

@ -10,4 +10,11 @@ $app->group('/pie/{pie_id}', function($app) {
});
$app->get('[/]', [Pies::class, 'cuotas']);
});
$files = new FilesystemIterator(implode(DIRECTORY_SEPARATOR, [__DIR__, 'pies']));
foreach ($files as $file) {
if ($file->isDir()) {
continue;
}
include_once $file->getRealPath();
}
});

View File

@ -0,0 +1,6 @@
<?php
use Incoviba\Controller\Ventas\Bonos;
$app->group('/bono_pie', function($app) {
$app->get('/add[/]', [Bonos::class, 'add']);
});

View File

@ -0,0 +1,61 @@
@extends('ventas.base')
@section('venta_subtitle')
Agregar Bono - Pie
@endsection
@section('venta_content')
<div class="ui compact segment">
<p>Valor Promesa {{$format->ufs($venta->valor)}}</p>
@if (isset($venta->formaPago()->pie))
<p>Valor Anticipo {{$format->ufs($venta->formaPago()->pie->valor)}}</p>
@endif
<p>Valor 10% {{$format->ufs($venta->valor * 0.1)}}</p>
</div>
<form class="ui form" id="add_bono">
<div class="three wide field">
<label for="fecha">Fecha</label>
<div class="ui calendar" id="fecha">
<div class="ui left icon input">
<i class="calendar icon"></i>
<input type="text" placeholder="Fecha" />
</div>
</div>
</div>
<div class="three wide field">
<label for="valor">Valor</label>
<div class="ui right labeled input">
<input type="text" name="valor" id="valor" />
<div class="ui basic label">UF</div>
</div>
</div>
<button class="ui button">Agregar</button>
</form>
@endsection
@push('page_scripts')
<script>
$(document).ready(() => {
calendar_date_options.initialDate = new Date({{$venta->fecha->format('Y, m-1, j')}})
$('#fecha').calendar(calendar_date_options)
$('#add_bono').submit(submitEvent => {
submitEvent.preventDefault()
const url = '{{$urls->api}}/venta/{{$venta->id}}/bono_pie/add'
const data = new FormData()
data.set('fecha', $('#fecha').calendar('get date').toISOString())
data.set('valor', $('#valor').val())
return APIClient.fetch(url, {method: 'post', body: data}).then(response => {
if (response.ok) {
return response.json()
}
}).then(json => {
if (!json.success) {
return
}
window.location = '{{$urls->base}}/venta/{{$venta->id}}'
})
})
})
</script>
@endpush

View File

@ -0,0 +1,42 @@
<?php
namespace Incoviba\Controller\API\Ventas;
use DateTimeImmutable;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Common\Ideal\Controller;
use Incoviba\Controller\API\withJson;
use Incoviba\Service;
use Incoviba\Repository;
class Bonos extends Controller
{
use withJson;
public function add(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta $ventaRepository,
Service\Valor $valorService, Service\UF $ufService,
Service\Venta\BonoPie $bonoPieService, int $venta_id): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'venta_id' => $venta_id,
'input' => $input,
'bono' => null,
'success' => false
];
try {
$venta = $ventaRepository->fetchById($venta_id);
$input['venta_id'] = $venta->id;
$uf = $ufService->get(new DateTimeImmutable($input['fecha']));
$input['valor'] = $input['valor'] * $uf;
$input['valor'] = $valorService->clean($input['valor']);
$input['uf'] = $uf;
$bono = $bonoPieService->add($input);
$ventaRepository->edit($venta, ['bono_pie' => $bono->id]);
$output['bono'] = $bono;
$output['success'] = true;
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace Incoviba\Controller\Ventas;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Alias\View;
use Incoviba\Repository;
use Incoviba\Service;
class Bonos
{
public function add(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Venta $ventaService, int $venta_id): ResponseInterface
{
$venta = $ventaService->getById($venta_id);
return $view->render($response, 'ventas.pies.bonos.add', compact('venta'));
}
}

View File

@ -16,7 +16,7 @@ class BonoPie extends Ideal\Repository
public function create(?array $data = null): Define\Model
{
$map = (new Implement\Repository\MapperParser())
$map = (new Implement\Repository\MapperParser(['valor']))
->register('pago', (new Implement\Repository\Mapper())
->setFunction(function($data) {
return $this->pagoRepository->fetchById($data['pago']);

View File

@ -1,18 +1,31 @@
<?php
namespace Incoviba\Service\Venta;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Repository;
use Incoviba\Model;
use Psr\Log\LoggerInterface;
class BonoPie
{
public function __construct(protected Repository\Venta\BonoPie $bonoPieRepository) {}
public function __construct(protected Repository\Venta\BonoPie $bonoPieRepository,
protected LoggerInterface $logger,
protected Pago $pagoService) {}
public function add(array $data): Model\Venta\BonoPie
{
$filteredData = $this->bonoPieRepository->filterData($data);
$bono = $this->bonoPieRepository->create($filteredData);
return $this->bonoPieRepository->save($bono);
if (!key_exists('pago', $filteredData)) {
$pago = $this->pagoService->add($filteredData);
$filteredData['pago'] = $pago->id;
}
try {
$bono = $this->bonoPieRepository->fetchByPago($filteredData['pago']);
} catch (EmptyResult) {
$bono = $this->bonoPieRepository->create($filteredData);
$bono = $this->bonoPieRepository->save($bono);
}
return $bono;
}
public function getByVenta(int $venta_id): Model\Venta\BonoPie
{