commonified totals calculation in js
diff --git a/erpnext/buying/doctype/purchase_common/purchase_common.js b/erpnext/buying/doctype/purchase_common/purchase_common.js
index b553931..6759550 100644
--- a/erpnext/buying/doctype/purchase_common/purchase_common.js
+++ b/erpnext/buying/doctype/purchase_common/purchase_common.js
@@ -172,64 +172,6 @@
}
},
- calculate_net_total: function() {
- var me = this;
-
- this.frm.doc.base_net_total = this.frm.doc.net_total = 0.0;
- $.each(this.frm.doc["items"] || [], function(i, item) {
- me.frm.doc.base_net_total += item.base_amount;
- me.frm.doc.net_total += item.amount;
- });
-
- frappe.model.round_floats_in(this.frm.doc, ["base_net_total", "net_total"]);
- },
-
- calculate_totals: function() {
- var tax_count = this.frm.doc["taxes"] ? this.frm.doc["taxes"].length : 0;
- this.frm.doc.base_grand_total = flt(tax_count ? this.frm.doc["taxes"][tax_count - 1].total : this.frm.doc.base_net_total);
-
- this.frm.doc.base_total_taxes_and_charges = flt(this.frm.doc.base_grand_total - this.frm.doc.base_net_total, precision("base_total_taxes_and_charges"));
-
- this.frm.doc.base_grand_total = flt(this.frm.doc.base_grand_total, precision("base_grand_total"));
-
- // rounded totals
- if(frappe.meta.get_docfield(this.frm.doc.doctype, "base_rounded_total", this.frm.doc.name)) {
- this.frm.doc.base_rounded_total = Math.round(this.frm.doc.base_grand_total);
- }
-
- // other charges added/deducted
- this.frm.doc.base_taxes_and_charges_added = 0.0
- this.frm.doc.base_taxes_and_charges_deducted = 0.0
- if(tax_count) {
- this.frm.doc.base_taxes_and_charges_added = frappe.utils.sum($.map(this.frm.doc["taxes"],
- function(tax) { return (tax.add_deduct_tax == "Add"
- && in_list(["Valuation and Total", "Total"], tax.category)) ?
- tax.tax_amount : 0.0; }));
-
- this.frm.doc.base_taxes_and_charges_deducted = frappe.utils.sum($.map(this.frm.doc["taxes"],
- function(tax) { return (tax.add_deduct_tax == "Deduct"
- && in_list(["Valuation and Total", "Total"], tax.category)) ?
- tax.tax_amount : 0.0; }));
-
- frappe.model.round_floats_in(this.frm.doc,
- ["base_taxes_and_charges_added", "base_taxes_and_charges_deducted"]);
- }
-
- this.frm.doc.grand_total = flt((this.frm.doc.base_taxes_and_charges_added || this.frm.doc.base_taxes_and_charges_deducted) ?
- flt(this.frm.doc.base_grand_total / this.frm.doc.conversion_rate) : this.frm.doc.net_total);
-
- this.frm.doc.grand_total = flt(this.frm.doc.grand_total, precision("grand_total"));
-
- if(frappe.meta.get_docfield(this.frm.doc.doctype, "rounded_total", this.frm.doc.name)) {
- this.frm.doc.rounded_total = Math.round(this.frm.doc.grand_total);
- }
-
- this.frm.doc.taxes_and_charges_added = flt(this.frm.doc.base_taxes_and_charges_added /
- this.frm.doc.conversion_rate, precision("taxes_and_charges_added"));
- this.frm.doc.taxes_and_charges_deducted = flt(this.frm.doc.base_taxes_and_charges_deducted /
- this.frm.doc.conversion_rate, precision("taxes_and_charges_deducted"));
- },
-
calculate_outstanding_amount: function() {
if(this.frm.doc.doctype == "Purchase Invoice" && this.frm.doc.docstatus < 2) {
frappe.model.round_floats_in(this.frm.doc, ["base_grand_total", "total_advance", "write_off_amount"]);
@@ -240,18 +182,6 @@
}
},
- set_item_tax_amount: function(item, tax, current_tax_amount) {
- // item_tax_amount is the total tax amount applied on that item
- // stored for valuation
- //
- // TODO: rename item_tax_amount to valuation_tax_amount
- if(["Valuation", "Valuation and Total"].indexOf(tax.category) != -1 &&
- frappe.meta.get_docfield(item.doctype, "item_tax_amount", item.parent || item.name)) {
- // accumulate only if tax is for Valuation / Valuation and Total
- item.item_tax_amount += flt(current_tax_amount, precision("item_tax_amount", item));
- }
- },
-
change_form_labels: function(company_currency) {
var me = this;
var field_label_map = {};
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index 8b1d7c5..3ad82b0 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -343,8 +343,7 @@
def _set_in_company_currency(self, item, print_field, base_field):
"""set values in base currency"""
value_in_company_currency = flt(self.conversion_rate *
- flt(item.get(print_field), self.precision(print_field, item)),
- self.precision(base_field, item))
+ flt(item.get(print_field), self.precision(print_field, item)), self.precision(base_field, item))
item.set(base_field, value_in_company_currency)
def validate_enabled_taxes_and_charges(self):
diff --git a/erpnext/controllers/buying_controller.py b/erpnext/controllers/buying_controller.py
index 78ab2f1..fb7fb31 100644
--- a/erpnext/controllers/buying_controller.py
+++ b/erpnext/controllers/buying_controller.py
@@ -77,8 +77,7 @@
if self.meta.get_field("base_in_words"):
self.base_in_words = money_in_words(self.base_grand_total, company_currency)
if self.meta.get_field("in_words"):
- self.in_words = money_in_words(self.grand_total,
- self.currency)
+ self.in_words = money_in_words(self.grand_total, self.currency)
def calculate_taxes_and_totals(self):
super(BuyingController, self).calculate_taxes_and_totals()
@@ -174,11 +173,9 @@
stock_items_amount += flt(d.base_amount)
last_stock_item_idx = d.idx
- total_valuation_amount = sum([flt(d.tax_amount) for d in
- self.get("taxes")
+ total_valuation_amount = sum([flt(d.tax_amount) for d in self.get("taxes")
if d.category in ["Valuation", "Valuation and Total"]])
-
valuation_amount_adjustment = total_valuation_amount
for i, item in enumerate(self.get(parentfield)):
if item.item_code and item.qty and item.item_code in stock_items:
@@ -357,7 +354,6 @@
return self._purchase_items
-
def is_item_table_empty(self):
if not len(self.get("items")):
frappe.throw(_("Item table can not be blank"))
diff --git a/erpnext/public/js/controllers/taxes_and_totals.js b/erpnext/public/js/controllers/taxes_and_totals.js
index 113612a..b454c54 100644
--- a/erpnext/public/js/controllers/taxes_and_totals.js
+++ b/erpnext/public/js/controllers/taxes_and_totals.js
@@ -144,6 +144,18 @@
tax.rate;
},
+ calculate_net_total: function() {
+ var me = this;
+ this.frm.doc.base_net_total = this.frm.doc.net_total = 0.0;
+
+ $.each(this.frm.doc["items"] || [], function(i, item) {
+ me.frm.doc.base_net_total += item.base_amount;
+ me.frm.doc.net_total += item.amount;
+ });
+
+ frappe.model.round_floats_in(this.frm.doc, ["base_net_total", "net_total"]);
+ },
+
calculate_taxes: function() {
var me = this;
var actual_tax_dict = {};
@@ -295,6 +307,65 @@
}
},
+ calculate_totals: function() {
+ // Changing sequence can cause roundiing issue and on-screen discrepency
+
+ var tax_count = this.frm.doc["taxes"] ? this.frm.doc["taxes"].length : 0;
+ this.frm.doc.base_grand_total = flt(tax_count ? this.frm.doc["taxes"][tax_count - 1].total : this.frm.doc.base_net_total);
+
+ this.frm.doc.base_total_taxes_and_charges = flt(this.frm.doc.base_grand_total - this.frm.doc.base_net_total,
+ precision("base_total_taxes_and_charges"));
+
+ if(in_list(["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"])) {
+ this.frm.doc.grand_total = (this.frm.doc.base_total_taxes_and_charges || this.frm.doc.discount_amount) ?
+ flt(this.frm.doc.base_grand_total / this.frm.doc.conversion_rate) : this.frm.doc.net_total;
+
+ this.frm.doc.total_taxes_and_charges = flt(this.frm.doc.grand_total - this.frm.doc.net_total
+ + flt(this.frm.doc.discount_amount), precision("total_taxes_and_charges"));
+ } else {
+ // other charges added/deducted
+ this.frm.doc.base_taxes_and_charges_added = 0.0
+ this.frm.doc.base_taxes_and_charges_deducted = 0.0
+ if(tax_count) {
+ this.frm.doc.base_taxes_and_charges_added = frappe.utils.sum($.map(this.frm.doc["taxes"],
+ function(tax) { return (tax.add_deduct_tax == "Add"
+ && in_list(["Valuation and Total", "Total"], tax.category)) ?
+ tax.tax_amount : 0.0; }));
+
+ this.frm.doc.base_taxes_and_charges_deducted = frappe.utils.sum($.map(this.frm.doc["taxes"],
+ function(tax) { return (tax.add_deduct_tax == "Deduct"
+ && in_list(["Valuation and Total", "Total"], tax.category)) ?
+ tax.tax_amount : 0.0; }));
+
+ frappe.model.round_floats_in(this.frm.doc,
+ ["base_taxes_and_charges_added", "base_taxes_and_charges_deducted"]);
+ }
+
+ this.frm.doc.grand_total = flt((this.frm.doc.base_taxes_and_charges_added || this.frm.doc.base_taxes_and_charges_deducted) ?
+ flt(this.frm.doc.base_grand_total / this.frm.doc.conversion_rate) : this.frm.doc.net_total);
+
+ this.frm.doc.total_taxes_and_charges = flt(this.frm.doc.grand_total - this.frm.doc.net_total,
+ precision("total_taxes_and_charges"));
+
+ this.frm.doc.taxes_and_charges_added = flt(this.frm.doc.base_taxes_and_charges_added /
+ this.frm.doc.conversion_rate, precision("taxes_and_charges_added"));
+ this.frm.doc.taxes_and_charges_deducted = flt(this.frm.doc.base_taxes_and_charges_deducted /
+ this.frm.doc.conversion_rate, precision("taxes_and_charges_deducted"));
+ }
+
+ // Round grand total as per precision
+ this.frm.doc.base_grand_total = flt(this.frm.doc.base_grand_total, precision("base_grand_total"));
+ this.frm.doc.grand_total = flt(this.frm.doc.grand_total, precision("grand_total"));
+
+ // rounded totals
+ if(frappe.meta.get_docfield(this.frm.doc.doctype, "base_rounded_total", this.frm.doc.name)) {
+ this.frm.doc.base_rounded_total = Math.round(this.frm.doc.base_grand_total);
+ }
+ if(frappe.meta.get_docfield(this.frm.doc.doctype, "rounded_total", this.frm.doc.name)) {
+ this.frm.doc.rounded_total = Math.round(this.frm.doc.grand_total);
+ }
+ },
+
apply_discount_amount: function() {
var me = this;
var distributed_amount = 0.0;
diff --git a/erpnext/selling/sales_common.js b/erpnext/selling/sales_common.js
index e4be0b8..3a01ca0 100644
--- a/erpnext/selling/sales_common.js
+++ b/erpnext/selling/sales_common.js
@@ -233,41 +233,6 @@
}
},
- calculate_net_total: function() {
- var me = this;
- this.frm.doc.base_net_total = this.frm.doc.net_total = 0.0;
-
- $.each(this.frm.doc["items"] || [], function(i, item) {
- me.frm.doc.base_net_total += item.base_amount;
- me.frm.doc.net_total += item.amount;
- });
-
- frappe.model.round_floats_in(this.frm.doc, ["base_net_total", "net_total"]);
- },
-
- calculate_totals: function() {
- var me = this;
- var tax_count = this.frm.doc["taxes"] ? this.frm.doc["taxes"].length: 0;
-
- this.frm.doc.base_grand_total = flt(tax_count ? this.frm.doc["taxes"][tax_count - 1].total : this.frm.doc.base_net_total);
-
- this.frm.doc.base_total_taxes_and_charges = flt(this.frm.doc.base_grand_total - this.frm.doc.base_net_total,
- precision("base_total_taxes_and_charges"));
-
- this.frm.doc.grand_total = (this.frm.doc.base_total_taxes_and_charges || this.frm.doc.discount_amount) ?
- flt(this.frm.doc.base_grand_total / this.frm.doc.conversion_rate) : this.frm.doc.net_total;
-
- this.frm.doc.total_taxes_and_charges = flt(this.frm.doc.grand_total -
- this.frm.doc.net_total + flt(this.frm.doc.discount_amount),
- precision("total_taxes_and_charges"));
-
- this.frm.doc.base_grand_total = flt(this.frm.doc.base_grand_total, precision("base_grand_total"));
- this.frm.doc.grand_total = flt(this.frm.doc.grand_total, precision("grand_total"));
-
- this.frm.doc.base_rounded_total = Math.round(this.frm.doc.base_grand_total);
- this.frm.doc.rounded_total = Math.round(this.frm.doc.grand_total);
- },
-
calculate_outstanding_amount: function(update_paid_amount) {
// NOTE:
// paid_amount and write_off_amount is only for POS Invoice