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.