EDUCAÇÃO E TECNOLOGIA

QR Code in Base64 encoding for KSA E-Invoicing – Directly from data in Table EDOSAINV

I have explained how QR code can be generated in Base64 encoding conforming to Saudi Arabia ZATCA standard in my previous blog.

In this blog I will explain how the same can be done directly from data stored in field QR_CODE of table EDOSAINV.

With the data stored in EDOSAINV-QR_CODE you don’t need to worry on how to build and convert data in TLV format.

There are just three steps to follow:

  1. Get the QR data stored in Hexadecimal value (RAWSTRING) from table EDOSINV
  2. Convert this value to String value.
  3. Convert the string value obtained from 2nd step to Base64

You can then use the base64 value obtained in the final step 3 to generate the QR code.

I have created a FM which will take the takes invoice number as input and gives QR code values in  Base64 along with hex and string.

FUNCTION z_einvoice_qrcode_from_table.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(INVOICE_NO) TYPE VBELN_VF
*" EXPORTING
*" REFERENCE(QRCODE_HEXSTR) TYPE EDOC_SA_XSTRING
*" REFERENCE(QRCODE_STRING) TYPE STRING
*" REFERENCE(QRCODE_BASE64) TYPE STRING
*" EXCEPTIONS
*" NO_INVOICE
*"----------------------------------------------------------------------
* For this FM to give QR value result in base64 encoding, XML should
* have been successfully generated from transaction EDOC_COCKPIT against
* the invoice.
*"---------------------------------------------------------------------- DATA: v_guid TYPE edoc_guid. SELECT SINGLE edoc_guid FROM edocument INTO v_guid WHERE source_key = invoice_no. IF sy-subrc = 0. SELECT SINGLE qr_code FROM edosainv INTO qrcode_hexstr WHERE edoc_guid = v_guid. IF sy-subrc = 0. PERFORM convert_hex_to_str USING qrcode_hexstr CHANGING qrcode_string.
***************Encode String to Base64********************* CALL METHOD cl_http_utility=>if_http_utility~encode_base64 EXPORTING unencoded = qrcode_string RECEIVING encoded = qrcode_base64. ENDIF. ELSE. RAISE no_invoice. ENDIF. ENDFUNCTION.

The subroutine convert_hex_to_str is

*&---------------------------------------------------------------------*
*& Form CONVERT_HEX_TO_STR
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_HEX text
* <--P_STR text
*----------------------------------------------------------------------*
FORM convert_hex_to_str USING p_hex CHANGING p_str.
* CALL FUNCTION 'HR_RU_CONVERT_HEX_TO_STRING'
* EXPORTING
* xstring = p_hex
* IMPORTING
* cstring = p_str. DATA: loc_conv TYPE REF TO cl_abap_conv_in_ce. CALL METHOD cl_abap_conv_in_ce=>create EXPORTING input = p_hex encoding = 'UTF-8' replacement = '?' ignore_cerr = abap_true RECEIVING conv = loc_conv. TRY. CALL METHOD loc_conv->read IMPORTING data = p_str. CATCH cx_sy_conversion_codepage. "Should ignore errors in code conversions CATCH cx_sy_codepage_converter_init."Should ignore errors in code conversions CATCH cx_parameter_invalid_type. CATCH cx_parameter_invalid_range. ENDTRY. ENDFORM.

Once you run the FM with any invoice whose eInvoice has been generated then you will get the output of this FM as shown below.

Take the value of export parameter QRCODE_BASE64 and generate the QR code.

You may follow the second half from “Setting up the QR Code font” of my previous blog on how to display QR code on any SAPScript/SmartForms layouts.

Regards,

Firoz.