blob: 78776a38334ca7e679e1ac7d43f2b555ba4515ef [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) {
Chude Osiegbu7be3aa32016-09-05 23:35:00 +0100427 this.get_exchange_rate(this.frm.doc.posting_date, 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
Chude Osiegbu7be3aa32016-09-05 23:35:00 +0100455 get_exchange_rate: function(posting_date, 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: {
Chude Osiegbu7be3aa32016-09-05 23:35:00 +0100459 posting_date: posting_date,
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +0530460 from_currency: from_currency,
461 to_currency: to_currency
462 },
463 callback: function(r) {
464 callback(flt(r.message));
465 }
Anand Doshi61a2f682013-06-21 17:55:31 +0530466 });
467 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530468
Anand Doshi3543f302013-05-24 19:25:01 +0530469 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530470 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530471 this.set_dynamic_labels();
Nabin Haitec361ea2015-05-11 18:14:45 +0530472
473 var company_currency = this.get_company_currency();
474 // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
475 if(this.frm.doc.price_list_currency !== company_currency && !this.frm.doc.ignore_pricing_rule) {
Chude Osiegbufbf27b52016-09-07 03:06:08 +0100476 this.get_exchange_rate(this.frm.doc.posting_date, this.frm.doc.price_list_currency, company_currency,
Nabin Haitec361ea2015-05-11 18:14:45 +0530477 function(exchange_rate) {
478 me.frm.set_value("plc_conversion_rate", exchange_rate);
479 });
480 } else {
481 this.plc_conversion_rate();
482 }
Anand Doshi3543f302013-05-24 19:25:01 +0530483 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530484
Anand Doshi3543f302013-05-24 19:25:01 +0530485 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530486 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
487 this.frm.set_value("plc_conversion_rate", 1.0);
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530488 } else if(this.frm.doc.price_list_currency === this.frm.doc.currency
Nabin Hait1d218422015-07-17 15:19:02 +0530489 && this.frm.doc.plc_conversion_rate && cint(this.frm.doc.plc_conversion_rate) != 1 &&
Nabin Haitec361ea2015-05-11 18:14:45 +0530490 cint(this.frm.doc.plc_conversion_rate) != cint(this.frm.doc.conversion_rate)) {
491 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530492 }
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530493
Nabin Haitec361ea2015-05-11 18:14:45 +0530494 if(!this.in_apply_price_list) {
495 this.apply_price_list();
Anand Doshi61a2f682013-06-21 17:55:31 +0530496 }
Anand Doshi3543f302013-05-24 19:25:01 +0530497 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530498
Anand Doshi3543f302013-05-24 19:25:01 +0530499 qty: function(doc, cdt, cdn) {
Nabin Hait444f9562014-06-20 15:59:49 +0530500 this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
Anand Doshi3543f302013-05-24 19:25:01 +0530501 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530502
Anand Doshi61a2f682013-06-21 17:55:31 +0530503 set_dynamic_labels: function() {
504 // What TODO? should we make price list system non-mandatory?
505 this.frm.toggle_reqd("plc_conversion_rate",
506 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +0530507
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +0530508 if(this.frm.doc_currency!==this.frm.doc.currency) {
509 // reset names only when the currency is different
510
511 var company_currency = this.get_company_currency();
512 this.change_form_labels(company_currency);
513 this.change_grid_labels(company_currency);
514 this.frm.refresh_fields();
515 this.frm.doc_currency = this.frm.doc.currency;
516 }
Anand Doshi61a2f682013-06-21 17:55:31 +0530517 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530518
Nabin Hait613d0812015-02-23 11:58:15 +0530519 change_form_labels: function(company_currency) {
520 var me = this;
521 var field_label_map = {};
522
523 var setup_field_label_map = function(fields_list, currency) {
524 $.each(fields_list, function(i, fname) {
525 var docfield = frappe.meta.docfield_map[me.frm.doc.doctype][fname];
526 if(docfield) {
527 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
528 field_label_map[fname] = label.trim() + " (" + currency + ")";
529 }
530 });
531 };
532 setup_field_label_map(["base_total", "base_net_total", "base_total_taxes_and_charges",
533 "base_discount_amount", "base_grand_total", "base_rounded_total", "base_in_words",
534 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted", "total_amount_to_pay",
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530535 "base_paid_amount", "base_write_off_amount", "base_change_amount"
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530536 ], company_currency);
Nabin Hait613d0812015-02-23 11:58:15 +0530537
538 setup_field_label_map(["total", "net_total", "total_taxes_and_charges", "discount_amount",
539 "grand_total", "taxes_and_charges_added", "taxes_and_charges_deducted",
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530540 "rounded_total", "in_words", "paid_amount", "write_off_amount"], this.frm.doc.currency);
Anand Doshi87da6622015-10-19 14:59:32 +0530541
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530542 setup_field_label_map(["outstanding_amount", "total_advance"], this.frm.doc.party_account_currency);
Nabin Hait613d0812015-02-23 11:58:15 +0530543
544 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
545 + " = [?] " + company_currency)
546
547 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
548 cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency
549 + " = [?] " + company_currency)
550 }
551
552 // toggle fields
553 this.frm.toggle_display(["conversion_rate", "base_total", "base_net_total", "base_total_taxes_and_charges",
554 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted",
Nabin Hait4ffd7f32015-08-27 12:28:36 +0530555 "base_grand_total", "base_rounded_total", "base_in_words", "base_discount_amount",
556 "base_paid_amount", "base_write_off_amount"],
Nabin Hait613d0812015-02-23 11:58:15 +0530557 this.frm.doc.currency != company_currency);
558
559 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
560 this.frm.doc.price_list_currency != company_currency);
561
562 // set labels
563 $.each(field_label_map, function(fname, label) {
564 me.frm.fields_dict[fname].set_label(label);
565 });
Nabin Hait37b047d2015-02-23 16:01:33 +0530566
Nabin Hait903c42a2015-02-24 15:24:49 +0530567 var show =cint(cur_frm.doc.discount_amount) ||
568 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
569
570 if(frappe.meta.get_docfield(cur_frm.doctype, "net_total"))
571 cur_frm.toggle_display("net_total", show);
572
573 if(frappe.meta.get_docfield(cur_frm.doctype, "base_net_total"))
574 cur_frm.toggle_display("base_net_total", (show && (me.frm.doc.currency != company_currency)));
575
Nabin Hait613d0812015-02-23 11:58:15 +0530576 },
577
578 change_grid_labels: function(company_currency) {
579 var me = this;
580 var field_label_map = {};
581
582 var setup_field_label_map = function(fields_list, currency, parentfield) {
583 var grid_doctype = me.frm.fields_dict[parentfield].grid.doctype;
584 $.each(fields_list, function(i, fname) {
585 var docfield = frappe.meta.docfield_map[grid_doctype][fname];
586 if(docfield) {
587 var label = __(docfield.label || "").replace(/\([^\)]*\)/g, "");
588 field_label_map[grid_doctype + "-" + fname] =
Rushabh Mehtaeb961372016-01-04 15:48:37 +0530589 label.trim() + " (" + __(currency) + ")";
Nabin Hait613d0812015-02-23 11:58:15 +0530590 }
591 });
592 }
593
594 setup_field_label_map(["base_rate", "base_net_rate", "base_price_list_rate", "base_amount", "base_net_amount"],
595 company_currency, "items");
596
597 setup_field_label_map(["rate", "net_rate", "price_list_rate", "amount", "net_amount"],
598 this.frm.doc.currency, "items");
599
600 if(this.frm.fields_dict["taxes"]) {
601 setup_field_label_map(["tax_amount", "total", "tax_amount_after_discount"], this.frm.doc.currency, "taxes");
602
603 setup_field_label_map(["base_tax_amount", "base_total", "base_tax_amount_after_discount"], company_currency, "taxes");
604 }
605
606 if(this.frm.fields_dict["advances"]) {
Anand Doshi70f57eb2015-11-27 14:37:40 +0530607 setup_field_label_map(["advance_amount", "allocated_amount"],
Nabin Haitaa015902015-10-16 13:08:33 +0530608 this.frm.doc.party_account_currency, "advances");
Nabin Hait613d0812015-02-23 11:58:15 +0530609 }
610
611 // toggle columns
612 var item_grid = this.frm.fields_dict["items"].grid;
613 $.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) {
614 if(frappe.meta.get_docfield(item_grid.doctype, fname))
615 item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency);
616 });
617
618 var show = (cint(cur_frm.doc.discount_amount)) ||
619 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
620
621 $.each(["net_rate", "net_amount"], function(i, fname) {
622 if(frappe.meta.get_docfield(item_grid.doctype, fname))
623 item_grid.set_column_disp(fname, show);
624 });
625
626 $.each(["base_net_rate", "base_net_amount"], function(i, fname) {
627 if(frappe.meta.get_docfield(item_grid.doctype, fname))
628 item_grid.set_column_disp(fname, (show && (me.frm.doc.currency != company_currency)));
629 });
630
631 // set labels
632 var $wrapper = $(this.frm.wrapper);
633 $.each(field_label_map, function(fname, label) {
634 fname = fname.split("-");
635 var df = frappe.meta.get_docfield(fname[0], fname[1], me.frm.doc.name);
636 if(df) df.label = label;
637 });
638 },
639
Anand Doshi923d41d2013-05-28 17:23:36 +0530640 recalculate: function() {
641 this.calculate_taxes_and_totals();
642 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530643
Anand Doshi923d41d2013-05-28 17:23:36 +0530644 recalculate_values: function() {
645 this.calculate_taxes_and_totals();
646 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530647
Anand Doshid0b00722013-05-28 18:54:48 +0530648 calculate_charges: function() {
649 this.calculate_taxes_and_totals();
650 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530651
Nabin Hait444f9562014-06-20 15:59:49 +0530652 ignore_pricing_rule: function() {
653 this.apply_pricing_rule();
654 },
655
656 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530657 var me = this;
Anand Doshi70f57eb2015-11-27 14:37:40 +0530658 var args = this._get_args(item);
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530659 if (!(args.items && args.items.length)) {
Anand Doshi70f57eb2015-11-27 14:37:40 +0530660 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
661 return;
662 }
663
Anand Doshidffec8f2014-07-01 17:45:15 +0530664 return this.frm.call({
665 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
Anand Doshi70f57eb2015-11-27 14:37:40 +0530666 args: { args: args },
Anand Doshidffec8f2014-07-01 17:45:15 +0530667 callback: function(r) {
Anand Doshif214bfc2014-07-16 17:52:08 +0530668 if (!r.exc && r.message) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530669 me._set_values_for_item_list(r.message);
Saurabh2d7af632016-02-26 11:09:20 +0530670 if(item) me.set_gross_profit(item);
Anand Doshidffec8f2014-07-01 17:45:15 +0530671 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
672 }
673 }
674 });
675 },
676
677 _get_args: function(item) {
678 var me = this;
679 return {
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530680 "items": this._get_item_list(item),
Nabin Hait444f9562014-06-20 15:59:49 +0530681 "customer": me.frm.doc.customer,
682 "customer_group": me.frm.doc.customer_group,
683 "territory": me.frm.doc.territory,
684 "supplier": me.frm.doc.supplier,
685 "supplier_type": me.frm.doc.supplier_type,
686 "currency": me.frm.doc.currency,
687 "conversion_rate": me.frm.doc.conversion_rate,
688 "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
Nabin Haitdd82bf22015-05-11 16:49:17 +0530689 "price_list_currency": me.frm.doc.price_list_currency,
Nabin Hait444f9562014-06-20 15:59:49 +0530690 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
691 "company": me.frm.doc.company,
692 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
693 "campaign": me.frm.doc.campaign,
694 "sales_partner": me.frm.doc.sales_partner,
695 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530696 "doctype": me.frm.doc.doctype,
697 "name": me.frm.doc.name
Nabin Hait444f9562014-06-20 15:59:49 +0530698 };
Nabin Hait444f9562014-06-20 15:59:49 +0530699 },
700
701 _get_item_list: function(item) {
702 var item_list = [];
703 var append_item = function(d) {
Anand Doshi39edcbc2014-07-28 16:00:19 +0530704 if (d.item_code) {
705 item_list.push({
706 "doctype": d.doctype,
707 "name": d.name,
708 "item_code": d.item_code,
709 "item_group": d.item_group,
710 "brand": d.brand,
Anand Doshi39a28912016-02-15 11:42:18 +0530711 "qty": d.qty,
712 "parenttype": d.parenttype,
713 "parent": d.parent
Anand Doshi39edcbc2014-07-28 16:00:19 +0530714 });
mbauskarc79415e2016-01-18 16:32:24 +0530715
716 // if doctype is Quotation Item / Sales Order Iten then add Margin Type and rate in item_list
mbauskara52472c2016-03-05 15:10:25 +0530717 if (in_list(["Quotation Item", "Sales Order Item", "Delivery Note Item", "Sales Invoice Item"]), d.doctype){
718 item_list[0]["margin_type"] = d.margin_type
719 item_list[0]["margin_rate_or_amount"] = d.margin_rate_or_amount
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +0530720 }
Anand Doshi39edcbc2014-07-28 16:00:19 +0530721 }
Nabin Hait444f9562014-06-20 15:59:49 +0530722 };
Nabin Haita3dd72a2014-05-28 12:49:20 +0530723
Nabin Hait444f9562014-06-20 15:59:49 +0530724 if (item) {
725 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +0530726 } else {
Nabin Hait96339342015-01-21 17:22:11 +0530727 $.each(this.frm.doc["items"] || [], function(i, d) {
Nabin Hait444f9562014-06-20 15:59:49 +0530728 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +0530729 });
730 }
Nabin Hait444f9562014-06-20 15:59:49 +0530731 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +0530732 },
733
Anand Doshidffec8f2014-07-01 17:45:15 +0530734 _set_values_for_item_list: function(children) {
Nabin Hait49a27292014-12-17 11:21:32 +0530735 var me = this;
Nabin Hait7d8fa802014-12-30 15:35:45 +0530736 var price_list_rate_changed = false;
Anand Doshidea3fb02015-02-05 15:05:58 +0530737 for(var i=0, l=children.length; i<l; i++) {
738 var d = children[i];
Nabin Hait49a27292014-12-17 11:21:32 +0530739 var existing_pricing_rule = frappe.model.get_value(d.doctype, d.name, "pricing_rule");
Anand Doshidea3fb02015-02-05 15:05:58 +0530740
741 for(var k in d) {
742 var v = d[k];
Anand Doshidffec8f2014-07-01 17:45:15 +0530743 if (["doctype", "name"].indexOf(k)===-1) {
Nabin Hait7d8fa802014-12-30 15:35:45 +0530744 if(k=="price_list_rate") {
745 if(flt(v) != flt(d.price_list_rate)) price_list_rate_changed = true;
746 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530747 frappe.model.set_value(d.doctype, d.name, k, v);
748 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530749 }
750
Nabin Hait49a27292014-12-17 11:21:32 +0530751 // if pricing rule set as blank from an existing value, apply price_list
Nabin Haitcfe3c542014-12-22 10:42:48 +0530752 if(!me.frm.doc.ignore_pricing_rule && existing_pricing_rule && !d.pricing_rule) {
Nabin Hait49a27292014-12-17 11:21:32 +0530753 me.apply_price_list(frappe.get_doc(d.doctype, d.name));
754 }
Anand Doshidea3fb02015-02-05 15:05:58 +0530755 }
Nabin Hait7d8fa802014-12-30 15:35:45 +0530756
Anand Doshidea3fb02015-02-05 15:05:58 +0530757 if(!price_list_rate_changed) me.calculate_taxes_and_totals();
Anand Doshidffec8f2014-07-01 17:45:15 +0530758 },
759
Nabin Hait49a27292014-12-17 11:21:32 +0530760 apply_price_list: function(item) {
Anand Doshidffec8f2014-07-01 17:45:15 +0530761 var me = this;
Rushabh Mehta052bd622015-01-30 16:57:24 +0530762 var args = this._get_args(item);
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530763 if (!((args.items && args.items.length) || args.price_list)) {
Anand Doshi70f57eb2015-11-27 14:37:40 +0530764 return;
765 }
Nabin Hait37b047d2015-02-23 16:01:33 +0530766
Anand Doshidffec8f2014-07-01 17:45:15 +0530767 return this.frm.call({
768 method: "erpnext.stock.get_item_details.apply_price_list",
Rushabh Mehta052bd622015-01-30 16:57:24 +0530769 args: { args: args },
Anand Doshidffec8f2014-07-01 17:45:15 +0530770 callback: function(r) {
771 if (!r.exc) {
Anand Doshi51f722d2014-07-04 15:44:26 +0530772 me.in_apply_price_list = true;
Anand Doshidffec8f2014-07-01 17:45:15 +0530773 me.frm.set_value("price_list_currency", r.message.parent.price_list_currency);
774 me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate);
Anand Doshi51f722d2014-07-04 15:44:26 +0530775 me.in_apply_price_list = false;
Nabin Hait37b047d2015-02-23 16:01:33 +0530776
Rushabh Mehtab46069d2016-01-15 16:59:26 +0530777 if(args.items.length) {
Nabin Hait37b047d2015-02-23 16:01:33 +0530778 me._set_values_for_item_list(r.message.children);
779 }
Anand Doshidffec8f2014-07-01 17:45:15 +0530780 }
781 }
782 });
783 },
784
Anand Doshi3543f302013-05-24 19:25:01 +0530785 get_item_wise_taxes_html: function() {
786 var item_tax = {};
787 var tax_accounts = [];
788 var company_currency = this.get_company_currency();
Rushabh Mehtaff938022014-04-15 18:40:00 +0530789
Nabin Hait96339342015-01-21 17:22:11 +0530790 $.each(this.frm.doc["taxes"] || [], function(i, tax) {
Anand Doshi3543f302013-05-24 19:25:01 +0530791 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530792 var tax_rate_precision = precision("rate", tax);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530793 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530794 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530795 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530796 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530797 var tax_rate = "";
798 if(tax_data[0] != null) {
799 tax_rate = (tax.charge_type === "Actual") ?
800 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
801 (flt(tax_data[0], tax_rate_precision) + "%");
802 }
803 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
804 tax_amount_precision);
Rushabh Mehtaff938022014-04-15 18:40:00 +0530805
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530806 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530807 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530808 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530809 }
Anand Doshi3543f302013-05-24 19:25:01 +0530810 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530811 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530812 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530813
814 var headings = $.map([__("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530815 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530816
Anand Doshi179e3772013-09-05 16:53:57 +0530817 var distinct_item_names = [];
818 var distinct_items = [];
Nabin Hait96339342015-01-21 17:22:11 +0530819 $.each(this.frm.doc["items"] || [], function(i, item) {
Anand Doshi179e3772013-09-05 16:53:57 +0530820 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
821 distinct_item_names.push(item.item_code || item.item_name);
822 distinct_items.push(item);
823 }
824 });
Rushabh Mehtaff938022014-04-15 18:40:00 +0530825
Anand Doshi179e3772013-09-05 16:53:57 +0530826 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530827 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530828 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530829 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
830 item_name: item.item_name,
831 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530832 return item_tax_record[head[0]] ?
833 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530834 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530835 }).join("\n")
836 });
837 }).join("\n");
Rushabh Mehtaff938022014-04-15 18:40:00 +0530838
Anand Doshi53b73422013-05-31 11:50:01 +0530839 if(!rows) return "";
Rushabh Mehtaf24f6042015-08-20 18:01:40 +0530840 return '<p><a class="h6 text-muted" href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">'
841 + __("Show tax break-up") + '</a><br><br></p>\
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530842 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530843 <thead><tr>' + headings + '</tr></thead> \
844 <tbody>' + rows + '</tbody> \
845 </table></div>';
846 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530847
Nabin Hait139dc7b2014-02-12 14:53:18 +0530848 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530849 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530850 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +0530851
Nabin Hait0f231062014-02-20 11:27:47 +0530852 $.each(["company", "customer"], function(i, fieldname) {
Saurabh8a8ef852015-10-21 19:28:53 +0530853 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname && me.frm.doc.doctype != "Purchase Order")) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530854 if (!me.frm.doc[fieldname]) {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530855 msgprint(__("Please specify") + ": " +
856 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +0530857 ". " + __("It is needed to fetch Item Details."));
Nabin Hait139dc7b2014-02-12 14:53:18 +0530858 valid = false;
859 }
Anand Doshi3543f302013-05-24 19:25:01 +0530860 }
861 });
862 return valid;
863 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530864
Anand Doshibf3e54a2013-06-05 14:11:32 +0530865 get_terms: function() {
866 var me = this;
867 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530868 return this.frm.call({
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530869 method: "frappe.client.get_value",
Anand Doshibf3e54a2013-06-05 14:11:32 +0530870 args: {
871 doctype: "Terms and Conditions",
872 fieldname: "terms",
873 filters: { name: this.frm.doc.tc_name },
874 },
875 });
876 }
877 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530878
879 taxes_and_charges: function() {
880 var me = this;
881 if(this.frm.doc.taxes_and_charges) {
882 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +0530883 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
884 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +0530885 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
886 this.frm.doc.name).options,
Nabin Hait6b039142014-05-02 15:45:10 +0530887 "master_name": this.frm.doc.taxes_and_charges,
Nabin Hait6b039142014-05-02 15:45:10 +0530888 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530889 callback: function(r) {
890 if(!r.exc) {
Nabin Haitdd38a262014-12-26 13:15:21 +0530891 me.frm.set_value("taxes", r.message);
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530892 me.calculate_taxes_and_totals();
893 }
894 }
895 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +0530896 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530897 },
898
899 show_item_wise_taxes: function() {
900 if(this.frm.fields_dict.other_charges_calculation) {
Anand Doshidd942bb2015-02-04 18:52:35 +0530901 var html = this.get_item_wise_taxes_html();
902 if (html) {
903 this.frm.toggle_display("other_charges_calculation", true);
904 $(this.frm.fields_dict.other_charges_calculation.wrapper).html(html);
905 } else {
906 this.frm.toggle_display("other_charges_calculation", false);
907 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530908 }
909 },
Anand Doshi13945092014-09-21 19:45:49 +0530910
911 is_recurring: function() {
912 // set default values for recurring documents
Rushabh Mehta756e5182016-03-03 13:00:17 +0530913 if(this.frm.doc.is_recurring && this.frm.doc.__islocal) {
914 frappe.msgprint(__("Please set recurring after saving"));
915 this.frm.set_value('is_recurring', 0);
916 return;
917 }
918
Anand Doshi13945092014-09-21 19:45:49 +0530919 if(this.frm.doc.is_recurring) {
Nabin Hait68a628e2016-03-21 12:03:16 +0530920 if(!this.frm.doc.recurring_id) {
921 this.frm.set_value('recurring_id', this.frm.doc.name);
922 }
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +0530923
Anand Doshi13945092014-09-21 19:45:49 +0530924 var owner_email = this.frm.doc.owner=="Administrator"
925 ? frappe.user_info("Administrator").email
926 : this.frm.doc.owner;
927
928 this.frm.doc.notification_email_address = $.map([cstr(owner_email),
929 cstr(this.frm.doc.contact_email)], function(v) { return v || null; }).join(", ");
930 this.frm.doc.repeat_on_day_of_month = frappe.datetime.str_to_obj(this.frm.doc.posting_date).getDate();
931 }
932
933 refresh_many(["notification_email_address", "repeat_on_day_of_month"]);
934 },
935
936 from_date: function() {
937 // set to_date
938 if(this.frm.doc.from_date) {
939 var recurring_type_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6,
940 'Yearly': 12};
941
942 var months = recurring_type_map[this.frm.doc.recurring_type];
943 if(months) {
944 var to_date = frappe.datetime.add_months(this.frm.doc.from_date,
945 months);
946 this.frm.doc.to_date = frappe.datetime.add_days(to_date, -1);
947 refresh_field('to_date');
948 }
949 }
Saurabhd3135532016-02-25 18:59:20 +0530950 },
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +0530951
Saurabh2d7af632016-02-26 11:09:20 +0530952 set_gross_profit: function(item) {
Saurabh5ada14b2016-02-26 18:02:55 +0530953 if (this.frm.doc.doctype == "Sales Order" && item.valuation_rate) {
Saurabh3fbf3ce2016-03-09 15:31:04 +0530954 rate = flt(item.rate) * flt(this.frm.doc.conversion_rate || 1);
955 item.gross_profit = flt(((rate - item.valuation_rate) * item.qty), precision("amount", item));
Saurabh2d7af632016-02-26 11:09:20 +0530956 }
Rushabh Mehta203cc962016-04-07 15:25:43 +0530957 },
958
959 setup_item_selector: function() {
Rushabh Mehtabc928242016-05-29 23:08:22 +0530960 // TODO: remove item selector
961
962 return;
Rushabh Mehta203cc962016-04-07 15:25:43 +0530963 if(!this.item_selector) {
964 this.item_selector = new erpnext.ItemSelector({frm: this.frm});
965 }
Nabin Hait28a05282016-06-27 17:41:39 +0530966 },
Rushabh Mehta76b13412016-07-18 14:30:18 +0530967
Nabin Hait28a05282016-06-27 17:41:39 +0530968 get_advances: function() {
969 if(!this.frm.is_return) {
970 return this.frm.call({
971 method: "set_advances",
972 doc: this.frm.doc,
973 callback: function(r, rt) {
974 refresh_field("advances");
975 }
976 })
977 }
Nabin Hait05aefbb2016-06-28 19:42:19 +0530978 },
Rushabh Mehta76b13412016-07-18 14:30:18 +0530979
Nabin Hait05aefbb2016-06-28 19:42:19 +0530980 make_payment_entry: function() {
981 return frappe.call({
Nabin Hait13093b42016-06-29 18:04:37 +0530982 method: "erpnext.accounts.doctype.payment_entry.payment_entry.get_payment_entry",
Nabin Hait05aefbb2016-06-28 19:42:19 +0530983 args: {
984 "dt": cur_frm.doc.doctype,
985 "dn": cur_frm.doc.name
986 },
987 callback: function(r) {
988 var doclist = frappe.model.sync(r.message);
989 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
Nabin Hait56548cb2016-07-01 15:58:39 +0530990 // cur_frm.refresh_fields()
Nabin Hait05aefbb2016-06-28 19:42:19 +0530991 }
992 });
Anand Doshi13945092014-09-21 19:45:49 +0530993 }
Rushabh Mehtabe2ee182016-04-29 17:22:42 +0530994});