From 98aa5818647a37270b7132149e14083528784ab6 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Tue, 14 Nov 2017 09:32:32 +0530 Subject: [PATCH] [fix] default tax only on insert (#11544) * [fix] default tax only on insert * [fix] default tax only on insert * [fix] default tax only on insert --- erpnext/controllers/accounts_controller.py | 9 +++-- .../crm/doctype/opportunity/opportunity.py | 18 ++++----- erpnext/public/js/controllers/transaction.js | 38 +++++++++++++------ 3 files changed, 42 insertions(+), 23 deletions(-) diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py index ebd45c4c59..d3f6d38e14 100644 --- a/erpnext/controllers/accounts_controller.py +++ b/erpnext/controllers/accounts_controller.py @@ -209,10 +209,10 @@ class AccountsController(TransactionBase): tax_master_doctype = self.meta.get_field("taxes_and_charges").options - if not self.get("taxes"): + if self.is_new() and not self.get("taxes"): if not self.get("taxes_and_charges"): # get the default tax master - self.set("taxes_and_charges", frappe.db.get_value(tax_master_doctype, {"is_default": 1})) + self.taxes_and_charges = frappe.db.get_value(tax_master_doctype, {"is_default": 1}) self.append_taxes_from_master(tax_master_doctype) @@ -608,7 +608,10 @@ def get_tax_rate(account_head): @frappe.whitelist() def get_default_taxes_and_charges(master_doctype): default_tax = frappe.db.get_value(master_doctype, {"is_default": 1}) - return get_taxes_and_charges(master_doctype, default_tax) + return { + 'taxes_and_charges': default_tax, + 'taxes': get_taxes_and_charges(master_doctype, default_tax) + } @frappe.whitelist() def get_taxes_and_charges(master_doctype, master_name): diff --git a/erpnext/crm/doctype/opportunity/opportunity.py b/erpnext/crm/doctype/opportunity/opportunity.py index 970fd570ff..e9a7baa4bc 100644 --- a/erpnext/crm/doctype/opportunity/opportunity.py +++ b/erpnext/crm/doctype/opportunity/opportunity.py @@ -48,16 +48,16 @@ class Opportunity(TransactionBase): # check if customer is already created agains the self.contact_email customer = frappe.db.sql("""select distinct `tabDynamic Link`.link_name as customer - from + from `tabContact`, `tabDynamic Link` where `tabContact`.email_id='{0}' - and + and `tabContact`.name=`tabDynamic Link`.parent and - ifnull(`tabDynamic Link`.link_name, '')<>'' - and - `tabDynamic Link`.link_doctype='Customer' + ifnull(`tabDynamic Link`.link_name, '')<>'' + and + `tabDynamic Link`.link_doctype='Customer' """.format(self.contact_email), as_dict=True) if customer and customer[0].customer: self.customer = customer[0].customer @@ -118,9 +118,9 @@ class Opportunity(TransactionBase): def has_ordered_quotation(self): return frappe.db.sql(""" - select q.name + select q.name from `tabQuotation` q, `tabQuotation Item` qi - where q.name = qi.parent and q.docstatus=1 and qi.prevdoc_docname =%s + where q.name = qi.parent and q.docstatus=1 and qi.prevdoc_docname =%s and q.status = 'Ordered'""", self.name) def has_lost_quotation(self): @@ -233,8 +233,8 @@ def make_quotation(source_name, target_doc=None): # get default taxes taxes = get_default_taxes_and_charges("Sales Taxes and Charges Template") - if taxes: - quotation.extend("taxes", taxes) + if taxes.get('taxes'): + quotation.update(taxes) quotation.run_method("set_missing_values") quotation.run_method("calculate_taxes_and_totals") diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js index 41fdc6e646..b07d0902a3 100644 --- a/erpnext/public/js/controllers/transaction.js +++ b/erpnext/public/js/controllers/transaction.js @@ -231,8 +231,16 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({ }, callback: function(r) { if(!r.exc) { - me.frm.set_value("taxes", r.message); - me.calculate_taxes_and_totals(); + frappe.run_serially([ + () => { + // directly set in doc, so as not to call triggers + me.frm.doc.taxes_and_charges = r.message.taxes_and_charges; + + // set taxes table + me.frm.set_value("taxes", r.message.taxes); + }, + () => me.calculate_taxes_and_totals() + ]); } } }); @@ -935,19 +943,27 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({ return; } + if (me.in_apply_price_list == true) return; + + me.in_apply_price_list = true; return this.frm.call({ method: "erpnext.stock.get_item_details.apply_price_list", args: { args: args }, callback: function(r) { if (!r.exc) { - me.in_apply_price_list = true; - me.frm.set_value("price_list_currency", r.message.parent.price_list_currency); - me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate); - me.in_apply_price_list = false; + frappe.run_serially([ + () => me.frm.set_value("price_list_currency", r.message.parent.price_list_currency), + () => me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate), + () => { + if(args.items.length) { + me._set_values_for_item_list(r.message.children); + } + }, + () => { me.in_apply_price_list = false; } + ]); - if(args.items.length) { - me._set_values_for_item_list(r.message.children); - } + } else { + me.in_apply_price_list = false; } } }); @@ -1112,11 +1128,11 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({ filters: {'item': item.item_code} } } else { - filters = { + let filters = { 'item_code': item.item_code, 'posting_date': me.frm.doc.posting_date || frappe.datetime.nowdate(), } - if(item.warehouse) filters["warehouse"] = item.warehouse + if (item.warehouse) filters["warehouse"] = item.warehouse return { query : "erpnext.controllers.queries.get_batch_no",