2024-01-17 16:19:38 -03:00
|
|
|
<?php
|
2024-03-26 09:38:20 -03:00
|
|
|
namespace Incoviba\Service\Contabilidad\Cartola;
|
2024-01-17 16:19:38 -03:00
|
|
|
|
|
|
|
use DateTimeImmutable;
|
|
|
|
use Incoviba\Common\Ideal\Cartola\Banco;
|
2024-03-26 09:38:20 -03:00
|
|
|
use PhpOffice\PhpSpreadsheet;
|
|
|
|
use Psr\Http\Message\UploadedFileInterface;
|
2024-01-17 16:19:38 -03:00
|
|
|
|
|
|
|
class Itau extends Banco
|
|
|
|
{
|
2024-02-19 22:39:22 -03:00
|
|
|
const CUENTA_CORRIENTE = 0;
|
|
|
|
const ULTIMOS_MOVIMIENTOS = 1;
|
|
|
|
|
|
|
|
public function processMovimientosDiarios(array $movimientos): array
|
|
|
|
{
|
|
|
|
return array_reverse($movimientos);
|
|
|
|
}
|
|
|
|
|
2024-01-17 16:19:38 -03:00
|
|
|
protected function columnMap(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'Fecha' => 'fecha',
|
|
|
|
'Número de operación' => 'documento',
|
2024-01-17 16:21:40 -03:00
|
|
|
'Descripción' => 'glosa',
|
2024-01-17 16:19:38 -03:00
|
|
|
'Depósitos o abonos' => 'abono',
|
2024-02-19 22:39:22 -03:00
|
|
|
'Giros o cargos' => 'cargo',
|
|
|
|
'Documentos' => 'documento',
|
|
|
|
'Movimientos' => 'glosa',
|
2024-07-17 22:33:33 -04:00
|
|
|
'Saldos' => 'saldo',
|
|
|
|
'Categoría' => 'categoria',
|
|
|
|
'Centro costo' => 'centro_costo',
|
|
|
|
'Detalle' => 'detalle',
|
|
|
|
'Factura Boleta' => 'identificador',
|
|
|
|
'RUT' => 'rut',
|
|
|
|
'Nombres' => 'nombres',
|
|
|
|
'Depto' => 'identificador',
|
2024-01-17 16:19:38 -03:00
|
|
|
];
|
|
|
|
}
|
2024-03-18 17:54:27 -03:00
|
|
|
protected function getFilename(UploadedFileInterface $uploadedFile): string
|
|
|
|
{
|
|
|
|
return '/tmp/cartola.xls';
|
|
|
|
}
|
|
|
|
protected function parseFile(string $filename): array
|
2024-01-17 16:19:38 -03:00
|
|
|
{
|
|
|
|
$reader = PhpSpreadsheet\IOFactory::createReader('Xls');
|
|
|
|
$xlsx = $reader->load($filename);
|
|
|
|
$sheet = $xlsx->getActiveSheet();
|
|
|
|
|
2024-02-19 22:39:22 -03:00
|
|
|
$data = [];
|
|
|
|
try {
|
|
|
|
switch ($this->identifySheet($sheet)) {
|
|
|
|
case self::CUENTA_CORRIENTE:
|
|
|
|
$data = $this->parseCuentaCorriente($sheet);
|
|
|
|
break;
|
|
|
|
case self::ULTIMOS_MOVIMIENTOS:
|
|
|
|
$data = $this->parseUltimosMovimientos($sheet);
|
|
|
|
break;
|
2024-01-17 16:19:38 -03:00
|
|
|
}
|
2024-02-19 22:39:22 -03:00
|
|
|
} catch (PhpSpreadsheet\Exception $exception) {
|
|
|
|
$this->logger->critical($exception);
|
2024-01-17 16:19:38 -03:00
|
|
|
}
|
2024-02-19 22:39:22 -03:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
protected function parseCuentaCorriente(PhpSpreadsheet\Worksheet\Worksheet $sheet): array
|
|
|
|
{
|
|
|
|
$found = false;
|
|
|
|
$year = 0;
|
|
|
|
$columns = [];
|
2024-01-17 16:19:38 -03:00
|
|
|
$data = [];
|
2024-02-19 22:39:22 -03:00
|
|
|
foreach ($sheet->getRowIterator() as $row) {
|
|
|
|
if (!$found and $sheet->getCell("A{$row->getRowIndex()}")->getCalculatedValue() === 'Cartola Histórica') {
|
|
|
|
$columnIndex = 'A';
|
|
|
|
foreach ($row->getColumnIterator() as $column) {
|
|
|
|
if ($column->getValue() !== 'Periodo') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$columnIndex = $column->getColumn();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$dates = explode(' - ', $sheet->getCell("{$columnIndex}{$row->getRowIndex()}")->getCalculatedValue());
|
|
|
|
$date = DateTimeImmutable::createFromFormat('d/m/Y', $dates[0]);
|
|
|
|
$year = $date->format('Y');
|
|
|
|
}
|
|
|
|
if (!$found and $sheet->getCell("A{$row->getRowIndex()}")->getCalculatedValue() === 'Movimientos') {
|
|
|
|
$found = true;
|
|
|
|
foreach ($row->getColumnIterator() as $column) {
|
|
|
|
if ($column->getValue() === null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$columns[$column->getColumn()] = trim($column->getValue());
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!$found) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($sheet->getCell("A{$row->getRowIndex()}")->getValue() === null) {
|
2024-01-17 16:19:38 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
$rowData = [];
|
2024-02-19 22:39:22 -03:00
|
|
|
foreach ($columns as $columnIndex => $column) {
|
|
|
|
$value = $sheet->getCell("{$columnIndex}{$row->getRowIndex()}")->getCalculatedValue();
|
|
|
|
$mapped = $this->columnMap()[$column];
|
|
|
|
if ($mapped === 'fecha') {
|
2024-01-17 16:19:38 -03:00
|
|
|
list($d, $m) = explode('/', $value);
|
|
|
|
$value = "{$year}-{$m}-{$d}";
|
|
|
|
}
|
2024-02-19 22:39:22 -03:00
|
|
|
if (in_array($mapped, ['cargo', 'abono', 'saldo'])) {
|
|
|
|
$value = (int) $value;
|
|
|
|
}
|
|
|
|
$rowData[$column] = $value;
|
2024-01-17 16:19:38 -03:00
|
|
|
}
|
|
|
|
$data []= $rowData;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2024-02-19 22:39:22 -03:00
|
|
|
protected function parseUltimosMovimientos(PhpSpreadsheet\Worksheet\Worksheet $sheet): array
|
|
|
|
{
|
|
|
|
$found = false;
|
|
|
|
$data = [];
|
|
|
|
$columns = [];
|
|
|
|
foreach ($sheet->getRowIterator() as $row) {
|
|
|
|
if (!$found and $sheet->getCell("A{$row->getRowIndex()}")->getCalculatedValue() === 'Últimos Movimientos') {
|
|
|
|
$found = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!$found) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (count($columns) === 0) {
|
|
|
|
foreach ($row->getColumnIterator() as $cell) {
|
|
|
|
if ($cell->getValue() === null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$columns[$cell->getColumn()] = trim($cell->getValue());
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($sheet->getCell("A{$row->getRowIndex()}")->getValue() === null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$rowData = [];
|
|
|
|
foreach ($columns as $columnIndex => $column) {
|
|
|
|
$value = $sheet->getCell("{$columnIndex}{$row->getRowIndex()}")->getCalculatedValue();
|
|
|
|
$mapped = $this->columnMap()[$column] ?? $column;
|
|
|
|
if ($mapped === 'fecha') {
|
|
|
|
$value = PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value, 'America/Santiago')->format('Y-m-d');
|
|
|
|
}
|
|
|
|
if (in_array($mapped, ['abono', 'cargo', 'saldo'])) {
|
|
|
|
$value = (int) $value;
|
|
|
|
}
|
|
|
|
$rowData[$column] = $value;
|
|
|
|
}
|
|
|
|
$data []= $rowData;
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function identifySheet(PhpSpreadsheet\Worksheet\Worksheet $sheet): int
|
|
|
|
{
|
|
|
|
foreach ($sheet->getRowIterator(1, 10) as $row) {
|
|
|
|
$value = $sheet->getCell("A{$row->getRowIndex()}")->getValue();
|
|
|
|
if ($value === 'Estado de Cuenta Corriente') {
|
|
|
|
return self::CUENTA_CORRIENTE;
|
|
|
|
}
|
|
|
|
if ($value === 'Consulta Saldos y Últimos movimientos') {
|
|
|
|
return self::ULTIMOS_MOVIMIENTOS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new PhpSpreadsheet\Exception();
|
|
|
|
}
|
2024-01-17 16:19:38 -03:00
|
|
|
}
|