-
Notifications
You must be signed in to change notification settings - Fork 11
/
class-backend.php
3213 lines (2736 loc) · 112 KB
/
class-backend.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class WooCommerceNFeBackend extends WooCommerceNFe {
function __construct(){
add_action( 'admin_notices', array($this, 'display_messages') );
add_action( 'admin_notices', array($this, 'validate_certificate') );
add_action( 'add_meta_boxes', array($this, 'register_metabox_nfe_emitida') );
add_action( 'admin_init', array($this, 'wmbr_compatibility_issues') );
add_action( 'add_meta_boxes', array($this, 'register_metabox_listar_nfe') );
add_action( 'init', array($this, 'atualizar_status_nota'), 100 );
add_action( 'woocommerce_api_nfe_callback', array($this, 'nfe_callback') );
add_action( 'woocommerce_api_nfse_callback', array($this, 'nfse_callback') );
add_action( 'save_post', array($this, 'save_informacoes_fiscais'), 10, 2);
add_action( 'admin_head', array($this, 'style') );
add_action( 'woocommerce_order_actions', array( $this, 'add_order_meta_box_actions' ) );
add_action( 'woocommerce_order_action_wc_nfe_emitir', array( $this, 'process_order_meta_box_actions' ) );
add_action( 'admin_footer', array( $this, 'add_order_bulk_actions' ) );
add_action( 'init', array( $this, 'process_order_bulk_actions' ) );
add_filter( 'woocommerce_settings_tabs_array', array($this, 'add_settings_tab'), 100 );
add_action( 'woocommerce_settings_tabs_woocommercenfe_tab', array($this, 'settings_tab'));
add_action( 'woocommerce_update_options_woocommercenfe_tab', array($this, 'update_settings' ));
add_action( 'admin_enqueue_scripts', array($this, 'global_admin_scripts') );
add_action( 'product_cat_add_form_fields', array($this, 'add_category_ncm'));
add_action( 'product_cat_edit_form_fields', array($this, 'edit_category_ncm'), 10, 2);
add_action( 'edited_product_cat', array($this, 'save_product_cat_ncm'), 10, 2);
add_action( 'create_product_cat', array($this, 'save_product_cat_ncm'), 10, 2);
add_action( 'admin_notices', array($this, 'cat_ncm_warning'));
add_action( 'admin_menu', array($this, 'add_admin_menu_item'));
add_action( 'admin_init', array($this, 'alert_auto_invoice_errors'));
add_action( 'wp_ajax_wmbr_remove_order_id_auto_invoice', array($this, 'wmbr_remove_order_id_auto_invoice'));
add_filter( 'woocommerce_admin_shipping_fields', array($this, 'extra_shipping_fields') );
add_action( 'admin_enqueue_scripts', array($this, 'scripts') );
add_action( 'wp_ajax_force_digital_certificate_update', array($this, 'ajax_force_certificate_update') );
// HPOS version
if (class_exists( \Automattic\WooCommerce\Utilities\OrderUtil::class ) && \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled()){
add_filter( 'manage_woocommerce_page_wc-orders_columns', array( $this, 'add_order_status_column_header' ), 20 );
add_action( 'manage_woocommerce_page_wc-orders_custom_column', array( $this, 'add_order_status_column_content' ), 10, 2 );
}
// Legacy version
add_filter( 'manage_edit-shop_order_columns', array( $this, 'add_order_status_column_header' ), 20 );
add_action( 'manage_shop_order_posts_custom_column', array( $this, 'add_order_status_column_content' ), 10, 2 );
// NCM in product variation
add_action( 'woocommerce_variation_options_dimensions', array($this, 'add_ncm_field_product_variation'), 10, 3);
add_action( 'woocommerce_save_product_variation', array($this, 'save_ncm_field_product_variation'), 10, 2 );
/**
* Plugin: Brazilian Market on WooCommerce (Customized)
* @author Claudio Sanches
* @link https://github.com/claudiosmweb/woocommerce-extra-checkout-fields-for-brazil
**/
if (
!WooCommerceNFe::is_extra_checkout_fields_activated() &&
get_option('wc_settings_woocommercenfe_tipo_pessoa') == 'yes'
){
add_filter( 'woocommerce_customer_meta_fields', array( $this, 'customer_meta_fields' ) );
add_filter( 'woocommerce_user_column_billing_address', array( $this, 'user_column_billing_address' ), 1, 2 );
add_filter( 'woocommerce_user_column_shipping_address', array( $this, 'user_column_shipping_address' ), 1, 2 );
add_filter( 'woocommerce_admin_billing_fields', array( $this, 'shop_order_billing_fields' ) );
add_filter( 'woocommerce_admin_shipping_fields', array( $this, 'shop_order_shipping_fields' ) );
add_filter( 'woocommerce_found_customer_details', array( $this, 'customer_details_ajax' ) );
add_action( 'woocommerce_process_shop_order_meta', array( $this, 'save_custom_shop_data' ) );
add_action( 'woocommerce_api_create_order', array( $this, 'wc_api_save_custom_shop_data' ), 10, 2 );
add_action( 'woocommerce_admin_order_data_after_billing_address', array( $this, 'order_data_after_billing_address' ) );
add_action( 'woocommerce_admin_order_data_after_shipping_address', array( $this, 'order_data_after_shipping_address' ) );
add_filter( 'woocommerce_api_order_response', array( $this, 'api_order_response' ), 100, 4 );
add_filter( 'woocommerce_api_customer_response', array( $this, 'api_customer_response' ), 100, 4 );
}
}
/**
* Scripts
*
* @return void
*/
function scripts(){
global $version_woonfe;
wp_register_script( 'woocommercenfe_maskedinput', '//cdnjs.cloudflare.com/ajax/libs/jquery.maskedinput/1.4.1/jquery.maskedinput.js', array('jquery'), $version_woonfe, true );
wp_register_script( 'woocommercenfe_admin_script', apply_filters( 'woocommercenfe_plugins_url', plugins_url( 'assets/js/admin_scripts.js', __FILE__ ) ), null, $version_woonfe );
wp_register_style( 'woocommercenfe_admin_style', apply_filters( 'woocommercenfe_plugins_url', plugins_url( 'assets/css/admin_style.css', __FILE__ ) ), null, $version_woonfe );
wp_enqueue_style( 'woocommercenfe_admin_style' );
wp_enqueue_script( 'woocommercenfe_admin_script' );
wp_enqueue_script( 'woocommercenfe_maskedinput' );
}
/**
* Global Scripts
*
* @return void
*/
function global_admin_scripts(){
wp_register_script( 'woocommercenfe_table_scripts', apply_filters( 'woocommercenfe_plugins_url', plugins_url( 'assets/js/nfe_table.js', __FILE__ ) ) );
wp_register_style( 'woocommercenfe_table_style', apply_filters( 'woocommercenfe_plugins_url', plugins_url( 'assets/css/nfe_table.css', __FILE__ ) ) );
wp_enqueue_style( 'woocommercenfe_table_style' );
wp_enqueue_script( 'woocommercenfe_table_scripts' );
}
/**
* Add new settings tag
*
* @return array
*/
function add_settings_tab( $settings_tabs ){
$settings_tabs['woocommercenfe_tab'] = __( 'Nota Fiscal', $this->domain );
return $settings_tabs;
}
/**
* Settings tab content
*
* @return html
*/
function settings_tab(){
woocommerce_admin_fields( $this->get_settings() );
$transportadoras = get_option('wc_settings_woocommercenfe_transportadoras', array());
?>
<style>
.nfe-shipping-label{
min-width: 120px;
display: inline-block;
}
.nfe-table-body,
.nfe-table-head{
overflow: hidden;
}
.nfe-table-head{
border-bottom: 1px solid #e5e5e5;
}
.nfe-table-head h4{
margin-top: 0;
margin-bottom: 15px;
}
.nfe-table-head--payment{
padding-bottom: 20px;
padding-top: 10px;
}
.nfe-table-head--payment > div,
.nfe-table-body--payment .entry > div{
width: 30%;
display: inline-block;
vertical-align: middle;
}
.nfe-table-head--payment > div h4{
margin-bottom: 0;
}
.nfe-table-head--payment > div h4 span{
font-size: 12px;
color: #696969;
}
.shipping-method-col-title{
float:left;
width: 30%;
}
.shipping-info-col-title{
float:left;
width: 70%;
}
.nfe-shipping-table{
background: #FFF;
border: 1px solid #e5e5e5;
padding: 15px;
}
.nfe-shipping-table .entry{
margin-top: 15px;
border-bottom: 1px solid #e5e5e5;
overflow: hidden;
position: relative;
}
.nfe-shipping-table.payment-info{
padding: 5px;
}
.nfe-shipping-table.payment-info .entry{
border-bottom: 0;
padding-left: 10px;
}
.nfe-shipping-table.payment-info .entry:nth-child(even){
background-color:#efefef;
}
.nfe-shipping-table .nfe-table-body .entry:first-child{
display: none;
}
.shipping-method-col{
display: inline-block;
width: 30%;
float: left;
}
.shipping-info-col{
display: inline-block;
width: 70%;
float: left;
}
.nfe-shipping-methods-sel{
max-width: 80%;
}
#wmbr-add-shipping-info{
margin-top: 15px;
}
.wmbr-remove-shipping-info,
.wmbr-remove-shipping-info:active{
position: absolute;
right: 15px;
top: 50%;
transform: translate(0, -50%)!important;
background-color: #e25050!important;
color: #FFF!important;
border: 0;
}
.wmbr-remove-shipping-info span{
vertical-align: middle;
position: relative;
top: -2px;
}
.cert_ajax_success, .cert_ajax_error {
background: white;
padding: 10px;
}
.cert_ajax_success {
border-left: 4px solid #46b450;
}
.cert_ajax_error {
border-left: 4px solid #dc3232;
}
</style>
<h3>Certificado Digital A1</h3>
<?php
add_action( 'admin_footer', array($this, 'force_digital_certificate_update') );
$validate_certificate = $this->validate_certificate(false, true);
$certificate = ($validate_certificate) ? json_decode($validate_certificate) : '';
echo '<span id="update-digital-certificate-response">';
if ( isset($certificate->status) && $certificate->status == 'success' ) {
echo '<h4 class="cert_ajax_success">Faltam ' . $certificate->msg . ' dias para o Certificado Digital A1 expirar.</h4>';
} elseif ( isset($certificate->status) && $certificate->status == 'error' ) {
echo '<h4 class="cert_ajax_error">Certificado Digital A1 expirado</h4>';
} elseif ( isset($certificate->status) && $certificate->status == 'null_credentials' ) {
echo '<h4 class="cert_ajax_error">'.$certificate->msg.'</h4>';
} else {
echo '<h4 class="cert_ajax_error">Informe as credenciais da API 1.0 para verificar o Certificado Digital A1.</h4>';
}
echo '</span>';
?>
<button type="button" class="button-primary" id="update-digital-certificate">Atualizar Certificado A1</button>
<h3>Informações de Transportadoras</h3>
<p>Cadastre as transportadoras particulares utilizadas em sua loja virtual para identificação na Nota Fiscal Eletrônica. <br>Observação: Para o transporte dos Correios não há necessidade de preenchimento dos dados.</p>
<div class="nfe-shipping-table">
<div class="nfe-table-head">
<h4 class="shipping-method-col-title">Método de Entrega</h4>
<h4 class="shipping-info-col-title">Informações da Transportadora</h4>
</div>
<div class="nfe-table-body">
<div class="entry">
<div class="shipping-method-col">
<?php echo $this->get_shipping_methods_select(); ?>
</div>
<div class="shipping-info-col">
<p><label class="nfe-shipping-label">Razão Social: </label><input type="text" name="shipping_info_rs_0"/></p>
<p><label class="nfe-shipping-label">CNPJ:</label> <input type="text" name="shipping_info_cnpj_0"/></p>
<p><label class="nfe-shipping-label">Inscrição estadual:</label> <input type="text" name="shipping_info_ie_0"/></p>
<p><label class="nfe-shipping-label">Endereço:</label> <input type="text" name="shipping_info_address_0"/></p>
<p><label class="nfe-shipping-label">CEP:</label> <input type="text" name="shipping_info_cep_0"/></p>
<p><label class="nfe-shipping-label">Cidade:</label> <input type="text" name="shipping_info_city_0"/></p>
<p><label class="nfe-shipping-label">UF:</label> <input type="text" name="shipping_info_uf_0"/></p>
</div>
<button type="button" class="button wmbr-remove-shipping-info"><span class="dashicons dashicons-no"></span> Remover</button>
</div>
<?php echo $this->get_transportadoras_entries(); ?>
</div>
<button type="button" class="button-primary" id="wmbr-add-shipping-info">Adicionar Método de Entrega</button>
<input type="hidden" name="shipping-info-count" value="<?php echo count($transportadoras); ?>" />
</div>
<?php
include_once(__DIR__).'/templates/payment-setting.php';
}
/**
* Update Certificate A1
*
* @return script
*/
function force_digital_certificate_update() {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
var data = {
'action': 'force_digital_certificate_update'
};
$("#update-digital-certificate").click(function(){
var response = '';
$("#update-digital-certificate").prop('disabled', true);
jQuery.post(ajaxurl, data, function(response) {
if ( response.status == 'success' ) {
response = '<h4 class="cert_ajax_success">Seu Certificado Digital A1 foi atualizado: Faltam ' + response.msg + ' dias para o certificado digital A1 expirar.</h4>';
} else if ( response.status == 'error' ) {
response = '<h4 class="cert_ajax_error">Erro ao atualizar o Certificado Digital A1: ' + response.msg + '</h4> ';
} else if ( response.status == 'null_credentials' ) {
response = '<h4 class="cert_ajax_error">' + response.msg + '</h4> ';
} else {
response = '<h4 class="cert_ajax_error">Não foi possível atualizar seu Certificado Digital A1. Por favor, solicite suporte para <a href="mailto:suporte@webmaniabr.com">suporte@webmaniabr.com</a></h4>';
}
$("#update-digital-certificate-response").html(response);
$("#update-digital-certificate").prop('disabled', false);
}, 'json');
});
});
</script>
<?php
}
/**
* Update Certificate A1
*
* @return script
*/
function update_settings(){
woocommerce_update_options( $this->get_settings() );
// Vars
$count = (int) $_POST['shipping-info-count'];
$transportadoras = array();
$payment_methods = array();
$patment_descs = array();
$cnpj_payment_methods = array();
if ($method = @$_POST['payment_method']){
$desc = @$_POST['payment_desc'];
foreach($method as $key => $value){
$payment_methods[$key] = sanitize_text_field($value);
$payment_descs[$key] = sanitize_text_field($desc[$key]);
}
}
// Mount carriers
if ($_POST){
for ($i = 1; $i < $count+1; $i++) {
$id = $_POST['shipping_info_method_'.$i];
if (!$id) continue;
$transportadoras[$id] = array();
$keys = array(
'razao_social' => 'rs',
'cnpj' => 'cnpj',
'ie' => 'ie',
'address' => 'address',
'cep' => 'cep',
'city' => 'city',
'uf' => 'uf'
);
foreach($keys as $name => $post_key){
$transportadoras[$id][$name] = sanitize_text_field($_POST['shipping_info_'.$post_key.'_'.$i]);
}
}
}
// Update
update_option('wc_settings_woocommercenfe_transportadoras', $transportadoras);
update_option('wc_settings_woocommercenfe_payment_methods', $payment_methods);
update_option('wc_settings_woocommercenfe_payment_descs', $payment_descs);
update_option('wc_settings_woocommercenfe_cnpj_payments', $cnpj_payment_methods);
}
/**
* WP-Admin plugin settings
*
* @return array
*/
function get_settings(){
$auto_invoice_report_url = get_admin_url(get_current_blog_id(), '/admin.php?page=wmbr_page_auto_invoice_errors');
$settings = array(
'title' => array(
'name' => __( 'Credenciais de Acesso (Nota Fiscal de Produto)', $this->domain ),
'type' => 'title',
'desc' => 'Informe os acessos da sua aplicação - API 1.0'
),
'consumer_key' => array(
'name' => __( 'Consumer Key', $this->domain ),
'type' => 'text',
'css' => 'width:300px;',
'id' => 'wc_settings_woocommercenfe_consumer_key'
),
'consumer_secret' => array(
'name' => __( 'Consumer Secret', $this->domain ),
'type' => 'text',
'css' => 'width:300px;',
'id' => 'wc_settings_woocommercenfe_consumer_secret'
),
'access_token' => array(
'name' => __( 'Access Token', $this->domain ),
'type' => 'text',
'css' => 'width:300px;',
'id' => 'wc_settings_woocommercenfe_access_token'
),
'access_token_secret' => array(
'name' => __( 'Access Token Secret', $this->domain ),
'type' => 'text',
'css' => 'width:300px;',
'id' => 'wc_settings_woocommercenfe_access_token_secret'
),
'section_end_nfe' => array(
'type' => 'sectionend',
'id' => 'wc_settings_woocommercenfe_end'
),
'title_nfse_credentials' => array(
'name' => __( 'Credenciais de Acesso (Nota Fiscal de Serviço)', $this->domain ),
'type' => 'title',
'desc' => 'Informe os acessos da sua aplicação - API 2.0'
),
'bearer_access_token' => array(
'name' => __( 'Bearer Access Token', $this->domain ),
'type' => 'text',
'css' => 'width:300px;',
'id' => 'wc_settings_woocommercenfe_bearer_access_token'
),
'section_end_nfse' => array(
'type' => 'sectionend',
'id' => 'wc_settings_woocommercenfe_end'
),
'title_environment' => array(
'name' => __( 'Ambiente de emissão', $this->domain ),
'type' => 'title',
'desc' => 'Informe o ambiente de emissão. Para validade fiscal (produção) ou para testes (desenvolvimento).'
),
'ambiente' => array(
'name' => __( 'Ambiente', $this->domain ),
'type' => 'radio',
'options' => array('1' => 'Produção', '2' => 'Desenvolvimento (Testes)'),
'default' => '2',
'id' => 'wc_settings_woocommercenfe_ambiente'
),
'section_end' => array(
'type' => 'sectionend',
'id' => 'wc_settings_woocommercenfe_end'
),
'title2' => array(
'name' => __( 'Configuração Padrão', $this->domain ),
'type' => 'title',
'desc' => 'A configuração padrão será utilizada para todos os produtos.<br>Caso deseje a configuração também pode ser personalizada em cada produto ou categoria.'
),
'emissao_automatica' => array(
'name' => __( 'Emissão automática', $this->domain ),
'type' => 'radio',
'options' => array(
'0' => 'Não emitir automaticamente',
'1' => 'Sempre que o status do pedido é alterado para Processando (Pagamento confirmado)',
'2' => 'Sempre que o status do pedido é alterado para Concluído'
),
'default' => '0',
'id' => 'wc_settings_woocommercenfe_emissao_automatica'
),
'envio_email' => array(
'name' => __( 'Envio automático de E-mail', $this->domain ),
'type' =>'checkbox',
'desc' => __( 'Enviar e-mail para o cliente após a emissão da Nota Fiscal'),
'default' => 'yes',
'id' => __('wc_settings_woocommercenfe_envio_email'),
),
'data_emissao' => array(
'name' => __( 'Emissão com Data do Pedido', $this->domain ),
'type' =>'checkbox',
'desc' => __( 'Emissão de Nota Fiscal com a data do pedido (retroativa)'),
'default' => 'no',
'id' => 'wc_settings_woocommercenfe_data_emissao'
),
'email_notification' => array(
'name' => __( 'Notificação de erros', $this->domain ),
'type' => 'email',
'desc' => __( 'Informe um e-mail para notificações de erros na emissão ou <a target="_blank" href="'.$auto_invoice_report_url.'">visualize as notificações</a>.'),
'css' => 'width:300px;',
'id' => 'wc_settings_woocommercenfe_email_notification'
),
'section_end_3' => array(
'type' => 'sectionend',
'id' => 'wc_settings_woocommercenfe_end2'
),
'title_nfse' => array(
'name' => __( 'Configurações (Nota Fiscal de Serviço)', $this->domain ),
'type' => 'title',
'desc' => 'Configuração de campos específicos para a emissão de NFS-e.'
),
'imposto_nfse' => array(
'name' => __( 'Classe de Imposto (NFS-e)', $this->domain ),
'type' => 'text',
'id' => 'wc_settings_woocommercenfe_imposto_nfse'
),
'tipo_desconto_nfse' => array(
'name' => __( 'Tipo de desconto', $this->domain ),
'type' => 'select',
'id' => 'wc_settings_woocommercenfe_tipo_desconto_nfse',
'options' => array(
'0' => 'Selecionar',
'3' => 'Nenhum',
'1' => 'Desconto incondicional',
'2' => 'Desconto condicional'
),
),
'inclui_taxas_nfse' => array(
'name' => __( 'Incluir taxas no valor do serviço', $this->domain ),
'id' => 'wc_settings_woocommercenfe_incluir_taxas_nfse',
'type' => 'checkbox',
'desc' => __( 'Incluir o valor das taxas do pedido no valor do serviço da NFS-e', $this->domain ),
'default' => 'no',
),
'section_end_nfse2' => array(
'type' => 'sectionend',
'id' => 'wc_settings_woocommercenfe_end_nfse'
),
'title_nfe' => array(
'name' => __( 'Configurações (Nota Fiscal de Produto)', $this->domain ),
'type' => 'title',
'desc' => 'Configuração de campos específicos para a emissão de NF-e.'
),
'natureza_operacao' => array(
'name' => __( 'Natureza da Operação', $this->domain ),
'type' => 'text',
'css' => 'width:300px;',
'id' => 'wc_settings_woocommercenfe_natureza_operacao'
),
'imposto' => array(
'name' => __( 'Classe de Imposto (NF-e)', $this->domain ),
'type' => 'text',
'id' => 'wc_settings_woocommercenfe_imposto'
),
'ncm' => array(
'name' => __( 'Código NCM', $this->domain ),
'type' => 'text',
'id' => 'wc_settings_woocommercenfe_ncm'
),
'cest' => array(
'name' => __( 'Código CEST', $this->domain ),
'type' => 'text',
'id' => 'wc_settings_woocommercenfe_cest'
),
'origem' => array(
'name' => __( 'Origem dos Produtos', $this->domain ),
'type' => 'select',
'options' => array(
'null' => 'Selecionar Origem dos Produtos',
'0' => '0 - Nacional, exceto as indicadas nos códigos 3, 4, 5 e 8',
'1' => '1 - Estrangeira - Importação direta, exceto a indicada no código 6',
'2' => '2 - Estrangeira - Adquirida no mercado interno, exceto a indicada no código 7',
'3' => '3 - Nacional, mercadoria ou bem com Conteúdo de Importação superior a 40% e inferior ou igual a 70%',
'4' => '4 - Nacional, cuja produção tenha sido feita em conformidade com os processos produtivos básicos de que tratam as legislações citadas nos Ajustes',
'5' => '5 - Nacional, mercadoria ou bem com Conteúdo de Importação inferior ou igual a 40%',
'6' => '6 - Estrangeira - Importação direta, sem similar nacional, constante em lista da CAMEX e gás natural',
'7' => '7 - Estrangeira - Adquirida no mercado interno, sem similar nacional, constante lista CAMEX e gás natural',
'8' => '8 - Nacional, mercadoria ou bem com Conteúdo de Importação superior a 70%'
),
'css' => 'width:300px;',
'id' => 'wc_settings_woocommercenfe_origem'
),
'section_end_nfe2' => array(
'type' => 'sectionend',
'id' => 'wc_settings_woocommercenfe_end_nfe'
),
'title_intermediador' => array(
'name' => __( 'Indicativo de Intermediador', $this->domain ),
'type' => 'title',
'desc' => 'Campos para indicar o intermediador da operação.'
),
'intermediador' => array(
'name' => __( 'Intermediador da operação', $this->domain ),
'type' => 'select',
'options' => array(
'0' => '0 - Operação sem intermediador (em site ou plataforma própria)',
'1' => '1 - Operação em site ou plataforma de terceiros (intermediadores/marketplace)'
),
'css' => 'width:300px;',
'id' => 'wc_settings_woocommercenfe_intermediador'
),
'cnpj_intermediador' => array(
'name' => __( 'CNPJ do Intermediador', $this->domain ),
'type' => 'text',
'css' => 'width:300px;',
'id' => 'wc_settings_woocommercenfe_cnpj_intermediador'
),
'id_intermediador' => array(
'name' => __( 'ID do intermediador', $this->domain ),
'type' => 'text',
'css' => 'width:300px;',
'id' => 'wc_settings_woocommercenfe_id_intermediador'
),
'section_end_intermediador' => array(
'type' => 'sectionend',
'id' => 'wc_settings_woocommercenfe_end_intermediador'
),
'title4' => array(
'name' => __( 'Informações Complementares (Opcional)', $this->domain ),
'type' => 'title',
'desc' => 'Informações fiscais complementares.'
),
'fisco_inf' => array(
'name' => __( 'Informações ao Fisco', $this->domain ),
'type' => 'textarea',
'id' => 'wc_settings_woocommercenfe_fisco_inf',
'class' => 'nfe_textarea',
),
'cons_inf' => array(
'name' => __( 'Informações Complementares ao Consumidor', $this->domain ),
'type' => 'textarea',
'id' => 'wc_settings_woocommercenfe_cons_inf',
'class' => 'nfe_textarea',
),
'servico_inf' => array(
'name' => __( 'Descrição Complementar do Serviço', $this->domain ),
'type' => 'textarea',
'id' => 'wc_settings_woocommercenfe_servico_inf',
'class' => 'nfe_textarea',
),
'section_ebanx' => array(
'type' => 'sectionend',
'id' => 'wc_settings_woocommercenfe_ebanx'
),
'ebanx_title' => array(
'name' => __( 'Gateways de Pagamento', $this->domain ),
'type' => 'title',
'desc' => 'Compatibilidade com EBANX, Pagar.me, PagSeguro e Paypal Plus.'
),
'ebanx_parcelas' => array(
'name' => __( 'Emitir pagamento parcelado como duplicata na Nota Fiscal', $this->domain ),
'type' => 'checkbox',
'desc' => __( 'Pagamento parcelado como duplicata na Nota Fiscal', $this->domain ),
'id' => 'wc_settings_parcelas_ebanx',
'default' => 'no',
),
'section_end3' => array(
'type' => 'sectionend',
'id' => 'wc_settings_woocommercenfe_end3'
),
'title6' => array(
'name' => __( 'Campos Personalizados no Checkout', $this->domain ),
'type' => 'title',
'desc' => 'Informe se deseja mostrar os campos na página de Finalizar Compra.'
),
'tipo_pessoa' => array(
'name' => __( 'Exibir Tipo de Pessoa', $this->domain ),
'type' => 'checkbox',
'desc' => __( 'Caso esteja marcado exibe os campos de Tipo de Pessoa, CPF, CNPJ e Empresa nas informações de cobrança.', $this->domain ),
'id' => 'wc_settings_woocommercenfe_tipo_pessoa',
'default' => 'yes',
),
'mascara_campos' => array(
'name' => __( 'Habilitar Máscara de Campos', $this->domain ),
'type' => 'checkbox',
'desc' => __( 'Caso esteja marcado adiciona máscaras de preenchimento para os campos de CPF e CNPJ.', $this->domain ),
'id' => 'wc_settings_woocommercenfe_mascara_campos',
'default' => 'yes',
),
'cep' => array(
'name' => __( 'Preenchimento automático do endereço', $this->domain ),
'type' => 'checkbox',
'desc' => __( 'Caso esteja marcado, o endereço será automaticamente preenchido quando o usuário informar o CEP.', $this->domain ),
'id' => 'wc_settings_woocommercenfe_cep',
'default' => 'yes',
),
'bairro' => array(
'name' => __( 'Bairro obrigatório', $this->domain ),
'type' => 'checkbox',
'desc' => __( 'Caso esteja marcado, o campo do endereço Bairro será obrigatório o preenchimento.', $this->domain ),
'id' => 'wc_settings_woocommercenfe_bairro',
'default' => 'no',
),
'section_end4' => array(
'type' => 'sectionend',
'id' => 'wc_settings_woocommercenfe_end4'
)
);
// WooCommerce Extra Checkout Fields for Brazil
if ( WooCommerceNFe::is_extra_checkout_fields_activated() ) {
unset($settings['tipo_pessoa']);
unset($settings['mascara_campos']);
} else {
unset($settings['bairro']);
}
if (
!NFeGatewayEbanx::is_activated() &&
!NFeGatewayPagarme::is_activated() &&
!NFeGatewayPagSeguro::is_activated() &&
!NFeGatewayPaypal::is_activated()
) {
unset($settings['section_ebanx']);
unset($settings['ebanx_title']);
unset($settings['ebanx_parcelas']);
}
// Return
return $settings;
}
/**
* Display Carriers
*
* @return string
*/
function get_transportadoras_entries(){
$transportadoras = get_option('wc_settings_woocommercenfe_transportadoras', array());
$html = '';
$i = 1;
foreach ($transportadoras as $key => $transp) {
$html .= '<div class="entry">';
$html .= '<div class="shipping-method-col">'.$this->get_shipping_methods_select($i, $key).'</div>';
$html .= '<div class="shipping-info-col">';
$html .= '<p><label class="nfe-shipping-label">Razão Social: </label><input type="text" name="shipping_info_rs_'.$i.'" value="'.$transp['razao_social'].'"/></p>';
$html .= '<p><label class="nfe-shipping-label">CNPJ: </label><input type="text" name="shipping_info_cnpj_'.$i.'" value="'.$transp['cnpj'].'"/></p>';
$html .= '<p><label class="nfe-shipping-label">Inscrição estadual: </label><input type="text" name="shipping_info_ie_'.$i.'" value="'.$transp['ie'].'"/></p>';
$html .= '<p><label class="nfe-shipping-label">Endereço: </label><input type="text" name="shipping_info_address_'.$i.'" value="'.$transp['address'].'"/></p>';
$html .= '<p><label class="nfe-shipping-label">CEP: </label><input type="text" name="shipping_info_cep_'.$i.'" value="'.$transp['cep'].'"/></p>';
$html .= '<p><label class="nfe-shipping-label">Cidade: </label><input type="text" name="shipping_info_city_'.$i.'" value="'.$transp['city'].'"/></p>';
$html .= '<p><label class="nfe-shipping-label">UF: </label><input type="text" name="shipping_info_uf_'.$i.'" value="'.$transp['uf'].'"/></p>';
$html .= '<button type="button" class="button wmbr-remove-shipping-info"><span class="dashicons dashicons-no"></span> Remover</button>';
$html .= '</div>';
$html .= '</div>';
$i++;
}
return $html;
}
/**
* Display Shipping Methods selected
*
* @return string
*/
function get_shipping_methods_select($index = 0, $id = ''){
// Vars
$carriers = get_option('wc_settings_woocommercenfe_transportadoras', array());
$html = '<select class="nfe-shipping-methods-sel" name="shipping_info_method_'.$index.'">';
$html .= '<option value="">Selecionar</option>';
// Shipping Methods
$shipping = new WC_Shipping();
$shipping->load_shipping_methods();
$shipping_methods = $shipping->get_shipping_methods();
// Display options
foreach ($shipping_methods as $method) {
// Skip
if ($method->id == 'correios') {
continue;
}
// Mount HTML Frenet
if ($method->id == 'frenet'){
$frenet = NFeUtils::get_frenet_carriers();
if (!$frenet)
continue;
foreach ($frenet->ShippingSeviceAvailableArray as $var){
$selected = '';
if ($id){
((isset($carriers['FRENET_'.$var->ServiceCode]) && $id == 'FRENET_'.$var->ServiceCode) ? $selected = 'selected' : $selected = '');
}
$html .= '<option value="FRENET_'.$var->ServiceCode.'" '.$selected.'>Frenet - '.$var->Carrier.' ('.$var->ServiceDescription.')</option>';
}
continue;
}
// Mount HTML Others Carriers
($method->id == $id ? $selected = 'selected' : $selected = '');
$title = $method->get_title();
if (!$title && isset($method->method_title)){
$title = $method->method_title;
}
$html .= '<option value="'.$method->id.'" '.$selected.'>'.$title.'</option>';
}
$html .= '</select>';
return $html;
}
/**
* Display Payment Methods selected
*
* @return string
*/
function get_payment_methods_select($method, $index = 0, $id = ''){
$saved_values = get_option('wc_settings_woocommercenfe_payment_methods', array());
$options = array(
'01' => 'Dinheiro',
'02' => 'Cheque',
'03' => 'Cartão de Crédito',
'04' => 'Cartão de Débito',
'05' => 'Crédito Loja',
'10' => 'Vale Alimentação',
'11' => 'Vale Refeição',
'12' => 'Vale Presente',
'13' => 'Vale Combustível',
'15' => 'Boleto Bancário',
'16' => 'Depósito Bancário',
'17' => 'Pagamento Instantâneo (PIX)',
'18' => 'Transferência bancária, Carteira Digital',
'19' => 'Programa de fidelidade, Cashback, Crédito Virtual',
'90' => 'Sem pagamento',
'99' => 'Outros',
);
$html = '<select class="nfe-payment-methods-sel" name="payment_method['.$method.']">';
$html .= '<option value="">Selecionar</option>';
foreach ($options as $value => $label) {
$selected = '';
if (isset($saved_values[$method]) && $saved_values[$method] == $value){
$selected = 'selected';
}
$html .= '<option value="'.$value.'" '.$selected.'>'.$label.'</option>';
}
$html .= '</select>';
return $html;
}
/**
* Display Payment Desc
*
* @return string
*/
function get_payment_desc_input($method, $index = 0, $id = ''){
$payment_methods = get_option('wc_settings_woocommercenfe_payment_methods', array());
$is_method_99 = (isset($payment_methods[$method]) && $payment_methods[$method] == 99) ? true : false;
$saved_values = get_option('wc_settings_woocommercenfe_payment_descs', array());
$html = '<input type="text" class="nfe-payment-desc" name="payment_desc['.$method.']" style="width: 400px; ';
if (!$is_method_99) {
$html .= 'display: none;';
}
if (isset($saved_values[$method]) && $is_method_99) {
$html .= '" value="'.$saved_values[$method].'">';
}
else {
$html .= '">';
}
return $html;
}
/**
* Register Metabox
*
* @return void
*/
function register_metabox_nfe_emitida() {
$screen = function_exists( 'wc_get_page_screen_id' ) ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order';
add_meta_box(
'woocommernfe_nfe_emitida',
'Nota Fiscal do Pedido',
array($this, 'metabox_content_woocommernfe_nfe_emitida'),
$screen,
'normal',
'high'
);
add_meta_box(
'woocommernfe_informacoes_adicionais',
'Informações Fiscais',
array($this, 'metabox_content_woocommernfe_informacoes_adicionais'),
$screen,
'side',
'high'
);
}
/**
* Update status
*
* @return void
*/
function atualizar_status_nota() {
if (!is_admin()) {
return false;
}
if ( isset($_GET['atualizar_nfe']) && $_GET['post'] && $_GET['chave']) {
$this->get_credentials();
$post_id = (int) sanitize_text_field($_GET['post']);
$order = wc_get_order( $post_id );
$chave = sanitize_text_field($_GET['chave']);
$webmaniabr = new NFe($this->settings);
$response = $webmaniabr->consultaNotaFiscal($chave);
if (isset($response->error)){
$this->add_error( __('Erro: '.$response->error, $this->domain) );
return false;
} else {
$new_status = $response->status;
$nfe_data = get_post_meta( $order->id, 'nfe', true );
foreach ($nfe_data as &$order_nfe) {
if ($order_nfe['chave_acesso'] == $chave) {
$order_nfe['status'] = $new_status;
isset($response->nfe) && $order_nfe['n_nfe'] = $response->nfe;
isset($response->uuid) && $order_nfe['uuid'] = $response->uuid;
isset($response->recibo) && $order_nfe['n_recibo'] = $response->recibo;
isset($response->serie) && $order_nfe['n_serie'] = $response->serie;
isset($response->xml) && $order_nfe['url_xml'] = $response->xml;
isset($response->danfe) && $order_nfe['url_danfe'] = $response->danfe;
isset($response->danfe_simples) && $order_nfe['url_danfe_simplificada'] = $response->danfe_simples;
isset($response->danfe_etiqueta) && $order_nfe['url_danfe_etiqueta'] = $response->danfe_etiqueta;
}
}
$order->update_meta_data( 'nfe', $nfe_data );
$order->save();
$this->add_success( 'NF-e atualizada com sucesso' );
}
}
}
/**
* Metabox content
*
* @return html
*/
function metabox_content_woocommernfe_nfe_emitida( $order ) {
if ( !is_a( $order, 'WC_Order' ) && isset($order->ID) ) {
$order = wc_get_order( $order->ID );
}
$nfe_data = $order->get_meta('nfe');
if (empty($nfe_data)):