Merge pull request #4222 from nabinhait/tax_rule

Set tax rule based on date
This commit is contained in:
Nabin Hait 2015-10-26 13:02:21 +05:30
commit 8e3ea32d6d
2 changed files with 31 additions and 6 deletions

View File

@ -109,7 +109,8 @@ 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 = []
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 @@ def get_tax_template(posting_date, args):
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

View File

@ -26,7 +26,12 @@ erpnext.utils.get_party_details = function(frm, method, args, callback) {
}
}
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 @@ erpnext.utils.get_address_display = function(frm, address_field, display_field)
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.get_contact_details = function(frm) {
})
}
}
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;
}