PDF Z en ABAP RAP - Certificado Retención

Crear el entorno "Forms Service by Adobe"
Entrar al desarrollo BTP, entrar en "Entitlements" 

BTP_Entorno

Documento de referencia <= ejemplo del cual nos guiamos

Buscar "Forms Service by Adobe" si esta disponible en la busqueda seguir con el siguiente paso si no, presionar "Edit" y seleccionar "Add service plans"

Buscar_FSA

Buscar "Adobe Service by Adobe" seleccionar default y seleccionar "Add Service Plans" recordar que se debe tener los roles requeridos para proseguir (seccion Role Collections).

BTP_Adobe_Serivce_Plans

Despues se debe entrar "Service Marketplace", buscar el servicio agregago, clickear en "Create" esperar y darle en "Go to Application".

 BTP_Create_FSA

FSA_portada_corta

Agregar el PDF de Adobe LiveCycle a el Template Store

Entrar al Template Store, selecionar "Create Form" asignar nombre y crear. Seleccionar "Update"y subir las plantillas en "browse..." para importarlos. Entrar a "Communication System" y seleccionar "new" y dar en "create"CM_create

Rellenar los campos
CM_campos_1

CS_campos_2

Crear un metodo de autenticacion
CS_campo_autotenticacion

Entorno ABAP RESTful 
En el proyecto buscar Communication Scenario y Outbound Service

ABAP_CM_create

Crear el Outbound Service y Communication Scenario:
ABAP_create_OS

Despues de crear el Outbound Service la parte baja entrar en "Outbound", elegir los metodos de autenticacion, seleccionar "add" y rellenar los campos

ABAP_OS_add_fields

Entrar al Outbound y seleccionar los campos

ABAP_OB_fill_fields

Posteriormente ir a customizing y buscar "Communication Arragements" darle el "new" y buscar el proyecto en este caso "ZCS_CERTIFICADO_RETENCION" y "create".

CA_create

Rellenar los campos para el acuerdo, guardar y realizar un check connection.
CA_fill_fields

Definir estructuras para usar el transformation
Struct_1

Struct_2

Crear un transformation con el template "Simple transformation"
Transformation_create

Rellenar el transportation con el codigo 

<?sap.transform simple?>

<tt:transform xmlns:tt="http://www.sap.com/transformation-templates" xmlns:ddic="http://www.sap.com/abapxml/types/defined" xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">

<tt:root name="Retention"/>

<tt:template>

<Retention xmlns="urn:sunat:names:specification:ubl:peru:schema:xsd:Retention-1"

xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"

xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"

xmlns:sac="urn:sunat:names:specification:ubl:peru:schema:xsd:SunatAggregateComponents-1">

<cbc:ID tt:value-ref="Retention.id"/>

<cbc:IssueDate tt:value-ref="Retention.issue_date"/>

<cac:AgentParty>

<cac:PartyIdentification>

<cbc:ID schemeID="6" tt:value-ref="Retention.agent.ruc"/>

</cac:PartyIdentification>

<cac:PartyName>

<cbc:Name tt:value-ref="Retention.agent.name"/>

</cac:PartyName>

<cac:PostalAddress>

<cbc:StreetName tt:value-ref="Retention.agent.address"/>

<cbc:CityName tt:value-ref="Retention.agent.city"/>

<cac:Country>

<cbc:IdentificationCode tt:value-ref="Retention.agent.country"/>

</cac:Country>

</cac:PostalAddress>

<cac:PartyLegalEntity>

<cbc:RegistrationName tt:value-ref="Retention.agent.name"/>

</cac:PartyLegalEntity>

</cac:AgentParty>

<cac:ReceiverParty>

<cac:PartyIdentification>

<cbc:ID schemeID="6" tt:value-ref="Retention.receiver.ruc"/>

</cac:PartyIdentification>

<cac:PartyName>

<cbc:Name tt:value-ref="Retention.receiver.name"/>

</cac:PartyName>

<cac:PostalAddress>

<cac:Country>

<cbc:IdentificationCode tt:value-ref="Retention.receiver.country"/>

</cac:Country>

</cac:PostalAddress>

<cac:PartyLegalEntity>

<cbc:RegistrationName tt:value-ref="Retention.receiver.name"/>

</cac:PartyLegalEntity>

</cac:ReceiverParty>

<sac:SUNATRetentionSystemCode tt:value-ref="Retention.system_code"/>

<sac:SUNATRetentionPercent tt:value-ref="Retention.percent"/>

<cbc:TotalInvoiceAmount currencyID="PEN" tt:value-ref="Retention.total_invoice"/>

<sac:SUNATTotalPaid currencyID="PEN" tt:value-ref="Retention.total_paid"/>

<!-- LOOP DOCUMENTOS -->

<tt:loop ref="Retention.documents">

<sac:SUNATRetentionDocumentReference>

<cbc:ID schemeID="01" tt:value-ref="doc_id"/>

<cbc:IssueDate tt:value-ref="issue_date"/>

<cbc:TotalInvoiceAmount tt:value-ref="total_amount" currencyID="PEN"/>

<cac:Payment>

<cbc:ID tt:value-ref="payment.id"/>

<cbc:PaidAmount tt:value-ref="payment.paid_amount" currencyID="PEN"/>

<cbc:PaidDate tt:value-ref="payment.paid_date"/>

</cac:Payment>

<sac:SUNATRetentionInformation>

<sac:SUNATRetentionAmount tt:value-ref="retention.retention_amount" currencyID="PEN"/>

<sac:SUNATRetentionDate tt:value-ref="retention.retention_date"/>

<sac:SUNATNetTotalPaid tt:value-ref="retention.net_total_paid" currencyID="PEN"/>

<cac:ExchangeRate>

<cbc:SourceCurrencyCode tt:value-ref="retention.exchange.source_currency"/>

<cbc:TargetCurrencyCode tt:value-ref="retention.exchange.target_currency"/>

<cbc:CalculationRate tt:value-ref="retention.exchange.rate"/>

<cbc:Date tt:value-ref="retention.exchange.date"/>

</cac:ExchangeRate>

</sac:SUNATRetentionInformation>

</sac:SUNATRetentionDocumentReference>

</tt:loop>

</Retention>

</tt:template>

</tt:transform>

Este es el codigo de la accion "Generar PDF".

DATA:lv_found TYPE abap_bool VALUE abap_false.

READ ENTITIES OF zr_tbl_clave IN LOCAL MODE

ENTITY clave_document

ALL FIELDS WITH CORRESPONDING #( keys )

RESULT DATA(lt_documents)

FAILED failed.

LOOP AT lt_documents INTO DATA(lv_document).

"---------- Constantes de configuración ----------------------------

CONSTANTS:

c_comm_scenario TYPE c LENGTH 30 VALUE 'ZCS_CERTIFICADO_RETENCION', "Scenario

c_comm_system TYPE c LENGTH 60 VALUE 'CS_ZADOBE_FORMS', "Comm System

c_service_id TYPE c LENGTH 40 VALUE 'ZOS_CERTIFICADO_RETENCION_REST'. "Service ID

TRY.

DATA(lo_dest) = cl_http_destination_provider=>create_by_comm_arrangement(

comm_scenario = c_comm_scenario

comm_system_id = c_comm_system

service_id = c_service_id ).

CATCH cx_http_dest_provider_error INTO DATA(lx_error).

APPEND VALUE #(

%tky = keys[ 1 ]-%tky

%msg = new_message_with_text(

severity = if_abap_behv_message=>severity-error

text = |No se pudo obtener el destino HTTP: { lx_error->get_text( ) }| ) )

TO reported-clave_document.

RETURN.

ENDTRY.

DATA:

ls_entity_key TYPE z_scm_electronicdocfile=>tys_electronic_doc_file_type,

ls_business_data TYPE TABLE OF z_scm_electronicdocfile=>tys_electronic_doc_file_type,

lo_http_client TYPE REF TO if_web_http_client,

lo_resource TYPE REF TO /iwbep/if_cp_resource_entity,

lo_client_proxy TYPE REF TO /iwbep/if_cp_client_proxy,

lo_request2 TYPE REF TO /iwbep/if_cp_request_read_list,

lo_response2 TYPE REF TO /iwbep/if_cp_response_read_lst,

lt_range TYPE RANGE OF string,

lo_filter_node_1 TYPE REF TO /iwbep/if_cp_filter_node,

lo_filter_node_2 TYPE REF TO /iwbep/if_cp_filter_node,

lo_filter_node_final TYPE REF TO /iwbep/if_cp_filter_node,

data_content TYPE xstring,

data_name TYPE string.

TRY.

DATA(lo_client) = cl_web_http_client_manager=>create_by_http_destination( lo_dest ).

DATA(lo_request) = lo_client->get_http_request( ).

lo_request->set_header_fields(

VALUE #(

( name = 'Accept' value = 'application/json, text/plain, */*' )

( name = 'Content-Type' value = 'application/json; charset=utf-8' )

)

).

DATA(lo_destination) = cl_http_destination_provider=>create_by_comm_arrangement(

comm_scenario = 'ZCS_ELECTRONICDOCFILE_OUT'

comm_system_id = 'CS_ELECTRONICDOCFILE'

* service_id = 'api_billingdocument'

).

lo_http_client = cl_web_http_client_manager=>create_by_http_destination( lo_destination ).

lo_client_proxy = /iwbep/cl_cp_factory_remote=>create_v4_remote_proxy(

EXPORTING

is_proxy_model_key = VALUE #( repository_id = 'DEFAULT'

proxy_model_id = 'Z_SCM_ELECTRONICDOCFILE'

proxy_model_version = '0001' )

io_http_client = lo_http_client

iv_relative_service_root = '' ).

ASSERT lo_http_client IS BOUND.

" Set entity key

lo_request2 = lo_client_proxy->create_resource_for_entity_set( 'ELECTRONIC_DOC_FILE' )->create_request_for_read( ).

DATA(lo_filter_factory) = lo_request2->create_filter_factory( ).

lt_range = VALUE #( ( option = 'EQ' sign = 'I' low = lv_document-claveedocumentid ) ).

lo_filter_node_1 = lo_filter_factory->create_by_range( iv_property_path = 'ELECTRONIC_DOC_SOURCE_KEY' it_range = lt_range ).

lt_range = VALUE #( ( option = 'EQ' sign = 'I' low = 'SIGNEDDOC' ) ).

lo_filter_node_2 = lo_filter_factory->create_by_range( iv_property_path = 'ELECTRONIC_DOC_FILE_TYPE_2' it_range = lt_range ).

lo_filter_node_final = lo_filter_node_1->and( lo_filter_node_2 ).

lo_request2->set_filter( lo_filter_node_final ).

lo_request2->set_top( 1 ).

lo_response2 = lo_request2->execute( ).

lo_response2->get_business_data( IMPORTING et_business_data = ls_business_data ).

READ TABLE ls_business_data INTO DATA(lt_business_data) INDEX 1.

IF sy-subrc = 0.

data_content = lt_business_data-electronic_doc_file_conten.

data_name = lt_business_data-electronic_doc_file_name.

lv_found = abap_true.

ELSE.

APPEND VALUE #( %tky = lv_document-%tky

%msg = new_message_with_text(

severity = if_abap_behv_message=>severity-error

text = |El source key { lv_document-claveedocumentid } no esta aprobado.| )

) TO reported-clave_document.

* READ ENTITIES OF zr_tbl_clave IN LOCAL MODE

* ENTITY clave_document

* ALL FIELDS WITH

* CORRESPONDING #( keys )

* RESULT DATA(lt_documents3).

* result = VALUE #( FOR lv_document3 IN lt_documents3

* ( %tky = lv_document3-%tky

* %param = lv_document3 ) ).

CONTINUE.

ENDIF.

CATCH cx_web_http_client_error INTO DATA(lx_client_error).

DATA(lx_client_error2) = lx_client_error->get_text( ).

RAISE SHORTDUMP lx_client_error.

RETURN.

CATCH cx_http_dest_provider_error INTO DATA(error1).

DATA(lv_error_remote) = error1->get_text( ).

RETURN.

CATCH /iwbep/cx_cp_remote INTO DATA(lx_remote).

DATA(lx_remote2) = lx_remote->get_text( ).

RETURN.

CATCH /iwbep/cx_gateway INTO DATA(lx_gateway).

DATA(lv_error_gateway) = lx_gateway->get_text( ).

RETURN.

ENDTRY.

*

* TRY.

* CALL TRANSFORMATION zcs_transformacion

* SOURCE retention = ls_retention

* RESULT XML DATA(lv_xml).

* CATCH cx_root INTO DATA(lo_root).

* ENDTRY.

DATA(lv_b64) = cl_web_http_utility=>encode_x_base64( data_content ).

"DATA(lv_b64) = cl_web_http_utility=>encode_x_base64( lv_xml ).

"---------- Variables ---------------------------------------------

* "---------- Tipos Request/Response --------------------------------

TYPES: BEGIN OF ty_req,

xdp_template TYPE string,

xml_data TYPE string,

form_type TYPE string,

form_locale TYPE string,

tagged_pdf TYPE i,

embed_font TYPE i,

change_not_allowed TYPE abap_bool,

print_not_allowed TYPE abap_bool,

END OF ty_req.

DATA: lo_cli TYPE REF TO if_web_http_client,

lo_req TYPE REF TO if_web_http_request,

lo_res TYPE REF TO if_web_http_response,

lv_txt TYPE string,

ls_req TYPE ty_req.

ls_req-xdp_template = 'ZCertificadoRetencion/ZCertificadoRetencionV01'.

ls_req-xml_data = lv_b64.

ls_req-form_type = 'print'.

ls_req-form_locale = 'tr_TR'.

ls_req-tagged_pdf = 1.

ls_req-embed_font = 0.

ls_req-change_not_allowed = abap_false.

ls_req-print_not_allowed = abap_false.

TRY.

CALL METHOD /ui2/cl_json=>serialize

EXPORTING

data = ls_req

pretty_name = /ui2/cl_json=>pretty_mode-camel_case

RECEIVING

r_json = DATA(lv_body).

CATCH cx_root INTO DATA(lx_root).

APPEND VALUE #(

%tky = keys[ 1 ]-%tky

%msg = new_message_with_text(

severity = if_abap_behv_message=>severity-error

text = |Error : { lx_root->get_text( ) }| ) )

TO reported-clave_document.

RETURN.

ENDTRY.

lo_request->set_text(

EXPORTING

i_text = lv_body

).

TRY.

DATA(lo_response) = lo_client->execute(

i_method = if_web_http_client=>post

).

CATCH cx_web_http_client_error INTO lx_client_error.

APPEND VALUE #(

%tky = keys[ 1 ]-%tky

%msg = new_message_with_text

¿Le ha resultado útil este artículo?

2026 Derechos de autor de Soluciones Tecnológicas A Medida SAC