feat: create QR field incase of missing

This commit is contained in:
Dany Robert
2021-12-08 23:04:02 +05:30
committed by GitHub
parent c6d4a23bdc
commit 2e3c9c0aad

View File

@@ -1,36 +1,45 @@
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt
import io import io
import os import os
from base64 import b64encode from base64 import b64encode
import frappe import frappe
from frappe import _ from frappe import _
from frappe.custom.doctype.custom_field.custom_field import create_custom_fields
from frappe.utils.data import add_to_date, get_time, getdate from frappe.utils.data import add_to_date, get_time, getdate
from pyqrcode import create as qr_create from pyqrcode import create as qr_create
from erpnext import get_region from erpnext import get_region
def create_qr_code(doc, method): def create_qr_code(doc, method=None):
"""Create QR Code after inserting Sales Inv
"""
region = get_region(doc.company) region = get_region(doc.company)
if region not in ['Saudi Arabia']: if region not in ['Saudi Arabia']:
return return
# if QR Code field not present, do nothing # if QR Code field not present, create it. Invoices without QR are invalid as per law.
if not hasattr(doc, 'qr_code'): if not hasattr(doc, 'ksa_einv_qr'):
return create_custom_fields({
doc.doctype: [
dict(
fieldname='ksa_einv_qr',
label='KSA E-Invoicing QR',
fieldtype='Attach Image',
read_only=1, no_copy=1, hidden=1
)
]
})
# Don't create QR Code if it already exists # Don't create QR Code if it already exists
qr_code = doc.get("qr_code") qr_code = doc.get("ksa_einv_qr")
if qr_code and frappe.db.exists({"doctype": "File", "file_url": qr_code}): if qr_code and frappe.db.exists({"doctype": "File", "file_url": qr_code}):
return return
meta = frappe.get_meta('Sales Invoice') meta = frappe.get_meta(doc.doctype)
for field in meta.get_image_fields(): if "ksa_einv_qr" in [d.fieldname for d in meta.get_image_fields()]:
if field.fieldname == 'qr_code':
''' TLV conversion for ''' TLV conversion for
1. Seller's Name 1. Seller's Name
2. VAT Number 2. VAT Number
@@ -112,36 +121,32 @@ def create_qr_code(doc, method):
"content": qr_image.getvalue(), "content": qr_image.getvalue(),
"attached_to_doctype": doc.get("doctype"), "attached_to_doctype": doc.get("doctype"),
"attached_to_name": doc.get("name"), "attached_to_name": doc.get("name"),
"attached_to_field": "qr_code" "attached_to_field": "ksa_einv_qr"
}) })
_file.save() _file.save()
# assigning to document # assigning to document
doc.db_set('qr_code', _file.file_url) doc.db_set('ksa_einv_qr', _file.file_url)
doc.notify_update() doc.notify_update()
break
def delete_qr_code_file(doc, method):
"""Delete QR Code on deleted sales invoice"""
def delete_qr_code_file(doc, method=None):
region = get_region(doc.company) region = get_region(doc.company)
if region not in ['Saudi Arabia']: if region not in ['Saudi Arabia']:
return return
if hasattr(doc, 'qr_code'): if hasattr(doc, 'ksa_einv_qr'):
if doc.get('qr_code'): if doc.get('ksa_einv_qr'):
file_doc = frappe.get_list('File', { file_doc = frappe.get_list('File', {
'file_url': doc.get('qr_code') 'file_url': doc.get('ksa_einv_qr')
}) })
if len(file_doc): if len(file_doc):
frappe.delete_doc('File', file_doc[0].name) frappe.delete_doc('File', file_doc[0].name)
def delete_vat_settings_for_company(doc, method): def delete_vat_settings_for_company(doc, method=None):
if doc.country != 'Saudi Arabia': if doc.country != 'Saudi Arabia':
return return
settings_doc = frappe.get_doc('KSA VAT Setting', {'company': doc.name}) if frappe.db.exists('KSA VAT Setting', doc.name):
settings_doc.delete() frappe.delete_doc('KSA VAT Setting', doc.name)