Nabin Hait | 0357fbc | 2018-03-09 13:19:52 +0530 | [diff] [blame] | 1 | # Copyright (c) 2018, Frappe Technologies and contributors |
| 2 | # For license information, please see license.txt |
| 3 | |
Aditya Hase | f3c22f3 | 2019-01-22 18:22:20 +0530 | [diff] [blame] | 4 | from __future__ import unicode_literals |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 5 | |
Nabin Hait | 0357fbc | 2018-03-09 13:19:52 +0530 | [diff] [blame] | 6 | import frappe |
| 7 | from frappe import _ |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 8 | |
Nabin Hait | 0357fbc | 2018-03-09 13:19:52 +0530 | [diff] [blame] | 9 | from erpnext import get_region |
Ahmad | 87380d0 | 2021-09-06 23:36:55 +0500 | [diff] [blame] | 10 | from pyqrcode import create as qr_create |
| 11 | import io |
| 12 | import os |
Nabin Hait | 0357fbc | 2018-03-09 13:19:52 +0530 | [diff] [blame] | 13 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 14 | |
Nabin Hait | 0357fbc | 2018-03-09 13:19:52 +0530 | [diff] [blame] | 15 | def check_deletion_permission(doc, method): |
Himanshu Mishra | 35b2627 | 2018-11-13 11:13:04 +0530 | [diff] [blame] | 16 | region = get_region(doc.company) |
Nabin Hait | a334ed8 | 2019-01-22 10:41:50 +0530 | [diff] [blame] | 17 | if region in ["Nepal", "France"] and doc.docstatus != 0: |
Suraj Shetty | 48e9bc3 | 2020-01-29 15:06:18 +0530 | [diff] [blame] | 18 | frappe.throw(_("Deletion is not permitted for country {0}").format(region)) |
Raffael Meyer | edba060 | 2019-11-06 14:37:04 +0100 | [diff] [blame] | 19 | |
| 20 | def 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) |
Ahmad | 87380d0 | 2021-09-06 23:36:55 +0500 | [diff] [blame] | 37 | |
| 38 | |
| 39 | def 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 | |
Ahmad | 87380d0 | 2021-09-06 23:36:55 +0500 | [diff] [blame] | 89 | |
| 90 | def 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) |