From 9abc685504cc14a949fe922d343ff42d5f0f9a2a Mon Sep 17 00:00:00 2001 From: Kenneth Sequeira Date: Wed, 2 Dec 2020 21:13:50 +0530 Subject: [PATCH 1/5] feat: add jinja templating in contract template --- erpnext/crm/doctype/contract/contract.js | 32 +++++++++++-------- erpnext/crm/doctype/contract/contract.json | 4 ++- .../contract_template/contract_template.py | 23 +++++++++++++ 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/erpnext/crm/doctype/contract/contract.js b/erpnext/crm/doctype/contract/contract.js index ee9e895130..6c0d739c89 100644 --- a/erpnext/crm/doctype/contract/contract.js +++ b/erpnext/crm/doctype/contract/contract.js @@ -1,23 +1,27 @@ // 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){ + frm.set_value("contract_terms", r.message.contract_terms); + + // 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"); + } + } }); } } diff --git a/erpnext/crm/doctype/contract/contract.json b/erpnext/crm/doctype/contract/contract.json index 0026e4a02e..fbc9f1c8d3 100755 --- a/erpnext/crm/doctype/contract/contract.json +++ b/erpnext/crm/doctype/contract/contract.json @@ -1,4 +1,5 @@ { + "actions": [], "allow_import": 1, "allow_rename": 1, "creation": "2018-04-12 06:32:04.582486", @@ -175,6 +176,7 @@ }, { "default": "0", + "fetch_from": "contract_template.requires_fulfilment", "fieldname": "requires_fulfilment", "fieldtype": "Check", "label": "Requires Fulfilment" @@ -247,7 +249,7 @@ ], "is_submittable": 1, "links": [], - "modified": "2020-03-30 06:56:07.257932", + "modified": "2020-12-02 21:12:44.118155", "modified_by": "Administrator", "module": "CRM", "name": "Contract", diff --git a/erpnext/crm/doctype/contract_template/contract_template.py b/erpnext/crm/doctype/contract_template/contract_template.py index 601ee9a28b..48ab8aad47 100644 --- a/erpnext/crm/doctype/contract_template/contract_template.py +++ b/erpnext/crm/doctype/contract_template/contract_template.py @@ -5,6 +5,29 @@ 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 + } \ No newline at end of file From 131e46bab79c6f275fd0358ffbb0510a2bc2eef4 Mon Sep 17 00:00:00 2001 From: Kenneth Sequeira Date: Wed, 2 Dec 2020 21:37:55 +0530 Subject: [PATCH 2/5] chore: add help section for Jinja --- .../contract_template/contract_template.json | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/erpnext/crm/doctype/contract_template/contract_template.json b/erpnext/crm/doctype/contract_template/contract_template.json index 5e4582f8d3..9fc24798cd 100644 --- a/erpnext/crm/doctype/contract_template/contract_template.json +++ b/erpnext/crm/doctype/contract_template/contract_template.json @@ -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": "

Contract Template Example

\n\n
Contract for Customer {{ party_name }}\n\n-Valid From : {{ start_date }} \n-Valid To : {{ end_date }}\n
\n\n

How to get fieldnames

\n\n

The fieldnames you can use in your email template are the fields in the document from which you are sending the email. You can find out the fields of any documents via Setup > Customize Form View and selecting the document type (e.g. Contract)

\n\n

Templating

\n\n

Templates are compiled using the Jinja Templating Language. To learn more about Jinja, read this documentation.

" } ], "links": [], - "modified": "2020-11-11 17:49:44.879363", + "modified": "2020-12-02 21:36:53.097074", "modified_by": "Administrator", "module": "CRM", "name": "Contract Template", From c4eb41c92b399bbb9f4ae005ef98522b30eaa227 Mon Sep 17 00:00:00 2001 From: Kenneth Sequeira Date: Fri, 4 Dec 2020 15:00:22 +0530 Subject: [PATCH 3/5] fix; styling for sider --- erpnext/crm/doctype/contract/contract.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/erpnext/crm/doctype/contract/contract.js b/erpnext/crm/doctype/contract/contract.js index 6c0d739c89..b02d06a601 100644 --- a/erpnext/crm/doctype/contract/contract.js +++ b/erpnext/crm/doctype/contract/contract.js @@ -11,7 +11,7 @@ frappe.ui.form.on("Contract", { doc: frm.doc }, callback: function(r) { - if(r && r.message){ + if (r && r.message) { frm.set_value("contract_terms", r.message.contract_terms); // Populate the fulfilment terms table from a contract template, if any From 5f95ba57a8b66e3d4f493c05e4342cf205fdded4 Mon Sep 17 00:00:00 2001 From: Kenneth Sequeira Date: Mon, 7 Dec 2020 10:45:13 +0530 Subject: [PATCH 4/5] fix: py code clean up and doc help --- erpnext/crm/doctype/contract_template/contract_template.json | 4 ++-- erpnext/crm/doctype/contract_template/contract_template.py | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/erpnext/crm/doctype/contract_template/contract_template.json b/erpnext/crm/doctype/contract_template/contract_template.json index 9fc24798cd..7cc5ec13cf 100644 --- a/erpnext/crm/doctype/contract_template/contract_template.json +++ b/erpnext/crm/doctype/contract_template/contract_template.json @@ -52,11 +52,11 @@ "fieldname": "contract_template_help", "fieldtype": "HTML", "label": "Contract Template Help", - "options": "

Contract Template Example

\n\n
Contract for Customer {{ party_name }}\n\n-Valid From : {{ start_date }} \n-Valid To : {{ end_date }}\n
\n\n

How to get fieldnames

\n\n

The fieldnames you can use in your email template are the fields in the document from which you are sending the email. You can find out the fields of any documents via Setup > Customize Form View and selecting the document type (e.g. Contract)

\n\n

Templating

\n\n

Templates are compiled using the Jinja Templating Language. To learn more about Jinja, read this documentation.

" + "options": "

Contract Template Example

\n\n
Contract for Customer {{ party_name }}\n\n-Valid From : {{ start_date }} \n-Valid To : {{ end_date }}\n
\n\n

How to get fieldnames

\n\n

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 > Customize Form View and selecting the document type (e.g. Contract)

\n\n

Templating

\n\n

Templates are compiled using the Jinja Templating Language. To learn more about Jinja, read this documentation.

" } ], "links": [], - "modified": "2020-12-02 21:36:53.097074", + "modified": "2020-12-07 10:44:22.587047", "modified_by": "Administrator", "module": "CRM", "name": "Contract Template", diff --git a/erpnext/crm/doctype/contract_template/contract_template.py b/erpnext/crm/doctype/contract_template/contract_template.py index 48ab8aad47..69fd86f7fb 100644 --- a/erpnext/crm/doctype/contract_template/contract_template.py +++ b/erpnext/crm/doctype/contract_template/contract_template.py @@ -10,8 +10,6 @@ from six import string_types import json class ContractTemplate(Document): - pass - def validate(self): if self.contract_terms: validate_template(self.contract_terms) From 85af44b7ed255e799e4869556927357421cb7c3e Mon Sep 17 00:00:00 2001 From: Kenneth Sequeira Date: Mon, 7 Dec 2020 11:18:20 +0530 Subject: [PATCH 5/5] fix: client side UX fixes --- erpnext/crm/doctype/contract/contract.js | 16 ++++++++++------ erpnext/crm/doctype/contract/contract.json | 3 +-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/erpnext/crm/doctype/contract/contract.js b/erpnext/crm/doctype/contract/contract.js index b02d06a601..9968855163 100644 --- a/erpnext/crm/doctype/contract/contract.js +++ b/erpnext/crm/doctype/contract/contract.js @@ -12,14 +12,18 @@ frappe.ui.form.on("Contract", { }, 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); - // 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"); + 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"); + } } } }); diff --git a/erpnext/crm/doctype/contract/contract.json b/erpnext/crm/doctype/contract/contract.json index fbc9f1c8d3..de3230f0e6 100755 --- a/erpnext/crm/doctype/contract/contract.json +++ b/erpnext/crm/doctype/contract/contract.json @@ -176,7 +176,6 @@ }, { "default": "0", - "fetch_from": "contract_template.requires_fulfilment", "fieldname": "requires_fulfilment", "fieldtype": "Check", "label": "Requires Fulfilment" @@ -249,7 +248,7 @@ ], "is_submittable": 1, "links": [], - "modified": "2020-12-02 21:12:44.118155", + "modified": "2020-12-07 11:15:58.385521", "modified_by": "Administrator", "module": "CRM", "name": "Contract",