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