blob: f62eed2175ed1b5954e61df3d89282cd91fb02dc [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302// License: GNU General Public License v3. See license.txt
Anand Doshi3543f302013-05-24 19:25:01 +05303
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05304frappe.provide("erpnext");
Nabin Haitb4a3dfa2015-01-22 17:13:13 +05305frappe.require("assets/erpnext/js/controllers/taxes_and_totals.js");
Neil Trini Lasrado78fa6952014-10-03 17:43:02 +05306frappe.require("assets/erpnext/js/utils.js");
7
Nabin Haitb4a3dfa2015-01-22 17:13:13 +05308erpnext.TransactionController = erpnext.taxes_and_totals.extend({
Anand Doshi3543f302013-05-24 19:25:01 +05309 onload: function() {
Anand Doshi1e4fb682013-08-23 12:56:33 +053010 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +053011 if(this.frm.doc.__islocal) {
Anand Doshi1e4fb682013-08-23 12:56:33 +053012 var today = get_today(),
Nabin Hait73083dc2014-03-14 15:10:42 +053013 currency = frappe.defaults.get_user_default("currency");
Rushabh Mehtaff938022014-04-15 18:40:00 +053014
Anand Doshi3543f302013-05-24 19:25:01 +053015 $.each({
Anand Doshifc777182013-05-27 19:29:07 +053016 posting_date: today,
17 due_date: today,
18 transaction_date: today,
19 currency: currency,
20 price_list_currency: currency,
21 status: "Draft",
Anand Doshifc777182013-05-27 19:29:07 +053022 is_subcontracted: "No",
Anand Doshi3543f302013-05-24 19:25:01 +053023 }, function(fieldname, value) {
24 if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname])
25 me.frm.set_value(fieldname, value);
Rushabh Mehtaff938022014-04-15 18:40:00 +053026 });
nabinhaitbc408d82014-07-07 16:51:52 +053027
nabinhaitb7392642014-07-28 14:55:08 +053028 if(this.frm.doc.company && !this.frm.doc.amended_from) {
nabinhaitbc408d82014-07-07 16:51:52 +053029 cur_frm.script_manager.trigger("company");
30 }
Anand Doshi3543f302013-05-24 19:25:01 +053031 }
Akhilesh Darjee564c6212013-10-17 11:31:11 +053032
Nabin Haitdd38a262014-12-26 13:15:21 +053033 if(this.frm.fields_dict["taxes"]) {
34 this["taxes_remove"] = this.calculate_taxes_and_totals;
Anand Doshia91c95f2013-08-23 13:00:47 +053035 }
Rushabh Mehtaff938022014-04-15 18:40:00 +053036
Nabin Haitdd38a262014-12-26 13:15:21 +053037 if(this.frm.fields_dict["items"]) {
38 this["items_remove"] = this.calculate_taxes_and_totals;
Anand Doshi1e4fb682013-08-23 12:56:33 +053039 }
Anand Doshi3543f302013-05-24 19:25:01 +053040 },
Rushabh Mehtaff938022014-04-15 18:40:00 +053041
Anand Doshi5e4e5d72013-08-06 17:23:44 +053042 onload_post_render: function() {
Akhilesh Darjee61b5c592013-10-15 20:24:34 +053043 var me = this;
Nabin Haitdd38a262014-12-26 13:15:21 +053044 if(this.frm.doc.__islocal && this.frm.doc.company && this.frm.doc["items"] && !this.frm.doc.is_pos) {
Anand Doshib054eb72014-04-22 20:35:22 +053045 this.calculate_taxes_and_totals();
Anand Doshi5e4e5d72013-08-06 17:23:44 +053046 }
Nabin Haitdd38a262014-12-26 13:15:21 +053047 if(frappe.meta.get_docfield(this.frm.doc.doctype + " Item", "item_code")) {
48 cur_frm.get_field("items").grid.set_multiple_add("item_code", "qty");
Rushabh Mehtab6843f22014-06-04 13:10:41 +053049 }
Anand Doshi5e4e5d72013-08-06 17:23:44 +053050 },
Rushabh Mehtaff938022014-04-15 18:40:00 +053051
Anand Doshi3543f302013-05-24 19:25:01 +053052 refresh: function() {
Anand Doshi8bde7f92014-04-24 18:11:49 +053053 erpnext.toggle_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +053054 erpnext.hide_company();
Nabin Hait239c2c32015-01-15 14:21:41 +053055 this.hide_currency_and_price_list()
Anand Doshi3543f302013-05-24 19:25:01 +053056 this.show_item_wise_taxes();
Nabin Hait1f996c32013-07-29 19:00:53 +053057 this.set_dynamic_labels();
Anand Doshi50d7e8c2015-01-06 17:14:13 +053058 erpnext.pos.make_pos_btn(this.frm);
Rushabh Mehta614e7ab2015-02-18 19:51:48 +053059 this.setup_sms();
60 },
61
62 setup_sms: function() {
63 var me = this;
64 if(this.frm.doc.docstatus===1 && !in_list(["Lost", "Stopped"], this.frm.doc.status)) {
65 this.frm.page.add_menu_item(__('Send SMS'), function() { me.send_sms(); });
66 }
67 },
68
69 send_sms: function() {
70 frappe.require("assets/erpnext/js/sms_manager.js");
71 var sms_man = new SMSManager(this.doc);
Nabin Hait239c2c32015-01-15 14:21:41 +053072 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +053073
Nabin Hait239c2c32015-01-15 14:21:41 +053074 hide_currency_and_price_list: function() {
75 if(this.frm.doc.docstatus > 0) {
76 hide_field("currency_and_price_list");
77 } else {
78 unhide_field("currency_and_price_list");
79 }
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053080 },
81
Nabin Hait139dc7b2014-02-12 14:53:18 +053082 item_code: function(doc, cdt, cdn) {
83 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +053084 var item = frappe.get_doc(cdt, cdn);
Nabin Hait139dc7b2014-02-12 14:53:18 +053085 if(item.item_code || item.barcode || item.serial_no) {
86 if(!this.validate_company_and_party()) {
Nabin Haitdd38a262014-12-26 13:15:21 +053087 cur_frm.fields_dict["items"].grid.grid_rows[item.idx - 1].remove();
Nabin Hait139dc7b2014-02-12 14:53:18 +053088 } else {
89 return this.frm.call({
90 method: "erpnext.stock.get_item_details.get_item_details",
91 child: item,
92 args: {
93 args: {
94 item_code: item.item_code,
95 barcode: item.barcode,
96 serial_no: item.serial_no,
97 warehouse: item.warehouse,
Nabin Hait444f9562014-06-20 15:59:49 +053098 parenttype: me.frm.doc.doctype,
99 parent: me.frm.doc.name,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530100 customer: me.frm.doc.customer,
101 supplier: me.frm.doc.supplier,
102 currency: me.frm.doc.currency,
103 conversion_rate: me.frm.doc.conversion_rate,
104 price_list: me.frm.doc.selling_price_list ||
105 me.frm.doc.buying_price_list,
106 price_list_currency: me.frm.doc.price_list_currency,
107 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
108 company: me.frm.doc.company,
109 order_type: me.frm.doc.order_type,
110 is_pos: cint(me.frm.doc.is_pos),
111 is_subcontracted: me.frm.doc.is_subcontracted,
Nabin Haita1bf43b2015-03-17 10:50:47 +0530112 transaction_date: me.frm.doc.transaction_date || me.frm.doc.posting_date,
Nabin Hait444f9562014-06-20 15:59:49 +0530113 ignore_pricing_rule: me.frm.doc.ignore_pricing_rule,
114 doctype: item.doctype,
Anand Doshi43f087c2014-08-26 14:25:53 +0530115 name: item.name,
Anand Doshi9b955fe2015-01-05 16:19:12 +0530116 project_name: item.project_name || me.frm.doc.project_name,
117 qty: item.qty
Nabin Hait139dc7b2014-02-12 14:53:18 +0530118 }
119 },
Anand Doshi13945092014-09-21 19:45:49 +0530120
Nabin Hait139dc7b2014-02-12 14:53:18 +0530121 callback: function(r) {
122 if(!r.exc) {
123 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
124 }
125 }
126 });
127 }
128 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530129 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530130
131 serial_no: function(doc, cdt, cdn) {
132 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530133 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530134
135 if (item.serial_no) {
136 if (!item.item_code) {
137 this.frm.script_manager.trigger("item_code", cdt, cdn);
138 }
139 else {
140 var sr_no = [];
141
142 // Replacing all occurences of comma with carriage return
143 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
144
145 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530146
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530147 // Trim each string and push unique string to new list
148 for (var x=0; x<=serial_nos.length - 1; x++) {
149 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
150 sr_no.push(serial_nos[x].trim());
151 }
152 }
153
154 // Add the new list to the serial no. field in grid with each in new line
155 item.serial_no = "";
156 for (var x=0; x<=sr_no.length - 1; x++)
157 item.serial_no += sr_no[x] + '\n';
158
159 refresh_field("serial_no", item.name, item.parentfield);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530160 frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530161 }
162 }
163 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530164
Anand Doshi923d41d2013-05-28 17:23:36 +0530165 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530166 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530167 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530168
Anand Doshi3543f302013-05-24 19:25:01 +0530169 company: function() {
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530170 var me = this;
171 var fn = function() {
172 if(me.frm.doc.company && me.frm.fields_dict.currency) {
173 var company_currency = me.get_company_currency();
174 if (!me.frm.doc.currency) {
175 me.frm.set_value("currency", company_currency);
176 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530177
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530178 if (me.frm.doc.currency == company_currency) {
179 me.frm.set_value("conversion_rate", 1.0);
180 }
181 if (me.frm.doc.price_list_currency == company_currency) {
182 me.frm.set_value('plc_conversion_rate', 1.0);
183 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530184
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530185 me.frm.script_manager.trigger("currency");
186 me.apply_pricing_rule();
Nabin Haitdd38a262014-12-26 13:15:21 +0530187 }
Anand Doshi3543f302013-05-24 19:25:01 +0530188 }
Nabin Haitdd38a262014-12-26 13:15:21 +0530189
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530190 if (this.frm.doc.posting_date) var date = this.frm.doc.posting_date;
191 else var date = this.frm.doc.transaction_date;
192 erpnext.get_fiscal_year(this.frm.doc.company, date, fn);
Neil Trini Lasrado31a92bd2014-12-15 15:49:40 +0530193 erpnext.get_letter_head(this.frm.doc.company);
Rushabh Mehta45ca8a42015-04-28 16:02:09 +0530194
195 if(this.frm.doc.company) {
196 erpnext.last_selected_company = this.frm.doc.company;
197 }
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530198 },
199
200 transaction_date: function() {
201 erpnext.get_fiscal_year(this.frm.doc.company, this.frm.doc.transaction_date);
Neil Trini Lasrado78fa6952014-10-03 17:43:02 +0530202 },
203
204 posting_date: function() {
Neil Trini Lasrado565d9862014-10-07 18:26:39 +0530205 erpnext.get_fiscal_year(this.frm.doc.company, this.frm.doc.posting_date);
Anand Doshi3543f302013-05-24 19:25:01 +0530206 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530207
Anand Doshi3543f302013-05-24 19:25:01 +0530208 get_company_currency: function() {
209 return erpnext.get_currency(this.frm.doc.company);
210 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530211
Anand Doshi3543f302013-05-24 19:25:01 +0530212 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530213 var me = this;
214 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530215
Anand Doshi61a2f682013-06-21 17:55:31 +0530216 var company_currency = this.get_company_currency();
217 if(this.frm.doc.currency !== company_currency) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530218 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530219 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530220 me.frm.set_value("conversion_rate", exchange_rate);
221 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530222 });
223 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530224 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530225 }
226 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530227
Anand Doshi61a2f682013-06-21 17:55:31 +0530228 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530229 if(this.frm.doc.currency === this.get_company_currency()) {
230 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530231 }
232 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530233 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
234 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
235 }
Nabin Haita3dd72a2014-05-28 12:49:20 +0530236 if(flt(this.frm.doc.conversion_rate)>0.0) {
Nabin Hait444f9562014-06-20 15:59:49 +0530237 if(this.frm.doc.ignore_pricing_rule) {
238 this.calculate_taxes_and_totals();
Anand Doshi51f722d2014-07-04 15:44:26 +0530239 } else if (!this.in_apply_price_list){
240 this.apply_price_list();
Nabin Hait444f9562014-06-20 15:59:49 +0530241 }
242
Nabin Haita3dd72a2014-05-28 12:49:20 +0530243 }
Anand Doshi3543f302013-05-24 19:25:01 +0530244 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530245
Anand Doshi61a2f682013-06-21 17:55:31 +0530246 get_exchange_rate: function(from_currency, to_currency, callback) {
247 var exchange_name = from_currency + "-" + to_currency;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530248 frappe.model.with_doc("Currency Exchange", exchange_name, function(name) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530249 var exchange_doc = frappe.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530250 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530251 });
252 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530253
Anand Doshi3543f302013-05-24 19:25:01 +0530254 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530255 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530256 this.set_dynamic_labels();
Anand Doshi51f722d2014-07-04 15:44:26 +0530257 this.set_plc_conversion_rate();
Anand Doshi3543f302013-05-24 19:25:01 +0530258 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530259
Anand Doshi3543f302013-05-24 19:25:01 +0530260 plc_conversion_rate: function() {
Anand Doshi51f722d2014-07-04 15:44:26 +0530261 this.set_plc_conversion_rate();
262 if(!this.in_apply_price_list) {
263 this.apply_price_list();
264 }
265 },
266
267 set_plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530268 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
269 this.frm.set_value("plc_conversion_rate", 1.0);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530270 }
271 if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
Anand Doshi61a2f682013-06-21 17:55:31 +0530272 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530273 }
Anand Doshi3543f302013-05-24 19:25:01 +0530274 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530275
Anand Doshi3543f302013-05-24 19:25:01 +0530276 qty: function(doc, cdt, cdn) {
Nabin Hait444f9562014-06-20 15:59:49 +0530277 this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
Anand Doshi3543f302013-05-24 19:25:01 +0530278 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530279
Anand Doshi61a2f682013-06-21 17:55:31 +0530280 set_dynamic_labels: function() {
281 // What TODO? should we make price list system non-mandatory?
282 this.frm.toggle_reqd("plc_conversion_rate",
283 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530284
Anand Doshi61a2f682013-06-21 17:55:31 +0530285 var company_currency = this.get_company_currency();
286 this.change_form_labels(company_currency);
287 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530288 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530289 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530290
Nabin Hait613d0812015-02-23 11:58:15 +0530291 change_form_labels: function(company_currency) {
292 var me = this;
293 var field_label_map = {};
294
295 var setup_field_label_map = function(fields_list, currency) {
296 $.each(fields_list, function(i, fname) {
297 var docfield = frappe.meta.docfield_map[me.frm.doc.doctype][fname];
298 if(docfield) {
299 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
300 field_label_map[fname] = label.trim() + " (" + currency + ")";
301 }
302 });
303 };
304 setup_field_label_map(["base_total", "base_net_total", "base_total_taxes_and_charges",
305 "base_discount_amount", "base_grand_total", "base_rounded_total", "base_in_words",
306 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted", "total_amount_to_pay",
Nabin Hait37b047d2015-02-23 16:01:33 +0530307 "outstanding_amount", "total_advance", "paid_amount", "write_off_amount"], company_currency);
Nabin Hait613d0812015-02-23 11:58:15 +0530308
309 setup_field_label_map(["total", "net_total", "total_taxes_and_charges", "discount_amount",
310 "grand_total", "taxes_and_charges_added", "taxes_and_charges_deducted",
311 "rounded_total", "in_words"], this.frm.doc.currency);
312
313 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
314 + " = [?] " + company_currency)
315
316 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
317 cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency
318 + " = [?] " + company_currency)
319 }
320
321 // toggle fields
322 this.frm.toggle_display(["conversion_rate", "base_total", "base_net_total", "base_total_taxes_and_charges",
323 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted",
324 "base_grand_total", "base_rounded_total", "base_in_words", "base_discount_amount"],
325 this.frm.doc.currency != company_currency);
326
327 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
328 this.frm.doc.price_list_currency != company_currency);
329
330 // set labels
331 $.each(field_label_map, function(fname, label) {
332 me.frm.fields_dict[fname].set_label(label);
333 });
Nabin Hait37b047d2015-02-23 16:01:33 +0530334
Nabin Hait903c42a2015-02-24 15:24:49 +0530335 var show =cint(cur_frm.doc.discount_amount) ||
336 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
337
338 if(frappe.meta.get_docfield(cur_frm.doctype, "net_total"))
339 cur_frm.toggle_display("net_total", show);
340
341 if(frappe.meta.get_docfield(cur_frm.doctype, "base_net_total"))
342 cur_frm.toggle_display("base_net_total", (show && (me.frm.doc.currency != company_currency)));
343
Nabin Hait613d0812015-02-23 11:58:15 +0530344 },
345
346 change_grid_labels: function(company_currency) {
347 var me = this;
348 var field_label_map = {};
349
350 var setup_field_label_map = function(fields_list, currency, parentfield) {
351 var grid_doctype = me.frm.fields_dict[parentfield].grid.doctype;
352 $.each(fields_list, function(i, fname) {
353 var docfield = frappe.meta.docfield_map[grid_doctype][fname];
354 if(docfield) {
355 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
356 field_label_map[grid_doctype + "-" + fname] =
357 label.trim() + " (" + currency + ")";
358 }
359 });
360 }
361
362 setup_field_label_map(["base_rate", "base_net_rate", "base_price_list_rate", "base_amount", "base_net_amount"],
363 company_currency, "items");
364
365 setup_field_label_map(["rate", "net_rate", "price_list_rate", "amount", "net_amount"],
366 this.frm.doc.currency, "items");
367
368 if(this.frm.fields_dict["taxes"]) {
369 setup_field_label_map(["tax_amount", "total", "tax_amount_after_discount"], this.frm.doc.currency, "taxes");
370
371 setup_field_label_map(["base_tax_amount", "base_total", "base_tax_amount_after_discount"], company_currency, "taxes");
372 }
373
374 if(this.frm.fields_dict["advances"]) {
375 setup_field_label_map(["advance_amount", "allocated_amount"], company_currency, "advances");
376 }
377
378 // toggle columns
379 var item_grid = this.frm.fields_dict["items"].grid;
380 $.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) {
381 if(frappe.meta.get_docfield(item_grid.doctype, fname))
382 item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency);
383 });
384
385 var show = (cint(cur_frm.doc.discount_amount)) ||
386 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
387
388 $.each(["net_rate", "net_amount"], function(i, fname) {
389 if(frappe.meta.get_docfield(item_grid.doctype, fname))
390 item_grid.set_column_disp(fname, show);
391 });
392
393 $.each(["base_net_rate", "base_net_amount"], function(i, fname) {
394 if(frappe.meta.get_docfield(item_grid.doctype, fname))
395 item_grid.set_column_disp(fname, (show && (me.frm.doc.currency != company_currency)));
396 });
397
398 // set labels
399 var $wrapper = $(this.frm.wrapper);
400 $.each(field_label_map, function(fname, label) {
401 fname = fname.split("-");
402 var df = frappe.meta.get_docfield(fname[0], fname[1], me.frm.doc.name);
403 if(df) df.label = label;
404 });
405 },
406
Anand Doshi923d41d2013-05-28 17:23:36 +0530407 recalculate: function() {
408 this.calculate_taxes_and_totals();
409 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530410
Anand Doshi923d41d2013-05-28 17:23:36 +0530411 recalculate_values: function() {
412 this.calculate_taxes_and_totals();
413 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530414
Anand Doshid0b00722013-05-28 18:54:48 +0530415 calculate_charges: function() {
416 this.calculate_taxes_and_totals();
417 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530418
Nabin Hait444f9562014-06-20 15:59:49 +0530419 ignore_pricing_rule: function() {
420 this.apply_pricing_rule();
421 },
422
423 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530424 var me = this;
Anand Doshidffec8f2014-07-01 17:45:15 +0530425 return this.frm.call({
426 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
427 args: { args: this._get_args(item) },
428 callback: function(r) {
Anand Doshif214bfc2014-07-16 17:52:08 +0530429 if (!r.exc && r.message) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530430 me._set_values_for_item_list(r.message);
431 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
432 }
433 }
434 });
435 },
436
437 _get_args: function(item) {
438 var me = this;
439 return {
440 "item_list": this._get_item_list(item),
Nabin Hait444f9562014-06-20 15:59:49 +0530441 "customer": me.frm.doc.customer,
442 "customer_group": me.frm.doc.customer_group,
443 "territory": me.frm.doc.territory,
444 "supplier": me.frm.doc.supplier,
445 "supplier_type": me.frm.doc.supplier_type,
446 "currency": me.frm.doc.currency,
447 "conversion_rate": me.frm.doc.conversion_rate,
448 "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
449 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
450 "company": me.frm.doc.company,
451 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
452 "campaign": me.frm.doc.campaign,
453 "sales_partner": me.frm.doc.sales_partner,
454 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
455 "parenttype": me.frm.doc.doctype,
456 "parent": me.frm.doc.name
457 };
Nabin Hait444f9562014-06-20 15:59:49 +0530458 },
459
460 _get_item_list: function(item) {
461 var item_list = [];
462 var append_item = function(d) {
Anand Doshi39edcbc2014-07-28 16:00:19 +0530463 if (d.item_code) {
464 item_list.push({
465 "doctype": d.doctype,
466 "name": d.name,
467 "item_code": d.item_code,
468 "item_group": d.item_group,
469 "brand": d.brand,
470 "qty": d.qty
471 });
472 }
Nabin Hait444f9562014-06-20 15:59:49 +0530473 };
Nabin Haita3dd72a2014-05-28 12:49:20 +0530474
Nabin Hait444f9562014-06-20 15:59:49 +0530475 if (item) {
476 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +0530477 } else {
Nabin Hait96339342015-01-21 17:22:11 +0530478 $.each(this.frm.doc["items"] || [], function(i, d) {
Nabin Hait444f9562014-06-20 15:59:49 +0530479 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530480 });
481 }
Nabin Hait444f9562014-06-20 15:59:49 +0530482 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +0530483 },
484
Anand Doshidffec8f2014-07-01 17:45:15 +0530485 _set_values_for_item_list: function(children) {
Nabin Hait49a27292014-12-17 11:21:32 +0530486 var me = this;
Nabin Hait7d8fa802014-12-30 15:35:45 +0530487 var price_list_rate_changed = false;
Anand Doshidea3fb02015-02-05 15:05:58 +0530488 for(var i=0, l=children.length; i<l; i++) {
489 var d = children[i];
Nabin Hait49a27292014-12-17 11:21:32 +0530490 var existing_pricing_rule = frappe.model.get_value(d.doctype, d.name, "pricing_rule");
Anand Doshidea3fb02015-02-05 15:05:58 +0530491
492 for(var k in d) {
493 var v = d[k];
Anand Doshidffec8f2014-07-01 17:45:15 +0530494 if (["doctype", "name"].indexOf(k)===-1) {
Nabin Hait7d8fa802014-12-30 15:35:45 +0530495 if(k=="price_list_rate") {
496 if(flt(v) != flt(d.price_list_rate)) price_list_rate_changed = true;
497 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530498 frappe.model.set_value(d.doctype, d.name, k, v);
499 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530500 }
501
Nabin Hait49a27292014-12-17 11:21:32 +0530502 // if pricing rule set as blank from an existing value, apply price_list
Nabin Haitcfe3c542014-12-22 10:42:48 +0530503 if(!me.frm.doc.ignore_pricing_rule && existing_pricing_rule && !d.pricing_rule) {
Nabin Hait49a27292014-12-17 11:21:32 +0530504 me.apply_price_list(frappe.get_doc(d.doctype, d.name));
505 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530506 }
Nabin Hait7d8fa802014-12-30 15:35:45 +0530507
Anand Doshidea3fb02015-02-05 15:05:58 +0530508 if(!price_list_rate_changed) me.calculate_taxes_and_totals();
Anand Doshidffec8f2014-07-01 17:45:15 +0530509 },
510
Nabin Hait49a27292014-12-17 11:21:32 +0530511 apply_price_list: function(item) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530512 var me = this;
Rushabh Mehta052bd622015-01-30 16:57:24 +0530513 var args = this._get_args(item);
Nabin Hait37b047d2015-02-23 16:01:33 +0530514
Anand Doshidffec8f2014-07-01 17:45:15 +0530515 return this.frm.call({
516 method: "erpnext.stock.get_item_details.apply_price_list",
Rushabh Mehta052bd622015-01-30 16:57:24 +0530517 args: { args: args },
Anand Doshidffec8f2014-07-01 17:45:15 +0530518 callback: function(r) {
519 if (!r.exc) {
Anand Doshi51f722d2014-07-04 15:44:26 +0530520 me.in_apply_price_list = true;
Anand Doshidffec8f2014-07-01 17:45:15 +0530521 me.frm.set_value("price_list_currency", r.message.parent.price_list_currency);
522 me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate);
Anand Doshi51f722d2014-07-04 15:44:26 +0530523 me.in_apply_price_list = false;
Nabin Hait37b047d2015-02-23 16:01:33 +0530524
525 if(args.item_list.length) {
526 me._set_values_for_item_list(r.message.children);
527 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530528 }
529 }
530 });
531 },
532
Anand Doshi3543f302013-05-24 19:25:01 +0530533 get_item_wise_taxes_html: function() {
534 var item_tax = {};
535 var tax_accounts = [];
536 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530537
Nabin Hait96339342015-01-21 17:22:11 +0530538 $.each(this.frm.doc["taxes"] || [], function(i, tax) {
Anand Doshi3543f302013-05-24 19:25:01 +0530539 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530540 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530541 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530542 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530543 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530544 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530545 var tax_rate = "";
546 if(tax_data[0] != null) {
547 tax_rate = (tax.charge_type === "Actual") ?
548 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
549 (flt(tax_data[0], tax_rate_precision) + "%");
550 }
551 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
552 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530553
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530554 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530555 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530556 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530557 }
Anand Doshi3543f302013-05-24 19:25:01 +0530558 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530559 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530560 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530561
562 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530563 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530564
Anand Doshi179e3772013-09-05 16:53:57 +0530565 var distinct_item_names = [];
566 var distinct_items = [];
Nabin Hait96339342015-01-21 17:22:11 +0530567 $.each(this.frm.doc["items"] || [], function(i, item) {
Anand Doshi179e3772013-09-05 16:53:57 +0530568 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
569 distinct_item_names.push(item.item_code || item.item_name);
570 distinct_items.push(item);
571 }
572 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530573
Anand Doshi179e3772013-09-05 16:53:57 +0530574 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530575 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530576 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530577 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
578 item_name: item.item_name,
579 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530580 return item_tax_record[head[0]] ?
581 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530582 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530583 }).join("\n")
584 });
585 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530586
Anand Doshi53b73422013-05-31 11:50:01 +0530587 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530588 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
589 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530590 <thead><tr>' + headings + '</tr></thead> \
591 <tbody>' + rows + '</tbody> \
592 </table></div>';
593 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530594
Nabin Hait139dc7b2014-02-12 14:53:18 +0530595 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530596 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530597 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530598
Nabin Hait0f231062014-02-20 11:27:47 +0530599 $.each(["company", "customer"], function(i, fieldname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530600 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname)) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530601 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530602 msgprint(__("Please specify") + ": " +
603 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530604 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530605 valid = false;
606 }
Anand Doshi3543f302013-05-24 19:25:01 +0530607 }
608 });
609 return valid;
610 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530611
Anand Doshibf3e54a2013-06-05 14:11:32 +0530612 get_terms: function() {
613 var me = this;
614 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530615 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530616 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530617 args: {
618 doctype: "Terms and Conditions",
619 fieldname: "terms",
620 filters: { name: this.frm.doc.tc_name },
621 },
622 });
623 }
624 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530625
626 taxes_and_charges: function() {
627 var me = this;
628 if(this.frm.doc.taxes_and_charges) {
629 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530630 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
631 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530632 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
633 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530634 "master_name": this.frm.doc.taxes_and_charges,
Nabin Haitdd38a262014-12-26 13:15:21 +0530635 "tax_parentfield": "taxes"
Nabin Hait6b039142014-05-02 15:45:10 +0530636 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530637 callback: function(r) {
638 if(!r.exc) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530639 me.frm.set_value("taxes", r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530640 me.calculate_taxes_and_totals();
641 }
642 }
643 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530644 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530645 },
646
647 show_item_wise_taxes: function() {
648 if(this.frm.fields_dict.other_charges_calculation) {
Anand Doshidd942bb2015-02-04 18:52:35 +0530649 var html = this.get_item_wise_taxes_html();
650 if (html) {
651 this.frm.toggle_display("other_charges_calculation", true);
652 $(this.frm.fields_dict.other_charges_calculation.wrapper).html(html);
653 } else {
654 this.frm.toggle_display("other_charges_calculation", false);
655 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530656 }
657 },
Anand Doshi13945092014-09-21 19:45:49 +0530658
659 is_recurring: function() {
660 // set default values for recurring documents
661 if(this.frm.doc.is_recurring) {
662 var owner_email = this.frm.doc.owner=="Administrator"
663 ? frappe.user_info("Administrator").email
664 : this.frm.doc.owner;
665
666 this.frm.doc.notification_email_address = $.map([cstr(owner_email),
667 cstr(this.frm.doc.contact_email)], function(v) { return v || null; }).join(", ");
668 this.frm.doc.repeat_on_day_of_month = frappe.datetime.str_to_obj(this.frm.doc.posting_date).getDate();
669 }
670
671 refresh_many(["notification_email_address", "repeat_on_day_of_month"]);
672 },
673
674 from_date: function() {
675 // set to_date
676 if(this.frm.doc.from_date) {
677 var recurring_type_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6,
678 'Yearly': 12};
679
680 var months = recurring_type_map[this.frm.doc.recurring_type];
681 if(months) {
682 var to_date = frappe.datetime.add_months(this.frm.doc.from_date,
683 months);
684 this.frm.doc.to_date = frappe.datetime.add_days(to_date, -1);
685 refresh_field('to_date');
686 }
687 }
688 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530689});
Nabin Haitce245122015-02-22 20:14:49 +0530690
Nabin Hait93b3a372015-02-24 17:08:34 +0530691frappe.ui.form.on(cur_frm.doctype + " Item", "rate", function(frm, cdt, cdn) {
Nabin Haitce245122015-02-22 20:14:49 +0530692 var item = frappe.get_doc(cdt, cdn);
693 frappe.model.round_floats_in(item, ["rate", "price_list_rate"]);
694
695 if(item.price_list_rate) {
696 item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0, precision("discount_percentage", item));
697 } else {
698 item.discount_percentage = 0.0;
699 }
700
701 cur_frm.cscript.calculate_taxes_and_totals();
702})
Nabin Hait613d0812015-02-23 11:58:15 +0530703
704frappe.ui.form.on(cur_frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
705 cur_frm.cscript.calculate_taxes_and_totals();
706})
707
708frappe.ui.form.on(cur_frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
709 cur_frm.cscript.calculate_taxes_and_totals();
710})
711
712frappe.ui.form.on(cur_frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
713 cur_frm.cscript.calculate_taxes_and_totals();
714})
715
716frappe.ui.form.on(cur_frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
Nabin Hait903c42a2015-02-24 15:24:49 +0530717 cur_frm.cscript.set_dynamic_labels();
Nabin Hait613d0812015-02-23 11:58:15 +0530718 cur_frm.cscript.calculate_taxes_and_totals();
719})
Nabin Hait37b047d2015-02-23 16:01:33 +0530720
721frappe.ui.form.on(cur_frm.doctype, "apply_discount_on", function(frm) {
722 cur_frm.cscript.calculate_taxes_and_totals();
723})
724
725frappe.ui.form.on(cur_frm.doctype, "discount_amount", function(frm) {
Nabin Hait903c42a2015-02-24 15:24:49 +0530726 cur_frm.cscript.set_dynamic_labels();
Nabin Hait37b047d2015-02-23 16:01:33 +0530727 cur_frm.cscript.calculate_taxes_and_totals();
728})