2024-01-17 10:33:11 -03:00
|
|
|
|
<?php
|
|
|
|
|
namespace Incoviba\Service\Cartola;
|
|
|
|
|
|
2024-01-17 14:31:49 -03:00
|
|
|
|
use DOMDocument;
|
|
|
|
|
use DateTimeImmutable;
|
2024-01-17 10:33:11 -03:00
|
|
|
|
use Psr\Http\Message\UploadedFileInterface;
|
|
|
|
|
use PhpOffice\PhpSpreadsheet;
|
2024-01-17 16:19:38 -03:00
|
|
|
|
use Incoviba\Common\Ideal\Cartola\Banco;
|
2024-01-17 10:33:11 -03:00
|
|
|
|
|
2024-01-17 16:19:38 -03:00
|
|
|
|
class Security extends Banco
|
2024-01-17 10:33:11 -03:00
|
|
|
|
{
|
2024-02-19 22:39:22 -03:00
|
|
|
|
public function processMovimientosDiarios(array $movimientos): array
|
|
|
|
|
{
|
|
|
|
|
$movimientos = array_reverse($movimientos);
|
|
|
|
|
array_shift($movimientos);
|
|
|
|
|
return $movimientos;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-17 16:19:38 -03:00
|
|
|
|
protected function parseFile(UploadedFileInterface $uploadedFile): array
|
2024-01-17 10:33:11 -03:00
|
|
|
|
{
|
2024-01-17 16:19:38 -03:00
|
|
|
|
$stream = $uploadedFile->getStream();
|
2024-01-17 14:31:49 -03:00
|
|
|
|
$stream->seek(3);
|
|
|
|
|
if ($stream->read(strlen('table')) === 'table') {
|
2024-01-17 16:19:38 -03:00
|
|
|
|
return $this->processHtm($uploadedFile);
|
2024-01-17 14:31:49 -03:00
|
|
|
|
}
|
2024-01-17 16:19:38 -03:00
|
|
|
|
return $this->processXls($uploadedFile);
|
|
|
|
|
}
|
|
|
|
|
protected function columnMap(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'fecha' => 'fecha',
|
|
|
|
|
'descripción' => 'glosa',
|
|
|
|
|
'número de documentos' => 'documento',
|
2024-02-07 23:48:31 -03:00
|
|
|
|
'nº documento' => 'documento',
|
2024-01-17 16:19:38 -03:00
|
|
|
|
'cargos' => 'cargo',
|
2024-02-07 23:48:31 -03:00
|
|
|
|
'abonos' => 'abono',
|
|
|
|
|
'saldos' => 'saldo'
|
2024-01-17 16:19:38 -03:00
|
|
|
|
];
|
2024-01-17 14:31:49 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function processXls(UploadedFileInterface $file): array
|
|
|
|
|
{
|
2024-01-17 10:33:11 -03:00
|
|
|
|
$filename = '/tmp/cartola.xls';
|
|
|
|
|
$file->moveTo($filename);
|
2024-02-07 23:48:31 -03:00
|
|
|
|
$xlsx = @PhpSpreadsheet\IOFactory::load($filename);
|
2024-01-17 10:33:11 -03:00
|
|
|
|
$worksheet = $xlsx->getActiveSheet();
|
2024-02-07 23:48:31 -03:00
|
|
|
|
$rows = $worksheet->getRowIterator(3);
|
2024-01-17 10:33:11 -03:00
|
|
|
|
$dataFound = false;
|
|
|
|
|
$columns = [];
|
|
|
|
|
$data = [];
|
|
|
|
|
foreach ($rows as $row) {
|
|
|
|
|
$cells = $row->getCellIterator();
|
|
|
|
|
$rowData = [];
|
|
|
|
|
foreach ($cells as $cell) {
|
2024-02-07 23:48:31 -03:00
|
|
|
|
if ($cell->getColumn() === 'A' and $cell->getCalculatedValue() !== null and strtolower($cell->getCalculatedValue()) === "fecha ") {
|
2024-01-17 10:33:11 -03:00
|
|
|
|
$cols = $row->getColumnIterator();
|
|
|
|
|
foreach ($cols as $col) {
|
2024-02-07 23:48:31 -03:00
|
|
|
|
$columns[$col->getColumn()] = trim(strtolower($col->getCalculatedValue()), ' ');
|
2024-01-17 10:33:11 -03:00
|
|
|
|
}
|
|
|
|
|
$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') {
|
2024-02-07 23:48:31 -03:00
|
|
|
|
if ((int) $cell->getValue() !== $cell->getValue()) {
|
|
|
|
|
$value = implode('-', array_reverse(explode('-', $cell->getValue())));
|
|
|
|
|
} else {
|
|
|
|
|
$value = PhpSpreadsheet\Shared\Date::excelToDateTimeObject($cell->getValue(), 'America/Santiago')->format('Y-m-d');
|
|
|
|
|
}
|
2024-01-17 10:33:11 -03:00
|
|
|
|
}
|
|
|
|
|
$rowData[$col] = $value;
|
|
|
|
|
}
|
|
|
|
|
if (count($rowData) > 0) {
|
|
|
|
|
$data []= $rowData;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
unlink($filename);
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
2024-01-17 14:31:49 -03:00
|
|
|
|
private function processHtm(UploadedFileInterface $file): array
|
|
|
|
|
{
|
|
|
|
|
$filename = '/tmp/cartola.htm';
|
|
|
|
|
$file->moveTo($filename);
|
|
|
|
|
|
|
|
|
|
$domDocument = new DOMDocument();
|
|
|
|
|
$domDocument->loadHTML('<body>' . file_get_contents($filename) . '</body>');
|
|
|
|
|
|
|
|
|
|
$tables = $domDocument->getElementsByTagName('table');
|
|
|
|
|
$table = $tables->item(4);
|
|
|
|
|
|
|
|
|
|
$columns = [];
|
|
|
|
|
$data = [];
|
|
|
|
|
foreach ($table->getElementsByTagName('tr')->getIterator() as $rowIndex => $row) {
|
|
|
|
|
if ($rowIndex === 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (str_contains($row->textContent, 'cargos')) {
|
|
|
|
|
foreach ($row->getElementsByTagName('td')->getIterator() as $cell) {
|
|
|
|
|
$columns []= trim($cell->textContent);
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$rowData = [];
|
|
|
|
|
foreach ($row->getElementsByTagName('td')->getIterator() as $colIndex => $cell) {
|
|
|
|
|
$col = $columns[$colIndex];
|
|
|
|
|
$value = trim($cell->textContent);
|
|
|
|
|
if ($col === 'fecha') {
|
|
|
|
|
$value = DateTimeImmutable::createFromFormat('d/m/Y', $value)->format('Y-m-d');
|
|
|
|
|
}
|
|
|
|
|
if (in_array($col, ['cargos', 'abonos', 'saldos'])) {
|
|
|
|
|
$value = (float) str_replace(',', '.', $value);
|
|
|
|
|
}
|
|
|
|
|
$rowData[$col] = $value;
|
|
|
|
|
}
|
|
|
|
|
$data []= $rowData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
2024-01-17 10:33:11 -03:00
|
|
|
|
}
|