*&---------------------------------------------------------------------*
*& Report /XEGR/RWS_EASY
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
report /xegr/rws_easy.


"-->POOL-------------------------------------------------------ʕ•ᴥ•ʔ"
type-pools : slis.

types: begin of ty_ylog,
taxjurcode type /xegr/tegrprefp-taxjurcode,
stcd1 type stcd1,
status type char4,
nf type char15,
message type string,
xstring_header type xstring,
xstring_body type xstring,
xstring_return type xstring,
xml_header type char4,
xml_body type char4,
xml_return type char4,
end of ty_ylog.

data: gt_ylog type standard table of ty_ylog,
gw_ylog type ty_ylog.

data: g_bedat type sy-datum.

data: gt_fieldcatalog type slis_t_fieldcat_alv with header line,
gd_layout type slis_layout_alv,
gd_repid like sy-repid,
g_exit type c.
"-->POOL-------------------------------------------------------ʕ•ᴥ•ʔ"


class /xegr/clws_easy_local definition.

public section.

methods execute
importing
!io_bundle type ref to /xegr/clegr_bundle .

types:
begin of ty_yelements,
name type string,
ancestral type string,
elemnt type ref to if_ixml_element,
end of ty_yelements .

data: mo_request_builder type ref to /xegr/clegr_ws_nfse_req_build,
mw_nfse_process type /xegr/tegrprefp,
mw_tegrmdcfg type /xegr/tegrmdcfg,
mt_yelements type standard table of ty_yelements,
mo_ixml type ref to if_ixml,
m_xstring_body type xstring,
m_xstring_header type xstring,
m_xstring_return type xstring,
mo_document type ref to if_ixml_document,
m_xstring type xstring,
mo_xml type ref to cl_xml_document.

class-methods: read_node importing i_key type string
io_xml type ref to cl_xml_document
returning value(r_value) type string.

methods: select_some_data.
methods sign_with_certificate .
methods send_request .
methods add_new_node
importing
!i_nam type any
!i_val type any
!i_anc type any
!i_att type any .
methods get_webservice_request .
methods handle_return .
methods get_certificate
returning
value(ro_certificate) type ref to /xegr/clegr_ws_certificate .
methods get_standard
returning
value(ro_xml) type ref to cl_xml_document .
methods standardise_xml
changing
!co_xml type ref to cl_xml_document .
methods input_to_egr
importing
!io_input type ref to /xegr/ifegr_input_xml
returning
value(rw_txmlk) type /xegr/txmlk .
private section.

data mw_ws_setup type /xegr/tegrwspre .
data mo_bundle type ref to /xegr/clegr_bundle .

methods generate_output_xml
importing
!io_document type ref to if_ixml_document
returning
value(r_xstring) type xstring .
methods sign_request .
endclass.

data: lo_ws_easy type ref to /xegr/clws_easy_local.

selection-screen begin of block b1 with frame title g_b1.
selection-screen begin of line.
selection-screen comment 1(20) g_com1 for field p_txcjd.
parameters: p_txcjd type /xegr/tegrprefp-taxjurcode.
selection-screen end of line.
selection-screen begin of line.
selection-screen comment 1(17) g_dats for field s_date.
select-options s_date for g_bedat.
selection-screen end of line.
selection-screen begin of line.
selection-screen comment 1(20) g_rep for field p_rep.
parameters: p_rep as checkbox.
selection-screen end of line.
selection-screen end of block b1.

initialization.
g_b1 = 'Restrições'.
g_com1 = 'CodDomFiscal'.
g_dats = 'Data de Busca'.
g_rep = 'Gerar Report'.

at line-selection.

break-point.

start-of-selection.

perform start_ws.

end-of-selection.

if p_rep = 'X'.
perform show_report.
endif.


*&---------------------------------------------------------------------*
*& Form SHOW_REPORT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form show_report.

"-->POOL-------------------------------------------------------ʕ•ᴥ•ʔ"
data: lo_xml type ref to cl_xml_document,
l_string type string.
"-->POOL-------------------------------------------------------ʕ•ᴥ•ʔ"

check p_rep = 'X'.

loop at gt_ylog into gw_ylog.

case gw_ylog-status.
when 'E'.
gw_ylog-status = '@02@'.
when others.
gw_ylog-status = '@01@'.
endcase.


if gw_ylog-xstring_body is not initial.
gw_ylog-xml_body = '@R4@'.
endif.


if gw_ylog-xstring_header is not initial.
gw_ylog-xml_header = '@R4@'.
endif.


if gw_ylog-xstring_return is not initial.
gw_ylog-xml_return = '@R4@'.
endif.

"-->Caso sucesso, ler a tag NF
create object lo_xml.
lo_xml->parse_xstring( stream = gw_ylog-xstring_return ).

case gw_ylog-taxjurcode(2).
when 'SP'.

call method /xegr/clws_easy_local=>read_node
exporting
i_key = 'ChaveNFe.NumeroNFe'
io_xml = lo_xml
receiving
r_value = l_string.

when others.
call method /xegr/clws_easy_local=>read_node
exporting
i_key = 'Numero'
io_xml = lo_xml
receiving
r_value = l_string.

endcase.

gw_ylog-nf = l_string.

modify gt_ylog from gw_ylog.

endloop.

perform build_gt_fieldcatalog.
perform display_alv_report.


endform.
form build_gt_fieldcatalog .


clear gt_fieldcatalog.
gt_fieldcatalog-fieldname = 'TAXJURCODE'.
gt_fieldcatalog-seltext_m = 'taxjurcode'.
gt_fieldcatalog-col_pos = 0.
append gt_fieldcatalog to gt_fieldcatalog.
clear gt_fieldcatalog.

clear gt_fieldcatalog.
gt_fieldcatalog-fieldname = 'STCD1'.
gt_fieldcatalog-seltext_m = 'stcd1'.
gt_fieldcatalog-col_pos = 0.
append gt_fieldcatalog to gt_fieldcatalog.
clear gt_fieldcatalog.


clear gt_fieldcatalog.
gt_fieldcatalog-fieldname = 'STATUS'.
gt_fieldcatalog-seltext_m = 'Status'.
gt_fieldcatalog-col_pos = 0.
append gt_fieldcatalog to gt_fieldcatalog.
clear gt_fieldcatalog.
gt_fieldcatalog-fieldname = 'MESSAGE'.
gt_fieldcatalog-seltext_m = 'Message'.
gt_fieldcatalog-col_pos = 1.
append gt_fieldcatalog to gt_fieldcatalog.
clear gt_fieldcatalog.
gt_fieldcatalog-fieldname = 'XML_HEADER'.
gt_fieldcatalog-seltext_m = 'XML Header'.
gt_fieldcatalog-col_pos = 2.
gt_fieldcatalog-outputlen = 4.
append gt_fieldcatalog to gt_fieldcatalog.
clear gt_fieldcatalog.
gt_fieldcatalog-fieldname = 'XML_BODY'.
gt_fieldcatalog-seltext_m = 'XML Body'.
gt_fieldcatalog-col_pos = 3.
gt_fieldcatalog-outputlen = 4.
append gt_fieldcatalog to gt_fieldcatalog.
clear gt_fieldcatalog.
gt_fieldcatalog-fieldname = 'XML_RETURN'.
gt_fieldcatalog-seltext_m = 'XML Return'.
gt_fieldcatalog-col_pos = 3.
gt_fieldcatalog-outputlen = 4.
append gt_fieldcatalog to gt_fieldcatalog.
clear gt_fieldcatalog.
gt_fieldcatalog-fieldname = 'NF'.
gt_fieldcatalog-seltext_m = 'Nf'.
gt_fieldcatalog-col_pos = 4.
gt_fieldcatalog-outputlen = 15.
append gt_fieldcatalog to gt_fieldcatalog.
clear gt_fieldcatalog.
endform. " BUILD_GT_fieldcatalog

form display_alv_report .
gd_repid = sy-repid.
call function 'REUSE_ALV_GRID_DISPLAY'
exporting
i_callback_program = gd_repid
i_callback_top_of_page = 'TOP-OF-PAGE' "see FORM
i_callback_user_command = 'USER_COMMAND'
it_fieldcat = gt_fieldcatalog[]
i_save = 'X'
tables
t_outtab = gt_ylog
exceptions
program_error = 1
others = 2.
if sy-subrc <> 0.

endif.
endform. "DISPLAY_ALV_REPORT

form top-of-page.
*ALV Header declarations
data: t_header type slis_t_listheader,
wa_header type slis_listheader,
t_line like wa_header-info,
ld_lines type i,
ld_linesc(10) type c.
* Title
wa_header-typ = 'H'.
wa_header-info = 'Log de Processamento do WS'.
append wa_header to t_header.
clear wa_header.
* Date
wa_header-typ = 'S'.
wa_header-key = 'Date: '.
concatenate sy-datum+6(2) '.'
sy-datum+4(2) '.'
sy-datum(4) into wa_header-info.
append wa_header to t_header.
clear: wa_header.
call function 'REUSE_ALV_COMMENTARY_WRITE'
exporting
it_list_commentary = t_header.
endform. "top-of-page

form user_command using r_ucomm
ls_selfield type slis_selfield.

clear gw_ylog.
read table gt_ylog into gw_ylog index ls_selfield-tabindex.

case ls_selfield-fieldname.
when 'XML_BODY'.

if gw_ylog-xstring_body is initial.
message 'XML vazio' type 'S'.
return.
endif.

call function 'DISPLAY_XML_STRING'
exporting
xml_string = gw_ylog-xstring_body
title = 'Body XML'
starting_x = 5
starting_y = 5
exceptions
no_xml_document = 1
others = 2.

when 'XML_HEADER'.

if gw_ylog-xstring_header is initial.
message 'XML vazio' type 'S'.
return.
endif.

call function 'DISPLAY_XML_STRING'
exporting
xml_string = gw_ylog-xstring_header
title = 'Header XML'
starting_x = 5
starting_y = 5
exceptions
no_xml_document = 1
others = 2.

when 'XML_RETURN'.

if gw_ylog-xstring_return is initial.
message 'XML vazio' type 'S'.
return.
endif.

call function 'DISPLAY_XML_STRING'
exporting
xml_string = gw_ylog-xstring_return
title = 'Return XML'
starting_x = 5
starting_y = 5
exceptions
no_xml_document = 1
others = 2.

endcase.

endform.


*&---------------------------------------------------------------------*
*& Form START_WS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form start_ws.

"-->POOL-------------------------------------------------------ʕ•ᴥ•ʔ"
data: lt_tegrmdcfg type table of /xegr/tegrmdcfg,
lw_tegrmdcfg like line of lt_tegrmdcfg,
lt_nfse_process type table of /xegr/tegrprefp,
lw_nfse_process like line of lt_nfse_process,
lo_process type ref to /xegr/ifegr_process,
lo_city_process type ref to /xegr/clegr_city_process,
lo_root type ref to cx_root,
l_stcd1 type lfa1-stcd1,
l_prestador type lfa1-stcd3,
w_ws_setup type /xegr/tegrwspre,
t_t001w type standard table of t001w,
w_t001w type t001w,
lo_bundle type ref to /xegr/clegr_bundle.

types: begin of ty_yprestadores,
stcd1 type lfa1-stcd1,
lifnr type lfa1-lifnr,
end of ty_yprestadores.

types: begin of ty_ypedidos,
ebeln type ekko-ebeln,
ebelp type ekpo-ebelp,
lifnr type ekko-lifnr,
loekz type ekpo-loekz,
elikz type ekpo-elikz,
end of ty_ypedidos.


data: t_yprestadores type standard table of ty_yprestadores,
w_yprestadores type ty_yprestadores.

data: t_ypedidos type standard table of ty_ypedidos,
w_ypedidos type ty_ypedidos.

data: mw_ws_setup type /xegr/tegrwspre.

"-->POOL-------------------------------------------------------ʕ•ᴥ•ʔ"

call method /xegr/cltracer=>start_trace( 'Iniciando DOWNLOAD_XMLS' ).

"-->POOL-------------------------------------------------------ʕ•ᴥ•ʔ"
* Select All NFSE Inbound Process available
* Select all companies registered at EGR
"-->POOL-------------------------------------------------------ʕ•ᴥ•ʔ"
select * from /xegr/tegrmdcfg into table lt_tegrmdcfg.
select * from /xegr/tegrprefp into table lt_nfse_process.
"-->POOL-------------------------------------------------------ʕ•ᴥ•ʔ"
* For each NFSE Inbound Process, execute it for each branch
"-->POOL-------------------------------------------------------ʕ•ᴥ•ʔ"

"-->Modificação Inicio Bertani 04.04.2017 15:35:49 EGR-1035
if lt_nfse_process[] is initial or lt_tegrmdcfg[] is initial.
call method /xegr/cltracer=>set_trace
exporting
i_text = `Configuração Vazia!`
i_msgtp = 'E'
io_obj1 = lt_tegrmdcfg
io_obj2 = lt_nfse_process.
endif.
"-->Modificação FIM Bertani

create object lo_city_process.

loop at lt_nfse_process into lw_nfse_process.

if p_txcjd is not initial.
check p_txcjd = lw_nfse_process-taxjurcode.
endif.

free lo_process.
try .
lo_process = lo_city_process->find_process(
i_country = lw_nfse_process-country
i_taxjurcode = lw_nfse_process-taxjurcode
i_origin = 'J' "JOB
).
catch /xegr/cx_egr_city_process.
call method /xegr/cltracer=>set_trace
exporting
i_text = `/xegr/cx_egr_city_process`
i_msgtp = 'E'
io_obj1 = lw_nfse_process.
continue.
endtry.

check lo_process is not initial.

loop at lt_tegrmdcfg into lw_tegrmdcfg.

"-->Verificação para saber se é necessário passar por todos os Prestadores
clear l_stcd1.
/xegr/clgr_tools=>get_branch_fiscal_data(
exporting
i_bukrs = lw_tegrmdcfg-bukrs
i_branch = lw_tegrmdcfg-branc
importing
e_stcd1 = l_stcd1 ).

select single *
from /xegr/tegrwspre
into w_ws_setup
where country = lw_nfse_process-country
and taxjurcode = lw_nfse_process-taxjurcode.

if w_ws_setup-loop_prestador = 'X'.
refresh t_yprestadores[].
select lfa1~stcd1 lfa1~lifnr
from lfbw
inner join lfa1
on lfa1~lifnr = lfbw~lifnr
into corresponding fields of table t_yprestadores
where lfa1~txjcd = lw_nfse_process-taxjurcode.

if sy-subrc <> 0.
continue.
endif.

sort t_yprestadores by stcd1.
delete adjacent duplicates from t_yprestadores comparing stcd1.

if not t_yprestadores[] is initial.

g_bedat = sy-datum - 300.

select * from ekko inner join ekpo
on ekko~ebeln = ekpo~ebeln
into corresponding fields of table t_ypedidos
for all entries in t_yprestadores
where ekko~lifnr = t_yprestadores-lifnr
and ekko~bedat >= g_bedat.

delete t_ypedidos where ( loekz <> space or elikz <> space ).

sort t_ypedidos by lifnr ascending.
delete adjacent duplicates from t_ypedidos comparing lifnr.

loop at t_yprestadores into w_yprestadores.

read table t_ypedidos with key lifnr = w_yprestadores-lifnr
transporting no fields binary search.

if sy-subrc <> 0.
delete t_yprestadores.
endif.

endloop.

endif.

else.
append l_stcd1 to t_yprestadores. "-->Neste caso ele apenas irá dar o loop
endif.

select single *
from /xegr/tegrwspre
into mw_ws_setup
where country = lw_nfse_process-country
and taxjurcode = lw_nfse_process-taxjurcode.

loop at t_yprestadores into w_yprestadores.

create object lo_bundle.

lo_bundle->add(
exporting
i_varname = 'tegrmdcfg'
i_data = lw_tegrmdcfg
).

lo_bundle->add(
exporting
i_varname = 'tegrprefp'
i_data = lw_nfse_process
).

lo_bundle->add(
exporting
i_varname = 'prestador'
i_data = w_yprestadores-stcd1
).

create object lo_ws_easy.

try .
clear gw_ylog.
gw_ylog-stcd1 = w_yprestadores-stcd1.
gw_ylog-taxjurcode = lw_nfse_process-taxjurcode.
call method lo_ws_easy->execute( lo_bundle ).
catch cx_root into lo_root.

gw_ylog-status = 'E'.
gw_ylog-message = `Exception no WS NFSe`.

call method /xegr/cltracer=>set_trace
exporting
i_text = `Exception no WS NFSe`
i_msgtp = 'E'
io_obj1 = lo_root.
endtry.

if p_rep = 'X'.
gw_ylog-xstring_header = lo_ws_easy->m_xstring_header.
gw_ylog-xstring_body = lo_ws_easy->m_xstring_body.
gw_ylog-xstring_return = lo_ws_easy->m_xstring_return.
append gw_ylog to gt_ylog.
endif.

free lo_ws_easy.
clear lo_ws_easy.

endloop.

endloop.

endloop.

endform.


class /xegr/clws_easy_local implementation.

method add_new_node.

"-->POOL-------------------------------------------------------ʕ•ᴥ•ʔ"
data: w_yelements type ty_yelements,
l_value type string.
field-symbols type ty_yelements.
"-->POOL-------------------------------------------------------ʕ•ᴥ•ʔ"

l_value = i_val.

if i_anc is initial.

if i_att is initial.
w_yelements-name = i_nam.
w_yelements-elemnt = mo_document->create_simple_element(
name = w_yelements-name
value = l_value
parent = mo_document ).
else.
w_yelements-name = i_nam.
w_yelements-elemnt = mo_document->create_simple_element(
name = w_yelements-name
parent = mo_document ).
endif.

else.

unassign .
read table mt_yelements assigning with key name = i_anc.


if i_att is initial.
w_yelements-name = i_nam.
w_yelements-elemnt = mo_document->create_simple_element(
name = w_yelements-name
value = l_value
parent = -elemnt ).
else.
w_yelements-name = i_nam.
w_yelements-elemnt = mo_document->create_simple_element(
name = w_yelements-name
parent = -elemnt ).
endif.
endif.

if i_att is not initial.

call method w_yelements-elemnt->set_attribute
exporting
name = i_att
value = l_value.

endif.

w_yelements-ancestral = i_anc.
insert w_yelements into mt_yelements index 1.

endmethod.

method execute.

mo_bundle = io_bundle.

select_some_data( ).

get_webservice_request( ).

sign_request( ).

send_request( ).

handle_return( ).

endmethod.

method generate_output_xml.

"-->POOL-------------------------------------------------------ʕ•ᴥ•ʔ"
data:
lo_streamfactory type ref to if_ixml_stream_factory,
lo_ostream type ref to if_ixml_ostream,
lo_renderer type ref to if_ixml_renderer,
lv_rc type i,
lv_xml_size type i.

data: s_encoding_type type string.
data: encoding type ref to if_ixml_encoding.
"-->POOL-------------------------------------------------------ʕ•ᴥ•ʔ"

lo_streamfactory = mo_ixml->create_stream_factory( ).
lo_ostream = lo_streamfactory->create_ostream_xstring( string = m_xstring ).
lo_renderer = mo_ixml->create_renderer( ostream = lo_ostream
document = io_document ).
* lo_ostream->set_pretty_print( 'X' ).
lo_ostream->set_pretty_print( '' ).

s_encoding_type = 'UTF-8'.
encoding = mo_ixml->create_encoding( character_set = s_encoding_type
byte_order = 0 ).

call method lo_ostream->set_encoding
exporting
encoding = encoding.

lv_rc = lo_renderer->render( ).

lv_xml_size = lo_ostream->get_num_written_raw( ).

r_xstring = m_xstring.

endmethod.

method get_certificate.
data: l_bukrs type bukrs.

l_bukrs = mw_tegrmdcfg-bukrs.

ro_certificate = /xegr/clegr_ws_certificate=>factory(
i_certificate_source = mw_ws_setup-signature_method
i_bukrs = l_bukrs
i_ssfappl = 'DFAULT'
).

endmethod.


method get_standard.
data: lw_tschemep type /xegr/tschemep,
lt_cluster type standard table of indx_clust,
l_xstring type xstring,
l_size type i.

import file = lt_cluster from database /xegr/tschemep(it) to lw_tschemep id mw_ws_setup-layout_xml_return.
check sy-subrc eq 0.
l_size = lw_tschemep-filesize.

call function 'SCMS_BINARY_TO_XSTRING'
exporting
input_length = l_size
importing
buffer = l_xstring
tables
binary_tab = lt_cluster.


create object ro_xml.

ro_xml->parse_xstring( l_xstring ).

endmethod.

* +--------------------------------------------------------------------------------------
method get_webservice_request.

"-->POOL-------------------------------------------------------ʕ•ᴥ•ʔ"
data: l_prestador type string,
l_stcd1 type stcd1,
l_stcd3 type stcd3,
l_today type char20,
l_yesterday type char20,
l_datum type sy-datum.
"-->POOL-------------------------------------------------------ʕ•ᴥ•ʔ"

"-->Dados Gerais
/xegr/clgr_tools=>get_branch_fiscal_data(
exporting
i_bukrs = mw_tegrmdcfg-bukrs
i_branch = mw_tegrmdcfg-branc
importing
e_stcd3 = l_stcd3
e_stcd1 = l_stcd1 ).
l_today = sy-datum.

concatenate l_today(4) l_today+4(2) l_today+6(2) into l_today separated by '-'.

l_datum = sy-datum - 1.
l_yesterday = l_datum.
condense l_yesterday.

concatenate l_yesterday(4) l_yesterday+4(2) l_yesterday+6(2) into l_yesterday separated by '-'.
"-->Dados Gerais


case mw_nfse_process-taxjurcode(2).
when 'MG'.

*
*
*
* 99999999000191
* 1733160024
*

*
* 2009-09-28
* 2009-09-28
*

*
*
* 99999999000191
*

* 1733160032
*

*


" Create iXML object
mo_ixml = cl_ixml=>create( ).
mo_document = mo_ixml->create_document( ).

call method me->add_new_node
exporting
i_nam = 'ConsultarNfseEnvio'
i_val = 'http://www.abrasf.org.br/nfse.xsd'
i_anc = ''
i_att = 'xmlns'.

call method mo_bundle->get
exporting
i_varname = 'prestador'
changing
c_data = l_prestador.

call method me->add_new_node
exporting
i_nam = 'Prestador'
i_val = ''
i_anc = 'ConsultarNfseEnvio'
i_att = ''.



call method me->add_new_node
exporting
i_nam = 'InscricaoMunicipal'
i_val = l_stcd3
i_anc = 'Prestador'
i_att = ''.


call method me->add_new_node
exporting
i_nam = 'Cnpj'
i_val = '24027450000136' " l_prestador
i_anc = 'Prestador'
i_att = ''.

call method me->add_new_node
exporting
i_nam = 'PeriodoEmissao'
i_val = ''
i_anc = 'ConsultarNfseEnvio'
i_att = ''.

call method me->add_new_node
exporting
i_nam = 'DataInicial'
i_val = l_today
i_anc = 'PeriodoEmissao'
i_att = ''.

call method me->add_new_node
exporting
i_nam = 'DataFinal'
i_val = l_yesterday
i_anc = 'PeriodoEmissao'
i_att = ''.


call method me->add_new_node
exporting
i_nam = 'Tomador'
i_val = ''
i_anc = 'ConsultarNfseEnvio'
i_att = ''.


call method me->add_new_node
exporting
i_nam = 'CpfCnpj'
i_val = ''
i_anc = 'Tomador'
i_att = ''.


call method me->add_new_node
exporting
i_nam = 'Cnpj'
i_val = '02359572000430' "l_stcd1
i_anc = 'CpfCnpj'
i_att = ''.

m_xstring_body = generate_output_xml( mo_document ).

when 'SP'.


*
*
*
*
* 02359572000278
*

*
* 02359572000278
*

*
* 2017-04-01
* 20170804
* 1
*

*


" Create iXML object
mo_ixml = cl_ixml=>create( ).
mo_document = mo_ixml->create_document( ).

call method me->add_new_node
exporting
i_nam = 'p1:PedidoConsultaNFePeriodo'
i_val = 'http://www.prefeitura.sp.gov.br/nfe'
i_anc = ''
i_att = 'xmlns:p1'.

call method me->add_new_node
exporting
i_nam = 'Cabecalho'
i_val = '1'
i_anc = 'p1:PedidoConsultaNFePeriodo'
i_att = 'Versao'.

call method me->add_new_node
exporting
i_nam = 'CPFCNPJRemetente'
i_val = ''
i_anc = 'Cabecalho'
i_att = ''.

call method mo_bundle->get
exporting
i_varname = 'prestador'
changing
c_data = l_prestador.

call method me->add_new_node
exporting
i_nam = 'CNPJ'
i_val = l_prestador
i_anc = 'CPFCNPJRemetente'
i_att = ''.

call method me->add_new_node
exporting
i_nam = 'CPFCNPJ'
i_val = ''
i_anc = 'Cabecalho'
i_att = ''.

call method me->add_new_node
exporting
i_nam = 'CNPJ'
i_val = l_stcd1
i_anc = 'CPFCNPJ'
i_att = ''.


if s_date is initial.
l_today = sy-datum.


l_datum = sy-datum - 1.
l_yesterday = l_datum.
condense l_yesterday.

else.

l_yesterday = s_date-low.
l_today = s_date-high.

endif.

concatenate l_today(4) l_today+4(2) l_today+6(2) into l_today separated by '-'.

concatenate l_yesterday(4) l_yesterday+4(2) l_yesterday+6(2) into l_yesterday separated by '-'.


call method me->add_new_node
exporting
i_nam = 'dtInicio'
i_val = l_yesterday
i_anc = 'Cabecalho'
i_att = ''.

call method me->add_new_node
exporting
i_nam = 'dtFim'
i_val = l_today
i_anc = 'Cabecalho'
i_att = ''.

call method me->add_new_node
exporting
i_nam = 'NumeroPagina'
i_val = '1'
i_anc = 'Cabecalho'
i_att = ''.

m_xstring_body = generate_output_xml( mo_document ).

when 'RJ'.

*
*
*
* 28672087000162
*

*
* 2017-04-01
* 20170804
*

*
*
*
*

*

*


" Create iXML object
mo_ixml = cl_ixml=>create( ).
mo_document = mo_ixml->create_document( ).

call method me->add_new_node
exporting
i_nam = 'ConsultarNfseEnvio'
i_val = 'http://www.abrasf.org.br/ABRASF/arquivos/nfse.xsd'
i_anc = ''
i_att = 'xmlns'.

call method me->add_new_node
exporting
i_nam = 'Prestador'
i_val = ''
i_anc = 'ConsultarNfseEnvio'
i_att = ''.

call method me->add_new_node
exporting
i_nam = 'Cnpj'
i_val = l_stcd1
i_anc = 'Prestador'
i_att = ''.


call method me->add_new_node
exporting
i_nam = 'PeriodoEmissao'
i_val = ''
i_anc = 'ConsultarNfseEnvio'
i_att = ''.


call method me->add_new_node
exporting
i_nam = 'DataInicial'
i_val = l_yesterday
i_anc = 'PeriodoEmissao'
i_att = ''.

call method me->add_new_node
exporting
i_nam = 'DataFinal'
i_val = l_today
i_anc = 'PeriodoEmissao'
i_att = ''.

call method me->add_new_node
exporting
i_nam = 'Tomador'
i_val = ''
i_anc = 'ConsultarNfseEnvio'
i_att = ''.

call method me->add_new_node
exporting
i_nam = 'CpfCnpj'
i_val = ''
i_anc = 'Tomador'
i_att = ''.

call method me->add_new_node
exporting
i_nam = 'Cnpj'
i_val = l_stcd1
i_anc = 'CpfCnpj'
i_att = ''.


m_xstring_body = generate_output_xml( mo_document ).

when 'PE'.

" Create iXML object
mo_ixml = cl_ixml=>create( ).
mo_document = mo_ixml->create_document( ).

call method me->add_new_node
exporting
i_nam = 'ConsultarNfseEnvio'
i_val = 'http://www.abrasf.org.br/ABRASF/arquivos/nfse.xsd'
i_anc = ''
i_att = 'xmlns'.

call method mo_bundle->get
exporting
i_varname = 'prestador'
changing
c_data = l_prestador.

call method me->add_new_node
exporting
i_nam = 'Prestador'
i_val = ''
i_anc = 'ConsultarNfseEnvio'
i_att = ''.


call method me->add_new_node
exporting
i_nam = 'Cnpj'
i_val = l_prestador
i_anc = 'Prestador'
i_att = ''.

call method me->add_new_node
exporting
i_nam = 'PeriodoEmissao'
i_val = ''
i_anc = 'ConsultarNfseEnvio'
i_att = ''.

call method me->add_new_node
exporting
i_nam = 'DataInicial'
i_val = l_yesterday
i_anc = 'PeriodoEmissao'
i_att = ''.

call method me->add_new_node
exporting
i_nam = 'DataFinal'
i_val = l_today
i_anc = 'PeriodoEmissao'
i_att = ''.


call method me->add_new_node
exporting
i_nam = 'Tomador'
i_val = ''
i_anc = 'ConsultarNfseEnvio'
i_att = ''.


call method me->add_new_node
exporting
i_nam = 'CpfCnpj'
i_val = ''
i_anc = 'Tomador'
i_att = ''.


call method me->add_new_node
exporting
i_nam = 'Cnpj'
i_val = l_stcd1
i_anc = 'CpfCnpj'
i_att = ''.

m_xstring_body = generate_output_xml( mo_document ).



when others.

endcase.

call method /xegr/cltracer=>set_trace
exporting
i_text = `XML's Gerados`
i_msgtp = 'I'
io_obj1 = m_xstring_header
io_obj2 = m_xstring_body.

endmethod.


method handle_return.

data: lo_input_xml type ref to /xegr/ifegr_input_xml,
lw_txmlk type /xegr/txmlk.

export mw_ws_setup from mw_ws_setup to memory id 'MW_WS_SETUP'.

call method /xegr/cltracer=>set_trace
exporting
i_text = `XML antes da Conversão pelo Converter`
i_msgtp = 'I'
io_obj1 = m_xstring_return.


lo_input_xml = /xegr/clegr_input_xml_builder=>build_from_xstring( m_xstring_return ).

lw_txmlk = input_to_egr( lo_input_xml ).

call method /xegr/cltracer=>set_trace
exporting
i_text = `Resultado da Conversão`
i_msgtp = 'I'
io_obj1 = lw_txmlk.

free memory id 'MW_WS_SETUP'.

if lw_txmlk-id is initial.

call method /xegr/cltracer=>set_trace
exporting
i_text = 'Erro de Conversão'
i_msgtp = 'E'.

endif.

endmethod.


method input_to_egr.
data: lo_xml_reader type ref to /xegr/clegr_input_xml_reader,
lo_nfexml type ref to /xegr/clegrnfexml.

create object lo_xml_reader.
create object lo_nfexml.

lo_xml_reader->read_data( io_input ).

lo_nfexml->record_data(
exporting
it_xmlhier = io_input->mo_data->mt_xml
* hardinsert =
it_txmlp = io_input->mo_data->mt_txmlp
* no_xml_record =
receiving
rw_txmlk = rw_txmlk
).

endmethod.


method select_some_data.

call method mo_bundle->get
exporting
i_varname = 'tegrprefp'
changing
c_data = mw_nfse_process.

call method mo_bundle->get
exporting
i_varname = 'tegrmdcfg'
changing
c_data = mw_tegrmdcfg.


select single *
from /xegr/tegrwspre
into mw_ws_setup
where country = mw_nfse_process-country
and taxjurcode = mw_nfse_process-taxjurcode.

endmethod.

method read_node.

"-->POOL-------------------------------------------------------ʕ•ᴥ•ʔ"
data: w_lxmlparse type line of /xeit/leit_xmlparse,
t_split type standard table of string,
lo_node type ref to if_ixml_node,
l_split type string,
l_value type string.
"-->POOL-------------------------------------------------------ʕ•ᴥ•ʔ"

refresh t_split.
split i_key at '.' into table t_split.

do 4 times.
read table t_split into l_split index 1.

lo_node = io_xml->find_node( name = l_split
root = lo_node ).
if lo_node is not initial.
delete t_split index sy-tabix.
l_value = lo_node->get_value( ).
endif.

if t_split[] is initial.
l_value = lo_node->get_value( ).
exit.
endif.

enddo.

if t_split[] is initial.
r_value = l_value.
endif.

endmethod.


method send_request.

"-->POOL-------------------------------------------------------ʕ•ᴥ•ʔ"
data: l_address type char200,
l_pstatret type /xegr/mdstatcod,
l_pmotret type /xegr/mdstatdsc,
lt_head type /xegr/legrbinfile,
lt_body type /xegr/legrbinfile,
lt_out type /xegr/legrbinfile,
l_url type char200,
l_bukrs type bukrs,
l_xmlversion type char10,
l_layout type char50.
data: l_rfcdest type rfcdest.
data: lo_edx type ref to /xegr/clegr_edx.
"-->POOL-------------------------------------------------------ʕ•ᴥ•ʔ"

l_address = mw_ws_setup-ws_address.

lt_head = /xegr/clegr_abap=>xstring_to_bintab( m_xstring_header ).
lt_body = /xegr/clegr_abap=>xstring_to_bintab( m_xstring_body ).

l_url = mw_ws_setup-ws_address.
l_bukrs = mw_tegrmdcfg-bukrs.
l_xmlversion = '1'.
l_layout = mw_ws_setup-layout.

call method /xegr/cltracer=>set_trace
exporting
i_text = `Iniciando Leitura de WS`
i_msgtp = 'I'
io_obj1 = l_bukrs
io_obj2 = l_xmlversion
io_obj3 = l_layout.

create object lo_edx.

call method lo_edx->get_rfc_destination(
exporting
i_bukrs = mw_tegrmdcfg-bukrs
i_branch = mw_tegrmdcfg-branc
i_function = lo_edx->c_read_nfse
receiving
r_rfc_conn = l_rfcdest
).

call function '/XEGR/FGR_GET_NFSE'
destination l_rfcdest
exporting
pcodemp = l_bukrs
playout = l_layout
pxmlver = l_xmlversion
pwbsadd = l_url
importing
pstaret = l_pstatret
pmotret = l_pmotret
tables
tegrbinfile_in_head = lt_head
tegrbinfile_in_body = lt_body
tegrbinfile_out = lt_out
exceptions
others = 09.

if sy-subrc = 09.

gw_ylog-status = 'E'.
gw_ylog-message = 'E-DX não está ativo!'.

call method /xegr/cltracer=>set_trace
exporting
i_text = 'E-DX não está ativo!'
i_msgtp = 'E'
io_obj1 = l_pstatret
io_obj2 = l_pmotret.

return.
endif.

sort lt_out by cntidnx ascending.

m_xstring_return = /xegr/clegr_abap=>bintab_to_xstring( lt_out ).

if l_pstatret <> '100'.

gw_ylog-status = 'E'.
gw_ylog-message = 'Erro na Resposta do E-DX'.

call method /xegr/cltracer=>set_trace
exporting
i_text = 'Erro na Resposta do E-DX'
i_msgtp = 'E'
io_obj1 = l_pstatret
io_obj2 = l_pmotret.
else.

call method /xegr/cltracer=>set_trace
exporting
i_text = 'Resposta da Requisição lo_edx->read_nfse'
i_msgtp = 'S'
io_obj1 = m_xstring_return.

endif.

endmethod.


method sign_request.

"-->POOL-------------------------------------------------------ʕ•ᴥ•ʔ"
data: lo_certificate type ref to /xegr/clegr_ws_certificate,
l_sign_element type string.
"-->POOL-------------------------------------------------------ʕ•ᴥ•ʔ"

check mw_ws_setup-assina = 'X'.

lo_certificate = get_certificate( ).

check lo_certificate is bound.

lo_certificate->sign_xml( exporting i_sign_element = l_sign_element
changing c_xmlstring = m_xstring_body ).

call method /xegr/cltracer=>set_trace
exporting
i_text = `XML Assinado`
i_msgtp = 'I'
io_obj1 = m_xstring_body.

endmethod.


method sign_with_certificate.



endmethod.



method standardise_xml.
data: lo_school type ref to /xegr/clegr_xml_standardiser,
lo_standard type ref to cl_xml_document,
lo_conversor type ref to /xegr/clxmlconversor_player,
l_schemeid type /xegr/eschemeid,
l_layout like mw_ws_setup-layout_xml_return,
l_xstring_in type xstring,
l_xstring_out type xstring.


lo_standard = get_standard( ).

check lo_standard is bound.

create object lo_school
exporting
io_standard_xml = lo_standard.

lo_school->standardise( changing co_xml = co_xml ).

create object lo_conversor.

l_layout = mw_ws_setup-layout_xml_return.


replace ';LEFT' in l_layout with ''.
replace ';RIGHT' in l_layout with ''.

l_schemeid = l_layout.

co_xml->render_2_xstring( importing stream = l_xstring_in ).

lo_conversor->run_interface(
exporting
i_schemeid = l_schemeid " ID do Esquema
i_xml_xstring = l_xstring_in
receiving
r_xstring = l_xstring_out
exceptions
no_schemeid = 1
no_xml_loaded = 2
others = 3
).
if sy-subrc eq 0.
co_xml->parse_xstring( l_xstring_out ).
endif.
endmethod.
endclass.