diff --git a/Init.php b/Init.php new file mode 100644 index 0000000..8d080d2 --- /dev/null +++ b/Init.php @@ -0,0 +1,66 @@ + + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +namespace FacturaScripts\Plugins\Modelo130; + +use FacturaScripts\Core\Base\DataBase; +use FacturaScripts\Core\Template\InitClass; +use FacturaScripts\Plugins\Modelo130\Model\Subcuenta130; + +final class Init extends InitClass +{ + public function init(): void + { + } + + public function uninstall(): void + { + } + + public function update(): void + { + new Subcuenta130(); + $this->cleanInvalidUsers(); + } + + private function cleanInvalidUsers(): void + { + // ver si existe la tabla subcuentas o usuario + $dataBase = new DataBase(); + if (false === $dataBase->tableExists('subcuentas_130') || false === $dataBase->tableExists('users')) { + return; + } + + // consulta compatible con mysql y postgresql + // elimina el registro foráneo si no existe en la tabla original + $templateSql = "UPDATE subcuentas_130 + SET REPLACE_COLUMN = NULL + WHERE REPLACE_COLUMN IS NOT NULL + AND NOT EXISTS ( + SELECT 1 + FROM users + WHERE users.nick = subcuentas_130.REPLACE_COLUMN + );"; + + foreach (['nick', 'last_nick'] as $column) { + // reemplazar la columna de user en la tabla subcuentas_130 y ejecutar + $sql = str_replace("REPLACE_COLUMN", $column, $templateSql); + $dataBase->exec($sql); + } + } +}