diff --git a/erpnext/accounts/doctype/tax_rule/tax_rule.js b/erpnext/accounts/doctype/tax_rule/tax_rule.js new file mode 100644 index 0000000000..b9221785d5 --- /dev/null +++ b/erpnext/accounts/doctype/tax_rule/tax_rule.js @@ -0,0 +1,7 @@ +// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors +// License: GNU General Public License v3. See license.txt + +cur_frm.add_fetch("customer", "customer_group", "customer_group" ); + +this.frm.toggle_reqd("sales_tax_template", this.frm.doc.tax_type=="Sales"); +this.frm.toggle_reqd("purchase_tax_template", this.frm.doc.tax_type=="Purchase"); \ No newline at end of file diff --git a/erpnext/accounts/doctype/tax_rule/tax_rule.json b/erpnext/accounts/doctype/tax_rule/tax_rule.json index 7e20f41291..f2df6544c4 100644 --- a/erpnext/accounts/doctype/tax_rule/tax_rule.json +++ b/erpnext/accounts/doctype/tax_rule/tax_rule.json @@ -2,6 +2,7 @@ "allow_copy": 0, "allow_import": 1, "allow_rename": 0, + "autoname": "TR.####", "creation": "2015-08-07 02:33:52.670866", "custom": 0, "docstatus": 0, @@ -320,7 +321,7 @@ "in_list_view": 0, "label": "Sales Tax Template", "no_copy": 0, - "options": "Sales Taxes and Charges", + "options": "Sales Taxes and Charges Template", "permlevel": 0, "precision": "", "print_hide": 0, @@ -342,7 +343,7 @@ "in_list_view": 0, "label": "Purchase Tax Template", "no_copy": 0, - "options": "Purchase Taxes and Charges", + "options": "Purchase Taxes and Charges Template", "permlevel": 0, "precision": "", "print_hide": 0, @@ -374,6 +375,7 @@ }, { "allow_on_submit": 0, + "default": "1", "fieldname": "priority", "fieldtype": "Int", "hidden": 0, @@ -440,7 +442,7 @@ "is_submittable": 0, "issingle": 0, "istable": 0, - "modified": "2015-08-13 09:12:42.118276", + "modified": "2015-08-14 08:10:56.694925", "modified_by": "Administrator", "module": "Accounts", "name": "Tax Rule", diff --git a/erpnext/accounts/doctype/tax_rule/tax_rule.py b/erpnext/accounts/doctype/tax_rule/tax_rule.py index f4bf17746f..d1b0bdc8de 100644 --- a/erpnext/accounts/doctype/tax_rule/tax_rule.py +++ b/erpnext/accounts/doctype/tax_rule/tax_rule.py @@ -4,7 +4,62 @@ from __future__ import unicode_literals import frappe +from frappe import _ from frappe.model.document import Document +class IncorrectCustomerGroup(frappe.ValidationError): pass + class TaxRule(Document): - pass + def validate(self): + self.validate_tax_template() + self.validate_customer_group() + self.validate_date() + self.validate_filters() + + def validate_tax_template(self): + if not (self.sales_tax_template or self.purchase_tax_template): + frappe.throw(_("Tax Template is mandatory.")) + if self.tax_type=="Sales": + self.purchase_tax_template= None + else: + self.sales_tax_template= None + + def validate_customer_group(self): + if self.customer and self.customer_group: + if not frappe.db.get_value("Customer", self.customer, "customer_group") == self.customer_group: + frappe.throw(_("Customer {0} does not belong to customer group {1}"). \ + format(self.customer, self.customer_group), IncorrectCustomerGroup) + + def validate_date(self): + if self.from_date and self.to_date and self.from_date > self.to_date: + frappe.throw(_("From Date cannot be greater than To Date")) + + def validate_filters(self): + filters = { + "customer": self.customer, + "customer_group": self.customer_group, + "billing_city": self.billing_city, + "billing_country": self.billing_country, + "shipping_city": self.shipping_city, + "shipping_country": self.shipping_country, + "tax_type": self.tax_type, + "company": self.company + } + + conds="" + for d in filters: + if conds: + conds += " and " + conds += """{0} = '{1}'""".format(d, filters[d]) + + conds += """ and ((from_date > '{from_date}' and from_date < '{to_date}') or + (to_date > '{from_date}' and to_date < '{to_date}') or + ('{from_date}' > from_date and '{from_date}' < to_date) or + ('{from_date}' = from_date and '{to_date}' = to_date))""".format(from_date=self.from_date, to_date=self.to_date) + + tax_rule = frappe.db.sql("select name, priority \ + from `tabTax Rule` where {0} and name != '{1}'".format(conds, self.name), as_dict=1) + + if tax_rule: + if tax_rule[0].priority == self.priority: + frappe.throw(_("Tax Rule Conflicts with {0}".format(tax_rule[0].name))) \ No newline at end of file diff --git a/erpnext/accounts/doctype/tax_rule/test_tax_rule.py b/erpnext/accounts/doctype/tax_rule/test_tax_rule.py index 4712243495..7a2c6ef080 100644 --- a/erpnext/accounts/doctype/tax_rule/test_tax_rule.py +++ b/erpnext/accounts/doctype/tax_rule/test_tax_rule.py @@ -5,8 +5,39 @@ from __future__ import unicode_literals import frappe import unittest +from frappe.utils import nowdate, add_days +from erpnext.accounts.doctype.tax_rule.tax_rule import IncorrectCustomerGroup # test_records = frappe.get_test_records('Tax Rule') class TestTaxRule(unittest.TestCase): - pass + def test_customer_group(self): + tax_rule = make_tax_rule_test_record(customer_group= "_Test Customer Group 1", do_not_save= True) + self.assertRaises(IncorrectCustomerGroup, tax_rule.save) + + def test_tax_template(self): + tax_rule = make_tax_rule_test_record() + self.assertEquals(tax_rule.purchase_tax_template, None) + +def make_tax_rule_test_record(**args): + args = frappe._dict(args) + + tax_rule = frappe.new_doc("Tax Rule") + tax_rule.customer= args.customer or "_Test Customer" + tax_rule.customer_group= args.customer_group or "_Test Customer Group" + tax_rule.billing_city= args.billing_city or "_Test City" + tax_rule.billing_country= args.billing_country or "_Test Country" + tax_rule.shipping_city= args.shipping_city or "_Test City" + tax_rule.shipping_country= args.shipping_country or "_Test Country" + tax_rule.from_date= args.from_date or nowdate() + tax_rule.to_date= args.to_date or add_days(nowdate(), 1) + tax_rule.tax_type= args.tax_type or "Sales" + tax_rule.sales_tax_template= args.sales_tax_template or "_Test Sales Taxes and Charges Template" + tax_rule.purchase_tax_template= args.purchase_tax_template or "_Test Purchase Taxes and Charges Template" + tax_rule.priority= args.priority or 1 + tax_rule.compant= args.company or "_Test Company" + + if not args.do_not_save: + tax_rule.save() + return tax_rule + \ No newline at end of file diff --git a/erpnext/setup/doctype/customer_group/test_records.json b/erpnext/setup/doctype/customer_group/test_records.json index 14d815ce76..cc3f87e098 100644 --- a/erpnext/setup/doctype/customer_group/test_records.json +++ b/erpnext/setup/doctype/customer_group/test_records.json @@ -4,5 +4,11 @@ "doctype": "Customer Group", "is_group": "No", "parent_customer_group": "All Customer Groups" + }, + { + "customer_group_name": "_Test Customer Group 1", + "doctype": "Customer Group", + "is_group": "No", + "parent_customer_group": "All Customer Groups" } ]