blob: 6b68bc032b99cb78c0b86b9ade3daae17f5ae314 [file] [log] [blame]
Nabin Hait0357fbc2018-03-09 13:19:52 +05301# Copyright (c) 2018, Frappe Technologies and contributors
2# For license information, please see license.txt
3
Aditya Hasef3c22f32019-01-22 18:22:20 +05304from __future__ import unicode_literals
Chillar Anand915b3432021-09-02 16:44:59 +05305
Nabin Hait0357fbc2018-03-09 13:19:52 +05306import frappe
7from frappe import _
Chillar Anand915b3432021-09-02 16:44:59 +05308
Nabin Hait0357fbc2018-03-09 13:19:52 +05309from erpnext import get_region
Ahmad87380d02021-09-06 23:36:55 +050010from pyqrcode import create as qr_create
11import io
12import os
Nabin Hait0357fbc2018-03-09 13:19:52 +053013
Chillar Anand915b3432021-09-02 16:44:59 +053014
Nabin Hait0357fbc2018-03-09 13:19:52 +053015def check_deletion_permission(doc, method):
Himanshu Mishra35b26272018-11-13 11:13:04 +053016 region = get_region(doc.company)
Nabin Haita334ed82019-01-22 10:41:50 +053017 if region in ["Nepal", "France"] and doc.docstatus != 0:
Suraj Shetty48e9bc32020-01-29 15:06:18 +053018 frappe.throw(_("Deletion is not permitted for country {0}").format(region))
Raffael Meyeredba0602019-11-06 14:37:04 +010019
20def create_transaction_log(doc, method):
21 """
22 Appends the transaction to a chain of hashed logs for legal resons.
23 Called on submit of Sales Invoice and Payment Entry.
24 """
25 region = get_region()
26 if region not in ["France", "Germany"]:
27 return
28
29 data = str(doc.as_dict())
30
31 frappe.get_doc({
32 "doctype": "Transaction Log",
33 "reference_doctype": doc.doctype,
34 "document_name": doc.name,
35 "data": data
36 }).insert(ignore_permissions=True)
Ahmad87380d02021-09-06 23:36:55 +050037
38
39def create_qr_code(doc, method):
40 """Create QR Code after inserting Sales Inv
41 """
42
43 region = get_region(doc.company)
44 if region not in ['Saudi Arabia']:
45 return
46
47 # if QR Code field not present, do nothing
48 if not hasattr(doc, 'qr_code'):
49 return
50
51 # Don't create QR Code if it already exists
52 qr_code = doc.get("qr_code")
53 if qr_code and frappe.db.exists({"doctype": "File", "file_url": qr_code}):
54 return
55
56 fields = frappe.get_meta('Sales Invoice').fields
57
58 for field in fields:
59 if field.fieldname == 'qr_code' and field.fieldtype == 'Attach Image':
60 # Creating public url to print format
61 default_print_format = frappe.db.get_value('Property Setter', dict(property='default_print_format', doc_type=doc.doctype), "value")
62
63 # System Language
64 language = frappe.get_system_settings('language')
65
66 # creating qr code for the url
67 url = f"{ frappe.utils.get_url() }/{ doc.doctype }/{ doc.name }?format={ default_print_format or 'Standard' }&_lang={ language }&key={ doc.get_signature() }"
68 qr_image = io.BytesIO()
69 url = qr_create(url, error='L')
70 url.png(qr_image, scale=2, quiet_zone=1)
71
72 # making file
73 filename = f"QR-CODE-{doc.name}.png".replace(os.path.sep, "__")
74 _file = frappe.get_doc({
75 "doctype": "File",
76 "file_name": filename,
77 "is_private": 0,
78 "content": qr_image.getvalue()
79 })
80
81 _file.save()
82
83 # assigning to document
84 doc.db_set('qr_code', _file.file_url)
85 doc.notify_update()
86
87 break
88
Ahmad87380d02021-09-06 23:36:55 +050089
90def delete_qr_code_file(doc, method):
91 """Delete QR Code on deleted sales invoice"""
92
93 region = get_region(doc.company)
94 if region not in ['Saudi Arabia']:
95 return
96
97 if hasattr(doc, 'qr_code'):
98 if doc.get('qr_code'):
99 file_doc = frappe.get_list('File', {
100 'file_url': doc.qr_code
101 })
102 if len(file_doc):
103 frappe.delete_doc('File', file_doc[0].name)