Apply default taxes on loading of document

This commit is contained in:
Nabin Hait 2015-05-12 15:07:02 +05:30
parent 23d6eb7e5c
commit ffc7f3fef6
6 changed files with 43 additions and 22 deletions

View File

@ -196,7 +196,7 @@ class SalesInvoice(SellingController):
# fetch charges
if self.taxes_and_charges and not len(self.get("taxes")):
self.set_taxes("taxes", "taxes_and_charges")
self.set_taxes()
def get_advances(self):
super(SalesInvoice, self).get_advances(self.debit_to, "Customer", self.customer,

View File

@ -140,30 +140,29 @@ class AccountsController(TransactionBase):
"discount_percentage", "base_rate", "rate"]:
item.set(field, ret.get(field))
def set_taxes(self, tax_parentfield, tax_master_field):
if not self.meta.get_field(tax_parentfield):
def set_taxes(self):
if not self.meta.get_field("taxes"):
return
tax_master_doctype = self.meta.get_field(tax_master_field).options
tax_master_doctype = self.meta.get_field("taxes_and_charges").options
if not self.get(tax_parentfield):
if not self.get(tax_master_field):
if not self.get("taxes"):
if not self.get("taxes_and_charges"):
# get the default tax master
self.set(tax_master_field, frappe.db.get_value(tax_master_doctype, {"is_default": 1}))
self.set("taxes_and_charges", frappe.db.get_value(tax_master_doctype, {"is_default": 1}))
self.append_taxes_from_master(tax_parentfield, tax_master_field, tax_master_doctype)
self.append_taxes_from_master(tax_master_doctype)
def append_taxes_from_master(self, tax_parentfield, tax_master_field, tax_master_doctype=None):
if self.get(tax_master_field):
def append_taxes_from_master(self, tax_master_doctype=None):
if self.get("taxes_and_charges"):
if not tax_master_doctype:
tax_master_doctype = self.meta.get_field(tax_master_field).options
tax_master_doctype = self.meta.get_field("taxes_and_charges").options
self.extend(tax_parentfield,
get_taxes_and_charges(tax_master_doctype, self.get(tax_master_field), tax_parentfield))
self.extend("taxes", get_taxes_and_charges(tax_master_doctype, self.get("taxes_and_charges")))
def set_other_charges(self):
self.set("taxes", [])
self.set_taxes("taxes", "taxes_and_charges")
self.set_taxes()
def validate_enabled_taxes_and_charges(self):
taxes_and_charges_doctype = self.meta.get_options("taxes_and_charges")
@ -335,14 +334,19 @@ class AccountsController(TransactionBase):
@frappe.whitelist()
def get_tax_rate(account_head):
return frappe.db.get_value("Account", account_head, "tax_rate")
@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)
@frappe.whitelist()
def get_taxes_and_charges(master_doctype, master_name, tax_parentfield):
def get_taxes_and_charges(master_doctype, master_name):
from frappe.model import default_fields
tax_master = frappe.get_doc(master_doctype, master_name)
taxes_and_charges = []
for i, tax in enumerate(tax_master.get(tax_parentfield)):
for i, tax in enumerate(tax_master.get("taxes")):
tax = tax.as_dict()
for fieldname in default_fields:

View File

@ -44,8 +44,6 @@ class BuyingController(StockController):
self.update_if_missing(get_party_details(self.supplier, party_type="Supplier"))
self.set_missing_item_details()
if self.get("__islocal"):
self.set_taxes("taxes", "taxes_and_charges")
def set_supplier_from_item_default(self):
if self.meta.get_field("supplier") and not self.supplier:

View File

@ -42,8 +42,6 @@ class SellingController(StockController):
# set contact and address details for customer, if they are not mentioned
self.set_missing_lead_customer_details()
self.set_price_list_and_item_details()
if self.get("__islocal"):
self.set_taxes("taxes", "taxes_and_charges")
def set_missing_lead_customer_details(self):
if getattr(self, "customer", None):

View File

@ -41,6 +41,11 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({
onload_post_render: function() {
var me = this;
if(this.frm.doc.__islocal && !(this.frm.doc.taxes || []).length
&& !(this.frm.doc.__onload ? this.frm.doc.__onload.load_after_mapping : false)) {
this.apply_default_taxes();
}
if(this.frm.doc.__islocal && this.frm.doc.company && this.frm.doc["items"] && !this.frm.doc.is_pos) {
this.calculate_taxes_and_totals();
}
@ -58,6 +63,23 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({
erpnext.pos.make_pos_btn(this.frm);
this.setup_sms();
},
apply_default_taxes: function() {
var me = this;
return frappe.call({
method: "erpnext.controllers.accounts_controller.get_default_taxes_and_charges",
args: {
"master_doctype": frappe.meta.get_docfield(me.frm.doc.doctype, "taxes_and_charges",
me.frm.doc.name).options
},
callback: function(r) {
if(!r.exc) {
me.frm.set_value("taxes", r.message);
me.calculate_taxes_and_totals();
}
}
});
},
setup_sms: function() {
var me = this;
@ -646,7 +668,6 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({
"master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
this.frm.doc.name).options,
"master_name": this.frm.doc.taxes_and_charges,
"tax_parentfield": "taxes"
},
callback: function(r) {
if(!r.exc) {

View File

@ -256,7 +256,7 @@ def set_taxes(quotation, cart_settings, billing_territory):
quotation.set("taxes", [])
# append taxes
quotation.append_taxes_from_master("taxes", "taxes_and_charges")
quotation.append_taxes_from_master()
def get_lead_or_customer():
customer = frappe.db.get_value("Contact", {"email_id": frappe.session.user}, "customer")