commonified totals calculation in js

This commit is contained in:
Nabin Hait 2015-02-12 17:55:50 +05:30
parent 5690be103c
commit 188f69a713
5 changed files with 74 additions and 113 deletions

View File

@ -172,64 +172,6 @@ erpnext.buying.BuyingController = erpnext.TransactionController.extend({
} }
}, },
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() { calculate_outstanding_amount: function() {
if(this.frm.doc.doctype == "Purchase Invoice" && this.frm.doc.docstatus < 2) { 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"]); frappe.model.round_floats_in(this.frm.doc, ["base_grand_total", "total_advance", "write_off_amount"]);
@ -240,18 +182,6 @@ erpnext.buying.BuyingController = erpnext.TransactionController.extend({
} }
}, },
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) { change_form_labels: function(company_currency) {
var me = this; var me = this;
var field_label_map = {}; var field_label_map = {};

View File

@ -343,8 +343,7 @@ class AccountsController(TransactionBase):
def _set_in_company_currency(self, item, print_field, base_field): def _set_in_company_currency(self, item, print_field, base_field):
"""set values in base currency""" """set values in base currency"""
value_in_company_currency = flt(self.conversion_rate * value_in_company_currency = flt(self.conversion_rate *
flt(item.get(print_field), self.precision(print_field, item)), flt(item.get(print_field), self.precision(print_field, item)), self.precision(base_field, item))
self.precision(base_field, item))
item.set(base_field, value_in_company_currency) item.set(base_field, value_in_company_currency)
def validate_enabled_taxes_and_charges(self): def validate_enabled_taxes_and_charges(self):

View File

@ -77,8 +77,7 @@ class BuyingController(StockController):
if self.meta.get_field("base_in_words"): if self.meta.get_field("base_in_words"):
self.base_in_words = money_in_words(self.base_grand_total, company_currency) self.base_in_words = money_in_words(self.base_grand_total, company_currency)
if self.meta.get_field("in_words"): if self.meta.get_field("in_words"):
self.in_words = money_in_words(self.grand_total, self.in_words = money_in_words(self.grand_total, self.currency)
self.currency)
def calculate_taxes_and_totals(self): def calculate_taxes_and_totals(self):
super(BuyingController, self).calculate_taxes_and_totals() super(BuyingController, self).calculate_taxes_and_totals()
@ -174,11 +173,9 @@ class BuyingController(StockController):
stock_items_amount += flt(d.base_amount) stock_items_amount += flt(d.base_amount)
last_stock_item_idx = d.idx last_stock_item_idx = d.idx
total_valuation_amount = sum([flt(d.tax_amount) for d in total_valuation_amount = sum([flt(d.tax_amount) for d in self.get("taxes")
self.get("taxes")
if d.category in ["Valuation", "Valuation and Total"]]) if d.category in ["Valuation", "Valuation and Total"]])
valuation_amount_adjustment = total_valuation_amount valuation_amount_adjustment = total_valuation_amount
for i, item in enumerate(self.get(parentfield)): for i, item in enumerate(self.get(parentfield)):
if item.item_code and item.qty and item.item_code in stock_items: if item.item_code and item.qty and item.item_code in stock_items:
@ -357,7 +354,6 @@ class BuyingController(StockController):
return self._purchase_items return self._purchase_items
def is_item_table_empty(self): def is_item_table_empty(self):
if not len(self.get("items")): if not len(self.get("items")):
frappe.throw(_("Item table can not be blank")) frappe.throw(_("Item table can not be blank"))

View File

@ -144,6 +144,18 @@ erpnext.taxes_and_totals = erpnext.stock.StockController.extend({
tax.rate; 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() { calculate_taxes: function() {
var me = this; var me = this;
var actual_tax_dict = {}; var actual_tax_dict = {};
@ -295,6 +307,65 @@ erpnext.taxes_and_totals = erpnext.stock.StockController.extend({
} }
}, },
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() { apply_discount_amount: function() {
var me = this; var me = this;
var distributed_amount = 0.0; var distributed_amount = 0.0;

View File

@ -233,41 +233,6 @@ erpnext.selling.SellingController = erpnext.TransactionController.extend({
} }
}, },
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) { calculate_outstanding_amount: function(update_paid_amount) {
// NOTE: // NOTE:
// paid_amount and write_off_amount is only for POS Invoice // paid_amount and write_off_amount is only for POS Invoice