| 1 |
import { formatDateTime } from "@web/core/l10n/dates"; |
| 2 |
|
| 3 |
export function computeSAQRCode(name, vat, date_isostring, amount_total, amount_tax) { |
| 4 |
/* Generate the qr code for Saudi e-invoicing. Specs are available at the following link at page 23 |
| 5 |
https://zatca.gov.sa/ar/E-Invoicing/SystemsDevelopers/Documents/20210528_ZATCA_Electronic_Invoice_Security_Features_Implementation_Standards_vShared.pdf |
| 6 |
*/ |
| 7 |
const ksa_timestamp = formatDateTime(date_isostring, { |
| 8 |
tz: "Asia/Riyadh", |
| 9 |
format: "MM/dd/yyyy, HH:mm:ss", |
| 10 |
}); |
| 11 |
|
| 12 |
const seller_name_enc = _compute_qr_code_field(1, name); |
| 13 |
const company_vat_enc = _compute_qr_code_field(2, vat); |
| 14 |
const timestamp_enc = _compute_qr_code_field(3, ksa_timestamp); |
| 15 |
const invoice_total_enc = _compute_qr_code_field(4, amount_total.toString()); |
| 16 |
const total_vat_enc = _compute_qr_code_field(5, amount_tax.toString()); |
| 17 |
|
| 18 |
const str_to_encode = seller_name_enc.concat( |
| 19 |
company_vat_enc, |
| 20 |
timestamp_enc, |
| 21 |
invoice_total_enc, |
| 22 |
total_vat_enc |
| 23 |
); |
| 24 |
|
| 25 |
let binary = ""; |
| 26 |
for (let i = 0; i < str_to_encode.length; i++) { |
| 27 |
binary += String.fromCharCode(str_to_encode[i]); |
| 28 |
} |
| 29 |
return btoa(binary); |
| 30 |
} |
| 31 |
function _compute_qr_code_field(tag, field) { |
| 32 |
const textEncoder = new TextEncoder(); |
| 33 |
const name_byte_array = Array.from(textEncoder.encode(field)); |
| 34 |
const name_tag_encoding = [tag]; |
| 35 |
const name_length_encoding = [name_byte_array.length]; |
| 36 |
return name_tag_encoding.concat(name_length_encoding, name_byte_array); |
| 37 |
} |