FIX: Importar cartolas correctamente.
This commit is contained in:
@ -30,6 +30,7 @@
|
||||
<table class="ui celled table" id="movimientos" style="display: none;">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Sigla</th>
|
||||
<th>Fecha</th>
|
||||
<th>Glosa</th>
|
||||
<th class="right aligned">Cargo</th>
|
||||
@ -65,21 +66,25 @@
|
||||
get banco() {
|
||||
return $(`#${this.ids.banco}${this.props.index}`).dropdown('get value')
|
||||
}
|
||||
get mes() {
|
||||
return $(`#${this.ids.mes}${this.props.index}`).calendar('get date')
|
||||
}
|
||||
get file() {
|
||||
return $(`#archivo${this.props.index}`)[0].files[0]
|
||||
}
|
||||
get data() {
|
||||
return {
|
||||
sociedad_rut: this.sociedad,
|
||||
banco_id: this.banco,
|
||||
cuenta_id: this.banco,
|
||||
mes: [this.mes.getFullYear(), this.mes.getMonth() + 1, 1].join('-'),
|
||||
file: this.file,
|
||||
index: this.props.index
|
||||
}
|
||||
}
|
||||
draw({sociedades, bancos}) {
|
||||
draw({sociedades}) {
|
||||
const output = [
|
||||
`<div class="fields" data-idx="${this.props.index}">`,
|
||||
'<div class="six wide field">',
|
||||
'<div class="five wide field">',
|
||||
'<label>Sociedad</label>',
|
||||
`<div class="ui search selection dropdown" id="${this.ids.sociedad}${this.props.index}">`,
|
||||
`<input type="hidden" name="sociedad_rut${this.props.index}" />`,
|
||||
@ -94,30 +99,30 @@
|
||||
'</div>',
|
||||
'</div>',
|
||||
'</div>',
|
||||
'<div class="two wide field">',
|
||||
'<div class="four wide field">',
|
||||
'<label>Banco</label>',
|
||||
`<div class="ui search selection dropdown" id="${this.ids.banco}${this.props.index}">`,
|
||||
`<input type="hidden" name="banco_id${this.props.index}" />`,
|
||||
'<i class="dropdown icon"></i>',
|
||||
'<div class="default text">Banco</div>',
|
||||
'<div class="menu">',
|
||||
...bancos.map(banco => {
|
||||
return [
|
||||
`<div class="item" data-value="${banco.id}">${banco.nombre}</div>`
|
||||
].join("\n")
|
||||
}),
|
||||
'<div class="menu"></div>',
|
||||
'</div>',
|
||||
'</div>',
|
||||
'<div class="field">',
|
||||
'<label>Mes</label>',
|
||||
`<div class="ui calendar" id="${this.ids.mes}${this.props.index}">`,
|
||||
'<div class="ui input left icon">',
|
||||
'<i class="calendar icon"></i>',
|
||||
`<input type="text" name="mes${this.props.index}" placeholder="Mes" />`,
|
||||
'</div>',
|
||||
'</div>',
|
||||
'</div>',
|
||||
'<div class="six wide field">',
|
||||
'<div class="field">',
|
||||
'<label>Archivo</label>',
|
||||
'<div class="ui file action input">',
|
||||
`<input type="file" name="archivo${this.props.index}" id="archivo${this.props.index}" />`,
|
||||
'<div class="ui icon button" id="archivo_button">',
|
||||
'<i class="file icon"></i>',
|
||||
'Seleccionar',
|
||||
'</div>',
|
||||
'</div>',
|
||||
`<input type="file" class="ui invisible file input" name="archivo${this.props.index}" id="archivo${this.props.index}" />`,
|
||||
`<label for="archivo${this.props.index}" class="ui icon button" id="archivo_button">`,
|
||||
'<i class="file icon"></i>',
|
||||
'</label>',
|
||||
'</div>',
|
||||
]
|
||||
if (this.props.index > 1) {
|
||||
@ -134,8 +139,31 @@
|
||||
return output.join("\n")
|
||||
}
|
||||
activate() {
|
||||
$(`#${this.ids.sociedad}${this.props.index}`).dropdown()
|
||||
$(`#${this.ids.sociedad}${this.props.index}`).dropdown({
|
||||
onChange: (value, text, $choice) => {
|
||||
cartolas.fetch().bancos(value).then(response => {
|
||||
if (!response) {
|
||||
return
|
||||
}
|
||||
return response.json().then(data => {
|
||||
const dropdown = $(`#${this.ids.banco}${this.props.index}`)
|
||||
dropdown.dropdown('clear')
|
||||
dropdown.dropdown('change values', data.cuentas.map(cuenta => {
|
||||
const desc = [cuenta.banco.nombre, cuenta.cuenta].join(' - ')
|
||||
return {
|
||||
name: desc,
|
||||
value: cuenta.id,
|
||||
text: desc
|
||||
}
|
||||
}))
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
$(`#${this.ids.banco}${this.props.index}`).dropdown()
|
||||
const cdo = structuredClone(calendar_date_options)
|
||||
cdo.type = 'month'
|
||||
$(`#${this.ids.mes}${this.props.index}`).calendar(cdo)
|
||||
if (this.props.index > 1) {
|
||||
$(`.${this.ids.buttons.remove}[data-idx="${this.props.index}"]`).click(clickEvent => {
|
||||
const index = $(clickEvent.currentTarget).data('idx')
|
||||
@ -151,8 +179,9 @@
|
||||
}
|
||||
class Movimiento {
|
||||
props
|
||||
constructor({fecha, glosa, cargo, abono, saldo, categoria, detalle, centro_costo, rut, nombres, identificador}) {
|
||||
constructor({sociedad, fecha, glosa, cargo, abono, saldo, categoria, detalle, centro_costo, rut, nombres, identificador}) {
|
||||
this.props = {
|
||||
sociedad,
|
||||
fecha,
|
||||
glosa,
|
||||
cargo,
|
||||
@ -170,6 +199,7 @@
|
||||
const fecha = new Date(this.props.fecha)
|
||||
return [
|
||||
'<tr>',
|
||||
`<td>${this.props.sociedad.sigla}</td>`,
|
||||
`<td>${formatters.date.format(fecha)}</td>`,
|
||||
`<td>${this.props.glosa}</td>`,
|
||||
`<td class="right aligned">${formatters.number.format(this.props.cargo ?? 0)}</td>`,
|
||||
@ -229,6 +259,14 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
fetch() {
|
||||
return {
|
||||
bancos: sociedad_rut => {
|
||||
const url = `{{$urls->api}}/inmobiliaria/${sociedad_rut}/cuentas`
|
||||
return fetchAPI(url)
|
||||
}
|
||||
}
|
||||
},
|
||||
import() {
|
||||
return {
|
||||
cartolas: () => {
|
||||
@ -237,7 +275,8 @@
|
||||
const body = new FormData()
|
||||
this.data.cartolas.forEach(cartola => {
|
||||
Object.entries(cartola.data).forEach(([key, value]) => {
|
||||
body.append(key, value)
|
||||
const name = `${key}[${cartola.props.index - 1}]`
|
||||
body.append(name, value)
|
||||
})
|
||||
})
|
||||
|
||||
@ -290,6 +329,7 @@
|
||||
cartolas: {
|
||||
sociedad: 'sociedad',
|
||||
banco: 'banco',
|
||||
mes: 'mes',
|
||||
buttons: {
|
||||
remove: 'remove'
|
||||
}
|
||||
|
@ -2,17 +2,14 @@
|
||||
namespace Incoviba\Controller\API\Contabilidad;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Incoviba\Common\Ideal\Controller;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Controller\API\withJson;
|
||||
use Incoviba\Model\Contabilidad\Banco;
|
||||
use Incoviba\Model\Inmobiliaria;
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Service;
|
||||
use PhpParser\Node\Stmt\TryCatch;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class Cartolas extends Controller
|
||||
{
|
||||
@ -33,7 +30,7 @@ class Cartolas extends Controller
|
||||
$banco = $bancoRepository->fetchById($body['banco']);
|
||||
$mes = new DateTimeImmutable($body['mes']);
|
||||
$file = $request->getUploadedFiles()['file'];
|
||||
$output['movimientos'] = $cartolaService->process($inmobiliaria, $banco, $mes, $file);
|
||||
$output['movimientos'] = $cartolaService->process($banco, $file);
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
@ -100,9 +97,6 @@ class Cartolas extends Controller
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function importar(ServerRequestInterface $request, ResponseInterface $response,
|
||||
LoggerInterface $logger,
|
||||
Repository\Inmobiliaria $inmobiliariaRepository,
|
||||
Repository\Contabilidad\Banco $bancoRepository,
|
||||
Repository\Inmobiliaria\Cuenta $cuentaRepository,
|
||||
Service\Contabilidad\Cartola $cartolaService,
|
||||
Service\Contabilidad\Movimiento $movimientoService): ResponseInterface
|
||||
@ -120,22 +114,26 @@ class Cartolas extends Controller
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
$inmobiliaria = $inmobiliariaRepository->fetchById($body['sociedad_rut'][$i]);
|
||||
$banco = $bancoRepository->fetchById($body['banco_id'][$i]);
|
||||
$movimientos = $cartolaService->process($inmobiliaria, $banco, new DateTimeImmutable($body['mes'][$i]), $file);
|
||||
$cuenta = $cuentaRepository->fetchByInmobiliariaAndBanco($inmobiliaria->rut, $banco->id);
|
||||
$cuenta = $cuentaRepository->fetchById($body['cuenta_id'][$i]);
|
||||
$movimientos = $cartolaService->process($cuenta->banco, $file);
|
||||
$this->addMovimientos($movimientoService, $cuenta, $movimientos);
|
||||
$output['movimientos'] = array_merge($output['movimientos'], $movimientos);
|
||||
$inmobiliaria = $cuenta->inmobiliaria;
|
||||
$output['movimientos'] = array_merge($output['movimientos'], array_map(function($movimiento) use ($inmobiliaria) {
|
||||
$movimiento['sociedad'] = $inmobiliaria;
|
||||
return $movimiento;
|
||||
}, $movimientos));
|
||||
} catch (EmptyResult) {}
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
$inmobiliaria = $inmobiliariaRepository->fetchById($body['sociedad_rut']);
|
||||
$banco = $bancoRepository->fetchById($body['banco_id']);
|
||||
$movimientos = $cartolaService->process($inmobiliaria, $banco, new DateTimeImmutable($body['mes']), $files['file']);
|
||||
$cuenta = $cuentaRepository->fetchByInmobiliariaAndBanco($inmobiliaria->rut, $banco->id);
|
||||
$cuenta = $cuentaRepository->fetchById($body['cuenta_id']);
|
||||
$movimientos = $cartolaService->process($cuenta->banco, $files['file']);
|
||||
$this->addMovimientos($movimientoService, $cuenta, $movimientos);
|
||||
$output['movimientos'] = $movimientos;
|
||||
$inmobiliaria = $cuenta->inmobiliaria;
|
||||
$output['movimientos'] = array_map(function($movimiento) use ($inmobiliaria) {
|
||||
$movimiento['sociedad'] = $inmobiliaria;
|
||||
return $movimiento;
|
||||
}, $movimientos);
|
||||
} catch (EmptyResult) {}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
@ -145,12 +143,14 @@ class Cartolas extends Controller
|
||||
{
|
||||
foreach ($movimientos as $dataMovimiento) {
|
||||
$dataMovimiento['cuenta_id'] = $cuenta->id;
|
||||
$dataMovimiento['centro_costo_id'] = $dataMovimiento['centro_costo'];
|
||||
if (array_key_exists('centro_costo', $dataMovimiento)) {
|
||||
$dataMovimiento['centro_costo_id'] = $dataMovimiento['centro_costo'];
|
||||
}
|
||||
$dataMovimiento['fecha'] = new DateTimeImmutable($dataMovimiento['fecha']);
|
||||
if (array_key_exists('rut', $dataMovimiento)) {
|
||||
list($rut, $digito) = explode('-', $dataMovimiento['rut']);
|
||||
$dataMovimiento['rut'] = preg_replace('/\D+/', '', $rut);
|
||||
$dataMovimiento['digito'] = $digito;
|
||||
$dataMovimiento['rut'] = trim(preg_replace('/\D+/', '', $rut));
|
||||
$dataMovimiento['digito'] = trim($digito);
|
||||
}
|
||||
$movimientoService->add($dataMovimiento);
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ class Cartola extends Service
|
||||
$this->bancos[$name] = $banco;
|
||||
return $this;
|
||||
}
|
||||
public function process(Model\Inmobiliaria $inmobiliaria, Model\Contabilidad\Banco $banco, DateTimeInterface $mes, UploadedFileInterface $file): array
|
||||
public function process(Model\Contabilidad\Banco $banco, UploadedFileInterface $file): array
|
||||
{
|
||||
return $this->bancos[strtolower($banco->nombre)]->process($file);
|
||||
}
|
||||
|
@ -38,11 +38,13 @@ class Itau extends Banco
|
||||
}
|
||||
protected function getFilename(UploadedFileInterface $uploadedFile): string
|
||||
{
|
||||
return '/tmp/cartola.xls';
|
||||
$ext = pathinfo($uploadedFile->getClientFilename(), PATHINFO_EXTENSION);
|
||||
return "/tmp/cartola.{$ext}";
|
||||
}
|
||||
protected function parseFile(string $filename): array
|
||||
{
|
||||
$reader = PhpSpreadsheet\IOFactory::createReader('Xls');
|
||||
$ext = pathinfo($filename, PATHINFO_EXTENSION);
|
||||
$reader = PhpSpreadsheet\IOFactory::createReader(ucwords($ext));
|
||||
$xlsx = $reader->load($filename);
|
||||
$sheet = $xlsx->getActiveSheet();
|
||||
|
||||
@ -145,6 +147,9 @@ class Itau extends Banco
|
||||
$value = $sheet->getCell("{$columnIndex}{$row->getRowIndex()}")->getCalculatedValue();
|
||||
$mapped = $this->columnMap()[$column] ?? $column;
|
||||
if ($mapped === 'fecha') {
|
||||
if ($value === '') {
|
||||
continue;
|
||||
}
|
||||
$value = PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value, 'America/Santiago')->format('Y-m-d');
|
||||
}
|
||||
if (in_array($mapped, ['abono', 'cargo', 'saldo'])) {
|
||||
@ -154,7 +159,16 @@ class Itau extends Banco
|
||||
}
|
||||
$data []= $rowData;
|
||||
}
|
||||
return $data;
|
||||
// Remove empty rows
|
||||
return array_filter($data, function($data) {
|
||||
$empties = 0;
|
||||
foreach ($data as $field) {
|
||||
if ($field === '' or $field === null or $field === 0) {
|
||||
$empties++;
|
||||
}
|
||||
}
|
||||
return $empties < count($data) - 1;
|
||||
});
|
||||
}
|
||||
|
||||
protected function identifySheet(PhpSpreadsheet\Worksheet\Worksheet $sheet): int
|
||||
|
Reference in New Issue
Block a user