diff --git a/app/resources/routes/04_ventas.php b/app/resources/routes/04_ventas.php index 49400bb..2ce4212 100644 --- a/app/resources/routes/04_ventas.php +++ b/app/resources/routes/04_ventas.php @@ -36,6 +36,7 @@ $app->group('/venta/{venta_id:[0-9]+}', function($app) { $app->get('[/]', Ventas\Abono\Cuotas::class); }); $app->get('/add[/]', [Ventas\Escrituras::class, 'add']); + $app->get('[/]', [Ventas\Escrituras::class, 'show']); }); $app->group('/credito', function($app) { $app->get('[/]', [Ventas\Creditos::class, 'show']); diff --git a/app/resources/routes/api/ventas.php b/app/resources/routes/api/ventas.php index 954eb1c..3a826a4 100644 --- a/app/resources/routes/api/ventas.php +++ b/app/resources/routes/api/ventas.php @@ -35,6 +35,9 @@ $app->group('/venta/{venta_id}', function($app) { $app->post('/add[/]', [Ventas\Bonos::class, 'add']); }); $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->group('/credito', function($app) { diff --git a/app/resources/views/ventas/escrituras/abono/cuotas.blade.php b/app/resources/views/ventas/escrituras/abono/cuotas.blade.php index af664d6..1266beb 100644 --- a/app/resources/views/ventas/escrituras/abono/cuotas.blade.php +++ b/app/resources/views/ventas/escrituras/abono/cuotas.blade.php @@ -14,7 +14,7 @@ UF Estado - @@ -50,13 +50,13 @@ $(document).ready(function () { const addModal = new AddModal({ modal: '#add_cuota_modal', - form: '#add_cuota_form', + form: 'add_cuota_form', fecha: '#add_fecha', }) const editModal = new EditModal({ table: 'cuotas', modal: '#edit_cuota_modal', - form: '#edit_cuota_form', + form: 'edit_cuota_form', fecha: '#edit_fecha', estado: '#edit_estado' }) diff --git a/app/resources/views/ventas/escrituras/abono/cuotas/add_modal.blade.php b/app/resources/views/ventas/escrituras/abono/cuotas/add_modal.blade.php index ea5541f..9b3336d 100644 --- a/app/resources/views/ventas/escrituras/abono/cuotas/add_modal.blade.php +++ b/app/resources/views/ventas/escrituras/abono/cuotas/add_modal.blade.php @@ -4,7 +4,7 @@
- +
diff --git a/app/resources/views/ventas/escrituras/abono/cuotas/edit_modal.blade.php b/app/resources/views/ventas/escrituras/abono/cuotas/edit_modal.blade.php index b25df7c..9e423a0 100644 --- a/app/resources/views/ventas/escrituras/abono/cuotas/edit_modal.blade.php +++ b/app/resources/views/ventas/escrituras/abono/cuotas/edit_modal.blade.php @@ -36,11 +36,11 @@ @@ -100,7 +100,7 @@ const body = new FormData(form) 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('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 method = 'post' APIClient.fetch(url, {method, body}).then(response => { diff --git a/app/resources/views/ventas/escrituras/show.blade.php b/app/resources/views/ventas/escrituras/show.blade.php index 5be5aab..77d5447 100644 --- a/app/resources/views/ventas/escrituras/show.blade.php +++ b/app/resources/views/ventas/escrituras/show.blade.php @@ -5,6 +5,13 @@ @endsection @section('venta_content') + @if (count($venta->formaPago()->cuotasAbono) > 0) + Ver Cuotas +

{{$format->pesos(array_reduce($venta->formaPago()->cuotas, function($sum, $cuota) {return $sum + $cuota->pago->valor;}, 0))}}

+

{{$format->ufs(array_reduce($venta->formaPago()->cuotas, function($sum, $cuota) {return $sum + $cuota->pago->valor();}, 0.0))}}

+ @else + Agregar Cuotas + @endif
@@ -15,6 +22,26 @@
+
+ +
+
$
+ +
+
+
+ + +
@endsection @@ -27,12 +54,13 @@ data.set('venta', {{$venta->id}}) const fecha = $('#fecha').calendar('get date') data.set('fecha', fecha.toISOString()) + data.set('estado', $('#estado').dropdown('get value')) return fetchAPI(url, {method: 'post', body: data}).then(response => { if (response.ok) { return response.json() } }).then(json => { - if (!json.edited) { + if (!json.success) { return } window.location = '{{$urls->base}}/venta/{{$venta->id}}' @@ -41,6 +69,8 @@ $(document).ready(() => { calendar_date_options.initialDate = new Date({{$venta->currentEstado()->fecha->format('Y, m-1, j')}}) $('#fecha').calendar(calendar_date_options) + $('#estado').dropdown() + $('#estado').dropdown('set selected', '{{$venta->currentEstado()->id}}') $('#edit_form').submit(event => { event.preventDefault() editEscritura() diff --git a/app/src/Controller/API/Ventas/Abonos/Cuotas.php b/app/src/Controller/API/Ventas/Abonos/Cuotas.php new file mode 100644 index 0000000..f550a5e --- /dev/null +++ b/app/src/Controller/API/Ventas/Abonos/Cuotas.php @@ -0,0 +1,60 @@ +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); + } +} diff --git a/app/src/Controller/Ventas/Abono/Cuotas.php b/app/src/Controller/Ventas/Abono/Cuotas.php index 318d65a..ac1a51f 100644 --- a/app/src/Controller/Ventas/Abono/Cuotas.php +++ b/app/src/Controller/Ventas/Abono/Cuotas.php @@ -12,6 +12,7 @@ class Cuotas { public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service\Venta $ventaService, + Repository\Venta\TipoEstadoPago $estadoPagoRepository, Repository\Venta\Abono\Cuota $cuotaRepository, View $view, int $venta_id): ResponseInterface { $venta = null; @@ -22,6 +23,10 @@ class Cuotas try { $cuotas = $cuotaRepository->fetchByVenta($venta_id); } 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')); } } diff --git a/app/src/Controller/Ventas/Escrituras.php b/app/src/Controller/Ventas/Escrituras.php index 31b71c4..c865fb1 100644 --- a/app/src/Controller/Ventas/Escrituras.php +++ b/app/src/Controller/Ventas/Escrituras.php @@ -10,10 +10,12 @@ use Psr\Http\Message\ServerRequestInterface; class Escrituras { public function show(ServerRequestInterface $request, ResponseInterface $response, View $view, + Repository\Venta\TipoEstadoPago $estadoPagoRepository, Service\Venta $ventaService, int $venta_id): ResponseInterface { $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, Service\Venta $ventaService, int $venta_id): ResponseInterface diff --git a/app/src/Repository/Venta/Abono/Cuota.php b/app/src/Repository/Venta/Abono/Cuota.php index 6ad7d9b..2e41138 100644 --- a/app/src/Repository/Venta/Abono/Cuota.php +++ b/app/src/Repository/Venta/Abono/Cuota.php @@ -7,11 +7,12 @@ use Incoviba\Common\Implement; use Incoviba\Common\Implement\Exception\EmptyResult; use Incoviba\Model; use Incoviba\Repository; +use Incoviba\Service; class Cuota extends Ideal\Repository { public function __construct(Define\Connection $connection, protected Repository\Venta $ventaRepository, - protected Repository\Venta\Pago $pagoRepository) + protected Service\Venta\Pago $pagoService) { parent::__construct($connection); $this->setTable('venta_abono_cuotas'); @@ -29,7 +30,7 @@ class Cuota extends Ideal\Repository ->register('pago_id', (new Implement\Repository\Mapper()) ->setProperty('pago') ->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);