blob: 106b3cca2713d55e8733483da2d92f079f5ed00e [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);
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 Haitdd82bf22015-05-11 16:49:17 +0530236
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) {
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +0530248 frappe.call({
249 method: "erpnext.setup.utils.get_exchange_rate",
250 args: {
251 from_currency: from_currency,
252 to_currency: to_currency
253 },
254 callback: function(r) {
255 callback(flt(r.message));
256 }
Anand Doshi61a2f682013-06-21 17:55:31 +0530257 });
258 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530259
Anand Doshi3543f302013-05-24 19:25:01 +0530260 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530261 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530262 this.set_dynamic_labels();
Nabin Haitec361ea2015-05-11 18:14:45 +0530263
264 var company_currency = this.get_company_currency();
265 // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
266 if(this.frm.doc.price_list_currency !== company_currency && !this.frm.doc.ignore_pricing_rule) {
267 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
268 function(exchange_rate) {
269 me.frm.set_value("plc_conversion_rate", exchange_rate);
270 });
271 } else {
272 this.plc_conversion_rate();
273 }
Anand Doshi3543f302013-05-24 19:25:01 +0530274 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530275
Anand Doshi3543f302013-05-24 19:25:01 +0530276 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530277 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
278 this.frm.set_value("plc_conversion_rate", 1.0);
Nabin Haitec361ea2015-05-11 18:14:45 +0530279 } else if(this.frm.doc.price_list_currency === this.frm.doc.currency && this.frm.doc.plc_conversion_rate && cint(this.frm.doc.plc_conversion_rate) != 1 &&
280 cint(this.frm.doc.plc_conversion_rate) != cint(this.frm.doc.conversion_rate)) {
281 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530282 }
Nabin Haitec361ea2015-05-11 18:14:45 +0530283
284 if(!this.in_apply_price_list) {
285 this.apply_price_list();
Anand Doshi61a2f682013-06-21 17:55:31 +0530286 }
Anand Doshi3543f302013-05-24 19:25:01 +0530287 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530288
Anand Doshi3543f302013-05-24 19:25:01 +0530289 qty: function(doc, cdt, cdn) {
Nabin Hait444f9562014-06-20 15:59:49 +0530290 this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
Anand Doshi3543f302013-05-24 19:25:01 +0530291 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530292
Anand Doshi61a2f682013-06-21 17:55:31 +0530293 set_dynamic_labels: function() {
294 // What TODO? should we make price list system non-mandatory?
295 this.frm.toggle_reqd("plc_conversion_rate",
296 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530297
Anand Doshi61a2f682013-06-21 17:55:31 +0530298 var company_currency = this.get_company_currency();
299 this.change_form_labels(company_currency);
300 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530301 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530302 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530303
Nabin Hait613d0812015-02-23 11:58:15 +0530304 change_form_labels: function(company_currency) {
305 var me = this;
306 var field_label_map = {};
307
308 var setup_field_label_map = function(fields_list, currency) {
309 $.each(fields_list, function(i, fname) {
310 var docfield = frappe.meta.docfield_map[me.frm.doc.doctype][fname];
311 if(docfield) {
312 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
313 field_label_map[fname] = label.trim() + " (" + currency + ")";
314 }
315 });
316 };
317 setup_field_label_map(["base_total", "base_net_total", "base_total_taxes_and_charges",
318 "base_discount_amount", "base_grand_total", "base_rounded_total", "base_in_words",
319 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted", "total_amount_to_pay",
Nabin Hait37b047d2015-02-23 16:01:33 +0530320 "outstanding_amount", "total_advance", "paid_amount", "write_off_amount"], company_currency);
Nabin Hait613d0812015-02-23 11:58:15 +0530321
322 setup_field_label_map(["total", "net_total", "total_taxes_and_charges", "discount_amount",
323 "grand_total", "taxes_and_charges_added", "taxes_and_charges_deducted",
324 "rounded_total", "in_words"], this.frm.doc.currency);
325
326 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
327 + " = [?] " + company_currency)
328
329 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
330 cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency
331 + " = [?] " + company_currency)
332 }
333
334 // toggle fields
335 this.frm.toggle_display(["conversion_rate", "base_total", "base_net_total", "base_total_taxes_and_charges",
336 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted",
337 "base_grand_total", "base_rounded_total", "base_in_words", "base_discount_amount"],
338 this.frm.doc.currency != company_currency);
339
340 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
341 this.frm.doc.price_list_currency != company_currency);
342
343 // set labels
344 $.each(field_label_map, function(fname, label) {
345 me.frm.fields_dict[fname].set_label(label);
346 });
Nabin Hait37b047d2015-02-23 16:01:33 +0530347
Nabin Hait903c42a2015-02-24 15:24:49 +0530348 var show =cint(cur_frm.doc.discount_amount) ||
349 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
350
351 if(frappe.meta.get_docfield(cur_frm.doctype, "net_total"))
352 cur_frm.toggle_display("net_total", show);
353
354 if(frappe.meta.get_docfield(cur_frm.doctype, "base_net_total"))
355 cur_frm.toggle_display("base_net_total", (show && (me.frm.doc.currency != company_currency)));
356
Nabin Hait613d0812015-02-23 11:58:15 +0530357 },
358
359 change_grid_labels: function(company_currency) {
360 var me = this;
361 var field_label_map = {};
362
363 var setup_field_label_map = function(fields_list, currency, parentfield) {
364 var grid_doctype = me.frm.fields_dict[parentfield].grid.doctype;
365 $.each(fields_list, function(i, fname) {
366 var docfield = frappe.meta.docfield_map[grid_doctype][fname];
367 if(docfield) {
368 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
369 field_label_map[grid_doctype + "-" + fname] =
370 label.trim() + " (" + currency + ")";
371 }
372 });
373 }
374
375 setup_field_label_map(["base_rate", "base_net_rate", "base_price_list_rate", "base_amount", "base_net_amount"],
376 company_currency, "items");
377
378 setup_field_label_map(["rate", "net_rate", "price_list_rate", "amount", "net_amount"],
379 this.frm.doc.currency, "items");
380
381 if(this.frm.fields_dict["taxes"]) {
382 setup_field_label_map(["tax_amount", "total", "tax_amount_after_discount"], this.frm.doc.currency, "taxes");
383
384 setup_field_label_map(["base_tax_amount", "base_total", "base_tax_amount_after_discount"], company_currency, "taxes");
385 }
386
387 if(this.frm.fields_dict["advances"]) {
388 setup_field_label_map(["advance_amount", "allocated_amount"], company_currency, "advances");
389 }
390
391 // toggle columns
392 var item_grid = this.frm.fields_dict["items"].grid;
393 $.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) {
394 if(frappe.meta.get_docfield(item_grid.doctype, fname))
395 item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency);
396 });
397
398 var show = (cint(cur_frm.doc.discount_amount)) ||
399 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
400
401 $.each(["net_rate", "net_amount"], function(i, fname) {
402 if(frappe.meta.get_docfield(item_grid.doctype, fname))
403 item_grid.set_column_disp(fname, show);
404 });
405
406 $.each(["base_net_rate", "base_net_amount"], function(i, fname) {
407 if(frappe.meta.get_docfield(item_grid.doctype, fname))
408 item_grid.set_column_disp(fname, (show && (me.frm.doc.currency != company_currency)));
409 });
410
411 // set labels
412 var $wrapper = $(this.frm.wrapper);
413 $.each(field_label_map, function(fname, label) {
414 fname = fname.split("-");
415 var df = frappe.meta.get_docfield(fname[0], fname[1], me.frm.doc.name);
416 if(df) df.label = label;
417 });
418 },
419
Anand Doshi923d41d2013-05-28 17:23:36 +0530420 recalculate: function() {
421 this.calculate_taxes_and_totals();
422 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530423
Anand Doshi923d41d2013-05-28 17:23:36 +0530424 recalculate_values: function() {
425 this.calculate_taxes_and_totals();
426 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530427
Anand Doshid0b00722013-05-28 18:54:48 +0530428 calculate_charges: function() {
429 this.calculate_taxes_and_totals();
430 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530431
Nabin Hait444f9562014-06-20 15:59:49 +0530432 ignore_pricing_rule: function() {
433 this.apply_pricing_rule();
434 },
435
436 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530437 var me = this;
Anand Doshidffec8f2014-07-01 17:45:15 +0530438 return this.frm.call({
439 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
440 args: { args: this._get_args(item) },
441 callback: function(r) {
Anand Doshif214bfc2014-07-16 17:52:08 +0530442 if (!r.exc && r.message) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530443 me._set_values_for_item_list(r.message);
444 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
445 }
446 }
447 });
448 },
449
450 _get_args: function(item) {
451 var me = this;
452 return {
453 "item_list": this._get_item_list(item),
Nabin Hait444f9562014-06-20 15:59:49 +0530454 "customer": me.frm.doc.customer,
455 "customer_group": me.frm.doc.customer_group,
456 "territory": me.frm.doc.territory,
457 "supplier": me.frm.doc.supplier,
458 "supplier_type": me.frm.doc.supplier_type,
459 "currency": me.frm.doc.currency,
460 "conversion_rate": me.frm.doc.conversion_rate,
461 "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
Nabin Haitdd82bf22015-05-11 16:49:17 +0530462 "price_list_currency": me.frm.doc.price_list_currency,
Nabin Hait444f9562014-06-20 15:59:49 +0530463 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
464 "company": me.frm.doc.company,
465 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
466 "campaign": me.frm.doc.campaign,
467 "sales_partner": me.frm.doc.sales_partner,
468 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
469 "parenttype": me.frm.doc.doctype,
470 "parent": me.frm.doc.name
471 };
Nabin Hait444f9562014-06-20 15:59:49 +0530472 },
473
474 _get_item_list: function(item) {
475 var item_list = [];
476 var append_item = function(d) {
Anand Doshi39edcbc2014-07-28 16:00:19 +0530477 if (d.item_code) {
478 item_list.push({
479 "doctype": d.doctype,
480 "name": d.name,
481 "item_code": d.item_code,
482 "item_group": d.item_group,
483 "brand": d.brand,
484 "qty": d.qty
485 });
486 }
Nabin Hait444f9562014-06-20 15:59:49 +0530487 };
Nabin Haita3dd72a2014-05-28 12:49:20 +0530488
Nabin Hait444f9562014-06-20 15:59:49 +0530489 if (item) {
490 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +0530491 } else {
Nabin Hait96339342015-01-21 17:22:11 +0530492 $.each(this.frm.doc["items"] || [], function(i, d) {
Nabin Hait444f9562014-06-20 15:59:49 +0530493 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530494 });
495 }
Nabin Hait444f9562014-06-20 15:59:49 +0530496 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +0530497 },
498
Anand Doshidffec8f2014-07-01 17:45:15 +0530499 _set_values_for_item_list: function(children) {
Nabin Hait49a27292014-12-17 11:21:32 +0530500 var me = this;
Nabin Hait7d8fa802014-12-30 15:35:45 +0530501 var price_list_rate_changed = false;
Anand Doshidea3fb02015-02-05 15:05:58 +0530502 for(var i=0, l=children.length; i<l; i++) {
503 var d = children[i];
Nabin Hait49a27292014-12-17 11:21:32 +0530504 var existing_pricing_rule = frappe.model.get_value(d.doctype, d.name, "pricing_rule");
Anand Doshidea3fb02015-02-05 15:05:58 +0530505
506 for(var k in d) {
507 var v = d[k];
Anand Doshidffec8f2014-07-01 17:45:15 +0530508 if (["doctype", "name"].indexOf(k)===-1) {
Nabin Hait7d8fa802014-12-30 15:35:45 +0530509 if(k=="price_list_rate") {
510 if(flt(v) != flt(d.price_list_rate)) price_list_rate_changed = true;
511 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530512 frappe.model.set_value(d.doctype, d.name, k, v);
513 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530514 }
515
Nabin Hait49a27292014-12-17 11:21:32 +0530516 // if pricing rule set as blank from an existing value, apply price_list
Nabin Haitcfe3c542014-12-22 10:42:48 +0530517 if(!me.frm.doc.ignore_pricing_rule && existing_pricing_rule && !d.pricing_rule) {
Nabin Hait49a27292014-12-17 11:21:32 +0530518 me.apply_price_list(frappe.get_doc(d.doctype, d.name));
519 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530520 }
Nabin Hait7d8fa802014-12-30 15:35:45 +0530521
Anand Doshidea3fb02015-02-05 15:05:58 +0530522 if(!price_list_rate_changed) me.calculate_taxes_and_totals();
Anand Doshidffec8f2014-07-01 17:45:15 +0530523 },
524
Nabin Hait49a27292014-12-17 11:21:32 +0530525 apply_price_list: function(item) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530526 var me = this;
Rushabh Mehta052bd622015-01-30 16:57:24 +0530527 var args = this._get_args(item);
Nabin Hait37b047d2015-02-23 16:01:33 +0530528
Anand Doshidffec8f2014-07-01 17:45:15 +0530529 return this.frm.call({
530 method: "erpnext.stock.get_item_details.apply_price_list",
Rushabh Mehta052bd622015-01-30 16:57:24 +0530531 args: { args: args },
Anand Doshidffec8f2014-07-01 17:45:15 +0530532 callback: function(r) {
533 if (!r.exc) {
Anand Doshi51f722d2014-07-04 15:44:26 +0530534 me.in_apply_price_list = true;
Anand Doshidffec8f2014-07-01 17:45:15 +0530535 me.frm.set_value("price_list_currency", r.message.parent.price_list_currency);
536 me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate);
Anand Doshi51f722d2014-07-04 15:44:26 +0530537 me.in_apply_price_list = false;
Nabin Hait37b047d2015-02-23 16:01:33 +0530538
539 if(args.item_list.length) {
540 me._set_values_for_item_list(r.message.children);
541 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530542 }
543 }
544 });
545 },
546
Anand Doshi3543f302013-05-24 19:25:01 +0530547 get_item_wise_taxes_html: function() {
548 var item_tax = {};
549 var tax_accounts = [];
550 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530551
Nabin Hait96339342015-01-21 17:22:11 +0530552 $.each(this.frm.doc["taxes"] || [], function(i, tax) {
Anand Doshi3543f302013-05-24 19:25:01 +0530553 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530554 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530555 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530556 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530557 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530558 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530559 var tax_rate = "";
560 if(tax_data[0] != null) {
561 tax_rate = (tax.charge_type === "Actual") ?
562 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
563 (flt(tax_data[0], tax_rate_precision) + "%");
564 }
565 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
566 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530567
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530568 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530569 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530570 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530571 }
Anand Doshi3543f302013-05-24 19:25:01 +0530572 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530573 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530574 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530575
576 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530577 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530578
Anand Doshi179e3772013-09-05 16:53:57 +0530579 var distinct_item_names = [];
580 var distinct_items = [];
Nabin Hait96339342015-01-21 17:22:11 +0530581 $.each(this.frm.doc["items"] || [], function(i, item) {
Anand Doshi179e3772013-09-05 16:53:57 +0530582 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
583 distinct_item_names.push(item.item_code || item.item_name);
584 distinct_items.push(item);
585 }
586 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530587
Anand Doshi179e3772013-09-05 16:53:57 +0530588 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530589 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530590 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530591 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
592 item_name: item.item_name,
593 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530594 return item_tax_record[head[0]] ?
595 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530596 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530597 }).join("\n")
598 });
599 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530600
Anand Doshi53b73422013-05-31 11:50:01 +0530601 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530602 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
603 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530604 <thead><tr>' + headings + '</tr></thead> \
605 <tbody>' + rows + '</tbody> \
606 </table></div>';
607 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530608
Nabin Hait139dc7b2014-02-12 14:53:18 +0530609 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530610 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530611 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530612
Nabin Hait0f231062014-02-20 11:27:47 +0530613 $.each(["company", "customer"], function(i, fieldname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530614 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname)) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530615 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530616 msgprint(__("Please specify") + ": " +
617 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530618 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530619 valid = false;
620 }
Anand Doshi3543f302013-05-24 19:25:01 +0530621 }
622 });
623 return valid;
624 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530625
Anand Doshibf3e54a2013-06-05 14:11:32 +0530626 get_terms: function() {
627 var me = this;
628 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530629 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530630 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530631 args: {
632 doctype: "Terms and Conditions",
633 fieldname: "terms",
634 filters: { name: this.frm.doc.tc_name },
635 },
636 });
637 }
638 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530639
640 taxes_and_charges: function() {
641 var me = this;
642 if(this.frm.doc.taxes_and_charges) {
643 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530644 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
645 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530646 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
647 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530648 "master_name": this.frm.doc.taxes_and_charges,
Nabin Haitdd38a262014-12-26 13:15:21 +0530649 "tax_parentfield": "taxes"
Nabin Hait6b039142014-05-02 15:45:10 +0530650 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530651 callback: function(r) {
652 if(!r.exc) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530653 me.frm.set_value("taxes", r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530654 me.calculate_taxes_and_totals();
655 }
656 }
657 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530658 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530659 },
660
661 show_item_wise_taxes: function() {
662 if(this.frm.fields_dict.other_charges_calculation) {
Anand Doshidd942bb2015-02-04 18:52:35 +0530663 var html = this.get_item_wise_taxes_html();
664 if (html) {
665 this.frm.toggle_display("other_charges_calculation", true);
666 $(this.frm.fields_dict.other_charges_calculation.wrapper).html(html);
667 } else {
668 this.frm.toggle_display("other_charges_calculation", false);
669 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530670 }
671 },
Anand Doshi13945092014-09-21 19:45:49 +0530672
673 is_recurring: function() {
674 // set default values for recurring documents
675 if(this.frm.doc.is_recurring) {
676 var owner_email = this.frm.doc.owner=="Administrator"
677 ? frappe.user_info("Administrator").email
678 : this.frm.doc.owner;
679
680 this.frm.doc.notification_email_address = $.map([cstr(owner_email),
681 cstr(this.frm.doc.contact_email)], function(v) { return v || null; }).join(", ");
682 this.frm.doc.repeat_on_day_of_month = frappe.datetime.str_to_obj(this.frm.doc.posting_date).getDate();
683 }
684
685 refresh_many(["notification_email_address", "repeat_on_day_of_month"]);
686 },
687
688 from_date: function() {
689 // set to_date
690 if(this.frm.doc.from_date) {
691 var recurring_type_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6,
692 'Yearly': 12};
693
694 var months = recurring_type_map[this.frm.doc.recurring_type];
695 if(months) {
696 var to_date = frappe.datetime.add_months(this.frm.doc.from_date,
697 months);
698 this.frm.doc.to_date = frappe.datetime.add_days(to_date, -1);
699 refresh_field('to_date');
700 }
701 }
702 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530703});
Nabin Haitce245122015-02-22 20:14:49 +0530704
Nabin Hait93b3a372015-02-24 17:08:34 +0530705frappe.ui.form.on(cur_frm.doctype + " Item", "rate", function(frm, cdt, cdn) {
Nabin Haitce245122015-02-22 20:14:49 +0530706 var item = frappe.get_doc(cdt, cdn);
707 frappe.model.round_floats_in(item, ["rate", "price_list_rate"]);
708
709 if(item.price_list_rate) {
710 item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0, precision("discount_percentage", item));
711 } else {
712 item.discount_percentage = 0.0;
713 }
714
715 cur_frm.cscript.calculate_taxes_and_totals();
716})
Nabin Hait613d0812015-02-23 11:58:15 +0530717
718frappe.ui.form.on(cur_frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
719 cur_frm.cscript.calculate_taxes_and_totals();
720})
721
722frappe.ui.form.on(cur_frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
723 cur_frm.cscript.calculate_taxes_and_totals();
724})
725
726frappe.ui.form.on(cur_frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
727 cur_frm.cscript.calculate_taxes_and_totals();
728})
729
730frappe.ui.form.on(cur_frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
Nabin Hait903c42a2015-02-24 15:24:49 +0530731 cur_frm.cscript.set_dynamic_labels();
Nabin Hait613d0812015-02-23 11:58:15 +0530732 cur_frm.cscript.calculate_taxes_and_totals();
733})
Nabin Hait37b047d2015-02-23 16:01:33 +0530734
735frappe.ui.form.on(cur_frm.doctype, "apply_discount_on", function(frm) {
736 cur_frm.cscript.calculate_taxes_and_totals();
737})
738
739frappe.ui.form.on(cur_frm.doctype, "discount_amount", function(frm) {
Nabin Hait903c42a2015-02-24 15:24:49 +0530740 cur_frm.cscript.set_dynamic_labels();
Nabin Hait37b047d2015-02-23 16:01:33 +0530741 cur_frm.cscript.calculate_taxes_and_totals();
742})