2013-11-20 07:29:58 +00:00
|
|
|
// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
|
2013-08-05 09:29:54 +00:00
|
|
|
// License: GNU General Public License v3. See license.txt
|
2013-05-24 13:55:01 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.provide("erpnext");
|
|
|
|
frappe.require("assets/erpnext/js/controllers/stock_controller.js");
|
2013-05-24 13:55:01 +00:00
|
|
|
|
2013-07-30 09:12:15 +00:00
|
|
|
erpnext.TransactionController = erpnext.stock.StockController.extend({
|
2013-05-24 13:55:01 +00:00
|
|
|
onload: function() {
|
2013-08-23 07:26:33 +00:00
|
|
|
var me = this;
|
2014-06-24 13:23:04 +00:00
|
|
|
this._super();
|
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
if(this.frm.doc.__islocal) {
|
2013-08-23 07:26:33 +00:00
|
|
|
var today = get_today(),
|
2014-03-14 09:40:42 +00:00
|
|
|
currency = frappe.defaults.get_user_default("currency");
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
$.each({
|
2013-05-27 13:59:07 +00:00
|
|
|
posting_date: today,
|
|
|
|
due_date: today,
|
|
|
|
transaction_date: today,
|
|
|
|
currency: currency,
|
|
|
|
price_list_currency: currency,
|
|
|
|
status: "Draft",
|
|
|
|
is_subcontracted: "No",
|
2013-05-24 13:55:01 +00:00
|
|
|
}, function(fieldname, value) {
|
|
|
|
if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname])
|
|
|
|
me.frm.set_value(fieldname, value);
|
2014-04-15 13:10:00 +00:00
|
|
|
});
|
2014-07-07 11:21:52 +00:00
|
|
|
|
2014-07-28 09:25:08 +00:00
|
|
|
if(this.frm.doc.company && !this.frm.doc.amended_from) {
|
2014-07-07 11:21:52 +00:00
|
|
|
cur_frm.script_manager.trigger("company");
|
|
|
|
}
|
2013-05-24 13:55:01 +00:00
|
|
|
}
|
2013-10-17 06:01:11 +00:00
|
|
|
|
2013-08-23 07:26:33 +00:00
|
|
|
if(this.other_fname) {
|
2013-08-23 07:30:47 +00:00
|
|
|
this[this.other_fname + "_remove"] = this.calculate_taxes_and_totals;
|
|
|
|
}
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-08-23 07:30:47 +00:00
|
|
|
if(this.fname) {
|
|
|
|
this[this.fname + "_remove"] = this.calculate_taxes_and_totals;
|
2013-08-23 07:26:33 +00:00
|
|
|
}
|
2013-05-24 13:55:01 +00:00
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-08-06 11:53:44 +00:00
|
|
|
onload_post_render: function() {
|
2013-10-15 14:54:34 +00:00
|
|
|
var me = this;
|
2014-06-03 08:42:49 +00:00
|
|
|
if(this.frm.doc.__islocal && this.frm.doc.company && this.frm.doc[this.fname] && !this.frm.doc.is_pos) {
|
2014-04-22 15:05:22 +00:00
|
|
|
this.calculate_taxes_and_totals();
|
2013-08-06 11:53:44 +00:00
|
|
|
}
|
2014-06-04 07:40:41 +00:00
|
|
|
if(frappe.meta.get_docfield(this.tname, "item_code")) {
|
2014-06-06 06:15:11 +00:00
|
|
|
cur_frm.get_field(this.fname).grid.set_multiple_add("item_code", "qty");
|
2014-06-04 07:40:41 +00:00
|
|
|
}
|
2013-08-06 11:53:44 +00:00
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
refresh: function() {
|
2014-04-24 12:41:49 +00:00
|
|
|
erpnext.toggle_naming_series();
|
2013-07-09 07:33:39 +00:00
|
|
|
erpnext.hide_company();
|
2013-05-24 13:55:01 +00:00
|
|
|
this.show_item_wise_taxes();
|
2013-07-29 13:30:53 +00:00
|
|
|
this.set_dynamic_labels();
|
2013-09-17 06:26:17 +00:00
|
|
|
|
|
|
|
// Show POS button only if it is enabled from features setup
|
2014-08-07 09:54:26 +00:00
|
|
|
if(cint(sys_defaults.fs_pos_view)===1 && this.frm.doctype!="Material Request" && this.frm.doc.docstatus===0)
|
2013-11-15 06:00:23 +00:00
|
|
|
this.make_pos_btn();
|
2013-09-17 06:26:17 +00:00
|
|
|
},
|
|
|
|
|
2013-11-15 06:00:23 +00:00
|
|
|
make_pos_btn: function() {
|
2013-09-17 06:26:17 +00:00
|
|
|
if(!this.pos_active) {
|
2014-04-14 10:55:30 +00:00
|
|
|
var btn_label = __("POS View"),
|
2014-08-07 09:54:26 +00:00
|
|
|
icon = "icon-th";
|
2013-09-17 06:26:17 +00:00
|
|
|
} else {
|
2014-08-07 09:54:26 +00:00
|
|
|
var btn_label = __("Form View"),
|
2013-09-17 06:26:17 +00:00
|
|
|
icon = "icon-file-text";
|
|
|
|
}
|
|
|
|
var me = this;
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2014-08-07 09:54:26 +00:00
|
|
|
this.$pos_btn = this.frm.appframe.add_primary_action(btn_label, function() {
|
2013-09-17 06:26:17 +00:00
|
|
|
me.toggle_pos();
|
2014-08-07 09:54:26 +00:00
|
|
|
}, icon, "btn-default");
|
2014-08-08 10:00:49 +00:00
|
|
|
|
|
|
|
if(erpnext.open_as_pos) {
|
|
|
|
me.toggle_pos(true);
|
|
|
|
erpnext.open_as_pos = false;
|
|
|
|
}
|
2013-09-17 06:26:17 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
toggle_pos: function(show) {
|
2013-09-18 13:05:12 +00:00
|
|
|
// Check whether it is Selling or Buying cycle
|
2014-02-14 10:17:51 +00:00
|
|
|
var price_list = frappe.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
|
2013-09-18 13:05:12 +00:00
|
|
|
this.frm.doc.selling_price_list : this.frm.doc.buying_price_list;
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-09-18 13:05:12 +00:00
|
|
|
if (!price_list)
|
2014-04-14 10:55:30 +00:00
|
|
|
msgprint(__("Please select Price List"))
|
2013-09-17 06:26:17 +00:00
|
|
|
else {
|
|
|
|
if((show===true && this.pos_active) || (show===false && !this.pos_active)) return;
|
|
|
|
|
|
|
|
// make pos
|
2013-09-23 06:57:16 +00:00
|
|
|
if(!this.frm.pos) {
|
2013-09-17 06:26:17 +00:00
|
|
|
this.frm.layout.add_view("pos");
|
|
|
|
this.frm.pos = new erpnext.POS(this.frm.layout.views.pos, this.frm);
|
|
|
|
}
|
|
|
|
|
|
|
|
// toggle view
|
|
|
|
this.frm.layout.set_view(this.pos_active ? "" : "pos");
|
|
|
|
this.pos_active = !this.pos_active;
|
|
|
|
|
|
|
|
// refresh
|
|
|
|
if(this.pos_active)
|
|
|
|
this.frm.pos.refresh();
|
2013-09-18 13:05:12 +00:00
|
|
|
this.frm.refresh();
|
2013-09-17 06:26:17 +00:00
|
|
|
}
|
2013-05-24 13:55:01 +00:00
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
|
|
|
|
2014-02-12 09:23:18 +00:00
|
|
|
item_code: function(doc, cdt, cdn) {
|
|
|
|
var me = this;
|
2014-03-27 08:47:33 +00:00
|
|
|
var item = frappe.get_doc(cdt, cdn);
|
2014-02-12 09:23:18 +00:00
|
|
|
if(item.item_code || item.barcode || item.serial_no) {
|
|
|
|
if(!this.validate_company_and_party()) {
|
|
|
|
cur_frm.fields_dict[me.frm.cscript.fname].grid.grid_rows[item.idx - 1].remove();
|
|
|
|
} else {
|
|
|
|
return this.frm.call({
|
|
|
|
method: "erpnext.stock.get_item_details.get_item_details",
|
|
|
|
child: item,
|
|
|
|
args: {
|
|
|
|
args: {
|
|
|
|
item_code: item.item_code,
|
|
|
|
barcode: item.barcode,
|
|
|
|
serial_no: item.serial_no,
|
|
|
|
warehouse: item.warehouse,
|
2014-06-20 10:29:49 +00:00
|
|
|
parenttype: me.frm.doc.doctype,
|
|
|
|
parent: me.frm.doc.name,
|
2014-02-12 09:23:18 +00:00
|
|
|
customer: me.frm.doc.customer,
|
|
|
|
supplier: me.frm.doc.supplier,
|
|
|
|
currency: me.frm.doc.currency,
|
|
|
|
conversion_rate: me.frm.doc.conversion_rate,
|
|
|
|
price_list: me.frm.doc.selling_price_list ||
|
|
|
|
me.frm.doc.buying_price_list,
|
|
|
|
price_list_currency: me.frm.doc.price_list_currency,
|
|
|
|
plc_conversion_rate: me.frm.doc.plc_conversion_rate,
|
|
|
|
company: me.frm.doc.company,
|
|
|
|
order_type: me.frm.doc.order_type,
|
|
|
|
is_pos: cint(me.frm.doc.is_pos),
|
|
|
|
is_subcontracted: me.frm.doc.is_subcontracted,
|
2014-06-20 10:29:49 +00:00
|
|
|
transaction_date: me.frm.doc.transaction_date,
|
|
|
|
ignore_pricing_rule: me.frm.doc.ignore_pricing_rule,
|
|
|
|
doctype: item.doctype,
|
|
|
|
name: item.name
|
2014-02-12 09:23:18 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
callback: function(r) {
|
|
|
|
if(!r.exc) {
|
|
|
|
me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2014-04-15 13:10:00 +00:00
|
|
|
},
|
2013-10-18 08:54:21 +00:00
|
|
|
|
|
|
|
serial_no: function(doc, cdt, cdn) {
|
|
|
|
var me = this;
|
2014-03-27 08:47:33 +00:00
|
|
|
var item = frappe.get_doc(cdt, cdn);
|
2013-10-18 08:54:21 +00:00
|
|
|
|
|
|
|
if (item.serial_no) {
|
|
|
|
if (!item.item_code) {
|
|
|
|
this.frm.script_manager.trigger("item_code", cdt, cdn);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var sr_no = [];
|
|
|
|
|
|
|
|
// Replacing all occurences of comma with carriage return
|
|
|
|
var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
|
|
|
|
|
|
|
|
serial_nos = serial_nos.trim().split('\n');
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-10-18 08:54:21 +00:00
|
|
|
// Trim each string and push unique string to new list
|
|
|
|
for (var x=0; x<=serial_nos.length - 1; x++) {
|
|
|
|
if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
|
|
|
|
sr_no.push(serial_nos[x].trim());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the new list to the serial no. field in grid with each in new line
|
|
|
|
item.serial_no = "";
|
|
|
|
for (var x=0; x<=sr_no.length - 1; x++)
|
|
|
|
item.serial_no += sr_no[x] + '\n';
|
|
|
|
|
|
|
|
refresh_field("serial_no", item.name, item.parentfield);
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
|
2013-10-18 08:54:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-28 11:53:36 +00:00
|
|
|
validate: function() {
|
2014-04-16 09:51:46 +00:00
|
|
|
this.calculate_taxes_and_totals(false);
|
2013-05-28 11:53:36 +00:00
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
company: function() {
|
2013-06-21 06:52:52 +00:00
|
|
|
if(this.frm.doc.company && this.frm.fields_dict.currency) {
|
2013-10-11 08:04:10 +00:00
|
|
|
var company_currency = this.get_company_currency();
|
2013-10-11 13:36:30 +00:00
|
|
|
if (!this.frm.doc.currency) {
|
2013-10-11 08:04:10 +00:00
|
|
|
this.frm.set_value("currency", company_currency);
|
2013-08-01 10:15:23 +00:00
|
|
|
}
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-10-11 13:36:30 +00:00
|
|
|
if (this.frm.doc.currency == company_currency) {
|
2013-10-11 08:04:10 +00:00
|
|
|
this.frm.set_value("conversion_rate", 1.0);
|
2013-10-11 13:36:30 +00:00
|
|
|
}
|
|
|
|
if (this.frm.doc.price_list_currency == company_currency) {
|
2013-10-11 08:04:10 +00:00
|
|
|
this.frm.set_value('plc_conversion_rate', 1.0);
|
2013-10-11 13:36:30 +00:00
|
|
|
}
|
2013-10-11 08:04:10 +00:00
|
|
|
|
2013-07-23 10:01:39 +00:00
|
|
|
this.frm.script_manager.trigger("currency");
|
2014-06-20 10:29:49 +00:00
|
|
|
this.apply_pricing_rule();
|
2013-05-24 13:55:01 +00:00
|
|
|
}
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
get_company_currency: function() {
|
|
|
|
return erpnext.get_currency(this.frm.doc.company);
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
currency: function() {
|
2013-06-21 12:25:31 +00:00
|
|
|
var me = this;
|
|
|
|
this.set_dynamic_labels();
|
2013-10-11 13:36:30 +00:00
|
|
|
|
2013-06-21 12:25:31 +00:00
|
|
|
var company_currency = this.get_company_currency();
|
|
|
|
if(this.frm.doc.currency !== company_currency) {
|
2014-04-15 13:10:00 +00:00
|
|
|
this.get_exchange_rate(this.frm.doc.currency, company_currency,
|
2013-06-21 12:25:31 +00:00
|
|
|
function(exchange_rate) {
|
2013-10-11 13:36:30 +00:00
|
|
|
me.frm.set_value("conversion_rate", exchange_rate);
|
|
|
|
me.conversion_rate();
|
2013-06-21 12:25:31 +00:00
|
|
|
});
|
|
|
|
} else {
|
2014-04-15 13:10:00 +00:00
|
|
|
this.conversion_rate();
|
2013-06-21 12:25:31 +00:00
|
|
|
}
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-06-21 12:25:31 +00:00
|
|
|
conversion_rate: function() {
|
2013-07-19 08:21:07 +00:00
|
|
|
if(this.frm.doc.currency === this.get_company_currency()) {
|
|
|
|
this.frm.set_value("conversion_rate", 1.0);
|
2013-10-17 07:34:26 +00:00
|
|
|
}
|
|
|
|
if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
|
2013-06-21 12:25:31 +00:00
|
|
|
this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
|
|
|
|
this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
|
|
|
|
}
|
2014-05-28 07:19:20 +00:00
|
|
|
if(flt(this.frm.doc.conversion_rate)>0.0) {
|
2014-06-20 10:29:49 +00:00
|
|
|
if(this.frm.doc.ignore_pricing_rule) {
|
|
|
|
this.calculate_taxes_and_totals();
|
2014-07-04 10:14:26 +00:00
|
|
|
} else if (!this.in_apply_price_list){
|
|
|
|
this.apply_price_list();
|
2014-06-20 10:29:49 +00:00
|
|
|
}
|
|
|
|
|
2014-05-28 07:19:20 +00:00
|
|
|
}
|
2013-05-24 13:55:01 +00:00
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-06-21 12:25:31 +00:00
|
|
|
get_exchange_rate: function(from_currency, to_currency, callback) {
|
|
|
|
var exchange_name = from_currency + "-" + to_currency;
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.model.with_doc("Currency Exchange", exchange_name, function(name) {
|
2014-03-27 08:47:33 +00:00
|
|
|
var exchange_doc = frappe.get_doc("Currency Exchange", exchange_name);
|
2013-06-25 05:26:39 +00:00
|
|
|
callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
|
2013-06-21 12:25:31 +00:00
|
|
|
});
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
price_list_currency: function() {
|
2013-09-11 07:35:24 +00:00
|
|
|
var me=this;
|
2013-06-21 12:25:31 +00:00
|
|
|
this.set_dynamic_labels();
|
2014-07-04 10:14:26 +00:00
|
|
|
this.set_plc_conversion_rate();
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2014-07-04 10:14:26 +00:00
|
|
|
plc_conversion_rate: function() {
|
|
|
|
this.set_plc_conversion_rate();
|
|
|
|
if(!this.in_apply_price_list) {
|
|
|
|
this.apply_price_list();
|
2013-05-24 13:55:01 +00:00
|
|
|
}
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2014-07-04 10:14:26 +00:00
|
|
|
set_plc_conversion_rate: function() {
|
2013-06-21 12:25:31 +00:00
|
|
|
if(this.frm.doc.price_list_currency === this.get_company_currency()) {
|
|
|
|
this.frm.set_value("plc_conversion_rate", 1.0);
|
2013-10-17 06:01:11 +00:00
|
|
|
}
|
|
|
|
if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
|
2013-06-21 12:25:31 +00:00
|
|
|
this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
|
|
|
|
}
|
2013-05-24 13:55:01 +00:00
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
qty: function(doc, cdt, cdn) {
|
2014-06-20 10:29:49 +00:00
|
|
|
this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
|
2013-05-24 13:55:01 +00:00
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2014-04-16 09:51:46 +00:00
|
|
|
// tax rate
|
|
|
|
rate: function(doc, cdt, cdn) {
|
2013-05-28 11:53:36 +00:00
|
|
|
this.calculate_taxes_and_totals();
|
|
|
|
},
|
2013-09-25 05:39:33 +00:00
|
|
|
|
2013-05-28 11:53:36 +00:00
|
|
|
row_id: function(doc, cdt, cdn) {
|
2014-03-27 08:47:33 +00:00
|
|
|
var tax = frappe.get_doc(cdt, cdn);
|
2013-05-28 11:53:36 +00:00
|
|
|
try {
|
|
|
|
this.validate_on_previous_row(tax);
|
|
|
|
this.calculate_taxes_and_totals();
|
|
|
|
} catch(e) {
|
|
|
|
tax.row_id = null;
|
|
|
|
refresh_field("row_id", tax.name, tax.parentfield);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-06-21 12:25:31 +00:00
|
|
|
set_dynamic_labels: 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));
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-06-21 12:25:31 +00:00
|
|
|
var company_currency = this.get_company_currency();
|
|
|
|
this.change_form_labels(company_currency);
|
|
|
|
this.change_grid_labels(company_currency);
|
2013-08-05 06:46:04 +00:00
|
|
|
this.frm.refresh_fields();
|
2013-06-21 12:25:31 +00:00
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-28 11:53:36 +00:00
|
|
|
recalculate: function() {
|
|
|
|
this.calculate_taxes_and_totals();
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-28 11:53:36 +00:00
|
|
|
recalculate_values: function() {
|
|
|
|
this.calculate_taxes_and_totals();
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-28 13:24:48 +00:00
|
|
|
calculate_charges: function() {
|
|
|
|
this.calculate_taxes_and_totals();
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2014-06-20 10:29:49 +00:00
|
|
|
ignore_pricing_rule: function() {
|
|
|
|
this.apply_pricing_rule();
|
|
|
|
},
|
2014-05-28 07:19:20 +00:00
|
|
|
|
2014-06-20 10:29:49 +00:00
|
|
|
apply_pricing_rule: function(item, calculate_taxes_and_totals) {
|
|
|
|
var me = this;
|
2014-07-01 12:15:15 +00:00
|
|
|
return this.frm.call({
|
|
|
|
method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
|
|
|
|
args: { args: this._get_args(item) },
|
|
|
|
callback: function(r) {
|
2014-07-16 12:22:08 +00:00
|
|
|
if (!r.exc && r.message) {
|
2014-07-01 12:15:15 +00:00
|
|
|
me._set_values_for_item_list(r.message);
|
|
|
|
if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_get_args: function(item) {
|
|
|
|
var me = this;
|
|
|
|
return {
|
|
|
|
"item_list": this._get_item_list(item),
|
2014-06-20 10:29:49 +00:00
|
|
|
"customer": me.frm.doc.customer,
|
|
|
|
"customer_group": me.frm.doc.customer_group,
|
|
|
|
"territory": me.frm.doc.territory,
|
|
|
|
"supplier": me.frm.doc.supplier,
|
|
|
|
"supplier_type": me.frm.doc.supplier_type,
|
|
|
|
"currency": me.frm.doc.currency,
|
|
|
|
"conversion_rate": me.frm.doc.conversion_rate,
|
|
|
|
"price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
|
|
|
|
"plc_conversion_rate": me.frm.doc.plc_conversion_rate,
|
|
|
|
"company": me.frm.doc.company,
|
|
|
|
"transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
|
|
|
|
"campaign": me.frm.doc.campaign,
|
|
|
|
"sales_partner": me.frm.doc.sales_partner,
|
|
|
|
"ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
|
|
|
|
"parenttype": me.frm.doc.doctype,
|
|
|
|
"parent": me.frm.doc.name
|
|
|
|
};
|
|
|
|
},
|
2014-05-28 07:19:20 +00:00
|
|
|
|
2014-06-20 10:29:49 +00:00
|
|
|
_get_item_list: function(item) {
|
|
|
|
var item_list = [];
|
|
|
|
var append_item = function(d) {
|
2014-07-28 10:30:19 +00:00
|
|
|
if (d.item_code) {
|
|
|
|
item_list.push({
|
|
|
|
"doctype": d.doctype,
|
|
|
|
"name": d.name,
|
|
|
|
"item_code": d.item_code,
|
|
|
|
"item_group": d.item_group,
|
|
|
|
"brand": d.brand,
|
|
|
|
"qty": d.qty
|
|
|
|
});
|
|
|
|
}
|
2014-06-20 10:29:49 +00:00
|
|
|
};
|
2014-05-28 07:19:20 +00:00
|
|
|
|
2014-06-20 10:29:49 +00:00
|
|
|
if (item) {
|
|
|
|
append_item(item);
|
2014-05-30 06:13:00 +00:00
|
|
|
} else {
|
2014-06-20 10:29:49 +00:00
|
|
|
$.each(this.get_item_doclist(), function(i, d) {
|
|
|
|
append_item(d);
|
2014-05-28 07:19:20 +00:00
|
|
|
});
|
|
|
|
}
|
2014-06-20 10:29:49 +00:00
|
|
|
return item_list;
|
2014-05-28 07:19:20 +00:00
|
|
|
},
|
|
|
|
|
2014-07-01 12:15:15 +00:00
|
|
|
_set_values_for_item_list: function(children) {
|
|
|
|
$.each(children, function(i, d) {
|
|
|
|
$.each(d, function(k, v) {
|
|
|
|
if (["doctype", "name"].indexOf(k)===-1) {
|
|
|
|
frappe.model.set_value(d.doctype, d.name, k, v);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
apply_price_list: function() {
|
|
|
|
var me = this;
|
|
|
|
return this.frm.call({
|
|
|
|
method: "erpnext.stock.get_item_details.apply_price_list",
|
|
|
|
args: { args: this._get_args() },
|
|
|
|
callback: function(r) {
|
|
|
|
if (!r.exc) {
|
2014-07-04 10:14:26 +00:00
|
|
|
me.in_apply_price_list = true;
|
2014-07-01 12:15:15 +00:00
|
|
|
me.frm.set_value("price_list_currency", r.message.parent.price_list_currency);
|
|
|
|
me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate);
|
2014-07-04 10:14:26 +00:00
|
|
|
me.in_apply_price_list = false;
|
2014-07-01 12:15:15 +00:00
|
|
|
me._set_values_for_item_list(r.message.children);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
included_in_print_rate: function(doc, cdt, cdn) {
|
2014-03-27 08:47:33 +00:00
|
|
|
var tax = frappe.get_doc(cdt, cdn);
|
2013-05-24 13:55:01 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
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
|
2014-04-15 13:10:00 +00:00
|
|
|
if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
|
2013-05-24 13:55:01 +00:00
|
|
|
(!tax.row_id || cint(tax.row_id) >= tax.idx)) {
|
2014-04-15 13:10:00 +00:00
|
|
|
var msg = __("Please specify a valid Row ID for row {0} in table {1}", [tax.idx, __(tax.doctype)])
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.throw(msg);
|
2013-05-24 13:55:01 +00:00
|
|
|
}
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
validate_inclusive_tax: function(tax) {
|
|
|
|
if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
var actual_type_error = function() {
|
2014-04-15 13:10:00 +00:00
|
|
|
var msg = __("Actual type tax cannot be included in Item rate in row {0}", [tax.idx])
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.throw(msg);
|
2013-05-24 13:55:01 +00:00
|
|
|
};
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
var on_previous_row_error = function(row_range) {
|
2014-04-15 13:10:00 +00:00
|
|
|
var msg = __("For row {0} in {1}. To include {2} in Item rate, rows {3} must also be included",
|
|
|
|
[tax.idx, __(tax.doctype), tax.charge_type, row_range])
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.throw(msg);
|
2013-05-24 13:55:01 +00:00
|
|
|
};
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
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") {
|
2014-04-15 13:10:00 +00:00
|
|
|
var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
|
2013-05-24 13:55:01 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
_load_item_tax_rate: function(item_tax_rate) {
|
|
|
|
return item_tax_rate ? JSON.parse(item_tax_rate) : {};
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
_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;
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
get_item_wise_taxes_html: function() {
|
|
|
|
var item_tax = {};
|
|
|
|
var tax_accounts = [];
|
|
|
|
var company_currency = this.get_company_currency();
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
$.each(this.get_tax_doclist(), function(i, tax) {
|
|
|
|
var tax_amount_precision = precision("tax_amount", tax);
|
2013-05-31 06:20:01 +00:00
|
|
|
var tax_rate_precision = precision("rate", tax);
|
2014-04-15 13:10:00 +00:00
|
|
|
$.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
|
2013-05-31 06:20:01 +00:00
|
|
|
function(item_code, tax_data) {
|
2013-05-24 13:55:01 +00:00
|
|
|
if(!item_tax[item_code]) item_tax[item_code] = {};
|
2013-05-31 06:20:01 +00:00
|
|
|
if($.isArray(tax_data)) {
|
2013-08-23 07:26:33 +00:00
|
|
|
var tax_rate = "";
|
|
|
|
if(tax_data[0] != null) {
|
|
|
|
tax_rate = (tax.charge_type === "Actual") ?
|
|
|
|
format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
|
|
|
|
(flt(tax_data[0], tax_rate_precision) + "%");
|
|
|
|
}
|
|
|
|
var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
|
|
|
|
tax_amount_precision);
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-08-05 07:30:53 +00:00
|
|
|
item_tax[item_code][tax.name] = [tax_rate, tax_amount];
|
2013-05-31 06:20:01 +00:00
|
|
|
} else {
|
2013-08-05 07:30:53 +00:00
|
|
|
item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
|
2013-05-31 06:20:01 +00:00
|
|
|
}
|
2013-05-24 13:55:01 +00:00
|
|
|
});
|
2013-08-05 07:30:53 +00:00
|
|
|
tax_accounts.push([tax.name, tax.account_head]);
|
2013-05-24 13:55:01 +00:00
|
|
|
});
|
2014-04-15 13:10:00 +00:00
|
|
|
|
|
|
|
var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
|
2013-05-24 13:55:01 +00:00
|
|
|
function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-09-05 11:23:57 +00:00
|
|
|
var distinct_item_names = [];
|
|
|
|
var distinct_items = [];
|
|
|
|
$.each(this.get_item_doclist(), function(i, item) {
|
|
|
|
if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
|
|
|
|
distinct_item_names.push(item.item_code || item.item_name);
|
|
|
|
distinct_items.push(item);
|
|
|
|
}
|
|
|
|
});
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-09-05 11:23:57 +00:00
|
|
|
var rows = $.map(distinct_items, function(item) {
|
2013-05-24 13:55:01 +00:00
|
|
|
var item_tax_record = item_tax[item.item_code || item.item_name];
|
2013-05-31 06:20:01 +00:00
|
|
|
if(!item_tax_record) { return null; }
|
2013-05-24 13:55:01 +00:00
|
|
|
return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
|
|
|
|
item_name: item.item_name,
|
|
|
|
taxes: $.map(tax_accounts, function(head) {
|
2013-08-05 07:30:53 +00:00
|
|
|
return item_tax_record[head[0]] ?
|
|
|
|
"<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
|
2013-07-17 09:42:22 +00:00
|
|
|
"<td></td>";
|
2013-05-24 13:55:01 +00:00
|
|
|
}).join("\n")
|
|
|
|
});
|
|
|
|
}).join("\n");
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-31 06:20:01 +00:00
|
|
|
if(!rows) return "";
|
2013-11-08 10:57:18 +00:00
|
|
|
return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
|
|
|
|
<div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
|
2013-05-24 13:55:01 +00:00
|
|
|
<thead><tr>' + headings + '</tr></thead> \
|
|
|
|
<tbody>' + rows + '</tbody> \
|
|
|
|
</table></div>';
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2014-02-12 09:23:18 +00:00
|
|
|
validate_company_and_party: function() {
|
2013-05-28 11:53:36 +00:00
|
|
|
var me = this;
|
2013-05-24 13:55:01 +00:00
|
|
|
var valid = true;
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2014-02-20 05:57:47 +00:00
|
|
|
$.each(["company", "customer"], function(i, fieldname) {
|
2014-02-14 10:17:51 +00:00
|
|
|
if(frappe.meta.has_field(me.frm.doc.doctype, fieldname)) {
|
2014-02-12 09:23:18 +00:00
|
|
|
if (!me.frm.doc[fieldname]) {
|
2014-04-15 13:10:00 +00:00
|
|
|
msgprint(__("Please specify") + ": " +
|
|
|
|
frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
|
2014-04-14 10:55:30 +00:00
|
|
|
". " + __("It is needed to fetch Item Details."));
|
2014-02-12 09:23:18 +00:00
|
|
|
valid = false;
|
|
|
|
}
|
2013-05-24 13:55:01 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return valid;
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
get_item_doclist: function() {
|
2014-03-26 12:54:30 +00:00
|
|
|
return this.frm.doc[this.fname] || [];
|
2013-05-24 13:55:01 +00:00
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
get_tax_doclist: function() {
|
2014-03-26 12:54:30 +00:00
|
|
|
return this.frm.doc[this.other_fname] || [];
|
2013-05-24 13:55:01 +00:00
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
validate_conversion_rate: function() {
|
|
|
|
this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
|
2014-04-15 13:10:00 +00:00
|
|
|
var conversion_rate_label = frappe.meta.get_label(this.frm.doc.doctype, "conversion_rate",
|
2013-05-24 13:55:01 +00:00
|
|
|
this.frm.doc.name);
|
|
|
|
var company_currency = this.get_company_currency();
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-10-11 08:04:10 +00:00
|
|
|
if(!this.frm.doc.conversion_rate) {
|
2014-04-15 13:10:00 +00:00
|
|
|
frappe.throw(repl('%(conversion_rate_label)s' +
|
|
|
|
__(' is mandatory. Maybe Currency Exchange record is not created for ') +
|
|
|
|
'%(from_currency)s' + __(" to ") + '%(to_currency)s',
|
2013-11-12 13:57:16 +00:00
|
|
|
{
|
|
|
|
"conversion_rate_label": conversion_rate_label,
|
2013-11-15 08:08:23 +00:00
|
|
|
"from_currency": this.frm.doc.currency,
|
2013-11-12 13:57:16 +00:00
|
|
|
"to_currency": company_currency
|
2013-11-15 08:08:23 +00:00
|
|
|
}));
|
2013-09-17 06:26:17 +00:00
|
|
|
}
|
2013-05-24 13:55:01 +00:00
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
calculate_taxes_and_totals: function() {
|
2014-01-03 12:45:07 +00:00
|
|
|
this.discount_amount_applied = false;
|
|
|
|
this._calculate_taxes_and_totals();
|
2014-02-14 10:17:51 +00:00
|
|
|
if (frappe.meta.get_docfield(this.frm.doc.doctype, "discount_amount"))
|
2014-01-03 12:45:07 +00:00
|
|
|
this.apply_discount_amount();
|
|
|
|
},
|
|
|
|
|
|
|
|
_calculate_taxes_and_totals: function() {
|
2013-05-24 13:55:01 +00:00
|
|
|
this.validate_conversion_rate();
|
|
|
|
this.frm.item_doclist = this.get_item_doclist();
|
|
|
|
this.frm.tax_doclist = this.get_tax_doclist();
|
2013-12-25 14:16:20 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
this.calculate_item_values();
|
|
|
|
this.initialize_taxes();
|
|
|
|
this.determine_exclusive_rate && this.determine_exclusive_rate();
|
|
|
|
this.calculate_net_total();
|
|
|
|
this.calculate_taxes();
|
|
|
|
this.calculate_totals();
|
|
|
|
this._cleanup();
|
2014-01-03 12:45:07 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
this.show_item_wise_taxes();
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
initialize_taxes: function() {
|
|
|
|
var me = this;
|
2013-12-23 10:19:08 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
$.each(this.frm.tax_doclist, function(i, tax) {
|
|
|
|
tax.item_wise_tax_detail = {};
|
2014-04-15 13:10:00 +00:00
|
|
|
tax_fields = ["total", "tax_amount_after_discount_amount",
|
2013-05-24 13:55:01 +00:00
|
|
|
"tax_amount_for_current_item", "grand_total_for_current_item",
|
2013-12-23 10:19:08 +00:00
|
|
|
"tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
|
|
|
|
|
2014-01-03 12:45:07 +00:00
|
|
|
if (!me.discount_amount_applied)
|
2013-12-23 10:19:08 +00:00
|
|
|
tax_fields.push("tax_amount");
|
|
|
|
|
|
|
|
$.each(tax_fields, function(i, fieldname) { tax[fieldname] = 0.0 });
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
me.validate_on_previous_row(tax);
|
|
|
|
me.validate_inclusive_tax(tax);
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe.model.round_floats_in(tax);
|
2013-05-24 13:55:01 +00:00
|
|
|
});
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
calculate_taxes: function() {
|
|
|
|
var me = this;
|
2014-01-14 12:14:34 +00:00
|
|
|
var actual_tax_dict = {};
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2014-01-14 12:14:34 +00:00
|
|
|
// maintain actual tax rate based on idx
|
|
|
|
$.each(this.frm.tax_doclist, function(i, tax) {
|
|
|
|
if (tax.charge_type == "Actual") {
|
|
|
|
actual_tax_dict[tax.idx] = flt(tax.rate);
|
|
|
|
}
|
|
|
|
});
|
2013-12-25 14:16:20 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
$.each(this.frm.item_doclist, function(n, item) {
|
|
|
|
var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
|
2013-12-25 14:16:20 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
$.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);
|
2013-05-31 06:20:01 +00:00
|
|
|
|
2014-01-14 12:14:34 +00:00
|
|
|
// Adjust divisional loss to the last item
|
|
|
|
if (tax.charge_type == "Actual") {
|
|
|
|
actual_tax_dict[tax.idx] -= current_tax_amount;
|
|
|
|
if (n == me.frm.item_doclist.length - 1) {
|
|
|
|
current_tax_amount += actual_tax_dict[tax.idx]
|
2013-05-24 13:55:01 +00:00
|
|
|
}
|
2014-01-14 12:14:34 +00:00
|
|
|
}
|
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
// 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;
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
// accumulate tax amount into tax.tax_amount
|
2014-01-03 12:45:07 +00:00
|
|
|
if (!me.discount_amount_applied)
|
2013-12-23 10:19:08 +00:00
|
|
|
tax.tax_amount += current_tax_amount;
|
2013-12-25 14:16:20 +00:00
|
|
|
|
2014-01-03 12:45:07 +00:00
|
|
|
tax.tax_amount_after_discount_amount += current_tax_amount;
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
// for buying
|
|
|
|
if(tax.category) {
|
|
|
|
// if just for valuation, do not add the tax amount in total
|
|
|
|
// hence, setting it as 0 for further steps
|
|
|
|
current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
|
|
|
|
}
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
// Calculate tax.total viz. grand total till that step
|
2014-04-15 13:10:00 +00:00
|
|
|
// note: grand_total_for_current_item contains the contribution of
|
2013-05-24 13:55:01 +00:00
|
|
|
// item's amount, previously applied tax and the current tax on that item
|
|
|
|
if(i==0) {
|
2014-02-10 13:50:15 +00:00
|
|
|
tax.grand_total_for_current_item = flt(item.base_amount + current_tax_amount,
|
2013-05-24 13:55:01 +00:00
|
|
|
precision("total", tax));
|
|
|
|
} else {
|
2014-04-15 13:10:00 +00:00
|
|
|
tax.grand_total_for_current_item =
|
2013-05-24 13:55:01 +00:00
|
|
|
flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
|
|
|
|
precision("total", tax));
|
|
|
|
}
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
// in tax.total, accumulate grand total for each item
|
|
|
|
tax.total += tax.grand_total_for_current_item;
|
2013-12-25 14:16:20 +00:00
|
|
|
|
|
|
|
// set precision in the last item iteration
|
2014-01-14 12:14:34 +00:00
|
|
|
if (n == me.frm.item_doclist.length - 1) {
|
2013-12-25 14:16:20 +00:00
|
|
|
me.round_off_totals(tax);
|
|
|
|
|
2014-01-03 12:45:07 +00:00
|
|
|
// adjust Discount Amount loss in last tax iteration
|
|
|
|
if ((i == me.frm.tax_doclist.length - 1) && me.discount_amount_applied)
|
|
|
|
me.adjust_discount_amount_loss(tax);
|
2014-01-14 12:14:34 +00:00
|
|
|
}
|
2013-05-24 13:55:01 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2013-12-25 14:16:20 +00:00
|
|
|
|
|
|
|
round_off_totals: function(tax) {
|
|
|
|
tax.total = flt(tax.total, precision("total", tax));
|
|
|
|
tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax));
|
2014-04-15 13:10:00 +00:00
|
|
|
tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount,
|
2013-12-25 14:16:20 +00:00
|
|
|
precision("tax_amount", tax));
|
|
|
|
},
|
|
|
|
|
2014-01-03 12:45:07 +00:00
|
|
|
adjust_discount_amount_loss: function(tax) {
|
|
|
|
var discount_amount_loss = this.frm.doc.grand_total - flt(this.frm.doc.discount_amount) - tax.total;
|
2014-04-15 13:10:00 +00:00
|
|
|
tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
|
2014-01-03 12:45:07 +00:00
|
|
|
discount_amount_loss, precision("tax_amount", tax));
|
|
|
|
tax.total = flt(tax.total + discount_amount_loss, precision("total", tax));
|
2013-12-25 14:16:20 +00:00
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
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;
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
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 ?
|
2014-02-10 13:50:15 +00:00
|
|
|
((item.base_amount / this.frm.doc.net_total) * actual) :
|
2013-05-24 13:55:01 +00:00
|
|
|
0.0;
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
} else if(tax.charge_type == "On Net Total") {
|
2014-02-10 13:50:15 +00:00
|
|
|
current_tax_amount = (tax_rate / 100.0) * item.base_amount;
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
} 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;
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
} 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;
|
|
|
|
}
|
2013-12-23 10:19:08 +00:00
|
|
|
|
2013-05-31 06:20:01 +00:00
|
|
|
current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-31 06:20:01 +00:00
|
|
|
// store tax breakup for each item
|
|
|
|
tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-31 06:20:01 +00:00
|
|
|
return current_tax_amount;
|
2013-05-24 13:55:01 +00:00
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
_cleanup: function() {
|
|
|
|
$.each(this.frm.tax_doclist, function(i, tax) {
|
|
|
|
$.each(["tax_amount_for_current_item", "grand_total_for_current_item",
|
2014-04-15 13:10:00 +00:00
|
|
|
"tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
|
2013-05-24 13:55:01 +00:00
|
|
|
function(i, fieldname) { delete tax[fieldname]; });
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
|
|
|
|
});
|
|
|
|
},
|
2013-05-27 13:59:07 +00:00
|
|
|
|
2014-04-16 09:51:46 +00:00
|
|
|
calculate_total_advance: function(parenttype, advance_parentfield, update_paid_amount) {
|
2013-05-27 13:59:07 +00:00
|
|
|
if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
|
2014-03-26 12:54:30 +00:00
|
|
|
var advance_doclist = this.frm.doc[advance_parentfield] || [];
|
2014-02-14 10:17:51 +00:00
|
|
|
this.frm.doc.total_advance = flt(frappe.utils.sum(
|
2013-05-27 13:59:07 +00:00
|
|
|
$.map(advance_doclist, function(adv) { return adv.allocated_amount })
|
|
|
|
), precision("total_advance"));
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2014-04-16 09:51:46 +00:00
|
|
|
this.calculate_outstanding_amount(update_paid_amount);
|
2013-05-27 13:59:07 +00:00
|
|
|
}
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-05-24 13:55:01 +00:00
|
|
|
_set_in_company_currency: function(item, print_field, base_field) {
|
|
|
|
// set values in base currency
|
|
|
|
item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
|
|
|
|
precision(base_field, item));
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
|
2013-06-05 08:41:32 +00:00
|
|
|
get_terms: function() {
|
|
|
|
var me = this;
|
|
|
|
if(this.frm.doc.tc_name) {
|
2013-07-29 14:00:39 +00:00
|
|
|
return this.frm.call({
|
2014-02-14 10:17:51 +00:00
|
|
|
method: "frappe.client.get_value",
|
2013-06-05 08:41:32 +00:00
|
|
|
args: {
|
|
|
|
doctype: "Terms and Conditions",
|
|
|
|
fieldname: "terms",
|
|
|
|
filters: { name: this.frm.doc.tc_name },
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2014-01-29 11:01:38 +00:00
|
|
|
|
|
|
|
taxes_and_charges: function() {
|
|
|
|
var me = this;
|
|
|
|
if(this.frm.doc.taxes_and_charges) {
|
|
|
|
return this.frm.call({
|
2014-05-02 10:15:10 +00:00
|
|
|
method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
|
|
|
|
args: {
|
2014-05-13 11:08:31 +00:00
|
|
|
"master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
|
|
|
|
this.frm.doc.name).options,
|
2014-05-02 10:15:10 +00:00
|
|
|
"master_name": this.frm.doc.taxes_and_charges,
|
|
|
|
"tax_parentfield": this.other_fname
|
|
|
|
},
|
2014-01-29 11:01:38 +00:00
|
|
|
callback: function(r) {
|
|
|
|
if(!r.exc) {
|
2014-05-02 10:15:10 +00:00
|
|
|
me.frm.set_value(me.other_fname, r.message);
|
2014-01-29 11:01:38 +00:00
|
|
|
me.calculate_taxes_and_totals();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-05-02 11:33:18 +00:00
|
|
|
}
|
2014-01-29 11:01:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
show_item_wise_taxes: function() {
|
|
|
|
if(this.frm.fields_dict.other_charges_calculation) {
|
|
|
|
$(this.get_item_wise_taxes_html())
|
|
|
|
.appendTo($(this.frm.fields_dict.other_charges_calculation.wrapper).empty());
|
|
|
|
}
|
|
|
|
},
|
2014-04-15 13:10:00 +00:00
|
|
|
});
|