Files
oficial/app/src/Service/Cartola/Security.php

118 lines
4.0 KiB
PHP
Raw Normal View History

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;
use Incoviba\Common\Ideal\Cartola\Banco;
2024-01-17 10:33:11 -03:00
class Security extends Banco
2024-01-17 10:33:11 -03:00
{
protected function parseFile(UploadedFileInterface $uploadedFile): array
2024-01-17 10:33:11 -03:00
{
$stream = $uploadedFile->getStream();
2024-01-17 14:31:49 -03:00
$stream->seek(3);
if ($stream->read(strlen('table')) === 'table') {
return $this->processHtm($uploadedFile);
2024-01-17 14:31:49 -03:00
}
return $this->processXls($uploadedFile);
}
protected function columnMap(): array
{
return [
'fecha' => 'fecha',
'descripción' => 'glosa',
'número de documentos' => 'documento',
'cargos' => 'cargo',
'abonos' => 'abono',
'saldos' => 'saldo'
];
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-01-17 14:31:49 -03:00
$reader = PhpSpreadsheet\IOFactory::createReader('Xls');
2024-01-17 10:33:11 -03:00
$xlsx = $reader->load($filename);
$worksheet = $xlsx->getActiveSheet();
$rows = $worksheet->getRowIterator();
$dataFound = false;
$columns = [];
$data = [];
foreach ($rows as $row) {
$cells = $row->getCellIterator();
$rowData = [];
foreach ($cells as $cell) {
if ($cell->getColumn() === 'A' and $cell->getCalculatedValue() === "fecha ") {
$cols = $row->getColumnIterator();
foreach ($cols as $col) {
$columns[$col->getColumn()] = trim($col->getCalculatedValue());
}
$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') {
$value = PhpSpreadsheet\Shared\Date::excelToDateTimeObject($cell->getValue(), 'America/Santiago')->format('Y-m-d');
}
$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
}