Cartolas
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -8,3 +8,4 @@
|
||||
**/cache/
|
||||
**/modules/
|
||||
**/.idea/
|
||||
**/upload?/
|
||||
|
9
app/common/Define/Cartola/Banco.php
Normal file
9
app/common/Define/Cartola/Banco.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Define\Cartola;
|
||||
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
|
||||
interface Banco
|
||||
{
|
||||
public function process(UploadedFileInterface $file): array;
|
||||
}
|
10
app/common/Define/Contabilidad/Exporter.php
Normal file
10
app/common/Define/Contabilidad/Exporter.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Define\Contabilidad;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Incoviba\Model;
|
||||
|
||||
interface Exporter
|
||||
{
|
||||
public function export(Model\Inmobiliaria $inmobiliaria, Model\Banco $banco, DateTimeInterface $mes, array $movimientos): string;
|
||||
}
|
@ -9,6 +9,7 @@
|
||||
"nyholm/psr7-server": "^1.0",
|
||||
"php-di/php-di": "^7.0",
|
||||
"php-di/slim-bridge": "^3.4",
|
||||
"phpoffice/phpspreadsheet": "^1.29",
|
||||
"predis/predis": "^2.2",
|
||||
"slim/slim": "^4.11"
|
||||
},
|
||||
|
9
app/resources/routes/api/contabilidad/cartolas.php
Normal file
9
app/resources/routes/api/contabilidad/cartolas.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
use Incoviba\Controller\API\Contabilidad;
|
||||
|
||||
$app->group('/cartolas', function($app) {
|
||||
$app->post('/procesar[/]', [Contabilidad::class, 'procesarCartola']);
|
||||
});
|
||||
$app->group('/cartola', function($app) {
|
||||
$app->post('/exportar[/]', [Contabilidad::class, 'exportarCartola']);
|
||||
});
|
11
app/resources/routes/api/contabilidad/nubox.php
Normal file
11
app/resources/routes/api/contabilidad/nubox.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
use Incoviba\Controller\API\Nubox;
|
||||
|
||||
$app->group('/nubox/{inmobiliaria_rut}', function($app) {
|
||||
$app->get('/token[/]', [Nubox::class, 'token']);
|
||||
$app->get('/sistemas[/]', [Nubox::class, 'sistemas']);
|
||||
$app->group('/libro', function($app) {
|
||||
$app->post('/mayor[/]', [Nubox::class, 'libroMayor']);
|
||||
$app->post('/diario[/]', [Nubox::class, 'libroDiario']);
|
||||
});
|
||||
});
|
10
app/resources/routes/api/inmobiliarias.php
Normal file
10
app/resources/routes/api/inmobiliarias.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
use Incoviba\Controller\API\Inmobiliarias;
|
||||
|
||||
$app->group('/inmobiliarias', function($app) {
|
||||
$app->get('[/]', Inmobiliarias::class);
|
||||
});
|
||||
$app->group('/inmobiliaria/{inmobiliaria_rut}', function($app) {
|
||||
$app->get('/cuentas[/]', [Inmobiliarias::class, 'cuentas']);
|
||||
$app->get('/proyectos[/]', [Inmobiliarias::class, 'proyectos']);
|
||||
});
|
@ -2,5 +2,6 @@
|
||||
use Incoviba\Controller\CentrosCostos;
|
||||
|
||||
$app->group('/centros_costos', function($app) {
|
||||
$app->get('/asignar[/]', [CentrosCostos::class, 'asignar']);
|
||||
$app->get('[/]', CentrosCostos::class);
|
||||
});
|
||||
|
@ -0,0 +1,402 @@
|
||||
@extends('layout.base')
|
||||
|
||||
@section('page_content')
|
||||
<div class="ui container">
|
||||
<h1 class="ui header">Asignar Centros de Costos</h1>
|
||||
<form class="ui form" id="asignar_form">
|
||||
<div class="ui grid">
|
||||
<div class="fourteen wide column">
|
||||
<div class="fields">
|
||||
<div class="five wide field">
|
||||
<label>Inmobiliaria</label>
|
||||
<div class="ui selection search dropdown" id="inmobiliaria">
|
||||
<input type="hidden" name="inmobiliaria"/>
|
||||
<i class="dropdown icon"></i>
|
||||
<div class="default text">Inmobiliaria</div>
|
||||
<div class="menu">
|
||||
@foreach ($inmobiliarias as $inmobiliaria)
|
||||
<div class="item" data-value="{{$inmobiliaria->rut}}">{{$inmobiliaria->razon}}</div>
|
||||
@endforeach
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="two wide field">
|
||||
<label>Banco</label>
|
||||
<div class="ui selection search dropdown" id="banco">
|
||||
<input type="hidden" name="banco"/>
|
||||
<i class="dropdown icon"></i>
|
||||
<div class="default text">Banco</div>
|
||||
<div class="menu"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Mes</label>
|
||||
<div class="ui calendar" id="mes">
|
||||
<div class="ui icon input">
|
||||
<i class="calendar icon"></i>
|
||||
<input type="text" name="mes"/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label for="file">Cartola</label>
|
||||
<input type="file" name="file" id="file" class="ui invisible file input" />
|
||||
<label for="file" class="ui icon button">
|
||||
<i class="file icon"></i>
|
||||
Cargar
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="two wide middle aligned column">
|
||||
<button class="ui icon button">
|
||||
Procesar
|
||||
<i class="sync icon"></i>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
<div class="ui two columns grid">
|
||||
<div class="column">
|
||||
<button class="ui icon button" id="export_button">
|
||||
<i class="file excel icon"></i>
|
||||
Exportar
|
||||
</button>
|
||||
</div>
|
||||
<div class="right aligned column">
|
||||
<div class="ui inline active loader" id="loader"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="ui fluid container">
|
||||
<table class="ui table" id="tabla_movimientos">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Fecha</th>
|
||||
<th>Glosa</th>
|
||||
<th>Documento</th>
|
||||
<th class="right aligned">Cargo</th>
|
||||
<th class="right aligned">Abono</th>
|
||||
<th class="right aligned">Saldo</th>
|
||||
<th>Centro de Costo</th>
|
||||
<th>Detalle</th>
|
||||
<th>Orden</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="movimientos"></tbody>
|
||||
</table>
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
@include('layout.head.styles.datatables')
|
||||
@include('layout.body.scripts.datatables')
|
||||
|
||||
@push('page_scripts')
|
||||
<script>
|
||||
const cartola = {
|
||||
ids: {
|
||||
table: {
|
||||
base: '',
|
||||
body: ''
|
||||
},
|
||||
form: {
|
||||
base: '',
|
||||
inmobiliaria: '',
|
||||
banco: '',
|
||||
mes: '',
|
||||
},
|
||||
button: '',
|
||||
loader: '',
|
||||
},
|
||||
data: {
|
||||
inmobiliaria: {
|
||||
rut: 0,
|
||||
razon: ''
|
||||
},
|
||||
banco: {
|
||||
id: 0,
|
||||
nombre: ''
|
||||
},
|
||||
mes: '',
|
||||
movimientos: [],
|
||||
centrosCostos: {
|
||||
ingresos: JSON.parse('{!! json_encode(array_values(array_map(function(\Incoviba\Model\CentroCosto $centroCosto) {
|
||||
return [
|
||||
'id' => $centroCosto->id,
|
||||
'descripcion' => $centroCosto->descripcion
|
||||
];
|
||||
}, array_filter($centrosCostos, function(\Incoviba\Model\CentroCosto $centroCosto) {
|
||||
return $centroCosto->tipoCentro->descripcion === 'Ingreso';
|
||||
})))) !!}'),
|
||||
egresos: JSON.parse('{!! json_encode(array_values(array_map(function(\Incoviba\Model\CentroCosto $centroCosto) {
|
||||
return [
|
||||
'id' => $centroCosto->id,
|
||||
'descripcion' => $centroCosto->descripcion
|
||||
];
|
||||
}, array_filter($centrosCostos, function(\Incoviba\Model\CentroCosto $centroCosto) {
|
||||
return $centroCosto->tipoCentro->descripcion === 'Egreso';
|
||||
})))) !!}'),
|
||||
}
|
||||
},
|
||||
get() {
|
||||
return {
|
||||
bancos: inmobiliaria_rut => {
|
||||
const url = '{{$urls->api}}/inmobiliaria/' + inmobiliaria_rut + '/cuentas'
|
||||
$(this.ids.loader).show()
|
||||
return fetchAPI(url).then(response => {
|
||||
$(this.ids.loader).hide()
|
||||
if (!response) {
|
||||
return
|
||||
}
|
||||
return response.json().then(json => {
|
||||
if (json.cuentas.length === 0) {
|
||||
return
|
||||
}
|
||||
$(this.ids.form.banco).dropdown('change values', json.cuentas.map(cuenta => {
|
||||
return {value: cuenta.banco.id, text: cuenta.banco.nombre, name: cuenta.banco.nombre}
|
||||
})).dropdown('refresh')
|
||||
})
|
||||
})
|
||||
},
|
||||
firstDate: inmobiliaria_rut => {
|
||||
const url = '{{$urls->api}}/inmobiliaria/' + inmobiliaria_rut + '/proyectos'
|
||||
$(this.ids.loader).show()
|
||||
return fetchAPI(url).then(response => {
|
||||
$(this.ids.loader).hide()
|
||||
if (!response) {
|
||||
return
|
||||
}
|
||||
return response.json().then(json => {
|
||||
if (json.proyectos.length === 0) {
|
||||
return
|
||||
}
|
||||
const min = json.proyectos.reduce((min, proyecto) => {
|
||||
const date = new Date(proyecto.current_estado.fecha.date)
|
||||
if (min > date.getTime()) {
|
||||
return date.getTime()
|
||||
}
|
||||
return min
|
||||
}, (new Date()).getTime())
|
||||
$(this.ids.form.mes).calendar('set minDate', new Date(min))
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
parse() {
|
||||
return {
|
||||
cartola: event => {
|
||||
event.preventDefault()
|
||||
const body = new FormData(document.getElementById('asignar_form'))
|
||||
body.set('mes', $('#mes').calendar('get date').toISOString())
|
||||
const url = '{{$urls->api}}/contabilidad/cartolas/procesar'
|
||||
$(this.ids.loader).show()
|
||||
fetchAPI(url, {method: 'post', body}).then(response => {
|
||||
$(this.ids.loader).hide()
|
||||
if (!response) {
|
||||
return
|
||||
}
|
||||
return response.json().then(json => {
|
||||
if (json.movimientos.length === 0) {
|
||||
return
|
||||
}
|
||||
this.data.movimientos = []
|
||||
json.movimientos.forEach((row, idx) => {
|
||||
const fecha = new Date(row.fecha)
|
||||
fecha.setDate(fecha.getDate() + 1)
|
||||
this.data.movimientos[idx] = {
|
||||
fecha: fecha,
|
||||
glosa: row['descripción'],
|
||||
documento: row['número de documentos'],
|
||||
cargo: row.cargos,
|
||||
abono: row.abonos,
|
||||
saldo: row.saldos
|
||||
}
|
||||
})
|
||||
this.draw().cartola()
|
||||
})
|
||||
})
|
||||
return false
|
||||
}
|
||||
}
|
||||
},
|
||||
export() {
|
||||
return {
|
||||
cartola: event => {
|
||||
const url = '{{$urls->api}}/contabilidad/cartola/exportar'
|
||||
const body = new FormData()
|
||||
body.set('inmobiliaria', $(this.ids.form.inmobiliaria).dropdown('get value'))
|
||||
body.set('banco', $(this.ids.form.banco).dropdown('get value'))
|
||||
body.set('mes', $(this.ids.form.mes).calendar('get date').toISOString())
|
||||
const movimientos = this.data.movimientos.map((movimiento, idx) => {
|
||||
const temp = structuredClone(movimiento)
|
||||
temp.fecha = movimiento.fecha.toISOString()
|
||||
let centro = $(".centro[data-index='" + (idx+1) + "']").dropdown('get value')
|
||||
if (centro.length === 0) {
|
||||
centro = ''
|
||||
}
|
||||
temp.centro_costo = centro
|
||||
let detalle = $("[name='detalle" + (idx+1) + "']").val()
|
||||
if (typeof detalle === 'undefined') {
|
||||
detalle = ''
|
||||
}
|
||||
temp.detalle = detalle
|
||||
return temp
|
||||
})
|
||||
body.set('movimientos', JSON.stringify(movimientos))
|
||||
$(this.ids.loader).show()
|
||||
fetchAPI(url, {method: 'post', body}).then(response => {
|
||||
$(this.ids.loader).hide()
|
||||
if (!response) {
|
||||
return
|
||||
}
|
||||
response.json().then(json => {
|
||||
if (json.filename === '') {
|
||||
return
|
||||
}
|
||||
const anchor = document.createElement('a')
|
||||
anchor.href = ['/uploads/', json.filename].join('/')
|
||||
anchor.download = 'Cartola ' + this.data.banco.nombre + ' - ' + this.data.inmobiliaria.razon + ' - ' + this.data.mes + '.xlsx'
|
||||
document.body.appendChild(anchor)
|
||||
anchor.click()
|
||||
anchor.remove()
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
draw() {
|
||||
return {
|
||||
cartola: () => {
|
||||
const table = $(this.ids.table.base)
|
||||
table.DataTable().clear()
|
||||
table.DataTable().destroy()
|
||||
const tbody = $(this.ids.table.body)
|
||||
tbody.html('')
|
||||
const dateFormatter = new Intl.DateTimeFormat('es-CL', {
|
||||
year: 'numeric',
|
||||
month: 'numeric',
|
||||
day: 'numeric'
|
||||
})
|
||||
const numberFormatter = new Intl.NumberFormat('es-CL', {minimumFractionDigits: 0, maximumFractionDigits: 0})
|
||||
this.data.movimientos.forEach((row, idx) => {
|
||||
tbody.append(
|
||||
$('<tr></tr>').append(
|
||||
$('<td></td>').html(dateFormatter.format(row.fecha))
|
||||
).append(
|
||||
$('<td></td>').html(row.glosa)
|
||||
).append(
|
||||
$('<td></td>').html(row.documento)
|
||||
).append(
|
||||
$('<td></td>').addClass('right aligned').html(row.cargo === 0 ? '' : numberFormatter.format(row.cargo))
|
||||
).append(
|
||||
$('<td></td>').addClass('right aligned').html(row.abono === 0 ? '' : numberFormatter.format(row.abono))
|
||||
).append(
|
||||
$('<td></td>').addClass('right aligned').html(row.saldo === 0 ? '' : numberFormatter.format(row.saldo))
|
||||
).append(
|
||||
$('<td></td>').append(this.draw().centrosDropdown(idx + 1, row.cargo === 0))
|
||||
).append(
|
||||
$('<td></td>').append(
|
||||
$('<div></div>').addClass('ui fluid input').append(
|
||||
$('<input />').attr('type', 'text').attr('name', 'detalle' + (idx + 1))
|
||||
)
|
||||
)
|
||||
).append(
|
||||
$('<td></td>').html(idx + 1)
|
||||
)
|
||||
)
|
||||
})
|
||||
table.DataTable({
|
||||
pageLength: 100,
|
||||
order: [[8, 'asc']],
|
||||
columnDefs: [
|
||||
{
|
||||
targets: [1, 2, 3, 4, 5],
|
||||
width: '10%'
|
||||
},
|
||||
{
|
||||
targets: [1],
|
||||
width: '20%'
|
||||
},
|
||||
{
|
||||
targets: [8],
|
||||
visible: false
|
||||
}
|
||||
],
|
||||
})
|
||||
},
|
||||
centrosDropdown: (idx, ingreso=true) => {
|
||||
const menu = $('<div></div>').addClass('menu')
|
||||
let centros = this.data.centrosCostos.ingresos
|
||||
if (!ingreso) {
|
||||
centros = this.data.centrosCostos.egresos
|
||||
}
|
||||
centros.forEach(centro => {
|
||||
menu.append(
|
||||
$('<div></div>').addClass('item').attr('data-value', centro.id).html(centro.id + ' - ' + centro.descripcion)
|
||||
)
|
||||
})
|
||||
return $('<div></div>').addClass('ui selection search dropdown centro').attr('data-index', idx).append(
|
||||
$('<input />').attr('type', 'hidden').attr('name', 'centro' + idx)
|
||||
).append(
|
||||
$('<i></i>').addClass('dropdown icon')
|
||||
).append(
|
||||
$('<div></div>').addClass('default text').html('Centro de Costos')
|
||||
).append(menu).dropdown()
|
||||
}
|
||||
}
|
||||
},
|
||||
setup({ids}) {
|
||||
this.ids = ids
|
||||
$(this.ids.form.inmobiliaria).dropdown({
|
||||
fireOnInit: true,
|
||||
onChange: (value, text, $choice) => {
|
||||
this.data.inmobiliaria.rut = value
|
||||
this.data.inmobiliaria.razon = text
|
||||
this.get().bancos(value)
|
||||
this.get().firstDate(value)
|
||||
},
|
||||
})
|
||||
$(this.ids.form.banco).dropdown({
|
||||
fireOnInit: true,
|
||||
onChange: (value, text, $choice) => {
|
||||
this.data.banco.id = value
|
||||
this.data.banco.nombre = text
|
||||
}
|
||||
})
|
||||
$(this.ids.loader).hide()
|
||||
|
||||
calendar_date_options['type'] = 'month'
|
||||
const lastMonth = new Date()
|
||||
lastMonth.setDate(0)
|
||||
calendar_date_options['maxDate'] = lastMonth
|
||||
calendar_date_options['onChange'] = (date, text, mode) => {
|
||||
this.data.mes = text
|
||||
}
|
||||
$(this.ids.form.mes).calendar(calendar_date_options)
|
||||
$(this.ids.form.base).submit(this.parse().cartola)
|
||||
$(this.ids.table.base).DataTable()
|
||||
|
||||
$(this.ids.button).click(this.export().cartola)
|
||||
}
|
||||
}
|
||||
$(document).ready(() => {
|
||||
cartola.setup({
|
||||
ids: {
|
||||
table: {
|
||||
base: '#tabla_movimientos',
|
||||
body: '#movimientos'
|
||||
},
|
||||
form: {
|
||||
base: '#asignar_form',
|
||||
inmobiliaria: '#inmobiliaria',
|
||||
banco: '#banco',
|
||||
mes: '#mes',
|
||||
},
|
||||
button: '#export_button',
|
||||
loader: '#loader'
|
||||
}
|
||||
})
|
||||
})
|
||||
</script>
|
||||
@endpush
|
@ -6,7 +6,9 @@ return [
|
||||
'resources' => DI\String('{base}/resources'),
|
||||
'routes' => DI\String('{resources}/routes'),
|
||||
'cache' => DI\String('{base}/cache'),
|
||||
'templates' => DI\String('{resources}/views')
|
||||
'templates' => DI\String('{resources}/views'),
|
||||
'public' => DI\String('{base}/public'),
|
||||
'uploads' => DI\String('{public}/uploads')
|
||||
]);
|
||||
}
|
||||
];
|
||||
|
8
app/setup/settings/nubox.php
Normal file
8
app/setup/settings/nubox.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
return [
|
||||
'nubox' => function() {
|
||||
return new DI\Container([
|
||||
'url' => 'https://api.nubox.com/Nubox.API'
|
||||
]);
|
||||
}
|
||||
];
|
@ -2,7 +2,13 @@
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
return [
|
||||
Psr\Http\Message\StreamFactoryInterface::class => function(ContainerInterface $container) {
|
||||
return $container->get(Nyholm\Psr7\Factory\Psr17Factory::class);
|
||||
},
|
||||
Psr\Http\Message\RequestFactoryInterface::class => function(ContainerInterface $container) {
|
||||
return $container->get(Nyholm\Psr7\Factory\Psr17Factory::class);
|
||||
},
|
||||
Psr\Http\Message\ResponseFactoryInterface::class => function(ContainerInterface $container) {
|
||||
return $container->get(Nyholm\Psr7\Factory\Psr17Factory::class);
|
||||
}
|
||||
},
|
||||
];
|
||||
|
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
return [
|
||||
@ -28,5 +29,26 @@ return [
|
||||
'host' => $container->get('REDIS_HOST'),
|
||||
'port' => $container->get('REDIS_PORT')
|
||||
]);
|
||||
},
|
||||
Incoviba\Service\Cartola::class => function(ContainerInterface $container) {
|
||||
return (new Incoviba\Service\Cartola(
|
||||
$container->get(Psr\Http\Message\StreamFactoryInterface::class),
|
||||
$container->get(Incoviba\Common\Define\Contabilidad\Exporter::class)
|
||||
))->register('security', $container->get(Incoviba\Service\Cartola\Security::class));
|
||||
},
|
||||
Incoviba\Common\Define\Contabilidad\Exporter::class => function(ContainerInterface $container) {
|
||||
return $container->get(Incoviba\Service\Contabilidad\Exporter\Nubox::class);
|
||||
},
|
||||
Incoviba\Service\Contabilidad\Exporter\Nubox::class => function(ContainerInterface $container) {
|
||||
return new Incoviba\Service\Contabilidad\Exporter\Nubox($container->get(Incoviba\Repository\CentroCosto::class),
|
||||
$container->get('folders')->get('uploads'));
|
||||
},
|
||||
Incoviba\Service\Contabilidad\Nubox::class => function(ContainerInterface $container) {
|
||||
return new Incoviba\Service\Contabilidad\Nubox(
|
||||
$container->get(Incoviba\Repository\Nubox::class),
|
||||
$container->get(Incoviba\Service\Redis::class),
|
||||
new GuzzleHttp\Client(),
|
||||
$container->get(Psr\Http\Message\RequestFactoryInterface::class),
|
||||
$container->get('nubox')->get('url'));
|
||||
}
|
||||
];
|
||||
|
52
app/src/Controller/API/Contabilidad.php
Normal file
52
app/src/Controller/API/Contabilidad.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
namespace Incoviba\Controller\API;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Service;
|
||||
|
||||
class Contabilidad
|
||||
{
|
||||
use withJson;
|
||||
|
||||
public function procesarCartola(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Repository\Inmobiliaria $inmobiliariaRepository,
|
||||
Repository\Banco $bancoRepository,
|
||||
Service\Cartola $cartolaService): ResponseInterface
|
||||
{
|
||||
$body = $request->getParsedBody();
|
||||
$output = [
|
||||
'input' => $body,
|
||||
'movimientos' => []
|
||||
];
|
||||
try {
|
||||
$inmobiliaria = $inmobiliariaRepository->fetchById($body['inmobiliaria']);
|
||||
$banco = $bancoRepository->fetchById($body['banco']);
|
||||
$mes = new DateTimeImmutable($body['mes']);
|
||||
$file = $request->getUploadedFiles()['file'];
|
||||
$output['movimientos'] = $cartolaService->process($inmobiliaria, $banco, $mes, $file);
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function exportarCartola(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Repository\Inmobiliaria $inmobiliariaRepository,
|
||||
Repository\Banco $bancoRepository,
|
||||
Service\Cartola $cartolaService): ResponseInterface
|
||||
{
|
||||
$body = $request->getParsedBody();
|
||||
$output = [
|
||||
'input' => $body,
|
||||
'filename' => ''
|
||||
];
|
||||
try {
|
||||
$inmobiliaria = $inmobiliariaRepository->fetchById($body['inmobiliaria']);
|
||||
$banco = $bancoRepository->fetchById($body['banco']);
|
||||
$mes = new DateTimeImmutable($body['mes']);
|
||||
$output['filename'] = $cartolaService->export($inmobiliaria, $banco, $mes, json_decode($body['movimientos']));
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
25
app/src/Controller/API/Informes.php
Normal file
25
app/src/Controller/API/Informes.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
namespace Incoviba\Controller\API;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Incoviba\Service;
|
||||
|
||||
class Informes
|
||||
{
|
||||
use withJson;
|
||||
|
||||
public function diario(ServerRequestInterface $request, ResponseInterface $response, Service\Informes\Diario $diarioService): ResponseInterface
|
||||
{
|
||||
$body = $request->getParsedBody();
|
||||
$output = [
|
||||
'input' => $body,
|
||||
'data' => []
|
||||
];
|
||||
try {
|
||||
$file = $request->getUploadedFiles()['file'];
|
||||
$output['data'] = $diarioService->process($file);
|
||||
} catch (\Error) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
48
app/src/Controller/API/Inmobiliarias.php
Normal file
48
app/src/Controller/API/Inmobiliarias.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
namespace Incoviba\Controller\API;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Service;
|
||||
|
||||
class Inmobiliarias
|
||||
{
|
||||
use withJson;
|
||||
|
||||
public function cuentas(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Repository\Inmobiliaria $inmobiliariaRepository,
|
||||
Repository\Inmobiliaria\Cuenta $cuentaRepository, int $inmobiliaria_rut): ResponseInterface
|
||||
{
|
||||
$output = [
|
||||
'inmobiliaria_rut' => $inmobiliaria_rut,
|
||||
'cuentas' => []
|
||||
];
|
||||
try {
|
||||
$inmobiliaria = $inmobiliariaRepository->fetchById($inmobiliaria_rut);
|
||||
$output['cuentas'] = $cuentaRepository->fetchByInmobiliaria($inmobiliaria->rut);
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function proyectos(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Repository\Inmobiliaria $inmobiliariaRepository,
|
||||
Service\Proyecto $proyectoService, int $inmobiliaria_rut): ResponseInterface
|
||||
{
|
||||
$output = [
|
||||
'inmobiliaria_rut' => $inmobiliaria_rut,
|
||||
'proyectos' => []
|
||||
];
|
||||
try {
|
||||
$inmobiliaria = $inmobiliariaRepository->fetchById($inmobiliaria_rut);
|
||||
$output['proyectos'] = array_map(function(Model\Proyecto $proyecto) {
|
||||
$p = json_decode(json_encode($proyecto));
|
||||
$p->current_estado = $proyecto->currentEstado();
|
||||
$p->estados = $proyecto->estados();
|
||||
return $p;
|
||||
},$proyectoService->getByInmobiliaria($inmobiliaria->rut));
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
90
app/src/Controller/API/Nubox.php
Normal file
90
app/src/Controller/API/Nubox.php
Normal file
@ -0,0 +1,90 @@
|
||||
<?php
|
||||
namespace Incoviba\Controller\API;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Incoviba\Common\Implement\Exception\HttpResponse;
|
||||
use Incoviba\Service;
|
||||
|
||||
class Nubox
|
||||
{
|
||||
use withJson;
|
||||
|
||||
public function token(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Service\Contabilidad\Nubox $nuboxService, int $inmobiliaria_rut): ResponseInterface
|
||||
{
|
||||
$output = [
|
||||
'inmobiliaria_rut' => $inmobiliaria_rut,
|
||||
'token' => ''
|
||||
];
|
||||
try {
|
||||
$output['token'] = $nuboxService->getToken($inmobiliaria_rut);
|
||||
} catch (HttpResponse $exception) {
|
||||
$output['error'] = [
|
||||
'code' => $exception->getCode(),
|
||||
'message' => $exception->getMessage()
|
||||
];
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function sistemas(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Service\Contabilidad\Nubox $nuboxService, int $inmobiliaria_rut): ResponseInterface
|
||||
{
|
||||
$output = [
|
||||
'inmobiliaria_rut' => $inmobiliaria_rut,
|
||||
'sistemas' => []
|
||||
];
|
||||
try {
|
||||
$output['sistemas'] = $nuboxService->getSistemas($inmobiliaria_rut);
|
||||
} catch (HttpResponse $exception) {
|
||||
$output['error'] = [
|
||||
'code' => $exception->getCode(),
|
||||
'message' => $exception->getMessage()
|
||||
];
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function libroMayor(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Service\Contabilidad\Nubox $nuboxService, int $inmobiliaria_rut): ResponseInterface
|
||||
{
|
||||
$body = $request->getParsedBody();
|
||||
$output = [
|
||||
'inmobiliaria_rut' => $inmobiliaria_rut,
|
||||
'input' => $body,
|
||||
'libro_mayor' => []
|
||||
];
|
||||
try {
|
||||
$from = new DateTimeImmutable($body['inicio']);
|
||||
$to = new DateTimeImmutable($body['termino']);
|
||||
$output['libro_mayor'] = $nuboxService->getLibroMayor($inmobiliaria_rut, $from, $to);
|
||||
} catch (HttpResponse $exception) {
|
||||
$output['error'] = [
|
||||
'code' => $exception->getCode(),
|
||||
'message' => $exception->getMessage()
|
||||
];
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function libroDiario(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Service\Contabilidad\Nubox $nuboxService, int $inmobiliaria_rut): ResponseInterface
|
||||
{
|
||||
$body = $request->getParsedBody();
|
||||
$output = [
|
||||
'inmobiliaria_rut' => $inmobiliaria_rut,
|
||||
'input' => $body,
|
||||
'libro_diario' => []
|
||||
];
|
||||
try {
|
||||
$from = new DateTimeImmutable($body['inicio']);
|
||||
$to = new DateTimeImmutable($body['termino']);
|
||||
$output['libro_diario'] = $nuboxService->getLibroDiario($inmobiliaria_rut, $from, $to);
|
||||
} catch (HttpResponse $exception) {
|
||||
$output['error'] = [
|
||||
'code' => $exception->getCode(),
|
||||
'message' => $exception->getMessage()
|
||||
];
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
@ -155,4 +155,19 @@ class Proyectos
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function tiposUnidades(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Repository\Proyecto $proyectoRepository,
|
||||
Repository\Proyecto\TipoUnidad $tipoUnidadRepository,
|
||||
int $proyecto_id): ResponseInterface
|
||||
{
|
||||
$output = [
|
||||
'proyecto_id' => $proyecto_id,
|
||||
'tipos' => []
|
||||
];
|
||||
try {
|
||||
$proyecto = $proyectoRepository->fetchById($proyecto_id);
|
||||
$output['tipos'] = $tipoUnidadRepository->fetchByProyecto($proyecto->id);
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
||||
|
@ -21,4 +21,12 @@ class CentrosCostos
|
||||
return $view->render($response, 'contabilidad.centros_costos', compact('centrosCostos',
|
||||
'tiposCentros', 'categorias', 'tiposCuentas'));
|
||||
}
|
||||
public function asignar(ServerRequestInterface $request, ResponseInterface $response, View $view,
|
||||
Repository\CentroCosto $centroCostoRepository,
|
||||
Repository\Inmobiliaria $inmobiliariaRepository): ResponseInterface
|
||||
{
|
||||
$centrosCostos = $centroCostoRepository->fetchAll();
|
||||
$inmobiliarias = $inmobiliariaRepository->fetchAllActive('razon');
|
||||
return $view->render($response, 'contabilidad.centros_costos.asignar', compact('centrosCostos', 'inmobiliarias'));
|
||||
}
|
||||
}
|
||||
|
18
app/src/Model/Nubox.php
Normal file
18
app/src/Model/Nubox.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
namespace Incoviba\Model;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Ideal;
|
||||
|
||||
class Nubox extends Ideal\Model
|
||||
{
|
||||
public Inmobiliaria $inmobiliaria;
|
||||
public string $alias;
|
||||
public string $usuario;
|
||||
public string $password;
|
||||
|
||||
public function getLogin(): string
|
||||
{
|
||||
return base64_encode(implode(':', [$this->usuario, $this->password]));
|
||||
}
|
||||
}
|
@ -46,4 +46,20 @@ class Inmobiliaria extends Ideal\Repository
|
||||
{
|
||||
return $this->update($model, ['dv', 'razon', 'abreviacion', 'cuenta', 'banco', 'sociedad'], $new_data);
|
||||
}
|
||||
|
||||
public function fetchAllActive(null|string|array $sorting = null): array
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select('a.*')
|
||||
->from("{$this->getTable()} a")
|
||||
->joined('JOIN proyecto ON a.rut = proyecto.inmobiliaria')
|
||||
->joined('JOIN (SELECT ep1.* FROM estado_proyecto ep1 JOIN (SELECT MAX(id) AS id, proyecto FROM estado_proyecto GROUP BY proyecto) ep0 ON ep0.id = ep1.id) ep ON ep.proyecto = proyecto.id')
|
||||
->joined('JOIN tipo_estado_proyecto tep ON tep.id = ep.estado')
|
||||
->joined('JOIN etapa_proyecto ON etapa_proyecto.id = tep.etapa')
|
||||
->where('etapa_proyecto.orden BETWEEN ? AND ?');
|
||||
if ($sorting !== null) {
|
||||
$query->order($sorting);
|
||||
}
|
||||
return $this->fetchMany($query, [1, 8]);
|
||||
}
|
||||
}
|
||||
|
52
app/src/Repository/Inmobiliaria/Cuenta.php
Normal file
52
app/src/Repository/Inmobiliaria/Cuenta.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
namespace Incoviba\Repository\Inmobiliaria;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Common\Implement;
|
||||
|
||||
class Cuenta extends Ideal\Repository
|
||||
{
|
||||
public function __construct(Define\Connection $connection,
|
||||
protected Repository\Inmobiliaria $inmobiliariaRepository,
|
||||
protected Repository\Banco $bancoRepository)
|
||||
{
|
||||
parent::__construct($connection);
|
||||
$this->setTable('cuenta');
|
||||
}
|
||||
|
||||
public function create(?array $data = null): Model\Inmobiliaria\Cuenta
|
||||
{
|
||||
$map = (new Implement\Repository\MapperParser(['cuenta']))
|
||||
->register('inmobiliaria', (new Implement\Repository\Mapper())
|
||||
->setFunction(function(array $data) {
|
||||
return $this->inmobiliariaRepository->fetchById($data['inmobiliaria']);
|
||||
}))
|
||||
->register('banco', (new Implement\Repository\Mapper())
|
||||
->setFunction(function(array $data) {
|
||||
return $this->bancoRepository->fetchById($data['banco']);
|
||||
}));
|
||||
return $this->parseData(new Model\Inmobiliaria\Cuenta(), $data, $map);
|
||||
}
|
||||
public function save(Define\Model $model): Model\Inmobiliaria\Cuenta
|
||||
{
|
||||
$model->id = $this->saveNew(['inmobiliaria', 'banco', 'cuenta'],
|
||||
[$model->inmobiliaria->rut, $model->banco->id, $model->cuenta]);
|
||||
return $model;
|
||||
}
|
||||
public function edit(Define\Model $model, array $new_data): Model\Inmobiliaria\Cuenta
|
||||
{
|
||||
return $this->update($model, ['inmobiliaria', 'banco', 'cuenta'], $new_data);
|
||||
}
|
||||
|
||||
public function fetchByInmobiliaria(int $inmobiliaria_rut): array
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where('inmobiliaria = ?');
|
||||
return $this->fetchMany($query, [$inmobiliaria_rut]);
|
||||
}
|
||||
}
|
55
app/src/Repository/Nubox.php
Normal file
55
app/src/Repository/Nubox.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
namespace Incoviba\Repository;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Implement;
|
||||
use Incoviba\Model;
|
||||
|
||||
class Nubox extends Ideal\Repository
|
||||
{
|
||||
public function __construct(Define\Connection $connection, protected Inmobiliaria $inmobiliariaRepository)
|
||||
{
|
||||
parent::__construct($connection);
|
||||
$this->setTable('inmobiliarias_nubox');
|
||||
}
|
||||
|
||||
public function create(?array $data = null): Model\Nubox
|
||||
{
|
||||
$map = (new Implement\Repository\MapperParser(['usuario', 'alias']))
|
||||
->register('inmobiliaria_rut', (new Implement\Repository\Mapper())
|
||||
->setProperty('inmobiliaria')
|
||||
->setFunction(function(array $data) {
|
||||
return $this->inmobiliariaRepository->fetchById($data['inmobiliaria_rut']);
|
||||
}))
|
||||
->register('contraseña', (new Implement\Repository\Mapper())
|
||||
->setProperty('password'));
|
||||
return $this->parseData(new Model\Nubox(), $data, $map);
|
||||
}
|
||||
public function save(Define\Model $model): Model\Nubox
|
||||
{
|
||||
$this->saveNew(
|
||||
['inmobiliaria_rut', 'alias', 'usuario', 'contraseña'],
|
||||
[$model->inmobiliaria->rut, $model->alias, $model->usuario, $model->password]
|
||||
);
|
||||
return $model;
|
||||
}
|
||||
public function edit(Define\Model $model, array $new_data): Model\Nubox
|
||||
{
|
||||
return $this->update($model, ['inmobiliaria_rut', 'alias', 'usuario', 'contraseña'], $new_data);
|
||||
}
|
||||
|
||||
public function fetchByInmobiliaria(int $inmobiliaria_rut): Model\Nubox
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where('inmobiliaria_rut = ?');
|
||||
return $this->fetchOne($query, [$inmobiliaria_rut]);
|
||||
}
|
||||
|
||||
protected function getKey(): string
|
||||
{
|
||||
return 'inmobiliaria_rut';
|
||||
}
|
||||
}
|
29
app/src/Service/Cartola.php
Normal file
29
app/src/Service/Cartola.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
namespace Incoviba\Service;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Psr\Http\Message\StreamFactoryInterface;
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
use Incoviba\Common\Define\Cartola\Banco;
|
||||
use Incoviba\Common\Define\Contabilidad\Exporter;
|
||||
use Incoviba\Model;
|
||||
|
||||
class Cartola
|
||||
{
|
||||
public function __construct(protected StreamFactoryInterface $streamFactory, protected Exporter $exporter) {}
|
||||
|
||||
protected array $bancos;
|
||||
public function register(string $name, Banco $banco): Cartola
|
||||
{
|
||||
$this->bancos[$name] = $banco;
|
||||
return $this;
|
||||
}
|
||||
public function process(Model\Inmobiliaria $inmobiliaria, Model\Banco $banco, DateTimeInterface $mes, UploadedFileInterface $file): array
|
||||
{
|
||||
return $this->bancos[strtolower($banco->nombre)]->process($file);
|
||||
}
|
||||
public function export(Model\Inmobiliaria $inmobiliaria, Model\Banco $banco, DateTimeInterface $mes, array $movimientos): string
|
||||
{
|
||||
return $this->exporter->export($inmobiliaria, $banco, $mes, $movimientos);
|
||||
}
|
||||
}
|
54
app/src/Service/Cartola/Security.php
Normal file
54
app/src/Service/Cartola/Security.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Cartola;
|
||||
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
use PhpOffice\PhpSpreadsheet;
|
||||
use Incoviba\Common\Define\Cartola\Banco;
|
||||
|
||||
class Security implements Banco
|
||||
{
|
||||
public function process(UploadedFileInterface $file): array
|
||||
{
|
||||
$reader = PhpSpreadsheet\IOFactory::createReader('Xls');
|
||||
$filename = '/tmp/cartola.xls';
|
||||
$file->moveTo($filename);
|
||||
$xlsx = $reader->load($filename);
|
||||
$worksheet = $xlsx->getActiveSheet();
|
||||
$rows = $worksheet->getRowIterator();
|
||||
$dataFound = false;
|
||||
$columns = [];
|
||||
$data = [];
|
||||
foreach ($rows as $row) {
|
||||
$cells = $row->getCellIterator();
|
||||
$rowData = [];
|
||||
foreach ($cells as $cell) {
|
||||
if ($cell->getColumn() === 'A' and $cell->getCalculatedValue() === "fecha ") {
|
||||
$cols = $row->getColumnIterator();
|
||||
foreach ($cols as $col) {
|
||||
$columns[$col->getColumn()] = trim($col->getCalculatedValue());
|
||||
}
|
||||
$dataFound = true;
|
||||
break;
|
||||
}
|
||||
if ($cell->getColumn() === 'A' and $cell->getCalculatedValue() === null) {
|
||||
$dataFound = false;
|
||||
break;
|
||||
}
|
||||
if (!$dataFound) {
|
||||
break;
|
||||
}
|
||||
$col = $columns[$cell->getColumn()];
|
||||
$value = $cell->getCalculatedValue();
|
||||
if ($col === 'fecha') {
|
||||
$value = PhpSpreadsheet\Shared\Date::excelToDateTimeObject($cell->getValue(), 'America/Santiago')->format('Y-m-d');
|
||||
}
|
||||
$rowData[$col] = $value;
|
||||
}
|
||||
if (count($rowData) > 0) {
|
||||
$data []= $rowData;
|
||||
}
|
||||
}
|
||||
unlink($filename);
|
||||
return $data;
|
||||
}
|
||||
}
|
115
app/src/Service/Contabilidad/Exporter/Nubox.php
Normal file
115
app/src/Service/Contabilidad/Exporter/Nubox.php
Normal file
@ -0,0 +1,115 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Contabilidad\Exporter;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
use Incoviba\Common\Define\Contabilidad\Exporter;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
use PhpOffice\PhpSpreadsheet;
|
||||
|
||||
class Nubox implements Exporter
|
||||
{
|
||||
public function __construct(protected Repository\CentroCosto $centroCostoRepository, protected string $uploadFolder) {}
|
||||
|
||||
public function export(Model\Inmobiliaria $inmobiliaria, Model\Banco $banco, DateTimeInterface $mes, array $movimientos): string
|
||||
{
|
||||
PhpSpreadsheet\Settings::setLocale('es-CL');
|
||||
$workbook = new PhpSpreadsheet\Spreadsheet();
|
||||
$sheet = $workbook->getActiveSheet();
|
||||
|
||||
$rowIndex = $this->buildHeaders($sheet);
|
||||
|
||||
foreach ($movimientos as $movimiento) {
|
||||
$tipoCentro = '';
|
||||
$cuenta = '';
|
||||
$centro = '';
|
||||
if ($movimiento->centro_costo !== '') {
|
||||
$centroCosto = $this->centroCostoRepository->fetchById($movimiento->centro_costo);
|
||||
$tipoCentro = substr($centroCosto->tipoCentro->descripcion, 0, 1);
|
||||
$cuenta = $centroCosto->cuentaContable;
|
||||
$centro = $centroCosto->id;
|
||||
}
|
||||
$fecha = (new DateTimeImmutable($movimiento->fecha))->format('d/m/Y');
|
||||
$rowIndex = $this->add($sheet, [
|
||||
'Número' => '0',
|
||||
'Tipo' => $tipoCentro,
|
||||
'Fecha' => $fecha,
|
||||
'Glosa' => $movimiento->detalle,
|
||||
'Cuenta Detalle' => $cuenta,
|
||||
'Glosa Detalle' => '',
|
||||
'Centro Costo' => $centro,
|
||||
'Sucursal' => '',
|
||||
'Debe' => $movimiento->abono === 0 ? '' : $movimiento->abono,
|
||||
'Haber' => $movimiento->cargo === 0 ? '' : $movimiento->cargo,
|
||||
'Tipo Auxiliar' => 'B',
|
||||
'A: Rut Cliente-Proveedor/H: Rut Prestador' => '',
|
||||
'A: Razon Social/B: Descripción Movimiento Bancario/ H: Nombre Prestador' => $movimiento->glosa,
|
||||
'A: Tipo De Documento/H: Tipo De Boleta Honorario' => '',
|
||||
'A: Folio /B: Numero Documento/H: Folio Boleta' => $movimiento->documento,
|
||||
'A/B/H: Monto' => ($movimiento->abono === 0) ? $movimiento->cargo : $movimiento->abono,
|
||||
'A: Fecha Vencimiento /B: Fecha /H: Fecha Emisión (DD/MM/AAAA)' => $fecha
|
||||
], $rowIndex);
|
||||
}
|
||||
$sheet->getStyle("I1:J{$rowIndex}")->getNumberFormat()
|
||||
->setFormatCode('#,##0');
|
||||
$sheet->getStyle("O1:O{$rowIndex}")->getNumberFormat()
|
||||
->setFormatCode('##0');
|
||||
$sheet->getStyle("P1:P{$rowIndex}")->getNumberFormat()
|
||||
->setFormatCode('#,##0');
|
||||
foreach (range('A', 'Q') as $col) {
|
||||
$sheet->getColumnDimension($col)->setAutoSize(true);
|
||||
}
|
||||
$sheet->getSheetView()->setZoomScale(90);
|
||||
|
||||
$writer = PhpSpreadsheet\IOFactory::createWriter($workbook, 'Xlsx');
|
||||
$filename = "Cartola {$banco->nombre} - {$inmobiliaria->abreviacion} - {$mes->format('M Y')}.xlsx";
|
||||
$writer->save(implode(DIRECTORY_SEPARATOR, [
|
||||
$this->uploadFolder,
|
||||
$filename
|
||||
]));
|
||||
return $filename;
|
||||
}
|
||||
|
||||
protected function getHeaders(): array
|
||||
{
|
||||
return [
|
||||
'Número',
|
||||
'Tipo',
|
||||
'Fecha',
|
||||
'Glosa',
|
||||
'Cuenta Detalle',
|
||||
'Glosa Detalle',
|
||||
'Centro Costo',
|
||||
'Sucursal',
|
||||
'Debe',
|
||||
'Haber',
|
||||
'Tipo Auxiliar',
|
||||
'A: Rut Cliente-Proveedor/H: Rut Prestador',
|
||||
'A: Razon Social/B: Descripción Movimiento Bancario/ H: Nombre Prestador',
|
||||
'A: Tipo De Documento/H: Tipo De Boleta Honorario',
|
||||
'A: Folio /B: Numero Documento/H: Folio Boleta',
|
||||
'A/B/H: Monto',
|
||||
'A: Fecha Vencimiento /B: Fecha /H: Fecha Emisión (DD/MM/AAAA)'
|
||||
];
|
||||
}
|
||||
protected function buildHeaders(PhpSpreadsheet\Worksheet\Worksheet &$sheet, int $rowIndex = 1): int
|
||||
{
|
||||
$map = $this->getHeaders();
|
||||
foreach ($map as $index => $header) {
|
||||
$columnIndex = $index + 1;
|
||||
$sheet->getCell([$columnIndex, $rowIndex])->setValue($header);
|
||||
}
|
||||
return $rowIndex + 1;
|
||||
}
|
||||
protected function add(PhpSpreadsheet\Worksheet\Worksheet &$sheet, array $row, int $rowIndex): int
|
||||
{
|
||||
$headers = $this->getHeaders();
|
||||
foreach ($headers as $index => $header) {
|
||||
$columnIndex = $index + 1;
|
||||
$sheet->getCell([$columnIndex, $rowIndex])->setValue($row[$header]);
|
||||
$sheet->getCell([$columnIndex, $rowIndex + 1])->setValue(($header === 'Tipo Auxiliar') ? 'A' : '');
|
||||
}
|
||||
return $rowIndex + 2;
|
||||
}
|
||||
}
|
139
app/src/Service/Contabilidad/Nubox.php
Normal file
139
app/src/Service/Contabilidad/Nubox.php
Normal file
@ -0,0 +1,139 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Contabilidad;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
use Psr\Http\Message\RequestFactoryInterface;
|
||||
use Incoviba\Common\Implement\Exception;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Service;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
use function Symfony\Component\Translation\t;
|
||||
|
||||
class Nubox
|
||||
{
|
||||
public function __construct(protected Repository\Nubox $nuboxRepository,
|
||||
protected Service\Redis $redisService,
|
||||
protected ClientInterface $client,
|
||||
protected RequestFactoryInterface $requestFactory,
|
||||
protected string $api_url) {}
|
||||
|
||||
protected array $tokens;
|
||||
public function getToken(int $inmobiliaria_rut): string
|
||||
{
|
||||
if (!isset($this->tokens[$inmobiliaria_rut])) {
|
||||
$redisKey = "token_nubox:{$inmobiliaria_rut}";
|
||||
try {
|
||||
$this->tokens[$inmobiliaria_rut] = $this->redisService->get($redisKey);
|
||||
} catch (Exception\EmptyRedis) {
|
||||
$nubox = $this->nuboxRepository->fetchByInmobiliaria($inmobiliaria_rut);
|
||||
$request = $this->requestFactory
|
||||
->createRequest('POST', implode('/', [$this->api_url, 'autenticar']))
|
||||
->withHeader('Authorization', "Basic {$nubox->getLogin()}")
|
||||
->withHeader('Content-Type', 'application/json')
|
||||
->withHeader('Accept', 'application/json');
|
||||
$response = $this->client->sendRequest($request);
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception\HttpResponse($response->getReasonPhrase(), $response->getStatusCode());
|
||||
}
|
||||
$sistemas = json_decode($response->getBody()->getContents(), JSON_OBJECT_AS_ARRAY);
|
||||
|
||||
$this->setToken($inmobiliaria_rut, $response->getHeaderLine('Token'))
|
||||
->setSistemas($inmobiliaria_rut, $sistemas['Sistemas']);
|
||||
}
|
||||
}
|
||||
return $this->tokens[$inmobiliaria_rut];
|
||||
}
|
||||
public function setToken(int $inmobiliaria_rut, string $token): Nubox
|
||||
{
|
||||
$this->tokens[$inmobiliaria_rut] = $token;
|
||||
|
||||
$redisKey = "token_nubox:{$inmobiliaria_rut}";
|
||||
$this->redisService->set($redisKey, $this->tokens[$inmobiliaria_rut], 60 * 15);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected array $sistemas;
|
||||
public function getSistemas(int $inmobiliaria_rut): array
|
||||
{
|
||||
if (!isset($this->sistemas[$inmobiliaria_rut])) {
|
||||
$redisKey = "nubox:{$inmobiliaria_rut}";
|
||||
$this->sistemas[$inmobiliaria_rut] = json_decode($this->redisService->get($redisKey));
|
||||
}
|
||||
return $this->sistemas[$inmobiliaria_rut];
|
||||
}
|
||||
public function setSistemas(int $inmobiliaria_rut, array $sistemas): Nubox
|
||||
{
|
||||
$this->sistemas[$inmobiliaria_rut] = $sistemas;
|
||||
|
||||
$redisKey = "nubox:{$inmobiliaria_rut}";
|
||||
$this->redisService->set($redisKey, json_encode($sistemas));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getLibroMayor(int $inmobiliaria_rut, DateTimeInterface $from, DateTimeInterface $to): array
|
||||
{
|
||||
$inmobiliaria = $this->nuboxRepository->fetchByInmobiliaria($inmobiliaria_rut);
|
||||
$query = [
|
||||
'NumeroSerie' => 1,
|
||||
'CodigoEmpresa' => $inmobiliaria->alias,
|
||||
'DDInicio' => $from->format('j'),
|
||||
'MMInicio' => $from->format('n'),
|
||||
'YYInicio' => $from->format('Y'),
|
||||
'DDTermino' => $to->format('j'),
|
||||
'MMTermino' => $to->format('n'),
|
||||
'YYTermino' => $to->format('Y'),
|
||||
'CodigoCentroDeCosto' => 0,
|
||||
'CodigoSucursal' => 0,
|
||||
'CodigoCuenta' => 0,
|
||||
'ModoIFRS' => 'false',
|
||||
'CuentasConSaldo' => 'false',
|
||||
'IncluirCodigoCuenta' => 'true',
|
||||
];
|
||||
$uri = 'contabilidad/libro-mayor?' . http_build_query($query);
|
||||
$response = $this->send($uri, $inmobiliaria_rut);
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception\HttpResponse($response->getReasonPhrase(), $response->getStatusCode());
|
||||
}
|
||||
return json_decode($response->getBody()->getContents(), JSON_OBJECT_AS_ARRAY);
|
||||
}
|
||||
public function getLibroDiario(int $inmobiliaria_rut, DateTimeInterface $from, DateTimeInterface $to): array
|
||||
{
|
||||
$inmobiliaria = $this->nuboxRepository->fetchByInmobiliaria($inmobiliaria_rut);
|
||||
$query = [
|
||||
'ModoIFRS' => 0,
|
||||
'DDInicio' => $from->format('j'),
|
||||
'MMInicio' => $from->format('n'),
|
||||
'YYInicio' => $from->format('Y'),
|
||||
'DDTermino' => $to->format('j'),
|
||||
'MMTermino' => $to->format('n'),
|
||||
'YYTermino' => $to->format('Y'),
|
||||
'Sucursal' => 0,
|
||||
'codigoEmpresa' => $inmobiliaria->alias,
|
||||
'NumeroSerie' => 1,
|
||||
'CuentasConSaldo' => 0,
|
||||
'incluirCodigoCuenta' => 1
|
||||
];
|
||||
$uri = 'contabilidad/libro-diario?' . http_build_query($query);
|
||||
$response = $this->send($uri, $inmobiliaria_rut);
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
error_log(var_export($uri,true).PHP_EOL,3,'/logs/debug');
|
||||
throw new Exception\HttpResponse($response->getReasonPhrase(), $response->getStatusCode());
|
||||
}
|
||||
return json_decode($response->getBody()->getContents(), JSON_OBJECT_AS_ARRAY);
|
||||
}
|
||||
|
||||
private function send(string $uri, int $inmobiliaria_rut, string $method = 'GET', ?StreamInterface $body = null): ResponseInterface
|
||||
{
|
||||
$request = $this->requestFactory
|
||||
->createRequest($method, implode('/', [$this->api_url, $uri]))
|
||||
->withHeader('token', $this->getToken($inmobiliaria_rut));
|
||||
if ($body !== null) {
|
||||
$request = $request->withBody($body);
|
||||
}
|
||||
return $this->client->sendRequest($request);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user