-
Notifications
You must be signed in to change notification settings - Fork 11
/
init-class.php
418 lines (323 loc) · 10.3 KB
/
init-class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class WooCommerceNFe {
public $domain = 'WooCommerceNFe';
public $version = '3.4.0.3';
protected static $_instance = NULL;
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
public function __get( $key ) {
return $this->$key();
}
function __construct(){
global $domain, $woocommerce;
// Validate plugin before Load
if (!$this->validate_plugin()){
return false;
}
// Globals
$GLOBALS['version_woonfe'] = $this->version;
// Load plugin
$this->includes();
$this->load_plugin_text_domain();
$this->validate_plugin_config();
$this->hooks();
}
/**
* Hooks
*
* @return void
*/
function hooks(){
add_filter( 'woocommercenfe_plugins_url', array($this, 'default_plugin_url') );
add_action( 'transition_post_status', array($this, 'issue_automatic_invoice'), 10, 4 );
add_filter( "plugin_action_links_".plugin_basename( __FILE__ ), array($this, 'plugin_add_settings_link') );
do_action( 'woocommercenfe_loaded' );
}
/**
* Validate Plugin before Load
*
* @return boolean
*/
function validate_plugin(){
global $pagenow, $woocommerce;
// WooCommerce Plugin
if ( !class_exists( 'WooCommerce' ) ) {
$this->add_error( __('<strong>Nota Fiscal WebmaniaBR®:</strong> Para a emissão de Nota Fiscal Eletrônica é necessário ativar o plugin WooCommerce.', $this->domain) );
return false;
}
// WooCommerce Version
$vars = get_object_vars($woocommerce);
if ($vars['version'] < '3.0.0'){
$this->add_error( __('<strong>Nota Fiscal WebmaniaBR®:</strong> Atualize o WooCommerce para a versão 3.0.0 ou superior.', $this->domain) );
return false;
}
// cURL function
if (!function_exists('curl_version')){
$this->add_error( __('<strong>Nota Fiscal WebmaniaBR®:</strong> Necessário instalar o comando cURL no servidor, entre em contato com a sua hospedagem ou administrador do servidor.', $this->domain) );
return false;
}
// Its valid
return true;
}
/**
* Validate Plugin
*
* @author boolean
*/
function validate_plugin_config(){
global $pagenow;
if (
is_admin() &&
$pagenow &&
(
in_array( $pagenow, [ 'index.php', 'admin.php', 'plugins.php' ] ) ||
strpos($pagenow, 'options-') !== false
)
){
$hasAPI1Credentials = false;
$hasAPI2Credentials = false;
// Validate Credentials
if (
get_option('wc_settings_woocommercenfe_access_token') &&
get_option('wc_settings_woocommercenfe_access_token_secret') &&
get_option('wc_settings_woocommercenfe_consumer_key') &&
get_option('wc_settings_woocommercenfe_consumer_secret')
) {
$hasAPI1Credentials = true;
}
if (get_option('wc_settings_woocommercenfe_bearer_access_token')) {
$hasAPI2Credentials = true;
}
if (!$hasAPI1Credentials && !$hasAPI2Credentials) {
$this->add_error( __('<strong>Nota Fiscal WebmaniaBR®:</strong> Informe as credenciais de acesso da aplicação em WooCommerce > Configurações > Nota Fiscal.', $this->domain) );
return false;
}
//Validate API 1.0 configs
if ($hasAPI1Credentials && (
!get_option('wc_settings_woocommercenfe_natureza_operacao') ||
!get_option('wc_settings_woocommercenfe_imposto') ||
!get_option('wc_settings_woocommercenfe_ncm') ||
get_option('wc_settings_woocommercenfe_origem') < 0 ||
get_option('wc_settings_woocommercenfe_origem') == 'null'
)) {
$this->add_error( __('<strong>Nota Fiscal WebmaniaBR®:</strong> Informe a Natureza da Operação, Classe de Imposto, Código NCM e Origem do produto em WooCommerce > Configurações > Nota Fiscal.', $this->domain) );
return false;
}
//Validate API 2.0 configs
if ($hasAPI2Credentials && (
!get_option('wc_settings_woocommercenfe_imposto_nfse')
)) {
$this->add_error( __('<strong>Nota Fiscal WebmaniaBR®:</strong> Informe a Classe de Imposto do serviço em WooCommerce > Configurações > Nota Fiscal.', $this->domain) );
return false;
}
}
// Its validated
return true;
}
/**
* Includes
*/
function includes(){
include_once( 'inc/sdk/NFe.php' );
include_once( 'inc/sdk/NFSe.php' );
include_once( 'inc/utils.php' );
include_once( 'inc/gateways/utils.php' );
include_once( 'inc/gateways/bacs.php' );
include_once( 'inc/gateways/ebanx.php' );
include_once( 'inc/gateways/pagarme.php' );
include_once( 'inc/gateways/pagseguro.php' );
include_once( 'inc/gateways/paypal.php' );
include_once( 'inc/pdf/PDFMerger.php' );
include_once( 'class-format.php' );
include_once( 'class-issue.php' );
include_once( 'class-print.php' );
include_once( 'class-backend.php' );
include_once( 'class-frontend.php' );
}
/**
* Load
*/
function load_plugin_text_domain(){
load_plugin_textdomain( $this->domain, false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* Get API Credentials
*
* @return array
*/
function get_credentials( $order_id = 0 ){
$this->settings = apply_filters( 'nfe_settings', array(
'oauth_access_token' => get_option('wc_settings_woocommercenfe_access_token'),
'oauth_access_token_secret' => get_option('wc_settings_woocommercenfe_access_token_secret'),
'consumer_key' => get_option('wc_settings_woocommercenfe_consumer_key'),
'consumer_secret' => get_option('wc_settings_woocommercenfe_consumer_secret'),
'bearer_access_token' => get_option('wc_settings_woocommercenfe_bearer_access_token'),
), $order_id );
}
/**
* Add error
*
* @return void
*/
function add_error( $message ){
$messages = get_option('woocommercenfe_error_messages');
if (!$messages)
$messages = array();
if ($messages && count($messages) > 0) {
foreach ($messages as $msg){
if ($msg == $message)
return false;
}
}
$messages[] = $message;
update_option('woocommercenfe_error_messages', $messages);
}
/**
* Add Success
*
* @return void
*/
function add_success( $message ){
$messages = get_option('woocommercenfe_success_messages');
if (!$messages)
$messages = array();
if ($messages && count($messages) > 0) {
foreach ($messages as $msg){
if ($msg == $message)
return false;
}
}
$messages[] = $message;
update_option('woocommercenfe_success_messages', $messages);
}
/**
* Issue automatic NFe when change statuses
*
* @return void
*/
function issue_automatic_invoice( $to, $from, $post ) {
// Validations
if (get_post_type($post) != 'shop_order')
return;
$option = apply_filters( 'nfe_issue_automatic', get_option('wc_settings_woocommercenfe_emissao_automatica') );
if ( !$option ){
return;
}
// Vars
$order_id = $post->ID;
$order = wc_get_order( $order_id );
// Process
if (
$to == 'wc-processing' && ($option == 1 || $option == 'yes') ||
$to == 'wc-completed' && $option == 2
){
$nfes = get_post_meta( $order->id, 'nfe', true );
if ( !empty($nfes) && is_array($nfes) ) {
foreach ( $nfes as $nfe ) {
if ( isset($nfe) ) {
return;
}
}
}
$nf = new WooCommerceNFeIssue;
// Prevent orders for only ignored products from auto issuing NFe
if ($nf->is_only_ignored_items( $order_id )) return;
$response = $nf->send( array( $order_id ), false, true );
}
if (!empty($response)) {
do_action( 'nfe_issued_automatic', $order_id, $to, $response );
}
}
/**
* Send e-mail notification to user when auto invoice fail
**/
function send_error_email( $message, $order_id ) {
$email = get_option('wc_settings_woocommercenfe_email_notification');
$ids_db = get_option('wmbr_auto_invoice_errors');
if ( !$email || (is_array($ids_db) && array_key_exists($order_id, $ids_db)) ) {
return;
}
$subject = 'Erro ao emitir NF-e - Pedido #'.$order_id;
$message = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta charset="UTF-8"></head>
<body>
<p>Houve um erro de emissão automatica no Pedido #'.$order_id.': <a target="_blank" href="'.get_admin_url().'/post.php?post='.$order_id.'&action=edit">Acesse o pedido</a></p>
'.$message.'
</body>
</html>';
$headers = array(
'Content-Type: text/html; charset=UTF-8'
);
$emails = array($email);
$enviar_email = wp_mail($emails, $subject, $message, $headers);
}
/**
* Plugin URL
*
* @return string
*/
function default_plugin_url( $url ){
return str_replace('inc/', '', $url);
}
/**
* Config button
*
* @return array
**/
public static function plugin_add_settings_link( $links ) {
$action_links = array(
'settings' => '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=woocommercenfe_tab' ) . '" aria-label="Visualizar Configurações">Configurações</a>',
);
return array_merge( $action_links, $links );
}
/**
* Custom function to verify
* if plugin is active
*
* @return array
**/
public static function wmbr_is_plugin_active( $plugin ) {
return in_array( $plugin, (array) get_option( 'active_plugins', array() ) );
}
/**
* Return person type
*
* @return string
**/
function get_person_type( $type ) {
if ($type == 1)
return 'F';
else if ($type == 2)
return 'J';
else
return '';
}
/**
* Plugin Extra Checkout Fields for Brazil
*
* @return boolean
*/
public static function is_extra_checkout_fields_activated(){
return self::wmbr_is_plugin_active('woocommerce-extra-checkout-fields-for-brazil/woocommerce-extra-checkout-fields-for-brazil.php');
}
/**
* Validate credentials
*
* @return boolean
*/
public function validate_credentials($credentials) {
if (strlen($credentials['consumer_key']) != 32 || strlen($credentials['consumer_secret']) != 48 || strlen($credentials['oauth_access_token']) <= 48 || strlen($credentials['oauth_access_token_secret']) != 48) {
return false;
}
return true;
}
}