diff --git a/modules/addons/gofasnfeio/config.php b/modules/addons/gofasnfeio/config.php index feb4bd6..5e2f744 100644 --- a/modules/addons/gofasnfeio/config.php +++ b/modules/addons/gofasnfeio/config.php @@ -99,13 +99,15 @@ function gofasnfeio_config() { // e a "Exclusivamente na versão 1.4.0 será realizada a migração da RPS do módulo para a NFE." // Verifica se a versão dos arquivos do módulo corresponde a versão do módulo no banco de dados. if ($module_version_int >= 140 && $module_version_int < 150 && $previous_version_int >= 140 && $previous_version_int < 150) { - $nfe_rps = gnfe_get_nfes()['rpsNumber']; // Verifica se a configuração rps_number existe no banco de dados. - if (Capsule::table('tbladdonmodules')->where('setting','=','rps_number')->count() == 0) { + if (Capsule::table('tbladdonmodules')->where('module', '=', 'gofasnfeio')->where('setting','=','rps_number')->count() == 0) { try { + $nfe_rps = gnfe_get_nfes()['rpsNumber']; Capsule::table('tbladdonmodules')->insert(['module' => 'gofasnfeio', 'setting' => 'rps_number', 'value' => $nfe_rps]); - } catch (\Throwable $th) {} + } catch (Exception $e) { + logModuleCall('gofas_nfeio', 'gofasnfeio_config', '', $e->getMessage(), '', ''); + } } $whmcs_rps = gnfe_config('rps_number'); @@ -216,12 +218,22 @@ function gofasnfeio_config() { set_custom_field_ini_date(); } + $rps_number = gnfe_config('rps_number'); + if (is_numeric($rps_number)) { + $rpsDescription = 'RPS atualizada de acordo com última nota fiscal emitida, clique no botão salvar alterações para atualizar automaticamente.'; + } else { + $rpsDescription = 'Consultar RPS'; + } + $intro = ['intro' => [ 'FriendlyName' => '', - 'Description' => '
'. $available_update_message .'
'; + $intro['intro']['Description'] .= ''. $update_denied .'
'; + $intro['intro']['Description'] .= ''. $rpsDescription .'
'; + $api_key = ['api_key' => [ 'FriendlyName' => 'API Key', 'Type' => 'text', @@ -239,10 +251,22 @@ function gofasnfeio_config() { 'Type' => 'text', 'Description' => 'O que é Código de Serviço?', ]]; + $nfeSerialNumber = gnfe_get_company_info()['rpsSerialNumber']; + + if ( + Capsule::table('tbladdonmodules') + ->where('module', '=', 'gofasnfeio') + ->where('setting', '=', 'rps_serial_number') + ->get(['value'])[0]->value != $nfeSerialNumber + ) { + try { + Capsule::table('tbladdonmodules') + ->where('module', '=', 'gofasnfeio') + ->where('setting', '=', 'rps_serial_number') + ->update(['value' => gnfe_get_company_info()['rpsSerialNumber']]); + } catch (\Throwable $th) {} + } - try { - Capsule::table('tbladdonmodules')->where('module', 'gofasnfeio')->where('setting', 'rps_serial_number')->update(['value' => gnfe_get_company_info()['rpsSerialNumber']]); - } catch (\Throwable $th) {} $rps_serial_number = ['rps_serial_number' => [ 'FriendlyName' => 'Série do RPS', 'Type' => 'text', @@ -250,21 +274,6 @@ function gofasnfeio_config() { 'Description' => 'Saiba mais', ]]; - $rps_number = gnfe_config('rps_number'); - - if (is_numeric($rps_number)) { - $rps_number_camp_description = 'RPS atualizada de acordo com última nota fiscal emitida, clique no botão salvar alterações para atualizar automaticamente.'; - } else { - $rps_number_camp_description = 'Consultar RPS'; - } - - $rps_number_camp = ['rps_number' => [ - 'FriendlyName' => 'Número do RPS', - 'Type' => 'text', - 'Disabled' => 'true', - 'Description' => $rps_number_camp_description - ]]; - $issue_note_default_cond = ['issue_note_default_cond' => [ 'FriendlyName' => 'Quando emitir NFE', 'Type' => 'radio', @@ -360,7 +369,7 @@ function gofasnfeio_config() { 'Description' => '© ' . date('Y') . ' Suporte módulo', ]]; - $fields = array_merge($intro, $api_key, $company_id, $service_code, $rps_serial_number, $rps_number_camp, $issue_note_default_cond, $issue_note_after, $gnfe_email_nfe_config,$development_, $cancel_invoice_cancel_nfe, $debug, $insc_municipal,$cpf,$cnpj, $tax, $invoiceDetails, $send_invoice_url,$desc_custom, $footer); + $fields = array_merge($intro, $api_key, $company_id, $service_code, $rps_serial_number, $issue_note_default_cond, $issue_note_after, $gnfe_email_nfe_config,$development_, $cancel_invoice_cancel_nfe, $debug, $insc_municipal,$cpf,$cnpj, $tax, $invoiceDetails, $send_invoice_url,$desc_custom, $footer); $configarray = [ 'name' => 'NFE.io', 'description' => 'Módulo Nota Fiscal NFE.io para WHMCS', diff --git a/modules/addons/gofasnfeio/functions.php b/modules/addons/gofasnfeio/functions.php index ad0a98a..d31ae83 100644 --- a/modules/addons/gofasnfeio/functions.php +++ b/modules/addons/gofasnfeio/functions.php @@ -12,7 +12,7 @@ */ if (!function_exists('gnfe_config')) { function gnfe_config($set = false) { - if (!$set) { + if ($set === false) { $setting = []; foreach (Capsule::table('tbladdonmodules')->where('module', '=', 'gofasnfeio')->get(['setting', 'value']) as $settings) { @@ -20,12 +20,12 @@ function gnfe_config($set = false) { } return $setting; - } - - return Capsule::table('tbladdonmodules') + } else { + return Capsule::table('tbladdonmodules') ->where('module', '=', 'gofasnfeio') ->where('setting', '=', $set) ->get(['value'])[0]->value; + } } } @@ -348,7 +348,7 @@ function gnfe_get_nfe($nf) { * @return array */ if (!function_exists('gnfe_get_company_info')) { - function gnfe_get_company_info() { + function gnfe_get_company_info($set = false) { $curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_URL => 'https://api.nfe.io/v1/companies/' . gnfe_config('company_id'), @@ -368,7 +368,7 @@ function gnfe_get_company_info() { curl_close($curl); if ($httpCode === 200) { - return $response; + return $set ? $response[$set] : $response; } else { return array( 'error' => @@ -389,7 +389,7 @@ function gnfe_get_company_info() { */ if (!function_exists('gnfe_put_rps')) { function gnfe_put_rps($company, $rpsNumber) { - $company['rpsNumber'] = $rpsNumber + 1; + $company['rpsNumber'] = intval($rpsNumber) + 1; $requestBody = json_encode($company); $curl = curl_init(); @@ -415,12 +415,15 @@ function gnfe_put_rps($company, $rpsNumber) { ' Consulte: https://nfe.io/docs/desenvolvedores/rest-api/nota-fiscal-de-servico-v1/#/Companies/Companies_Put'; logModuleCall('gofas_nfeio', 'gnfe_put_rps', $requestBody, $response, '', ''); } else { - $nfe_rps = intval(gnfe_get_nfes()['rpsNumber']); + $nfe_rps = intval(gnfe_get_company_info('rpsNumber')); $whmcs_rps = intval(gnfe_config('rps_number')); // Verifica se o RPS na NFe é maior ou igual ao RPS no WHMCS para garantir que a ação foi efetivada. if ($nfe_rps >= $whmcs_rps) { - Capsule::table('tbladdonmodules')->where('module', 'gofasnfeio')->where('setting', 'rps_number')->update(['value' => 'RPS administrado pela NFe.']); + Capsule::table('tbladdonmodules') + ->where('module', '=', 'gofasnfeio') + ->where('setting', '=', 'rps_number') + ->update(['value' => 'RPS administrado pela NFe.']); } else { logModuleCall('gofas_nfeio', 'gnfe_put_rps', $requestBody, 'Erro ao tentar passar tratativa de RPS para NFe. ' . $response, '', ''); } @@ -975,14 +978,16 @@ function gnfe_insert_issue_nfe_cond_in_database() { $previousConditions = Capsule::table('tbladdonmodules') ->where('module', '=', 'gofasnfeio') ->where('setting', '=', 'issue_note_conditions') - ->count() > 0; + ->get(['value'])[0]->value; - if ($previousConditions) { + if (count($previousConditions) <= 0 || $previousConditions != $conditions) { Capsule::table('tbladdonmodules') ->where('module', '=', 'gofasnfeio') ->where('setting', '=', 'issue_note_conditions') ->update(['value' => $conditions]); - } else { + } + + if (count($previousConditions) <= 0) { Capsule::table('tbladdonmodules')->insert(['module' => 'gofasnfeio','setting' => 'issue_note_conditions','value' => $conditions]); } } diff --git a/modules/addons/gofasnfeio/hooks.php b/modules/addons/gofasnfeio/hooks.php index d67d68a..8f77acc 100644 --- a/modules/addons/gofasnfeio/hooks.php +++ b/modules/addons/gofasnfeio/hooks.php @@ -43,6 +43,7 @@ add_hook('AdminClientProfileTabFields', 1, function($vars) { require_once __DIR__ . '/functions.php'; + require_once __DIR__ . '/update.php'; return require_once __DIR__ . '/hooks/customclientissueinvoice.php'; }); diff --git a/modules/addons/gofasnfeio/hooks/customclientissueinvoice.php b/modules/addons/gofasnfeio/hooks/customclientissueinvoice.php index f5f7dcd..1e631e6 100644 --- a/modules/addons/gofasnfeio/hooks/customclientissueinvoice.php +++ b/modules/addons/gofasnfeio/hooks/customclientissueinvoice.php @@ -8,6 +8,7 @@ if (Capsule::schema()->hasTable('mod_nfeio_custom_configs')) { return ['Emitir nota fiscal quando' => gnfe_show_issue_invoice_conds($vars['userid'])]; } else { + gnfe_verifyInstall(); return [ 'Módulo NFE.io' => 'Atualize a página para exibir as informações.' ];