Compare commits
3 Commits
6e4dd8180e
...
74b15add36
Author | SHA1 | Date | |
---|---|---|---|
74b15add36 | |||
d8d2cba308 | |||
5983a4b20f |
@ -98,14 +98,19 @@ class Cartola extends Service
|
||||
|
||||
foreach ($movimientos as $dataMovimiento) {
|
||||
$dataMovimiento['cuenta_id'] = $cuenta->id;
|
||||
if (array_key_exists('centro_costo', $dataMovimiento)) {
|
||||
if (array_key_exists('centro_costo', $dataMovimiento) and $dataMovimiento['centro_costo'] !== 0) {
|
||||
$dataMovimiento['centro_costo_id'] = $dataMovimiento['centro_costo'];
|
||||
}
|
||||
$dataMovimiento['fecha'] = new DateTimeImmutable($dataMovimiento['fecha']);
|
||||
if (array_key_exists('rut', $dataMovimiento)) {
|
||||
list($rut, $digito) = explode('-', $dataMovimiento['rut']);
|
||||
$dataMovimiento['rut'] = trim(preg_replace('/\D+/', '', $rut));
|
||||
$dataMovimiento['digito'] = trim($digito);
|
||||
$ruts = $this->parseRut($dataMovimiento['rut']);
|
||||
if (key_exists('rut', $ruts)) {
|
||||
$dataMovimiento['rut'] = $ruts['rut'];
|
||||
$dataMovimiento['digito'] = $ruts['digito'];
|
||||
} else {
|
||||
$dataMovimiento['rut'] = $ruts[0]['rut'];
|
||||
$dataMovimiento['digito'] = $ruts[0]['digito'];
|
||||
}
|
||||
}
|
||||
$this->movimientoService->add($dataMovimiento);
|
||||
}
|
||||
@ -250,4 +255,17 @@ class Cartola extends Service
|
||||
}
|
||||
}
|
||||
}
|
||||
protected function parseRut(string $rut): array
|
||||
{
|
||||
if (str_contains($rut, '/')) {
|
||||
$ruts = explode('/', $rut);
|
||||
$output = [];
|
||||
foreach ($ruts as $rut) {
|
||||
$output []= $this->parseRut($rut);
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
list($rut, $digito) = explode('-', $rut);
|
||||
return ['rut' => trim(preg_replace('/\D+/', '', $rut)), 'digito' => trim($digito)];
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,8 @@ use Psr\Http\Message\UploadedFileInterface;
|
||||
|
||||
class Security extends Banco
|
||||
{
|
||||
use isExcel;
|
||||
|
||||
public function processMovimientosDiarios(array $movimientos): array
|
||||
{
|
||||
$movimientos = array_reverse($movimientos);
|
||||
@ -39,11 +41,13 @@ class Security extends Banco
|
||||
'descripción' => 'glosa',
|
||||
'número de documentos' => 'documento',
|
||||
'nº documento' => 'documento',
|
||||
'n de docu' => 'documento',
|
||||
'cargos' => 'cargo',
|
||||
'abonos' => 'abono',
|
||||
'saldos' => 'saldo',
|
||||
'categoría' => 'categoria',
|
||||
'centro costos' => 'centro_costo',
|
||||
'centro costo' => 'centro_costo',
|
||||
'detalle' => 'detalle',
|
||||
'factura boleta' => 'identificador',
|
||||
'rut' => 'rut',
|
||||
@ -56,43 +60,30 @@ class Security extends Banco
|
||||
{
|
||||
$xlsx = @PhpSpreadsheet\IOFactory::load($filename);
|
||||
$worksheet = $xlsx->getActiveSheet();
|
||||
$rows = $worksheet->getRowIterator(3);
|
||||
$dataFound = false;
|
||||
$columns = [];
|
||||
|
||||
$rowIterator = $worksheet->getRowIterator(3);
|
||||
$titleRow = $this->findTitlesRow($rowIterator, 'fecha', caseInsensitive: true);
|
||||
$columns = $this->grabTitlesRow($titleRow, toLower: true);
|
||||
$rowIterator->next();
|
||||
|
||||
$titleValueMap = [
|
||||
'fecha' => 'fecha',
|
||||
'cargos' => 'int',
|
||||
'abonos' => 'int',
|
||||
'saldos' => 'int',
|
||||
'centro costo' => 'int',
|
||||
'centro costos' => 'int',
|
||||
];
|
||||
$data = [];
|
||||
foreach ($rows as $row) {
|
||||
$cells = $row->getCellIterator();
|
||||
$rowData = [];
|
||||
foreach ($cells as $cell) {
|
||||
if ($cell->getColumn() === 'A' and $cell->getCalculatedValue() !== null and strtolower($cell->getCalculatedValue()) === "fecha ") {
|
||||
$cols = $row->getColumnIterator();
|
||||
foreach ($cols as $col) {
|
||||
$columns[$col->getColumn()] = trim(strtolower($col->getCalculatedValue()), ' ');
|
||||
}
|
||||
$dataFound = true;
|
||||
while ($rowIterator->valid()) {
|
||||
if ($this->isExitValue($rowIterator->current(), ['resúmen del período', 'resumen del período', 'Resúmen del período', 'Resumen del período', null])) {
|
||||
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') {
|
||||
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');
|
||||
}
|
||||
}
|
||||
$rowData[$col] = $value;
|
||||
}
|
||||
$rowData = $this->grabRow($rowIterator->current(), $columns, process: true, titleValueMap: $titleValueMap);
|
||||
if (count($rowData) > 0) {
|
||||
$data []= $rowData;
|
||||
}
|
||||
$rowIterator->next();
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
110
app/src/Service/Contabilidad/Cartola/isExcel.php
Normal file
110
app/src/Service/Contabilidad/Cartola/isExcel.php
Normal file
@ -0,0 +1,110 @@
|
||||
<?php
|
||||
namespace Incoviba\Service\Contabilidad\Cartola;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet;
|
||||
|
||||
trait isExcel
|
||||
{
|
||||
/**
|
||||
* @throws PhpSpreadsheet\Exception
|
||||
* @throws PhpSpreadsheet\Calculation\Exception
|
||||
*/
|
||||
protected function findTitlesRow(PhpSpreadsheet\Worksheet\RowIterator &$rowIterator, string $firstTitle, int $columnOffset = 1, bool $caseInsensitive = false): ?PhpSpreadsheet\Worksheet\Row
|
||||
{
|
||||
if ($caseInsensitive) {
|
||||
$firstTitle = strtolower($firstTitle);
|
||||
}
|
||||
foreach ($rowIterator as $row) {
|
||||
$cellIterator = $row->getCellIterator();
|
||||
if ($columnOffset > 1) {
|
||||
$cellIterator->seek($columnOffset);
|
||||
}
|
||||
$value = ($cellIterator->current()->getCalculatedValue());
|
||||
if ($value === null) {
|
||||
continue;
|
||||
}
|
||||
if ($caseInsensitive) {
|
||||
$value = strtolower($value);
|
||||
}
|
||||
$value = trim($value, ' ');
|
||||
if ($value === $firstTitle) {
|
||||
return $row;
|
||||
}
|
||||
}
|
||||
throw new PhpSpreadsheet\Exception('No se encontró la fila de títulos');
|
||||
}
|
||||
/**
|
||||
* @throws PhpSpreadsheet\Exception
|
||||
* @throws PhpSpreadsheet\Calculation\Exception
|
||||
*/
|
||||
protected function grabTitlesRow(PhpSpreadsheet\Worksheet\Row $row, int $columnOffset = 1, bool $toLower = false): array
|
||||
{
|
||||
$titles = [];
|
||||
$cellIterator = $row->getCellIterator();
|
||||
if ($columnOffset > 1) {
|
||||
$cellIterator->seek($columnOffset);
|
||||
}
|
||||
while ($cellIterator->valid()) {
|
||||
$title = trim($cellIterator->current()->getCalculatedValue(), ' ');
|
||||
if ($toLower) {
|
||||
$title = strtolower($title);
|
||||
}
|
||||
$titles[$cellIterator->current()->getColumn()] = $title;
|
||||
$cellIterator->next();
|
||||
if ($cellIterator->current()->getCalculatedValue() === null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $titles;
|
||||
}
|
||||
protected function grabRow(PhpSpreadsheet\Worksheet\Row $row, array $titles, int $columnOffset = 1, bool $process = false, ?array $titleValueMap = null): array
|
||||
{
|
||||
$data = [];
|
||||
$cellIterator = $row->getCellIterator();
|
||||
if ($columnOffset > 1) {
|
||||
$cellIterator->seek($columnOffset);
|
||||
}
|
||||
while ($cellIterator->valid()) {
|
||||
$col = $cellIterator->current()->getColumn();
|
||||
$colIndex = PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($col);
|
||||
if (count($titles) + $columnOffset < $colIndex or ($colIndex >= $columnOffset and !key_exists($col, $titles))) {
|
||||
break;
|
||||
}
|
||||
$title = $titles[$col];
|
||||
$value = $cellIterator->current()->getCalculatedValue();
|
||||
if ($process) {
|
||||
$value = $this->processValue($titleValueMap, $title, $value);
|
||||
}
|
||||
$data[$title] = $value;
|
||||
$cellIterator->next();
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
protected function processValue(array $titleValueMap, string $title, mixed $value): mixed
|
||||
{
|
||||
if (!key_exists($title, $titleValueMap)) {
|
||||
return $value;
|
||||
}
|
||||
return match (strtolower($titleValueMap[$title])) {
|
||||
'fecha' => (int) $value != $value
|
||||
? implode('-', array_reverse(explode('-', $value)))
|
||||
: PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value, 'America/Santiago')->format('Y-m-d'),
|
||||
'int' => (int) $value,
|
||||
default => $value,
|
||||
};
|
||||
}
|
||||
protected function isExitValue(PhpSpreadsheet\Worksheet\Row $row, array $exitValues, int $columnOffset = 1): bool
|
||||
{
|
||||
$cellIterator = $row->getCellIterator();
|
||||
if ($columnOffset > 1) {
|
||||
$cellIterator->seek($columnOffset);
|
||||
}
|
||||
foreach ($exitValues as $exitValue) {
|
||||
if ($cellIterator->current()->getCalculatedValue() === $exitValue) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user