Cartolas
This commit is contained in:
54
app/src/Service/Cartola/Security.php
Normal file
54
app/src/Service/Cartola/Security.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Cartola;
|
||||
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
use PhpOffice\PhpSpreadsheet;
|
||||
use Incoviba\Common\Define\Cartola\Banco;
|
||||
|
||||
class Security implements Banco
|
||||
{
|
||||
public function process(UploadedFileInterface $file): array
|
||||
{
|
||||
$reader = PhpSpreadsheet\IOFactory::createReader('Xls');
|
||||
$filename = '/tmp/cartola.xls';
|
||||
$file->moveTo($filename);
|
||||
$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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user