blob: 8ca076d75542507f4c72dab03150ecc2dbc9b6aa [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302// License: GNU General Public License v3. See license.txt
Rushabh Mehta3966f1d2012-02-23 12:35:32 +05303
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +05304// Preset
5// ------
6// cur_frm.cscript.tname - Details table name
7// cur_frm.cscript.fname - Details fieldname
Rushabh Mehta6de403f2013-12-13 14:10:14 +05308// cur_frm.cscript.other_fname - fieldname
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +05309// cur_frm.cscript.sales_team_fname - Sales Team fieldname
10
Anand Doshi1dde46a2013-05-15 21:15:57 +053011wn.provide("erpnext.selling");
Rushabh Mehta6de403f2013-12-13 14:10:14 +053012wn.require("assets/erpnext/js/transaction.js");
Rushabh Mehtab09d9da2014-01-02 11:47:23 +053013
Rushabh Mehta6de403f2013-12-13 14:10:14 +053014{% include "public/js/controllers/accounts.js" %}
Anand Doshi1dde46a2013-05-15 21:15:57 +053015
Anand Doshi3543f302013-05-24 19:25:01 +053016erpnext.selling.SellingController = erpnext.TransactionController.extend({
Anand Doshi9b496142013-07-11 19:13:58 +053017 onload: function() {
18 this._super();
19 this.toggle_rounded_total();
20 this.setup_queries();
Anand Doshi33fe8672013-08-02 12:39:10 +053021 this.toggle_editable_price_list_rate();
Anand Doshi9b496142013-07-11 19:13:58 +053022 },
23
24 setup_queries: function() {
Anand Doshi99100a42013-07-04 17:13:53 +053025 var me = this;
26
Anand Doshif3096132013-05-21 19:35:06 +053027 this.frm.add_fetch("sales_partner", "commission_rate", "commission_rate");
Anand Doshi8f9f8a42013-06-28 19:18:33 +053028
Anand Doshid5d39ac2013-07-29 13:28:37 +053029 $.each([["customer_address", "customer_filter"],
30 ["shipping_address_name", "customer_filter"],
31 ["contact_person", "customer_filter"],
32 ["customer", "customer"],
33 ["lead", "lead"]],
34 function(i, opts) {
35 if(me.frm.fields_dict[opts[0]])
36 me.frm.set_query(opts[0], erpnext.queries[opts[1]]);
37 });
Anand Doshi99100a42013-07-04 17:13:53 +053038
39 if(this.frm.fields_dict.charge) {
40 this.frm.set_query("charge", function() {
Saurabhf52dc072013-07-10 13:07:49 +053041 return {
42 filters: [
43 ['Sales Taxes and Charges Master', 'company', '=', me.frm.doc.company],
Saurabhf52dc072013-07-10 13:07:49 +053044 ['Sales Taxes and Charges Master', 'docstatus', '!=', 2]
45 ]
46 }
Anand Doshi99100a42013-07-04 17:13:53 +053047 });
48 }
Anand Doshi99100a42013-07-04 17:13:53 +053049
Rushabh Mehta4a404e92013-08-09 18:11:35 +053050 if(this.frm.fields_dict.selling_price_list) {
51 this.frm.set_query("selling_price_list", function() {
Anand Doshi720a01a2013-07-26 11:32:02 +053052 return { filters: { buying_or_selling: "Selling" } };
53 });
Anand Doshi720a01a2013-07-26 11:32:02 +053054 }
55
Anand Doshi99100a42013-07-04 17:13:53 +053056 if(!this.fname) {
57 return;
58 }
59
60 if(this.frm.fields_dict[this.fname].grid.get_field('item_code')) {
61 this.frm.set_query("item_code", this.fname, function() {
Anand Doshi9b496142013-07-11 19:13:58 +053062 return {
Rushabh Mehta6de403f2013-12-13 14:10:14 +053063 query: "erpnext.controllers.queries.item_query",
Anand Doshi9b496142013-07-11 19:13:58 +053064 filters: (me.frm.doc.order_type === "Maintenance" ?
65 {'is_service_item': 'Yes'}:
66 {'is_sales_item': 'Yes' })
67 }
Anand Doshi99100a42013-07-04 17:13:53 +053068 });
69 }
70
71 if(this.frm.fields_dict[this.fname].grid.get_field('batch_no')) {
72 this.frm.set_query("batch_no", this.fname, function(doc, cdt, cdn) {
73 var item = wn.model.get_doc(cdt, cdn);
74 if(!item.item_code) {
Bárbara Perretti4098c262013-09-27 17:05:17 -030075 wn.throw(wn._("Please enter Item Code to get batch no"));
Anand Doshi99100a42013-07-04 17:13:53 +053076 } else {
Nabin Haitd1fd1e22013-10-18 12:29:11 +053077 filters = {
78 'item_code': item.item_code,
79 'posting_date': me.frm.doc.posting_date,
80 }
81 if(item.warehouse) filters["warehouse"] = item.warehouse
82
83 return {
84 query : "controllers.queries.get_batch_no",
85 filters: filters
Anand Doshi99100a42013-07-04 17:13:53 +053086 }
87 }
88 });
89 }
Anand Doshied698922013-07-23 15:16:50 +053090
91 if(this.frm.fields_dict.sales_team && this.frm.fields_dict.sales_team.grid.get_field("sales_person")) {
Anand Doshid5d39ac2013-07-29 13:28:37 +053092 this.frm.set_query("sales_person", "sales_team", erpnext.queries.not_a_group_filter);
Anand Doshied698922013-07-23 15:16:50 +053093 }
Anand Doshi1dde46a2013-05-15 21:15:57 +053094 },
95
Anand Doshic4a54fe2013-08-01 18:19:51 +053096 refresh: function() {
97 this._super();
Rushabh Mehta8aded132013-07-04 12:50:52 +053098 this.frm.toggle_display("customer_name",
Akhilesh Darjee5ce1b8b2013-12-09 16:29:04 +053099 (this.frm.doc.customer_name && this.frm.doc.customer_name!==this.frm.doc.customer));
Anand Doshic4a54fe2013-08-01 18:19:51 +0530100 if(this.frm.fields_dict.packing_details) {
101 var packing_list_exists = this.frm.get_doclist({parentfield: "packing_details"}).length;
102 this.frm.toggle_display("packing_list", packing_list_exists ? true : false);
103 }
Rushabh Mehta8aded132013-07-04 12:50:52 +0530104 },
105
Anand Doshi3543f302013-05-24 19:25:01 +0530106 customer: function() {
Rushabh Mehtab09d9da2014-01-02 11:47:23 +0530107 erpnext.utils.get_customer_details(this.frm);
Anand Doshi3543f302013-05-24 19:25:01 +0530108 },
109
Nabin Haita279d782013-07-15 13:04:33 +0530110 customer_address: function() {
Rushabh Mehtab09d9da2014-01-02 11:47:23 +0530111 erpnext.utils.get_address_display(this.frm, "customer_address");
Nabin Haita279d782013-07-15 13:04:33 +0530112 },
113
114 contact_person: function() {
Rushabh Mehtab09d9da2014-01-02 11:47:23 +0530115 erpnext.utils.get_contact_details(this.frm);
Nabin Haita279d782013-07-15 13:04:33 +0530116 },
117
Anand Doshif3096132013-05-21 19:35:06 +0530118 barcode: function(doc, cdt, cdn) {
119 this.item_code(doc, cdt, cdn);
Anand Doshi1dde46a2013-05-15 21:15:57 +0530120 },
121
122 item_code: function(doc, cdt, cdn) {
123 var me = this;
124 var item = wn.model.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530125 if(item.item_code || item.barcode || item.serial_no) {
Anand Doshi3543f302013-05-24 19:25:01 +0530126 if(!this.validate_company_and_party("customer")) {
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530127 cur_frm.fields_dict[me.frm.cscript.fname].grid.grid_rows[item.idx - 1].remove();
Anand Doshi1dde46a2013-05-15 21:15:57 +0530128 } else {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530129 return this.frm.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530130 method: "erpnext.selling.utils.get_item_details",
Anand Doshi1dde46a2013-05-15 21:15:57 +0530131 child: item,
132 args: {
133 args: {
134 item_code: item.item_code,
Anand Doshif3096132013-05-21 19:35:06 +0530135 barcode: item.barcode,
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530136 serial_no: item.serial_no,
Anand Doshi1dde46a2013-05-15 21:15:57 +0530137 warehouse: item.warehouse,
138 doctype: me.frm.doc.doctype,
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530139 parentfield: item.parentfield,
Anand Doshi1dde46a2013-05-15 21:15:57 +0530140 customer: me.frm.doc.customer,
141 currency: me.frm.doc.currency,
142 conversion_rate: me.frm.doc.conversion_rate,
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530143 selling_price_list: me.frm.doc.selling_price_list,
Anand Doshi1dde46a2013-05-15 21:15:57 +0530144 price_list_currency: me.frm.doc.price_list_currency,
145 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
146 company: me.frm.doc.company,
Anand Doshif3096132013-05-21 19:35:06 +0530147 order_type: me.frm.doc.order_type,
148 is_pos: cint(me.frm.doc.is_pos),
Anand Doshi1dde46a2013-05-15 21:15:57 +0530149 }
150 },
151 callback: function(r) {
Anand Doshif3096132013-05-21 19:35:06 +0530152 if(!r.exc) {
Anand Doshi80166e22013-07-17 14:49:13 +0530153 me.frm.script_manager.trigger("ref_rate", cdt, cdn);
Anand Doshif3096132013-05-21 19:35:06 +0530154 }
Anand Doshi1dde46a2013-05-15 21:15:57 +0530155 }
156 });
157 }
158 }
159 },
160
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530161 selling_price_list: function() {
162 this.get_price_list_currency("Selling");
Anand Doshif3096132013-05-21 19:35:06 +0530163 },
164
165 ref_rate: function(doc, cdt, cdn) {
166 var item = wn.model.get_doc(cdt, cdn);
167 wn.model.round_floats_in(item, ["ref_rate", "adj_rate"]);
168
169 item.export_rate = flt(item.ref_rate * (1 - item.adj_rate / 100.0),
170 precision("export_rate", item));
171
172 this.calculate_taxes_and_totals();
173 },
174
Anand Doshif3096132013-05-21 19:35:06 +0530175 adj_rate: function(doc, cdt, cdn) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530176 var item = wn.model.get_doc(cdt, cdn);
177 if(!item.ref_rate) {
178 item.adj_rate = 0.0;
179 } else {
180 this.ref_rate(doc, cdt, cdn);
181 }
Anand Doshif3096132013-05-21 19:35:06 +0530182 },
183
184 export_rate: function(doc, cdt, cdn) {
185 var item = wn.model.get_doc(cdt, cdn);
186 wn.model.round_floats_in(item, ["export_rate", "ref_rate"]);
187
188 if(item.ref_rate) {
189 item.adj_rate = flt((1 - item.export_rate / item.ref_rate) * 100.0,
190 precision("adj_rate", item));
191 } else {
192 item.adj_rate = 0.0;
193 }
194
195 this.calculate_taxes_and_totals();
196 },
197
Anand Doshif3096132013-05-21 19:35:06 +0530198 commission_rate: function() {
199 this.calculate_commission();
200 refresh_field("total_commission");
201 },
202
203 total_commission: function() {
204 if(this.frm.doc.net_total) {
205 wn.model.round_floats_in(this.frm.doc, ["net_total", "total_commission"]);
206
207 if(this.frm.doc.net_total < this.frm.doc.total_commission) {
208 var msg = (wn._("[Error]") + " " +
209 wn._(wn.meta.get_label(this.frm.doc.doctype, "total_commission",
210 this.frm.doc.name)) + " > " +
211 wn._(wn.meta.get_label(this.frm.doc.doctype, "net_total", this.frm.doc.name)));
212 msgprint(msg);
213 throw msg;
214 }
215
216 this.frm.set_value("commission_rate",
217 flt(this.frm.doc.total_commission * 100.0 / this.frm.doc.net_total));
218 }
219 },
220
221 allocated_percentage: function(doc, cdt, cdn) {
222 var sales_person = wn.model.get_doc(cdt, cdn);
223
224 if(sales_person.allocated_percentage) {
225 sales_person.allocated_percentage = flt(sales_person.allocated_percentage,
226 precision("allocated_percentage", sales_person));
227 sales_person.allocated_amount = flt(this.frm.doc.net_total *
228 sales_person.allocated_percentage / 100.0,
229 precision("allocated_amount", sales_person));
230
231 refresh_field(["allocated_percentage", "allocated_amount"], sales_person.name,
232 sales_person.parentfield);
233 }
234 },
235
Anand Doshifc777182013-05-27 19:29:07 +0530236 warehouse: function(doc, cdt, cdn) {
Anand Doshi7dd50342013-07-22 16:36:11 +0530237 var item = wn.model.get_doc(cdt, cdn);
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530238 if(item.item_code && item.warehouse) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530239 return this.frm.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530240 method: "erpnext.selling.utils.get_available_qty",
Anand Doshifc777182013-05-27 19:29:07 +0530241 child: item,
242 args: {
243 item_code: item.item_code,
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530244 warehouse: item.warehouse,
Anand Doshifc777182013-05-27 19:29:07 +0530245 },
246 });
247 }
248 },
249
Anand Doshif3096132013-05-21 19:35:06 +0530250 toggle_rounded_total: function() {
251 var me = this;
252 if(cint(wn.defaults.get_global_default("disable_rounded_total"))) {
253 $.each(["rounded_total", "rounded_total_export"], function(i, fieldname) {
254 me.frm.set_df_property(fieldname, "print_hide", 1);
255 me.frm.toggle_display(fieldname, false);
256 });
257 }
258 },
259
Anand Doshi33fe8672013-08-02 12:39:10 +0530260 toggle_editable_price_list_rate: function() {
261 var df = wn.meta.get_docfield(this.tname, "ref_rate", this.frm.doc.name);
262 var editable_price_list_rate = cint(wn.defaults.get_default("editable_price_list_rate"));
263
264 if(df && editable_price_list_rate) {
265 df.read_only = 0;
266 }
267 },
268
Anand Doshif3096132013-05-21 19:35:06 +0530269 calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530270 this._super();
Anand Doshi923d41d2013-05-28 17:23:36 +0530271 this.calculate_total_advance("Sales Invoice", "advance_adjustment_details");
Anand Doshif3096132013-05-21 19:35:06 +0530272 this.calculate_commission();
273 this.calculate_contribution();
Anand Doshi923d41d2013-05-28 17:23:36 +0530274
275 // TODO check for custom_recalc in custom scripts of server
Anand Doshif3096132013-05-21 19:35:06 +0530276
Anand Doshi2168e392013-05-23 19:25:08 +0530277 this.frm.refresh_fields();
Anand Doshif3096132013-05-21 19:35:06 +0530278 },
279
280 calculate_item_values: function() {
281 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530282 $.each(this.frm.item_doclist, function(i, item) {
283 wn.model.round_floats_in(item);
284 item.export_amount = flt(item.export_rate * item.qty, precision("export_amount", item));
285
Anand Doshi3543f302013-05-24 19:25:01 +0530286 me._set_in_company_currency(item, "ref_rate", "base_ref_rate");
287 me._set_in_company_currency(item, "export_rate", "basic_rate");
288 me._set_in_company_currency(item, "export_amount", "amount");
Anand Doshif3096132013-05-21 19:35:06 +0530289 });
290
291 },
292
Anand Doshif3096132013-05-21 19:35:06 +0530293 determine_exclusive_rate: function() {
294 var me = this;
295 $.each(me.frm.item_doclist, function(n, item) {
296 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
297 var cumulated_tax_fraction = 0.0;
298
299 $.each(me.frm.tax_doclist, function(i, tax) {
300 tax.tax_fraction_for_current_item = me.get_current_tax_fraction(tax, item_tax_map);
301
302 if(i==0) {
Anand Doshi2168e392013-05-23 19:25:08 +0530303 tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530304 } else {
Anand Doshi2168e392013-05-23 19:25:08 +0530305 tax.grand_total_fraction_for_current_item =
306 me.frm.tax_doclist[i-1].grand_total_fraction_for_current_item +
Anand Doshif3096132013-05-21 19:35:06 +0530307 tax.tax_fraction_for_current_item;
308 }
309
310 cumulated_tax_fraction += tax.tax_fraction_for_current_item;
311 });
312
313 if(cumulated_tax_fraction) {
Anand Doshifbe1e162013-08-06 19:38:19 +0530314 item.amount = flt(
315 (item.export_amount * me.frm.doc.conversion_rate) / (1 + cumulated_tax_fraction),
316 precision("amount", item));
317
318 item.basic_rate = flt(item.amount / item.qty, precision("basic_rate", item));
Anand Doshif3096132013-05-21 19:35:06 +0530319
320 if(item.adj_rate == 100) {
321 item.base_ref_rate = item.basic_rate;
322 item.basic_rate = 0.0;
323 } else {
324 item.base_ref_rate = flt(item.basic_rate / (1 - item.adj_rate / 100.0),
325 precision("base_ref_rate", item));
326 }
327 }
328 });
329 },
330
331 get_current_tax_fraction: function(tax, item_tax_map) {
332 // Get tax fraction for calculating tax exclusive amount
333 // from tax inclusive amount
334 var current_tax_fraction = 0.0;
335
336 if(cint(tax.included_in_print_rate)) {
Anand Doshi2168e392013-05-23 19:25:08 +0530337 var tax_rate = this._get_tax_rate(tax, item_tax_map);
Anand Doshif3096132013-05-21 19:35:06 +0530338
339 if(tax.charge_type == "On Net Total") {
340 current_tax_fraction = (tax_rate / 100.0);
341
342 } else if(tax.charge_type == "On Previous Row Amount") {
343 current_tax_fraction = (tax_rate / 100.0) *
Anand Doshi2168e392013-05-23 19:25:08 +0530344 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530345
346 } else if(tax.charge_type == "On Previous Row Total") {
347 current_tax_fraction = (tax_rate / 100.0) *
Anand Doshi2168e392013-05-23 19:25:08 +0530348 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_fraction_for_current_item;
Anand Doshif3096132013-05-21 19:35:06 +0530349 }
350 }
351
352 return current_tax_fraction;
353 },
354
355 calculate_net_total: function() {
356 var me = this;
357
358 this.frm.doc.net_total = this.frm.doc.net_total_export = 0.0;
359 $.each(this.frm.item_doclist, function(i, item) {
360 me.frm.doc.net_total += item.amount;
361 me.frm.doc.net_total_export += item.export_amount;
362 });
363
364 wn.model.round_floats_in(this.frm.doc, ["net_total", "net_total_export"]);
365 },
366
Anand Doshif3096132013-05-21 19:35:06 +0530367 calculate_totals: function() {
368 var tax_count = this.frm.tax_doclist.length;
369 this.frm.doc.grand_total = flt(
370 tax_count ? this.frm.tax_doclist[tax_count - 1].total : this.frm.doc.net_total,
371 precision("grand_total"));
372 this.frm.doc.grand_total_export = flt(this.frm.doc.grand_total / this.frm.doc.conversion_rate,
373 precision("grand_total_export"));
374
375 this.frm.doc.other_charges_total = flt(this.frm.doc.grand_total - this.frm.doc.net_total,
376 precision("other_charges_total"));
377 this.frm.doc.other_charges_total_export = flt(
378 this.frm.doc.grand_total_export - this.frm.doc.net_total_export,
379 precision("other_charges_total_export"));
380
381 this.frm.doc.rounded_total = Math.round(this.frm.doc.grand_total);
382 this.frm.doc.rounded_total_export = Math.round(this.frm.doc.grand_total_export);
383 },
384
Anand Doshifc777182013-05-27 19:29:07 +0530385 calculate_outstanding_amount: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530386 // NOTE:
Akhilesh Darjee38e8f982013-09-05 12:59:33 +0530387 // paid_amount and write_off_amount is only for POS Invoice
Anand Doshi923d41d2013-05-28 17:23:36 +0530388 // total_advance is only for non POS Invoice
Anand Doshi29ea5d02013-07-05 17:23:14 +0530389 if(this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.docstatus==0) {
Anand Doshifc777182013-05-27 19:29:07 +0530390 wn.model.round_floats_in(this.frm.doc, ["grand_total", "total_advance", "write_off_amount",
391 "paid_amount"]);
Akhilesh Darjee38e8f982013-09-05 12:59:33 +0530392 var total_amount_to_pay = this.frm.doc.grand_total - this.frm.doc.write_off_amount - this.frm.doc.total_advance;
393 this.frm.doc.paid_amount = this.frm.doc.is_pos? flt(total_amount_to_pay): 0.0;
394
395 this.frm.doc.outstanding_amount = flt(total_amount_to_pay - this.frm.doc.paid_amount,
396 precision("outstanding_amount"));
Anand Doshifc777182013-05-27 19:29:07 +0530397 }
398 },
399
Anand Doshif3096132013-05-21 19:35:06 +0530400 calculate_commission: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530401 if(this.frm.fields_dict.commission_rate) {
402 if(this.frm.doc.commission_rate > 100) {
403 var msg = wn._(wn.meta.get_label(this.frm.doc.doctype, "commission_rate", this.frm.doc.name)) +
404 " " + wn._("cannot be greater than 100");
405 msgprint(msg);
406 throw msg;
407 }
Anand Doshif3096132013-05-21 19:35:06 +0530408
Anand Doshi923d41d2013-05-28 17:23:36 +0530409 this.frm.doc.total_commission = flt(this.frm.doc.net_total * this.frm.doc.commission_rate / 100.0,
410 precision("total_commission"));
411 }
Anand Doshif3096132013-05-21 19:35:06 +0530412 },
413
414 calculate_contribution: function() {
Anand Doshi2168e392013-05-23 19:25:08 +0530415 var me = this;
Anand Doshif3096132013-05-21 19:35:06 +0530416 $.each(wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
417 {parentfield: "sales_team"}), function(i, sales_person) {
418 wn.model.round_floats_in(sales_person);
419 if(sales_person.allocated_percentage) {
420 sales_person.allocated_amount = flt(
421 me.frm.doc.net_total * sales_person.allocated_percentage / 100.0,
422 precision("allocated_amount", sales_person));
423 }
424 });
425 },
426
427 _cleanup: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530428 this._super();
429 this.frm.doc.in_words = this.frm.doc.in_words_export = "";
Anand Doshif3096132013-05-21 19:35:06 +0530430 },
Anand Doshi2168e392013-05-23 19:25:08 +0530431
432 show_item_wise_taxes: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530433 if(this.frm.fields_dict.other_charges_calculation) {
434 $(this.get_item_wise_taxes_html())
435 .appendTo($(this.frm.fields_dict.other_charges_calculation.wrapper).empty());
436 }
Anand Doshi2168e392013-05-23 19:25:08 +0530437 },
438
Anand Doshi77cd10b2013-06-27 12:17:30 +0530439 charge: function() {
Anand Doshi2168e392013-05-23 19:25:08 +0530440 var me = this;
441 if(this.frm.doc.charge) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530442 return this.frm.call({
Anand Doshi2168e392013-05-23 19:25:08 +0530443 doc: this.frm.doc,
444 method: "get_other_charges",
445 callback: function(r) {
446 if(!r.exc) {
447 me.calculate_taxes_and_totals();
448 }
449 }
450 });
451 }
452 },
453
Anand Doshicefccb92013-07-15 18:28:14 +0530454 shipping_rule: function() {
455 var me = this;
456 if(this.frm.doc.shipping_rule) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530457 return this.frm.call({
Anand Doshicefccb92013-07-15 18:28:14 +0530458 doc: this.frm.doc,
459 method: "apply_shipping_rule",
460 callback: function(r) {
461 if(!r.exc) {
462 me.calculate_taxes_and_totals();
463 }
464 }
465 })
466 }
467 },
468
Anand Doshi2168e392013-05-23 19:25:08 +0530469 set_dynamic_labels: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530470 this._super();
Anand Doshi9d049242013-06-05 20:46:56 +0530471 set_sales_bom_help(this.frm.doc);
Anand Doshi2168e392013-05-23 19:25:08 +0530472 },
473
474 change_form_labels: function(company_currency) {
475 var me = this;
476 var field_label_map = {};
477
478 var setup_field_label_map = function(fields_list, currency) {
479 $.each(fields_list, function(i, fname) {
Anand Doshi99100a42013-07-04 17:13:53 +0530480 var docfield = wn.meta.docfield_map[me.frm.doc.doctype][fname];
Anand Doshi2168e392013-05-23 19:25:08 +0530481 if(docfield) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530482 var label = wn._(docfield.label || "").replace(/\([^\)]*\)/g, "");
Anand Doshi2168e392013-05-23 19:25:08 +0530483 field_label_map[fname] = label.trim() + " (" + currency + ")";
484 }
485 });
486 };
Anand Doshi2168e392013-05-23 19:25:08 +0530487 setup_field_label_map(["net_total", "other_charges_total", "grand_total",
488 "rounded_total", "in_words",
489 "outstanding_amount", "total_advance", "paid_amount", "write_off_amount"],
490 company_currency);
491
492 setup_field_label_map(["net_total_export", "other_charges_total_export", "grand_total_export",
493 "rounded_total_export", "in_words_export"], this.frm.doc.currency);
494
Rushabh Mehtaef584552013-11-02 14:47:11 +0530495 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
496 + " = [?] " + company_currency)
Anand Doshi2168e392013-05-23 19:25:08 +0530497
498 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
Rushabh Mehtaef584552013-11-02 14:47:11 +0530499 cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency
500 + " = [?] " + company_currency)
Anand Doshi2168e392013-05-23 19:25:08 +0530501 }
502
503 // toggle fields
504 this.frm.toggle_display(["conversion_rate", "net_total", "other_charges_total",
505 "grand_total", "rounded_total", "in_words"],
506 this.frm.doc.currency != company_currency);
507
Rushabh Mehtaa063b4d2013-11-06 11:29:47 +0530508 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
Anand Doshi2168e392013-05-23 19:25:08 +0530509 this.frm.doc.price_list_currency != company_currency);
510
511 // set labels
512 $.each(field_label_map, function(fname, label) {
513 me.frm.fields_dict[fname].set_label(label);
514 });
515 },
516
517 change_grid_labels: function(company_currency) {
518 var me = this;
519 var field_label_map = {};
520
521 var setup_field_label_map = function(fields_list, currency, parentfield) {
522 var grid_doctype = me.frm.fields_dict[parentfield].grid.doctype;
523 $.each(fields_list, function(i, fname) {
Anand Doshi99100a42013-07-04 17:13:53 +0530524 var docfield = wn.meta.docfield_map[grid_doctype][fname];
Anand Doshi2168e392013-05-23 19:25:08 +0530525 if(docfield) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530526 var label = wn._(docfield.label || "").replace(/\([^\)]*\)/g, "");
Anand Doshi2168e392013-05-23 19:25:08 +0530527 field_label_map[grid_doctype + "-" + fname] =
528 label.trim() + " (" + currency + ")";
529 }
530 });
531 }
532
533 setup_field_label_map(["basic_rate", "base_ref_rate", "amount"],
534 company_currency, this.fname);
535
536 setup_field_label_map(["export_rate", "ref_rate", "export_amount"],
537 this.frm.doc.currency, this.fname);
538
539 setup_field_label_map(["tax_amount", "total"], company_currency, "other_charges");
540
541 if(this.frm.fields_dict["advance_allocation_details"]) {
542 setup_field_label_map(["advance_amount", "allocated_amount"], company_currency,
543 "advance_allocation_details");
544 }
545
546 // toggle columns
547 var item_grid = this.frm.fields_dict[this.fname].grid;
Anand Doshiedfba962013-08-05 11:56:54 +0530548 var show = (this.frm.doc.currency != company_currency) ||
549 (wn.model.get_doclist(cur_frm.doctype, cur_frm.docname,
550 {parentfield: "other_charges", included_in_print_rate: 1}).length);
551
Anand Doshi2168e392013-05-23 19:25:08 +0530552 $.each(["basic_rate", "base_ref_rate", "amount"], function(i, fname) {
553 if(wn.meta.get_docfield(item_grid.doctype, fname))
554 item_grid.set_column_disp(fname, show);
555 });
556
557 // set labels
558 var $wrapper = $(this.frm.wrapper);
559 $.each(field_label_map, function(fname, label) {
Anand Doshi5013dcb2013-08-05 12:16:04 +0530560 fname = fname.split("-");
561 var df = wn.meta.get_docfield(fname[0], fname[1], me.frm.doc.name);
562 if(df) df.label = label;
Anand Doshi2168e392013-05-23 19:25:08 +0530563 });
564 },
565
Anand Doshi8f9f8a42013-06-28 19:18:33 +0530566 shipping_address_name: function () {
567 var me = this;
568 if(this.frm.doc.shipping_address_name) {
569 wn.model.with_doc("Address", this.frm.doc.shipping_address_name, function(name) {
570 var address = wn.model.get_doc("Address", name);
571
572 var out = $.map(["address_line1", "address_line2", "city"],
573 function(f) { return address[f]; });
574
575 var state_pincode = $.map(["state", "pincode"], function(f) { return address[f]; }).join(" ");
576 if(state_pincode) out.push(state_pincode);
577
578 if(address["country"]) out.push(address["country"]);
579
580 out.concat($.map([["Phone:", address["phone"]], ["Fax:", address["fax"]]],
581 function(val) { return val[1] ? val.join(" ") : null; }));
582
583 me.frm.set_value("shipping_address", out.join("\n"));
584 });
585 }
586 }
Anand Doshi1dde46a2013-05-15 21:15:57 +0530587});
588
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530589// Help for Sales BOM items
590var set_sales_bom_help = function(doc) {
Rushabh Mehta2eef40b2012-07-16 14:16:57 +0530591 if(!cur_frm.fields_dict.packing_list) return;
Nabin Haitd1fd1e22013-10-18 12:29:11 +0530592 if (getchildren('Packed Item', doc.name, 'packing_details').length) {
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530593 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(true);
594
595 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
Bárbara Perretti4098c262013-09-27 17:05:17 -0300596 help_msg = "<div class='alert alert-warning'>" +
597 wn._("For 'Sales BOM' items, warehouse, serial no and batch no \
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530598 will be considered from the 'Packing List' table. \
599 If warehouse and batch no are same for all packing items for any 'Sales BOM' item, \
Bárbara Perretti4098c262013-09-27 17:05:17 -0300600 those values can be entered in the main item table, values will be copied to 'Packing List' table.")+
601 "</div>";
Rushabh Mehta2eef40b2012-07-16 14:16:57 +0530602 wn.meta.get_docfield(doc.doctype, 'sales_bom_help', doc.name).options = help_msg;
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530603 }
604 } else {
605 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(false);
606 if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
Rushabh Mehta2eef40b2012-07-16 14:16:57 +0530607 wn.meta.get_docfield(doc.doctype, 'sales_bom_help', doc.name).options = '';
Nabin Haitcfc6bb12012-07-11 13:14:52 +0530608 }
609 }
610 refresh_field('sales_bom_help');
611}