blob: 0e444cc197a1edf737aa4d0213ee23db69428419 [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 Haitffc7f3f2015-05-12 15:07:02 +053044 if(this.frm.doc.__islocal && !(this.frm.doc.taxes || []).length
45 && !(this.frm.doc.__onload ? this.frm.doc.__onload.load_after_mapping : false)) {
46 this.apply_default_taxes();
47 }
48
Nabin Haitdd38a262014-12-26 13:15:21 +053049 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 +053050 this.calculate_taxes_and_totals();
Anand Doshi5e4e5d72013-08-06 17:23:44 +053051 }
Nabin Haitdd38a262014-12-26 13:15:21 +053052 if(frappe.meta.get_docfield(this.frm.doc.doctype + " Item", "item_code")) {
53 cur_frm.get_field("items").grid.set_multiple_add("item_code", "qty");
Rushabh Mehtab6843f22014-06-04 13:10:41 +053054 }
Anand Doshi5e4e5d72013-08-06 17:23:44 +053055 },
Rushabh Mehtaff938022014-04-15 18:40:00 +053056
Anand Doshi3543f302013-05-24 19:25:01 +053057 refresh: function() {
Anand Doshi8bde7f92014-04-24 18:11:49 +053058 erpnext.toggle_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +053059 erpnext.hide_company();
Nabin Hait239c2c32015-01-15 14:21:41 +053060 this.hide_currency_and_price_list()
Anand Doshi3543f302013-05-24 19:25:01 +053061 this.show_item_wise_taxes();
Nabin Hait1f996c32013-07-29 19:00:53 +053062 this.set_dynamic_labels();
Anand Doshi50d7e8c2015-01-06 17:14:13 +053063 erpnext.pos.make_pos_btn(this.frm);
Rushabh Mehta614e7ab2015-02-18 19:51:48 +053064 this.setup_sms();
65 },
Nabin Haitffc7f3f2015-05-12 15:07:02 +053066
67 apply_default_taxes: function() {
68 var me = this;
69 return frappe.call({
70 method: "erpnext.controllers.accounts_controller.get_default_taxes_and_charges",
71 args: {
72 "master_doctype": frappe.meta.get_docfield(me.frm.doc.doctype, "taxes_and_charges",
73 me.frm.doc.name).options
74 },
75 callback: function(r) {
76 if(!r.exc) {
77 me.frm.set_value("taxes", r.message);
78 me.calculate_taxes_and_totals();
79 }
80 }
81 });
82 },
Rushabh Mehta614e7ab2015-02-18 19:51:48 +053083
84 setup_sms: function() {
85 var me = this;
86 if(this.frm.doc.docstatus===1 && !in_list(["Lost", "Stopped"], this.frm.doc.status)) {
87 this.frm.page.add_menu_item(__('Send SMS'), function() { me.send_sms(); });
88 }
89 },
90
91 send_sms: function() {
92 frappe.require("assets/erpnext/js/sms_manager.js");
93 var sms_man = new SMSManager(this.doc);
Nabin Hait239c2c32015-01-15 14:21:41 +053094 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +053095
Nabin Hait239c2c32015-01-15 14:21:41 +053096 hide_currency_and_price_list: function() {
97 if(this.frm.doc.docstatus > 0) {
98 hide_field("currency_and_price_list");
99 } else {
100 unhide_field("currency_and_price_list");
101 }
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530102 },
103
Nabin Hait139dc7b2014-02-12 14:53:18 +0530104 item_code: function(doc, cdt, cdn) {
105 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530106 var item = frappe.get_doc(cdt, cdn);
Nabin Hait139dc7b2014-02-12 14:53:18 +0530107 if(item.item_code || item.barcode || item.serial_no) {
108 if(!this.validate_company_and_party()) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530109 cur_frm.fields_dict["items"].grid.grid_rows[item.idx - 1].remove();
Nabin Hait139dc7b2014-02-12 14:53:18 +0530110 } else {
111 return this.frm.call({
112 method: "erpnext.stock.get_item_details.get_item_details",
113 child: item,
114 args: {
115 args: {
116 item_code: item.item_code,
117 barcode: item.barcode,
118 serial_no: item.serial_no,
119 warehouse: item.warehouse,
Nabin Hait444f9562014-06-20 15:59:49 +0530120 parenttype: me.frm.doc.doctype,
121 parent: me.frm.doc.name,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530122 customer: me.frm.doc.customer,
123 supplier: me.frm.doc.supplier,
124 currency: me.frm.doc.currency,
125 conversion_rate: me.frm.doc.conversion_rate,
126 price_list: me.frm.doc.selling_price_list ||
127 me.frm.doc.buying_price_list,
128 price_list_currency: me.frm.doc.price_list_currency,
129 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
130 company: me.frm.doc.company,
131 order_type: me.frm.doc.order_type,
132 is_pos: cint(me.frm.doc.is_pos),
133 is_subcontracted: me.frm.doc.is_subcontracted,
Nabin Haita1bf43b2015-03-17 10:50:47 +0530134 transaction_date: me.frm.doc.transaction_date || me.frm.doc.posting_date,
Nabin Hait444f9562014-06-20 15:59:49 +0530135 ignore_pricing_rule: me.frm.doc.ignore_pricing_rule,
136 doctype: item.doctype,
Anand Doshi43f087c2014-08-26 14:25:53 +0530137 name: item.name,
Anand Doshi9b955fe2015-01-05 16:19:12 +0530138 project_name: item.project_name || me.frm.doc.project_name,
139 qty: item.qty
Nabin Hait139dc7b2014-02-12 14:53:18 +0530140 }
141 },
Anand Doshi13945092014-09-21 19:45:49 +0530142
Nabin Hait139dc7b2014-02-12 14:53:18 +0530143 callback: function(r) {
144 if(!r.exc) {
145 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
146 }
147 }
148 });
149 }
150 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530151 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530152
153 serial_no: function(doc, cdt, cdn) {
154 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530155 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530156
157 if (item.serial_no) {
158 if (!item.item_code) {
159 this.frm.script_manager.trigger("item_code", cdt, cdn);
160 }
161 else {
162 var sr_no = [];
163
164 // Replacing all occurences of comma with carriage return
165 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
166
167 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530168
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530169 // Trim each string and push unique string to new list
170 for (var x=0; x<=serial_nos.length - 1; x++) {
171 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
172 sr_no.push(serial_nos[x].trim());
173 }
174 }
175
176 // Add the new list to the serial no. field in grid with each in new line
177 item.serial_no = "";
178 for (var x=0; x<=sr_no.length - 1; x++)
179 item.serial_no += sr_no[x] + '\n';
180
181 refresh_field("serial_no", item.name, item.parentfield);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530182 frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530183 }
184 }
185 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530186
Anand Doshi923d41d2013-05-28 17:23:36 +0530187 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530188 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530189 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530190
Anand Doshi3543f302013-05-24 19:25:01 +0530191 company: function() {
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530192 var me = this;
193 var fn = function() {
194 if(me.frm.doc.company && me.frm.fields_dict.currency) {
195 var company_currency = me.get_company_currency();
196 if (!me.frm.doc.currency) {
197 me.frm.set_value("currency", company_currency);
198 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530199
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530200 if (me.frm.doc.currency == company_currency) {
201 me.frm.set_value("conversion_rate", 1.0);
202 }
203 if (me.frm.doc.price_list_currency == company_currency) {
204 me.frm.set_value('plc_conversion_rate', 1.0);
205 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530206
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530207 me.frm.script_manager.trigger("currency");
208 me.apply_pricing_rule();
Nabin Haitdd38a262014-12-26 13:15:21 +0530209 }
Anand Doshi3543f302013-05-24 19:25:01 +0530210 }
Nabin Haitdd38a262014-12-26 13:15:21 +0530211
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530212 if (this.frm.doc.posting_date) var date = this.frm.doc.posting_date;
213 else var date = this.frm.doc.transaction_date;
214 erpnext.get_fiscal_year(this.frm.doc.company, date, fn);
Neil Trini Lasrado31a92bd2014-12-15 15:49:40 +0530215 erpnext.get_letter_head(this.frm.doc.company);
Rushabh Mehta45ca8a42015-04-28 16:02:09 +0530216
217 if(this.frm.doc.company) {
218 erpnext.last_selected_company = this.frm.doc.company;
219 }
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530220 },
221
222 transaction_date: function() {
223 erpnext.get_fiscal_year(this.frm.doc.company, this.frm.doc.transaction_date);
Neil Trini Lasrado78fa6952014-10-03 17:43:02 +0530224 },
225
226 posting_date: function() {
Neil Trini Lasrado565d9862014-10-07 18:26:39 +0530227 erpnext.get_fiscal_year(this.frm.doc.company, this.frm.doc.posting_date);
Anand Doshi3543f302013-05-24 19:25:01 +0530228 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530229
Anand Doshi3543f302013-05-24 19:25:01 +0530230 get_company_currency: function() {
231 return erpnext.get_currency(this.frm.doc.company);
232 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530233
Anand Doshi3543f302013-05-24 19:25:01 +0530234 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530235 var me = this;
236 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530237
Anand Doshi61a2f682013-06-21 17:55:31 +0530238 var company_currency = this.get_company_currency();
Nabin Hait69cdf592015-05-02 18:46:10 +0530239 // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
240 if(this.frm.doc.currency !== company_currency && !this.frm.doc.ignore_pricing_rule) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530241 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530242 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530243 me.frm.set_value("conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530244 });
245 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530246 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530247 }
248 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530249
Anand Doshi61a2f682013-06-21 17:55:31 +0530250 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530251 if(this.frm.doc.currency === this.get_company_currency()) {
252 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530253 }
254 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530255 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
256 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
257 }
Nabin Haitdd82bf22015-05-11 16:49:17 +0530258
Nabin Haita3dd72a2014-05-28 12:49:20 +0530259 if(flt(this.frm.doc.conversion_rate)>0.0) {
Nabin Hait444f9562014-06-20 15:59:49 +0530260 if(this.frm.doc.ignore_pricing_rule) {
261 this.calculate_taxes_and_totals();
Anand Doshi51f722d2014-07-04 15:44:26 +0530262 } else if (!this.in_apply_price_list){
263 this.apply_price_list();
Nabin Hait444f9562014-06-20 15:59:49 +0530264 }
265
Nabin Haita3dd72a2014-05-28 12:49:20 +0530266 }
Anand Doshi3543f302013-05-24 19:25:01 +0530267 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530268
Anand Doshi61a2f682013-06-21 17:55:31 +0530269 get_exchange_rate: function(from_currency, to_currency, callback) {
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +0530270 frappe.call({
271 method: "erpnext.setup.utils.get_exchange_rate",
272 args: {
273 from_currency: from_currency,
274 to_currency: to_currency
275 },
276 callback: function(r) {
277 callback(flt(r.message));
278 }
Anand Doshi61a2f682013-06-21 17:55:31 +0530279 });
280 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530281
Anand Doshi3543f302013-05-24 19:25:01 +0530282 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530283 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530284 this.set_dynamic_labels();
Nabin Haitec361ea2015-05-11 18:14:45 +0530285
286 var company_currency = this.get_company_currency();
287 // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
288 if(this.frm.doc.price_list_currency !== company_currency && !this.frm.doc.ignore_pricing_rule) {
289 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
290 function(exchange_rate) {
291 me.frm.set_value("plc_conversion_rate", exchange_rate);
292 });
293 } else {
294 this.plc_conversion_rate();
295 }
Anand Doshi3543f302013-05-24 19:25:01 +0530296 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530297
Anand Doshi3543f302013-05-24 19:25:01 +0530298 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530299 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
300 this.frm.set_value("plc_conversion_rate", 1.0);
Nabin Haitec361ea2015-05-11 18:14:45 +0530301 } 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 &&
302 cint(this.frm.doc.plc_conversion_rate) != cint(this.frm.doc.conversion_rate)) {
303 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530304 }
Nabin Haitec361ea2015-05-11 18:14:45 +0530305
306 if(!this.in_apply_price_list) {
307 this.apply_price_list();
Anand Doshi61a2f682013-06-21 17:55:31 +0530308 }
Anand Doshi3543f302013-05-24 19:25:01 +0530309 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530310
Anand Doshi3543f302013-05-24 19:25:01 +0530311 qty: function(doc, cdt, cdn) {
Nabin Hait444f9562014-06-20 15:59:49 +0530312 this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
Anand Doshi3543f302013-05-24 19:25:01 +0530313 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530314
Anand Doshi61a2f682013-06-21 17:55:31 +0530315 set_dynamic_labels: function() {
316 // What TODO? should we make price list system non-mandatory?
317 this.frm.toggle_reqd("plc_conversion_rate",
318 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530319
Anand Doshi61a2f682013-06-21 17:55:31 +0530320 var company_currency = this.get_company_currency();
321 this.change_form_labels(company_currency);
322 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530323 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530324 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530325
Nabin Hait613d0812015-02-23 11:58:15 +0530326 change_form_labels: function(company_currency) {
327 var me = this;
328 var field_label_map = {};
329
330 var setup_field_label_map = function(fields_list, currency) {
331 $.each(fields_list, function(i, fname) {
332 var docfield = frappe.meta.docfield_map[me.frm.doc.doctype][fname];
333 if(docfield) {
334 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
335 field_label_map[fname] = label.trim() + " (" + currency + ")";
336 }
337 });
338 };
339 setup_field_label_map(["base_total", "base_net_total", "base_total_taxes_and_charges",
340 "base_discount_amount", "base_grand_total", "base_rounded_total", "base_in_words",
341 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted", "total_amount_to_pay",
Nabin Hait37b047d2015-02-23 16:01:33 +0530342 "outstanding_amount", "total_advance", "paid_amount", "write_off_amount"], company_currency);
Nabin Hait613d0812015-02-23 11:58:15 +0530343
344 setup_field_label_map(["total", "net_total", "total_taxes_and_charges", "discount_amount",
345 "grand_total", "taxes_and_charges_added", "taxes_and_charges_deducted",
346 "rounded_total", "in_words"], this.frm.doc.currency);
347
348 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
349 + " = [?] " + company_currency)
350
351 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
352 cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency
353 + " = [?] " + company_currency)
354 }
355
356 // toggle fields
357 this.frm.toggle_display(["conversion_rate", "base_total", "base_net_total", "base_total_taxes_and_charges",
358 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted",
359 "base_grand_total", "base_rounded_total", "base_in_words", "base_discount_amount"],
360 this.frm.doc.currency != company_currency);
361
362 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
363 this.frm.doc.price_list_currency != company_currency);
364
365 // set labels
366 $.each(field_label_map, function(fname, label) {
367 me.frm.fields_dict[fname].set_label(label);
368 });
Nabin Hait37b047d2015-02-23 16:01:33 +0530369
Nabin Hait903c42a2015-02-24 15:24:49 +0530370 var show =cint(cur_frm.doc.discount_amount) ||
371 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
372
373 if(frappe.meta.get_docfield(cur_frm.doctype, "net_total"))
374 cur_frm.toggle_display("net_total", show);
375
376 if(frappe.meta.get_docfield(cur_frm.doctype, "base_net_total"))
377 cur_frm.toggle_display("base_net_total", (show && (me.frm.doc.currency != company_currency)));
378
Nabin Hait613d0812015-02-23 11:58:15 +0530379 },
380
381 change_grid_labels: function(company_currency) {
382 var me = this;
383 var field_label_map = {};
384
385 var setup_field_label_map = function(fields_list, currency, parentfield) {
386 var grid_doctype = me.frm.fields_dict[parentfield].grid.doctype;
387 $.each(fields_list, function(i, fname) {
388 var docfield = frappe.meta.docfield_map[grid_doctype][fname];
389 if(docfield) {
390 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
391 field_label_map[grid_doctype + "-" + fname] =
392 label.trim() + " (" + currency + ")";
393 }
394 });
395 }
396
397 setup_field_label_map(["base_rate", "base_net_rate", "base_price_list_rate", "base_amount", "base_net_amount"],
398 company_currency, "items");
399
400 setup_field_label_map(["rate", "net_rate", "price_list_rate", "amount", "net_amount"],
401 this.frm.doc.currency, "items");
402
403 if(this.frm.fields_dict["taxes"]) {
404 setup_field_label_map(["tax_amount", "total", "tax_amount_after_discount"], this.frm.doc.currency, "taxes");
405
406 setup_field_label_map(["base_tax_amount", "base_total", "base_tax_amount_after_discount"], company_currency, "taxes");
407 }
408
409 if(this.frm.fields_dict["advances"]) {
410 setup_field_label_map(["advance_amount", "allocated_amount"], company_currency, "advances");
411 }
412
413 // toggle columns
414 var item_grid = this.frm.fields_dict["items"].grid;
415 $.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) {
416 if(frappe.meta.get_docfield(item_grid.doctype, fname))
417 item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency);
418 });
419
420 var show = (cint(cur_frm.doc.discount_amount)) ||
421 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
422
423 $.each(["net_rate", "net_amount"], function(i, fname) {
424 if(frappe.meta.get_docfield(item_grid.doctype, fname))
425 item_grid.set_column_disp(fname, show);
426 });
427
428 $.each(["base_net_rate", "base_net_amount"], function(i, fname) {
429 if(frappe.meta.get_docfield(item_grid.doctype, fname))
430 item_grid.set_column_disp(fname, (show && (me.frm.doc.currency != company_currency)));
431 });
432
433 // set labels
434 var $wrapper = $(this.frm.wrapper);
435 $.each(field_label_map, function(fname, label) {
436 fname = fname.split("-");
437 var df = frappe.meta.get_docfield(fname[0], fname[1], me.frm.doc.name);
438 if(df) df.label = label;
439 });
440 },
441
Anand Doshi923d41d2013-05-28 17:23:36 +0530442 recalculate: function() {
443 this.calculate_taxes_and_totals();
444 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530445
Anand Doshi923d41d2013-05-28 17:23:36 +0530446 recalculate_values: function() {
447 this.calculate_taxes_and_totals();
448 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530449
Anand Doshid0b00722013-05-28 18:54:48 +0530450 calculate_charges: function() {
451 this.calculate_taxes_and_totals();
452 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530453
Nabin Hait444f9562014-06-20 15:59:49 +0530454 ignore_pricing_rule: function() {
455 this.apply_pricing_rule();
456 },
457
458 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530459 var me = this;
Anand Doshidffec8f2014-07-01 17:45:15 +0530460 return this.frm.call({
461 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
462 args: { args: this._get_args(item) },
463 callback: function(r) {
Anand Doshif214bfc2014-07-16 17:52:08 +0530464 if (!r.exc && r.message) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530465 me._set_values_for_item_list(r.message);
466 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
467 }
468 }
469 });
470 },
471
472 _get_args: function(item) {
473 var me = this;
474 return {
475 "item_list": this._get_item_list(item),
Nabin Hait444f9562014-06-20 15:59:49 +0530476 "customer": me.frm.doc.customer,
477 "customer_group": me.frm.doc.customer_group,
478 "territory": me.frm.doc.territory,
479 "supplier": me.frm.doc.supplier,
480 "supplier_type": me.frm.doc.supplier_type,
481 "currency": me.frm.doc.currency,
482 "conversion_rate": me.frm.doc.conversion_rate,
483 "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
Nabin Haitdd82bf22015-05-11 16:49:17 +0530484 "price_list_currency": me.frm.doc.price_list_currency,
Nabin Hait444f9562014-06-20 15:59:49 +0530485 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
486 "company": me.frm.doc.company,
487 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
488 "campaign": me.frm.doc.campaign,
489 "sales_partner": me.frm.doc.sales_partner,
490 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
491 "parenttype": me.frm.doc.doctype,
492 "parent": me.frm.doc.name
493 };
Nabin Hait444f9562014-06-20 15:59:49 +0530494 },
495
496 _get_item_list: function(item) {
497 var item_list = [];
498 var append_item = function(d) {
Anand Doshi39edcbc2014-07-28 16:00:19 +0530499 if (d.item_code) {
500 item_list.push({
501 "doctype": d.doctype,
502 "name": d.name,
503 "item_code": d.item_code,
504 "item_group": d.item_group,
505 "brand": d.brand,
506 "qty": d.qty
507 });
508 }
Nabin Hait444f9562014-06-20 15:59:49 +0530509 };
Nabin Haita3dd72a2014-05-28 12:49:20 +0530510
Nabin Hait444f9562014-06-20 15:59:49 +0530511 if (item) {
512 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +0530513 } else {
Nabin Hait96339342015-01-21 17:22:11 +0530514 $.each(this.frm.doc["items"] || [], function(i, d) {
Nabin Hait444f9562014-06-20 15:59:49 +0530515 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530516 });
517 }
Nabin Hait444f9562014-06-20 15:59:49 +0530518 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +0530519 },
520
Anand Doshidffec8f2014-07-01 17:45:15 +0530521 _set_values_for_item_list: function(children) {
Nabin Hait49a27292014-12-17 11:21:32 +0530522 var me = this;
Nabin Hait7d8fa802014-12-30 15:35:45 +0530523 var price_list_rate_changed = false;
Anand Doshidea3fb02015-02-05 15:05:58 +0530524 for(var i=0, l=children.length; i<l; i++) {
525 var d = children[i];
Nabin Hait49a27292014-12-17 11:21:32 +0530526 var existing_pricing_rule = frappe.model.get_value(d.doctype, d.name, "pricing_rule");
Anand Doshidea3fb02015-02-05 15:05:58 +0530527
528 for(var k in d) {
529 var v = d[k];
Anand Doshidffec8f2014-07-01 17:45:15 +0530530 if (["doctype", "name"].indexOf(k)===-1) {
Nabin Hait7d8fa802014-12-30 15:35:45 +0530531 if(k=="price_list_rate") {
532 if(flt(v) != flt(d.price_list_rate)) price_list_rate_changed = true;
533 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530534 frappe.model.set_value(d.doctype, d.name, k, v);
535 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530536 }
537
Nabin Hait49a27292014-12-17 11:21:32 +0530538 // if pricing rule set as blank from an existing value, apply price_list
Nabin Haitcfe3c542014-12-22 10:42:48 +0530539 if(!me.frm.doc.ignore_pricing_rule && existing_pricing_rule && !d.pricing_rule) {
Nabin Hait49a27292014-12-17 11:21:32 +0530540 me.apply_price_list(frappe.get_doc(d.doctype, d.name));
541 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530542 }
Nabin Hait7d8fa802014-12-30 15:35:45 +0530543
Anand Doshidea3fb02015-02-05 15:05:58 +0530544 if(!price_list_rate_changed) me.calculate_taxes_and_totals();
Anand Doshidffec8f2014-07-01 17:45:15 +0530545 },
546
Nabin Hait49a27292014-12-17 11:21:32 +0530547 apply_price_list: function(item) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530548 var me = this;
Rushabh Mehta052bd622015-01-30 16:57:24 +0530549 var args = this._get_args(item);
Nabin Hait37b047d2015-02-23 16:01:33 +0530550
Anand Doshidffec8f2014-07-01 17:45:15 +0530551 return this.frm.call({
552 method: "erpnext.stock.get_item_details.apply_price_list",
Rushabh Mehta052bd622015-01-30 16:57:24 +0530553 args: { args: args },
Anand Doshidffec8f2014-07-01 17:45:15 +0530554 callback: function(r) {
555 if (!r.exc) {
Anand Doshi51f722d2014-07-04 15:44:26 +0530556 me.in_apply_price_list = true;
Anand Doshidffec8f2014-07-01 17:45:15 +0530557 me.frm.set_value("price_list_currency", r.message.parent.price_list_currency);
558 me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate);
Anand Doshi51f722d2014-07-04 15:44:26 +0530559 me.in_apply_price_list = false;
Nabin Hait37b047d2015-02-23 16:01:33 +0530560
561 if(args.item_list.length) {
562 me._set_values_for_item_list(r.message.children);
563 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530564 }
565 }
566 });
567 },
568
Anand Doshi3543f302013-05-24 19:25:01 +0530569 get_item_wise_taxes_html: function() {
570 var item_tax = {};
571 var tax_accounts = [];
572 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530573
Nabin Hait96339342015-01-21 17:22:11 +0530574 $.each(this.frm.doc["taxes"] || [], function(i, tax) {
Anand Doshi3543f302013-05-24 19:25:01 +0530575 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530576 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530577 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530578 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530579 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530580 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530581 var tax_rate = "";
582 if(tax_data[0] != null) {
583 tax_rate = (tax.charge_type === "Actual") ?
584 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
585 (flt(tax_data[0], tax_rate_precision) + "%");
586 }
587 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
588 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530589
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530590 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530591 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530592 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530593 }
Anand Doshi3543f302013-05-24 19:25:01 +0530594 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530595 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530596 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530597
598 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530599 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530600
Anand Doshi179e3772013-09-05 16:53:57 +0530601 var distinct_item_names = [];
602 var distinct_items = [];
Nabin Hait96339342015-01-21 17:22:11 +0530603 $.each(this.frm.doc["items"] || [], function(i, item) {
Anand Doshi179e3772013-09-05 16:53:57 +0530604 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
605 distinct_item_names.push(item.item_code || item.item_name);
606 distinct_items.push(item);
607 }
608 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530609
Anand Doshi179e3772013-09-05 16:53:57 +0530610 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530611 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530612 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530613 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
614 item_name: item.item_name,
615 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530616 return item_tax_record[head[0]] ?
617 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530618 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530619 }).join("\n")
620 });
621 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530622
Anand Doshi53b73422013-05-31 11:50:01 +0530623 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530624 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
625 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530626 <thead><tr>' + headings + '</tr></thead> \
627 <tbody>' + rows + '</tbody> \
628 </table></div>';
629 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530630
Nabin Hait139dc7b2014-02-12 14:53:18 +0530631 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530632 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530633 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530634
Nabin Hait0f231062014-02-20 11:27:47 +0530635 $.each(["company", "customer"], function(i, fieldname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530636 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname)) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530637 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530638 msgprint(__("Please specify") + ": " +
639 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530640 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530641 valid = false;
642 }
Anand Doshi3543f302013-05-24 19:25:01 +0530643 }
644 });
645 return valid;
646 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530647
Anand Doshibf3e54a2013-06-05 14:11:32 +0530648 get_terms: function() {
649 var me = this;
650 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530651 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530652 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530653 args: {
654 doctype: "Terms and Conditions",
655 fieldname: "terms",
656 filters: { name: this.frm.doc.tc_name },
657 },
658 });
659 }
660 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530661
662 taxes_and_charges: function() {
663 var me = this;
664 if(this.frm.doc.taxes_and_charges) {
665 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530666 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
667 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530668 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
669 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530670 "master_name": this.frm.doc.taxes_and_charges,
Nabin Hait6b039142014-05-02 15:45:10 +0530671 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530672 callback: function(r) {
673 if(!r.exc) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530674 me.frm.set_value("taxes", r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530675 me.calculate_taxes_and_totals();
676 }
677 }
678 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530679 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530680 },
681
682 show_item_wise_taxes: function() {
683 if(this.frm.fields_dict.other_charges_calculation) {
Anand Doshidd942bb2015-02-04 18:52:35 +0530684 var html = this.get_item_wise_taxes_html();
685 if (html) {
686 this.frm.toggle_display("other_charges_calculation", true);
687 $(this.frm.fields_dict.other_charges_calculation.wrapper).html(html);
688 } else {
689 this.frm.toggle_display("other_charges_calculation", false);
690 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530691 }
692 },
Anand Doshi13945092014-09-21 19:45:49 +0530693
694 is_recurring: function() {
695 // set default values for recurring documents
696 if(this.frm.doc.is_recurring) {
697 var owner_email = this.frm.doc.owner=="Administrator"
698 ? frappe.user_info("Administrator").email
699 : this.frm.doc.owner;
700
701 this.frm.doc.notification_email_address = $.map([cstr(owner_email),
702 cstr(this.frm.doc.contact_email)], function(v) { return v || null; }).join(", ");
703 this.frm.doc.repeat_on_day_of_month = frappe.datetime.str_to_obj(this.frm.doc.posting_date).getDate();
704 }
705
706 refresh_many(["notification_email_address", "repeat_on_day_of_month"]);
707 },
708
709 from_date: function() {
710 // set to_date
711 if(this.frm.doc.from_date) {
712 var recurring_type_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6,
713 'Yearly': 12};
714
715 var months = recurring_type_map[this.frm.doc.recurring_type];
716 if(months) {
717 var to_date = frappe.datetime.add_months(this.frm.doc.from_date,
718 months);
719 this.frm.doc.to_date = frappe.datetime.add_days(to_date, -1);
720 refresh_field('to_date');
721 }
722 }
723 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530724});
Nabin Haitce245122015-02-22 20:14:49 +0530725
Nabin Hait93b3a372015-02-24 17:08:34 +0530726frappe.ui.form.on(cur_frm.doctype + " Item", "rate", function(frm, cdt, cdn) {
Nabin Haitce245122015-02-22 20:14:49 +0530727 var item = frappe.get_doc(cdt, cdn);
728 frappe.model.round_floats_in(item, ["rate", "price_list_rate"]);
729
730 if(item.price_list_rate) {
731 item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0, precision("discount_percentage", item));
732 } else {
733 item.discount_percentage = 0.0;
734 }
735
736 cur_frm.cscript.calculate_taxes_and_totals();
737})
Nabin Hait613d0812015-02-23 11:58:15 +0530738
739frappe.ui.form.on(cur_frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
740 cur_frm.cscript.calculate_taxes_and_totals();
741})
742
743frappe.ui.form.on(cur_frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
744 cur_frm.cscript.calculate_taxes_and_totals();
745})
746
747frappe.ui.form.on(cur_frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
748 cur_frm.cscript.calculate_taxes_and_totals();
749})
750
751frappe.ui.form.on(cur_frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
Nabin Hait903c42a2015-02-24 15:24:49 +0530752 cur_frm.cscript.set_dynamic_labels();
Nabin Hait613d0812015-02-23 11:58:15 +0530753 cur_frm.cscript.calculate_taxes_and_totals();
754})
Nabin Hait37b047d2015-02-23 16:01:33 +0530755
756frappe.ui.form.on(cur_frm.doctype, "apply_discount_on", function(frm) {
757 cur_frm.cscript.calculate_taxes_and_totals();
758})
759
760frappe.ui.form.on(cur_frm.doctype, "discount_amount", function(frm) {
Nabin Hait903c42a2015-02-24 15:24:49 +0530761 cur_frm.cscript.set_dynamic_labels();
Nabin Hait37b047d2015-02-23 16:01:33 +0530762 cur_frm.cscript.calculate_taxes_and_totals();
763})