blob: b45e1208cef70c3668714bcd7141df22022f74a4 [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();
Nabin Hait69cdf592015-05-02 18:46:10 +0530217 // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
218 if(this.frm.doc.currency !== company_currency && !this.frm.doc.ignore_pricing_rule) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530219 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530220 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530221 me.frm.set_value("conversion_rate", exchange_rate);
222 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530223 });
224 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530225 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530226 }
227 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530228
Anand Doshi61a2f682013-06-21 17:55:31 +0530229 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530230 if(this.frm.doc.currency === this.get_company_currency()) {
231 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530232 }
233 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530234 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
235 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
236 }
Nabin Haita3dd72a2014-05-28 12:49:20 +0530237 if(flt(this.frm.doc.conversion_rate)>0.0) {
Nabin Hait444f9562014-06-20 15:59:49 +0530238 if(this.frm.doc.ignore_pricing_rule) {
239 this.calculate_taxes_and_totals();
Anand Doshi51f722d2014-07-04 15:44:26 +0530240 } else if (!this.in_apply_price_list){
241 this.apply_price_list();
Nabin Hait444f9562014-06-20 15:59:49 +0530242 }
243
Nabin Haita3dd72a2014-05-28 12:49:20 +0530244 }
Anand Doshi3543f302013-05-24 19:25:01 +0530245 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530246
Anand Doshi61a2f682013-06-21 17:55:31 +0530247 get_exchange_rate: function(from_currency, to_currency, callback) {
248 var exchange_name = from_currency + "-" + to_currency;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530249 frappe.model.with_doc("Currency Exchange", exchange_name, function(name) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530250 var exchange_doc = frappe.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530251 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530252 });
253 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530254
Anand Doshi3543f302013-05-24 19:25:01 +0530255 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530256 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530257 this.set_dynamic_labels();
Anand Doshi51f722d2014-07-04 15:44:26 +0530258 this.set_plc_conversion_rate();
Anand Doshi3543f302013-05-24 19:25:01 +0530259 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530260
Anand Doshi3543f302013-05-24 19:25:01 +0530261 plc_conversion_rate: function() {
Anand Doshi51f722d2014-07-04 15:44:26 +0530262 this.set_plc_conversion_rate();
263 if(!this.in_apply_price_list) {
264 this.apply_price_list();
265 }
266 },
267
268 set_plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530269 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
270 this.frm.set_value("plc_conversion_rate", 1.0);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530271 }
272 if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
Anand Doshi61a2f682013-06-21 17:55:31 +0530273 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530274 }
Anand Doshi3543f302013-05-24 19:25:01 +0530275 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530276
Anand Doshi3543f302013-05-24 19:25:01 +0530277 qty: function(doc, cdt, cdn) {
Nabin Hait444f9562014-06-20 15:59:49 +0530278 this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
Anand Doshi3543f302013-05-24 19:25:01 +0530279 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530280
Anand Doshi61a2f682013-06-21 17:55:31 +0530281 set_dynamic_labels: function() {
282 // What TODO? should we make price list system non-mandatory?
283 this.frm.toggle_reqd("plc_conversion_rate",
284 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530285
Anand Doshi61a2f682013-06-21 17:55:31 +0530286 var company_currency = this.get_company_currency();
287 this.change_form_labels(company_currency);
288 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530289 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530290 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530291
Nabin Hait613d0812015-02-23 11:58:15 +0530292 change_form_labels: function(company_currency) {
293 var me = this;
294 var field_label_map = {};
295
296 var setup_field_label_map = function(fields_list, currency) {
297 $.each(fields_list, function(i, fname) {
298 var docfield = frappe.meta.docfield_map[me.frm.doc.doctype][fname];
299 if(docfield) {
300 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
301 field_label_map[fname] = label.trim() + " (" + currency + ")";
302 }
303 });
304 };
305 setup_field_label_map(["base_total", "base_net_total", "base_total_taxes_and_charges",
306 "base_discount_amount", "base_grand_total", "base_rounded_total", "base_in_words",
307 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted", "total_amount_to_pay",
Nabin Hait37b047d2015-02-23 16:01:33 +0530308 "outstanding_amount", "total_advance", "paid_amount", "write_off_amount"], company_currency);
Nabin Hait613d0812015-02-23 11:58:15 +0530309
310 setup_field_label_map(["total", "net_total", "total_taxes_and_charges", "discount_amount",
311 "grand_total", "taxes_and_charges_added", "taxes_and_charges_deducted",
312 "rounded_total", "in_words"], this.frm.doc.currency);
313
314 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
315 + " = [?] " + company_currency)
316
317 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
318 cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency
319 + " = [?] " + company_currency)
320 }
321
322 // toggle fields
323 this.frm.toggle_display(["conversion_rate", "base_total", "base_net_total", "base_total_taxes_and_charges",
324 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted",
325 "base_grand_total", "base_rounded_total", "base_in_words", "base_discount_amount"],
326 this.frm.doc.currency != company_currency);
327
328 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
329 this.frm.doc.price_list_currency != company_currency);
330
331 // set labels
332 $.each(field_label_map, function(fname, label) {
333 me.frm.fields_dict[fname].set_label(label);
334 });
Nabin Hait37b047d2015-02-23 16:01:33 +0530335
Nabin Hait903c42a2015-02-24 15:24:49 +0530336 var show =cint(cur_frm.doc.discount_amount) ||
337 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
338
339 if(frappe.meta.get_docfield(cur_frm.doctype, "net_total"))
340 cur_frm.toggle_display("net_total", show);
341
342 if(frappe.meta.get_docfield(cur_frm.doctype, "base_net_total"))
343 cur_frm.toggle_display("base_net_total", (show && (me.frm.doc.currency != company_currency)));
344
Nabin Hait613d0812015-02-23 11:58:15 +0530345 },
346
347 change_grid_labels: function(company_currency) {
348 var me = this;
349 var field_label_map = {};
350
351 var setup_field_label_map = function(fields_list, currency, parentfield) {
352 var grid_doctype = me.frm.fields_dict[parentfield].grid.doctype;
353 $.each(fields_list, function(i, fname) {
354 var docfield = frappe.meta.docfield_map[grid_doctype][fname];
355 if(docfield) {
356 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
357 field_label_map[grid_doctype + "-" + fname] =
358 label.trim() + " (" + currency + ")";
359 }
360 });
361 }
362
363 setup_field_label_map(["base_rate", "base_net_rate", "base_price_list_rate", "base_amount", "base_net_amount"],
364 company_currency, "items");
365
366 setup_field_label_map(["rate", "net_rate", "price_list_rate", "amount", "net_amount"],
367 this.frm.doc.currency, "items");
368
369 if(this.frm.fields_dict["taxes"]) {
370 setup_field_label_map(["tax_amount", "total", "tax_amount_after_discount"], this.frm.doc.currency, "taxes");
371
372 setup_field_label_map(["base_tax_amount", "base_total", "base_tax_amount_after_discount"], company_currency, "taxes");
373 }
374
375 if(this.frm.fields_dict["advances"]) {
376 setup_field_label_map(["advance_amount", "allocated_amount"], company_currency, "advances");
377 }
378
379 // toggle columns
380 var item_grid = this.frm.fields_dict["items"].grid;
381 $.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) {
382 if(frappe.meta.get_docfield(item_grid.doctype, fname))
383 item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency);
384 });
385
386 var show = (cint(cur_frm.doc.discount_amount)) ||
387 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
388
389 $.each(["net_rate", "net_amount"], function(i, fname) {
390 if(frappe.meta.get_docfield(item_grid.doctype, fname))
391 item_grid.set_column_disp(fname, show);
392 });
393
394 $.each(["base_net_rate", "base_net_amount"], function(i, fname) {
395 if(frappe.meta.get_docfield(item_grid.doctype, fname))
396 item_grid.set_column_disp(fname, (show && (me.frm.doc.currency != company_currency)));
397 });
398
399 // set labels
400 var $wrapper = $(this.frm.wrapper);
401 $.each(field_label_map, function(fname, label) {
402 fname = fname.split("-");
403 var df = frappe.meta.get_docfield(fname[0], fname[1], me.frm.doc.name);
404 if(df) df.label = label;
405 });
406 },
407
Anand Doshi923d41d2013-05-28 17:23:36 +0530408 recalculate: function() {
409 this.calculate_taxes_and_totals();
410 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530411
Anand Doshi923d41d2013-05-28 17:23:36 +0530412 recalculate_values: function() {
413 this.calculate_taxes_and_totals();
414 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530415
Anand Doshid0b00722013-05-28 18:54:48 +0530416 calculate_charges: function() {
417 this.calculate_taxes_and_totals();
418 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530419
Nabin Hait444f9562014-06-20 15:59:49 +0530420 ignore_pricing_rule: function() {
421 this.apply_pricing_rule();
422 },
423
424 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530425 var me = this;
Anand Doshidffec8f2014-07-01 17:45:15 +0530426 return this.frm.call({
427 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
428 args: { args: this._get_args(item) },
429 callback: function(r) {
Anand Doshif214bfc2014-07-16 17:52:08 +0530430 if (!r.exc && r.message) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530431 me._set_values_for_item_list(r.message);
432 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
433 }
434 }
435 });
436 },
437
438 _get_args: function(item) {
439 var me = this;
440 return {
441 "item_list": this._get_item_list(item),
Nabin Hait444f9562014-06-20 15:59:49 +0530442 "customer": me.frm.doc.customer,
443 "customer_group": me.frm.doc.customer_group,
444 "territory": me.frm.doc.territory,
445 "supplier": me.frm.doc.supplier,
446 "supplier_type": me.frm.doc.supplier_type,
447 "currency": me.frm.doc.currency,
448 "conversion_rate": me.frm.doc.conversion_rate,
449 "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
450 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
451 "company": me.frm.doc.company,
452 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
453 "campaign": me.frm.doc.campaign,
454 "sales_partner": me.frm.doc.sales_partner,
455 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
456 "parenttype": me.frm.doc.doctype,
457 "parent": me.frm.doc.name
458 };
Nabin Hait444f9562014-06-20 15:59:49 +0530459 },
460
461 _get_item_list: function(item) {
462 var item_list = [];
463 var append_item = function(d) {
Anand Doshi39edcbc2014-07-28 16:00:19 +0530464 if (d.item_code) {
465 item_list.push({
466 "doctype": d.doctype,
467 "name": d.name,
468 "item_code": d.item_code,
469 "item_group": d.item_group,
470 "brand": d.brand,
471 "qty": d.qty
472 });
473 }
Nabin Hait444f9562014-06-20 15:59:49 +0530474 };
Nabin Haita3dd72a2014-05-28 12:49:20 +0530475
Nabin Hait444f9562014-06-20 15:59:49 +0530476 if (item) {
477 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +0530478 } else {
Nabin Hait96339342015-01-21 17:22:11 +0530479 $.each(this.frm.doc["items"] || [], function(i, d) {
Nabin Hait444f9562014-06-20 15:59:49 +0530480 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530481 });
482 }
Nabin Hait444f9562014-06-20 15:59:49 +0530483 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +0530484 },
485
Anand Doshidffec8f2014-07-01 17:45:15 +0530486 _set_values_for_item_list: function(children) {
Nabin Hait49a27292014-12-17 11:21:32 +0530487 var me = this;
Nabin Hait7d8fa802014-12-30 15:35:45 +0530488 var price_list_rate_changed = false;
Anand Doshidea3fb02015-02-05 15:05:58 +0530489 for(var i=0, l=children.length; i<l; i++) {
490 var d = children[i];
Nabin Hait49a27292014-12-17 11:21:32 +0530491 var existing_pricing_rule = frappe.model.get_value(d.doctype, d.name, "pricing_rule");
Anand Doshidea3fb02015-02-05 15:05:58 +0530492
493 for(var k in d) {
494 var v = d[k];
Anand Doshidffec8f2014-07-01 17:45:15 +0530495 if (["doctype", "name"].indexOf(k)===-1) {
Nabin Hait7d8fa802014-12-30 15:35:45 +0530496 if(k=="price_list_rate") {
497 if(flt(v) != flt(d.price_list_rate)) price_list_rate_changed = true;
498 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530499 frappe.model.set_value(d.doctype, d.name, k, v);
500 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530501 }
502
Nabin Hait49a27292014-12-17 11:21:32 +0530503 // if pricing rule set as blank from an existing value, apply price_list
Nabin Haitcfe3c542014-12-22 10:42:48 +0530504 if(!me.frm.doc.ignore_pricing_rule && existing_pricing_rule && !d.pricing_rule) {
Nabin Hait49a27292014-12-17 11:21:32 +0530505 me.apply_price_list(frappe.get_doc(d.doctype, d.name));
506 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530507 }
Nabin Hait7d8fa802014-12-30 15:35:45 +0530508
Anand Doshidea3fb02015-02-05 15:05:58 +0530509 if(!price_list_rate_changed) me.calculate_taxes_and_totals();
Anand Doshidffec8f2014-07-01 17:45:15 +0530510 },
511
Nabin Hait49a27292014-12-17 11:21:32 +0530512 apply_price_list: function(item) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530513 var me = this;
Rushabh Mehta052bd622015-01-30 16:57:24 +0530514 var args = this._get_args(item);
Nabin Hait37b047d2015-02-23 16:01:33 +0530515
Anand Doshidffec8f2014-07-01 17:45:15 +0530516 return this.frm.call({
517 method: "erpnext.stock.get_item_details.apply_price_list",
Rushabh Mehta052bd622015-01-30 16:57:24 +0530518 args: { args: args },
Anand Doshidffec8f2014-07-01 17:45:15 +0530519 callback: function(r) {
520 if (!r.exc) {
Anand Doshi51f722d2014-07-04 15:44:26 +0530521 me.in_apply_price_list = true;
Anand Doshidffec8f2014-07-01 17:45:15 +0530522 me.frm.set_value("price_list_currency", r.message.parent.price_list_currency);
523 me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate);
Anand Doshi51f722d2014-07-04 15:44:26 +0530524 me.in_apply_price_list = false;
Nabin Hait37b047d2015-02-23 16:01:33 +0530525
526 if(args.item_list.length) {
527 me._set_values_for_item_list(r.message.children);
528 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530529 }
530 }
531 });
532 },
533
Anand Doshi3543f302013-05-24 19:25:01 +0530534 get_item_wise_taxes_html: function() {
535 var item_tax = {};
536 var tax_accounts = [];
537 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530538
Nabin Hait96339342015-01-21 17:22:11 +0530539 $.each(this.frm.doc["taxes"] || [], function(i, tax) {
Anand Doshi3543f302013-05-24 19:25:01 +0530540 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530541 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530542 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530543 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530544 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530545 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530546 var tax_rate = "";
547 if(tax_data[0] != null) {
548 tax_rate = (tax.charge_type === "Actual") ?
549 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
550 (flt(tax_data[0], tax_rate_precision) + "%");
551 }
552 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
553 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530554
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530555 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530556 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530557 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530558 }
Anand Doshi3543f302013-05-24 19:25:01 +0530559 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530560 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530561 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530562
563 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530564 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530565
Anand Doshi179e3772013-09-05 16:53:57 +0530566 var distinct_item_names = [];
567 var distinct_items = [];
Nabin Hait96339342015-01-21 17:22:11 +0530568 $.each(this.frm.doc["items"] || [], function(i, item) {
Anand Doshi179e3772013-09-05 16:53:57 +0530569 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
570 distinct_item_names.push(item.item_code || item.item_name);
571 distinct_items.push(item);
572 }
573 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530574
Anand Doshi179e3772013-09-05 16:53:57 +0530575 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530576 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530577 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530578 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
579 item_name: item.item_name,
580 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530581 return item_tax_record[head[0]] ?
582 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530583 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530584 }).join("\n")
585 });
586 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530587
Anand Doshi53b73422013-05-31 11:50:01 +0530588 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530589 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
590 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530591 <thead><tr>' + headings + '</tr></thead> \
592 <tbody>' + rows + '</tbody> \
593 </table></div>';
594 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530595
Nabin Hait139dc7b2014-02-12 14:53:18 +0530596 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530597 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530598 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530599
Nabin Hait0f231062014-02-20 11:27:47 +0530600 $.each(["company", "customer"], function(i, fieldname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530601 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname)) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530602 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530603 msgprint(__("Please specify") + ": " +
604 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530605 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530606 valid = false;
607 }
Anand Doshi3543f302013-05-24 19:25:01 +0530608 }
609 });
610 return valid;
611 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530612
Anand Doshibf3e54a2013-06-05 14:11:32 +0530613 get_terms: function() {
614 var me = this;
615 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530616 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530617 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530618 args: {
619 doctype: "Terms and Conditions",
620 fieldname: "terms",
621 filters: { name: this.frm.doc.tc_name },
622 },
623 });
624 }
625 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530626
627 taxes_and_charges: function() {
628 var me = this;
629 if(this.frm.doc.taxes_and_charges) {
630 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530631 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
632 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530633 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
634 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530635 "master_name": this.frm.doc.taxes_and_charges,
Nabin Haitdd38a262014-12-26 13:15:21 +0530636 "tax_parentfield": "taxes"
Nabin Hait6b039142014-05-02 15:45:10 +0530637 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530638 callback: function(r) {
639 if(!r.exc) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530640 me.frm.set_value("taxes", r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530641 me.calculate_taxes_and_totals();
642 }
643 }
644 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530645 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530646 },
647
648 show_item_wise_taxes: function() {
649 if(this.frm.fields_dict.other_charges_calculation) {
Anand Doshidd942bb2015-02-04 18:52:35 +0530650 var html = this.get_item_wise_taxes_html();
651 if (html) {
652 this.frm.toggle_display("other_charges_calculation", true);
653 $(this.frm.fields_dict.other_charges_calculation.wrapper).html(html);
654 } else {
655 this.frm.toggle_display("other_charges_calculation", false);
656 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530657 }
658 },
Anand Doshi13945092014-09-21 19:45:49 +0530659
660 is_recurring: function() {
661 // set default values for recurring documents
662 if(this.frm.doc.is_recurring) {
663 var owner_email = this.frm.doc.owner=="Administrator"
664 ? frappe.user_info("Administrator").email
665 : this.frm.doc.owner;
666
667 this.frm.doc.notification_email_address = $.map([cstr(owner_email),
668 cstr(this.frm.doc.contact_email)], function(v) { return v || null; }).join(", ");
669 this.frm.doc.repeat_on_day_of_month = frappe.datetime.str_to_obj(this.frm.doc.posting_date).getDate();
670 }
671
672 refresh_many(["notification_email_address", "repeat_on_day_of_month"]);
673 },
674
675 from_date: function() {
676 // set to_date
677 if(this.frm.doc.from_date) {
678 var recurring_type_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6,
679 'Yearly': 12};
680
681 var months = recurring_type_map[this.frm.doc.recurring_type];
682 if(months) {
683 var to_date = frappe.datetime.add_months(this.frm.doc.from_date,
684 months);
685 this.frm.doc.to_date = frappe.datetime.add_days(to_date, -1);
686 refresh_field('to_date');
687 }
688 }
689 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530690});
Nabin Haitce245122015-02-22 20:14:49 +0530691
Nabin Hait93b3a372015-02-24 17:08:34 +0530692frappe.ui.form.on(cur_frm.doctype + " Item", "rate", function(frm, cdt, cdn) {
Nabin Haitce245122015-02-22 20:14:49 +0530693 var item = frappe.get_doc(cdt, cdn);
694 frappe.model.round_floats_in(item, ["rate", "price_list_rate"]);
695
696 if(item.price_list_rate) {
697 item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0, precision("discount_percentage", item));
698 } else {
699 item.discount_percentage = 0.0;
700 }
701
702 cur_frm.cscript.calculate_taxes_and_totals();
703})
Nabin Hait613d0812015-02-23 11:58:15 +0530704
705frappe.ui.form.on(cur_frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
706 cur_frm.cscript.calculate_taxes_and_totals();
707})
708
709frappe.ui.form.on(cur_frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
710 cur_frm.cscript.calculate_taxes_and_totals();
711})
712
713frappe.ui.form.on(cur_frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
714 cur_frm.cscript.calculate_taxes_and_totals();
715})
716
717frappe.ui.form.on(cur_frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
Nabin Hait903c42a2015-02-24 15:24:49 +0530718 cur_frm.cscript.set_dynamic_labels();
Nabin Hait613d0812015-02-23 11:58:15 +0530719 cur_frm.cscript.calculate_taxes_and_totals();
720})
Nabin Hait37b047d2015-02-23 16:01:33 +0530721
722frappe.ui.form.on(cur_frm.doctype, "apply_discount_on", function(frm) {
723 cur_frm.cscript.calculate_taxes_and_totals();
724})
725
726frappe.ui.form.on(cur_frm.doctype, "discount_amount", function(frm) {
Nabin Hait903c42a2015-02-24 15:24:49 +0530727 cur_frm.cscript.set_dynamic_labels();
Nabin Hait37b047d2015-02-23 16:01:33 +0530728 cur_frm.cscript.calculate_taxes_and_totals();
729})