2021-09-14 09:45:23 +00:00
|
|
|
import io
|
|
|
|
import os
|
|
|
|
|
2021-09-16 20:14:41 +00:00
|
|
|
import frappe
|
|
|
|
from pyqrcode import create as qr_create
|
|
|
|
|
|
|
|
from erpnext import get_region
|
|
|
|
|
2021-09-14 09:45:23 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-09-16 20:33:32 +00:00
|
|
|
meta = frappe.get_meta('Sales Invoice')
|
2021-09-16 20:14:41 +00:00
|
|
|
|
2021-09-16 20:33:32 +00:00
|
|
|
for field in meta.get_image_fields():
|
|
|
|
if field.fieldname == 'qr_code':
|
2021-11-20 05:52:19 +00:00
|
|
|
from urllib.parse import urlencode
|
|
|
|
|
2021-09-14 09:45:23 +00:00
|
|
|
# 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")
|
2021-09-16 20:14:41 +00:00
|
|
|
|
2021-09-14 09:45:23 +00:00
|
|
|
# System Language
|
|
|
|
language = frappe.get_system_settings('language')
|
2021-09-16 20:14:41 +00:00
|
|
|
|
2021-11-20 05:52:19 +00:00
|
|
|
params = urlencode({
|
|
|
|
'format': default_print_format or 'Standard',
|
|
|
|
'_lang': language,
|
|
|
|
'key': doc.get_signature()
|
|
|
|
})
|
|
|
|
|
2021-09-14 09:45:23 +00:00
|
|
|
# creating qr code for the url
|
2021-11-20 05:52:19 +00:00
|
|
|
url = f"{ frappe.utils.get_url() }/{ doc.doctype }/{ doc.name }?{ params }"
|
2021-09-14 09:45:23 +00:00
|
|
|
qr_image = io.BytesIO()
|
|
|
|
url = qr_create(url, error='L')
|
|
|
|
url.png(qr_image, scale=2, quiet_zone=1)
|
2021-09-16 20:14:41 +00:00
|
|
|
|
2021-09-14 09:45:23 +00:00
|
|
|
# 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,
|
2021-09-16 20:28:52 +00:00
|
|
|
"content": qr_image.getvalue(),
|
|
|
|
"attached_to_doctype": doc.get("doctype"),
|
|
|
|
"attached_to_name": doc.get("name"),
|
|
|
|
"attached_to_field": "qr_code"
|
2021-09-14 09:45:23 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
_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"""
|
2021-09-16 20:14:41 +00:00
|
|
|
|
2021-09-14 09:45:23 +00:00
|
|
|
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', {
|
2021-09-16 20:27:37 +00:00
|
|
|
'file_url': doc.get('qr_code')
|
2021-09-14 09:45:23 +00:00
|
|
|
})
|
|
|
|
if len(file_doc):
|
2021-11-22 08:51:53 +00:00
|
|
|
frappe.delete_doc('File', file_doc[0].name)
|
|
|
|
|
|
|
|
def delete_vat_settings_for_company(doc, method):
|
|
|
|
if doc.country != 'Saudi Arabia':
|
|
|
|
return
|
|
|
|
|
|
|
|
settings_doc = frappe.get_doc('KSA VAT Setting', {'company': doc.name})
|
|
|
|
settings_doc.delete()
|