fix bugs: add `payment_schedule` if `payment_terms_schedule` specified
diff --git a/erpnext/buying/doctype/purchase_order/test_purchase_order.py b/erpnext/buying/doctype/purchase_order/test_purchase_order.py
index 1bfc9cb..44279d5 100644
--- a/erpnext/buying/doctype/purchase_order/test_purchase_order.py
+++ b/erpnext/buying/doctype/purchase_order/test_purchase_order.py
@@ -85,14 +85,12 @@
 		self.assertEquals(len(pi.get("items", [])), 1)
 
 	def test_make_purchase_invoice_with_terms(self):
-		po = create_purchase_order(do_not_submit=True)
+		po = create_purchase_order(do_not_save=True)
 
 		self.assertRaises(frappe.ValidationError, make_purchase_invoice, po.name)
 
 		po.update(
-			{"payment_schedule": get_payment_terms(
-				"_Test Payment Term Template", po.transaction_date, po.grand_total
-			)}
+			{"payment_terms_template": "_Test Payment Term Template"}
 		)
 
 		po.save()
@@ -103,14 +101,15 @@
 		self.assertEqual(po.payment_schedule[1].payment_amount, 2500.0)
 		self.assertEqual(po.payment_schedule[1].due_date, add_days(po.transaction_date, 30))
 		pi = make_purchase_invoice(po.name)
+		pi.save()
 
 		self.assertEquals(pi.doctype, "Purchase Invoice")
 		self.assertEquals(len(pi.get("items", [])), 1)
 
 		self.assertEqual(pi.payment_schedule[0].payment_amount, 2500.0)
-		self.assertEqual(pi.payment_schedule[0].due_date.strftime(DATE_FORMAT), po.transaction_date)
+		self.assertEqual(pi.payment_schedule[0].due_date, po.transaction_date)
 		self.assertEqual(pi.payment_schedule[1].payment_amount, 2500.0)
-		self.assertEqual(pi.payment_schedule[1].due_date.strftime(DATE_FORMAT), add_days(po.transaction_date, 30))
+		self.assertEqual(pi.payment_schedule[1].due_date, add_days(po.transaction_date, 30))
 
 	def test_subcontracting(self):
 		po = create_purchase_order(item_code="_Test FG Item", is_subcontracted="Yes")
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index 693513d..8c070d4 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -26,7 +26,9 @@
 		return self.__company_currency
 
 	def onload(self):
+		print("onload called:", self.get("__onload"))
 		self.get("__onload").make_payment_via_journal_entry = frappe.db.get_single_value('Accounts Settings', 'make_payment_via_journal_entry')
+		self.set_payment_schedule()
 
 	def validate(self):
 		if self.get("_action") and self._action != "update_after_submit":
@@ -51,6 +53,8 @@
 				self.set_due_date()
 			self.validate_due_date()
 			self.validate_advance_entries()
+		elif self.doctype in ("Quotation", "Purchase Order", "Sales Order"):
+			self.set_payment_schedule()
 
 		if self.meta.get_field("taxes_and_charges"):
 			self.validate_enabled_taxes_and_charges()
@@ -608,13 +612,15 @@
 			self.remove(item)
 
 	def set_payment_schedule(self):
-		due_date = self.due_date or get_due_date(self.posting_date)
+		posting_date = self.get("posting_date") or self.get("transaction_date")
+		date = self.get("due_date")
+		due_date = date or posting_date
 
-		if self.get("payment_terms_template"):
-			data = get_payment_terms(self.payment_terms_template, self.posting_date, self.grand_total)
+		if self.get("payment_terms_template") and not self.get("payment_schedule"):
+			data = get_payment_terms(self.payment_terms_template, posting_date, self.grand_total)
 			for item in data:
 				self.append("payment_schedule", item)
-		else:
+		elif not self.get("payment_schedule"):
 			data = dict(due_date=due_date, invoice_portion=100, payment_amount=self.grand_total)
 			self.append("payment_schedule", data)
 
diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js
index 2d11d6b..6344a96 100644
--- a/erpnext/public/js/controllers/transaction.js
+++ b/erpnext/public/js/controllers/transaction.js
@@ -123,6 +123,11 @@
 			}
 		}
 
+		if(this.frm.fields_dict["payment_terms_template"]){
+			console.log("triggered");
+			this.frm.trigger("payment_terms_template");
+		}
+
 		if(this.frm.fields_dict["taxes"]) {
 			this["taxes_remove"] = this.calculate_taxes_and_totals;
 		}
diff --git a/erpnext/selling/doctype/quotation/test_quotation.py b/erpnext/selling/doctype/quotation/test_quotation.py
index f40a4ca..bb083ee 100644
--- a/erpnext/selling/doctype/quotation/test_quotation.py
+++ b/erpnext/selling/doctype/quotation/test_quotation.py
@@ -40,16 +40,15 @@
 		quotation = frappe.copy_doc(test_records[0])
 		quotation.transaction_date = nowdate()
 		quotation.valid_till = add_months(quotation.transaction_date, 1)
-		quotation.insert()
 		quotation.update(
-			{"payment_schedule": get_payment_terms(
-				"_Test Payment Term Template", quotation.transaction_date, quotation.grand_total
-			)}
+			{"payment_terms_template": "_Test Payment Term Template"}
 		)
+		quotation.insert()
 
 		self.assertRaises(frappe.ValidationError, make_sales_order, quotation.name)
 		quotation.save()
 		quotation.submit()
+
 		self.assertEqual(quotation.payment_schedule[0].payment_amount, 8906.25)
 		self.assertEqual(quotation.payment_schedule[0].due_date, quotation.transaction_date)
 		self.assertEqual(quotation.payment_schedule[1].payment_amount, 8906.25)
@@ -62,16 +61,19 @@
 		self.assertEquals(sales_order.get("items")[0].doctype, "Sales Order Item")
 		self.assertEquals(sales_order.get("items")[0].prevdoc_docname, quotation.name)
 		self.assertEquals(sales_order.customer, "_Test Customer")
-		self.assertEqual(sales_order.payment_schedule[0].payment_amount, 8906.25)
-		self.assertEqual(sales_order.payment_schedule[0].due_date.strftime(DATE_FORMAT), quotation.transaction_date)
-		self.assertEqual(sales_order.payment_schedule[1].payment_amount, 8906.25)
-		self.assertEqual(sales_order.payment_schedule[1].due_date.strftime(DATE_FORMAT), add_days(quotation.transaction_date, 30))
 
 		sales_order.delivery_date = "2014-01-01"
 		sales_order.naming_series = "_T-Quotation-"
 		sales_order.transaction_date = nowdate()
 		sales_order.insert()
 
+		self.assertEqual(sales_order.payment_schedule[0].payment_amount, 8906.25)
+		self.assertEqual(sales_order.payment_schedule[0].due_date, quotation.transaction_date)
+		self.assertEqual(sales_order.payment_schedule[1].payment_amount, 8906.25)
+		self.assertEqual(
+			sales_order.payment_schedule[1].due_date, add_days(quotation.transaction_date, 30)
+		)
+
 	def test_valid_till(self):
 		from erpnext.selling.doctype.quotation.quotation import make_sales_order