Listado de brokers

This commit is contained in:
Juan Pablo Vial
2025-03-06 20:34:43 -03:00
parent 18dd8c4ec0
commit c7ee440e03
6 changed files with 226 additions and 0 deletions

View File

@ -2,6 +2,16 @@
use Incoviba\Controller\Proyectos;
$app->group('/proyectos', function($app) {
$folder = implode(DIRECTORY_SEPARATOR, [__DIR__, 'proyectos']);
if (file_exists($folder)) {
$files = new FilesystemIterator($folder);
foreach ($files as $file) {
if ($file->isDir()) {
continue;
}
include_once $file->getRealPath();
}
}
$app->get('/unidades[/]', [Proyectos::class, 'unidades']);
$app->get('[/]', Proyectos::class);
})->add($app->getContainer()->get(Incoviba\Middleware\Authentication::class));

View File

@ -0,0 +1,6 @@
<?php
use Incoviba\Controller\Proyectos\Brokers;
$app->group('/brokers', function($app) {
$app->get('[/]', [Brokers::class, 'projects']);
});

View File

@ -0,0 +1,119 @@
@extends('proyectos.brokers.base')
@section('brokers_content')
@include('proyectos.brokers.proyectos')
<div id="brokers"></div>
@endsection
@push('page_scripts')
<script>
const brokers = {
data: {
contracts: {},
project_id: null
},
formatters: {
date: new Intl.DateTimeFormat('es-CL', {dateStyle: 'medium'}),
percent: new Intl.NumberFormat('es-CL', {style: 'percent', minimumFractionDigits: 2, maximumFractionDigits: 2})
},
draw() {
return {
brokers: project_id => {
$('#projects').hide()
const div = document.getElementById('brokers')
const table = document.createElement('table')
table.classList.add('ui', 'table')
table.innerHTML = [
"<thead>",
"<tr>",
"<th>Operador</th>",
"<th>Comisión</th>",
"<th>Fecha Inicio</th>",
"<th class='right aligned'><button class='ui small tertiary green icon button' id='add_button'><i class='plus icon'></i></button></th>",
"</tr>",
"</thead>"].join('')
const tbody = document.createElement('tbody')
brokers.data.contracts[project_id].forEach(contract => {
const dateParts = contract.current.date.split('-')
const date = new Date([dateParts[0], parseInt(dateParts[1]) - 1, dateParts[2]].join('-'))
date.setMonth(date.getMonth() + 1)
tbody.insertAdjacentHTML('beforeend', [
"<tr>",
`<td>${contract.broker.name}</td>`,
`<td>${brokers.formatters.percent.format(contract.commission)}</td>`,
`<td>${brokers.formatters.date.format(date)}</td>`,
`<td class="right aligned"><button class="ui small tertiary red icon button remove_button" data-id="${contract.id}"><i class="remove icon"></i></button></td>`,
"</tr>"].join(''))
})
table.append(tbody)
div.innerHTML = ''
div.append(table)
$('#brokers').show()
}
}
},
get() {
return {
contracts: project_id => {
brokers.actions().startSpinner()
brokers.data.project_id = project_id
const url = `{{$urls->api}}/proyecto/${project_id}/brokers`
APIClient.fetch(url).then(response => {
if (!response.ok) {
return
}
return response.json().then(json => {
brokers.data.contracts[json.proyecto_id] = json.contracts
const promises = []
json.contracts.forEach(contract => {
promises.push(brokers.get().broker(contract.broker_rut).then(broker => {
brokers.data.contracts[json.proyecto_id].find(contract => contract.broker_rut === broker.rut).broker = broker
}))
})
Promise.all(promises).then(() => {
brokers.draw().brokers(json.proyecto_id)
})
})
}).finally(() => {
brokers.actions().stopSpinner()
})
},
broker: broker_rut => {
const url = `{{$urls->api}}/proyectos/broker/${broker_rut}`
return APIClient.fetch(url).then(response => {
if (!response.ok) {
return
}
return response.json().then(json => {
return json.broker
})
})
}
}
},
actions() {
return {
startSpinner: () => {
$('#refresh_button').addClass('loading')
},
stopSpinner: () => {
$('#refresh_button').removeClass('loading')
},
refresh: () => {
brokers.get().contracts(brokers.data.project_id)
},
up: () => {
$('#brokers').hide()
$('#projects').show()
brokers.data.project_id = null
document.getElementById('brokers').innerHTML = ''
}
}
}
}
$(document).ready(() => {
$('#brokers').hide()
})
</script>
@endpush

View File

@ -0,0 +1,15 @@
@extends('layout.base')
@section('page_title')
Operadores
@hasSection('brokers_title')
- @yield('brokers_title')
@endif
@endsection
@section('page_content')
<div class="ui container">
<h2 class="ui header">Operadores</h2>
@yield('brokers_content')
</div>
@endsection

View File

@ -0,0 +1,52 @@
<div class="ui top attached right aligned basic segment">
<button type="button" class="ui mini tertiary icon button" id="refresh_button">
<i class="sync alternate icon"></i>
</button>
<button type="button" class="ui mini tertiary icon button" id="up_button">
<i class="arrow up icon"></i>
</button>
</div>
<table class="ui table" id="projects">
<thead>
<tr>
<th>Proyecto</th>
</tr>
</thead>
<tbody>
@foreach($projects as $project)
<tr data-index="{{$project->id}}">
<td class="link" colspan="2">{{$project->descripcion}}</td>
</tr>
@endforeach
</tbody>
</table>
@push('page_scripts')
<script>
$(document).ready(function () {
document.querySelectorAll('#projects td.link').forEach(column => {
column.style.cursor = 'pointer'
column.addEventListener('click', () => {
const index = column.parentNode.dataset.index
if (typeof brokers.data.contracts[index] !== 'undefined') {
brokers.data.project_id = index
brokers.draw().brokers(index)
return
}
brokers.get().contracts(index)
})
})
document.getElementById('refresh_button').addEventListener('click', () => {
if (brokers.data.project_id === null) {
return
}
brokers.actions().refresh()
})
document.getElementById('up_button').addEventListener('click', () => {
brokers.actions().up()
})
})
</script>
@endpush

View File

@ -0,0 +1,24 @@
<?php
namespace Incoviba\Controller\Proyectos;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Incoviba\Common\Alias\View;
use Incoviba\Repository;
use Incoviba\Service;
use Psr\Log\LoggerInterface;
class Brokers
{
public function projects(ServerRequestInterface $request, ResponseInterface $response, LoggerInterface $logger, Repository\Proyecto $proyectoRepository, View $view): ResponseInterface
{
$projects = [];
try {
$projects = $proyectoRepository->fetchAll('descripcion');
} catch (EmptyResult $exception) {
$logger->debug($exception);
}
return $view->render($response, 'proyectos.brokers', compact('projects'));
}
}