-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZ_J1B1_IMPORTA_CSV
599 lines (492 loc) · 18.4 KB
/
Z_J1B1_IMPORTA_CSV
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
*&---------------------------------------------------------------------*
*& Report Z_J1B1_IMPORTA_CSV
*& Eberton Sobreira - 09/12/2019
*&---------------------------------------------------------------------*
*& Exporta NF da Planilha XLS de Solicitação de NF Writer e carrega no
*& SAP.
*&---------------------------------------------------------------------*
report z_j1b1_importa_csv.
tables: j_1bnfdoc, j_1bnflin.
data: it_files type filetable,
wa_files type file_table,
v_rc type i.
types:
begin of ty_j1bnfdoc,
tp_reg(1) type c,
nftype type j_1bnfdoc-nftype,
bukrs type j_1bnfdoc-bukrs,
branch type j_1bnfdoc-branch,
parvw type j_1bnfdoc-parvw,
parid type j_1bnfdoc-parid,
"DOCDAT TYPE j_1bnfdoc-DOCDAT,
"OBSERVAT TYPE j_1bnfdoc-OBSERVAT,
"DOCREF TYPE j_1bnfdoc-DOCREF,
parvw_transp type j_1bnfdoc-parvw,
parid_transp type j_1bnfdoc-parid,
inco1 type j_1bnfdoc-inco1,
inco2 type j_1bnfdoc-inco2,
anzpk(5) type c, "j_1bnfdoc-ANZPK,
shpunt(3) type c, "j_1bnfdoc-SHPUNT,
ntgew(15) type c,
brgew(15) type c, "j_1bnfdoc-BRGEW,
gewei type j_1bnfdoc-gewei,
modfrete type j_1bnfdoc-modfrete,
end of ty_j1bnfdoc,
begin of ty_j1bdylin,
tp_reg(1) type c,
item(3) type c,
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(16) type c, "Quantidade
meins like j_1bdylin-meins, "Unidade de medida básica
netpr(16) type c, "Preço líquido
"netwr(15) type c, "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_j1bdylin,
begin of ty_j1bnfstx,
tp_reg(1) type c,
item(3) type c,
"itmnum like j_1bnfstx-itmnum,
taxtyp like j_1bnfstx-taxtyp,
base(20) type c, "j_1bnfstx-base,
rate(20) type c, "j_1bnfstx-rate,
taxval(20) type c, "j_1bnfstx-taxval,
excbas(20) type c, "j_1bnfstx-excbas,
othbas(20) type c, "j_1bnfstx-othbas,
col(3) type c, "Indica a coluna a ser salvo o valor.
end of ty_j1bnfstx,
begin of ty_j1bnfftx,
tp_reg(1) type c,
seqnum like j_1bnfftx-seqnum,
message like j_1bnfftx-message,
filler(10) type c, "para remover as vírgulas restantes.
end of ty_j1bnfftx.
*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.
"GUI_UPLOAD
data: lv_file type string.
data: qtd_msgs type i value 0.
"TABELA E WA PARA O GUI UPLOAD
types:begin of ty_arquivo,
linha type c length 500,
end of ty_arquivo.
data it_outdata type standard table of ty_arquivo with header line.
data wa_outdata type ty_arquivo.
selection-screen begin of block b1 with frame title text-t01.
parameter: p_file(1024) type c obligatory default 'C:\Users\sobebe01\Desktop\Solicitação NF Writer\TESTE.csv'.
selection-screen end of block b1.
selection-screen begin of block b2 with frame title text-t02.
parameters: p_observ type j_1bdydoc-observat,
p_ctitem type j_1bdylin-cod_cta.
selection-screen end of block b2.
at selection-screen on value-request for p_file.
********************* metodo para exibir o popup de seleção de arquivos
call method cl_gui_frontend_services=>file_open_dialog
changing
file_table = it_files
rc = v_rc
exceptions
file_open_dialog_failed = 1
cntl_error = 2
error_no_gui = 3
not_supported_by_gui = 4
others = 5.
if sy-subrc ne 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
else.
************************** identifica o arquivo selecionado e joga para o parametro de seleção
read table it_files into wa_files index 1.
p_file = wa_files-filename.
endif.
start-of-selection.
lv_file = p_file.
call function 'GUI_UPLOAD'
exporting
filename = lv_file
filetype = 'ASC'
has_field_separator = ' '
header_length = 0
read_by_line = 'X'
dat_mode = ' '
codepage = ' '
ignore_cerr = abap_true
replacement = '#'
check_bom = ' '
no_auth_check = ' '
"IMPORTING
" FILELENGTH =
" HEADER =
tables
data_tab = it_outdata
exceptions
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
others = 17.
if sy-subrc ne 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
if sy-subrc ne 0.
"WRITE: / v_codservico_sp, v_codservico_nac, v_fl_construtora.
write: / 'Erro'.
endif.
"Carrega os dados.
data tp_reg(1) type c.
data it_header type standard table of ty_j1bnfdoc with header line.
data wa_header type ty_j1bnfdoc.
data it_items type standard table of ty_j1bdylin with header line.
data wa_item type ty_j1bdylin.
data it_msgs type standard table of ty_j1bnfftx with header line.
data wa_msg type ty_j1bnfftx.
data it_taxes type standard table of ty_j1bnfstx with header line.
data wa_tax type ty_j1bnfstx.
"Lê os dados do arquivo carregado.
loop at it_outdata into wa_outdata.
tp_reg = wa_outdata(1).
if tp_reg eq '1'. "mestre
split wa_outdata at ',' into
wa_header-tp_reg
wa_header-nftype
wa_header-bukrs
wa_header-branch
wa_header-parvw
wa_header-parid
"wa_header-DOCDAT
"wa_header-OBSERVAT
wa_header-parvw_transp
wa_header-parid_transp
wa_header-inco1 "Incoterms parte 1
wa_header-inco2 "Incoterms parte 2
wa_header-anzpk "Número total dos volumes da remessa
wa_header-shpunt "Meio auxiliar de expedição (espécie)
wa_header-ntgew "Peso líquido
wa_header-brgew "Peso bruto
wa_header-gewei "Unidade de peso
wa_header-modfrete. "Modalidade do Frete
append wa_header to it_header.
elseif tp_reg eq '3'. "itens
split wa_outdata at ',' into
wa_item-tp_reg
wa_item-item
wa_item-itmtyp
wa_item-matnr
wa_item-werks
wa_item-maktx
wa_item-matkl
wa_item-menge
wa_item-meins
wa_item-netpr
"wa_item-netwr
wa_item-cfop
wa_item-taxlw1
wa_item-taxlw2
wa_item-matorg
wa_item-matuse
wa_item-nbm
wa_item-taxlw4
wa_item-taxlw5.
"wa_item-cod_cta.
append wa_item to it_items.
elseif tp_reg eq '2'. "mensagens
split wa_outdata at ',' into
wa_msg-tp_reg
wa_msg-message
wa_msg-filler.
append wa_msg to it_msgs.
"Aqui carrega msg em branco.
qtd_msgs = qtd_msgs + 1.
elseif tp_reg eq '4'. "impostos
"Carrega os impostos de cada item.
split wa_outdata at ',' into
wa_tax-tp_reg
wa_tax-item
wa_tax-taxtyp
wa_tax-base
wa_tax-rate
wa_tax-taxval
wa_tax-excbas
wa_tax-othbas
wa_tax-col.
append wa_tax to it_taxes.
endif.
endloop.
"Verificações dos dados carregados.
clear qtd_msgs.
loop at it_msgs into wa_msg.
if not wa_msg-message is initial.
qtd_msgs = qtd_msgs + 1.
endif.
endloop.
if qtd_msgs > 9.
message 'Excedida a qtde. de linhas de mensagem. Até 9 linhas nos dados adicionais da NF.' type 'I'.
stop.
endif.
"Carrega os dados mestre.
perform f_load_bdcdata using:
'X' 'SAPMJ1B1' '0900',
' ' 'J_1BDYDOC-NFTYPE' wa_header-nftype, "i0, i1, i8, etc...
' ' 'J_1BDYDOC-BUKRS' wa_header-bukrs, "Empresa
' ' 'J_1BDYDOC-BRANCH' wa_header-branch, "Local de negócios
' ' 'J_1BDYDOC-PARVW' wa_header-parvw,
' ' 'J_1BDYDOC-PARID' wa_header-parid,
' ' 'J_1BDYLIN-INCLTX' 'X'.
perform f_load_bdcdata using:
'X' 'SAPLJ1BB2' '2000',
"' ' 'J_1BDYDOC-DOCDAT' wa_header-docdat,
"' ' 'J_1BDYDOC-OBSERVAT' wa_header-OBSERVAT,
' ' 'J_1BDYDOC-OBSERVAT' p_observ,
' ' 'BDC_OKCODE' '=TAB3'. "Vai para Parceiros
perform f_load_bdcdata using:
'X' 'SAPLJ1BB2' '2000',
' ' 'J_1BDYNAD-PARVW(01)' wa_header-parvw,
' ' 'J_1BDYNAD-PARID(01)' wa_header-parid,
' ' 'J_1BDYNAD-PARVW(02)' wa_header-parvw_transp,
' ' 'J_1BDYNAD-PARID(02)' wa_header-parid_transp,
' ' 'BDC_OKCODE' '=TAB5'. "Vai para Transporte
replace all occurrences of '.' in wa_header-ntgew with ','.
replace all occurrences of '.' in wa_header-brgew with ','.
condense wa_header-inco1.
"SAP
"0 - remetente CIF
"1 - destinatário FOB
* IF wa_header-INCO1 EQ 'CIF'.
* MODFRETE = '0'.
* ELSE.
* MODFRETE = '1'.
* ENDIF.
perform f_load_bdcdata using:
'X' 'SAPLJ1BB2' '2000',
' ' 'J_1BDYDOC-INCO1' wa_header-inco1,
' ' 'J_1BDYDOC-INCO2' wa_header-inco2,
' ' 'J_1BDYDOC-ANZPK' wa_header-anzpk, "Qtde
' ' 'J_1BDYDOC-SHPUNT' wa_header-shpunt, "Volume
' ' 'J_1BDYDOC-NTGEW' wa_header-ntgew, "Peso Líquido
' ' 'J_1BDYDOC-BRGEW' wa_header-brgew, "Peso Bruto
' ' 'J_1BDYDOC-GEWEI' wa_header-gewei, "Unidade de Peso
"' ' 'J_1BDYDOC-MODFRETE' wa_header-modfrete,
' ' 'BDC_OKCODE' '=ENTER'.
perform f_load_bdcdata using:
'X' 'SAPLJ1BB2' '2000',
' ' 'J_1BDYDOC-MODFRETE' wa_header-modfrete,
' ' 'BDC_OKCODE' '=TAB1'. "Vai pra Tela Inicial
perform f_load_bdcdata using:
'X' 'SAPLJ1BB2' '2000',
' ' 'BDC_OKCODE' '=ADIT'. "Vai pra Tela Inicial
data idx_imp type n.
data curitem type i.
data qtde type i.
data qtdeitensvalidos type i value 0.
"Conta qtde de itens válidos.
loop at it_items into wa_item.
clear qtde.
move wa_item-menge to qtde.
if qtde > 0.
qtdeitensvalidos = qtdeitensvalidos + 1.
endif.
endloop.
"Vai para Itens (itens define mensagens automáticas)
loop at it_items into wa_item.
clear idx_imp.
idx_imp = 1.
"Processa o item apenas se tiver quantidade;
clear qtde.
move wa_item-menge to qtde.
if qtde > 0.
curitem = curitem + 1.
"Troca vírgulas por pontos.
replace all occurrences of '.' in wa_item-netpr with ','.
perform f_load_bdcdata using:
'X' 'SAPLJ1BB2' '3000',
' ' 'J_1BDYLIN-ITMTYP' wa_item-itmtyp,
' ' 'J_1BDYLIN-MATNR' wa_item-matnr,
' ' 'J_1BDYLIN-WERKS' wa_item-werks,
' ' 'J_1BDYLIN-MAKTX' wa_item-maktx,
' ' 'J_1BDYLIN-MATKL' wa_item-matkl,
' ' 'J_1BDYLIN-MENGE' wa_item-menge, "Qtde
' ' 'J_1BDYLIN-MEINS' wa_item-meins, "Unid Med
' ' 'J_1BDYLIN-NETPR' wa_item-netpr, "Valor Unitário
' ' 'J_1BDYLIN-CFOP' wa_item-cfop,
' ' 'J_1BDYLIN-TAXLW1' wa_item-taxlw1, "Direitos ICMS
' ' 'J_1BDYLIN-TAXLW2' wa_item-taxlw2, "Direitos IPI
' ' 'J_1BDYLIN-MATORG' wa_item-matorg,
' ' 'J_1BDYLIN-MATUSE' wa_item-matuse,
' ' 'J_1BDYLIN-NBM' wa_item-nbm,
' ' 'J_1BDYLIN-TAXLW4' wa_item-taxlw4, "Direitos COFINS
' ' 'J_1BDYLIN-TAXLW5' wa_item-taxlw5, "Direitos PIS
"' ' 'J_1BDYLIN-DOCREF' wa_docref, "Nr Doc original
"' ' 'J_1BDYLIN-ITMREF' wa_itmnum, "Item Doc Original
"' ' 'J_1BDYLIN-COD_CTA' wa_item-cod_cta,
' ' 'J_1BDYLIN-COD_CTA' p_ctitem, "Cta Analítica Item
' ' 'BDC_OKCODE' '=TAX'.
loop at it_taxes into wa_tax.
if wa_item-item eq wa_tax-item.
perform insere_imposto using wa_tax idx_imp.
idx_imp = idx_imp + 1.
endif.
endloop.
if curitem ne qtdeitensvalidos.
"Adiciona novo item.
perform f_load_bdcdata using:
'X' 'SAPLJ1BB2' '2000',
' ' 'BDC_OKCODE' '=ADIT'.
endif.
endif.
endloop.
"Vai para Mensagens
if qtd_msgs > 0.
perform f_load_bdcdata using:
' ' 'BDC_OKCODE' '/00',
'X' 'SAPLJ1BB2' '3000',
' ' 'BDC_OKCODE' '=BACK',
'X' 'SAPLJ1BB2' '2000',
' ' 'BDC_OKCODE' '=TAB4'.
data idx_msg type n value 0.
loop at it_msgs into wa_msg.
if not wa_msg-message is initial.
idx_msg = idx_msg + 1.
perform insere_mensagem using wa_msg idx_msg.
endif.
endloop.
"vai pro início.
perform f_load_bdcdata using:
'X' 'SAPLJ1BB2' '2000',
' ' 'BDC_OKCODE' '=TAB1'.
else.
"sem mensagens; vai pro início.
perform finaliza_impostos.
endif.
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.
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 insere_mensagem using p_msg type ty_j1bnfftx
p_index.
data: lv_message type char21.
concatenate 'J_1BDYFTX-MESSAGE(' p_index ')' into lv_message.
perform f_load_bdcdata using:
'X' 'SAPLJ1BB2' '2000',
' ' lv_message p_msg-message.
endform.
form insere_imposto using p_imp type ty_j1bnfstx
p_index.
data: lv_ftaxtyp type fnam_____4,
lv_ftaxval type fnam_____4,
lv_frate type fnam_____4,
lv_fothbas type fnam_____4,
lv_fbase type fnam_____4,
lv_fexcbas type fnam_____4,
lv_base type char21,
lv_taxval type char21,
lv_excbas type char21,
lv_rate type char08,
lv_othbas type char21.
write p_imp-base to lv_base .
write p_imp-rate to lv_rate .
write p_imp-taxval to lv_taxval .
write p_imp-excbas to lv_excbas .
write p_imp-othbas to lv_othbas .
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(' p_index ')' into lv_ftaxtyp.
concatenate 'J_1BDYSTX-BASE(' p_index ')' into lv_fbase.
concatenate 'J_1BDYSTX-RATE(' p_index ')' into lv_frate.
concatenate 'J_1BDYSTX-TAXVAL(' p_index ')' into lv_ftaxval.
concatenate 'J_1BDYSTX-EXCBAS(' p_index ')' into lv_fexcbas.
concatenate 'J_1BDYSTX-OTHBAS(' p_index ')' into lv_fothbas.
if p_index eq 4.
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' '=TAB1'.
else.
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.
endif.
endform.
form finaliza_impostos.
perform f_load_bdcdata using:
' ' 'BDC_OKCODE' '/00',
'X' 'SAPLJ1BB2' '3000',
' ' 'BDC_OKCODE' '=BACK',
'X' 'SAPLJ1BB2' '2000'.
endform.