-
Notifications
You must be signed in to change notification settings - Fork 0
/
Z_J1B1_IMPORTA_NF_EGR
694 lines (579 loc) · 21.6 KB
/
Z_J1B1_IMPORTA_NF_EGR
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
*&---------------------------------------------------------------------*
*& Report Z_J1B1_IMPORTA_NF_EGR
*& Criado por: Eberton Sobreira, 27/12/2019
*&---------------------------------------------------------------------*
*& Carrega uma chave de Acesso do EGR para a J1B1 via BCD.
*&
*&---------------------------------------------------------------------*
report z_j1b1_importa_nf_egr.
tables: j_1bnfdoc, j_1bnflin, /xegr/txmlk.
types:
begin of ty_itens,
itmnum like j_1bdylin-itmnum,
itmtyp like j_1bdylin-itmtyp,
matnr like j_1bdylin-matnr,
werks like j_1bdylin-werks,
maktx like j_1bdylin-maktx,
matkl like j_1bdylin-matkl,
menge like j_1bdylin-menge,
meins like j_1bdylin-meins,
netpr like j_1bdylin-netpr,
netwr like j_1bdylin-netwr,
cfop like j_1bdylin-cfop,
taxlw1 like j_1bdylin-taxlw1,
taxlw2 like j_1bdylin-taxlw2,
matorg like j_1bdylin-matorg,
matuse like j_1bdylin-matuse,
nbm like j_1bdylin-nbm,
taxlw4 like j_1bdylin-taxlw4,
taxlw5 like j_1bdylin-taxlw5,
docref like j_1bdylin-docref,
itmref like j_1bdylin-itmref,
cod_cta like j_1bdylin-cod_cta,
end of ty_itens,
begin of ty_j1bnfstx,
taxtyp(4) type c,
base like j_1bnfstx-base,
rate like j_1bnfstx-rate,
taxval like j_1bnfstx-taxval,
excbas like j_1bnfstx-excbas,
othbas like j_1bnfstx-othbas,
cst(3) type c,
total like j_1bnfstx-taxval,
end of ty_j1bnfstx.
types: begin of ty_saida,
bukrs type /xegr/txmlk-bukrs, "Empresa
branch type /xegr/txmlk-branch, " FILIAL
demi type /xegr/txmlk-demi, " DT Emissão
dtemi type char10, " DT Emisão (formato data)
partyp type /xegr/txmlk-partyp, " Tp Parc
lifnr type /xegr/txmlk-lifnr, " Cod Emitente
name1 type /xegr/txmlk-name1, " Raz Social
uf type /xegr/wegr9002-uf, " UF
"model type /xegr/txmlk-model, " MODELO (57)
nfenum type /xegr/txmlk-nfenum, " Nº NF/NF-e NFe
id type /xegr/txmlk-id, "ID
serie type /xegr/txmlk-serie, " Ser
nitem type char10, "/xegr/wegr9004-nitem, " Item NF
cprod type /xegr/wegr9004-cprod, " Codigo Produto fornecedor
xprod type /xegr/wegr9004-xprod, " Descrição
cfop type /xegr/wegr9004-cfop, " Cfop
ncm type /xegr/wegr9004-ncm, " Nbm/NCM
qcom type /xegr/wegr9004-qcom, " Qtde NF
ucom type /xegr/wegr9004-ucom, " Und
vuncom type /xegr/wegr9004-vuncom, " Und
"vprod type /xegr/wegr9004-vprod, " Vl Liquido
"vnf type /xegr/wegr9005-vnf, " Vl Contábil
modfrete type /xegr/wegr9006-modfrete,
placa type /xegr/wegr9006-placa,
especie type /xegr/wegr9006v-esp,
nvol type /xegr/wegr9006v-nvol,
pesob type /xegr/wegr9006v-pesob,
pesol type /xegr/wegr9006v-pesol,
qvol type /xegr/wegr9006v-qvol,
"xped type /xegr/wegr9004-xped, " Pedido de compra
"nitemped type /xegr/wegr9004-nitemped, " Item do pedido compra
"ekgrp type char025, " Grp Compradores
"infadi type /xegr/wegr9008v-infcpl, " Informações Adicionais
"statxt type char40, " Situação da Manifestação do Destinatário
end of ty_saida.
data:
gw_wnfetab_orig type /xegr/wnfetab,
go_nfexml type ref to /xegr/clegrnfexml.
data: ls_w_wnfeitem like /xegr/wegr9004,
ls_w_wnfevol like /xegr/wegr9006v.
*Tabela Interna
data: ti_bdc type table of bdcdata,
ti_message type table of bdcmsgcoll.
*Work Area
data: wa_bdc type bdcdata,
wa_params type ctu_params,
wa_message type bdcmsgcoll.
data: wa_saida type ty_saida.
data:
"wx_docdat(10) type c,
wx_nfenum(9) type c,
"wx_nftype(2) type c,
"wx_parid(10) type c,
"wx_parvw(2) type c,
wx_inco1 like j_1bnfdoc-inco1,
wx_anzpk(5) type n,
wx_ntgew(15) type c,
wx_brgew(15) type c,
wx_traid(20) type c.
"wx_shpunt(3) type c.
data:
wx_menge(16) type c, "Qtde
wx_netpr(16) type c, "Preço Unitário/Líquido
wx_cstpis(3) type c,
wx_cstcofins(3) type c.
data:
wx_qtdeitens type i value 0,
wx_idxitem type i value 1.
selection-screen begin of block b1 with frame title text-t01.
parameter: p_chave type /xegr/txmlk-id obligatory.
selection-screen end of block b1.
selection-screen begin of block b3 with frame title text-t03.
parameters: "p_docdat type j_1bdydoc-docdat OBLIGATORY,
p_nftype type j_1bnfdoc-nftype default 'I6' obligatory, "Categoria NF
p_parid type j_1bnfdoc-parid, " default '93740',
p_parvw type j_1bnfdoc-parvw,
p_observ type j_1bdydoc-observat.
selection-screen end of block b3.
selection-screen begin of block b2 with frame title text-t02.
parameters: p_cfop type j_1bdylin-cfop, "Novo CFOP
p_matkl type j_1bdylin-matkl,
p_matuse type j_1bdylin-matuse,
p_csticm type j_1bdylin-taxlw1, "Direito ICMS
p_cstipi type j_1bdylin-taxlw2, "Direito IPI
p_cstpis type j_1bdylin-taxlw5, "Direito PIS
p_cstcof type j_1bdylin-taxlw4. "Direito Cofins
selection-screen end of block b2.
start-of-selection.
*-->(Load XML Data)
create object go_nfexml.
clear: gw_wnfetab_orig.
gw_wnfetab_orig = go_nfexml->get_xmltab( i_id = p_chave ).
if not gw_wnfetab_orig is initial.
wa_saida-bukrs = gw_wnfetab_orig-wegrxmlk-bukrs.
wa_saida-branch = gw_wnfetab_orig-wegrxmlk-branch.
wa_saida-demi = gw_wnfetab_orig-wegrxmlk-demi.
wa_saida-id = p_chave.
wa_saida-partyp = gw_wnfetab_orig-wegrxmlk-partyp. "Tipo Parceiro
wa_saida-lifnr = gw_wnfetab_orig-wegrxmlk-lifnr. "ID Parceiro
wa_saida-name1 = gw_wnfetab_orig-wegrxmlk-name1.
"wa_saida-model = gw_wnfetab_orig-wegrxmlk-model.
wa_saida-nfenum = gw_wnfetab_orig-wegrxmlk-nfenum.
wa_saida-serie = gw_wnfetab_orig-wegrxmlk-serie.
wa_saida-modfrete = gw_wnfetab_orig-wegr9006-modfrete.
wa_saida-placa = gw_wnfetab_orig-wegr9006-placa.
concatenate gw_wnfetab_orig-wegrxmlk-demi+6(02) '.'
gw_wnfetab_orig-wegrxmlk-demi+4(02) '.'
gw_wnfetab_orig-wegrxmlk-demi+0(04) into wa_saida-dtemi.
write wa_saida-nfenum+6(9) to wx_nfenum no-zero.
endif.
if sy-subrc ne 0.
message 'NF não encontrada com esta chave de acesso.' type 'I'.
stop.
endif.
* ************************
* HEADER DA NF
* ************************
if wa_saida-partyp is not initial.
if wa_saida-partyp eq 'V'. "Fornecedor
p_parvw = 'LF'.
else.
p_parvw = 'AG'.
endif.
else.
"p_parvw = 'XX'.
p_parvw = 'LF'.
endif.
if p_parid is not initial.
wa_saida-lifnr = p_parid.
endif.
* TELA INICIAL
perform f_load_bdcdata using:
'X' 'SAPMJ1B1' '0900',
' ' 'J_1BDYDOC-NFTYPE' p_nftype,
' ' 'J_1BDYDOC-BUKRS' wa_saida-bukrs, "Empresa
' ' 'J_1BDYDOC-BRANCH' wa_saida-branch, "Local de negócios
' ' 'J_1BDYDOC-PARVW' p_parvw,
' ' 'J_1BDYDOC-PARID' wa_saida-lifnr,
' ' 'J_1BDYLIN-INCLTX' 'X'.
perform f_load_bdcdata using:
'X' 'SAPLJ1BB2' '2000',
' ' 'J_1BDYDOC-NFENUM' wx_nfenum,
' ' 'J_1BDYDOC-DOCDAT' wa_saida-dtemi,
' ' 'J_1BDYDOC-SERIES' wa_saida-serie,
"' ' 'J_1BDYDOC-OBSERVAT' p_observ,
"' ' 'J_1BDYDOC-DOCREF' wa_header-docnum,
' ' 'BDC_OKCODE' '=TAB5'. "Vai para Transporte
* Não necessário salvar o Parceiro SP pois o EGR não salva o transportador na J1B1N.
*perform f_load_bdcdata using:
* 'X' 'SAPLJ1BB2' '2000',
* ' ' 'J_1BDYNAD-PARVW(01)' p_parvw,
* ' ' 'J_1BDYNAD-PARID(01)' p_parid,
* ' ' 'J_1BDYNAD-PARVW(02)' 'SP',
* ' ' 'J_1BDYNAD-PARID(02)' p_parid1, "Parid do Transportador
* ' ' 'BDC_OKCODE' wx_nextcode.
wx_inco1 = 'SSS'.
if wa_saida-modfrete is not initial.
if wa_saida-modfrete eq 0.
wx_inco1 = 'CIF'.
elseif wa_saida-modfrete eq 1.
wx_inco1 = 'FOB'.
endif.
endif.
loop at gw_wnfetab_orig-w_wnfevol into ls_w_wnfevol.
wa_saida-especie = ls_w_wnfevol-esp.
wa_saida-nvol = ls_w_wnfevol-nvol.
wa_saida-qvol = ls_w_wnfevol-qvol.
wa_saida-pesob = ls_w_wnfevol-pesob.
wa_saida-pesol = ls_w_wnfevol-pesol.
condense wa_saida-especie.
write wa_saida-qvol+10(5) to wx_anzpk.
write wa_saida-pesol to wx_ntgew no-zero.
write wa_saida-pesob to wx_brgew no-zero.
endloop.
* Transporte
perform f_load_bdcdata using:
'X' 'SAPLJ1BB2' '2000',
' ' 'J_1BDYDOC-INCO1' wx_inco1,
' ' 'J_1BDYDOC-INCO2' wx_inco1,
' ' 'J_1BDYDOC-TRAID' wa_saida-placa,
' ' 'J_1BDYDOC-ANZPK' wx_anzpk, "Qtde
"' ' 'J_1BDYDOC-SHPUNT' 'PC', "Volume
' ' 'J_1BDYDOC-SHPNUM' wa_saida-especie, "Volume
' ' 'J_1BDYDOC-NTGEW' wx_ntgew, "Peso Líquido
' ' 'J_1BDYDOC-BRGEW' wx_brgew, "Peso Bruto
"' ' 'J_1BDYDOC-GEWEI' 'KG', "Unidade de Peso
' ' 'BDC_OKCODE' '=TAB1'. "Vai pra Tela Inicial
"Adiciona Itens da NFe
perform f_load_bdcdata using:
'X' 'SAPLJ1BB2' '2000',
' ' 'BDC_OKCODE' '=ADIT'.
* ************************
* ITENS DA NF
* ************************
*Conta a Qtde.
loop at gw_wnfetab_orig-w_wnfeitem into ls_w_wnfeitem.
wx_qtdeitens = wx_qtdeitens + 1.
endloop.
loop at gw_wnfetab_orig-w_wnfeitem into ls_w_wnfeitem.
"pack ls_w_wnfeitem-nitem to wa_saida-nitem.
wa_saida-cprod = ls_w_wnfeitem-cprod.
wa_saida-xprod = ls_w_wnfeitem-xprod.
wa_saida-cfop = ls_w_wnfeitem-cfop.
wa_saida-ncm = ls_w_wnfeitem-ncm.
wa_saida-qcom = ls_w_wnfeitem-qcom.
wa_saida-ucom = ls_w_wnfeitem-ucom.
wa_saida-vuncom = ls_w_wnfeitem-vuncom.
"wa_saida-vprod = ls_w_wnfeitem-vprod.
"wa_saida-xped = ls_w_wnfeitem-xped.
"wa_saida-nitemped = ls_w_wnfeitem-nitemped.
"wa_saida-vnf = gw_wnfetab_orig-wegr9005-vnf.
write wa_saida-qcom to wx_menge decimals 2.
write wa_saida-vuncom to wx_netpr decimals 2.
if p_cstpis is initial.
concatenate 'P' ls_w_wnfeitem-cstpis into wx_cstpis.
else.
wx_cstpis = p_cstpis.
endif.
if p_cstcof is initial.
concatenate 'C' ls_w_wnfeitem-cstcofins into wx_cstcofins.
else.
wx_cstcofins = p_cstcof.
endif.
perform normaliza_string using wa_saida-ucom.
if p_cfop is initial.
"Lê do xml.
concatenate wa_saida-cfop 'AA' into p_cfop.
endif.
"Trunca a descrição do produto
data :
wx_len type i value 0,
wx_prod(40) type c.
condense wa_saida-xprod.
wx_len = strlen( wa_saida-xprod ).
if wx_len > 40.
wx_prod = wa_saida-xprod+0(40).
else.
wx_prod = wa_saida-xprod.
endif.
perform f_load_bdcdata using:
'X' 'SAPLJ1BB2' '3000',
' ' 'J_1BDYLIN-ITMTYP' '1',
"' ' 'J_1BDYLIN-MATNR' wa_saida-cprod,
"' ' 'J_1BDYLIN-WERKS' wa_item-werks,
' ' 'J_1BDYLIN-MAKTX' wx_prod,
' ' 'J_1BDYLIN-MATKL' p_matkl,
' ' 'J_1BDYLIN-MENGE' wx_menge, "Qtde
' ' 'J_1BDYLIN-MEINS' wa_saida-ucom, "UnidMed
' ' 'J_1BDYLIN-NETPR' wx_netpr, "Preço Líquido
' ' 'J_1BDYLIN-CFOP' p_cfop,
"' ' 'J_1BDYLIN-TAXLW1' ls_w_wnfeitem-csticms, "Direitos ICMS
"' ' 'J_1BDYLIN-TAXLW2' ls_w_wnfeitem-cstipi, "Direitos IPI
' ' 'J_1BDYLIN-TAXLW1' p_csticm, "Direitos ICMS
' ' 'J_1BDYLIN-TAXLW2' p_cstipi, "Direitos IPI
' ' 'J_1BDYLIN-MATORG' ls_w_wnfeitem-orig,
' ' 'J_1BDYLIN-MATUSE' p_matuse,
' ' 'J_1BDYLIN-NBM' wa_saida-ncm,
' ' 'J_1BDYLIN-TAXLW4' wx_cstcofins, "Direitos COFINS
' ' 'J_1BDYLIN-TAXLW5' wx_cstpis, "Direitos PIS
"' ' 'J_1BDYLIN-COD_CTA' wx_ctanalit,
"' ' 'BDC_OKCODE' WX_NEXTCODE.
' ' 'BDC_OKCODE' '=TAX'.
"wa_saida-uf = gw_wnfetab_orig-wegr9002-uf.
"* ***************************
"* IMPOSTOS DO ITEM
"* ***************************
"Se valor da BC > 0 e Aliquota > 0, então ICM1
"Senão, ICM0
data:
wa_tax_icms type ty_j1bnfstx,
wa_tax_ipi type ty_j1bnfstx,
wa_tax_pis type ty_j1bnfstx,
wa_tax_cofins type ty_j1bnfstx.
"ICMS
move ls_w_wnfeitem-vbcicms to wa_tax_icms-base.
move ls_w_wnfeitem-picms to wa_tax_icms-rate.
move ls_w_wnfeitem-vicms to wa_tax_icms-taxval.
wa_tax_icms-othbas = 0.
wa_tax_icms-excbas = 0.
wa_tax_icms-cst = p_csticm.
move ls_w_wnfeitem-vprod to wa_tax_icms-total.
perform insere_imposto using wa_tax_icms '1'.
"IPI
move ls_w_wnfeitem-vbcipi to wa_tax_ipi-base.
move ls_w_wnfeitem-pipi to wa_tax_ipi-rate.
move ls_w_wnfeitem-vipi to wa_tax_ipi-taxval.
wa_tax_ipi-othbas = 0.
wa_tax_ipi-excbas = 0.
wa_tax_ipi-cst = p_cstipi.
move ls_w_wnfeitem-vprod to wa_tax_ipi-total.
perform insere_imposto using wa_tax_ipi '2'.
"Pis Cofins C70 e P70, não adiciona as linhas.
if wx_cstpis ne 'P08' or wx_cstpis ne 'P70' or wx_cstpis ne 'P99'.
move ls_w_wnfeitem-vbcpis to wa_tax_pis-base.
move ls_w_wnfeitem-ppis to wa_tax_pis-rate.
move ls_w_wnfeitem-vpis to wa_tax_pis-taxval.
wa_tax_pis-othbas = 0.
wa_tax_pis-excbas = 0.
wa_tax_pis-cst = wx_cstpis.
move ls_w_wnfeitem-vprod to wa_tax_pis-total.
perform insere_imposto using wa_tax_pis '3'.
endif.
if wx_cstcofins ne 'C08' or wx_cstcofins ne 'C70' or wx_cstcofins ne 'C99'.
move ls_w_wnfeitem-vbccofins to wa_tax_cofins-base.
move ls_w_wnfeitem-pcofins to wa_tax_cofins-rate.
move ls_w_wnfeitem-vcofins to wa_tax_cofins-taxval.
wa_tax_cofins-othbas = 0.
wa_tax_cofins-excbas = 0.
wa_tax_cofins-cst = wx_cstcofins.
move ls_w_wnfeitem-vprod to wa_tax_cofins-total.
perform insere_imposto using wa_tax_pis '4'.
endif.
perform f_load_bdcdata using:
'X' 'SAPLJ1BB2' '3000',
' ' 'BDC_OKCODE' '=TAB1'.
clear wa_tax_icms.
clear wa_tax_ipi.
clear wa_tax_pis.
clear wa_tax_cofins.
"Verifica se está no último item.
if wx_idxitem eq wx_qtdeitens.
"Adiciona novo item.
perform f_load_bdcdata using:
'X' 'SAPLJ1BB2' '2000',
' ' 'BDC_OKCODE' '=TAB8'. "Vai para Dados NFe
else.
perform f_load_bdcdata using:
'X' 'SAPLJ1BB2' '2000',
' ' 'BDC_OKCODE' '=ADIT'.
endif.
wx_idxitem = wx_idxitem + 1.
endloop.
"Dados NFe
data:
wx_digit8(8) type c,
wx_lastdig(1) type c.
wx_digit8 = wa_saida-id+35(8).
wx_lastdig = wa_saida-id+43(1).
perform f_load_bdcdata using:
'X' 'SAPLJ1BB2' '2000',
' ' 'J_1BDYDOC-AUTHCOD' gw_wnfetab_orig-wegr90xx-nprot,
' ' 'J_1BNFE_DOCNUM9_DIVIDED-DOCNUM8' wx_digit8,
' ' 'J_1BNFE_ACTIVE-CDV' wx_lastdig,
' ' 'BDC_OKCODE' '=TAB1'.
* MENSAGENS
* EGR Não carrega mensagens de dados adicionais da NF.
*PERFORM f_load_bdcdata USING:
* 'X' 'SAPLJ1BB2' '2000',
* ' ' 'BDC_OKCODE' '=SAVE'.
wa_params-dismode = 'A'. " ( A - Visivel N - Background )
wa_params-racommit = 'X'.
call transaction 'J1B1N'
using ti_bdc
mode 'E' "Errors only
"OPTIONS FROM wa_params
messages into ti_message. "Retorno do sucesso ou fracasso do batch input.
"Message 'Cópia da NF concluída. CONFIRME OS DADOS ANTES DE SALVAR!!!' type 'S' DISPLAY LIKE 'E'.
data vl_msg type string. "STRING É IGUAL CHAR, VAI AUTOMÁTICO DE ACORDO COM O TEXTO
loop at ti_message into wa_message.
call function 'MESSAGE_TEXT_BUILD'
exporting
msgid = wa_message-msgid
msgnr = wa_message-msgnr
msgv1 = wa_message-msgv1
msgv2 = wa_message-msgv2
importing
message_text_output = vl_msg.
write / vl_msg.
endloop.
*&---------------------------------------------------------------------*
*& Form F_LOAD_BDCDATA
*&---------------------------------------------------------------------*
form f_load_bdcdata using p_x
p_2
p_3.
if p_x = 'X'.
wa_bdc-program = p_2.
wa_bdc-dynpro = p_3.
wa_bdc-dynbegin = p_x.
append wa_bdc to ti_bdc.
else.
wa_bdc-fnam = p_2.
wa_bdc-fval = p_3.
append wa_bdc to ti_bdc.
endif.
clear wa_bdc.
endform. " F_LOAD_BDCDATA
*&---------------------------------------------------------------------*
*& Form NORMALIZA STRING
*& Remove caracteres indevidos de string
*&---------------------------------------------------------------------*
form normaliza_string using p_x.
translate p_x to upper case.
replace all occurrences of 'Ç' in p_x with 'C'.
endform.
form insere_imposto using
p_imp type ty_j1bnfstx
p_index type c.
data: lv_ftaxtyp(30) type c,
lv_ftaxval(30) type c,
lv_frate(30) type c,
lv_fothbas(30) type c,
lv_fbase(30) type c,
lv_fexcbas(30) type c,
lv_base type char21,
lv_taxval type char21,
lv_excbas type char21,
lv_rate type char08,
lv_othbas type char21,
wx_outise type i value 0.
if p_index eq '1'. "ICMS
"Trata outras.
if p_imp-cst eq 090. "Outras
p_imp-taxtyp = 'ICM0'.
p_imp-base = 0.
p_imp-rate = 0.
p_imp-taxval = 0.
p_imp-othbas = p_imp-total.
p_imp-excbas = 0.
wx_outise = 1.
endif.
"Trata isentas.
if p_imp-cst eq 040. "Isentas
p_imp-taxtyp = 'ICM0'.
p_imp-base = 0.
p_imp-rate = 0.
p_imp-taxval = 0.
p_imp-othbas = 0.
p_imp-excbas = p_imp-total.
wx_outise = 1.
endif.
if wx_outise eq 0.
if p_imp-base > 0 and p_imp-rate > 0.
p_imp-taxtyp = 'ICM1'. "Com crédito
else.
p_imp-taxtyp = 'ICM0'.
p_imp-base = 0.
p_imp-rate = 0.
p_imp-taxval = 0.
p_imp-othbas = 0.
p_imp-excbas = p_imp-total.
endif.
endif.
endif.
wx_outise = 0.
if p_index eq '2'. "IPI
"Trata outras.
if p_imp-cst eq 049 or p_imp-cst eq 099. "Outras
p_imp-taxtyp = 'IPI0'.
p_imp-base = 0.
p_imp-rate = 0.
p_imp-taxval = 0.
p_imp-othbas = p_imp-total.
p_imp-excbas = 0.
wx_outise = 1.
endif.
"Trata isentas.
if p_imp-cst eq 002 or p_imp-cst eq 052. "Isentas
p_imp-taxtyp = 'IPI0'.
p_imp-base = 0.
p_imp-rate = 0.
p_imp-taxval = 0.
p_imp-othbas = 0.
p_imp-excbas = p_imp-total.
wx_outise = 1.
endif.
if wx_outise eq 0.
if p_imp-base > 0 and p_imp-rate > 0.
p_imp-taxtyp = 'IPI1'. "Com crédito
else.
p_imp-taxtyp = 'IPI0'.
p_imp-base = 0.
p_imp-rate = 0.
p_imp-taxval = 0.
p_imp-othbas = 0.
p_imp-excbas = p_imp-total.
endif.
endif.
endif.
if p_index eq '3'. "PIS
if p_imp-base > 0 and p_imp-rate > 0.
p_imp-taxtyp = 'IPIS'.
else.
p_imp-taxtyp = 'ZPI1'.
endif.
"Isentas
if p_imp-cst eq 'P08' or p_imp-cst eq 'P70' or p_imp-cst eq 'P99'.
p_imp-base = 0.
p_imp-rate = 0.
p_imp-taxval = 0.
p_imp-othbas = 0.
p_imp-excbas = p_imp-total.
endif.
endif.
if p_index eq '4'. "COFINS
if p_imp-base > 0 and p_imp-rate > 0.
p_imp-taxtyp = 'ICOF'.
else.
p_imp-taxtyp = 'ZCO1'.
endif.
"Isentas
if p_imp-cst eq 'C08' or p_imp-cst eq 'C70' or p_imp-cst eq 'C99'.
p_imp-base = 0.
p_imp-rate = 0.
p_imp-taxval = 0.
p_imp-othbas = 0.
p_imp-excbas = p_imp-total.
endif.
endif.
write p_imp-base to lv_base decimals 2 .
write p_imp-rate to lv_rate decimals 2.
write p_imp-taxval to lv_taxval decimals 2 .
write p_imp-excbas to lv_excbas decimals 2 .
write p_imp-othbas to lv_othbas decimals 2 .
* replace all occurrences of '.' in lv_base with ','.
* replace all occurrences of '.' in lv_rate with ','.
* replace all occurrences of '.' in lv_taxval with ','.
* replace all occurrences of '.' in lv_excbas with ','.
* replace all occurrences of '.' in lv_othbas with ','.
concatenate 'J_1BDYSTX-TAXTYP(0' p_index ')' into lv_ftaxtyp.
concatenate 'J_1BDYSTX-BASE(0' p_index ')' into lv_fbase.
concatenate 'J_1BDYSTX-RATE(0' p_index ')' into lv_frate.
concatenate 'J_1BDYSTX-TAXVAL(0' p_index ')' into lv_ftaxval.
concatenate 'J_1BDYSTX-EXCBAS(0' p_index ')' into lv_fexcbas.
concatenate 'J_1BDYSTX-OTHBAS(0' p_index ')' into lv_fothbas.
perform f_load_bdcdata using:
'X' 'SAPLJ1BB2' '3000',
' ' lv_ftaxtyp p_imp-taxtyp,
' ' lv_fbase lv_base,
' ' lv_frate lv_rate,
' ' lv_ftaxval lv_taxval,
' ' lv_fexcbas lv_excbas,
' ' lv_fothbas lv_othbas.
"' ' 'BDC_OKCODE' '=ENTER'.
endform.