From 7275751e25fe7ec151da84e3fa090187551867fa Mon Sep 17 00:00:00 2001 From: Lucas Awade Date: Tue, 5 Dec 2023 12:37:40 +0000 Subject: [PATCH] correcao importa dados empresa --- .../scripts/manutencaoDB/importaEmpresa.php | 21 ++++++++++++++++--- include/util/funcoesApl.php | 8 +++++-- projeto/base/sql/versao-1.9.0.sql | 1 + 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/asterisk/var_lib_asterisk/scripts/manutencaoDB/importaEmpresa.php b/asterisk/var_lib_asterisk/scripts/manutencaoDB/importaEmpresa.php index 4af5caf1..844e0298 100755 --- a/asterisk/var_lib_asterisk/scripts/manutencaoDB/importaEmpresa.php +++ b/asterisk/var_lib_asterisk/scripts/manutencaoDB/importaEmpresa.php @@ -46,13 +46,15 @@ try { /* * Cria as empresas padrao. */ - //CriaOrgPadrao($dbcon); + CriaOrgPadrao($dbcon); /* * Cria a coluna org_id em todas as tabelas. */ CriaCampo($dbcon); + importIdTables($dbcon); + /* * Apaga todas as fk. */ @@ -94,8 +96,13 @@ function CriaCampo($dbcon) continue; } - $query = sprintf("alter table \"%s\" drop column org_id;", $table["table_name"]); - $query .= sprintf("alter table \"%s\" add org_id int not null default 0;", $table["table_name"]); + $query = "DO $$ + BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = '{$table["table_name"]}' AND column_name = 'org_id') THEN + ALTER TABLE {$table["table_name"]} DROP COLUMN IF EXISTS org_id; + END IF; + END $$;"; + $query .= sprintf("alter table %s add org_id int not null default 0;", $table["table_name"]); if (!$result = pg_query($dbcon, $query)) { throw new Exception("Não foi possivel criar a coluna \"org_id\" na table!" . $table["table_name"] . "\"!\n"); @@ -137,6 +144,14 @@ function ApagaFK($dbcon) function CriaOrgPadrao($dbcon) { + $query = "SELECT * FROM pbx_organizacao WHERE master = 1;"; + $result = pg_query($dbcon, $query); + $res = pg_fetch_assoc($result); + + if ($res) { + return; + } + $query = "INSERT INTO pbx_organizacao(nome, master, status, updated_at) VALUES ('Simples IP', 1, 1, NOW()) RETURNING id;"; $result = pg_query($dbcon, $query); $res = pg_fetch_assoc($result); diff --git a/include/util/funcoesApl.php b/include/util/funcoesApl.php index eb0bdef0..2fb541e1 100755 --- a/include/util/funcoesApl.php +++ b/include/util/funcoesApl.php @@ -581,7 +581,11 @@ function IsSuper() function IsUserAdmin() { - $query = sprintf("select count(*) from pbx_grupo_usuario where gp_id = (select gp_id from pbx_grupo where gp_nome = 'ADMINISTRADOR') and user_id = '%s'", GetIdUser()); + $org_id = GetOrganizacao(); + $query = sprintf("SELECT count(*) FROM pbx_grupo_usuario + WHERE gp_id = (SELECT gp_id FROM pbx_grupo WHERE gp_nome = 'ADMINISTRADOR' AND org_id = {$org_id}) + AND user_id = '%s'", GetIdUser()); + $result = pg_query($query); $row = @pg_fetch_row($result); return $row[0] ? true : false; @@ -601,7 +605,7 @@ function AgenteON($idUser = null) return pg_num_rows(pg_query($query)); } else { - $query = sprintf("select '' from pbx_supervisor_agentes where matricula = '%s' and ramal = '%s' ", GetMatricula(), GetRamalMonitorar(RAMAL_MONITOR_AGENTE)); + $query = sprintf("SELECT '' FROM pbx_supervisor_agentes WHERE matricula = '%s' AND ramal = '%s' ", GetMatricula(), GetRamalMonitorar(RAMAL_MONITOR_AGENTE)); return pg_num_rows(pg_query($query)); } diff --git a/projeto/base/sql/versao-1.9.0.sql b/projeto/base/sql/versao-1.9.0.sql index 56eeb0eb..4c4b495c 100644 --- a/projeto/base/sql/versao-1.9.0.sql +++ b/projeto/base/sql/versao-1.9.0.sql @@ -82,6 +82,7 @@ END; $function$ ; +ALTER TABLE pbx_usuarios ADD COLUMN org_padrao int ------------------------------------------ ----- ADICAO DE ID NAS TABELAS ----- ------------------------------------------