blob: 421a7a53d2fa5fa32df305296d2c7bcf3a2b75b1 [file] [log] [blame]
Anand Doshi3543f302013-05-24 19:25:01 +05301// ERPNext - web based ERP (http://erpnext.com)
2// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17wn.provide("erpnext");
18
19erpnext.TransactionController = wn.ui.form.Controller.extend({
20 onload: function() {
21 if(this.frm.doc.__islocal) {
22 var me = this,
23 today = get_today(),
24 currency = wn.defaults.get_default("currency");
25
26 $.each({
Anand Doshifc777182013-05-27 19:29:07 +053027 posting_date: today,
28 due_date: today,
29 transaction_date: today,
30 currency: currency,
31 price_list_currency: currency,
32 status: "Draft",
33 company: wn.defaults.get_default("company"),
34 fiscal_year: wn.defaults.get_default("fiscal_year"),
35 is_subcontracted: "No",
36 conversion_rate: 1.0,
37 plc_conversion_rate: 1.0
Anand Doshi3543f302013-05-24 19:25:01 +053038 }, function(fieldname, value) {
39 if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname])
40 me.frm.set_value(fieldname, value);
41 });
Anand Doshi535d8332013-07-19 13:51:07 +053042
43 me.frm.script_manager.trigger("company");
Anand Doshi3543f302013-05-24 19:25:01 +053044 }
45 },
46
47 refresh: function() {
48 this.frm.clear_custom_buttons();
49 erpnext.hide_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +053050 erpnext.hide_company();
Anand Doshi3543f302013-05-24 19:25:01 +053051 this.show_item_wise_taxes();
Anand Doshi923d41d2013-05-28 17:23:36 +053052 this.frm.fields_dict.currency ? this.currency() : this.set_dynamic_labels();
Anand Doshi3543f302013-05-24 19:25:01 +053053 },
54
55 onload_post_render: function() {
56 if(this.frm.doc.__islocal && this.frm.doc.company) {
57 var me = this;
58 this.frm.call({
59 doc: this.frm.doc,
60 method: "onload_post_render",
61 freeze: true,
62 callback: function(r) {
63 // remove this call when using client side mapper
64 me.set_default_values();
65 me.frm.refresh();
66 }
67 });
68 }
69 },
70
Anand Doshi923d41d2013-05-28 17:23:36 +053071 validate: function() {
72 this.calculate_taxes_and_totals();
73 },
74
Anand Doshi3543f302013-05-24 19:25:01 +053075 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +053076 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Anand Doshi3543f302013-05-24 19:25:01 +053077 var me = this;
78 var company_currency = this.get_company_currency();
79 $.each(["currency", "price_list_currency"], function(i, fieldname) {
Anand Doshi535d8332013-07-19 13:51:07 +053080 if(!me.frm.doc[fieldname]) {
Anand Doshi3543f302013-05-24 19:25:01 +053081 me.frm.set_value(fieldname, company_currency);
Anand Doshi535d8332013-07-19 13:51:07 +053082 me.script_manager.trigger(fieldname);
Anand Doshi3543f302013-05-24 19:25:01 +053083 }
84 });
Anand Doshi3543f302013-05-24 19:25:01 +053085 }
86 },
87
88 get_company_currency: function() {
89 return erpnext.get_currency(this.frm.doc.company);
90 },
91
92 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +053093 var me = this;
94 this.set_dynamic_labels();
Anand Doshid52b03a2013-06-21 12:22:52 +053095
Anand Doshi61a2f682013-06-21 17:55:31 +053096 var company_currency = this.get_company_currency();
97 if(this.frm.doc.currency !== company_currency) {
98 this.get_exchange_rate(this.frm.doc.currency, company_currency,
99 function(exchange_rate) {
100 if(exchange_rate) {
101 me.frm.set_value("conversion_rate", exchange_rate);
102 me.conversion_rate();
103 }
104 });
105 } else {
106 this.conversion_rate();
107 }
108 },
109
110 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530111 if(this.frm.doc.currency === this.get_company_currency()) {
112 this.frm.set_value("conversion_rate", 1.0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530113 } else if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
114 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
115 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
116 }
117
118 this.calculate_taxes_and_totals();
Anand Doshi3543f302013-05-24 19:25:01 +0530119 },
120
Anand Doshi060d9242013-06-12 17:40:36 +0530121 price_list_name: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +0530122 var me = this;
123 if(this.frm.doc.price_list_name) {
124 this.frm.call({
125 method: "setup.utils.get_price_list_currency",
Anand Doshi61a2f682013-06-21 17:55:31 +0530126 args: { args: {
Anand Doshi3543f302013-05-24 19:25:01 +0530127 price_list_name: this.frm.doc.price_list_name,
Anand Doshi060d9242013-06-12 17:40:36 +0530128 buying_or_selling: buying_or_selling
Anand Doshi3543f302013-05-24 19:25:01 +0530129 }},
130 callback: function(r) {
131 if(!r.exc) {
132 me.price_list_currency();
133 }
134 }
135 });
136 }
137 },
138
Anand Doshi61a2f682013-06-21 17:55:31 +0530139 get_exchange_rate: function(from_currency, to_currency, callback) {
140 var exchange_name = from_currency + "-" + to_currency;
141 wn.model.with_doc("Currency Exchange", exchange_name, function(name) {
142 var exchange_doc = wn.model.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530143 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530144 });
145 },
146
Anand Doshi3543f302013-05-24 19:25:01 +0530147 price_list_currency: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530148 this.set_dynamic_labels();
Anand Doshi61a2f682013-06-21 17:55:31 +0530149
150 var company_currency = this.get_company_currency();
151 if(this.frm.doc.price_list_currency !== company_currency) {
152 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
153 function(exchange_rate) {
154 if(exchange_rate) {
155 me.frm.set_value("price_list_currency", exchange_rate);
156 me.plc_conversion_rate();
157 }
158 });
159 } else {
160 this.plc_conversion_rate();
161 }
Anand Doshi3543f302013-05-24 19:25:01 +0530162 },
163
164 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530165 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
166 this.frm.set_value("plc_conversion_rate", 1.0);
167 } else if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
168 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
169 this.calculate_taxes_and_totals();
170 }
Anand Doshi3543f302013-05-24 19:25:01 +0530171 },
172
173 qty: function(doc, cdt, cdn) {
174 this.calculate_taxes_and_totals();
175 },
176
Anand Doshi923d41d2013-05-28 17:23:36 +0530177 tax_rate: function(doc, cdt, cdn) {
178 this.calculate_taxes_and_totals();
179 },
180
181 row_id: function(doc, cdt, cdn) {
182 var tax = wn.model.get_doc(cdt, cdn);
183 try {
184 this.validate_on_previous_row(tax);
185 this.calculate_taxes_and_totals();
186 } catch(e) {
187 tax.row_id = null;
188 refresh_field("row_id", tax.name, tax.parentfield);
189 throw e;
190 }
191 },
192
Anand Doshi61a2f682013-06-21 17:55:31 +0530193 set_dynamic_labels: function() {
194 // What TODO? should we make price list system non-mandatory?
195 this.frm.toggle_reqd("plc_conversion_rate",
196 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
197
198 var company_currency = this.get_company_currency();
199 this.change_form_labels(company_currency);
200 this.change_grid_labels(company_currency);
201 },
202
Anand Doshi923d41d2013-05-28 17:23:36 +0530203 recalculate: function() {
204 this.calculate_taxes_and_totals();
205 },
206
207 recalculate_values: function() {
208 this.calculate_taxes_and_totals();
209 },
210
Anand Doshid0b00722013-05-28 18:54:48 +0530211 calculate_charges: function() {
212 this.calculate_taxes_and_totals();
213 },
214
Anand Doshi3543f302013-05-24 19:25:01 +0530215 included_in_print_rate: function(doc, cdt, cdn) {
216 var tax = wn.model.get_doc(cdt, cdn);
217 try {
218 this.validate_on_previous_row(tax);
219 this.validate_inclusive_tax(tax);
220 this.calculate_taxes_and_totals();
221 } catch(e) {
222 tax.included_in_print_rate = 0;
223 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
224 throw e;
225 }
226 },
227
228 validate_on_previous_row: function(tax) {
229 // validate if a valid row id is mentioned in case of
230 // On Previous Row Amount and On Previous Row Total
231 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
232 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
233 var msg = repl(wn._("Row") + " # %(idx)s [%(doctype)s]: " +
234 wn._("Please specify a valid") + " %(row_id_label)s", {
235 idx: tax.idx,
236 doctype: tax.doctype,
237 row_id_label: wn.meta.get_label(tax.doctype, "row_id", tax.name)
238 });
239 msgprint(msg);
240 throw msg;
241 }
242 },
243
244 validate_inclusive_tax: function(tax) {
245 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
246
247 var actual_type_error = function() {
248 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
249 "%(charge_type_label)s = \"%(charge_type)s\" " +
250 wn._("cannot be included in Item's rate"), {
251 idx: tax.idx,
252 doctype: tax.doctype,
253 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
254 charge_type: tax.charge_type
255 });
256 msgprint(msg);
257 throw msg;
258 };
259
260 var on_previous_row_error = function(row_range) {
261 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
262 wn._("to be included in Item's rate, it is required that: ") +
263 " [" + wn._("Row") + " # %(row_range)s] " + wn._("also be included in Item's rate"), {
264 idx: tax.idx,
265 doctype: tax.doctype,
266 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
267 charge_type: tax.charge_type,
268 inclusive_label: wn.meta.get_label(tax.doctype, "included_in_print_rate", tax.name),
269 row_range: row_range,
270 });
271
272 msgprint(msg);
273 throw msg;
274 };
275
276 if(cint(tax.included_in_print_rate)) {
277 if(tax.charge_type == "Actual") {
278 // inclusive tax cannot be of type Actual
279 actual_type_error();
280 } else if(tax.charge_type == "On Previous Row Amount" &&
281 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
282 // referred row should also be an inclusive tax
283 on_previous_row_error(tax.row_id);
284 } else if(tax.charge_type == "On Previous Row Total") {
285 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
286 function(t) { return cint(t.included_in_print_rate) ? null : t; });
287 if(taxes_not_included.length > 0) {
288 // all rows above this tax should be inclusive
289 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
290 }
291 }
292 }
293 },
294
295 _load_item_tax_rate: function(item_tax_rate) {
296 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
297 },
298
299 _get_tax_rate: function(tax, item_tax_map) {
300 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
301 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
302 tax.rate;
303 },
304
305 get_item_wise_taxes_html: function() {
306 var item_tax = {};
307 var tax_accounts = [];
308 var company_currency = this.get_company_currency();
309
310 $.each(this.get_tax_doclist(), function(i, tax) {
311 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530312 var tax_rate_precision = precision("rate", tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530313 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530314 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530315 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530316 if($.isArray(tax_data)) {
317 var tax_rate = tax_data[0] == null ? "" : (flt(tax_data[0], tax_rate_precision) + "%"),
318 tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency);
319
320 item_tax[item_code][tax.account_head] = [tax_rate, tax_amount];
321 } else {
322 item_tax[item_code][tax.account_head] = [flt(tax_data, tax_rate_precision) + "%", ""];
323 }
Anand Doshi3543f302013-05-24 19:25:01 +0530324 });
325 tax_accounts.push(tax.account_head);
326 });
327
328 var headings = $.map([wn._("Item Name")].concat(tax_accounts),
329 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
330
331 var rows = $.map(this.get_item_doclist(), function(item) {
332 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530333 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530334 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
335 item_name: item.item_name,
336 taxes: $.map(tax_accounts, function(head) {
Anand Doshi64f316b2013-07-17 15:12:22 +0530337 return item_tax_record[head] ?
338 "<td>(" + item_tax_record[head][0] + ") " + item_tax_record[head][1] + "</td>" :
339 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530340 }).join("\n")
341 });
342 }).join("\n");
343
Anand Doshi53b73422013-05-31 11:50:01 +0530344 if(!rows) return "";
Anand Doshi3543f302013-05-24 19:25:01 +0530345 return '<div style="overflow-x: scroll;"><table class="table table-bordered table-hover">\
346 <thead><tr>' + headings + '</tr></thead> \
347 <tbody>' + rows + '</tbody> \
348 </table></div>';
349 },
350
351 set_default_values: function() {
352 $.each(wn.model.get_doclist(this.frm.doctype, this.frm.docname), function(i, doc) {
353 var updated = wn.model.set_default_values(doc);
354 if(doc.parentfield) {
355 refresh_field(doc.parentfield);
356 } else {
357 refresh_field(updated);
358 }
359 });
360 },
361
Anand Doshi923d41d2013-05-28 17:23:36 +0530362 _validate_before_fetch: function(fieldname) {
363 var me = this;
364 if(!me.frm.doc[fieldname]) {
365 return (wn._("Please specify") + ": " +
366 wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
367 ". " + wn._("It is needed to fetch Item Details."));
368 }
369 return null;
370 },
371
Anand Doshi3543f302013-05-24 19:25:01 +0530372 validate_company_and_party: function(party_field) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530373 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530374 var valid = true;
Anand Doshi923d41d2013-05-28 17:23:36 +0530375 var msg = "";
Anand Doshi3543f302013-05-24 19:25:01 +0530376 $.each(["company", party_field], function(i, fieldname) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530377 var msg_for_fieldname = me._validate_before_fetch(fieldname);
378 if(msg_for_fieldname) {
379 msgprint(msg_for_fieldname);
Anand Doshi3543f302013-05-24 19:25:01 +0530380 valid = false;
Anand Doshi3543f302013-05-24 19:25:01 +0530381 }
382 });
383 return valid;
384 },
385
386 get_item_doclist: function() {
387 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
388 {parentfield: this.fname});
389 },
390
391 get_tax_doclist: function() {
392 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
393 {parentfield: this.other_fname});
394 },
395
396 validate_conversion_rate: function() {
397 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
398 var conversion_rate_label = wn.meta.get_label(this.frm.doc.doctype, "conversion_rate",
399 this.frm.doc.name);
400
401 if(this.frm.doc.conversion_rate == 0) {
402 wn.throw(wn._(conversion_rate_label) + " " + wn._("cannot be 0"));
403 }
404
405 var company_currency = this.get_company_currency();
Anand Doshi923d41d2013-05-28 17:23:36 +0530406 var valid_conversion_rate = this.frm.doc.conversion_rate ?
Anand Doshi3543f302013-05-24 19:25:01 +0530407 ((this.frm.doc.currency == company_currency && this.frm.doc.conversion_rate == 1.0) ||
408 (this.frm.doc.currency != company_currency && this.frm.doc.conversion_rate != 1.0)) :
409 false;
410
411 if(!valid_conversion_rate) {
412 wn.throw(wn._("Please enter valid") + " " + wn._(conversion_rate_label) +
413 " 1 " + this.frm.doc.currency + " = [?] " + company_currency);
414 }
415 },
416
417 calculate_taxes_and_totals: function() {
418 this.validate_conversion_rate();
419 this.frm.item_doclist = this.get_item_doclist();
420 this.frm.tax_doclist = this.get_tax_doclist();
421
422 this.calculate_item_values();
423 this.initialize_taxes();
424 this.determine_exclusive_rate && this.determine_exclusive_rate();
425 this.calculate_net_total();
426 this.calculate_taxes();
427 this.calculate_totals();
428 this._cleanup();
Anand Doshi923d41d2013-05-28 17:23:36 +0530429
Anand Doshi3543f302013-05-24 19:25:01 +0530430 this.show_item_wise_taxes();
431 },
432
433 initialize_taxes: function() {
434 var me = this;
435 $.each(this.frm.tax_doclist, function(i, tax) {
436 tax.item_wise_tax_detail = {};
437 $.each(["tax_amount", "total",
438 "tax_amount_for_current_item", "grand_total_for_current_item",
439 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
440 function(i, fieldname) { tax[fieldname] = 0.0 });
441
442 me.validate_on_previous_row(tax);
443 me.validate_inclusive_tax(tax);
444 wn.model.round_floats_in(tax);
445 });
446 },
447
448 calculate_taxes: function() {
449 var me = this;
450
451 $.each(this.frm.item_doclist, function(n, item) {
452 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
453
454 $.each(me.frm.tax_doclist, function(i, tax) {
455 // tax_amount represents the amount of tax for the current step
456 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530457
Anand Doshi3543f302013-05-24 19:25:01 +0530458 me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
459
460 // case when net total is 0 but there is an actual type charge
461 // in this case add the actual amount to tax.tax_amount
462 // and tax.grand_total_for_current_item for the first such iteration
463 if(tax.charge_type == "Actual" &&
464 !(current_tax_amount || me.frm.doc.net_total || tax.tax_amount)) {
465 var zero_net_total_adjustment = flt(tax.rate, precision("tax_amount", tax));
466 current_tax_amount += zero_net_total_adjustment;
467 }
468
469 // store tax_amount for current item as it will be used for
470 // charge type = 'On Previous Row Amount'
471 tax.tax_amount_for_current_item = current_tax_amount;
472
473 // accumulate tax amount into tax.tax_amount
474 tax.tax_amount += current_tax_amount;
475
Anand Doshi3543f302013-05-24 19:25:01 +0530476 // for buying
477 if(tax.category) {
478 // if just for valuation, do not add the tax amount in total
479 // hence, setting it as 0 for further steps
480 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
481
482 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
483 }
484
485 // Calculate tax.total viz. grand total till that step
486 // note: grand_total_for_current_item contains the contribution of
487 // item's amount, previously applied tax and the current tax on that item
488 if(i==0) {
489 tax.grand_total_for_current_item = flt(item.amount + current_tax_amount,
490 precision("total", tax));
491 } else {
492 tax.grand_total_for_current_item =
493 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
494 precision("total", tax));
495 }
496
497 // in tax.total, accumulate grand total for each item
498 tax.total += tax.grand_total_for_current_item;
499 });
500 });
501 },
502
503 get_current_tax_amount: function(item, tax, item_tax_map) {
504 var tax_rate = this._get_tax_rate(tax, item_tax_map);
505 var current_tax_amount = 0.0;
506
507 if(tax.charge_type == "Actual") {
508 // distribute the tax amount proportionally to each item row
509 var actual = flt(tax.rate, precision("tax_amount", tax));
510 current_tax_amount = this.frm.doc.net_total ?
511 ((item.amount / this.frm.doc.net_total) * actual) :
512 0.0;
513
514 } else if(tax.charge_type == "On Net Total") {
515 current_tax_amount = (tax_rate / 100.0) * item.amount;
516
517 } else if(tax.charge_type == "On Previous Row Amount") {
518 current_tax_amount = (tax_rate / 100.0) *
519 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
520
521 } else if(tax.charge_type == "On Previous Row Total") {
522 current_tax_amount = (tax_rate / 100.0) *
523 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
524
525 }
526
Anand Doshi53b73422013-05-31 11:50:01 +0530527 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
528
529 // store tax breakup for each item
530 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
531
532 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530533 },
534
535 _cleanup: function() {
536 $.each(this.frm.tax_doclist, function(i, tax) {
537 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
538 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
539 function(i, fieldname) { delete tax[fieldname]; });
540
541 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
542 });
543 },
Anand Doshifc777182013-05-27 19:29:07 +0530544
545 calculate_total_advance: function(parenttype, advance_parentfield) {
546 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
547 var advance_doclist = wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
548 {parentfield: advance_parentfield});
549 this.frm.doc.total_advance = flt(wn.utils.sum(
550 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
551 ), precision("total_advance"));
552
553 this.calculate_outstanding_amount();
554 }
555 },
Anand Doshi3543f302013-05-24 19:25:01 +0530556
557 _set_in_company_currency: function(item, print_field, base_field) {
558 // set values in base currency
559 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
560 precision(base_field, item));
561 },
Anand Doshibf3e54a2013-06-05 14:11:32 +0530562
563 get_terms: function() {
564 var me = this;
565 if(this.frm.doc.tc_name) {
566 this.frm.call({
567 method: "webnotes.client.get_value",
568 args: {
569 doctype: "Terms and Conditions",
570 fieldname: "terms",
571 filters: { name: this.frm.doc.tc_name },
572 },
573 });
574 }
575 },
Anand Doshi3543f302013-05-24 19:25:01 +0530576});