Merge pull request #24046 from kennethsequeira/cont-temp

feat: add jinja templating in Contract Template
This commit is contained in:
Marica 2020-12-09 18:22:18 +05:30 committed by GitHub
commit 6cf36bfb0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 18 deletions

View File

@ -1,23 +1,31 @@
// Copyright (c) 2018, Frappe Technologies Pvt. Ltd. and contributors
// For license information, please see license.txt
cur_frm.add_fetch("contract_template", "contract_terms", "contract_terms");
cur_frm.add_fetch("contract_template", "requires_fulfilment", "requires_fulfilment");
// Add fulfilment terms from contract template into contract
frappe.ui.form.on("Contract", {
contract_template: function (frm) {
// Populate the fulfilment terms table from a contract template, if any
if (frm.doc.contract_template) {
frappe.model.with_doc("Contract Template", frm.doc.contract_template, function () {
var tabletransfer = frappe.model.get_doc("Contract Template", frm.doc.contract_template);
frm.doc.fulfilment_terms = [];
$.each(tabletransfer.fulfilment_terms, function (index, row) {
var d = frm.add_child("fulfilment_terms");
d.requirement = row.requirement;
frm.refresh_field("fulfilment_terms");
});
frappe.call({
method: 'erpnext.crm.doctype.contract_template.contract_template.get_contract_template',
args: {
template_name: frm.doc.contract_template,
doc: frm.doc
},
callback: function(r) {
if (r && r.message) {
let contract_template = r.message.contract_template;
frm.set_value("contract_terms", r.message.contract_terms);
frm.set_value("requires_fulfilment", contract_template.requires_fulfilment);
if (frm.doc.requires_fulfilment) {
// Populate the fulfilment terms table from a contract template, if any
r.message.contract_template.fulfilment_terms.forEach(element => {
let d = frm.add_child("fulfilment_terms");
d.requirement = element.requirement;
});
frm.refresh_field("fulfilment_terms");
}
}
}
});
}
}

View File

@ -1,4 +1,5 @@
{
"actions": [],
"allow_import": 1,
"allow_rename": 1,
"creation": "2018-04-12 06:32:04.582486",
@ -247,7 +248,7 @@
],
"is_submittable": 1,
"links": [],
"modified": "2020-03-30 06:56:07.257932",
"modified": "2020-12-07 11:15:58.385521",
"modified_by": "Administrator",
"module": "CRM",
"name": "Contract",

View File

@ -11,7 +11,9 @@
"contract_terms",
"sb_fulfilment",
"requires_fulfilment",
"fulfilment_terms"
"fulfilment_terms",
"section_break_6",
"contract_template_help"
],
"fields": [
{
@ -41,10 +43,20 @@
"fieldtype": "Table",
"label": "Fulfilment Terms and Conditions",
"options": "Contract Template Fulfilment Terms"
},
{
"fieldname": "section_break_6",
"fieldtype": "Section Break"
},
{
"fieldname": "contract_template_help",
"fieldtype": "HTML",
"label": "Contract Template Help",
"options": "<h4>Contract Template Example</h4>\n\n<pre>Contract for Customer {{ party_name }}\n\n-Valid From : {{ start_date }} \n-Valid To : {{ end_date }}\n</pre>\n\n<h4>How to get fieldnames</h4>\n\n<p>The field names you can use in your Contract Template are the fields in the Contract for which you are creating the template. You can find out the fields of any documents via Setup &gt; Customize Form View and selecting the document type (e.g. Contract)</p>\n\n<h4>Templating</h4>\n\n<p>Templates are compiled using the Jinja Templating Language. To learn more about Jinja, <a class=\"strong\" href=\"http://jinja.pocoo.org/docs/dev/templates/\">read this documentation.</a></p>"
}
],
"links": [],
"modified": "2020-11-11 17:49:44.879363",
"modified": "2020-12-07 10:44:22.587047",
"modified_by": "Administrator",
"module": "CRM",
"name": "Contract Template",

View File

@ -5,6 +5,27 @@
from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
from frappe.utils.jinja import validate_template
from six import string_types
import json
class ContractTemplate(Document):
pass
def validate(self):
if self.contract_terms:
validate_template(self.contract_terms)
@frappe.whitelist()
def get_contract_template(template_name, doc):
if isinstance(doc, string_types):
doc = json.loads(doc)
contract_template = frappe.get_doc("Contract Template", template_name)
contract_terms = None
if contract_template.contract_terms:
contract_terms = frappe.render_template(contract_template.contract_terms, doc)
return {
'contract_template': contract_template,
'contract_terms': contract_terms
}