Merge pull request #4222 from nabinhait/tax_rule

Set tax rule based on date
diff --git a/erpnext/accounts/doctype/tax_rule/tax_rule.py b/erpnext/accounts/doctype/tax_rule/tax_rule.py
index bff1898..7b62082 100644
--- a/erpnext/accounts/doctype/tax_rule/tax_rule.py
+++ b/erpnext/accounts/doctype/tax_rule/tax_rule.py
@@ -109,7 +109,8 @@
 def get_tax_template(posting_date, args):
 	"""Get matching tax rule"""
 	args = frappe._dict(args)
-	conditions = []
+	conditions = ["""(from_date is null  or from_date = '' or from_date <= '{0}') 
+		and (to_date is null  or to_date = '' or to_date >= '{0}')""".format(posting_date)]
 
 	for key, value in args.iteritems():
 		if key in "use_for_shopping_cart":
@@ -117,16 +118,16 @@
 		else:
 			conditions.append("ifnull({0}, '') in ('', '{1}')".format(key, frappe.db.escape(cstr(value))))
 
-	matching = frappe.db.sql("""select * from `tabTax Rule`
+	tax_rule = frappe.db.sql("""select * from `tabTax Rule`
 		where {0}""".format(" and ".join(conditions)), as_dict = True)
 
-	if not matching:
+	if not tax_rule:
 		return None
 
-	for rule in matching:
+	for rule in tax_rule:
 		rule.no_of_keys_matched = 0
 		for key in args:
 			if rule.get(key): rule.no_of_keys_matched += 1
 
-	rule = sorted(matching, lambda b, a: cmp(a.no_of_keys_matched, b.no_of_keys_matched) or cmp(a.priority, b.priority))[0]
+	rule = sorted(tax_rule, lambda b, a: cmp(a.no_of_keys_matched, b.no_of_keys_matched) or cmp(a.priority, b.priority))[0]
 	return rule.sales_tax_template or rule.purchase_tax_template
diff --git a/erpnext/public/js/utils/party.js b/erpnext/public/js/utils/party.js
index 9173b71..cc6f112 100644
--- a/erpnext/public/js/utils/party.js
+++ b/erpnext/public/js/utils/party.js
@@ -26,7 +26,12 @@
 		}
 	}
 	if(!args) return;
-
+	
+	if(frappe.meta.get_docfield(frm.doc.doctype, "taxes")) {
+		if(!erpnext.utils.validate_mandatory(frm, "Posting/Transaction Date", 
+			args.posting_date, args.party_type=="Customer" ? "customer": "supplier")) return;
+	}
+	
 	args.currency = frm.doc.currency;
 	args.company = frm.doc.company;
 	args.doctype = frm.doc.doctype;
@@ -64,6 +69,15 @@
 				if(r.message){
 					frm.set_value(display_field, r.message)
 				}
+				
+				if(frappe.meta.get_docfield(frm.doc.doctype, "taxes")) {
+					if(!erpnext.utils.validate_mandatory(frm, "Customer/Supplier", 
+						frm.doc.customer || frm.doc.supplier, address_field)) return;
+	
+					if(!erpnext.utils.validate_mandatory(frm, "Posting/Transaction Date", 
+						frm.doc.posting_date || frm.doc.transaction_date, address_field)) return;
+				} else return;
+				
 				frappe.call({
 					method: "erpnext.accounts.party.set_taxes",
 					args: {
@@ -99,3 +113,13 @@
 		})
 	}
 }
+
+erpnext.utils.validate_mandatory = function(frm, label, value, trigger_on) {
+	if(!value) {
+		frm.doc[trigger_on] = "";
+		refresh_field(trigger_on);
+		frappe.msgprint(__("Please enter {0} first", [label]));
+		return false;
+	}
+	return true;
+}
\ No newline at end of file