[calculations] [client/server] first cut

This commit is contained in:
Anand Doshi
2013-05-24 19:25:01 +05:30
parent 2168e39bf3
commit 3543f30046
21 changed files with 958 additions and 1275 deletions

View File

@@ -53,14 +53,6 @@ cur_frm.cscript.onload = function(doc, cdt, cdn) {
}
}
cur_frm.cscript.onload_post_render = function(doc, dt, dn) {
var callback = function(doc, dt, dn) {
// defined in sales_common.js
cur_frm.cscript.update_item_details(doc, dt, dn);
}
cur_frm.cscript.hide_price_list_currency(doc, dt, dn, callback);
}
// hide - unhide fields based on lead or customer..
// =======================================================================================================================
cur_frm.cscript.lead_cust_show = function(doc,cdt,cdn){

View File

@@ -22,61 +22,19 @@
// cur_frm.cscript.sales_team_fname - Sales Team fieldname
wn.provide("erpnext.selling");
wn.require("app/js/transaction.js");
erpnext.selling.SellingController = wn.ui.form.Controller.extend({
erpnext.selling.SellingController = erpnext.TransactionController.extend({
setup: function() {
this.frm.add_fetch("sales_partner", "commission_rate", "commission_rate");
},
// events when rendering form
// 1
onload: function() {
var me = this;
this._super();
this.toggle_rounded_total();
if(this.frm.doc.__islocal) {
// set date fields
$.each(["posting_date", "due_date", "transaction_date"], function(i, fieldname) {
if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname]) {
me.frm.set_value(fieldname, get_today());
}
});
// set currency fields
$.each(["currency", "price_list_currency"], function(i, fieldname) {
if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname]) {
me.frm.set_value(fieldname, wn.defaults.get_default("currency"));
}
});
// status
if(!this.frm.doc.status) this.frm.set_value("status", "Draft");
// TODO set depends_on for customer related fields
}
},
// 2
refresh: function() {
erpnext.hide_naming_series();
this.show_item_wise_taxes();
this.set_dynamic_labels();
},
// 3
onload_post_render: function() {
if(this.frm.doc.__islocal && this.frm.doc.company) {
var me = this;
this.frm.call({
doc: this.frm.doc,
method: "onload_post_render",
freeze: true,
callback: function(r) {
// remove this call when using client side mapper
me.set_default_values();
me.frm.refresh();
}
});
}
// TODO set depends_on for customer related fields
},
validate: function() {
@@ -85,6 +43,32 @@ erpnext.selling.SellingController = wn.ui.form.Controller.extend({
// TODO calc adjustment amount
},
customer: function() {
if(this.frm.doc.customer || this.frm.doc.debit_to) {
if(!this.frm.doc.company) {
this.frm.set_value("customer", null);
msgprint(wn._("Please specify Company"));
} else {
var me = this;
var price_list_name = this.frm.doc.price_list_name;
this.frm.call({
doc: this.frm.doc,
method: "set_customer_defaults",
freeze: true,
callback: function(r) {
if(!r.exc) {
me.frm.refresh_fields();
if(me.frm.doc.price_list_name !== price_list_name) me.price_list_name();
}
}
});
}
}
// TODO hide/unhide related fields
},
barcode: function(doc, cdt, cdn) {
this.item_code(doc, cdt, cdn);
},
@@ -93,7 +77,7 @@ erpnext.selling.SellingController = wn.ui.form.Controller.extend({
var me = this;
var item = wn.model.get_doc(cdt, cdn);
if(item.item_code || item.barcode) {
if(!this.validate_company_and_party()) {
if(!this.validate_company_and_party("customer")) {
item.item_code = null;
refresh_field("item_code", item.name, item.parentfield);
} else {
@@ -128,90 +112,8 @@ erpnext.selling.SellingController = wn.ui.form.Controller.extend({
}
},
company: function() {
if(this.frm.doc.company) {
var me = this;
var company_currency = this.get_company_currency();
$.each(["currency", "price_list_currency"], function(i, fieldname) {
if(!me.doc[fieldname]) {
me.frm.set_value(fieldname, company_currency);
}
});
this.price_list_currency();
}
},
customer: function() {
if(this.frm.doc.customer || this.frm.doc.debit_to) {
if(!this.frm.doc.company) {
this.frm.set_value("customer", null);
msgprint(wn._("Please specify Company"));
} else {
var me = this;
var price_list_name = this.frm.doc.price_list_name;
this.frm.call({
doc: this.frm.doc,
method: "set_customer_defaults",
freeze: true,
callback: function(r) {
if(!r.exc) {
me.frm.refresh_fields();
if(me.frm.doc.price_list_name !== price_list_name) me.price_list_name();
}
}
});
}
}
// TODO hide/unhide related fields
},
price_list_name: function() {
var me = this;
if(this.frm.doc.price_list_name) {
this.frm.call({
method: "setup.utils.get_price_list_currency",
args: {args: {
price_list_name: this.frm.doc.price_list_name,
use_for: "selling"
}},
callback: function(r) {
if(!r.exc) {
me.price_list_currency();
}
}
});
}
},
currency: function() {
this.price_list_currency();
},
price_list_currency: function() {
// What TODO? should we make price list system non-mandatory?
// this.frm.toggle_reqd("plc_conversion_rate",
// !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
if(this.frm.doc.price_list_currency === this.get_company_currency()) {
this.frm.set_value("plc_conversion_rate", 1.0);
this.calculate_taxes_and_totals();
} else if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
this.calculate_taxes_and_totals();
}
this.set_dynamic_labels();
},
conversion_rate: function() {
this.price_list_currency();
this.calculate_taxes_and_totals();
},
plc_conversion_rate: function() {
this.price_list_currency();
this._super("selling");
},
ref_rate: function(doc, cdt, cdn) {
@@ -224,10 +126,6 @@ erpnext.selling.SellingController = wn.ui.form.Controller.extend({
this.calculate_taxes_and_totals();
},
qty: function(doc, cdt, cdn) {
this.calculate_taxes_and_totals();
},
adj_rate: function(doc, cdt, cdn) {
this.ref_rate(doc, cdt, cdn);
},
@@ -246,19 +144,6 @@ erpnext.selling.SellingController = wn.ui.form.Controller.extend({
this.calculate_taxes_and_totals();
},
included_in_print_rate: function(doc, cdt, cdn) {
var tax = wn.model.get_doc(cdt, cdn);
try {
this.validate_on_previous_row(tax);
this.validate_inclusive_tax(tax);
this.calculate_taxes_and_totals();
} catch(e) {
tax.included_in_print_rate = 0;
refresh_field("included_in_print_rate", tax.name, tax.parentfield);
throw e;
}
},
commission_rate: function() {
this.calculate_commission();
refresh_field("total_commission");
@@ -307,49 +192,10 @@ erpnext.selling.SellingController = wn.ui.form.Controller.extend({
}
},
validate_company_and_party: function() {
var me = this;
var valid = true;
$.each(["company", "customer"], function(i, fieldname) {
if(!me.frm.doc[fieldname]) {
valid = false;
msgprint(wn._("Please specify") + ": " +
wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
". " + wn._("It is needed to fetch Item Details."));
}
});
return valid;
},
set_default_values: function() {
$.each(wn.model.get_doclist(this.frm.doctype, this.frm.docname), function(i, doc) {
var updated = wn.model.set_default_values(doc);
if(doc.parentfield) {
refresh_field(doc.parentfield);
} else {
refresh_field(updated);
}
});
},
calculate_taxes_and_totals: function() {
this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
// TODO validate conversion rate
this.frm.item_doclist = this.get_item_doclist();
this.frm.tax_doclist = this.get_tax_doclist();
this.calculate_item_values();
this.initialize_taxes();
this.determine_exclusive_rate();
this.calculate_net_total();
this.calculate_taxes();
this.calculate_totals();
this._super();
this.calculate_commission();
this.calculate_contribution();
this._cleanup();
this.frm.doc.in_words = this.frm.doc.in_words_export = "";
// TODO
// outstanding amount
@@ -357,57 +203,21 @@ erpnext.selling.SellingController = wn.ui.form.Controller.extend({
// check for custom_recalc in custom scripts of server
this.frm.refresh_fields();
this.show_item_wise_taxes();
},
get_item_doclist: function() {
return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
{parentfield: this.fname});
},
get_tax_doclist: function() {
return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
{parentfield: "other_charges"});
},
calculate_item_values: function() {
var me = this;
var _set_base = function(item, print_field, base_field) {
// set values in base currency
item[base_field] = flt(item[print_field] * me.frm.doc.conversion_rate,
precision(base_field, item));
};
$.each(this.frm.item_doclist, function(i, item) {
wn.model.round_floats_in(item);
item.export_amount = flt(item.export_rate * item.qty, precision("export_amount", item));
_set_base(item, "ref_rate", "base_ref_rate");
_set_base(item, "export_rate", "basic_rate");
_set_base(item, "export_amount", "amount");
me._set_in_company_currency(item, "ref_rate", "base_ref_rate");
me._set_in_company_currency(item, "export_rate", "basic_rate");
me._set_in_company_currency(item, "export_amount", "amount");
});
},
initialize_taxes: function() {
var me = this;
$.each(this.frm.tax_doclist, function(i, tax) {
tax.tax_amount = tax.total = 0.0;
tax.item_wise_tax_detail = {};
// temporary fields
tax.tax_amount_for_current_item = tax.grand_total_for_current_item = 0.0;
tax.tax_fraction_for_current_item = tax.grand_total_fraction_for_current_item = 0.0;
me.validate_on_previous_row(tax);
me.validate_inclusive_tax(tax);
wn.model.round_floats_in(tax);
});
},
determine_exclusive_rate: function() {
var me = this;
$.each(me.frm.item_doclist, function(n, item) {
@@ -482,81 +292,6 @@ erpnext.selling.SellingController = wn.ui.form.Controller.extend({
wn.model.round_floats_in(this.frm.doc, ["net_total", "net_total_export"]);
},
calculate_taxes: function() {
var me = this;
$.each(this.frm.item_doclist, function(n, item) {
var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
$.each(me.frm.tax_doclist, function(i, tax) {
// tax_amount represents the amount of tax for the current step
var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
// case when net total is 0 but there is an actual type charge
// in this case add the actual amount to tax.tax_amount
// and tax.grand_total_for_current_item for the first such iteration
if(tax.charge_type == "Actual" &&
!(current_tax_amount || me.frm.doc.net_total || tax.tax_amount)) {
var zero_net_total_adjustment = flt(tax.rate, precision("tax_amount", tax));
current_tax_amount += zero_net_total_adjustment;
}
// store tax_amount for current item as it will be used for
// charge type = 'On Previous Row Amount'
tax.tax_amount_for_current_item = current_tax_amount;
// accumulate tax amount into tax.tax_amount
tax.tax_amount += current_tax_amount;
// Calculate tax.total viz. grand total till that step
// note: grand_total_for_current_item contains the contribution of
// item's amount, previously applied tax and the current tax on that item
if(i==0) {
tax.grand_total_for_current_item = flt(item.amount + current_tax_amount,
precision("total", tax));
} else {
tax.grand_total_for_current_item =
flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
precision("total", tax));
}
// in tax.total, accumulate grand total for each item
tax.total += tax.grand_total_for_current_item;
// store tax breakup for each item
tax.item_wise_tax_detail[item.item_code || item.item_name] = current_tax_amount;
});
});
},
get_current_tax_amount: function(item, tax, item_tax_map) {
var tax_rate = this._get_tax_rate(tax, item_tax_map);
var current_tax_amount = 0.0;
if(tax.charge_type == "Actual") {
// distribute the tax amount proportionally to each item row
var actual = flt(tax.rate, precision("tax_amount", tax));
current_tax_amount = this.frm.doc.net_total ?
((item.amount / this.frm.doc.net_total) * actual) :
0.0;
} else if(tax.charge_type == "On Net Total") {
current_tax_amount = (tax_rate / 100.0) * item.amount;
} else if(tax.charge_type == "On Previous Row Amount") {
current_tax_amount = (tax_rate / 100.0) *
this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
} else if(tax.charge_type == "On Previous Row Total") {
current_tax_amount = (tax_rate / 100.0) *
this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
}
return flt(current_tax_amount, precision("tax_amount", tax));
},
calculate_totals: function() {
var tax_count = this.frm.tax_doclist.length;
this.frm.doc.grand_total = flt(
@@ -601,91 +336,8 @@ erpnext.selling.SellingController = wn.ui.form.Controller.extend({
},
_cleanup: function() {
$.each(this.frm.tax_doclist, function(i, tax) {
var tax_fields = keys(tax);
$.each(["tax_amount_for_current_item", "grand_total_for_current_item",
"tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
function(i, fieldname) { delete tax[fieldname];});
tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
});
},
validate_on_previous_row: function(tax) {
// validate if a valid row id is mentioned in case of
// On Previous Row Amount and On Previous Row Total
if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
(!tax.row_id || cint(tax.row_id) >= tax.idx)) {
var msg = repl(wn._("Row") + " # %(idx)s [%(doctype)s]: " +
wn._("Please specify a valid") + " %(row_id_label)s", {
idx: tax.idx,
doctype: tax.doctype,
row_id_label: wn.meta.get_label(tax.doctype, "row_id", tax.name)
});
msgprint(msg);
throw msg;
}
},
validate_inclusive_tax: function(tax) {
if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
var actual_type_error = function() {
var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
"%(charge_type_label)s = \"%(charge_type)s\" " +
wn._("cannot be included in Item's rate"), {
idx: tax.idx,
doctype: tax.doctype,
charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
charge_type: tax.charge_type
});
msgprint(msg);
throw msg;
};
var on_previous_row_error = function(row_range) {
var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
wn._("to be included in Item's rate, it is required that: ") +
" [" + wn._("Row") + " # %(row_range)s] " + wn._("also be included in Item's rate"), {
idx: tax.idx,
doctype: tax.doctype,
charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
charge_type: tax.charge_type,
inclusive_label: wn.meta.get_label(tax.doctype, "included_in_print_rate", tax.name),
row_range: row_range,
});
msgprint(msg);
throw msg;
};
if(cint(tax.included_in_print_rate)) {
if(tax.charge_type == "Actual") {
// inclusive tax cannot be of type Actual
actual_type_error();
} else if(tax.charge_type == "On Previous Row Amount" &&
!cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
// referred row should also be an inclusive tax
on_previous_row_error(tax.row_id);
} else if(tax.charge_type == "On Previous Row Total") {
var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
function(t) { return cint(t.included_in_print_rate) ? null : t; });
if(taxes_not_included.length > 0) {
// all rows above this tax should be inclusive
on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
}
}
}
},
_load_item_tax_rate: function(item_tax_rate) {
return item_tax_rate ? JSON.parse(item_tax_rate) : {};
},
_get_tax_rate: function(tax, item_tax_map) {
return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
flt(item_tax_map[tax.account_head], precision("rate", tax)) :
tax.rate;
this._super();
this.frm.doc.in_words = this.frm.doc.in_words_export = "";
},
show_item_wise_taxes: function() {
@@ -693,40 +345,6 @@ erpnext.selling.SellingController = wn.ui.form.Controller.extend({
.appendTo($(this.frm.fields_dict.other_charges_calculation.wrapper).empty());
},
get_item_wise_taxes_html: function() {
var item_tax = {};
var tax_accounts = [];
var company_currency = this.get_company_currency();
$.each(this.get_tax_doclist(), function(i, tax) {
var tax_amount_precision = precision("tax_amount", tax);
$.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
function(item_code, tax_amount) {
if(!item_tax[item_code]) item_tax[item_code] = {};
item_tax[item_code][tax.account_head] = flt(tax_amount, tax_amount_precision);
});
tax_accounts.push(tax.account_head);
});
var headings = $.map([wn._("Item Name")].concat(tax_accounts),
function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
var rows = $.map(this.get_item_doclist(), function(item) {
var item_tax_record = item_tax[item.item_code || item.item_name];
return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
item_name: item.item_name,
taxes: $.map(tax_accounts, function(head) {
return "<td>" + format_currency(item_tax_record[head], company_currency) + "</td>"
}).join("\n")
});
}).join("\n");
return '<div style="overflow-x: scroll;"><table class="table table-bordered table-hover">\
<thead><tr>' + headings + '</tr></thead> \
<tbody>' + rows + '</tbody> \
</table></div>';
},
get_charges: function() {
var me = this;
if(this.frm.doc.charge) {
@@ -837,9 +455,6 @@ erpnext.selling.SellingController = wn.ui.form.Controller.extend({
});
},
get_company_currency: function() {
return erpnext.get_currency(this.frm.doc.company);
}
});
// to save previous state of cur_frm.cscript

View File

@@ -28,7 +28,7 @@ wn.require('app/utilities/doctype/sms_control/sms_control.js');
cur_frm.cscript.onload = function(doc, cdt, cdn) {
cur_frm.cscript.manage_rounded_total();
cur_frm.cscript.toggle_rounded_total();
if(!doc.status) set_multiple(cdt,cdn,{status:'Draft'});
if(!doc.transaction_date) set_multiple(cdt,cdn,{transaction_date:get_today()});
@@ -42,25 +42,10 @@ cur_frm.cscript.onload = function(doc, cdt, cdn) {
}
}
cur_frm.cscript.onload_post_render = function(doc, cdt, cdn) {
var callback = function(doc, cdt, cdn) {
if(doc.__islocal) {
// defined in sales_common.js
cur_frm.cscript.update_item_details(doc, cdt, cdn);
}
}
cur_frm.cscript.hide_price_list_currency(doc, cdt, cdn, callback);
}
cur_frm.cscript.refresh = function(doc, cdt, cdn) {
cur_frm.clear_custom_buttons();
erpnext.hide_naming_series();
if (!cur_frm.cscript.is_onload) cur_frm.cscript.hide_price_list_currency(doc, cdt, cdn);
cur_frm.toggle_display("contact_info", doc.customer);
if(doc.docstatus==1) {