From 79a415e3b87fb7c312863b76b5052349ca06b537 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Fri, 8 Apr 2016 17:05:49 +0530 Subject: [PATCH] [fix] Use for shopping cart validation in Tax Rule --- erpnext/accounts/doctype/tax_rule/tax_rule.js | 15 +------------- erpnext/accounts/doctype/tax_rule/tax_rule.py | 20 ++++++++++++++----- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/erpnext/accounts/doctype/tax_rule/tax_rule.js b/erpnext/accounts/doctype/tax_rule/tax_rule.js index 4c35370eae..935ea62b67 100644 --- a/erpnext/accounts/doctype/tax_rule/tax_rule.js +++ b/erpnext/accounts/doctype/tax_rule/tax_rule.js @@ -19,19 +19,6 @@ frappe.ui.form.on("Tax Rule", "refresh", function(frm) { frappe.ui.form.trigger("Tax Rule", "tax_type"); }) -frappe.ui.form.on("Tax Rule", "use_for_shopping_cart", function(frm) { - if(!frm.doc.use_for_shopping_cart && - (frappe.get_list("Tax Rule", {"use_for_shopping_cart":1}).length == 0)) { - frappe.model.get_value("Shopping Cart Settings", "Shopping Cart Settings", - "enabled", function(docfield) { - if(docfield.enabled){ - frm.set_value("use_for_shopping_cart", 1); - frappe.throw(__("Shopping Cart is enabled")); - } - }); - } -}) - frappe.ui.form.on("Tax Rule", "customer", function(frm) { frappe.call({ method:"erpnext.accounts.doctype.tax_rule.tax_rule.get_party_details", @@ -64,4 +51,4 @@ frappe.ui.form.on("Tax Rule", "supplier", function(frm) { } } }); -}); \ No newline at end of file +}); diff --git a/erpnext/accounts/doctype/tax_rule/tax_rule.py b/erpnext/accounts/doctype/tax_rule/tax_rule.py index 7b6208293a..ce20d3a3d5 100644 --- a/erpnext/accounts/doctype/tax_rule/tax_rule.py +++ b/erpnext/accounts/doctype/tax_rule/tax_rule.py @@ -6,7 +6,7 @@ from __future__ import unicode_literals import frappe from frappe import _ from frappe.model.document import Document -from frappe.utils import cstr +from frappe.utils import cstr, cint class IncorrectCustomerGroup(frappe.ValidationError): pass class IncorrectSupplierType(frappe.ValidationError): pass @@ -20,15 +20,16 @@ class TaxRule(Document): self.validate_tax_template() self.validate_date() self.validate_filters() + self.validate_use_for_shopping_cart() def validate_tax_template(self): if self.tax_type== "Sales": - self.purchase_tax_template = self.supplier = self.supplier_type= None + self.purchase_tax_template = self.supplier = self.supplier_type = None if self.customer: self.customer_group = None else: - self.sales_tax_template= self.customer = self.customer_group= None + self.sales_tax_template = self.customer = self.customer_group = None if self.supplier: self.supplier_type = None @@ -81,6 +82,15 @@ class TaxRule(Document): if tax_rule[0].priority == self.priority: frappe.throw(_("Tax Rule Conflicts with {0}".format(tax_rule[0].name)), ConflictingTaxRule) + def validate_use_for_shopping_cart(self): + '''If shopping cart is enabled and no tax rule exists for shopping cart, enable this one''' + if (not self.use_for_shopping_cart + and cint(frappe.db.get_single_value('Shopping Cart Settings', 'enabled')) + and not frappe.db.get_value('Tax Rule', {'use_for_shopping_cart': 1, 'name': ['!=', self.name]})): + + self.use_for_shopping_cart = 1 + frappe.msgprint(_("Enabling 'Use for Shopping Cart', as Shopping Cart is enabled and there should be at least one Tax Rule for Shopping Cart")) + @frappe.whitelist() def get_party_details(party, party_type, args=None): out = {} @@ -109,11 +119,11 @@ def get_party_details(party, party_type, args=None): def get_tax_template(posting_date, args): """Get matching tax rule""" args = frappe._dict(args) - conditions = ["""(from_date is null or from_date = '' or from_date <= '{0}') + 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": + if key=="use_for_shopping_cart": conditions.append("use_for_shopping_cart = {0}".format(1 if value else 0)) else: conditions.append("ifnull({0}, '') in ('', '{1}')".format(key, frappe.db.escape(cstr(value))))