Added validations, test-cases to Tax Rule

This commit is contained in:
Neil Trini Lasrado 2015-08-17 14:55:15 +05:30 committed by Saurabh
parent 72e6aa160c
commit 949d7dbaba
5 changed files with 106 additions and 5 deletions

View File

@ -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");

View File

@ -2,6 +2,7 @@
"allow_copy": 0, "allow_copy": 0,
"allow_import": 1, "allow_import": 1,
"allow_rename": 0, "allow_rename": 0,
"autoname": "TR.####",
"creation": "2015-08-07 02:33:52.670866", "creation": "2015-08-07 02:33:52.670866",
"custom": 0, "custom": 0,
"docstatus": 0, "docstatus": 0,
@ -320,7 +321,7 @@
"in_list_view": 0, "in_list_view": 0,
"label": "Sales Tax Template", "label": "Sales Tax Template",
"no_copy": 0, "no_copy": 0,
"options": "Sales Taxes and Charges", "options": "Sales Taxes and Charges Template",
"permlevel": 0, "permlevel": 0,
"precision": "", "precision": "",
"print_hide": 0, "print_hide": 0,
@ -342,7 +343,7 @@
"in_list_view": 0, "in_list_view": 0,
"label": "Purchase Tax Template", "label": "Purchase Tax Template",
"no_copy": 0, "no_copy": 0,
"options": "Purchase Taxes and Charges", "options": "Purchase Taxes and Charges Template",
"permlevel": 0, "permlevel": 0,
"precision": "", "precision": "",
"print_hide": 0, "print_hide": 0,
@ -374,6 +375,7 @@
}, },
{ {
"allow_on_submit": 0, "allow_on_submit": 0,
"default": "1",
"fieldname": "priority", "fieldname": "priority",
"fieldtype": "Int", "fieldtype": "Int",
"hidden": 0, "hidden": 0,
@ -440,7 +442,7 @@
"is_submittable": 0, "is_submittable": 0,
"issingle": 0, "issingle": 0,
"istable": 0, "istable": 0,
"modified": "2015-08-13 09:12:42.118276", "modified": "2015-08-14 08:10:56.694925",
"modified_by": "Administrator", "modified_by": "Administrator",
"module": "Accounts", "module": "Accounts",
"name": "Tax Rule", "name": "Tax Rule",

View File

@ -4,7 +4,62 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import frappe import frappe
from frappe import _
from frappe.model.document import Document from frappe.model.document import Document
class IncorrectCustomerGroup(frappe.ValidationError): pass
class TaxRule(Document): 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)))

View File

@ -5,8 +5,39 @@ from __future__ import unicode_literals
import frappe import frappe
import unittest 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') # test_records = frappe.get_test_records('Tax Rule')
class TestTaxRule(unittest.TestCase): 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

View File

@ -4,5 +4,11 @@
"doctype": "Customer Group", "doctype": "Customer Group",
"is_group": "No", "is_group": "No",
"parent_customer_group": "All Customer Groups" "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"
} }
] ]