blob: 4a647adc1cfd92793855aa04a54748f0efd1db41 [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");
Anand Doshia648f462013-07-30 14:42:15 +053018wn.require("app/js/controllers/stock_controller.js");
Anand Doshi3543f302013-05-24 19:25:01 +053019
Anand Doshia648f462013-07-30 14:42:15 +053020erpnext.TransactionController = erpnext.stock.StockController.extend({
Anand Doshi3543f302013-05-24 19:25:01 +053021 onload: function() {
22 if(this.frm.doc.__islocal) {
23 var me = this,
24 today = get_today(),
25 currency = wn.defaults.get_default("currency");
26
27 $.each({
Anand Doshifc777182013-05-27 19:29:07 +053028 posting_date: today,
29 due_date: today,
30 transaction_date: today,
31 currency: currency,
32 price_list_currency: currency,
33 status: "Draft",
34 company: wn.defaults.get_default("company"),
35 fiscal_year: wn.defaults.get_default("fiscal_year"),
36 is_subcontracted: "No",
37 conversion_rate: 1.0,
38 plc_conversion_rate: 1.0
Anand Doshi3543f302013-05-24 19:25:01 +053039 }, function(fieldname, value) {
40 if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname])
41 me.frm.set_value(fieldname, value);
42 });
Anand Doshi535d8332013-07-19 13:51:07 +053043
44 me.frm.script_manager.trigger("company");
Anand Doshi3543f302013-05-24 19:25:01 +053045 }
46 },
47
48 refresh: function() {
49 this.frm.clear_custom_buttons();
50 erpnext.hide_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +053051 erpnext.hide_company();
Anand Doshi3543f302013-05-24 19:25:01 +053052 this.show_item_wise_taxes();
Nabin Hait1f996c32013-07-29 19:00:53 +053053 this.set_dynamic_labels();
Anand Doshi3543f302013-05-24 19:25:01 +053054 },
55
56 onload_post_render: function() {
57 if(this.frm.doc.__islocal && this.frm.doc.company) {
58 var me = this;
Anand Doshi1fac2a92013-07-29 19:30:39 +053059 return this.frm.call({
Anand Doshi3543f302013-05-24 19:25:01 +053060 doc: this.frm.doc,
61 method: "onload_post_render",
62 freeze: true,
63 callback: function(r) {
64 // remove this call when using client side mapper
65 me.set_default_values();
66 me.frm.refresh();
67 }
68 });
69 }
70 },
71
Anand Doshi923d41d2013-05-28 17:23:36 +053072 validate: function() {
73 this.calculate_taxes_and_totals();
74 },
75
Anand Doshi3543f302013-05-24 19:25:01 +053076 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +053077 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Anand Doshi17350b82013-08-01 15:45:23 +053078 if(!this.frm.doc.currency) {
79 this.frm.set_value("currency", this.get_company_currency());
80 }
81
Nabin Hait48a0bd32013-07-23 15:31:39 +053082 this.frm.script_manager.trigger("currency");
Anand Doshi3543f302013-05-24 19:25:01 +053083 }
84 },
85
86 get_company_currency: function() {
87 return erpnext.get_currency(this.frm.doc.company);
88 },
89
90 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +053091 var me = this;
92 this.set_dynamic_labels();
Anand Doshid52b03a2013-06-21 12:22:52 +053093
Anand Doshi61a2f682013-06-21 17:55:31 +053094 var company_currency = this.get_company_currency();
95 if(this.frm.doc.currency !== company_currency) {
96 this.get_exchange_rate(this.frm.doc.currency, company_currency,
97 function(exchange_rate) {
98 if(exchange_rate) {
99 me.frm.set_value("conversion_rate", exchange_rate);
100 me.conversion_rate();
101 }
102 });
103 } else {
104 this.conversion_rate();
105 }
106 },
107
108 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530109 if(this.frm.doc.currency === this.get_company_currency()) {
110 this.frm.set_value("conversion_rate", 1.0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530111 } else if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
112 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
113 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
114 }
115
116 this.calculate_taxes_and_totals();
Anand Doshi3543f302013-05-24 19:25:01 +0530117 },
118
Anand Doshi060d9242013-06-12 17:40:36 +0530119 price_list_name: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +0530120 var me = this;
121 if(this.frm.doc.price_list_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530122 return this.frm.call({
Anand Doshi3543f302013-05-24 19:25:01 +0530123 method: "setup.utils.get_price_list_currency",
Nabin Hait48a0bd32013-07-23 15:31:39 +0530124 args: {
Anand Doshi3543f302013-05-24 19:25:01 +0530125 price_list_name: this.frm.doc.price_list_name,
Nabin Hait48a0bd32013-07-23 15:31:39 +0530126 },
Anand Doshi3543f302013-05-24 19:25:01 +0530127 callback: function(r) {
128 if(!r.exc) {
129 me.price_list_currency();
130 }
131 }
132 });
133 }
134 },
135
Anand Doshi61a2f682013-06-21 17:55:31 +0530136 get_exchange_rate: function(from_currency, to_currency, callback) {
137 var exchange_name = from_currency + "-" + to_currency;
138 wn.model.with_doc("Currency Exchange", exchange_name, function(name) {
139 var exchange_doc = wn.model.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530140 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530141 });
142 },
143
Anand Doshi3543f302013-05-24 19:25:01 +0530144 price_list_currency: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530145 this.set_dynamic_labels();
Anand Doshi61a2f682013-06-21 17:55:31 +0530146
147 var company_currency = this.get_company_currency();
148 if(this.frm.doc.price_list_currency !== company_currency) {
149 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
150 function(exchange_rate) {
151 if(exchange_rate) {
152 me.frm.set_value("price_list_currency", exchange_rate);
153 me.plc_conversion_rate();
154 }
155 });
156 } else {
157 this.plc_conversion_rate();
158 }
Anand Doshi3543f302013-05-24 19:25:01 +0530159 },
160
161 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530162 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
163 this.frm.set_value("plc_conversion_rate", 1.0);
164 } else if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
165 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
166 this.calculate_taxes_and_totals();
167 }
Anand Doshi3543f302013-05-24 19:25:01 +0530168 },
169
170 qty: function(doc, cdt, cdn) {
171 this.calculate_taxes_and_totals();
172 },
173
Anand Doshi923d41d2013-05-28 17:23:36 +0530174 tax_rate: function(doc, cdt, cdn) {
175 this.calculate_taxes_and_totals();
176 },
177
178 row_id: function(doc, cdt, cdn) {
179 var tax = wn.model.get_doc(cdt, cdn);
180 try {
181 this.validate_on_previous_row(tax);
182 this.calculate_taxes_and_totals();
183 } catch(e) {
184 tax.row_id = null;
185 refresh_field("row_id", tax.name, tax.parentfield);
186 throw e;
187 }
188 },
189
Anand Doshi61a2f682013-06-21 17:55:31 +0530190 set_dynamic_labels: function() {
191 // What TODO? should we make price list system non-mandatory?
192 this.frm.toggle_reqd("plc_conversion_rate",
193 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
194
195 var company_currency = this.get_company_currency();
196 this.change_form_labels(company_currency);
197 this.change_grid_labels(company_currency);
198 },
199
Anand Doshi923d41d2013-05-28 17:23:36 +0530200 recalculate: function() {
201 this.calculate_taxes_and_totals();
202 },
203
204 recalculate_values: function() {
205 this.calculate_taxes_and_totals();
206 },
207
Anand Doshid0b00722013-05-28 18:54:48 +0530208 calculate_charges: function() {
209 this.calculate_taxes_and_totals();
210 },
211
Anand Doshi3543f302013-05-24 19:25:01 +0530212 included_in_print_rate: function(doc, cdt, cdn) {
213 var tax = wn.model.get_doc(cdt, cdn);
214 try {
215 this.validate_on_previous_row(tax);
216 this.validate_inclusive_tax(tax);
217 this.calculate_taxes_and_totals();
218 } catch(e) {
219 tax.included_in_print_rate = 0;
220 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
221 throw e;
222 }
223 },
224
225 validate_on_previous_row: function(tax) {
226 // validate if a valid row id is mentioned in case of
227 // On Previous Row Amount and On Previous Row Total
228 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
229 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
230 var msg = repl(wn._("Row") + " # %(idx)s [%(doctype)s]: " +
231 wn._("Please specify a valid") + " %(row_id_label)s", {
232 idx: tax.idx,
233 doctype: tax.doctype,
234 row_id_label: wn.meta.get_label(tax.doctype, "row_id", tax.name)
235 });
236 msgprint(msg);
237 throw msg;
238 }
239 },
240
241 validate_inclusive_tax: function(tax) {
242 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
243
244 var actual_type_error = function() {
245 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
246 "%(charge_type_label)s = \"%(charge_type)s\" " +
247 wn._("cannot be included in Item's rate"), {
248 idx: tax.idx,
249 doctype: tax.doctype,
250 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
251 charge_type: tax.charge_type
252 });
253 msgprint(msg);
254 throw msg;
255 };
256
257 var on_previous_row_error = function(row_range) {
258 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
259 wn._("to be included in Item's rate, it is required that: ") +
260 " [" + wn._("Row") + " # %(row_range)s] " + wn._("also be included in Item's rate"), {
261 idx: tax.idx,
262 doctype: tax.doctype,
263 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
264 charge_type: tax.charge_type,
265 inclusive_label: wn.meta.get_label(tax.doctype, "included_in_print_rate", tax.name),
266 row_range: row_range,
267 });
268
269 msgprint(msg);
270 throw msg;
271 };
272
273 if(cint(tax.included_in_print_rate)) {
274 if(tax.charge_type == "Actual") {
275 // inclusive tax cannot be of type Actual
276 actual_type_error();
277 } else if(tax.charge_type == "On Previous Row Amount" &&
278 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
279 // referred row should also be an inclusive tax
280 on_previous_row_error(tax.row_id);
281 } else if(tax.charge_type == "On Previous Row Total") {
282 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
283 function(t) { return cint(t.included_in_print_rate) ? null : t; });
284 if(taxes_not_included.length > 0) {
285 // all rows above this tax should be inclusive
286 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
287 }
288 }
289 }
290 },
291
292 _load_item_tax_rate: function(item_tax_rate) {
293 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
294 },
295
296 _get_tax_rate: function(tax, item_tax_map) {
297 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
298 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
299 tax.rate;
300 },
301
302 get_item_wise_taxes_html: function() {
303 var item_tax = {};
304 var tax_accounts = [];
305 var company_currency = this.get_company_currency();
306
307 $.each(this.get_tax_doclist(), function(i, tax) {
308 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530309 var tax_rate_precision = precision("rate", tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530310 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530311 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530312 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530313 if($.isArray(tax_data)) {
314 var tax_rate = tax_data[0] == null ? "" : (flt(tax_data[0], tax_rate_precision) + "%"),
315 tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency);
316
317 item_tax[item_code][tax.account_head] = [tax_rate, tax_amount];
318 } else {
319 item_tax[item_code][tax.account_head] = [flt(tax_data, tax_rate_precision) + "%", ""];
320 }
Anand Doshi3543f302013-05-24 19:25:01 +0530321 });
322 tax_accounts.push(tax.account_head);
323 });
324
325 var headings = $.map([wn._("Item Name")].concat(tax_accounts),
326 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
327
328 var rows = $.map(this.get_item_doclist(), function(item) {
329 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530330 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530331 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
332 item_name: item.item_name,
333 taxes: $.map(tax_accounts, function(head) {
Anand Doshi64f316b2013-07-17 15:12:22 +0530334 return item_tax_record[head] ?
335 "<td>(" + item_tax_record[head][0] + ") " + item_tax_record[head][1] + "</td>" :
336 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530337 }).join("\n")
338 });
339 }).join("\n");
340
Anand Doshi53b73422013-05-31 11:50:01 +0530341 if(!rows) return "";
Anand Doshi3543f302013-05-24 19:25:01 +0530342 return '<div style="overflow-x: scroll;"><table class="table table-bordered table-hover">\
343 <thead><tr>' + headings + '</tr></thead> \
344 <tbody>' + rows + '</tbody> \
345 </table></div>';
346 },
347
348 set_default_values: function() {
349 $.each(wn.model.get_doclist(this.frm.doctype, this.frm.docname), function(i, doc) {
350 var updated = wn.model.set_default_values(doc);
351 if(doc.parentfield) {
352 refresh_field(doc.parentfield);
353 } else {
354 refresh_field(updated);
355 }
356 });
357 },
358
Anand Doshi923d41d2013-05-28 17:23:36 +0530359 _validate_before_fetch: function(fieldname) {
360 var me = this;
361 if(!me.frm.doc[fieldname]) {
362 return (wn._("Please specify") + ": " +
363 wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
364 ". " + wn._("It is needed to fetch Item Details."));
365 }
366 return null;
367 },
368
Anand Doshi3543f302013-05-24 19:25:01 +0530369 validate_company_and_party: function(party_field) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530370 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530371 var valid = true;
Anand Doshi923d41d2013-05-28 17:23:36 +0530372 var msg = "";
Anand Doshi3543f302013-05-24 19:25:01 +0530373 $.each(["company", party_field], function(i, fieldname) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530374 var msg_for_fieldname = me._validate_before_fetch(fieldname);
375 if(msg_for_fieldname) {
376 msgprint(msg_for_fieldname);
Anand Doshi3543f302013-05-24 19:25:01 +0530377 valid = false;
Anand Doshi3543f302013-05-24 19:25:01 +0530378 }
379 });
380 return valid;
381 },
382
383 get_item_doclist: function() {
384 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
385 {parentfield: this.fname});
386 },
387
388 get_tax_doclist: function() {
389 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
390 {parentfield: this.other_fname});
391 },
392
393 validate_conversion_rate: function() {
394 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
395 var conversion_rate_label = wn.meta.get_label(this.frm.doc.doctype, "conversion_rate",
396 this.frm.doc.name);
397
398 if(this.frm.doc.conversion_rate == 0) {
399 wn.throw(wn._(conversion_rate_label) + " " + wn._("cannot be 0"));
400 }
401
402 var company_currency = this.get_company_currency();
Anand Doshi923d41d2013-05-28 17:23:36 +0530403 var valid_conversion_rate = this.frm.doc.conversion_rate ?
Anand Doshi3543f302013-05-24 19:25:01 +0530404 ((this.frm.doc.currency == company_currency && this.frm.doc.conversion_rate == 1.0) ||
405 (this.frm.doc.currency != company_currency && this.frm.doc.conversion_rate != 1.0)) :
406 false;
407
408 if(!valid_conversion_rate) {
409 wn.throw(wn._("Please enter valid") + " " + wn._(conversion_rate_label) +
410 " 1 " + this.frm.doc.currency + " = [?] " + company_currency);
411 }
412 },
413
414 calculate_taxes_and_totals: function() {
415 this.validate_conversion_rate();
416 this.frm.item_doclist = this.get_item_doclist();
417 this.frm.tax_doclist = this.get_tax_doclist();
418
419 this.calculate_item_values();
420 this.initialize_taxes();
421 this.determine_exclusive_rate && this.determine_exclusive_rate();
422 this.calculate_net_total();
423 this.calculate_taxes();
424 this.calculate_totals();
425 this._cleanup();
Anand Doshi923d41d2013-05-28 17:23:36 +0530426
Anand Doshi3543f302013-05-24 19:25:01 +0530427 this.show_item_wise_taxes();
428 },
429
430 initialize_taxes: function() {
431 var me = this;
432 $.each(this.frm.tax_doclist, function(i, tax) {
433 tax.item_wise_tax_detail = {};
434 $.each(["tax_amount", "total",
435 "tax_amount_for_current_item", "grand_total_for_current_item",
436 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
437 function(i, fieldname) { tax[fieldname] = 0.0 });
438
439 me.validate_on_previous_row(tax);
440 me.validate_inclusive_tax(tax);
441 wn.model.round_floats_in(tax);
442 });
443 },
444
445 calculate_taxes: function() {
446 var me = this;
447
448 $.each(this.frm.item_doclist, function(n, item) {
449 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
450
451 $.each(me.frm.tax_doclist, function(i, tax) {
452 // tax_amount represents the amount of tax for the current step
453 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530454
Anand Doshi3543f302013-05-24 19:25:01 +0530455 me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
456
457 // case when net total is 0 but there is an actual type charge
458 // in this case add the actual amount to tax.tax_amount
459 // and tax.grand_total_for_current_item for the first such iteration
460 if(tax.charge_type == "Actual" &&
461 !(current_tax_amount || me.frm.doc.net_total || tax.tax_amount)) {
462 var zero_net_total_adjustment = flt(tax.rate, precision("tax_amount", tax));
463 current_tax_amount += zero_net_total_adjustment;
464 }
465
466 // store tax_amount for current item as it will be used for
467 // charge type = 'On Previous Row Amount'
468 tax.tax_amount_for_current_item = current_tax_amount;
469
470 // accumulate tax amount into tax.tax_amount
471 tax.tax_amount += current_tax_amount;
472
Anand Doshi3543f302013-05-24 19:25:01 +0530473 // for buying
474 if(tax.category) {
475 // if just for valuation, do not add the tax amount in total
476 // hence, setting it as 0 for further steps
477 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
478
479 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
480 }
481
482 // Calculate tax.total viz. grand total till that step
483 // note: grand_total_for_current_item contains the contribution of
484 // item's amount, previously applied tax and the current tax on that item
485 if(i==0) {
486 tax.grand_total_for_current_item = flt(item.amount + current_tax_amount,
487 precision("total", tax));
488 } else {
489 tax.grand_total_for_current_item =
490 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
491 precision("total", tax));
492 }
493
494 // in tax.total, accumulate grand total for each item
495 tax.total += tax.grand_total_for_current_item;
496 });
497 });
498 },
499
500 get_current_tax_amount: function(item, tax, item_tax_map) {
501 var tax_rate = this._get_tax_rate(tax, item_tax_map);
502 var current_tax_amount = 0.0;
503
504 if(tax.charge_type == "Actual") {
505 // distribute the tax amount proportionally to each item row
506 var actual = flt(tax.rate, precision("tax_amount", tax));
507 current_tax_amount = this.frm.doc.net_total ?
508 ((item.amount / this.frm.doc.net_total) * actual) :
509 0.0;
510
511 } else if(tax.charge_type == "On Net Total") {
512 current_tax_amount = (tax_rate / 100.0) * item.amount;
513
514 } else if(tax.charge_type == "On Previous Row Amount") {
515 current_tax_amount = (tax_rate / 100.0) *
516 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
517
518 } else if(tax.charge_type == "On Previous Row Total") {
519 current_tax_amount = (tax_rate / 100.0) *
520 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
521
522 }
523
Anand Doshi53b73422013-05-31 11:50:01 +0530524 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
525
526 // store tax breakup for each item
527 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
528
529 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530530 },
531
532 _cleanup: function() {
533 $.each(this.frm.tax_doclist, function(i, tax) {
534 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
535 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
536 function(i, fieldname) { delete tax[fieldname]; });
537
538 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
539 });
540 },
Anand Doshifc777182013-05-27 19:29:07 +0530541
542 calculate_total_advance: function(parenttype, advance_parentfield) {
543 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
544 var advance_doclist = wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
545 {parentfield: advance_parentfield});
546 this.frm.doc.total_advance = flt(wn.utils.sum(
547 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
548 ), precision("total_advance"));
549
550 this.calculate_outstanding_amount();
551 }
552 },
Anand Doshi3543f302013-05-24 19:25:01 +0530553
554 _set_in_company_currency: function(item, print_field, base_field) {
555 // set values in base currency
556 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
557 precision(base_field, item));
558 },
Anand Doshibf3e54a2013-06-05 14:11:32 +0530559
560 get_terms: function() {
561 var me = this;
562 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530563 return this.frm.call({
Anand Doshibf3e54a2013-06-05 14:11:32 +0530564 method: "webnotes.client.get_value",
565 args: {
566 doctype: "Terms and Conditions",
567 fieldname: "terms",
568 filters: { name: this.frm.doc.tc_name },
569 },
570 });
571 }
572 },
Anand Doshi3543f302013-05-24 19:25:01 +0530573});