-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Community Developed Custom Scripts
Revant Nandgaonkar edited this page Aug 17, 2016
·
17 revisions
The following provides a database of community developed custom scripts for implementing unique features through the ERPNext custom script tool. In certain cases, server-side scripts have also been written to support the client-side scripts.
###Attachment transfer This script takes attachments identified in specific custom fields of an Item document and copies them into another document as general attachments. This is used in practice to attach critical design documents to Request for Quotations and Purchase Orders that get sent automatically through e-mail.
Custom Script
frappe.ui.form.on("Request for Quotation",{
refresh: function(frm) {
frm.add_custom_button(__("Load Attachments"), function(foo) {
frappe.call({
method:"my_app.my_app.controller.attach_all_docs",
args: {
document: cur_frm.doc,
},
callback: function(r) {
frm.reload_doc();
}
});
});
}
});
Server-Side Script
@frappe.whitelist()
def attach_all_docs(document, method=None):
"""This function attaches drawings to the purchase order based on the items being ordered"""
document = json.loads(document)
current_attachments = []
for file_url in frappe.db.sql("""select file_url from `tabFile` where attached_to_doctype = %(doctype)s and attached_to_name = %(docname)s""", {'doctype': document["doctype"], 'docname': document["name"]}, as_dict=True ):
current_attachments.append(file_url.file_url)
count = 0
for item_doc in document["items"]:
#frappe.msgprint(item_doc)
# Check to see if the quantity is = 1
item = frappe.get_doc("Item",item_doc["item_code"])
attachments = []
# Get the path for the attachments
if item.drawing_attachment:
attachments.append(item.drawing_attachment)
if item.stp_attachment:
attachments.append(item.stp_attachment)
if item.dxf_attachment:
attachments.append(item.dxf_attachment)
if item.x_t_attachment:
attachments.append(item.x_t_attachment)
for attach in attachments:
# Check to see if this file is attached to the one we are looking for
if not attach in current_attachments:
count = count + 1
save_url(attach, document["doctype"], document["name"], "Home/Attachments")
frappe.msgprint("Attached {0} files".format(count))
###Manual Dropbox Backup
Add this to Setup > Custom Script > New
Doctype : Dropbox Backup
Script :
frappe.ui.form.on("Dropbox Backup", "refresh", function(frm){
if(frm.doc.send_backups_to_dropbox){
frm.add_custom_button(__("Take Backup"), function() {
frappe.call({
method: "frappe.integrations.doctype.dropbox_backup.dropbox_backup.take_backups_dropbox",
freeze: true,
freeze_message: __("Taking backup"),
callback: function(r){
if(!r.exc) {
frappe.msgprint(__("Backup taken successfully"));
} else {
frappe.msgprint(__("Error while taking backup. <br /> " + r.exc));
}
}
});
})
}
});