blob: 7ee342f685db395cb4a62fb311a1a4947e445fa6 [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);
Nabin Haitdd38a262014-12-26 13:15:21 +0530193
Neil Trini Lasrado31a92bd2014-12-15 15:49:40 +0530194 erpnext.get_letter_head(this.frm.doc.company);
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530195 },
196
197 transaction_date: function() {
198 erpnext.get_fiscal_year(this.frm.doc.company, this.frm.doc.transaction_date);
Neil Trini Lasrado78fa6952014-10-03 17:43:02 +0530199 },
200
201 posting_date: function() {
Neil Trini Lasrado565d9862014-10-07 18:26:39 +0530202 erpnext.get_fiscal_year(this.frm.doc.company, this.frm.doc.posting_date);
Anand Doshi3543f302013-05-24 19:25:01 +0530203 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530204
Anand Doshi3543f302013-05-24 19:25:01 +0530205 get_company_currency: function() {
206 return erpnext.get_currency(this.frm.doc.company);
207 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530208
Anand Doshi3543f302013-05-24 19:25:01 +0530209 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530210 var me = this;
211 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530212
Anand Doshi61a2f682013-06-21 17:55:31 +0530213 var company_currency = this.get_company_currency();
214 if(this.frm.doc.currency !== company_currency) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530215 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530216 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530217 me.frm.set_value("conversion_rate", exchange_rate);
218 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530219 });
220 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530221 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530222 }
223 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530224
Anand Doshi61a2f682013-06-21 17:55:31 +0530225 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530226 if(this.frm.doc.currency === this.get_company_currency()) {
227 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530228 }
229 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530230 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
231 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
232 }
Nabin Haita3dd72a2014-05-28 12:49:20 +0530233 if(flt(this.frm.doc.conversion_rate)>0.0) {
Nabin Hait444f9562014-06-20 15:59:49 +0530234 if(this.frm.doc.ignore_pricing_rule) {
235 this.calculate_taxes_and_totals();
Anand Doshi51f722d2014-07-04 15:44:26 +0530236 } else if (!this.in_apply_price_list){
237 this.apply_price_list();
Nabin Hait444f9562014-06-20 15:59:49 +0530238 }
239
Nabin Haita3dd72a2014-05-28 12:49:20 +0530240 }
Anand Doshi3543f302013-05-24 19:25:01 +0530241 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530242
Anand Doshi61a2f682013-06-21 17:55:31 +0530243 get_exchange_rate: function(from_currency, to_currency, callback) {
244 var exchange_name = from_currency + "-" + to_currency;
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530245 frappe.model.with_doc("Currency Exchange", exchange_name, function(name) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530246 var exchange_doc = frappe.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530247 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530248 });
249 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530250
Anand Doshi3543f302013-05-24 19:25:01 +0530251 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530252 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530253 this.set_dynamic_labels();
Anand Doshi51f722d2014-07-04 15:44:26 +0530254 this.set_plc_conversion_rate();
Anand Doshi3543f302013-05-24 19:25:01 +0530255 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530256
Anand Doshi3543f302013-05-24 19:25:01 +0530257 plc_conversion_rate: function() {
Anand Doshi51f722d2014-07-04 15:44:26 +0530258 this.set_plc_conversion_rate();
259 if(!this.in_apply_price_list) {
260 this.apply_price_list();
261 }
262 },
263
264 set_plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530265 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
266 this.frm.set_value("plc_conversion_rate", 1.0);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530267 }
268 if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
Anand Doshi61a2f682013-06-21 17:55:31 +0530269 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530270 }
Anand Doshi3543f302013-05-24 19:25:01 +0530271 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530272
Anand Doshi3543f302013-05-24 19:25:01 +0530273 qty: function(doc, cdt, cdn) {
Nabin Hait444f9562014-06-20 15:59:49 +0530274 this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
Anand Doshi3543f302013-05-24 19:25:01 +0530275 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530276
Anand Doshi61a2f682013-06-21 17:55:31 +0530277 set_dynamic_labels: function() {
278 // What TODO? should we make price list system non-mandatory?
279 this.frm.toggle_reqd("plc_conversion_rate",
280 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530281
Anand Doshi61a2f682013-06-21 17:55:31 +0530282 var company_currency = this.get_company_currency();
283 this.change_form_labels(company_currency);
284 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530285 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530286 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530287
Nabin Hait613d0812015-02-23 11:58:15 +0530288 change_form_labels: function(company_currency) {
289 var me = this;
290 var field_label_map = {};
291
292 var setup_field_label_map = function(fields_list, currency) {
293 $.each(fields_list, function(i, fname) {
294 var docfield = frappe.meta.docfield_map[me.frm.doc.doctype][fname];
295 if(docfield) {
296 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
297 field_label_map[fname] = label.trim() + " (" + currency + ")";
298 }
299 });
300 };
301 setup_field_label_map(["base_total", "base_net_total", "base_total_taxes_and_charges",
302 "base_discount_amount", "base_grand_total", "base_rounded_total", "base_in_words",
303 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted", "total_amount_to_pay",
Nabin Hait37b047d2015-02-23 16:01:33 +0530304 "outstanding_amount", "total_advance", "paid_amount", "write_off_amount"], company_currency);
Nabin Hait613d0812015-02-23 11:58:15 +0530305
306 setup_field_label_map(["total", "net_total", "total_taxes_and_charges", "discount_amount",
307 "grand_total", "taxes_and_charges_added", "taxes_and_charges_deducted",
308 "rounded_total", "in_words"], this.frm.doc.currency);
309
310 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
311 + " = [?] " + company_currency)
312
313 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
314 cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency
315 + " = [?] " + company_currency)
316 }
317
318 // toggle fields
319 this.frm.toggle_display(["conversion_rate", "base_total", "base_net_total", "base_total_taxes_and_charges",
320 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted",
321 "base_grand_total", "base_rounded_total", "base_in_words", "base_discount_amount"],
322 this.frm.doc.currency != company_currency);
323
324 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
325 this.frm.doc.price_list_currency != company_currency);
326
327 // set labels
328 $.each(field_label_map, function(fname, label) {
329 me.frm.fields_dict[fname].set_label(label);
330 });
Nabin Hait37b047d2015-02-23 16:01:33 +0530331
Nabin Hait903c42a2015-02-24 15:24:49 +0530332 var show =cint(cur_frm.doc.discount_amount) ||
333 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
334
335 if(frappe.meta.get_docfield(cur_frm.doctype, "net_total"))
336 cur_frm.toggle_display("net_total", show);
337
338 if(frappe.meta.get_docfield(cur_frm.doctype, "base_net_total"))
339 cur_frm.toggle_display("base_net_total", (show && (me.frm.doc.currency != company_currency)));
340
Nabin Hait613d0812015-02-23 11:58:15 +0530341 },
342
343 change_grid_labels: function(company_currency) {
344 var me = this;
345 var field_label_map = {};
346
347 var setup_field_label_map = function(fields_list, currency, parentfield) {
348 var grid_doctype = me.frm.fields_dict[parentfield].grid.doctype;
349 $.each(fields_list, function(i, fname) {
350 var docfield = frappe.meta.docfield_map[grid_doctype][fname];
351 if(docfield) {
352 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
353 field_label_map[grid_doctype + "-" + fname] =
354 label.trim() + " (" + currency + ")";
355 }
356 });
357 }
358
359 setup_field_label_map(["base_rate", "base_net_rate", "base_price_list_rate", "base_amount", "base_net_amount"],
360 company_currency, "items");
361
362 setup_field_label_map(["rate", "net_rate", "price_list_rate", "amount", "net_amount"],
363 this.frm.doc.currency, "items");
364
365 if(this.frm.fields_dict["taxes"]) {
366 setup_field_label_map(["tax_amount", "total", "tax_amount_after_discount"], this.frm.doc.currency, "taxes");
367
368 setup_field_label_map(["base_tax_amount", "base_total", "base_tax_amount_after_discount"], company_currency, "taxes");
369 }
370
371 if(this.frm.fields_dict["advances"]) {
372 setup_field_label_map(["advance_amount", "allocated_amount"], company_currency, "advances");
373 }
374
375 // toggle columns
376 var item_grid = this.frm.fields_dict["items"].grid;
377 $.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) {
378 if(frappe.meta.get_docfield(item_grid.doctype, fname))
379 item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency);
380 });
381
382 var show = (cint(cur_frm.doc.discount_amount)) ||
383 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
384
385 $.each(["net_rate", "net_amount"], function(i, fname) {
386 if(frappe.meta.get_docfield(item_grid.doctype, fname))
387 item_grid.set_column_disp(fname, show);
388 });
389
390 $.each(["base_net_rate", "base_net_amount"], function(i, fname) {
391 if(frappe.meta.get_docfield(item_grid.doctype, fname))
392 item_grid.set_column_disp(fname, (show && (me.frm.doc.currency != company_currency)));
393 });
394
395 // set labels
396 var $wrapper = $(this.frm.wrapper);
397 $.each(field_label_map, function(fname, label) {
398 fname = fname.split("-");
399 var df = frappe.meta.get_docfield(fname[0], fname[1], me.frm.doc.name);
400 if(df) df.label = label;
401 });
402 },
403
Anand Doshi923d41d2013-05-28 17:23:36 +0530404 recalculate: function() {
405 this.calculate_taxes_and_totals();
406 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530407
Anand Doshi923d41d2013-05-28 17:23:36 +0530408 recalculate_values: function() {
409 this.calculate_taxes_and_totals();
410 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530411
Anand Doshid0b00722013-05-28 18:54:48 +0530412 calculate_charges: function() {
413 this.calculate_taxes_and_totals();
414 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530415
Nabin Hait444f9562014-06-20 15:59:49 +0530416 ignore_pricing_rule: function() {
417 this.apply_pricing_rule();
418 },
419
420 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530421 var me = this;
Anand Doshidffec8f2014-07-01 17:45:15 +0530422 return this.frm.call({
423 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
424 args: { args: this._get_args(item) },
425 callback: function(r) {
Anand Doshif214bfc2014-07-16 17:52:08 +0530426 if (!r.exc && r.message) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530427 me._set_values_for_item_list(r.message);
428 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
429 }
430 }
431 });
432 },
433
434 _get_args: function(item) {
435 var me = this;
436 return {
437 "item_list": this._get_item_list(item),
Nabin Hait444f9562014-06-20 15:59:49 +0530438 "customer": me.frm.doc.customer,
439 "customer_group": me.frm.doc.customer_group,
440 "territory": me.frm.doc.territory,
441 "supplier": me.frm.doc.supplier,
442 "supplier_type": me.frm.doc.supplier_type,
443 "currency": me.frm.doc.currency,
444 "conversion_rate": me.frm.doc.conversion_rate,
445 "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
446 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
447 "company": me.frm.doc.company,
448 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
449 "campaign": me.frm.doc.campaign,
450 "sales_partner": me.frm.doc.sales_partner,
451 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
452 "parenttype": me.frm.doc.doctype,
453 "parent": me.frm.doc.name
454 };
Nabin Hait444f9562014-06-20 15:59:49 +0530455 },
456
457 _get_item_list: function(item) {
458 var item_list = [];
459 var append_item = function(d) {
Anand Doshi39edcbc2014-07-28 16:00:19 +0530460 if (d.item_code) {
461 item_list.push({
462 "doctype": d.doctype,
463 "name": d.name,
464 "item_code": d.item_code,
465 "item_group": d.item_group,
466 "brand": d.brand,
467 "qty": d.qty
468 });
469 }
Nabin Hait444f9562014-06-20 15:59:49 +0530470 };
Nabin Haita3dd72a2014-05-28 12:49:20 +0530471
Nabin Hait444f9562014-06-20 15:59:49 +0530472 if (item) {
473 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +0530474 } else {
Nabin Hait96339342015-01-21 17:22:11 +0530475 $.each(this.frm.doc["items"] || [], function(i, d) {
Nabin Hait444f9562014-06-20 15:59:49 +0530476 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530477 });
478 }
Nabin Hait444f9562014-06-20 15:59:49 +0530479 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +0530480 },
481
Anand Doshidffec8f2014-07-01 17:45:15 +0530482 _set_values_for_item_list: function(children) {
Nabin Hait49a27292014-12-17 11:21:32 +0530483 var me = this;
Nabin Hait7d8fa802014-12-30 15:35:45 +0530484 var price_list_rate_changed = false;
Anand Doshidea3fb02015-02-05 15:05:58 +0530485 for(var i=0, l=children.length; i<l; i++) {
486 var d = children[i];
Nabin Hait49a27292014-12-17 11:21:32 +0530487 var existing_pricing_rule = frappe.model.get_value(d.doctype, d.name, "pricing_rule");
Anand Doshidea3fb02015-02-05 15:05:58 +0530488
489 for(var k in d) {
490 var v = d[k];
Anand Doshidffec8f2014-07-01 17:45:15 +0530491 if (["doctype", "name"].indexOf(k)===-1) {
Nabin Hait7d8fa802014-12-30 15:35:45 +0530492 if(k=="price_list_rate") {
493 if(flt(v) != flt(d.price_list_rate)) price_list_rate_changed = true;
494 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530495 frappe.model.set_value(d.doctype, d.name, k, v);
496 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530497 }
498
Nabin Hait49a27292014-12-17 11:21:32 +0530499 // if pricing rule set as blank from an existing value, apply price_list
Nabin Haitcfe3c542014-12-22 10:42:48 +0530500 if(!me.frm.doc.ignore_pricing_rule && existing_pricing_rule && !d.pricing_rule) {
Nabin Hait49a27292014-12-17 11:21:32 +0530501 me.apply_price_list(frappe.get_doc(d.doctype, d.name));
502 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530503 }
Nabin Hait7d8fa802014-12-30 15:35:45 +0530504
Anand Doshidea3fb02015-02-05 15:05:58 +0530505 if(!price_list_rate_changed) me.calculate_taxes_and_totals();
Anand Doshidffec8f2014-07-01 17:45:15 +0530506 },
507
Nabin Hait49a27292014-12-17 11:21:32 +0530508 apply_price_list: function(item) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530509 var me = this;
Rushabh Mehta052bd622015-01-30 16:57:24 +0530510 var args = this._get_args(item);
Nabin Hait37b047d2015-02-23 16:01:33 +0530511
Anand Doshidffec8f2014-07-01 17:45:15 +0530512 return this.frm.call({
513 method: "erpnext.stock.get_item_details.apply_price_list",
Rushabh Mehta052bd622015-01-30 16:57:24 +0530514 args: { args: args },
Anand Doshidffec8f2014-07-01 17:45:15 +0530515 callback: function(r) {
516 if (!r.exc) {
Anand Doshi51f722d2014-07-04 15:44:26 +0530517 me.in_apply_price_list = true;
Anand Doshidffec8f2014-07-01 17:45:15 +0530518 me.frm.set_value("price_list_currency", r.message.parent.price_list_currency);
519 me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate);
Anand Doshi51f722d2014-07-04 15:44:26 +0530520 me.in_apply_price_list = false;
Nabin Hait37b047d2015-02-23 16:01:33 +0530521
522 if(args.item_list.length) {
523 me._set_values_for_item_list(r.message.children);
524 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530525 }
526 }
527 });
528 },
529
Anand Doshi3543f302013-05-24 19:25:01 +0530530 get_item_wise_taxes_html: function() {
531 var item_tax = {};
532 var tax_accounts = [];
533 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530534
Nabin Hait96339342015-01-21 17:22:11 +0530535 $.each(this.frm.doc["taxes"] || [], function(i, tax) {
Anand Doshi3543f302013-05-24 19:25:01 +0530536 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530537 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530538 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530539 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530540 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530541 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530542 var tax_rate = "";
543 if(tax_data[0] != null) {
544 tax_rate = (tax.charge_type === "Actual") ?
545 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
546 (flt(tax_data[0], tax_rate_precision) + "%");
547 }
548 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
549 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530550
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530551 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530552 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530553 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530554 }
Anand Doshi3543f302013-05-24 19:25:01 +0530555 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530556 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530557 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530558
559 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530560 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530561
Anand Doshi179e3772013-09-05 16:53:57 +0530562 var distinct_item_names = [];
563 var distinct_items = [];
Nabin Hait96339342015-01-21 17:22:11 +0530564 $.each(this.frm.doc["items"] || [], function(i, item) {
Anand Doshi179e3772013-09-05 16:53:57 +0530565 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
566 distinct_item_names.push(item.item_code || item.item_name);
567 distinct_items.push(item);
568 }
569 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530570
Anand Doshi179e3772013-09-05 16:53:57 +0530571 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530572 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530573 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530574 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
575 item_name: item.item_name,
576 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530577 return item_tax_record[head[0]] ?
578 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530579 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530580 }).join("\n")
581 });
582 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530583
Anand Doshi53b73422013-05-31 11:50:01 +0530584 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530585 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
586 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530587 <thead><tr>' + headings + '</tr></thead> \
588 <tbody>' + rows + '</tbody> \
589 </table></div>';
590 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530591
Nabin Hait139dc7b2014-02-12 14:53:18 +0530592 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530593 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530594 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530595
Nabin Hait0f231062014-02-20 11:27:47 +0530596 $.each(["company", "customer"], function(i, fieldname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530597 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname)) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530598 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530599 msgprint(__("Please specify") + ": " +
600 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530601 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530602 valid = false;
603 }
Anand Doshi3543f302013-05-24 19:25:01 +0530604 }
605 });
606 return valid;
607 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530608
Anand Doshibf3e54a2013-06-05 14:11:32 +0530609 get_terms: function() {
610 var me = this;
611 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530612 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530613 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530614 args: {
615 doctype: "Terms and Conditions",
616 fieldname: "terms",
617 filters: { name: this.frm.doc.tc_name },
618 },
619 });
620 }
621 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530622
623 taxes_and_charges: function() {
624 var me = this;
625 if(this.frm.doc.taxes_and_charges) {
626 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530627 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
628 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530629 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
630 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530631 "master_name": this.frm.doc.taxes_and_charges,
Nabin Haitdd38a262014-12-26 13:15:21 +0530632 "tax_parentfield": "taxes"
Nabin Hait6b039142014-05-02 15:45:10 +0530633 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530634 callback: function(r) {
635 if(!r.exc) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530636 me.frm.set_value("taxes", r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530637 me.calculate_taxes_and_totals();
638 }
639 }
640 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530641 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530642 },
643
644 show_item_wise_taxes: function() {
645 if(this.frm.fields_dict.other_charges_calculation) {
Anand Doshidd942bb2015-02-04 18:52:35 +0530646 var html = this.get_item_wise_taxes_html();
647 if (html) {
648 this.frm.toggle_display("other_charges_calculation", true);
649 $(this.frm.fields_dict.other_charges_calculation.wrapper).html(html);
650 } else {
651 this.frm.toggle_display("other_charges_calculation", false);
652 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530653 }
654 },
Anand Doshi13945092014-09-21 19:45:49 +0530655
656 is_recurring: function() {
657 // set default values for recurring documents
658 if(this.frm.doc.is_recurring) {
659 var owner_email = this.frm.doc.owner=="Administrator"
660 ? frappe.user_info("Administrator").email
661 : this.frm.doc.owner;
662
663 this.frm.doc.notification_email_address = $.map([cstr(owner_email),
664 cstr(this.frm.doc.contact_email)], function(v) { return v || null; }).join(", ");
665 this.frm.doc.repeat_on_day_of_month = frappe.datetime.str_to_obj(this.frm.doc.posting_date).getDate();
666 }
667
668 refresh_many(["notification_email_address", "repeat_on_day_of_month"]);
669 },
670
671 from_date: function() {
672 // set to_date
673 if(this.frm.doc.from_date) {
674 var recurring_type_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6,
675 'Yearly': 12};
676
677 var months = recurring_type_map[this.frm.doc.recurring_type];
678 if(months) {
679 var to_date = frappe.datetime.add_months(this.frm.doc.from_date,
680 months);
681 this.frm.doc.to_date = frappe.datetime.add_days(to_date, -1);
682 refresh_field('to_date');
683 }
684 }
685 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530686});
Nabin Haitce245122015-02-22 20:14:49 +0530687
Nabin Hait93b3a372015-02-24 17:08:34 +0530688frappe.ui.form.on(cur_frm.doctype + " Item", "rate", function(frm, cdt, cdn) {
Nabin Haitce245122015-02-22 20:14:49 +0530689 var item = frappe.get_doc(cdt, cdn);
690 frappe.model.round_floats_in(item, ["rate", "price_list_rate"]);
691
692 if(item.price_list_rate) {
693 item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0, precision("discount_percentage", item));
694 } else {
695 item.discount_percentage = 0.0;
696 }
697
698 cur_frm.cscript.calculate_taxes_and_totals();
699})
Nabin Hait613d0812015-02-23 11:58:15 +0530700
701frappe.ui.form.on(cur_frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
702 cur_frm.cscript.calculate_taxes_and_totals();
703})
704
705frappe.ui.form.on(cur_frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
706 cur_frm.cscript.calculate_taxes_and_totals();
707})
708
709frappe.ui.form.on(cur_frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
710 cur_frm.cscript.calculate_taxes_and_totals();
711})
712
713frappe.ui.form.on(cur_frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
Nabin Hait903c42a2015-02-24 15:24:49 +0530714 cur_frm.cscript.set_dynamic_labels();
Nabin Hait613d0812015-02-23 11:58:15 +0530715 cur_frm.cscript.calculate_taxes_and_totals();
716})
Nabin Hait37b047d2015-02-23 16:01:33 +0530717
718frappe.ui.form.on(cur_frm.doctype, "apply_discount_on", function(frm) {
719 cur_frm.cscript.calculate_taxes_and_totals();
720})
721
722frappe.ui.form.on(cur_frm.doctype, "discount_amount", function(frm) {
Nabin Hait903c42a2015-02-24 15:24:49 +0530723 cur_frm.cscript.set_dynamic_labels();
Nabin Hait37b047d2015-02-23 16:01:33 +0530724 cur_frm.cscript.calculate_taxes_and_totals();
725})