FIX: Importar cartolas correctamente.

This commit is contained in:
Juan Pablo Vial
2024-07-18 21:25:10 -04:00
parent a471a1083e
commit b2f1bd5ba0
4 changed files with 101 additions and 47 deletions

View File

@ -31,7 +31,7 @@ class Cartola extends Service
$this->bancos[$name] = $banco;
return $this;
}
public function process(Model\Inmobiliaria $inmobiliaria, Model\Contabilidad\Banco $banco, DateTimeInterface $mes, UploadedFileInterface $file): array
public function process(Model\Contabilidad\Banco $banco, UploadedFileInterface $file): array
{
return $this->bancos[strtolower($banco->nombre)]->process($file);
}

View File

@ -38,11 +38,13 @@ class Itau extends Banco
}
protected function getFilename(UploadedFileInterface $uploadedFile): string
{
return '/tmp/cartola.xls';
$ext = pathinfo($uploadedFile->getClientFilename(), PATHINFO_EXTENSION);
return "/tmp/cartola.{$ext}";
}
protected function parseFile(string $filename): array
{
$reader = PhpSpreadsheet\IOFactory::createReader('Xls');
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$reader = PhpSpreadsheet\IOFactory::createReader(ucwords($ext));
$xlsx = $reader->load($filename);
$sheet = $xlsx->getActiveSheet();
@ -145,6 +147,9 @@ class Itau extends Banco
$value = $sheet->getCell("{$columnIndex}{$row->getRowIndex()}")->getCalculatedValue();
$mapped = $this->columnMap()[$column] ?? $column;
if ($mapped === 'fecha') {
if ($value === '') {
continue;
}
$value = PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value, 'America/Santiago')->format('Y-m-d');
}
if (in_array($mapped, ['abono', 'cargo', 'saldo'])) {
@ -154,7 +159,16 @@ class Itau extends Banco
}
$data []= $rowData;
}
return $data;
// Remove empty rows
return array_filter($data, function($data) {
$empties = 0;
foreach ($data as $field) {
if ($field === '' or $field === null or $field === 0) {
$empties++;
}
}
return $empties < count($data) - 1;
});
}
protected function identifySheet(PhpSpreadsheet\Worksheet\Worksheet $sheet): int