refactor(regional): moved methos to region specific utils for KSA
This commit is contained in:
@@ -257,7 +257,7 @@ doc_events = {
|
|||||||
"validate": "erpnext.regional.india.utils.validate_tax_category"
|
"validate": "erpnext.regional.india.utils.validate_tax_category"
|
||||||
},
|
},
|
||||||
"Sales Invoice": {
|
"Sales Invoice": {
|
||||||
"after_insert": "erpnext.regional.create_qr_code",
|
"after_insert": "erpnext.regional.saudi_arabia.utils.create_qr_code",
|
||||||
"on_submit": [
|
"on_submit": [
|
||||||
"erpnext.regional.create_transaction_log",
|
"erpnext.regional.create_transaction_log",
|
||||||
"erpnext.regional.italy.utils.sales_invoice_on_submit",
|
"erpnext.regional.italy.utils.sales_invoice_on_submit",
|
||||||
@@ -269,7 +269,7 @@ doc_events = {
|
|||||||
],
|
],
|
||||||
"on_trash": [
|
"on_trash": [
|
||||||
"erpnext.regional.check_deletion_permission",
|
"erpnext.regional.check_deletion_permission",
|
||||||
"erpnext.regional.delete_qr_code_file"
|
"erpnext.regional.saudi_arabia.utils.delete_qr_code_file"
|
||||||
],
|
],
|
||||||
"validate": [
|
"validate": [
|
||||||
"erpnext.regional.india.utils.validate_document_name",
|
"erpnext.regional.india.utils.validate_document_name",
|
||||||
|
|||||||
@@ -3,12 +3,8 @@
|
|||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import io
|
|
||||||
import os
|
|
||||||
|
|
||||||
import frappe
|
import frappe
|
||||||
from frappe import _
|
from frappe import _
|
||||||
from pyqrcode import create as qr_create
|
|
||||||
|
|
||||||
from erpnext import get_region
|
from erpnext import get_region
|
||||||
|
|
||||||
@@ -36,69 +32,3 @@ def create_transaction_log(doc, method):
|
|||||||
"data": data
|
"data": data
|
||||||
}).insert(ignore_permissions=True)
|
}).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
|
|
||||||
|
|
||||||
|
|
||||||
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)
|
|
||||||
74
erpnext/regional/saudi_arabia/utils.py
Normal file
74
erpnext/regional/saudi_arabia/utils.py
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
import io
|
||||||
|
import os
|
||||||
|
|
||||||
|
import frappe
|
||||||
|
from pyqrcode import create as qr_create
|
||||||
|
|
||||||
|
from erpnext import get_region
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
Reference in New Issue
Block a user