feat(regional): QR Code generation for Saudi Arabia Sales Invoices

This commit is contained in:
Ahmad 2021-09-06 23:36:55 +05:00
parent b42d63e526
commit 87380d021d
3 changed files with 90 additions and 5 deletions

View File

@ -262,6 +262,7 @@ doc_events = {
"validate": "erpnext.regional.india.utils.validate_tax_category"
},
"Sales Invoice": {
"after_insert": "erpnext.regional.create_qr_code",
"on_submit": [
"erpnext.regional.create_transaction_log",
"erpnext.regional.italy.utils.sales_invoice_on_submit",
@ -271,7 +272,10 @@ doc_events = {
"erpnext.regional.italy.utils.sales_invoice_on_cancel",
"erpnext.erpnext_integrations.taxjar_integration.delete_transaction"
],
"on_trash": "erpnext.regional.check_deletion_permission",
"on_trash": [
"erpnext.regional.check_deletion_permission",
"erpnext.regional.delete_qr_code_file"
],
"validate": [
"erpnext.regional.india.utils.validate_document_name",
"erpnext.regional.india.utils.update_taxable_values"

View File

@ -7,6 +7,9 @@ import frappe
from frappe import _
from erpnext import get_region
from pyqrcode import create as qr_create
import io
import os
def check_deletion_permission(doc, method):
@ -31,3 +34,72 @@ def create_transaction_log(doc, method):
"document_name": doc.name,
"data": data
}).insert(ignore_permissions=True)
def create_qr_code(doc, method):
"""Create QR Code after inserting Sales Inv
"""
region = get_region(doc.company)
if region not in ['Saudi Arabia']:
return
# if QR Code field not present, do nothing
if not hasattr(doc, 'qr_code'):
return
# Don't create QR Code if it already exists
qr_code = doc.get("qr_code")
if qr_code and frappe.db.exists({"doctype": "File", "file_url": qr_code}):
return
fields = frappe.get_meta('Sales Invoice').fields
for field in fields:
if field.fieldname == 'qr_code' and field.fieldtype == 'Attach Image':
# 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')
# creating qr code for the url
url = f"{ frappe.utils.get_url() }/{ doc.doctype }/{ doc.name }?format={ default_print_format or 'Standard' }&_lang={ language }&key={ doc.get_signature() }"
qr_image = io.BytesIO()
url = qr_create(url, error='L')
url.png(qr_image, scale=2, quiet_zone=1)
# making file
filename = f"QR-CODE-{doc.name}.png".replace(os.path.sep, "__")
_file = frappe.get_doc({
"doctype": "File",
"file_name": filename,
"is_private": 0,
"content": qr_image.getvalue()
})
_file.save()
# assigning to document
doc.db_set('qr_code', _file.file_url)
doc.notify_update()
break
else:
pass
def delete_qr_code_file(doc, method):
"""Delete QR Code on deleted sales invoice"""
region = get_region(doc.company)
if region not in ['Saudi Arabia']:
return
if hasattr(doc, 'qr_code'):
if doc.get('qr_code'):
file_doc = frappe.get_list('File', {
'file_url': doc.qr_code
})
if len(file_doc):
frappe.delete_doc('File', file_doc[0].name)

View File

@ -2,18 +2,18 @@
# License: GNU General Public License v3. See license.txt
from __future__ import unicode_literals
import frappe
from frappe.permissions import add_permission, update_permission_property
from erpnext.regional.united_arab_emirates.setup import make_custom_fields, add_print_formats
from erpnext.regional.united_arab_emirates.setup import make_custom_fields as uae_custom_fields, add_print_formats
from erpnext.regional.saudi_arabia.wizard.operations.setup_ksa_vat_setting import create_ksa_vat_setting
from frappe.custom.doctype.custom_field.custom_field import create_custom_field
def setup(company=None, patch=True):
make_custom_fields()
uae_custom_fields()
add_print_formats()
add_permissions()
create_ksa_vat_setting(company)
make_custom_fields()
def add_permissions():
@ -26,3 +26,12 @@ def add_permissions():
"""Enable KSA VAT Report"""
frappe.db.set_value('Report', 'KSA VAT', 'disabled', 0)
def make_custom_fields():
qr_code_field = dict(
fieldname='qr_code',
label='QR Code',
fieldtype='Attach Image',
read_only=1, no_copy=1, hidden=1)
create_custom_field('Sales Invoice', qr_code_field)