blob: 34cbbae31bdc8c171a21cd892bd02f0799b5c8a7 [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
Nabin Haitb4a3dfa2015-01-22 17:13:13 +05304erpnext.TransactionController = erpnext.taxes_and_totals.extend({
Rushabh Mehtabe2ee182016-04-29 17:22:42 +05305 setup: function() {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +05306 this._super();
Rushabh Mehtabe2ee182016-04-29 17:22:42 +05307 frappe.ui.form.on(this.frm.doctype + " Item", "rate", function(frm, cdt, cdn) {
8 var item = frappe.get_doc(cdt, cdn);
9 frappe.model.round_floats_in(item, ["rate", "price_list_rate"]);
10
11 if(item.price_list_rate) {
12 item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0, precision("discount_percentage", item));
13 } else {
14 item.discount_percentage = 0.0;
15 }
16
17 cur_frm.cscript.set_gross_profit(item);
18 cur_frm.cscript.calculate_taxes_and_totals();
19 })
20
21 frappe.ui.form.on(this.frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
22 cur_frm.cscript.calculate_taxes_and_totals();
Rushabh Mehta764aa922016-05-02 13:28:46 +053023 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053024
25 frappe.ui.form.on(this.frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
26 cur_frm.cscript.calculate_taxes_and_totals();
Rushabh Mehta764aa922016-05-02 13:28:46 +053027 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053028
29 frappe.ui.form.on(this.frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
30 cur_frm.cscript.calculate_taxes_and_totals();
Rushabh Mehta764aa922016-05-02 13:28:46 +053031 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053032
33 frappe.ui.form.on(this.frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
34 cur_frm.cscript.set_dynamic_labels();
35 cur_frm.cscript.calculate_taxes_and_totals();
Rushabh Mehta764aa922016-05-02 13:28:46 +053036 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053037
38 frappe.ui.form.on(this.frm.doctype, "apply_discount_on", function(frm) {
39 if(frm.doc.additional_discount_percentage) {
40 frm.trigger("additional_discount_percentage");
41 } else {
42 cur_frm.cscript.calculate_taxes_and_totals();
43 }
Rushabh Mehta764aa922016-05-02 13:28:46 +053044 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053045
46 frappe.ui.form.on(this.frm.doctype, "additional_discount_percentage", function(frm) {
47 if (frm.via_discount_amount) {
48 return;
49 }
50
51 if(!frm.doc.apply_discount_on) {
52 frappe.msgprint(__("Please set 'Apply Additional Discount On'"));
53 return
54 }
55
56 frm.via_discount_percentage = true;
57
58 if(frm.doc.additional_discount_percentage && frm.doc.discount_amount) {
59 // Reset discount amount and net / grand total
60 frm.set_value("discount_amount", 0);
61 }
62
63 var total = flt(frm.doc[frappe.model.scrub(frm.doc.apply_discount_on)]);
64 var discount_amount = flt(total*flt(frm.doc.additional_discount_percentage) / 100,
65 precision("discount_amount"));
66
67 frm.set_value("discount_amount", discount_amount);
68 delete frm.via_discount_percentage;
69 });
70
71 frappe.ui.form.on(this.frm.doctype, "discount_amount", function(frm) {
72 frm.cscript.set_dynamic_labels();
73
74 if (!frm.via_discount_percentage) {
75 frm.via_discount_amount = true;
76 frm.set_value("additional_discount_percentage", 0);
77 delete frm.via_discount_amount;
78 }
79
80 frm.cscript.calculate_taxes_and_totals();
81 });
82
83 },
Anand Doshi3543f302013-05-24 19:25:01 +053084 onload: function() {
Anand Doshi1e4fb682013-08-23 12:56:33 +053085 var me = this;
Rushabh Mehta8c9c57c2016-04-13 18:22:06 +053086 //this.frm.show_print_first = true;
Anand Doshi3543f302013-05-24 19:25:01 +053087 if(this.frm.doc.__islocal) {
Anand Doshi1e4fb682013-08-23 12:56:33 +053088 var today = get_today(),
Nabin Hait73083dc2014-03-14 15:10:42 +053089 currency = frappe.defaults.get_user_default("currency");
Rushabh Mehtaff938022014-04-15 18:40:00 +053090
Anand Doshi3543f302013-05-24 19:25:01 +053091 $.each({
Anand Doshifc777182013-05-27 19:29:07 +053092 currency: currency,
93 price_list_currency: currency,
94 status: "Draft",
Anand Doshifc777182013-05-27 19:29:07 +053095 is_subcontracted: "No",
Anand Doshi3543f302013-05-24 19:25:01 +053096 }, function(fieldname, value) {
97 if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname])
98 me.frm.set_value(fieldname, value);
Rushabh Mehtaff938022014-04-15 18:40:00 +053099 });
nabinhaitbc408d82014-07-07 16:51:52 +0530100
nabinhaitb7392642014-07-28 14:55:08 +0530101 if(this.frm.doc.company && !this.frm.doc.amended_from) {
Rushabh Mehta78b7a532016-04-06 15:29:38 +0530102 this.frm.script_manager.trigger("company");
nabinhaitbc408d82014-07-07 16:51:52 +0530103 }
Anand Doshi3543f302013-05-24 19:25:01 +0530104 }
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530105
Nabin Haitdd38a262014-12-26 13:15:21 +0530106 if(this.frm.fields_dict["taxes"]) {
107 this["taxes_remove"] = this.calculate_taxes_and_totals;
Anand Doshia91c95f2013-08-23 13:00:47 +0530108 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530109
Nabin Haitdd38a262014-12-26 13:15:21 +0530110 if(this.frm.fields_dict["items"]) {
111 this["items_remove"] = this.calculate_taxes_and_totals;
Anand Doshi1e4fb682013-08-23 12:56:33 +0530112 }
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530113
Neil Trini Lasradoe2d8dd02015-06-22 19:06:15 +0530114 if(this.frm.fields_dict["recurring_print_format"]) {
115 this.frm.set_query("recurring_print_format", function(doc) {
116 return{
117 filters: [
118 ['Print Format', 'doc_type', '=', cur_frm.doctype],
119 ]
120 }
121 });
122 }
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530123
Nabin Hait1d218422015-07-17 15:19:02 +0530124 if(this.frm.fields_dict["return_against"]) {
125 this.frm.set_query("return_against", function(doc) {
126 var filters = {
127 "docstatus": 1,
128 "is_return": 0,
129 "company": doc.company
130 };
131 if (me.frm.fields_dict["customer"] && doc.customer) filters["customer"] = doc.customer;
132 if (me.frm.fields_dict["supplier"] && doc.supplier) filters["supplier"] = doc.supplier;
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530133
Nabin Hait1d218422015-07-17 15:19:02 +0530134 return {
135 filters: filters
136 }
137 });
138 }
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530139
Anand Doshi3543f302013-05-24 19:25:01 +0530140 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530141
Anand Doshi5e4e5d72013-08-06 17:23:44 +0530142 onload_post_render: function() {
Akhilesh Darjee61b5c592013-10-15 20:24:34 +0530143 var me = this;
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530144 if(this.frm.doc.__islocal && !(this.frm.doc.taxes || []).length
145 && !(this.frm.doc.__onload ? this.frm.doc.__onload.load_after_mapping : false)) {
146 this.apply_default_taxes();
Anand Doshi70f57eb2015-11-27 14:37:40 +0530147 } else if(this.frm.doc.__islocal && this.frm.doc.company && this.frm.doc["items"]
Nabin Haitbdfb0702015-10-28 15:38:08 +0530148 && !this.frm.doc.is_pos) {
149 me.calculate_taxes_and_totals();
Anand Doshi5e4e5d72013-08-06 17:23:44 +0530150 }
Nabin Haitdd38a262014-12-26 13:15:21 +0530151 if(frappe.meta.get_docfield(this.frm.doc.doctype + " Item", "item_code")) {
Rushabh Mehta203cc962016-04-07 15:25:43 +0530152 this.setup_item_selector();
Rushabh Mehtab6843f22014-06-04 13:10:41 +0530153 }
Anand Doshi5e4e5d72013-08-06 17:23:44 +0530154 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530155
Anand Doshi3543f302013-05-24 19:25:01 +0530156 refresh: function() {
Anand Doshi8bde7f92014-04-24 18:11:49 +0530157 erpnext.toggle_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +0530158 erpnext.hide_company();
Anand Doshi3543f302013-05-24 19:25:01 +0530159 this.show_item_wise_taxes();
Nabin Hait1f996c32013-07-29 19:00:53 +0530160 this.set_dynamic_labels();
Rushabh Mehta614e7ab2015-02-18 19:51:48 +0530161 this.setup_sms();
162 },
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530163
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530164 apply_default_taxes: function() {
165 var me = this;
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530166 var taxes_and_charges_field = frappe.meta.get_docfield(me.frm.doc.doctype, "taxes_and_charges",
167 me.frm.doc.name);
168
169 if(taxes_and_charges_field) {
Nabin Haitbdfb0702015-10-28 15:38:08 +0530170 return frappe.call({
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530171 method: "erpnext.controllers.accounts_controller.get_default_taxes_and_charges",
172 args: {
173 "master_doctype": taxes_and_charges_field.options
174 },
175 callback: function(r) {
176 if(!r.exc) {
177 me.frm.set_value("taxes", r.message);
Nabin Haitbdfb0702015-10-28 15:38:08 +0530178 me.calculate_taxes_and_totals();
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530179 }
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530180 }
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530181 });
182 }
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530183 },
Rushabh Mehta614e7ab2015-02-18 19:51:48 +0530184
185 setup_sms: function() {
186 var me = this;
Nabin Hait7132bd32016-04-13 15:19:37 +0530187 if(this.frm.doc.docstatus===1 && !in_list(["Lost", "Stopped", "Closed"], this.frm.doc.status)
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530188 && this.frm.doctype != "Purchase Invoice") {
Rushabh Mehta614e7ab2015-02-18 19:51:48 +0530189 this.frm.page.add_menu_item(__('Send SMS'), function() { me.send_sms(); });
190 }
191 },
192
193 send_sms: function() {
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530194 var sms_man = new SMSManager(this.frm.doc);
Nabin Hait239c2c32015-01-15 14:21:41 +0530195 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530196
Neil Trini Lasradod0151952015-06-09 15:21:50 +0530197 barcode: function(doc, cdt, cdn) {
198 var d = locals[cdt][cdn];
199 if(d.barcode=="" || d.barcode==null) {
200 // barcode cleared, remove item
201 d.item_code = "";
202 }
Rushabh Mehta675f9c62016-06-02 16:34:00 +0530203 this.item_code(doc, cdt, cdn, true);
Neil Trini Lasradod0151952015-06-09 15:21:50 +0530204 },
205
Rushabh Mehta675f9c62016-06-02 16:34:00 +0530206 item_code: function(doc, cdt, cdn, from_barcode) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530207 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530208 var item = frappe.get_doc(cdt, cdn);
Rushabh Mehta675f9c62016-06-02 16:34:00 +0530209
210 // clear barcode if setting item (else barcode will take priority)
211 if(!from_barcode) {
212 item.barcode = null;
213 }
Nabin Hait139dc7b2014-02-12 14:53:18 +0530214 if(item.item_code || item.barcode || item.serial_no) {
215 if(!this.validate_company_and_party()) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530216 cur_frm.fields_dict["items"].grid.grid_rows[item.idx - 1].remove();
Nabin Hait139dc7b2014-02-12 14:53:18 +0530217 } else {
218 return this.frm.call({
219 method: "erpnext.stock.get_item_details.get_item_details",
220 child: item,
221 args: {
222 args: {
223 item_code: item.item_code,
224 barcode: item.barcode,
225 serial_no: item.serial_no,
226 warehouse: item.warehouse,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530227 customer: me.frm.doc.customer,
228 supplier: me.frm.doc.supplier,
229 currency: me.frm.doc.currency,
230 conversion_rate: me.frm.doc.conversion_rate,
231 price_list: me.frm.doc.selling_price_list ||
232 me.frm.doc.buying_price_list,
233 price_list_currency: me.frm.doc.price_list_currency,
234 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
235 company: me.frm.doc.company,
236 order_type: me.frm.doc.order_type,
237 is_pos: cint(me.frm.doc.is_pos),
238 is_subcontracted: me.frm.doc.is_subcontracted,
Nabin Haita1bf43b2015-03-17 10:50:47 +0530239 transaction_date: me.frm.doc.transaction_date || me.frm.doc.posting_date,
Nabin Hait444f9562014-06-20 15:59:49 +0530240 ignore_pricing_rule: me.frm.doc.ignore_pricing_rule,
Anand Doshiafe85bd2016-01-21 21:04:16 +0530241 doctype: me.frm.doc.doctype,
242 name: me.frm.doc.name,
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +0530243 project: item.project || me.frm.doc.project,
Anand Doshi9b955fe2015-01-05 16:19:12 +0530244 qty: item.qty
Nabin Hait139dc7b2014-02-12 14:53:18 +0530245 }
246 },
Anand Doshi13945092014-09-21 19:45:49 +0530247
Nabin Hait139dc7b2014-02-12 14:53:18 +0530248 callback: function(r) {
249 if(!r.exc) {
250 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
251 }
252 }
253 });
254 }
255 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530256 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530257
258 serial_no: function(doc, cdt, cdn) {
259 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530260 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530261
262 if (item.serial_no) {
263 if (!item.item_code) {
264 this.frm.script_manager.trigger("item_code", cdt, cdn);
265 }
266 else {
267 var sr_no = [];
268
269 // Replacing all occurences of comma with carriage return
270 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
271
272 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530273
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530274 // Trim each string and push unique string to new list
275 for (var x=0; x<=serial_nos.length - 1; x++) {
276 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
277 sr_no.push(serial_nos[x].trim());
278 }
279 }
280
281 // Add the new list to the serial no. field in grid with each in new line
282 item.serial_no = "";
283 for (var x=0; x<=sr_no.length - 1; x++)
284 item.serial_no += sr_no[x] + '\n';
285
286 refresh_field("serial_no", item.name, item.parentfield);
Nabin Haitb7c0c552015-08-04 12:18:12 +0530287 if(!doc.is_return) {
288 frappe.model.set_value(item.doctype, item.name, "qty", sr_no.length);
289 }
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530290 }
291 }
292 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530293
Anand Doshi923d41d2013-05-28 17:23:36 +0530294 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530295 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530296 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530297
Anand Doshi3543f302013-05-24 19:25:01 +0530298 company: function() {
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530299 var me = this;
Nabin Haita731ad42016-02-04 17:21:28 +0530300 var set_pricing = function() {
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530301 if(me.frm.doc.company && me.frm.fields_dict.currency) {
302 var company_currency = me.get_company_currency();
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530303 var company_doc = frappe.get_doc(":Company", me.frm.doc.company);
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530304 if (!me.frm.doc.currency) {
305 me.frm.set_value("currency", company_currency);
306 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530307
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530308 if (me.frm.doc.currency == company_currency) {
309 me.frm.set_value("conversion_rate", 1.0);
310 }
311 if (me.frm.doc.price_list_currency == company_currency) {
312 me.frm.set_value('plc_conversion_rate', 1.0);
313 }
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530314 if (company_doc.default_letter_head) {
Rushabh Mehtac1857342015-05-27 12:29:57 +0530315 if(me.frm.fields_dict.letter_head) {
316 me.frm.set_value("letter_head", company_doc.default_letter_head);
317 }
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530318 }
Rushabh Mehtac1857342015-05-27 12:29:57 +0530319 if (company_doc.default_terms && me.frm.doc.doctype != "Purchase Invoice") {
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530320 me.frm.set_value("tc_name", company_doc.default_terms);
321 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530322
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530323 me.frm.script_manager.trigger("currency");
324 me.apply_pricing_rule();
Nabin Haitdd38a262014-12-26 13:15:21 +0530325 }
Anand Doshi3543f302013-05-24 19:25:01 +0530326 }
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530327
Nabin Haita731ad42016-02-04 17:21:28 +0530328 var set_party_account = function(set_pricing) {
329 if (in_list(["Sales Invoice", "Purchase Invoice"], me.frm.doc.doctype)) {
330 if(me.frm.doc.doctype=="Sales Invoice") {
331 var party_type = "Customer";
332 var party_account_field = 'debit_to';
333 } else {
334 var party_type = "Supplier";
335 var party_account_field = 'credit_to';
336 }
Anand Doshi39a28912016-02-15 11:42:18 +0530337
Nabin Haitef864402016-02-10 14:47:25 +0530338 var party = me.frm.doc[frappe.model.scrub(party_type)];
339 if(party) {
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530340 return frappe.call({
341 method: "erpnext.accounts.party.get_party_account",
342 args: {
343 company: me.frm.doc.company,
344 party_type: party_type,
Nabin Haitef864402016-02-10 14:47:25 +0530345 party: party
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530346 },
347 callback: function(r) {
348 if(!r.exc && r.message) {
349 me.frm.set_value(party_account_field, r.message);
350 set_pricing();
351 }
Nabin Haita731ad42016-02-04 17:21:28 +0530352 }
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530353 });
Nabin Haitef864402016-02-10 14:47:25 +0530354 } else {
355 set_pricing();
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530356 }
Nabin Haita731ad42016-02-04 17:21:28 +0530357 } else {
358 set_pricing();
359 }
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530360
Nabin Haita731ad42016-02-04 17:21:28 +0530361 }
Nabin Haitdd38a262014-12-26 13:15:21 +0530362
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530363 if (this.frm.doc.posting_date) var date = this.frm.doc.posting_date;
364 else var date = this.frm.doc.transaction_date;
Rushabh Mehta5dfe20c2016-04-26 12:59:08 +0530365
366 if (frappe.meta.get_docfield(this.frm.doctype, "shipping_address") &&
Rohit Waghchaureb2e7a1f2016-04-11 12:10:20 +0530367 in_list(['Purchase Order', 'Purchase Receipt', 'Purchase Invoice'], this.frm.doctype)){
368 erpnext.utils.get_shipping_address(this.frm, function(){
369 set_party_account(set_pricing);
370 })
371 }else{
372 set_party_account(set_pricing);
373 }
Rushabh Mehta45ca8a42015-04-28 16:02:09 +0530374
375 if(this.frm.doc.company) {
376 erpnext.last_selected_company = this.frm.doc.company;
377 }
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530378 },
379
380 transaction_date: function() {
Anand Doshi87da6622015-10-19 14:59:32 +0530381 if (this.frm.doc.transaction_date) {
382 this.frm.transaction_date = this.frm.doc.transaction_date;
383 }
Neil Trini Lasrado78fa6952014-10-03 17:43:02 +0530384 },
385
386 posting_date: function() {
Nabin Hait40e92b62015-07-09 17:12:23 +0530387 var me = this;
388 if (this.frm.doc.posting_date) {
Anand Doshi87da6622015-10-19 14:59:32 +0530389 this.frm.posting_date = this.frm.doc.posting_date;
390
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530391 if ((this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.customer) ||
Nabin Hait40e92b62015-07-09 17:12:23 +0530392 (this.frm.doc.doctype == "Purchase Invoice" && this.frm.doc.supplier)) {
393 return frappe.call({
394 method: "erpnext.accounts.party.get_due_date",
395 args: {
396 "posting_date": me.frm.doc.posting_date,
397 "party_type": me.frm.doc.doctype == "Sales Invoice" ? "Customer" : "Supplier",
398 "party": me.frm.doc.doctype == "Sales Invoice" ? me.frm.doc.customer : me.frm.doc.supplier,
399 "company": me.frm.doc.company
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530400 },
Nabin Hait40e92b62015-07-09 17:12:23 +0530401 callback: function(r, rt) {
402 if(r.message) {
403 me.frm.set_value("due_date", r.message);
404 }
Nabin Hait40e92b62015-07-09 17:12:23 +0530405 }
406 })
Nabin Hait40e92b62015-07-09 17:12:23 +0530407 }
408 }
Anand Doshi3543f302013-05-24 19:25:01 +0530409 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530410
Anand Doshi3543f302013-05-24 19:25:01 +0530411 get_company_currency: function() {
412 return erpnext.get_currency(this.frm.doc.company);
413 },
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530414
Neil Trini Lasrado09a66c42015-07-07 16:41:37 +0530415 contact_person: function() {
416 erpnext.utils.get_contact_details(this.frm);
417 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530418
Anand Doshi3543f302013-05-24 19:25:01 +0530419 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530420 var me = this;
421 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530422
Anand Doshi61a2f682013-06-21 17:55:31 +0530423 var company_currency = this.get_company_currency();
Nabin Hait69cdf592015-05-02 18:46:10 +0530424 // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
Rushabh Mehta76b13412016-07-18 14:30:18 +0530425 if(this.frm.doc.currency && this.frm.doc.currency !== company_currency
Nabin Hait39505072016-07-07 11:11:32 +0530426 && !this.frm.doc.ignore_pricing_rule) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530427 this.get_exchange_rate(this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530428 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530429 me.frm.set_value("conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530430 });
431 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530432 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530433 }
434 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530435
Anand Doshi61a2f682013-06-21 17:55:31 +0530436 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530437 if(this.frm.doc.currency === this.get_company_currency()) {
438 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530439 }
440 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530441 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
442 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
443 }
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530444
Nabin Haita3dd72a2014-05-28 12:49:20 +0530445 if(flt(this.frm.doc.conversion_rate)>0.0) {
Nabin Hait444f9562014-06-20 15:59:49 +0530446 if(this.frm.doc.ignore_pricing_rule) {
447 this.calculate_taxes_and_totals();
Anand Doshi51f722d2014-07-04 15:44:26 +0530448 } else if (!this.in_apply_price_list){
449 this.apply_price_list();
Nabin Hait444f9562014-06-20 15:59:49 +0530450 }
451
Nabin Haita3dd72a2014-05-28 12:49:20 +0530452 }
Anand Doshi3543f302013-05-24 19:25:01 +0530453 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530454
Anand Doshi61a2f682013-06-21 17:55:31 +0530455 get_exchange_rate: function(from_currency, to_currency, callback) {
Nabin Haitbdfb0702015-10-28 15:38:08 +0530456 return frappe.call({
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +0530457 method: "erpnext.setup.utils.get_exchange_rate",
458 args: {
459 from_currency: from_currency,
460 to_currency: to_currency
461 },
462 callback: function(r) {
463 callback(flt(r.message));
464 }
Anand Doshi61a2f682013-06-21 17:55:31 +0530465 });
466 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530467
Anand Doshi3543f302013-05-24 19:25:01 +0530468 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530469 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530470 this.set_dynamic_labels();
Nabin Haitec361ea2015-05-11 18:14:45 +0530471
472 var company_currency = this.get_company_currency();
473 // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
474 if(this.frm.doc.price_list_currency !== company_currency && !this.frm.doc.ignore_pricing_rule) {
475 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
476 function(exchange_rate) {
477 me.frm.set_value("plc_conversion_rate", exchange_rate);
478 });
479 } else {
480 this.plc_conversion_rate();
481 }
Anand Doshi3543f302013-05-24 19:25:01 +0530482 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530483
Anand Doshi3543f302013-05-24 19:25:01 +0530484 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530485 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
486 this.frm.set_value("plc_conversion_rate", 1.0);
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530487 } else if(this.frm.doc.price_list_currency === this.frm.doc.currency
Nabin Hait1d218422015-07-17 15:19:02 +0530488 && this.frm.doc.plc_conversion_rate && cint(this.frm.doc.plc_conversion_rate) != 1 &&
Nabin Haitec361ea2015-05-11 18:14:45 +0530489 cint(this.frm.doc.plc_conversion_rate) != cint(this.frm.doc.conversion_rate)) {
490 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530491 }
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530492
Nabin Haitec361ea2015-05-11 18:14:45 +0530493 if(!this.in_apply_price_list) {
494 this.apply_price_list();
Anand Doshi61a2f682013-06-21 17:55:31 +0530495 }
Anand Doshi3543f302013-05-24 19:25:01 +0530496 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530497
Anand Doshi3543f302013-05-24 19:25:01 +0530498 qty: function(doc, cdt, cdn) {
Nabin Hait444f9562014-06-20 15:59:49 +0530499 this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
Anand Doshi3543f302013-05-24 19:25:01 +0530500 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530501
Anand Doshi61a2f682013-06-21 17:55:31 +0530502 set_dynamic_labels: function() {
503 // What TODO? should we make price list system non-mandatory?
504 this.frm.toggle_reqd("plc_conversion_rate",
505 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530506
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +0530507 if(this.frm.doc_currency!==this.frm.doc.currency) {
508 // reset names only when the currency is different
509
510 var company_currency = this.get_company_currency();
511 this.change_form_labels(company_currency);
512 this.change_grid_labels(company_currency);
513 this.frm.refresh_fields();
514 this.frm.doc_currency = this.frm.doc.currency;
515 }
Anand Doshi61a2f682013-06-21 17:55:31 +0530516 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530517
Nabin Hait613d0812015-02-23 11:58:15 +0530518 change_form_labels: function(company_currency) {
519 var me = this;
520 var field_label_map = {};
521
522 var setup_field_label_map = function(fields_list, currency) {
523 $.each(fields_list, function(i, fname) {
524 var docfield = frappe.meta.docfield_map[me.frm.doc.doctype][fname];
525 if(docfield) {
526 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
527 field_label_map[fname] = label.trim() + " (" + currency + ")";
528 }
529 });
530 };
531 setup_field_label_map(["base_total", "base_net_total", "base_total_taxes_and_charges",
532 "base_discount_amount", "base_grand_total", "base_rounded_total", "base_in_words",
533 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted", "total_amount_to_pay",
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530534 "base_paid_amount", "base_write_off_amount", "base_change_amount"
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530535 ], company_currency);
Nabin Hait613d0812015-02-23 11:58:15 +0530536
537 setup_field_label_map(["total", "net_total", "total_taxes_and_charges", "discount_amount",
538 "grand_total", "taxes_and_charges_added", "taxes_and_charges_deducted",
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530539 "rounded_total", "in_words", "paid_amount", "write_off_amount"], this.frm.doc.currency);
Anand Doshi87da6622015-10-19 14:59:32 +0530540
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530541 setup_field_label_map(["outstanding_amount", "total_advance"], this.frm.doc.party_account_currency);
Nabin Hait613d0812015-02-23 11:58:15 +0530542
543 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
544 + " = [?] " + company_currency)
545
546 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
547 cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency
548 + " = [?] " + company_currency)
549 }
550
551 // toggle fields
552 this.frm.toggle_display(["conversion_rate", "base_total", "base_net_total", "base_total_taxes_and_charges",
553 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted",
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530554 "base_grand_total", "base_rounded_total", "base_in_words", "base_discount_amount",
555 "base_paid_amount", "base_write_off_amount"],
Nabin Hait613d0812015-02-23 11:58:15 +0530556 this.frm.doc.currency != company_currency);
557
558 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
559 this.frm.doc.price_list_currency != company_currency);
560
561 // set labels
562 $.each(field_label_map, function(fname, label) {
563 me.frm.fields_dict[fname].set_label(label);
564 });
Nabin Hait37b047d2015-02-23 16:01:33 +0530565
Nabin Hait903c42a2015-02-24 15:24:49 +0530566 var show =cint(cur_frm.doc.discount_amount) ||
567 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
568
569 if(frappe.meta.get_docfield(cur_frm.doctype, "net_total"))
570 cur_frm.toggle_display("net_total", show);
571
572 if(frappe.meta.get_docfield(cur_frm.doctype, "base_net_total"))
573 cur_frm.toggle_display("base_net_total", (show && (me.frm.doc.currency != company_currency)));
574
Nabin Hait613d0812015-02-23 11:58:15 +0530575 },
576
577 change_grid_labels: function(company_currency) {
578 var me = this;
579 var field_label_map = {};
580
581 var setup_field_label_map = function(fields_list, currency, parentfield) {
582 var grid_doctype = me.frm.fields_dict[parentfield].grid.doctype;
583 $.each(fields_list, function(i, fname) {
584 var docfield = frappe.meta.docfield_map[grid_doctype][fname];
585 if(docfield) {
586 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
587 field_label_map[grid_doctype + "-" + fname] =
Rushabh Mehtaeb961372016-01-04 15:48:37 +0530588 label.trim() + " (" + __(currency) + ")";
Nabin Hait613d0812015-02-23 11:58:15 +0530589 }
590 });
591 }
592
593 setup_field_label_map(["base_rate", "base_net_rate", "base_price_list_rate", "base_amount", "base_net_amount"],
594 company_currency, "items");
595
596 setup_field_label_map(["rate", "net_rate", "price_list_rate", "amount", "net_amount"],
597 this.frm.doc.currency, "items");
598
599 if(this.frm.fields_dict["taxes"]) {
600 setup_field_label_map(["tax_amount", "total", "tax_amount_after_discount"], this.frm.doc.currency, "taxes");
601
602 setup_field_label_map(["base_tax_amount", "base_total", "base_tax_amount_after_discount"], company_currency, "taxes");
603 }
604
605 if(this.frm.fields_dict["advances"]) {
Anand Doshi70f57eb2015-11-27 14:37:40 +0530606 setup_field_label_map(["advance_amount", "allocated_amount"],
Nabin Haitaa015902015-10-16 13:08:33 +0530607 this.frm.doc.party_account_currency, "advances");
Nabin Hait613d0812015-02-23 11:58:15 +0530608 }
609
610 // toggle columns
611 var item_grid = this.frm.fields_dict["items"].grid;
612 $.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) {
613 if(frappe.meta.get_docfield(item_grid.doctype, fname))
614 item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency);
615 });
616
617 var show = (cint(cur_frm.doc.discount_amount)) ||
618 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
619
620 $.each(["net_rate", "net_amount"], function(i, fname) {
621 if(frappe.meta.get_docfield(item_grid.doctype, fname))
622 item_grid.set_column_disp(fname, show);
623 });
624
625 $.each(["base_net_rate", "base_net_amount"], function(i, fname) {
626 if(frappe.meta.get_docfield(item_grid.doctype, fname))
627 item_grid.set_column_disp(fname, (show && (me.frm.doc.currency != company_currency)));
628 });
629
630 // set labels
631 var $wrapper = $(this.frm.wrapper);
632 $.each(field_label_map, function(fname, label) {
633 fname = fname.split("-");
634 var df = frappe.meta.get_docfield(fname[0], fname[1], me.frm.doc.name);
635 if(df) df.label = label;
636 });
637 },
638
Anand Doshi923d41d2013-05-28 17:23:36 +0530639 recalculate: function() {
640 this.calculate_taxes_and_totals();
641 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530642
Anand Doshi923d41d2013-05-28 17:23:36 +0530643 recalculate_values: function() {
644 this.calculate_taxes_and_totals();
645 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530646
Anand Doshid0b00722013-05-28 18:54:48 +0530647 calculate_charges: function() {
648 this.calculate_taxes_and_totals();
649 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530650
Nabin Hait444f9562014-06-20 15:59:49 +0530651 ignore_pricing_rule: function() {
652 this.apply_pricing_rule();
653 },
654
655 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530656 var me = this;
Anand Doshi70f57eb2015-11-27 14:37:40 +0530657 var args = this._get_args(item);
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530658 if (!(args.items && args.items.length)) {
Anand Doshi70f57eb2015-11-27 14:37:40 +0530659 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
660 return;
661 }
662
Anand Doshidffec8f2014-07-01 17:45:15 +0530663 return this.frm.call({
664 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
Anand Doshi70f57eb2015-11-27 14:37:40 +0530665 args: { args: args },
Anand Doshidffec8f2014-07-01 17:45:15 +0530666 callback: function(r) {
Anand Doshif214bfc2014-07-16 17:52:08 +0530667 if (!r.exc && r.message) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530668 me._set_values_for_item_list(r.message);
Saurabh2d7af632016-02-26 11:09:20 +0530669 if(item) me.set_gross_profit(item);
Anand Doshidffec8f2014-07-01 17:45:15 +0530670 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
671 }
672 }
673 });
674 },
675
676 _get_args: function(item) {
677 var me = this;
678 return {
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530679 "items": this._get_item_list(item),
Nabin Hait444f9562014-06-20 15:59:49 +0530680 "customer": me.frm.doc.customer,
681 "customer_group": me.frm.doc.customer_group,
682 "territory": me.frm.doc.territory,
683 "supplier": me.frm.doc.supplier,
684 "supplier_type": me.frm.doc.supplier_type,
685 "currency": me.frm.doc.currency,
686 "conversion_rate": me.frm.doc.conversion_rate,
687 "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
Nabin Haitdd82bf22015-05-11 16:49:17 +0530688 "price_list_currency": me.frm.doc.price_list_currency,
Nabin Hait444f9562014-06-20 15:59:49 +0530689 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
690 "company": me.frm.doc.company,
691 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
692 "campaign": me.frm.doc.campaign,
693 "sales_partner": me.frm.doc.sales_partner,
694 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530695 "doctype": me.frm.doc.doctype,
696 "name": me.frm.doc.name
Nabin Hait444f9562014-06-20 15:59:49 +0530697 };
Nabin Hait444f9562014-06-20 15:59:49 +0530698 },
699
700 _get_item_list: function(item) {
701 var item_list = [];
702 var append_item = function(d) {
Anand Doshi39edcbc2014-07-28 16:00:19 +0530703 if (d.item_code) {
704 item_list.push({
705 "doctype": d.doctype,
706 "name": d.name,
707 "item_code": d.item_code,
708 "item_group": d.item_group,
709 "brand": d.brand,
Anand Doshi39a28912016-02-15 11:42:18 +0530710 "qty": d.qty,
711 "parenttype": d.parenttype,
712 "parent": d.parent
Anand Doshi39edcbc2014-07-28 16:00:19 +0530713 });
mbauskarc79415e2016-01-18 16:32:24 +0530714
715 // if doctype is Quotation Item / Sales Order Iten then add Margin Type and rate in item_list
mbauskara52472c2016-03-05 15:10:25 +0530716 if (in_list(["Quotation Item", "Sales Order Item", "Delivery Note Item", "Sales Invoice Item"]), d.doctype){
717 item_list[0]["margin_type"] = d.margin_type
718 item_list[0]["margin_rate_or_amount"] = d.margin_rate_or_amount
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +0530719 }
Anand Doshi39edcbc2014-07-28 16:00:19 +0530720 }
Nabin Hait444f9562014-06-20 15:59:49 +0530721 };
Nabin Haita3dd72a2014-05-28 12:49:20 +0530722
Nabin Hait444f9562014-06-20 15:59:49 +0530723 if (item) {
724 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +0530725 } else {
Nabin Hait96339342015-01-21 17:22:11 +0530726 $.each(this.frm.doc["items"] || [], function(i, d) {
Nabin Hait444f9562014-06-20 15:59:49 +0530727 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530728 });
729 }
Nabin Hait444f9562014-06-20 15:59:49 +0530730 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +0530731 },
732
Anand Doshidffec8f2014-07-01 17:45:15 +0530733 _set_values_for_item_list: function(children) {
Nabin Hait49a27292014-12-17 11:21:32 +0530734 var me = this;
Nabin Hait7d8fa802014-12-30 15:35:45 +0530735 var price_list_rate_changed = false;
Anand Doshidea3fb02015-02-05 15:05:58 +0530736 for(var i=0, l=children.length; i<l; i++) {
737 var d = children[i];
Nabin Hait49a27292014-12-17 11:21:32 +0530738 var existing_pricing_rule = frappe.model.get_value(d.doctype, d.name, "pricing_rule");
Anand Doshidea3fb02015-02-05 15:05:58 +0530739
740 for(var k in d) {
741 var v = d[k];
Anand Doshidffec8f2014-07-01 17:45:15 +0530742 if (["doctype", "name"].indexOf(k)===-1) {
Nabin Hait7d8fa802014-12-30 15:35:45 +0530743 if(k=="price_list_rate") {
744 if(flt(v) != flt(d.price_list_rate)) price_list_rate_changed = true;
745 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530746 frappe.model.set_value(d.doctype, d.name, k, v);
747 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530748 }
749
Nabin Hait49a27292014-12-17 11:21:32 +0530750 // if pricing rule set as blank from an existing value, apply price_list
Nabin Haitcfe3c542014-12-22 10:42:48 +0530751 if(!me.frm.doc.ignore_pricing_rule && existing_pricing_rule && !d.pricing_rule) {
Nabin Hait49a27292014-12-17 11:21:32 +0530752 me.apply_price_list(frappe.get_doc(d.doctype, d.name));
753 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530754 }
Nabin Hait7d8fa802014-12-30 15:35:45 +0530755
Anand Doshidea3fb02015-02-05 15:05:58 +0530756 if(!price_list_rate_changed) me.calculate_taxes_and_totals();
Anand Doshidffec8f2014-07-01 17:45:15 +0530757 },
758
Nabin Hait49a27292014-12-17 11:21:32 +0530759 apply_price_list: function(item) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530760 var me = this;
Rushabh Mehta052bd622015-01-30 16:57:24 +0530761 var args = this._get_args(item);
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530762 if (!((args.items && args.items.length) || args.price_list)) {
Anand Doshi70f57eb2015-11-27 14:37:40 +0530763 return;
764 }
Nabin Hait37b047d2015-02-23 16:01:33 +0530765
Anand Doshidffec8f2014-07-01 17:45:15 +0530766 return this.frm.call({
767 method: "erpnext.stock.get_item_details.apply_price_list",
Rushabh Mehta052bd622015-01-30 16:57:24 +0530768 args: { args: args },
Anand Doshidffec8f2014-07-01 17:45:15 +0530769 callback: function(r) {
770 if (!r.exc) {
Anand Doshi51f722d2014-07-04 15:44:26 +0530771 me.in_apply_price_list = true;
Anand Doshidffec8f2014-07-01 17:45:15 +0530772 me.frm.set_value("price_list_currency", r.message.parent.price_list_currency);
773 me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate);
Anand Doshi51f722d2014-07-04 15:44:26 +0530774 me.in_apply_price_list = false;
Nabin Hait37b047d2015-02-23 16:01:33 +0530775
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530776 if(args.items.length) {
Nabin Hait37b047d2015-02-23 16:01:33 +0530777 me._set_values_for_item_list(r.message.children);
778 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530779 }
780 }
781 });
782 },
783
Anand Doshi3543f302013-05-24 19:25:01 +0530784 get_item_wise_taxes_html: function() {
785 var item_tax = {};
786 var tax_accounts = [];
787 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530788
Nabin Hait96339342015-01-21 17:22:11 +0530789 $.each(this.frm.doc["taxes"] || [], function(i, tax) {
Anand Doshi3543f302013-05-24 19:25:01 +0530790 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530791 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530792 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530793 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530794 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530795 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530796 var tax_rate = "";
797 if(tax_data[0] != null) {
798 tax_rate = (tax.charge_type === "Actual") ?
799 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
800 (flt(tax_data[0], tax_rate_precision) + "%");
801 }
802 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
803 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530804
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530805 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530806 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530807 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530808 }
Anand Doshi3543f302013-05-24 19:25:01 +0530809 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530810 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530811 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530812
813 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530814 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530815
Anand Doshi179e3772013-09-05 16:53:57 +0530816 var distinct_item_names = [];
817 var distinct_items = [];
Nabin Hait96339342015-01-21 17:22:11 +0530818 $.each(this.frm.doc["items"] || [], function(i, item) {
Anand Doshi179e3772013-09-05 16:53:57 +0530819 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
820 distinct_item_names.push(item.item_code || item.item_name);
821 distinct_items.push(item);
822 }
823 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530824
Anand Doshi179e3772013-09-05 16:53:57 +0530825 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530826 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530827 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530828 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
829 item_name: item.item_name,
830 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530831 return item_tax_record[head[0]] ?
832 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530833 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530834 }).join("\n")
835 });
836 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530837
Anand Doshi53b73422013-05-31 11:50:01 +0530838 if(!rows) return "";
Rushabh Mehtaf24f6042015-08-20 18:01:40 +0530839 return '<p><a class="h6 text-muted" href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">'
840 + __("Show tax break-up") + '</a><br><br></p>\
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530841 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530842 <thead><tr>' + headings + '</tr></thead> \
843 <tbody>' + rows + '</tbody> \
844 </table></div>';
845 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530846
Nabin Hait139dc7b2014-02-12 14:53:18 +0530847 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530848 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530849 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530850
Nabin Hait0f231062014-02-20 11:27:47 +0530851 $.each(["company", "customer"], function(i, fieldname) {
Saurabh8a8ef852015-10-21 19:28:53 +0530852 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname && me.frm.doc.doctype != "Purchase Order")) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530853 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530854 msgprint(__("Please specify") + ": " +
855 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530856 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530857 valid = false;
858 }
Anand Doshi3543f302013-05-24 19:25:01 +0530859 }
860 });
861 return valid;
862 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530863
Anand Doshibf3e54a2013-06-05 14:11:32 +0530864 get_terms: function() {
865 var me = this;
866 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530867 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530868 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530869 args: {
870 doctype: "Terms and Conditions",
871 fieldname: "terms",
872 filters: { name: this.frm.doc.tc_name },
873 },
874 });
875 }
876 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530877
878 taxes_and_charges: function() {
879 var me = this;
880 if(this.frm.doc.taxes_and_charges) {
881 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530882 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
883 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530884 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
885 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530886 "master_name": this.frm.doc.taxes_and_charges,
Nabin Hait6b039142014-05-02 15:45:10 +0530887 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530888 callback: function(r) {
889 if(!r.exc) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530890 me.frm.set_value("taxes", r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530891 me.calculate_taxes_and_totals();
892 }
893 }
894 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530895 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530896 },
897
898 show_item_wise_taxes: function() {
899 if(this.frm.fields_dict.other_charges_calculation) {
Anand Doshidd942bb2015-02-04 18:52:35 +0530900 var html = this.get_item_wise_taxes_html();
901 if (html) {
902 this.frm.toggle_display("other_charges_calculation", true);
903 $(this.frm.fields_dict.other_charges_calculation.wrapper).html(html);
904 } else {
905 this.frm.toggle_display("other_charges_calculation", false);
906 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530907 }
908 },
Anand Doshi13945092014-09-21 19:45:49 +0530909
910 is_recurring: function() {
911 // set default values for recurring documents
Rushabh Mehta756e5182016-03-03 13:00:17 +0530912 if(this.frm.doc.is_recurring && this.frm.doc.__islocal) {
913 frappe.msgprint(__("Please set recurring after saving"));
914 this.frm.set_value('is_recurring', 0);
915 return;
916 }
917
Anand Doshi13945092014-09-21 19:45:49 +0530918 if(this.frm.doc.is_recurring) {
Nabin Hait68a628e2016-03-21 12:03:16 +0530919 if(!this.frm.doc.recurring_id) {
920 this.frm.set_value('recurring_id', this.frm.doc.name);
921 }
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +0530922
Anand Doshi13945092014-09-21 19:45:49 +0530923 var owner_email = this.frm.doc.owner=="Administrator"
924 ? frappe.user_info("Administrator").email
925 : this.frm.doc.owner;
926
927 this.frm.doc.notification_email_address = $.map([cstr(owner_email),
928 cstr(this.frm.doc.contact_email)], function(v) { return v || null; }).join(", ");
929 this.frm.doc.repeat_on_day_of_month = frappe.datetime.str_to_obj(this.frm.doc.posting_date).getDate();
930 }
931
932 refresh_many(["notification_email_address", "repeat_on_day_of_month"]);
933 },
934
935 from_date: function() {
936 // set to_date
937 if(this.frm.doc.from_date) {
938 var recurring_type_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6,
939 'Yearly': 12};
940
941 var months = recurring_type_map[this.frm.doc.recurring_type];
942 if(months) {
943 var to_date = frappe.datetime.add_months(this.frm.doc.from_date,
944 months);
945 this.frm.doc.to_date = frappe.datetime.add_days(to_date, -1);
946 refresh_field('to_date');
947 }
948 }
Saurabhd3135532016-02-25 18:59:20 +0530949 },
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +0530950
Saurabh2d7af632016-02-26 11:09:20 +0530951 set_gross_profit: function(item) {
Saurabh5ada14b2016-02-26 18:02:55 +0530952 if (this.frm.doc.doctype == "Sales Order" && item.valuation_rate) {
Saurabh3fbf3ce2016-03-09 15:31:04 +0530953 rate = flt(item.rate) * flt(this.frm.doc.conversion_rate || 1);
954 item.gross_profit = flt(((rate - item.valuation_rate) * item.qty), precision("amount", item));
Saurabh2d7af632016-02-26 11:09:20 +0530955 }
Rushabh Mehta203cc962016-04-07 15:25:43 +0530956 },
957
958 setup_item_selector: function() {
Rushabh Mehtabc928242016-05-29 23:08:22 +0530959 // TODO: remove item selector
960
961 return;
Rushabh Mehta203cc962016-04-07 15:25:43 +0530962 if(!this.item_selector) {
963 this.item_selector = new erpnext.ItemSelector({frm: this.frm});
964 }
Nabin Hait28a05282016-06-27 17:41:39 +0530965 },
Rushabh Mehta76b13412016-07-18 14:30:18 +0530966
Nabin Hait28a05282016-06-27 17:41:39 +0530967 get_advances: function() {
968 if(!this.frm.is_return) {
969 return this.frm.call({
970 method: "set_advances",
971 doc: this.frm.doc,
972 callback: function(r, rt) {
973 refresh_field("advances");
974 }
975 })
976 }
Nabin Hait05aefbb2016-06-28 19:42:19 +0530977 },
Rushabh Mehta76b13412016-07-18 14:30:18 +0530978
Nabin Hait05aefbb2016-06-28 19:42:19 +0530979 make_payment_entry: function() {
980 return frappe.call({
Nabin Hait13093b42016-06-29 18:04:37 +0530981 method: "erpnext.accounts.doctype.payment_entry.payment_entry.get_payment_entry",
Nabin Hait05aefbb2016-06-28 19:42:19 +0530982 args: {
983 "dt": cur_frm.doc.doctype,
984 "dn": cur_frm.doc.name
985 },
986 callback: function(r) {
987 var doclist = frappe.model.sync(r.message);
988 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
Nabin Hait56548cb2016-07-01 15:58:39 +0530989 // cur_frm.refresh_fields()
Nabin Hait05aefbb2016-06-28 19:42:19 +0530990 }
991 });
Anand Doshi13945092014-09-21 19:45:49 +0530992 }
Rushabh Mehtabe2ee182016-04-29 17:22:42 +0530993});