Skip to content

Commit

Permalink
fix: Additional discount calculations in Invoices (frappe#26553)
Browse files Browse the repository at this point in the history
* fix: Additional discount calculations in Invoices

* revert: Client side handling for Dynamic GST Rates

* fix: Add update item tax template method back

* fix: Revert refresh field

* fix: add company change trigger
  • Loading branch information
deepeshgarg007 authored Jul 26, 2021
1 parent 06fb0b9 commit 8d52a22
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 60 deletions.
69 changes: 11 additions & 58 deletions erpnext/public/js/controllers/taxes_and_totals.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,23 @@ erpnext.taxes_and_totals = erpnext.payments.extend({
this.frm.refresh_fields();
},

calculate_discount_amount: function(){
calculate_discount_amount: function() {
if (frappe.meta.get_docfield(this.frm.doc.doctype, "discount_amount")) {
this.calculate_item_values();
this.calculate_net_total();
this.set_discount_amount();
this.apply_discount_amount();
}
},

_calculate_taxes_and_totals: function() {
frappe.run_serially([
() => this.validate_conversion_rate(),
() => this.calculate_item_values(),
() => this.update_item_tax_map(),
() => this.initialize_taxes(),
() => this.determine_exclusive_rate(),
() => this.calculate_net_total(),
() => this.calculate_taxes(),
() => this.manipulate_grand_total_for_inclusive_tax(),
() => this.calculate_totals(),
() => this._cleanup()
]);
this.validate_conversion_rate();
this.calculate_item_values();
this.initialize_taxes();
this.determine_exclusive_rate();
this.calculate_net_total();
this.calculate_taxes();
this.manipulate_grand_total_for_inclusive_tax();
this.calculate_totals();
this._cleanup();
},

validate_conversion_rate: function() {
Expand All @@ -107,7 +102,7 @@ erpnext.taxes_and_totals = erpnext.payments.extend({
},

calculate_item_values: function() {
var me = this;
let me = this;
if (!this.discount_amount_applied) {
$.each(this.frm.doc["items"] || [], function(i, item) {
frappe.model.round_floats_in(item);
Expand Down Expand Up @@ -268,46 +263,6 @@ erpnext.taxes_and_totals = erpnext.payments.extend({
frappe.model.round_floats_in(this.frm.doc, ["total", "base_total", "net_total", "base_net_total"]);
},

update_item_tax_map: function() {
let me = this;
let item_codes = [];
let item_rates = {};
let item_tax_templates = {};

$.each(this.frm.doc.items || [], function(i, item) {
if (item.item_code) {
// Use combination of name and item code in case same item is added multiple times
item_codes.push([item.item_code, item.name]);
item_rates[item.name] = item.net_rate;
item_tax_templates[item.name] = item.item_tax_template;
}
});

if (item_codes.length) {
return this.frm.call({
method: "erpnext.stock.get_item_details.get_item_tax_info",
args: {
company: me.frm.doc.company,
tax_category: cstr(me.frm.doc.tax_category),
item_codes: item_codes,
item_rates: item_rates,
item_tax_templates: item_tax_templates
},
callback: function(r) {
if (!r.exc) {
$.each(me.frm.doc.items || [], function(i, item) {
if (item.name && r.message.hasOwnProperty(item.name) && r.message[item.name].item_tax_template) {
item.item_tax_template = r.message[item.name].item_tax_template;
item.item_tax_rate = r.message[item.name].item_tax_rate;
me.add_taxes_from_item_tax_template(item.item_tax_rate);
}
});
}
}
});
}
},

add_taxes_from_item_tax_template: function(item_tax_map) {
let me = this;

Expand Down Expand Up @@ -632,8 +587,6 @@ erpnext.taxes_and_totals = erpnext.payments.extend({
tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
});
}

this.frm.refresh_fields();
},

set_discount_amount: function() {
Expand Down
44 changes: 42 additions & 2 deletions erpnext/public/js/controllers/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,9 +826,9 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({

frappe.run_serially([
() => me.frm.script_manager.trigger("currency"),
() => me.update_item_tax_map(),
() => me.apply_default_taxes(),
() => me.apply_pricing_rule(),
() => me.calculate_taxes_and_totals()
() => me.apply_pricing_rule()
]);
}
}
Expand Down Expand Up @@ -1787,6 +1787,46 @@ erpnext.TransactionController = erpnext.taxes_and_totals.extend({
]);
},

update_item_tax_map: function() {
let me = this;
let item_codes = [];
let item_rates = {};
let item_tax_templates = {};

$.each(this.frm.doc.items || [], function(i, item) {
if (item.item_code) {
// Use combination of name and item code in case same item is added multiple times
item_codes.push([item.item_code, item.name]);
item_rates[item.name] = item.net_rate;
item_tax_templates[item.name] = item.item_tax_template;
}
});

if (item_codes.length) {
return this.frm.call({
method: "erpnext.stock.get_item_details.get_item_tax_info",
args: {
company: me.frm.doc.company,
tax_category: cstr(me.frm.doc.tax_category),
item_codes: item_codes,
item_rates: item_rates,
item_tax_templates: item_tax_templates
},
callback: function(r) {
if (!r.exc) {
$.each(me.frm.doc.items || [], function(i, item) {
if (item.name && r.message.hasOwnProperty(item.name) && r.message[item.name].item_tax_template) {
item.item_tax_template = r.message[item.name].item_tax_template;
item.item_tax_rate = r.message[item.name].item_tax_rate;
me.add_taxes_from_item_tax_template(item.item_tax_rate);
}
});
}
}
});
}
},

item_tax_template: function(doc, cdt, cdn) {
var me = this;
if(me.frm.updating_party_details) return;
Expand Down

0 comments on commit 8d52a22

Please sign in to comment.