Best Software Company in Patna, Bihar, Digital Marketing Company Patna

Tag, Length, Value (TLV) in PHP and encode it in Base64 – ZATCA Enabled Invoice Generation

There is a new regulation from the Government asking all VAT registered companies to implement QR CODE in the new E-Invoice.

  • The QR code fields shall be encoded in Tag-Length-Value (TLV) format with the tag values specified in the “Tag” column of the adjacent table.

  • The TLV encoding shall be as follows:

  • Tag: the tag value as mentioned above stored in one byte.

  • Length: the length of the byte array resulted from the UTF8 encoding of the field value. The length shall be stored in one byte.

  • Value: the byte array resulting from the UTF8 encoding of the field value.• Value: the byte array resulting from the UTF8 encoding of the field value.

As per Zakat, Tax and Customs Authority (ZATCA) of Saudi Arabia, one of the main requirements is the implementation of QR codes on tax invoices in the e-invoicing project (Fatoora), which will be mandatory starting December 4, 2021

As per the ZATCA instructions(Page No. 23), the minimum requirements that must be shown after scanning a QR code are the following fields, which should be represented in form of based64 encoding:


1. Seller’s name.

2. VAT registration number of the seller.

3. Time stamp of the invoice (date and time).

4. Invoice total (with VAT).

5. VAT total.

In this blog, I will show how to encode the QR data in base64 format using PHP and then using it in SmartForms to print QR code on Invoice layouts.

1st Step is to prepare each of the five values in TLV (Tag-Length-Value) structure

Tag is fixed (1 for Seller’s name, 2 for VAT No……5 for VAT Total)

Length is the size of the value field in bytes (it’s not the count of characters but how many bytes the value represents)

Value is the data against each of the five fields.

Let’s take an example to clarify TLV


1. Seller name; for example, “Software Services and Solutions

  • Tag = 1(1 as a type represents the seller name)

  • Length = 31 (The number of the bytes in “Software Services and Solutions” word)

  • Value = Software Services and Solutions

2. VAT Number; for example, 123456789101112

  • Tag = 2(2 as a type represents the VAT number)

  • Length = 15

  • Value = 123456789101112

3. Time Stamp; for example, 2021-11-17 08:30:00

  • Tag = 3(3 as a type represents invoice time stamp)

  • Length = 19

  • Value = 2021-11-17 08:30:00

4. Invoice Total; for example, 100.00

  • Tag =4(4 as a type represents the invoice amount)

  • Length = 6

  • Value = 100.00

5. VAT Total; for example, 15.00

  • Tag =5(5 as a type represents the tax amount)

  • Length = 6

  • Value = 15.00

2nd Step is to convert ‘Tag’ and ‘Length’ to Hexadecimal and then to string. Then concatenate these two strings with ‘Value’ (stored as string)

concatenate all the five TLVs into one string

‘##Software Services and Solutions##123456789101112##2021-11-17 08:30:00##115.00##15.00’

3rd Step is to convert the concatenated string to Base64 format


Software Services and Solution is the best software company in Patna



<?php

$seller_name="Software Services and Solutions";

$vat_registration_number="";

$invoice_datetimez="2021-11-17 08:30:00";

$invoice_amount="100.00";

$invoice_tax_amount="15.00";

$result = chr(1) . chr(strlen($seller_name) ) . $seller_name;

$result.=chr(2) . chr(strlen($vat_registration_number) ) . $vat_registration_number;

$result.=chr(3) . chr(strlen($invoice_datetimez) ) . $invoice_datetimez;

$result.=chr(4) . chr(strlen($invoice_amount) ) . $invoice_amount;

$result.=chr(5) . chr(strlen($invoice_tax_amount) ) . $invoice_tax_amount;

echo base64_encode($result);

?>

This will generate the following Code

AQxTYWJpaGEgRmF0bWECDzEyMzQ1Njc4OTExMjMxNQMTMjAyMS0xMS0xNyAwODozMDowMAQGMTAwLjAwBQUxNS4wMA==

Now Next step is to convert it to QRCode

And then view it by E-Invoice QR Code Reader



Best digital marketing company in Patna



up