Agregar y editar abono cuotas.

This commit is contained in:
Juan Pablo Vial
2024-11-28 17:12:35 -03:00
parent 4f0a56c711
commit f4b8634cb4
10 changed files with 114 additions and 12 deletions

View File

@ -36,6 +36,7 @@ $app->group('/venta/{venta_id:[0-9]+}', function($app) {
$app->get('[/]', Ventas\Abono\Cuotas::class); $app->get('[/]', Ventas\Abono\Cuotas::class);
}); });
$app->get('/add[/]', [Ventas\Escrituras::class, 'add']); $app->get('/add[/]', [Ventas\Escrituras::class, 'add']);
$app->get('[/]', [Ventas\Escrituras::class, 'show']);
}); });
$app->group('/credito', function($app) { $app->group('/credito', function($app) {
$app->get('[/]', [Ventas\Creditos::class, 'show']); $app->get('[/]', [Ventas\Creditos::class, 'show']);

View File

@ -35,6 +35,9 @@ $app->group('/venta/{venta_id}', function($app) {
$app->post('/add[/]', [Ventas\Bonos::class, 'add']); $app->post('/add[/]', [Ventas\Bonos::class, 'add']);
}); });
$app->group('/escritura', function($app) { $app->group('/escritura', function($app) {
$app->group('/cuotas', function($app) {
$app->post('/add[/]', [Ventas\Abonos\Cuotas::class, 'add']);
});
$app->post('/add[/]', [Ventas\Escrituras::class, 'add']); $app->post('/add[/]', [Ventas\Escrituras::class, 'add']);
}); });
$app->group('/credito', function($app) { $app->group('/credito', function($app) {

View File

@ -14,7 +14,7 @@
<th>UF</th> <th>UF</th>
<th rowspan="2">Estado</th> <th rowspan="2">Estado</th>
<th class="right aligned"> <th class="right aligned">
<button class="ui green icon button" id="add_button"> <button class="ui tertiary green icon button" id="add_button">
<i class="plus icon"></i> <i class="plus icon"></i>
</button> </button>
</th> </th>
@ -50,13 +50,13 @@
$(document).ready(function () { $(document).ready(function () {
const addModal = new AddModal({ const addModal = new AddModal({
modal: '#add_cuota_modal', modal: '#add_cuota_modal',
form: '#add_cuota_form', form: 'add_cuota_form',
fecha: '#add_fecha', fecha: '#add_fecha',
}) })
const editModal = new EditModal({ const editModal = new EditModal({
table: 'cuotas', table: 'cuotas',
modal: '#edit_cuota_modal', modal: '#edit_cuota_modal',
form: '#edit_cuota_form', form: 'edit_cuota_form',
fecha: '#edit_fecha', fecha: '#edit_fecha',
estado: '#edit_estado' estado: '#edit_estado'
}) })

View File

@ -4,7 +4,7 @@
</div> </div>
<div class="content"> <div class="content">
<form class="ui form" id="add_cuota_form"> <form class="ui form" id="add_cuota_form">
<input type="hidden" name="id" /> <input type="hidden" name="venta_id" value="{{$venta->id}}" />
<div class="two wide field"> <div class="two wide field">
<label>Número</label> <label>Número</label>
<input type="text" name="numero" /> <input type="text" name="numero" />

View File

@ -36,11 +36,11 @@
<label>Estado</label> <label>Estado</label>
<div class="ui selection search dropdown" id="edit_estado"> <div class="ui selection search dropdown" id="edit_estado">
<i class="dropdown icon"></i> <i class="dropdown icon"></i>
<input type="hidden" name="estado" /> <input type="hidden" name="tipo_estado_id" />
<div class="default text">Estado</div> <div class="default text">Estado</div>
<div class="menu"> <div class="menu">
@foreach($estados as $estado) @foreach($estados as $estado)
<div class="item" data-value="{{$estado->id}}">{{$estado->nombre}}</div> <div class="item" data-value="{{$estado->id}}">{{$estado->descripcion}}</div>
@endforeach @endforeach
</div> </div>
</div> </div>
@ -100,7 +100,7 @@
const body = new FormData(form) const body = new FormData(form)
const fecha = $(this.props.fecha).calendar('get date') const fecha = $(this.props.fecha).calendar('get date')
body.set('fecha', fecha.getFullYear() + '-' + (fecha.getMonth() + 1).toString().padStart(2, '0') + '-' + fecha.getDate().toString().padStart(2, '0')) body.set('fecha', fecha.getFullYear() + '-' + (fecha.getMonth() + 1).toString().padStart(2, '0') + '-' + fecha.getDate().toString().padStart(2, '0'))
body.set('estado', $(this.props.estado).dropdown('get value')) body.set('tipo_estado_id', $(this.props.estado).dropdown('get value'))
const url = `{{$urls->api}}/venta/{{$venta->id}}/escritura/cuota/${this.data.id}/edit` const url = `{{$urls->api}}/venta/{{$venta->id}}/escritura/cuota/${this.data.id}/edit`
const method = 'post' const method = 'post'
APIClient.fetch(url, {method, body}).then(response => { APIClient.fetch(url, {method, body}).then(response => {

View File

@ -5,6 +5,13 @@
@endsection @endsection
@section('venta_content') @section('venta_content')
@if (count($venta->formaPago()->cuotasAbono) > 0)
<a href="{{$urls->base}}/venta/{{$venta->id}}/escritura/cuotas" class="ui small green button">Ver Cuotas</a>
<p>{{$format->pesos(array_reduce($venta->formaPago()->cuotas, function($sum, $cuota) {return $sum + $cuota->pago->valor;}, 0))}}</p>
<p>{{$format->ufs(array_reduce($venta->formaPago()->cuotas, function($sum, $cuota) {return $sum + $cuota->pago->valor();}, 0.0))}}</p>
@else
<a href="{{$urls->base}}/venta/{{$venta->id}}/escritura/cuotas" class="ui small green button"><i class="plus icon"></i> Agregar Cuotas</a>
@endif
<form class="ui form" id="edit_form"> <form class="ui form" id="edit_form">
<div class="three wide field"> <div class="three wide field">
<label for="fecha">Fecha</label> <label for="fecha">Fecha</label>
@ -15,6 +22,26 @@
</div> </div>
</div> </div>
</div> </div>
<div class="three wide field">
<label for="valor">Valor</label>
<div class="ui left labeled input">
<div class="ui basic label">$</div>
<input type="text" name="valor" value="{{$venta->formaPago()->escritura->pago->valor}}" />
</div>
</div>
<div class="three wide field">
<label>Estado</label>
<div class="ui selection dropdown" id="estado">
<input type="hidden" name="estado" />
<div class="default text">Estado</div>
<i class="dropdown icon"></i>
<div class="menu">
@foreach($estados as $estado)
<div class="item" data-value="{{$estado->id}}">{{ucwords($estado->descripcion)}}</div>
@endforeach
</div>
</div>
</div>
<button class="ui button">Guardar</button> <button class="ui button">Guardar</button>
</form> </form>
@endsection @endsection
@ -27,12 +54,13 @@
data.set('venta', {{$venta->id}}) data.set('venta', {{$venta->id}})
const fecha = $('#fecha').calendar('get date') const fecha = $('#fecha').calendar('get date')
data.set('fecha', fecha.toISOString()) data.set('fecha', fecha.toISOString())
data.set('estado', $('#estado').dropdown('get value'))
return fetchAPI(url, {method: 'post', body: data}).then(response => { return fetchAPI(url, {method: 'post', body: data}).then(response => {
if (response.ok) { if (response.ok) {
return response.json() return response.json()
} }
}).then(json => { }).then(json => {
if (!json.edited) { if (!json.success) {
return return
} }
window.location = '{{$urls->base}}/venta/{{$venta->id}}' window.location = '{{$urls->base}}/venta/{{$venta->id}}'
@ -41,6 +69,8 @@
$(document).ready(() => { $(document).ready(() => {
calendar_date_options.initialDate = new Date({{$venta->currentEstado()->fecha->format('Y, m-1, j')}}) calendar_date_options.initialDate = new Date({{$venta->currentEstado()->fecha->format('Y, m-1, j')}})
$('#fecha').calendar(calendar_date_options) $('#fecha').calendar(calendar_date_options)
$('#estado').dropdown()
$('#estado').dropdown('set selected', '{{$venta->currentEstado()->id}}')
$('#edit_form').submit(event => { $('#edit_form').submit(event => {
event.preventDefault() event.preventDefault()
editEscritura() editEscritura()

View File

@ -0,0 +1,60 @@
<?php
namespace Incoviba\Controller\API\Ventas\Abonos;
use DateTimeImmutable;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement;
use Incoviba\Controller\API;
use Incoviba\Repository;
use Incoviba\Service;
class Cuotas extends Ideal\Controller
{
use API\withJson;
public function add(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\Pago $pagoService,
Service\UF $ufService,
Service\Valor $valorService,
Repository\Venta\Abono\Cuota $cuotaRepository): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'input' => $input,
'cuota' => null,
'success' => false,
];
try {
$input['valor'] = $valorService->clean($input['valor']);
if (isset($input['uf']) and !empty($input['uf'])) {
$uf = $ufService->get(new DateTimeImmutable($input['fecha']));
$input['valor'] = $uf * $valorService->clean($input['uf']);
}
$pagoData = array_intersect_key($input, array_flip(['fecha', 'valor']));
$pago = $pagoService->add($pagoData);
$cuotaData = array_intersect_key($input, array_flip(['venta_id', 'numero']));
$cuotaData['pago_id'] = $pago->id;
$cuota = $cuotaRepository->create($cuotaData);
$output['cuota'] = $cuotaRepository->save($cuota);
$output['success'] = true;
} catch (Implement\Exception\EmptyResult) {}
return $this->withJson($response, $output);
}
public function edit(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Abono\Cuota $cuotaRepository, int $cuota_id): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'input' => $input,
'cuota' => null,
'success' => false,
];
try {
$cuota = $cuotaRepository->fetchById($cuota_id);
$output['cuota'] = $cuotaRepository->edit($cuota, $input);
$output['success'] = true;
} catch (Implement\Exception\EmptyResult) {}
return $this->withJson($response, $output);
}
}

View File

@ -12,6 +12,7 @@ class Cuotas
{ {
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, public function __invoke(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta $ventaService, Service\Venta $ventaService,
Repository\Venta\TipoEstadoPago $estadoPagoRepository,
Repository\Venta\Abono\Cuota $cuotaRepository, View $view, int $venta_id): ResponseInterface Repository\Venta\Abono\Cuota $cuotaRepository, View $view, int $venta_id): ResponseInterface
{ {
$venta = null; $venta = null;
@ -22,6 +23,10 @@ class Cuotas
try { try {
$cuotas = $cuotaRepository->fetchByVenta($venta_id); $cuotas = $cuotaRepository->fetchByVenta($venta_id);
} catch (EmptyResult $e) {} } catch (EmptyResult $e) {}
return $view->render($response, 'ventas.escrituras.abono.cuotas', compact('venta', 'cuotas')); $estados = [];
try {
$estados = $estadoPagoRepository->fetchAll();
} catch (EmptyResult $e) {}
return $view->render($response, 'ventas.escrituras.abono.cuotas', compact('venta', 'cuotas', 'estados'));
} }
} }

View File

@ -10,10 +10,12 @@ use Psr\Http\Message\ServerRequestInterface;
class Escrituras class Escrituras
{ {
public function show(ServerRequestInterface $request, ResponseInterface $response, View $view, public function show(ServerRequestInterface $request, ResponseInterface $response, View $view,
Repository\Venta\TipoEstadoPago $estadoPagoRepository,
Service\Venta $ventaService, int $venta_id): ResponseInterface Service\Venta $ventaService, int $venta_id): ResponseInterface
{ {
$venta = $ventaService->getById($venta_id); $venta = $ventaService->getById($venta_id);
return $view->render($response, 'ventas.escrituras.show', compact('venta')); $estados = $estadoPagoRepository->fetchAll();
return $view->render($response, 'ventas.escrituras.show', compact('venta', 'estados'));
} }
public function informe(ServerRequestInterface $request, ResponseInterface $response, View $view, public function informe(ServerRequestInterface $request, ResponseInterface $response, View $view,
Service\Venta $ventaService, int $venta_id): ResponseInterface Service\Venta $ventaService, int $venta_id): ResponseInterface

View File

@ -7,11 +7,12 @@ use Incoviba\Common\Implement;
use Incoviba\Common\Implement\Exception\EmptyResult; use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Model; use Incoviba\Model;
use Incoviba\Repository; use Incoviba\Repository;
use Incoviba\Service;
class Cuota extends Ideal\Repository class Cuota extends Ideal\Repository
{ {
public function __construct(Define\Connection $connection, protected Repository\Venta $ventaRepository, public function __construct(Define\Connection $connection, protected Repository\Venta $ventaRepository,
protected Repository\Venta\Pago $pagoRepository) protected Service\Venta\Pago $pagoService)
{ {
parent::__construct($connection); parent::__construct($connection);
$this->setTable('venta_abono_cuotas'); $this->setTable('venta_abono_cuotas');
@ -29,7 +30,7 @@ class Cuota extends Ideal\Repository
->register('pago_id', (new Implement\Repository\Mapper()) ->register('pago_id', (new Implement\Repository\Mapper())
->setProperty('pago') ->setProperty('pago')
->setFunction(function($data) { ->setFunction(function($data) {
return $this->pagoRepository->fetchById($data['pago_id']); return $this->pagoService->getById($data['pago_id']);
}) })
); );
return $this->parseData(new Model\Venta\Abono\Cuota(), $data, $map); return $this->parseData(new Model\Venta\Abono\Cuota(), $data, $map);