78 lines
2.6 KiB
PHP
78 lines
2.6 KiB
PHP
<?php
|
|
namespace Incoviba\Service\Cartola;
|
|
|
|
use DateTimeImmutable;
|
|
use Psr\Http\Message\UploadedFileInterface;
|
|
use PhpOffice\PhpSpreadsheet;
|
|
use Incoviba\Common\Ideal\Cartola\Banco;
|
|
|
|
class Santander extends Banco
|
|
{
|
|
protected function columnMap(): array
|
|
{
|
|
// MONTO DESCRIPCIÓN MOVIMIENTO FECHA N° DOCUMENTO SUCURSAL CARGO/ABONO
|
|
return [
|
|
'cargo' => 'cargo',
|
|
'abono' => 'abono',
|
|
'DESCRIPCIÓN MOVIMIENTO' => 'glosa',
|
|
'FECHA' => 'fecha',
|
|
'N° DOCUMENTO' => 'documento',
|
|
'SALDO' => 'saldo'
|
|
];
|
|
}
|
|
protected function parseFile(UploadedFileInterface $uploadedFile): array
|
|
{
|
|
$filename = '/tmp/cartola.xlsx';
|
|
$uploadedFile->moveTo($filename);
|
|
|
|
$reader = PhpSpreadsheet\IOFactory::createReader('Xlsx');
|
|
$xlsx = $reader->load($filename);
|
|
$sheet = $xlsx->getActiveSheet();
|
|
|
|
$found = false;
|
|
$columns = [];
|
|
$data = [];
|
|
foreach ($sheet->getRowIterator() as $row) {
|
|
if (!$found and $sheet->getCell("A{$row->getRowIndex()}")->getCalculatedValue() === 'MONTO') {
|
|
$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) {
|
|
break;
|
|
}
|
|
$rowData = [];
|
|
foreach ($columns as $columnIndex => $column) {
|
|
$value = $sheet->getCell("{$columnIndex}{$row->getRowIndex()}")->getCalculatedValue();
|
|
$mapped = $this->columnMap()[$column] ?? $column;
|
|
if ($mapped === 'fecha') {
|
|
$value = implode('-', array_reverse(explode('/', $value)));
|
|
}
|
|
if ($column === 'MONTO') {
|
|
$value = (int) $value;
|
|
}
|
|
$rowData[$column] = $value;
|
|
}
|
|
if ($rowData['CARGO/ABONO'] === 'C') {
|
|
$rowData['MONTO'] = -$rowData['MONTO'];
|
|
$rowData['cargo'] = $rowData['MONTO'];
|
|
$rowData['abono'] = 0;
|
|
} else {
|
|
$rowData['cargo'] = 0;
|
|
$rowData['abono'] = $rowData['MONTO'];
|
|
}
|
|
$data []= $rowData;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|