From 9fa18cfeb6477ca422103ba4fece56a63d1998b0 Mon Sep 17 00:00:00 2001 From: Ahmad Date: Wed, 24 Nov 2021 02:54:43 +0100 Subject: [PATCH 1/3] refactor(KSA VAT): QR Code as per ZATKA specification --- erpnext/regional/saudi_arabia/utils.py | 91 +++++++++++++++++++++----- 1 file changed, 75 insertions(+), 16 deletions(-) diff --git a/erpnext/regional/saudi_arabia/utils.py b/erpnext/regional/saudi_arabia/utils.py index a2f634ee22cd..558c7ee1d79b 100644 --- a/erpnext/regional/saudi_arabia/utils.py +++ b/erpnext/regional/saudi_arabia/utils.py @@ -1,7 +1,10 @@ import io import os +from base64 import b64encode import frappe +from frappe import _ +from frappe.utils.data import add_to_date, get_time, getdate from pyqrcode import create as qr_create from erpnext import get_region @@ -28,24 +31,80 @@ def create_qr_code(doc, method): for field in meta.get_image_fields(): if field.fieldname == 'qr_code': - from urllib.parse import urlencode + ''' TLV conversion for + 1. Seller's Name + 2. VAT Number + 3. Time Stamp + 4. Invoice Amount + 5. VAT Amount + ''' + tlv_array = [] + # Sellers Name + + '''TODO: Fix arabic conversion''' + # seller_name = frappe.db.get_value( + # 'Company', + # doc.company, + # 'company_name_in_arabic') + + # if not seller_name: + # frappe.throw(_('Arabic name missing for {} in the company document'.format(doc.company))) + + seller_name = doc.company + tag = bytes([1]).hex() + length = bytes([len(seller_name)]).hex() + value = seller_name.encode('utf-8').hex() + tlv_array.append(''.join([tag, length, value])) + + # VAT Number + tax_id = frappe.db.get_value('Company', doc.company, 'tax_id') + if not tax_id: + frappe.throw(_('Tax ID missing for {} in the company document'.format(doc.company))) + + # tax_id = '310122393500003' # + tag = bytes([2]).hex() + length = bytes([len(tax_id)]).hex() + value = tax_id.encode('utf-8').hex() + tlv_array.append(''.join([tag, length, value])) + + # Time Stamp + posting_date = getdate(doc.posting_date) + time = get_time(doc.posting_time) + seconds = time.hour * 60 * 60 + time.minute * 60 + time.second + time_stamp = add_to_date(posting_date, seconds=seconds) + time_stamp = time_stamp.strftime('%Y-%m-%dT%H:%M:%SZ') + + # time_stamp = '2022-04-25T15:30:00Z' # + tag = bytes([3]).hex() + length = bytes([len(time_stamp)]).hex() + value = time_stamp.encode('utf-8').hex() + tlv_array.append(''.join([tag, length, value])) + + # Invoice Amount + invoice_amount = str(doc.total) + # invoice_amount = '1000.00' # + tag = bytes([4]).hex() + length = bytes([len(invoice_amount)]).hex() + value = invoice_amount.encode('utf-8').hex() + tlv_array.append(''.join([tag, length, value])) + + # VAT Amount + vat_amount = str(doc.total_taxes_and_charges) + + # vat_amount = '150.00' # + tag = bytes([5]).hex() + length = bytes([len(vat_amount)]).hex() + value = vat_amount.encode('utf-8').hex() + tlv_array.append(''.join([tag, length, value])) + + # Joining bytes into one + tlv_buff = ''.join(tlv_array) + + # base64 conversion for QR Code + base64_string = b64encode(bytes.fromhex(tlv_buff)).decode() - # Creating public url to print format - default_print_format = frappe.db.get_value('Property Setter', dict(property='default_print_format', doc_type=doc.doctype), "value") - - # System Language - language = frappe.get_system_settings('language') - - params = urlencode({ - 'format': default_print_format or 'Standard', - '_lang': language, - 'key': doc.get_signature() - }) - - # creating qr code for the url - url = f"{ frappe.utils.get_url() }/{ doc.doctype }/{ doc.name }?{ params }" qr_image = io.BytesIO() - url = qr_create(url, error='L') + url = qr_create(base64_string, error='L') url.png(qr_image, scale=2, quiet_zone=1) # making file From f7aa473297e651a330735d6a3772fece07cc1f97 Mon Sep 17 00:00:00 2001 From: Ahmad Date: Wed, 24 Nov 2021 03:03:17 +0100 Subject: [PATCH 2/3] refactor: comments removed --- erpnext/regional/saudi_arabia/utils.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/erpnext/regional/saudi_arabia/utils.py b/erpnext/regional/saudi_arabia/utils.py index 558c7ee1d79b..c0f4e31b2368 100644 --- a/erpnext/regional/saudi_arabia/utils.py +++ b/erpnext/regional/saudi_arabia/utils.py @@ -61,7 +61,6 @@ def create_qr_code(doc, method): if not tax_id: frappe.throw(_('Tax ID missing for {} in the company document'.format(doc.company))) - # tax_id = '310122393500003' # tag = bytes([2]).hex() length = bytes([len(tax_id)]).hex() value = tax_id.encode('utf-8').hex() @@ -74,7 +73,6 @@ def create_qr_code(doc, method): time_stamp = add_to_date(posting_date, seconds=seconds) time_stamp = time_stamp.strftime('%Y-%m-%dT%H:%M:%SZ') - # time_stamp = '2022-04-25T15:30:00Z' # tag = bytes([3]).hex() length = bytes([len(time_stamp)]).hex() value = time_stamp.encode('utf-8').hex() @@ -82,7 +80,6 @@ def create_qr_code(doc, method): # Invoice Amount invoice_amount = str(doc.total) - # invoice_amount = '1000.00' # tag = bytes([4]).hex() length = bytes([len(invoice_amount)]).hex() value = invoice_amount.encode('utf-8').hex() @@ -91,7 +88,6 @@ def create_qr_code(doc, method): # VAT Amount vat_amount = str(doc.total_taxes_and_charges) - # vat_amount = '150.00' # tag = bytes([5]).hex() length = bytes([len(vat_amount)]).hex() value = vat_amount.encode('utf-8').hex() From 2bb2c69975de273ebdaf3a8ed0978baff7ad175a Mon Sep 17 00:00:00 2001 From: Ahmad Date: Sat, 27 Nov 2021 14:28:13 +0100 Subject: [PATCH 3/3] fix: KSA VAT QR Code arabic conversion --- erpnext/regional/saudi_arabia/utils.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/erpnext/regional/saudi_arabia/utils.py b/erpnext/regional/saudi_arabia/utils.py index c0f4e31b2368..516b87c75d65 100644 --- a/erpnext/regional/saudi_arabia/utils.py +++ b/erpnext/regional/saudi_arabia/utils.py @@ -41,18 +41,16 @@ def create_qr_code(doc, method): tlv_array = [] # Sellers Name - '''TODO: Fix arabic conversion''' - # seller_name = frappe.db.get_value( - # 'Company', - # doc.company, - # 'company_name_in_arabic') + seller_name = frappe.db.get_value( + 'Company', + doc.company, + 'company_name_in_arabic') - # if not seller_name: - # frappe.throw(_('Arabic name missing for {} in the company document'.format(doc.company))) + if not seller_name: + frappe.throw(_('Arabic name missing for {} in the company document'.format(doc.company))) - seller_name = doc.company tag = bytes([1]).hex() - length = bytes([len(seller_name)]).hex() + length = bytes([len(seller_name.encode('utf-8'))]).hex() value = seller_name.encode('utf-8').hex() tlv_array.append(''.join([tag, length, value]))