blob: 7a1dc0c6c4531e9a2e397a680cdcdd4ffc8cb5f3 [file] [log] [blame]
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05301// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
2// License: GNU General Public License v3. See license.txt
Anand Doshi3543f302013-05-24 19:25:01 +05303
4wn.provide("erpnext");
Anand Doshia648f462013-07-30 14:42:15 +05305wn.require("app/js/controllers/stock_controller.js");
Anand Doshi3543f302013-05-24 19:25:01 +05306
Anand Doshia648f462013-07-30 14:42:15 +05307erpnext.TransactionController = erpnext.stock.StockController.extend({
Anand Doshi3543f302013-05-24 19:25:01 +05308 onload: function() {
9 if(this.frm.doc.__islocal) {
10 var me = this,
11 today = get_today(),
12 currency = wn.defaults.get_default("currency");
13
14 $.each({
Anand Doshifc777182013-05-27 19:29:07 +053015 posting_date: today,
16 due_date: today,
17 transaction_date: today,
18 currency: currency,
19 price_list_currency: currency,
20 status: "Draft",
21 company: wn.defaults.get_default("company"),
22 fiscal_year: wn.defaults.get_default("fiscal_year"),
23 is_subcontracted: "No",
24 conversion_rate: 1.0,
25 plc_conversion_rate: 1.0
Anand Doshi3543f302013-05-24 19:25:01 +053026 }, function(fieldname, value) {
27 if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname])
28 me.frm.set_value(fieldname, value);
29 });
Anand Doshi535d8332013-07-19 13:51:07 +053030
31 me.frm.script_manager.trigger("company");
Anand Doshi3543f302013-05-24 19:25:01 +053032 }
33 },
34
35 refresh: function() {
36 this.frm.clear_custom_buttons();
37 erpnext.hide_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +053038 erpnext.hide_company();
Anand Doshi3543f302013-05-24 19:25:01 +053039 this.show_item_wise_taxes();
Nabin Hait1f996c32013-07-29 19:00:53 +053040 this.set_dynamic_labels();
Anand Doshi3543f302013-05-24 19:25:01 +053041 },
42
Anand Doshi923d41d2013-05-28 17:23:36 +053043 validate: function() {
44 this.calculate_taxes_and_totals();
45 },
46
Anand Doshi3543f302013-05-24 19:25:01 +053047 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +053048 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Anand Doshi17350b82013-08-01 15:45:23 +053049 if(!this.frm.doc.currency) {
50 this.frm.set_value("currency", this.get_company_currency());
51 }
52
Nabin Hait48a0bd32013-07-23 15:31:39 +053053 this.frm.script_manager.trigger("currency");
Anand Doshi3543f302013-05-24 19:25:01 +053054 }
55 },
56
57 get_company_currency: function() {
58 return erpnext.get_currency(this.frm.doc.company);
59 },
60
61 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +053062 var me = this;
63 this.set_dynamic_labels();
Anand Doshid52b03a2013-06-21 12:22:52 +053064
Anand Doshi61a2f682013-06-21 17:55:31 +053065 var company_currency = this.get_company_currency();
66 if(this.frm.doc.currency !== company_currency) {
67 this.get_exchange_rate(this.frm.doc.currency, company_currency,
68 function(exchange_rate) {
69 if(exchange_rate) {
70 me.frm.set_value("conversion_rate", exchange_rate);
71 me.conversion_rate();
72 }
73 });
74 } else {
75 this.conversion_rate();
76 }
77 },
78
79 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +053080 if(this.frm.doc.currency === this.get_company_currency()) {
81 this.frm.set_value("conversion_rate", 1.0);
Anand Doshi61a2f682013-06-21 17:55:31 +053082 } else if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
83 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
84 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
85 }
86
87 this.calculate_taxes_and_totals();
Anand Doshi3543f302013-05-24 19:25:01 +053088 },
89
Anand Doshi060d9242013-06-12 17:40:36 +053090 price_list_name: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +053091 var me = this;
92 if(this.frm.doc.price_list_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +053093 return this.frm.call({
Anand Doshi3543f302013-05-24 19:25:01 +053094 method: "setup.utils.get_price_list_currency",
Nabin Hait48a0bd32013-07-23 15:31:39 +053095 args: {
Anand Doshi3543f302013-05-24 19:25:01 +053096 price_list_name: this.frm.doc.price_list_name,
Nabin Hait48a0bd32013-07-23 15:31:39 +053097 },
Anand Doshi3543f302013-05-24 19:25:01 +053098 callback: function(r) {
99 if(!r.exc) {
100 me.price_list_currency();
101 }
102 }
103 });
104 }
105 },
106
Anand Doshi61a2f682013-06-21 17:55:31 +0530107 get_exchange_rate: function(from_currency, to_currency, callback) {
108 var exchange_name = from_currency + "-" + to_currency;
109 wn.model.with_doc("Currency Exchange", exchange_name, function(name) {
110 var exchange_doc = wn.model.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530111 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530112 });
113 },
114
Anand Doshi3543f302013-05-24 19:25:01 +0530115 price_list_currency: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530116 this.set_dynamic_labels();
Anand Doshi61a2f682013-06-21 17:55:31 +0530117
118 var company_currency = this.get_company_currency();
119 if(this.frm.doc.price_list_currency !== company_currency) {
120 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
121 function(exchange_rate) {
122 if(exchange_rate) {
123 me.frm.set_value("price_list_currency", exchange_rate);
124 me.plc_conversion_rate();
125 }
126 });
127 } else {
128 this.plc_conversion_rate();
129 }
Anand Doshi3543f302013-05-24 19:25:01 +0530130 },
131
132 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530133 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
134 this.frm.set_value("plc_conversion_rate", 1.0);
135 } else if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
136 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
137 this.calculate_taxes_and_totals();
138 }
Anand Doshi3543f302013-05-24 19:25:01 +0530139 },
140
141 qty: function(doc, cdt, cdn) {
142 this.calculate_taxes_and_totals();
143 },
144
Anand Doshi923d41d2013-05-28 17:23:36 +0530145 tax_rate: function(doc, cdt, cdn) {
146 this.calculate_taxes_and_totals();
147 },
148
149 row_id: function(doc, cdt, cdn) {
150 var tax = wn.model.get_doc(cdt, cdn);
151 try {
152 this.validate_on_previous_row(tax);
153 this.calculate_taxes_and_totals();
154 } catch(e) {
155 tax.row_id = null;
156 refresh_field("row_id", tax.name, tax.parentfield);
157 throw e;
158 }
159 },
160
Anand Doshi61a2f682013-06-21 17:55:31 +0530161 set_dynamic_labels: function() {
162 // What TODO? should we make price list system non-mandatory?
163 this.frm.toggle_reqd("plc_conversion_rate",
164 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
165
166 var company_currency = this.get_company_currency();
167 this.change_form_labels(company_currency);
168 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530169 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530170 },
171
Anand Doshi923d41d2013-05-28 17:23:36 +0530172 recalculate: function() {
173 this.calculate_taxes_and_totals();
174 },
175
176 recalculate_values: function() {
177 this.calculate_taxes_and_totals();
178 },
179
Anand Doshid0b00722013-05-28 18:54:48 +0530180 calculate_charges: function() {
181 this.calculate_taxes_and_totals();
182 },
183
Anand Doshi3543f302013-05-24 19:25:01 +0530184 included_in_print_rate: function(doc, cdt, cdn) {
185 var tax = wn.model.get_doc(cdt, cdn);
186 try {
187 this.validate_on_previous_row(tax);
188 this.validate_inclusive_tax(tax);
189 this.calculate_taxes_and_totals();
190 } catch(e) {
191 tax.included_in_print_rate = 0;
192 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
193 throw e;
194 }
195 },
196
197 validate_on_previous_row: function(tax) {
198 // validate if a valid row id is mentioned in case of
199 // On Previous Row Amount and On Previous Row Total
200 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
201 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
202 var msg = repl(wn._("Row") + " # %(idx)s [%(doctype)s]: " +
203 wn._("Please specify a valid") + " %(row_id_label)s", {
204 idx: tax.idx,
205 doctype: tax.doctype,
206 row_id_label: wn.meta.get_label(tax.doctype, "row_id", tax.name)
207 });
208 msgprint(msg);
209 throw msg;
210 }
211 },
212
213 validate_inclusive_tax: function(tax) {
214 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
215
216 var actual_type_error = function() {
217 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
218 "%(charge_type_label)s = \"%(charge_type)s\" " +
219 wn._("cannot be included in Item's rate"), {
220 idx: tax.idx,
221 doctype: tax.doctype,
222 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
223 charge_type: tax.charge_type
224 });
225 msgprint(msg);
226 throw msg;
227 };
228
229 var on_previous_row_error = function(row_range) {
230 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
231 wn._("to be included in Item's rate, it is required that: ") +
232 " [" + wn._("Row") + " # %(row_range)s] " + wn._("also be included in Item's rate"), {
233 idx: tax.idx,
234 doctype: tax.doctype,
235 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
236 charge_type: tax.charge_type,
237 inclusive_label: wn.meta.get_label(tax.doctype, "included_in_print_rate", tax.name),
238 row_range: row_range,
239 });
240
241 msgprint(msg);
242 throw msg;
243 };
244
245 if(cint(tax.included_in_print_rate)) {
246 if(tax.charge_type == "Actual") {
247 // inclusive tax cannot be of type Actual
248 actual_type_error();
249 } else if(tax.charge_type == "On Previous Row Amount" &&
250 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
251 // referred row should also be an inclusive tax
252 on_previous_row_error(tax.row_id);
253 } else if(tax.charge_type == "On Previous Row Total") {
254 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
255 function(t) { return cint(t.included_in_print_rate) ? null : t; });
256 if(taxes_not_included.length > 0) {
257 // all rows above this tax should be inclusive
258 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
259 }
260 }
261 }
262 },
263
264 _load_item_tax_rate: function(item_tax_rate) {
265 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
266 },
267
268 _get_tax_rate: function(tax, item_tax_map) {
269 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
270 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
271 tax.rate;
272 },
273
274 get_item_wise_taxes_html: function() {
275 var item_tax = {};
276 var tax_accounts = [];
277 var company_currency = this.get_company_currency();
278
279 $.each(this.get_tax_doclist(), function(i, tax) {
280 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530281 var tax_rate_precision = precision("rate", tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530282 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530283 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530284 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530285 if($.isArray(tax_data)) {
286 var tax_rate = tax_data[0] == null ? "" : (flt(tax_data[0], tax_rate_precision) + "%"),
287 tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency);
288
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530289 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530290 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530291 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530292 }
Anand Doshi3543f302013-05-24 19:25:01 +0530293 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530294 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530295 });
296
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530297 var headings = $.map([wn._("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530298 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
299
300 var rows = $.map(this.get_item_doclist(), function(item) {
301 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530302 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530303 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
304 item_name: item.item_name,
305 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530306 return item_tax_record[head[0]] ?
307 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530308 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530309 }).join("\n")
310 });
311 }).join("\n");
312
Anand Doshi53b73422013-05-31 11:50:01 +0530313 if(!rows) return "";
Anand Doshi3543f302013-05-24 19:25:01 +0530314 return '<div style="overflow-x: scroll;"><table class="table table-bordered table-hover">\
315 <thead><tr>' + headings + '</tr></thead> \
316 <tbody>' + rows + '</tbody> \
317 </table></div>';
318 },
319
Anand Doshi923d41d2013-05-28 17:23:36 +0530320 _validate_before_fetch: function(fieldname) {
321 var me = this;
322 if(!me.frm.doc[fieldname]) {
323 return (wn._("Please specify") + ": " +
324 wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
325 ". " + wn._("It is needed to fetch Item Details."));
326 }
327 return null;
328 },
329
Anand Doshi3543f302013-05-24 19:25:01 +0530330 validate_company_and_party: function(party_field) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530331 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530332 var valid = true;
Anand Doshi923d41d2013-05-28 17:23:36 +0530333 var msg = "";
Anand Doshi3543f302013-05-24 19:25:01 +0530334 $.each(["company", party_field], function(i, fieldname) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530335 var msg_for_fieldname = me._validate_before_fetch(fieldname);
336 if(msg_for_fieldname) {
337 msgprint(msg_for_fieldname);
Anand Doshi3543f302013-05-24 19:25:01 +0530338 valid = false;
Anand Doshi3543f302013-05-24 19:25:01 +0530339 }
340 });
341 return valid;
342 },
343
344 get_item_doclist: function() {
345 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
346 {parentfield: this.fname});
347 },
348
349 get_tax_doclist: function() {
350 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
351 {parentfield: this.other_fname});
352 },
353
354 validate_conversion_rate: function() {
355 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
356 var conversion_rate_label = wn.meta.get_label(this.frm.doc.doctype, "conversion_rate",
357 this.frm.doc.name);
358
359 if(this.frm.doc.conversion_rate == 0) {
360 wn.throw(wn._(conversion_rate_label) + " " + wn._("cannot be 0"));
361 }
362
363 var company_currency = this.get_company_currency();
Anand Doshi923d41d2013-05-28 17:23:36 +0530364 var valid_conversion_rate = this.frm.doc.conversion_rate ?
Anand Doshi3543f302013-05-24 19:25:01 +0530365 ((this.frm.doc.currency == company_currency && this.frm.doc.conversion_rate == 1.0) ||
366 (this.frm.doc.currency != company_currency && this.frm.doc.conversion_rate != 1.0)) :
367 false;
368
369 if(!valid_conversion_rate) {
370 wn.throw(wn._("Please enter valid") + " " + wn._(conversion_rate_label) +
371 " 1 " + this.frm.doc.currency + " = [?] " + company_currency);
372 }
373 },
374
375 calculate_taxes_and_totals: function() {
376 this.validate_conversion_rate();
377 this.frm.item_doclist = this.get_item_doclist();
378 this.frm.tax_doclist = this.get_tax_doclist();
379
380 this.calculate_item_values();
381 this.initialize_taxes();
382 this.determine_exclusive_rate && this.determine_exclusive_rate();
383 this.calculate_net_total();
384 this.calculate_taxes();
385 this.calculate_totals();
386 this._cleanup();
Anand Doshi923d41d2013-05-28 17:23:36 +0530387
Anand Doshi3543f302013-05-24 19:25:01 +0530388 this.show_item_wise_taxes();
389 },
390
391 initialize_taxes: function() {
392 var me = this;
393 $.each(this.frm.tax_doclist, function(i, tax) {
394 tax.item_wise_tax_detail = {};
395 $.each(["tax_amount", "total",
396 "tax_amount_for_current_item", "grand_total_for_current_item",
397 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
398 function(i, fieldname) { tax[fieldname] = 0.0 });
399
400 me.validate_on_previous_row(tax);
401 me.validate_inclusive_tax(tax);
402 wn.model.round_floats_in(tax);
403 });
404 },
405
406 calculate_taxes: function() {
407 var me = this;
408
409 $.each(this.frm.item_doclist, function(n, item) {
410 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
411
412 $.each(me.frm.tax_doclist, function(i, tax) {
413 // tax_amount represents the amount of tax for the current step
414 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530415
Anand Doshi3543f302013-05-24 19:25:01 +0530416 me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
417
418 // case when net total is 0 but there is an actual type charge
419 // in this case add the actual amount to tax.tax_amount
420 // and tax.grand_total_for_current_item for the first such iteration
421 if(tax.charge_type == "Actual" &&
422 !(current_tax_amount || me.frm.doc.net_total || tax.tax_amount)) {
423 var zero_net_total_adjustment = flt(tax.rate, precision("tax_amount", tax));
424 current_tax_amount += zero_net_total_adjustment;
425 }
426
427 // store tax_amount for current item as it will be used for
428 // charge type = 'On Previous Row Amount'
429 tax.tax_amount_for_current_item = current_tax_amount;
430
431 // accumulate tax amount into tax.tax_amount
432 tax.tax_amount += current_tax_amount;
433
Anand Doshi3543f302013-05-24 19:25:01 +0530434 // for buying
435 if(tax.category) {
436 // if just for valuation, do not add the tax amount in total
437 // hence, setting it as 0 for further steps
438 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
439
440 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
441 }
442
443 // Calculate tax.total viz. grand total till that step
444 // note: grand_total_for_current_item contains the contribution of
445 // item's amount, previously applied tax and the current tax on that item
446 if(i==0) {
447 tax.grand_total_for_current_item = flt(item.amount + current_tax_amount,
448 precision("total", tax));
449 } else {
450 tax.grand_total_for_current_item =
451 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
452 precision("total", tax));
453 }
454
455 // in tax.total, accumulate grand total for each item
456 tax.total += tax.grand_total_for_current_item;
457 });
458 });
459 },
460
461 get_current_tax_amount: function(item, tax, item_tax_map) {
462 var tax_rate = this._get_tax_rate(tax, item_tax_map);
463 var current_tax_amount = 0.0;
464
465 if(tax.charge_type == "Actual") {
466 // distribute the tax amount proportionally to each item row
467 var actual = flt(tax.rate, precision("tax_amount", tax));
468 current_tax_amount = this.frm.doc.net_total ?
469 ((item.amount / this.frm.doc.net_total) * actual) :
470 0.0;
471
472 } else if(tax.charge_type == "On Net Total") {
473 current_tax_amount = (tax_rate / 100.0) * item.amount;
474
475 } else if(tax.charge_type == "On Previous Row Amount") {
476 current_tax_amount = (tax_rate / 100.0) *
477 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
478
479 } else if(tax.charge_type == "On Previous Row Total") {
480 current_tax_amount = (tax_rate / 100.0) *
481 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
482
483 }
484
Anand Doshi53b73422013-05-31 11:50:01 +0530485 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
486
487 // store tax breakup for each item
488 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
489
490 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530491 },
492
493 _cleanup: function() {
494 $.each(this.frm.tax_doclist, function(i, tax) {
495 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
496 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
497 function(i, fieldname) { delete tax[fieldname]; });
498
499 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
500 });
501 },
Anand Doshifc777182013-05-27 19:29:07 +0530502
503 calculate_total_advance: function(parenttype, advance_parentfield) {
504 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
505 var advance_doclist = wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
506 {parentfield: advance_parentfield});
507 this.frm.doc.total_advance = flt(wn.utils.sum(
508 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
509 ), precision("total_advance"));
510
511 this.calculate_outstanding_amount();
512 }
513 },
Anand Doshi3543f302013-05-24 19:25:01 +0530514
515 _set_in_company_currency: function(item, print_field, base_field) {
516 // set values in base currency
517 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
518 precision(base_field, item));
519 },
Anand Doshibf3e54a2013-06-05 14:11:32 +0530520
521 get_terms: function() {
522 var me = this;
523 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530524 return this.frm.call({
Anand Doshibf3e54a2013-06-05 14:11:32 +0530525 method: "webnotes.client.get_value",
526 args: {
527 doctype: "Terms and Conditions",
528 fieldname: "terms",
529 filters: { name: this.frm.doc.tc_name },
530 },
531 });
532 }
533 },
Anand Doshi3543f302013-05-24 19:25:01 +0530534});