BAdI to handle synchronized custom documents

The BAdI /AEB/AES_DOC_01 will be run through during the synchronization of the data. You can implement it to handle processing of received customs documents, e.g. EAD or POE. The documents are provided as a byte stream representing the PDF.
The BAdI is run through only once a document is received the first time.

Sending documents to Carrier Connect for Paperless Trade (e.g. DHL)

Works for export declarations from invoice and shipping order from delivery

METHOD /aeb/if_ex_aes_doc_01~document_received.
IF im_type_of_received_doc <> 'ABD'.
  RETURN.
ENDIF.

DATA: lv_vbeln           TYPE vbeln,
      ls_comwa           TYPE vbco6,
      lt_vbfas           TYPE TABLE OF vbfa,
      ls_likp            TYPE likp,
      lt_lips            TYPE /aeb/01_lipss,
      lo_request         TYPE REF TO /aeb/cl_pa_pb_add_satt_req_do,
      lt_attachements    TYPE /aeb/cl_pa_pb_attachment_do=>tt_attachment_do,
      lo_attachement     TYPE REF TO /aeb/cl_pa_pb_attachment_do,
      lt_langs           TYPE /aeb/01_char2s,
      lv_lang            TYPE /aeb/01_char2 VALUE 'DE',
      lo_process_request TYPE REF TO /aeb/cl_pa_pb_pr_shp_req_do,
      lo_creation_parms  TYPE REF TO /aeb/cl_pa_pb_cr_prm_do,
      lo_update_data     TYPE REF TO /aeb/cl_pa_pb_shp_upd_req_do,
      lv_mrn             TYPE /aeb/01_char35
      .

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
  EXPORTING
    input  = im_transaction_label_host
  IMPORTING
    output = lv_vbeln.

ls_comwa-vbeln = lv_vbeln.

CALL FUNCTION 'RV_ORDER_FLOW_INFORMATION'
  EXPORTING
    comwa         = ls_comwa
  TABLES
    vbfa_tab      = lt_vbfas
  EXCEPTIONS
    no_vbfa       = 1
    no_vbuk_found = 2
    OTHERS        = 3.
SORT lt_vbfas BY erdat DESCENDING erzet DESCENDING.

READ TABLE lt_vbfas INTO DATA(ls_delivey) WITH KEY vbtyp_v = 'C'.
SELECT SINGLE * FROM likp WHERE vbeln = @ls_delivey-vbeln INTO @ls_likp.
SELECT * FROM lips WHERE vbeln = @ls_delivey-vbeln INTO TABLE @lt_lips.

DATA(lo_org_unit_bc) = /aeb/cl_pa_pb_dlv_ou_rule_bc=>new_for(
                         im_likp  = ls_likp
                         im_lipss = lt_lips
                       ).
DATA(lv_org_unit) = lo_org_unit_bc->get_org_unit( ).

DATA(lo_connection_parms_bc) = /aeb/cl_pa_pb_engn_prm_bc=>get_instance( ).
DATA(lo_engn_prm) = lo_connection_parms_bc->get_engn_prm_do_for( im_org_unit = lv_org_unit ).

DATA(lo_carrier_bf) = /aeb/cl_pa_pb_carrier_bf=>new_for(
                        im_engn_prm = lo_engn_prm
                        im_org_unit = lv_org_unit
                      ).

DATA(lo_reference_bc) = /aeb/cl_pa_pb_dlv_shp_ref_bc=>new_for( im_likp = ls_likp ).
DATA(lv_shp_ref) = lo_reference_bc->get_shp_ref_do( ).

CREATE OBJECT lo_attachement.
lo_attachement->set_data( im_value = im_pdf_binary_of_received_doc ).
lo_attachement->set_file_name( im_value = 'ABD_' && im_et_id && '.pdf' ).
lo_attachement->set_content_type( im_value = 'EXPORT_ACCOMPANYING_DOCUMENT' ). "COMMERCIAL_INVOICE bzw. PRO_FORMA_INVOICE
lo_attachement->set_mime_type( im_value = 'application/pdf' ).

APPEND lo_attachement TO lt_attachements.

CREATE OBJECT lo_request.
lo_request->set_client_identcode( im_value = lo_engn_prm->get_engine_client( ) ).
lo_request->set_client_system_id( im_value = sy-sysid && '_' && sy-mandt ).
APPEND lv_lang TO lt_langs.
lo_request->set_result_language_iso_codes( im_value = lt_langs ).
lo_request->set_mode( im_value = 'ADD' ).
lo_request->set_shipment_reference( im_value = lv_shp_ref ).
lo_request->set_attachments( im_value = lt_attachements ).
lo_request->set_username( im_value = sy-uname ).

TRY.
    data(lo_result) = lo_carrier_bf->add_shipment_attachments( im_request_do = lo_request ).
  CATCH /aeb/cx_01_pb_bf_inv_sc. " Communication exception

ENDTRY.

DATA(lo_shp_coll_bc) = /aeb/cl_pa_pb_dlv_shp_coll_bc=>new_for(
                         im_likp  = ls_likp
                         im_lipss = lt_lips
                       ).

DATA(ls_shipment) = lo_shp_coll_bc->create_shipment( im_org_unit = lv_org_unit ).

CREATE OBJECT lo_process_request.
lo_process_request->set_client_identcode( im_value = lo_engn_prm->get_engine_client( ) ).
lo_process_request->set_client_system_id( im_value = sy-sysid && '_' && sy-mandt ).
CREATE OBJECT lo_creation_parms.
lo_creation_parms->set_creation_mode( im_value = 'ALWAYS' ).
lo_process_request->set_creation_parms( im_value = lo_creation_parms ).
lo_process_request->set_items( im_value = ls_shipment-items  ).
lo_process_request->set_result_language_iso_codes( im_value = lt_langs ).
lo_process_request->set_shipment_reference( im_value = lv_shp_ref ).
CREATE OBJECT lo_update_data.
lv_mrn = im_declaration->get_customs_registration_numbe( )->v.
lo_update_data->set_customs_registration_no( im_value = lv_mrn ).
lo_process_request->set_shipment_update_data( im_value = lo_update_data ).
lo_process_request->set_username( im_value = sy-uname ).

TRY.
    data(lo_process_result) = lo_carrier_bf->process_shipment( im_request = lo_process_request ).
  CATCH /aeb/cx_01_pb_bf_inv_sc. " Communication exception

ENDTRY.
  ENDMETHOD.

Example to print a document directly from the badi

DATA: lv_spool_handle TYPE syst_tabix,  
          lv_spool        TYPE rspoid,  
          lv_xstring      TYPE xstring,  
          lv_filesize     TYPE i,  
          lt_raw_buffer   TYPE solix_tab,  
          lv_title        TYPE rspotitle,  
          lv_name         TYPE rspo0name,  
          lv_suffix1      TYPE rspo1name,  
          lv_suffix2      TYPE rspo2name,  
          lv_ldest        TYPE ldest,  
          lo_logger       TYPE REF TO /aeb/cl_aes_pb_logger_bc.

"Only print ABD
IF im_type_of_received_doc <> 'ABD'.
  RETURN.
ENDIF.

lo_logger = /aeb/cl_aes_pb_logger_bc=>new( ).

" Prepare data for output of the labels
lv_xstring    = im_pdf_binary_of_received_doc.
lt_raw_buffer = cl_bcs_convert=>xstring_to_solix( iv_xstring = lv_xstring ).
lv_filesize   = xstrlen( lv_xstring ).

" Spool-Settings
CONCATENATE 'ABD' im_reference_no_aes INTO lv_title SEPARATED BY space.
lv_name    = 'AEB'.
lv_suffix2 = im_reference_no_aes.
lv_ldest   = 'PDF'.  "Printer has to be determined beforehand


IF lv_ldest IS INITIAL.
  " No printer determined for output based in AEB-parameter!
  lo_logger->create_log_error(
    EXPORTING
      im_org_unit = CONV #( im_declaration->get_org_unit( )->v )       " Organizational unit
      im_ref_no   = CONV #( im_reference_no_aes )                " Reference
      im_log_text = 'No printer determined for output based in AEB-parameter!'                 " Log text
  ).
  RETURN.
ENDIF.

" Create Spool Request
CALL FUNCTION 'RSPO_SR_OPEN'
  EXPORTING
    dest             = lv_ldest
    layout           = 'G_RAW'
    doctype          = 'BIN'
    titleline        = lv_title
    suffix1          = lv_suffix1
    suffix2          = lv_suffix2
    name             = lv_name
    immediate_print  = abap_true       " Direct Print
  IMPORTING
    handle           = lv_spool_handle
    spoolid          = lv_spool
  EXCEPTIONS
    device_missing   = 1
    name_twice       = 2
    no_such_device   = 3
    operation_failed = 4
    OTHERS           = 5.

IF sy-subrc NE 0.
  lo_logger->create_log_error(
    EXPORTING
      im_org_unit = CONV #( im_declaration->get_org_unit( )->v )       " Organizational unit
      im_ref_no   = CONV #( im_reference_no_aes )                " Reference
      im_log_text = 'Error in creating the spool requests'                 " Log text
  ).
  RETURN.
ENDIF.

" Add the binary content into Spool Request
CALL FUNCTION 'RSPO_SR_TABLE_WRITE_BINARY'
  EXPORTING
    handle           = lv_spool_handle
    total            = lv_filesize
  TABLES
    lines            = lt_raw_buffer
  EXCEPTIONS
    handle_not_valid = 1.

IF sy-subrc NE 0.
  lo_logger->create_log_error(
    EXPORTING
      im_org_unit = CONV #( im_declaration->get_org_unit( )->v )       " Organizational unit
      im_ref_no   = CONV #( im_reference_no_aes )                " Reference
      im_log_text = 'Error adding the binary content into spool request'                 " Log text
  ).
  RETURN.
ENDIF.

" Close Spool Request
CALL FUNCTION 'RSPO_SR_CLOSE'
  EXPORTING
    handle           = lv_spool_handle
  EXCEPTIONS
    handle_not_valid = 1
    operation_failed = 2
    OTHERS           = 3.

IF sy-subrc NE 0.
  lo_logger->create_log_error(
    EXPORTING
      im_org_unit = CONV #( im_declaration->get_org_unit( )->v )       " Organizational unit
      im_ref_no   = CONV #( im_reference_no_aes )                " Reference
      im_log_text = 'Error closing spool request'                 " Log text
  ).
  RETURN.
ENDIF.