/
attach_pdf.py
71 lines (57 loc) · 2.86 KB
/
attach_pdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
PDF on Submit. Creates a PDF when a document is submitted.
Copyright (C) 2019 Raffael Meyer <raffael@alyf.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import frappe
from frappe import _
def sales_invoice(doc, event=None):
if frappe.get_single("PDF on Submit Settings").sales_invoice:
frappe.enqueue(method=execute, queue='long', timeout=30, is_async=True,
**{"doctype": "Sales Invoice", "name": doc.name, "party": doc.customer})
def delivery_note(doc, event=None):
if frappe.get_single("PDF on Submit Settings").delivery_note:
frappe.enqueue(method=execute, queue='long', timeout=30, is_async=True,
**{"doctype": "Delivery Note", "name": doc.name, "party": doc.customer})
def quotation(doc, event=None):
if frappe.get_single("PDF on Submit Settings").quotation:
frappe.enqueue(method=execute, queue='long', timeout=30, is_async=True,
**{"doctype": "Quotation", "name": doc.name, "party": doc.party_name})
def sales_order(doc, event=None):
if frappe.get_single("PDF on Submit Settings").sales_order:
frappe.enqueue(method=execute, queue='long', timeout=30, is_async=True,
**{"doctype": "Sales Order", "name": doc.name, "party": doc.customer})
def execute(doctype, name, party):
doctype_folder = create_folder(_(doctype), "Home")
party_folder = create_folder(party, doctype_folder)
pdf_data = get_pdf_data(doctype, name)
save_and_attach(pdf_data, doctype, name, party_folder)
def create_folder(folder, parent):
"""Make sure the folder exists and return it's name."""
from frappe.core.doctype.file.file import create_new_folder
try:
create_new_folder(folder, parent)
except frappe.DuplicateEntryError:
pass
return "/".join([parent, folder])
def get_pdf_data(doctype, name):
"""Document -> HTML -> PDF."""
html = frappe.get_print(doctype, name)
return frappe.utils.pdf.get_pdf(html)
def save_and_attach(content, to_doctype, to_name, folder):
"""
Save content to disk and create a File document.
File document is linked to another document.
"""
from frappe.utils.file_manager import save_file
file_name = "{}.pdf".format(to_name.replace(" ", "-").replace("/", "-"))
save_file(file_name, content, to_doctype, to_name, folder=folder, is_private=1)