Skip to content

Commit 5a66cca

Browse files
author
Daniel Fernández Giménez
committed
refactor: mejorar la importación de CSV en la actualización del plugin
- Simplificada la lógica de importación de archivos CSV para evitar errores innecesarios. - Se mejoró el manejo de excepciones y se optimizó el registro de errores.
1 parent 9982888 commit 5a66cca

1 file changed

Lines changed: 20 additions & 11 deletions

File tree

Init.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* This file is part of AsientosPredefinidos plugin for FacturaScripts
45
* Copyright (C) 2022-2025 Carlos Garcia Gomez <carlos@facturascripts.com>
@@ -20,6 +21,10 @@
2021
namespace FacturaScripts\Plugins\AsientosPredefinidos;
2122

2223
use FacturaScripts\Core\Template\InitClass;
24+
use FacturaScripts\Core\Tools;
25+
use FacturaScripts\Core\Base\DataBase;
26+
use FacturaScripts\Core\Lib\Import\CSVImport;
27+
use Throwable;
2328

2429
require_once __DIR__ . '/vendor/autoload.php';
2530

@@ -31,29 +36,33 @@ public function init(): void
3136
$this->loadExtension(new Extension\Controller\ListAsiento());
3237
}
3338

34-
public function uninstall(): void
35-
{
36-
}
39+
public function uninstall(): void {}
3740

3841
public function update(): void
3942
{
4043
// Importar/Actualizar tablas desde los CSV incluidos en el plugin
4144
// Esto asegura que nuevas plantillas en Data/Codpais/ESP se sincronicen con la BBDD
4245
try {
4346
$tables = ['asientospre', 'asientospre_lineas', 'asientospre_variables'];
44-
$database = new \FacturaScripts\Core\Base\DataBase();
47+
$database = new DataBase();
4548
foreach ($tables as $table) {
4649
$file = __DIR__ . DIRECTORY_SEPARATOR . 'Data' . DIRECTORY_SEPARATOR . 'Codpais' . DIRECTORY_SEPARATOR . 'ESP' . DIRECTORY_SEPARATOR . $table . '.csv';
47-
if (file_exists($file)) {
48-
$sql = \FacturaScripts\Core\Lib\Import\CSVImport::importFileSQL($table, $file, true);
49-
if (!empty($sql)) {
50-
$database->query($sql);
51-
}
50+
if (!file_exists($file)) {
51+
continue;
52+
}
53+
54+
$sql = CSVImport::importFileSQL($table, $file, true);
55+
if (empty($sql)) {
56+
continue;
57+
}
58+
59+
if (!$database->exec($sql)) {
60+
Tools::log()->error('asientospredefinidos-import-error: ' . $table);
5261
}
5362
}
54-
} catch (\Throwable $e) {
63+
} catch (Throwable $e) {
5564
// no interrumpir la actualización por un error de importación; logueamos
56-
\FacturaScripts\Core\Tools::log()->warning('asientospredefinidos-import-error', ['message' => $e->getMessage()]);
65+
Tools::log()->warning('asientospredefinidos-import-error', ['message' => $e->getMessage()]);
5766
}
5867
}
5968
}

0 commit comments

Comments
 (0)