feat: create QR Code field if not existing
This commit is contained in:
parent
ef53eb7fcd
commit
276bf73974
@ -1,36 +1,42 @@
|
||||
import io
|
||||
import os
|
||||
from base64 import b64encode
|
||||
from pyqrcode import create as qr_create
|
||||
|
||||
import frappe
|
||||
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 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
|
||||
# if QR Code field not present, create it. Invoices without QR are invalid as per law.
|
||||
if not hasattr(doc, 'ksa_einv_qr'):
|
||||
create_custom_fields({
|
||||
'Sales Invoice': [
|
||||
dict(
|
||||
fieldname='ksa_einv_qr',
|
||||
label='QR Code',
|
||||
fieldtype='Attach Image',
|
||||
read_only=1, no_copy=1, hidden=1
|
||||
)
|
||||
]
|
||||
})
|
||||
|
||||
# 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}):
|
||||
return
|
||||
|
||||
meta = frappe.get_meta('Sales Invoice')
|
||||
meta = frappe.get_meta(doc.doctype)
|
||||
|
||||
for field in meta.get_image_fields():
|
||||
if field.fieldname == 'qr_code':
|
||||
if "ksa_einv_qr" in [d.fieldname for d in meta.get_image_fields()]:
|
||||
''' TLV conversion for
|
||||
1. Seller's Name
|
||||
2. VAT Number
|
||||
@ -112,29 +118,25 @@ def create_qr_code(doc, method):
|
||||
"content": qr_image.getvalue(),
|
||||
"attached_to_doctype": doc.get("doctype"),
|
||||
"attached_to_name": doc.get("name"),
|
||||
"attached_to_field": "qr_code"
|
||||
"attached_to_field": "ksa_einv_qr"
|
||||
})
|
||||
|
||||
_file.save()
|
||||
|
||||
# assigning to document
|
||||
doc.db_set('qr_code', _file.file_url)
|
||||
doc.db_set('ksa_einv_qr', _file.file_url)
|
||||
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)
|
||||
if region not in ['Saudi Arabia']:
|
||||
return
|
||||
|
||||
if hasattr(doc, 'qr_code'):
|
||||
if doc.get('qr_code'):
|
||||
if hasattr(doc, 'ksa_einv_qr'):
|
||||
if doc.get('ksa_einv_qr'):
|
||||
file_doc = frappe.get_list('File', {
|
||||
'file_url': doc.get('qr_code')
|
||||
'file_url': doc.get('ksa_einv_qr')
|
||||
})
|
||||
if len(file_doc):
|
||||
frappe.delete_doc('File', file_doc[0].name)
|
||||
@ -143,5 +145,6 @@ 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()
|
||||
settings_doc = frappe.get_all('KSA VAT Setting', filters={'company': doc.name}, pluck='name')
|
||||
for settings in settings_doc:
|
||||
frappe.delete_doc('KSA VAT Setting', settings)
|
||||
|
Loading…
x
Reference in New Issue
Block a user