diff --git a/frappe/printing/page/print_format_builder/print_format_builder.js b/frappe/printing/page/print_format_builder/print_format_builder.js index 7611f4d63b..aa733cbe31 100644 --- a/frappe/printing/page/print_format_builder/print_format_builder.js +++ b/frappe/printing/page/print_format_builder/print_format_builder.js @@ -12,10 +12,9 @@ frappe.pages['print-format-builder'].on_page_show = function(wrapper) { }); } else if(frappe.route_options) { if(frappe.route_options.make_new) { - var doctype = frappe.route_options.doctype; - var name = frappe.route_options.name; + let { doctype, name, based_on } = frappe.route_options; frappe.route_options = null; - frappe.print_format_builder.setup_new_print_format(doctype, name); + frappe.print_format_builder.setup_new_print_format(doctype, name, based_on); } else { frappe.print_format_builder.print_format = frappe.route_options.doc; frappe.route_options = null; @@ -130,25 +129,16 @@ frappe.PrintFormatBuilder = Class.extend({ }); }, - setup_new_print_format: function(doctype, name) { - var me = this; - frappe.call({ - method: "frappe.client.insert", - args: { - doc: { - doctype: "Print Format", - name: name, - standard: "No", - doc_type: doctype, - print_format_builder: 1 - } - }, - callback: function(r) { - frappe.model.with_doc('Print Format', r.message.name) - .then(() => $(document).trigger({ type: 'new-print-format', print_format: r.message.name })); - me.print_format = r.message; - me.refresh(); - } + setup_new_print_format: function(doctype, name, based_on) { + frappe.call('frappe.printing.page.print_format_builder.print_format_builder.create_custom_format', { + doctype, + name, + based_on + }).then((r) => { + frappe.model.with_doc('Print Format', r.message.name) + .then(() => $(document).trigger({ type: 'new-print-format', print_format: r.message.name })); + this.print_format = r.message; + this.refresh(); }); }, setup_print_format: function() { diff --git a/frappe/printing/page/print_format_builder/print_format_builder.py b/frappe/printing/page/print_format_builder/print_format_builder.py new file mode 100644 index 0000000000..17baa9314e --- /dev/null +++ b/frappe/printing/page/print_format_builder/print_format_builder.py @@ -0,0 +1,12 @@ +import frappe + +@frappe.whitelist() +def create_custom_format(doctype, name, based_on): + doc = frappe.new_doc('Print Format') + doc.doc_type = doctype + doc.name = name + doc.print_format_builder = 1 + doc.format_data = frappe.db.get_value('Print Format', based_on, 'format_data') \ + if based_on != 'Standard' else None + doc.insert() + return doc diff --git a/frappe/public/js/frappe/form/print.js b/frappe/public/js/frappe/form/print.js index 609a5bec31..4a388841d5 100644 --- a/frappe/public/js/frappe/form/print.js +++ b/frappe/public/js/frappe/form/print.js @@ -88,26 +88,42 @@ frappe.ui.form.PrintPreview = Class.extend({ this.wrapper.find(".btn-print-edit").on("click", function () { let print_format = me.get_print_format(); - if (print_format && print_format.name) { - if (print_format.print_format_builder) { - frappe.set_route("print-format-builder", print_format.name); - } else { - frappe.set_route("Form", "Print Format", print_format.name); - } - } else { - // start a new print format - frappe.prompt({ - fieldname: "print_format_name", fieldtype: "Data", reqd: 1, - label: "New Print Format Name" - }, function (data) { - frappe.route_options = { - make_new: true, - doctype: me.frm.doctype, - name: data.print_format_name - }; - frappe.set_route("print-format-builder"); - }, __("New Custom Print Format"), __("Start")); + let is_custom_format = print_format.name + && print_format.print_format_builder + && print_format.standard === 'No'; + let is_standard_but_editable = print_format.name && print_format.custom_format; + + if (is_standard_but_editable) { + frappe.set_route("Form", "Print Format", print_format.name); + return; } + if (is_custom_format) { + frappe.set_route("print-format-builder", print_format.name); + return; + } + // start a new print format + frappe.prompt([ + { + label: __("New Print Format Name"), + fieldname: "print_format_name", + fieldtype: "Data", + reqd: 1, + }, + { + label: __('Based On'), + fieldname: 'based_on', + fieldtype: 'Read Only', + default: print_format.name || 'Standard' + } + ], (data) => { + frappe.route_options = { + make_new: true, + doctype: me.frm.doctype, + name: data.print_format_name, + based_on: data.based_on + }; + frappe.set_route("print-format-builder"); + }, __("New Custom Print Format"), __("Start")); }); $(document).on('new-print-format', (e) => { @@ -289,7 +305,7 @@ frappe.ui.form.PrintPreview = Class.extend({ }); }, get_mapped_printer: function () { - // returns a list of "print format: printer" mapping filtered by the current print format + // returns a list of "print format: printer" mapping filtered by the current print format let print_format_printer_map = this.get_print_format_printer_map(); if (print_format_printer_map[this.frm.doctype]) { return print_format_printer_map[this.frm.doctype].filter( @@ -554,7 +570,7 @@ frappe.ui.form.qz_init = function () { }); qz.api.setSha256Type(function (data) { // Codacy fix - /*global sha256*/ + /*global sha256*/ return sha256(data); }); resolve();