blob: b4965ee7b2dc799c48aa015dd7bcd1f4064b80e4 [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 }
Rushabh Mehta4a3c8052015-05-19 11:10:12 +053048
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 },
Rushabh Mehta4a3c8052015-05-19 11:10:12 +053066
Nabin Haitffc7f3f2015-05-12 15:07:02 +053067 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;
Rushabh Mehta4a3c8052015-05-19 11:10:12 +053086 if(this.frm.doc.docstatus===1 && !in_list(["Lost", "Stopped"], this.frm.doc.status)
87 && this.frm.doctype != "Purchase Invoice") {
Rushabh Mehta614e7ab2015-02-18 19:51:48 +053088 this.frm.page.add_menu_item(__('Send SMS'), function() { me.send_sms(); });
89 }
90 },
91
92 send_sms: function() {
93 frappe.require("assets/erpnext/js/sms_manager.js");
Rushabh Mehta4a3c8052015-05-19 11:10:12 +053094 var sms_man = new SMSManager(this.frm.doc);
Nabin Hait239c2c32015-01-15 14:21:41 +053095 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +053096
Nabin Hait239c2c32015-01-15 14:21:41 +053097 hide_currency_and_price_list: function() {
98 if(this.frm.doc.docstatus > 0) {
99 hide_field("currency_and_price_list");
100 } else {
101 unhide_field("currency_and_price_list");
102 }
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530103 },
104
Nabin Hait139dc7b2014-02-12 14:53:18 +0530105 item_code: function(doc, cdt, cdn) {
106 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530107 var item = frappe.get_doc(cdt, cdn);
Nabin Hait139dc7b2014-02-12 14:53:18 +0530108 if(item.item_code || item.barcode || item.serial_no) {
109 if(!this.validate_company_and_party()) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530110 cur_frm.fields_dict["items"].grid.grid_rows[item.idx - 1].remove();
Nabin Hait139dc7b2014-02-12 14:53:18 +0530111 } else {
112 return this.frm.call({
113 method: "erpnext.stock.get_item_details.get_item_details",
114 child: item,
115 args: {
116 args: {
117 item_code: item.item_code,
118 barcode: item.barcode,
119 serial_no: item.serial_no,
120 warehouse: item.warehouse,
Nabin Hait444f9562014-06-20 15:59:49 +0530121 parenttype: me.frm.doc.doctype,
122 parent: me.frm.doc.name,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530123 customer: me.frm.doc.customer,
124 supplier: me.frm.doc.supplier,
125 currency: me.frm.doc.currency,
126 conversion_rate: me.frm.doc.conversion_rate,
127 price_list: me.frm.doc.selling_price_list ||
128 me.frm.doc.buying_price_list,
129 price_list_currency: me.frm.doc.price_list_currency,
130 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
131 company: me.frm.doc.company,
132 order_type: me.frm.doc.order_type,
133 is_pos: cint(me.frm.doc.is_pos),
134 is_subcontracted: me.frm.doc.is_subcontracted,
Nabin Haita1bf43b2015-03-17 10:50:47 +0530135 transaction_date: me.frm.doc.transaction_date || me.frm.doc.posting_date,
Nabin Hait444f9562014-06-20 15:59:49 +0530136 ignore_pricing_rule: me.frm.doc.ignore_pricing_rule,
137 doctype: item.doctype,
Anand Doshi43f087c2014-08-26 14:25:53 +0530138 name: item.name,
Anand Doshi9b955fe2015-01-05 16:19:12 +0530139 project_name: item.project_name || me.frm.doc.project_name,
140 qty: item.qty
Nabin Hait139dc7b2014-02-12 14:53:18 +0530141 }
142 },
Anand Doshi13945092014-09-21 19:45:49 +0530143
Nabin Hait139dc7b2014-02-12 14:53:18 +0530144 callback: function(r) {
145 if(!r.exc) {
146 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
147 }
148 }
149 });
150 }
151 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530152 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530153
154 serial_no: function(doc, cdt, cdn) {
155 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530156 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530157
158 if (item.serial_no) {
159 if (!item.item_code) {
160 this.frm.script_manager.trigger("item_code", cdt, cdn);
161 }
162 else {
163 var sr_no = [];
164
165 // Replacing all occurences of comma with carriage return
166 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
167
168 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530169
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530170 // Trim each string and push unique string to new list
171 for (var x=0; x<=serial_nos.length - 1; x++) {
172 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
173 sr_no.push(serial_nos[x].trim());
174 }
175 }
176
177 // Add the new list to the serial no. field in grid with each in new line
178 item.serial_no = "";
179 for (var x=0; x<=sr_no.length - 1; x++)
180 item.serial_no += sr_no[x] + '\n';
181
182 refresh_field("serial_no", item.name, item.parentfield);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530183 frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530184 }
185 }
186 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530187
Anand Doshi923d41d2013-05-28 17:23:36 +0530188 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530189 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530190 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530191
Anand Doshi3543f302013-05-24 19:25:01 +0530192 company: function() {
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530193 var me = this;
194 var fn = function() {
195 if(me.frm.doc.company && me.frm.fields_dict.currency) {
196 var company_currency = me.get_company_currency();
197 if (!me.frm.doc.currency) {
198 me.frm.set_value("currency", company_currency);
199 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530200
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530201 if (me.frm.doc.currency == company_currency) {
202 me.frm.set_value("conversion_rate", 1.0);
203 }
204 if (me.frm.doc.price_list_currency == company_currency) {
205 me.frm.set_value('plc_conversion_rate', 1.0);
206 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530207
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530208 me.frm.script_manager.trigger("currency");
209 me.apply_pricing_rule();
Nabin Haitdd38a262014-12-26 13:15:21 +0530210 }
Anand Doshi3543f302013-05-24 19:25:01 +0530211 }
Nabin Haitdd38a262014-12-26 13:15:21 +0530212
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530213 if (this.frm.doc.posting_date) var date = this.frm.doc.posting_date;
214 else var date = this.frm.doc.transaction_date;
215 erpnext.get_fiscal_year(this.frm.doc.company, date, fn);
Neil Trini Lasrado31a92bd2014-12-15 15:49:40 +0530216 erpnext.get_letter_head(this.frm.doc.company);
Rushabh Mehta45ca8a42015-04-28 16:02:09 +0530217
218 if(this.frm.doc.company) {
219 erpnext.last_selected_company = this.frm.doc.company;
220 }
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530221 },
222
223 transaction_date: function() {
224 erpnext.get_fiscal_year(this.frm.doc.company, this.frm.doc.transaction_date);
Neil Trini Lasrado78fa6952014-10-03 17:43:02 +0530225 },
226
227 posting_date: function() {
Neil Trini Lasrado565d9862014-10-07 18:26:39 +0530228 erpnext.get_fiscal_year(this.frm.doc.company, this.frm.doc.posting_date);
Anand Doshi3543f302013-05-24 19:25:01 +0530229 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530230
Anand Doshi3543f302013-05-24 19:25:01 +0530231 get_company_currency: function() {
232 return erpnext.get_currency(this.frm.doc.company);
233 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530234
Anand Doshi3543f302013-05-24 19:25:01 +0530235 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530236 var me = this;
237 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530238
Anand Doshi61a2f682013-06-21 17:55:31 +0530239 var company_currency = this.get_company_currency();
Nabin Hait69cdf592015-05-02 18:46:10 +0530240 // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
241 if(this.frm.doc.currency !== company_currency && !this.frm.doc.ignore_pricing_rule) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530242 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530243 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530244 me.frm.set_value("conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530245 });
246 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530247 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530248 }
249 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530250
Anand Doshi61a2f682013-06-21 17:55:31 +0530251 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530252 if(this.frm.doc.currency === this.get_company_currency()) {
253 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530254 }
255 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530256 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
257 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
258 }
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530259
Nabin Haita3dd72a2014-05-28 12:49:20 +0530260 if(flt(this.frm.doc.conversion_rate)>0.0) {
Nabin Hait444f9562014-06-20 15:59:49 +0530261 if(this.frm.doc.ignore_pricing_rule) {
262 this.calculate_taxes_and_totals();
Anand Doshi51f722d2014-07-04 15:44:26 +0530263 } else if (!this.in_apply_price_list){
264 this.apply_price_list();
Nabin Hait444f9562014-06-20 15:59:49 +0530265 }
266
Nabin Haita3dd72a2014-05-28 12:49:20 +0530267 }
Anand Doshi3543f302013-05-24 19:25:01 +0530268 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530269
Anand Doshi61a2f682013-06-21 17:55:31 +0530270 get_exchange_rate: function(from_currency, to_currency, callback) {
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +0530271 frappe.call({
272 method: "erpnext.setup.utils.get_exchange_rate",
273 args: {
274 from_currency: from_currency,
275 to_currency: to_currency
276 },
277 callback: function(r) {
278 callback(flt(r.message));
279 }
Anand Doshi61a2f682013-06-21 17:55:31 +0530280 });
281 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530282
Anand Doshi3543f302013-05-24 19:25:01 +0530283 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530284 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530285 this.set_dynamic_labels();
Nabin Haitec361ea2015-05-11 18:14:45 +0530286
287 var company_currency = this.get_company_currency();
288 // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
289 if(this.frm.doc.price_list_currency !== company_currency && !this.frm.doc.ignore_pricing_rule) {
290 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
291 function(exchange_rate) {
292 me.frm.set_value("plc_conversion_rate", exchange_rate);
293 });
294 } else {
295 this.plc_conversion_rate();
296 }
Anand Doshi3543f302013-05-24 19:25:01 +0530297 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530298
Anand Doshi3543f302013-05-24 19:25:01 +0530299 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530300 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
301 this.frm.set_value("plc_conversion_rate", 1.0);
Nabin Haitec361ea2015-05-11 18:14:45 +0530302 } 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 &&
303 cint(this.frm.doc.plc_conversion_rate) != cint(this.frm.doc.conversion_rate)) {
304 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530305 }
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530306
Nabin Haitec361ea2015-05-11 18:14:45 +0530307 if(!this.in_apply_price_list) {
308 this.apply_price_list();
Anand Doshi61a2f682013-06-21 17:55:31 +0530309 }
Anand Doshi3543f302013-05-24 19:25:01 +0530310 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530311
Anand Doshi3543f302013-05-24 19:25:01 +0530312 qty: function(doc, cdt, cdn) {
Nabin Hait444f9562014-06-20 15:59:49 +0530313 this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
Anand Doshi3543f302013-05-24 19:25:01 +0530314 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530315
Anand Doshi61a2f682013-06-21 17:55:31 +0530316 set_dynamic_labels: function() {
317 // What TODO? should we make price list system non-mandatory?
318 this.frm.toggle_reqd("plc_conversion_rate",
319 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530320
Anand Doshi61a2f682013-06-21 17:55:31 +0530321 var company_currency = this.get_company_currency();
322 this.change_form_labels(company_currency);
323 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530324 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530325 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530326
Nabin Hait613d0812015-02-23 11:58:15 +0530327 change_form_labels: function(company_currency) {
328 var me = this;
329 var field_label_map = {};
330
331 var setup_field_label_map = function(fields_list, currency) {
332 $.each(fields_list, function(i, fname) {
333 var docfield = frappe.meta.docfield_map[me.frm.doc.doctype][fname];
334 if(docfield) {
335 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
336 field_label_map[fname] = label.trim() + " (" + currency + ")";
337 }
338 });
339 };
340 setup_field_label_map(["base_total", "base_net_total", "base_total_taxes_and_charges",
341 "base_discount_amount", "base_grand_total", "base_rounded_total", "base_in_words",
342 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted", "total_amount_to_pay",
Nabin Hait37b047d2015-02-23 16:01:33 +0530343 "outstanding_amount", "total_advance", "paid_amount", "write_off_amount"], company_currency);
Nabin Hait613d0812015-02-23 11:58:15 +0530344
345 setup_field_label_map(["total", "net_total", "total_taxes_and_charges", "discount_amount",
346 "grand_total", "taxes_and_charges_added", "taxes_and_charges_deducted",
347 "rounded_total", "in_words"], this.frm.doc.currency);
348
349 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
350 + " = [?] " + company_currency)
351
352 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
353 cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency
354 + " = [?] " + company_currency)
355 }
356
357 // toggle fields
358 this.frm.toggle_display(["conversion_rate", "base_total", "base_net_total", "base_total_taxes_and_charges",
359 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted",
360 "base_grand_total", "base_rounded_total", "base_in_words", "base_discount_amount"],
361 this.frm.doc.currency != company_currency);
362
363 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
364 this.frm.doc.price_list_currency != company_currency);
365
366 // set labels
367 $.each(field_label_map, function(fname, label) {
368 me.frm.fields_dict[fname].set_label(label);
369 });
Nabin Hait37b047d2015-02-23 16:01:33 +0530370
Nabin Hait903c42a2015-02-24 15:24:49 +0530371 var show =cint(cur_frm.doc.discount_amount) ||
372 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
373
374 if(frappe.meta.get_docfield(cur_frm.doctype, "net_total"))
375 cur_frm.toggle_display("net_total", show);
376
377 if(frappe.meta.get_docfield(cur_frm.doctype, "base_net_total"))
378 cur_frm.toggle_display("base_net_total", (show && (me.frm.doc.currency != company_currency)));
379
Nabin Hait613d0812015-02-23 11:58:15 +0530380 },
381
382 change_grid_labels: function(company_currency) {
383 var me = this;
384 var field_label_map = {};
385
386 var setup_field_label_map = function(fields_list, currency, parentfield) {
387 var grid_doctype = me.frm.fields_dict[parentfield].grid.doctype;
388 $.each(fields_list, function(i, fname) {
389 var docfield = frappe.meta.docfield_map[grid_doctype][fname];
390 if(docfield) {
391 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
392 field_label_map[grid_doctype + "-" + fname] =
393 label.trim() + " (" + currency + ")";
394 }
395 });
396 }
397
398 setup_field_label_map(["base_rate", "base_net_rate", "base_price_list_rate", "base_amount", "base_net_amount"],
399 company_currency, "items");
400
401 setup_field_label_map(["rate", "net_rate", "price_list_rate", "amount", "net_amount"],
402 this.frm.doc.currency, "items");
403
404 if(this.frm.fields_dict["taxes"]) {
405 setup_field_label_map(["tax_amount", "total", "tax_amount_after_discount"], this.frm.doc.currency, "taxes");
406
407 setup_field_label_map(["base_tax_amount", "base_total", "base_tax_amount_after_discount"], company_currency, "taxes");
408 }
409
410 if(this.frm.fields_dict["advances"]) {
411 setup_field_label_map(["advance_amount", "allocated_amount"], company_currency, "advances");
412 }
413
414 // toggle columns
415 var item_grid = this.frm.fields_dict["items"].grid;
416 $.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) {
417 if(frappe.meta.get_docfield(item_grid.doctype, fname))
418 item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency);
419 });
420
421 var show = (cint(cur_frm.doc.discount_amount)) ||
422 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
423
424 $.each(["net_rate", "net_amount"], function(i, fname) {
425 if(frappe.meta.get_docfield(item_grid.doctype, fname))
426 item_grid.set_column_disp(fname, show);
427 });
428
429 $.each(["base_net_rate", "base_net_amount"], function(i, fname) {
430 if(frappe.meta.get_docfield(item_grid.doctype, fname))
431 item_grid.set_column_disp(fname, (show && (me.frm.doc.currency != company_currency)));
432 });
433
434 // set labels
435 var $wrapper = $(this.frm.wrapper);
436 $.each(field_label_map, function(fname, label) {
437 fname = fname.split("-");
438 var df = frappe.meta.get_docfield(fname[0], fname[1], me.frm.doc.name);
439 if(df) df.label = label;
440 });
441 },
442
Anand Doshi923d41d2013-05-28 17:23:36 +0530443 recalculate: function() {
444 this.calculate_taxes_and_totals();
445 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530446
Anand Doshi923d41d2013-05-28 17:23:36 +0530447 recalculate_values: function() {
448 this.calculate_taxes_and_totals();
449 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530450
Anand Doshid0b00722013-05-28 18:54:48 +0530451 calculate_charges: function() {
452 this.calculate_taxes_and_totals();
453 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530454
Nabin Hait444f9562014-06-20 15:59:49 +0530455 ignore_pricing_rule: function() {
456 this.apply_pricing_rule();
457 },
458
459 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530460 var me = this;
Anand Doshidffec8f2014-07-01 17:45:15 +0530461 return this.frm.call({
462 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
463 args: { args: this._get_args(item) },
464 callback: function(r) {
Anand Doshif214bfc2014-07-16 17:52:08 +0530465 if (!r.exc && r.message) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530466 me._set_values_for_item_list(r.message);
467 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
468 }
469 }
470 });
471 },
472
473 _get_args: function(item) {
474 var me = this;
475 return {
476 "item_list": this._get_item_list(item),
Nabin Hait444f9562014-06-20 15:59:49 +0530477 "customer": me.frm.doc.customer,
478 "customer_group": me.frm.doc.customer_group,
479 "territory": me.frm.doc.territory,
480 "supplier": me.frm.doc.supplier,
481 "supplier_type": me.frm.doc.supplier_type,
482 "currency": me.frm.doc.currency,
483 "conversion_rate": me.frm.doc.conversion_rate,
484 "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
Nabin Haitdd82bf22015-05-11 16:49:17 +0530485 "price_list_currency": me.frm.doc.price_list_currency,
Nabin Hait444f9562014-06-20 15:59:49 +0530486 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
487 "company": me.frm.doc.company,
488 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
489 "campaign": me.frm.doc.campaign,
490 "sales_partner": me.frm.doc.sales_partner,
491 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
492 "parenttype": me.frm.doc.doctype,
493 "parent": me.frm.doc.name
494 };
Nabin Hait444f9562014-06-20 15:59:49 +0530495 },
496
497 _get_item_list: function(item) {
498 var item_list = [];
499 var append_item = function(d) {
Anand Doshi39edcbc2014-07-28 16:00:19 +0530500 if (d.item_code) {
501 item_list.push({
502 "doctype": d.doctype,
503 "name": d.name,
504 "item_code": d.item_code,
505 "item_group": d.item_group,
506 "brand": d.brand,
507 "qty": d.qty
508 });
509 }
Nabin Hait444f9562014-06-20 15:59:49 +0530510 };
Nabin Haita3dd72a2014-05-28 12:49:20 +0530511
Nabin Hait444f9562014-06-20 15:59:49 +0530512 if (item) {
513 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +0530514 } else {
Nabin Hait96339342015-01-21 17:22:11 +0530515 $.each(this.frm.doc["items"] || [], function(i, d) {
Nabin Hait444f9562014-06-20 15:59:49 +0530516 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530517 });
518 }
Nabin Hait444f9562014-06-20 15:59:49 +0530519 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +0530520 },
521
Anand Doshidffec8f2014-07-01 17:45:15 +0530522 _set_values_for_item_list: function(children) {
Nabin Hait49a27292014-12-17 11:21:32 +0530523 var me = this;
Nabin Hait7d8fa802014-12-30 15:35:45 +0530524 var price_list_rate_changed = false;
Anand Doshidea3fb02015-02-05 15:05:58 +0530525 for(var i=0, l=children.length; i<l; i++) {
526 var d = children[i];
Nabin Hait49a27292014-12-17 11:21:32 +0530527 var existing_pricing_rule = frappe.model.get_value(d.doctype, d.name, "pricing_rule");
Anand Doshidea3fb02015-02-05 15:05:58 +0530528
529 for(var k in d) {
530 var v = d[k];
Anand Doshidffec8f2014-07-01 17:45:15 +0530531 if (["doctype", "name"].indexOf(k)===-1) {
Nabin Hait7d8fa802014-12-30 15:35:45 +0530532 if(k=="price_list_rate") {
533 if(flt(v) != flt(d.price_list_rate)) price_list_rate_changed = true;
534 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530535 frappe.model.set_value(d.doctype, d.name, k, v);
536 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530537 }
538
Nabin Hait49a27292014-12-17 11:21:32 +0530539 // if pricing rule set as blank from an existing value, apply price_list
Nabin Haitcfe3c542014-12-22 10:42:48 +0530540 if(!me.frm.doc.ignore_pricing_rule && existing_pricing_rule && !d.pricing_rule) {
Nabin Hait49a27292014-12-17 11:21:32 +0530541 me.apply_price_list(frappe.get_doc(d.doctype, d.name));
542 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530543 }
Nabin Hait7d8fa802014-12-30 15:35:45 +0530544
Anand Doshidea3fb02015-02-05 15:05:58 +0530545 if(!price_list_rate_changed) me.calculate_taxes_and_totals();
Anand Doshidffec8f2014-07-01 17:45:15 +0530546 },
547
Nabin Hait49a27292014-12-17 11:21:32 +0530548 apply_price_list: function(item) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530549 var me = this;
Rushabh Mehta052bd622015-01-30 16:57:24 +0530550 var args = this._get_args(item);
Nabin Hait37b047d2015-02-23 16:01:33 +0530551
Anand Doshidffec8f2014-07-01 17:45:15 +0530552 return this.frm.call({
553 method: "erpnext.stock.get_item_details.apply_price_list",
Rushabh Mehta052bd622015-01-30 16:57:24 +0530554 args: { args: args },
Anand Doshidffec8f2014-07-01 17:45:15 +0530555 callback: function(r) {
556 if (!r.exc) {
Anand Doshi51f722d2014-07-04 15:44:26 +0530557 me.in_apply_price_list = true;
Anand Doshidffec8f2014-07-01 17:45:15 +0530558 me.frm.set_value("price_list_currency", r.message.parent.price_list_currency);
559 me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate);
Anand Doshi51f722d2014-07-04 15:44:26 +0530560 me.in_apply_price_list = false;
Nabin Hait37b047d2015-02-23 16:01:33 +0530561
562 if(args.item_list.length) {
563 me._set_values_for_item_list(r.message.children);
564 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530565 }
566 }
567 });
568 },
569
Anand Doshi3543f302013-05-24 19:25:01 +0530570 get_item_wise_taxes_html: function() {
571 var item_tax = {};
572 var tax_accounts = [];
573 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530574
Nabin Hait96339342015-01-21 17:22:11 +0530575 $.each(this.frm.doc["taxes"] || [], function(i, tax) {
Anand Doshi3543f302013-05-24 19:25:01 +0530576 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530577 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530578 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530579 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530580 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530581 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530582 var tax_rate = "";
583 if(tax_data[0] != null) {
584 tax_rate = (tax.charge_type === "Actual") ?
585 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
586 (flt(tax_data[0], tax_rate_precision) + "%");
587 }
588 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
589 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530590
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530591 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530592 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530593 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530594 }
Anand Doshi3543f302013-05-24 19:25:01 +0530595 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530596 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530597 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530598
599 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530600 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530601
Anand Doshi179e3772013-09-05 16:53:57 +0530602 var distinct_item_names = [];
603 var distinct_items = [];
Nabin Hait96339342015-01-21 17:22:11 +0530604 $.each(this.frm.doc["items"] || [], function(i, item) {
Anand Doshi179e3772013-09-05 16:53:57 +0530605 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
606 distinct_item_names.push(item.item_code || item.item_name);
607 distinct_items.push(item);
608 }
609 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530610
Anand Doshi179e3772013-09-05 16:53:57 +0530611 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530612 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530613 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530614 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
615 item_name: item.item_name,
616 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530617 return item_tax_record[head[0]] ?
618 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530619 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530620 }).join("\n")
621 });
622 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530623
Anand Doshi53b73422013-05-31 11:50:01 +0530624 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530625 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
626 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530627 <thead><tr>' + headings + '</tr></thead> \
628 <tbody>' + rows + '</tbody> \
629 </table></div>';
630 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530631
Nabin Hait139dc7b2014-02-12 14:53:18 +0530632 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530633 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530634 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530635
Nabin Hait0f231062014-02-20 11:27:47 +0530636 $.each(["company", "customer"], function(i, fieldname) {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530637 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname)) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530638 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530639 msgprint(__("Please specify") + ": " +
640 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530641 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530642 valid = false;
643 }
Anand Doshi3543f302013-05-24 19:25:01 +0530644 }
645 });
646 return valid;
647 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530648
Anand Doshibf3e54a2013-06-05 14:11:32 +0530649 get_terms: function() {
650 var me = this;
651 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530652 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530653 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530654 args: {
655 doctype: "Terms and Conditions",
656 fieldname: "terms",
657 filters: { name: this.frm.doc.tc_name },
658 },
659 });
660 }
661 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530662
663 taxes_and_charges: function() {
664 var me = this;
665 if(this.frm.doc.taxes_and_charges) {
666 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530667 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
668 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530669 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
670 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530671 "master_name": this.frm.doc.taxes_and_charges,
Nabin Hait6b039142014-05-02 15:45:10 +0530672 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530673 callback: function(r) {
674 if(!r.exc) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530675 me.frm.set_value("taxes", r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530676 me.calculate_taxes_and_totals();
677 }
678 }
679 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530680 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530681 },
682
683 show_item_wise_taxes: function() {
684 if(this.frm.fields_dict.other_charges_calculation) {
Anand Doshidd942bb2015-02-04 18:52:35 +0530685 var html = this.get_item_wise_taxes_html();
686 if (html) {
687 this.frm.toggle_display("other_charges_calculation", true);
688 $(this.frm.fields_dict.other_charges_calculation.wrapper).html(html);
689 } else {
690 this.frm.toggle_display("other_charges_calculation", false);
691 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530692 }
693 },
Anand Doshi13945092014-09-21 19:45:49 +0530694
695 is_recurring: function() {
696 // set default values for recurring documents
697 if(this.frm.doc.is_recurring) {
698 var owner_email = this.frm.doc.owner=="Administrator"
699 ? frappe.user_info("Administrator").email
700 : this.frm.doc.owner;
701
702 this.frm.doc.notification_email_address = $.map([cstr(owner_email),
703 cstr(this.frm.doc.contact_email)], function(v) { return v || null; }).join(", ");
704 this.frm.doc.repeat_on_day_of_month = frappe.datetime.str_to_obj(this.frm.doc.posting_date).getDate();
705 }
706
707 refresh_many(["notification_email_address", "repeat_on_day_of_month"]);
708 },
709
710 from_date: function() {
711 // set to_date
712 if(this.frm.doc.from_date) {
713 var recurring_type_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6,
714 'Yearly': 12};
715
716 var months = recurring_type_map[this.frm.doc.recurring_type];
717 if(months) {
718 var to_date = frappe.datetime.add_months(this.frm.doc.from_date,
719 months);
720 this.frm.doc.to_date = frappe.datetime.add_days(to_date, -1);
721 refresh_field('to_date');
722 }
723 }
724 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530725});
Nabin Haitce245122015-02-22 20:14:49 +0530726
Nabin Hait93b3a372015-02-24 17:08:34 +0530727frappe.ui.form.on(cur_frm.doctype + " Item", "rate", function(frm, cdt, cdn) {
Nabin Haitce245122015-02-22 20:14:49 +0530728 var item = frappe.get_doc(cdt, cdn);
729 frappe.model.round_floats_in(item, ["rate", "price_list_rate"]);
730
731 if(item.price_list_rate) {
732 item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0, precision("discount_percentage", item));
733 } else {
734 item.discount_percentage = 0.0;
735 }
736
737 cur_frm.cscript.calculate_taxes_and_totals();
738})
Nabin Hait613d0812015-02-23 11:58:15 +0530739
740frappe.ui.form.on(cur_frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
741 cur_frm.cscript.calculate_taxes_and_totals();
742})
743
744frappe.ui.form.on(cur_frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
745 cur_frm.cscript.calculate_taxes_and_totals();
746})
747
748frappe.ui.form.on(cur_frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
749 cur_frm.cscript.calculate_taxes_and_totals();
750})
751
752frappe.ui.form.on(cur_frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
Nabin Hait903c42a2015-02-24 15:24:49 +0530753 cur_frm.cscript.set_dynamic_labels();
Nabin Hait613d0812015-02-23 11:58:15 +0530754 cur_frm.cscript.calculate_taxes_and_totals();
755})
Nabin Hait37b047d2015-02-23 16:01:33 +0530756
757frappe.ui.form.on(cur_frm.doctype, "apply_discount_on", function(frm) {
758 cur_frm.cscript.calculate_taxes_and_totals();
759})
760
761frappe.ui.form.on(cur_frm.doctype, "discount_amount", function(frm) {
Nabin Hait903c42a2015-02-24 15:24:49 +0530762 cur_frm.cscript.set_dynamic_labels();
Nabin Hait37b047d2015-02-23 16:01:33 +0530763 cur_frm.cscript.calculate_taxes_and_totals();
764})