|
|
|
@ -3,65 +3,46 @@
|
|
|
|
|
include "util/Crypt.php"; |
|
|
|
|
|
|
|
|
|
define("CONF_KEY_LICENCA", md5("S1MPL3S_IP")); |
|
|
|
|
define("CONF_FILE_PATH", "/var/www/html/aplicativo/cadastros/licenca/"); |
|
|
|
|
define("CONF_FILE_PATH", "/var/www/html/pabx-app/cadastros/licenca/"); |
|
|
|
|
define("CONF_FILE_UUID", 'uuid.lca'); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* COLETA AS INFORMAÇÕES DO VENDA MAIS E GERA O TOTAL DE LICENCA EM UM ARQUIVO CRIPTOGRAFADO |
|
|
|
|
* @param type $licenca |
|
|
|
|
* @param type $perfil |
|
|
|
|
/* |
|
|
|
|
* RECEBE OS DADOS DO FORMULÁRIO E GERA O TOTAL DE LICENCA EM UM ARQUIVO CRIPTOGRAFADO |
|
|
|
|
* @param type $fields |
|
|
|
|
*/ |
|
|
|
|
function GerarArquivo($licenca, $perfil) { |
|
|
|
|
|
|
|
|
|
$company = [ |
|
|
|
|
"perfil_aplicacao_id" => $perfil, |
|
|
|
|
]; |
|
|
|
|
function GerarArquivo($fields) { |
|
|
|
|
|
|
|
|
|
/** ARRAY PARA GUARDAR INFORMACOES DOS CONTRATOS * */ |
|
|
|
|
$info = [ |
|
|
|
|
'licenca_pa' => 0, |
|
|
|
|
'licenca_ramal' => 0, |
|
|
|
|
'licenca_supervisor' => 0, |
|
|
|
|
'licenca_discador_preview' => 0, |
|
|
|
|
'licenca_discador_powerdial' => 0, |
|
|
|
|
'licenca_discador_predictive' => 0, |
|
|
|
|
'licenca_pesquisa' => 0, |
|
|
|
|
'licenca_tarifador' => 0, |
|
|
|
|
'licenca_whatsapp' => 0, |
|
|
|
|
'licenca_workflow' => 0, |
|
|
|
|
'licenca_mesaoperadora' => 0, |
|
|
|
|
'licenca_mult_channel' => 0, |
|
|
|
|
'licenca_helpdesk' => 0, |
|
|
|
|
'licenca_crm' => 0, |
|
|
|
|
'licenca_manager_one' => 0, |
|
|
|
|
'licenca_help_custom' => 0, |
|
|
|
|
'data_registro' => date('Y-m-d'), |
|
|
|
|
'user_registro' => 0, |
|
|
|
|
'server_registro' => 0, |
|
|
|
|
'confirma_registro' => date('Y-m-d') |
|
|
|
|
'document', |
|
|
|
|
'razao_social', |
|
|
|
|
'perfil', |
|
|
|
|
'pa', |
|
|
|
|
'pa_fixo', |
|
|
|
|
'ramais', |
|
|
|
|
'supervisor', |
|
|
|
|
'pesquisa', |
|
|
|
|
'tarifador', |
|
|
|
|
'mesaoperadora', |
|
|
|
|
'manager_one' |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
$clto_data = ["contrato_assinatura", "contrato_inicio", "contrato_fim"]; |
|
|
|
|
$data = []; |
|
|
|
|
//LIMPA O ARRAY, SETA O PREFIXO NAS CHAVES E INSERE VALOR ZERO PARA OS VAZIOS |
|
|
|
|
$unfix = 'lca_'; |
|
|
|
|
$prefix = "clto_"; |
|
|
|
|
foreach ($licenca as $clto) { |
|
|
|
|
foreach ($clto as $k => $v) { |
|
|
|
|
$data[$prefix . $k] = $v; |
|
|
|
|
if (array_key_exists($k, $company)) { |
|
|
|
|
$data[$prefix . $k] = $company[$k]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (array_key_exists($k, $info) && is_int($v)) { |
|
|
|
|
$data[$prefix . $k] += $v; |
|
|
|
|
} |
|
|
|
|
foreach ($fields as $key => $value) { |
|
|
|
|
$key = str_replace($unfix, '', $key); |
|
|
|
|
|
|
|
|
|
if (in_array($k, $clto_data)) { |
|
|
|
|
$data[$prefix . $k] = date('Y-d-m', strtotime($v)); |
|
|
|
|
} |
|
|
|
|
if (in_array($key, $info)) { |
|
|
|
|
$data[$prefix . $key] = empty($value) ? '0' : $value; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$fileName = $data['clto_razao_social'] . $data['clto_cnpj']; |
|
|
|
|
$data['clto_server_registro'] = 0; |
|
|
|
|
$data['clto_contrato_numero'] = 0; |
|
|
|
|
$data['clto_date_record'] = date('Y-m-d'); |
|
|
|
|
|
|
|
|
|
$fileName = $data['clto_razao_social'] . $data['clto_document']; |
|
|
|
|
$file = criarArquivo($fileName, $data); |
|
|
|
|
DownloadArquivo($file); |
|
|
|
|
} |
|
|
|
@ -73,7 +54,7 @@ function GerarArquivo($licenca, $perfil) {
|
|
|
|
|
* @return string |
|
|
|
|
*/ |
|
|
|
|
function GerarUUID($tmp = false) { |
|
|
|
|
$file = CONF_FILE_PATH . ($tmp ? '/tmp/' . md5(date('Y-m-d') . CONF_KEY_LICENCA) . "_" : '') . CONF_FILE_UUID; |
|
|
|
|
$file = CONF_FILE_PATH . ($tmp ? 'tmp/' . md5(date('Y-m-d') . CONF_KEY_LICENCA) . "_" : '') . CONF_FILE_UUID; |
|
|
|
|
|
|
|
|
|
if (!file_exists($file) && $tmp) { |
|
|
|
|
foreach (glob('*_' . CONF_FILE_UUID) as $f) { |
|
|
|
@ -248,74 +229,84 @@ function __getLicencaVMais($document) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* INSERE AS INFORMACOES PASSADAS NO ARQUIVO DE LICENCA PARA O BANCO DO SIMPLESIP E VENDA MAIS |
|
|
|
|
* INSERE AS INFORMACOES PASSADAS NO ARQUIVO DE LICENCA PARA O BANCO DO SIMPLESIP |
|
|
|
|
* @param type $dbcon |
|
|
|
|
* @param type $data |
|
|
|
|
* @return boolean |
|
|
|
|
* @throws Exception |
|
|
|
|
*/ |
|
|
|
|
function __validLicenca($dbcon, $data) { |
|
|
|
|
|
|
|
|
|
if (!$data['clto_cnpj']) { |
|
|
|
|
if (!$data['clto_document']) { |
|
|
|
|
throw new Exception("As informações do arquivo está corrompido!"); |
|
|
|
|
} |
|
|
|
|
$respbx = __getLicencaPBX($dbcon); |
|
|
|
|
$resVmais = __getLicencaVMais($data['clto_cnpj']); |
|
|
|
|
$table = 'pbx_controle_licenciamento'; |
|
|
|
|
$where = 'clto_cnpj'; |
|
|
|
|
$filter = $data['clto_document']; |
|
|
|
|
$columns = array( |
|
|
|
|
'clto_cnpj', |
|
|
|
|
'clto_razao_social', |
|
|
|
|
'clto_perfil_aplicacao_id', |
|
|
|
|
'clto_licenca_pa', |
|
|
|
|
'clto_licenca_pa_fixo', |
|
|
|
|
'clto_licenca_ramal', |
|
|
|
|
//'clto_licenca_supervisor', |
|
|
|
|
//'clto_licenca_pesquisa', |
|
|
|
|
//'clto_licenca_tarifador', |
|
|
|
|
//'clto_licenca_mesaoperadora', |
|
|
|
|
//'clto_licenca_manager_one', |
|
|
|
|
'clto_server_registro', |
|
|
|
|
'clto_contrato_numero', |
|
|
|
|
'clto_data_registro', |
|
|
|
|
'clto_data_log' |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
$vendamais = $resVmais['clto_cnpj'] == $data['clto_cnpj'] ? 'vendamais' : ''; |
|
|
|
|
try { |
|
|
|
|
if (!pg_query($dbcon, 'BEGIN')) { |
|
|
|
|
throw new Exception("Não foi possivel iniciar a transação!"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ($respbx['clto_cnpj'] == $data['clto_cnpj'] || $vendamais) { |
|
|
|
|
__updateLicenca($dbcon, $data, $vendamais); |
|
|
|
|
return true; |
|
|
|
|
recordParams($dbcon, $data); |
|
|
|
|
|
|
|
|
|
if ($respbx['clto_cnpj'] == $data['clto_document']) { |
|
|
|
|
__updateLicenca($dbcon, $data, $columns, $table, $where, $filter); |
|
|
|
|
} else { |
|
|
|
|
__sendLicenca($dbcon, $data, $columns, $table, $where, $filter); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (__sendLicenca($dbcon, $data)) { |
|
|
|
|
if (!pg_query($dbcon, 'COMMIT')) { |
|
|
|
|
throw new Exception("Não foi possivel realizar o commit da transação!"); |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} catch (Exception $ex) { |
|
|
|
|
pg_query($dbcon, 'ROLLBACK'); |
|
|
|
|
$GLOBALS['jsStartup'][] = sprintf("alert('%s');", $ex->getMessage()); |
|
|
|
|
throw new Exception($ex->getMessage()); |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function __updateLicenca($dbcon, $data, $t = null) { |
|
|
|
|
$msql = ConectaMssql(); |
|
|
|
|
$info = ""; |
|
|
|
|
foreach ($data as $k => $v) { |
|
|
|
|
$info .= "$k = '$v',"; |
|
|
|
|
function __updateLicenca($dbcon, $data, $columns, $table, $where, $filter) { |
|
|
|
|
$values = array_combine($columns, $data); |
|
|
|
|
|
|
|
|
|
foreach ($values as $key => $value) { |
|
|
|
|
$values[$key] = " {$key} = '{$value}'"; |
|
|
|
|
} |
|
|
|
|
$set = substr($info, 0, -1); |
|
|
|
|
$table = ["pbx" => 'pbx_controle_licenciamento', "vendamais" => 'sip_controle_licenciamento']; |
|
|
|
|
foreach ($table as $k => $v) { |
|
|
|
|
$query = sprintf("UPDATE $v SET %s WHERE clto_cnpj = '%s';", $set, $data['clto_cnpj']); |
|
|
|
|
|
|
|
|
|
if ($k == 'pbx' && (empty($t) || $t == $k)) { |
|
|
|
|
$query = sprintf("UPDATE %s SET %s WHERE %s = '%s'", $table, implode(',', $values), $where, $filter); |
|
|
|
|
$result = pg_query($dbcon, $query); |
|
|
|
|
|
|
|
|
|
if (!$result) { |
|
|
|
|
throw new Exception("Não foi possível atualizar as informações da licença!"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ($k == 'vendamais' && (empty($t) || $t == $k)) { |
|
|
|
|
__execSQLServer($query, false); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function __sendLicenca($dbcon, $data) { |
|
|
|
|
$column = array_keys($data); |
|
|
|
|
function __sendLicenca($dbcon, $data, $columns, $table, $where, $filter) { |
|
|
|
|
$values = array_values($data); |
|
|
|
|
$table = ["pbx" => 'pbx_controle_licenciamento', "vendamais" => 'sip_controle_licenciamento']; |
|
|
|
|
$query = sprintf("INSERT INTO %s (%s) VALUES('%s');", $table, implode(',', $columns), implode("','", $values)); |
|
|
|
|
|
|
|
|
|
foreach ($table as $k => $v) { |
|
|
|
|
$query = sprintf("INSERT INTO {$v} (%s) VALUES('%s');", implode(',', $column), implode("','", $values)); |
|
|
|
|
if ($k == 'pbx') { |
|
|
|
|
if (!pg_query($dbcon, $query)) { |
|
|
|
|
throw new Exception("Não foi possível inserir as informações da licença no [Simples IP]!"); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
__execSQLServer($query, false); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function GetPerfilApl() { |
|
|
|
@ -324,6 +315,79 @@ function GetPerfilApl() {
|
|
|
|
|
return $resp; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
* REALIZA A INSERÇÃO DAS INFORMAÇÕES DA LICENÇA NA TABELA DE PARAMETROS DO SISTEMA |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
function recordParams($dbcon, $data) { |
|
|
|
|
$columns = array( |
|
|
|
|
'prm_max_licenca', |
|
|
|
|
'prm_max_licenca_fixo', |
|
|
|
|
'prm_max_ramal' |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
$values = array( |
|
|
|
|
$data['clto_pa'], |
|
|
|
|
$data['clto_pa_fixo'], |
|
|
|
|
$data['clto_ramais'] |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
$table = 'pbx_parametros'; |
|
|
|
|
$where = 'id'; |
|
|
|
|
$filter = '1'; |
|
|
|
|
|
|
|
|
|
managePaFixo($dbcon, $data); |
|
|
|
|
|
|
|
|
|
if (!VerificaRamaisCriados($data['clto_ramais'])) { |
|
|
|
|
throw new Exception("O número de ramais criados é superior ao parametro informado na licença!"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
__updateLicenca($dbcon, $values, $columns, $table, $where, $filter); |
|
|
|
|
|
|
|
|
|
$_SESSION['prm_max_licenca_fixo'] = $data['clto_pa_fixo']; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
* RESPONSÁVEL POR GERENCIAR AS LICENÇAS PA FIXO |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
function managePaFixo($dbcon, $data) { |
|
|
|
|
$query = "SELECT COUNT(*) AS qtd_pa_fixo FROM pbx_licenca_pa_fixo WHERE id_user IS NOT NULL"; |
|
|
|
|
$result = pg_query($dbcon, $query); |
|
|
|
|
$result = pg_fetch_assoc($result); |
|
|
|
|
|
|
|
|
|
if ($data['clto_pa_fixo'] < $result['qtd_pa_fixo']) { |
|
|
|
|
throw new Exception("O número de P.A. fixos criados é superior ao parametro informado na licença!"); |
|
|
|
|
} else { |
|
|
|
|
$query = "DELETE FROM pbx_licenca_pa_fixo WHERE id_user IS NULL"; |
|
|
|
|
|
|
|
|
|
if (!pg_query($dbcon, $query)) { |
|
|
|
|
throw new Exception('A operação não pode ser realizada!'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$rows = $data['clto_pa_fixo'] - $result['qtd_pa_fixo']; |
|
|
|
|
|
|
|
|
|
for ($cont = 0; $cont < $rows; $cont++) { |
|
|
|
|
$query = sprintf("INSERT INTO pbx_licenca_pa_fixo (licenca) VALUES ('%s')", generateRandomKey()); |
|
|
|
|
if (!pg_query($dbcon, $query)) { |
|
|
|
|
throw new Exception('A operação não pode ser realizada!'); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
* GERADOR DE CHAVES ALEATÓRIAS UTILIZADOS PARA LICENÇA PA FIXO |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
function generateRandomKey() { |
|
|
|
|
return bin2hex(random_bytes(10)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
######################################## |
|
|
|
|
#### CONEXÃO VENDA+ #### |
|
|
|
|
######################################## |
|
|
|
|
|
|
|
|
|
function __dadosLicenca($document, $contracts = []) { |
|
|
|
|
$ctact = implode("','", $contracts); |
|
|
|
|
$data = date('Y-m-d'); |
|
|
|
@ -417,10 +481,6 @@ function __dadosLicenca($document, $contracts = []) {
|
|
|
|
|
return $resp; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
######################################## |
|
|
|
|
#### CONEXÃO VENDA+ #### |
|
|
|
|
######################################## |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Realiza conexao com o banco do venda+ |
|
|
|
|
* @return type |
|
|
|
|