blob: 2cee8437a9761d3662627bdde1fa240eefd1c08f [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 Doshi3543f302013-05-24 19:25:01 +053078 var company_currency = this.get_company_currency();
Nabin Hait48a0bd32013-07-23 15:31:39 +053079 this.frm.set_value("currency", company_currency);
80 this.frm.script_manager.trigger("currency");
Anand Doshi3543f302013-05-24 19:25:01 +053081 }
82 },
83
84 get_company_currency: function() {
85 return erpnext.get_currency(this.frm.doc.company);
86 },
87
88 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +053089 var me = this;
90 this.set_dynamic_labels();
Anand Doshid52b03a2013-06-21 12:22:52 +053091
Anand Doshi61a2f682013-06-21 17:55:31 +053092 var company_currency = this.get_company_currency();
93 if(this.frm.doc.currency !== company_currency) {
94 this.get_exchange_rate(this.frm.doc.currency, company_currency,
95 function(exchange_rate) {
96 if(exchange_rate) {
97 me.frm.set_value("conversion_rate", exchange_rate);
98 me.conversion_rate();
99 }
100 });
101 } else {
102 this.conversion_rate();
103 }
104 },
105
106 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530107 if(this.frm.doc.currency === this.get_company_currency()) {
108 this.frm.set_value("conversion_rate", 1.0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530109 } else if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
110 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
111 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
112 }
113
114 this.calculate_taxes_and_totals();
Anand Doshi3543f302013-05-24 19:25:01 +0530115 },
116
Anand Doshi060d9242013-06-12 17:40:36 +0530117 price_list_name: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +0530118 var me = this;
119 if(this.frm.doc.price_list_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530120 return this.frm.call({
Anand Doshi3543f302013-05-24 19:25:01 +0530121 method: "setup.utils.get_price_list_currency",
Nabin Hait48a0bd32013-07-23 15:31:39 +0530122 args: {
Anand Doshi3543f302013-05-24 19:25:01 +0530123 price_list_name: this.frm.doc.price_list_name,
Nabin Hait48a0bd32013-07-23 15:31:39 +0530124 },
Anand Doshi3543f302013-05-24 19:25:01 +0530125 callback: function(r) {
126 if(!r.exc) {
127 me.price_list_currency();
128 }
129 }
130 });
131 }
132 },
133
Anand Doshi61a2f682013-06-21 17:55:31 +0530134 get_exchange_rate: function(from_currency, to_currency, callback) {
135 var exchange_name = from_currency + "-" + to_currency;
136 wn.model.with_doc("Currency Exchange", exchange_name, function(name) {
137 var exchange_doc = wn.model.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530138 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530139 });
140 },
141
Anand Doshi3543f302013-05-24 19:25:01 +0530142 price_list_currency: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530143 this.set_dynamic_labels();
Anand Doshi61a2f682013-06-21 17:55:31 +0530144
145 var company_currency = this.get_company_currency();
146 if(this.frm.doc.price_list_currency !== company_currency) {
147 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
148 function(exchange_rate) {
149 if(exchange_rate) {
150 me.frm.set_value("price_list_currency", exchange_rate);
151 me.plc_conversion_rate();
152 }
153 });
154 } else {
155 this.plc_conversion_rate();
156 }
Anand Doshi3543f302013-05-24 19:25:01 +0530157 },
158
159 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530160 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
161 this.frm.set_value("plc_conversion_rate", 1.0);
162 } else if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
163 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
164 this.calculate_taxes_and_totals();
165 }
Anand Doshi3543f302013-05-24 19:25:01 +0530166 },
167
168 qty: function(doc, cdt, cdn) {
169 this.calculate_taxes_and_totals();
170 },
171
Anand Doshi923d41d2013-05-28 17:23:36 +0530172 tax_rate: function(doc, cdt, cdn) {
173 this.calculate_taxes_and_totals();
174 },
175
176 row_id: function(doc, cdt, cdn) {
177 var tax = wn.model.get_doc(cdt, cdn);
178 try {
179 this.validate_on_previous_row(tax);
180 this.calculate_taxes_and_totals();
181 } catch(e) {
182 tax.row_id = null;
183 refresh_field("row_id", tax.name, tax.parentfield);
184 throw e;
185 }
186 },
187
Anand Doshi61a2f682013-06-21 17:55:31 +0530188 set_dynamic_labels: function() {
189 // What TODO? should we make price list system non-mandatory?
190 this.frm.toggle_reqd("plc_conversion_rate",
191 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
192
193 var company_currency = this.get_company_currency();
194 this.change_form_labels(company_currency);
195 this.change_grid_labels(company_currency);
196 },
197
Anand Doshi923d41d2013-05-28 17:23:36 +0530198 recalculate: function() {
199 this.calculate_taxes_and_totals();
200 },
201
202 recalculate_values: function() {
203 this.calculate_taxes_and_totals();
204 },
205
Anand Doshid0b00722013-05-28 18:54:48 +0530206 calculate_charges: function() {
207 this.calculate_taxes_and_totals();
208 },
209
Anand Doshi3543f302013-05-24 19:25:01 +0530210 included_in_print_rate: function(doc, cdt, cdn) {
211 var tax = wn.model.get_doc(cdt, cdn);
212 try {
213 this.validate_on_previous_row(tax);
214 this.validate_inclusive_tax(tax);
215 this.calculate_taxes_and_totals();
216 } catch(e) {
217 tax.included_in_print_rate = 0;
218 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
219 throw e;
220 }
221 },
222
223 validate_on_previous_row: function(tax) {
224 // validate if a valid row id is mentioned in case of
225 // On Previous Row Amount and On Previous Row Total
226 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
227 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
228 var msg = repl(wn._("Row") + " # %(idx)s [%(doctype)s]: " +
229 wn._("Please specify a valid") + " %(row_id_label)s", {
230 idx: tax.idx,
231 doctype: tax.doctype,
232 row_id_label: wn.meta.get_label(tax.doctype, "row_id", tax.name)
233 });
234 msgprint(msg);
235 throw msg;
236 }
237 },
238
239 validate_inclusive_tax: function(tax) {
240 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
241
242 var actual_type_error = function() {
243 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
244 "%(charge_type_label)s = \"%(charge_type)s\" " +
245 wn._("cannot be included in Item's rate"), {
246 idx: tax.idx,
247 doctype: tax.doctype,
248 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
249 charge_type: tax.charge_type
250 });
251 msgprint(msg);
252 throw msg;
253 };
254
255 var on_previous_row_error = function(row_range) {
256 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
257 wn._("to be included in Item's rate, it is required that: ") +
258 " [" + wn._("Row") + " # %(row_range)s] " + wn._("also be included in Item's rate"), {
259 idx: tax.idx,
260 doctype: tax.doctype,
261 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
262 charge_type: tax.charge_type,
263 inclusive_label: wn.meta.get_label(tax.doctype, "included_in_print_rate", tax.name),
264 row_range: row_range,
265 });
266
267 msgprint(msg);
268 throw msg;
269 };
270
271 if(cint(tax.included_in_print_rate)) {
272 if(tax.charge_type == "Actual") {
273 // inclusive tax cannot be of type Actual
274 actual_type_error();
275 } else if(tax.charge_type == "On Previous Row Amount" &&
276 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
277 // referred row should also be an inclusive tax
278 on_previous_row_error(tax.row_id);
279 } else if(tax.charge_type == "On Previous Row Total") {
280 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
281 function(t) { return cint(t.included_in_print_rate) ? null : t; });
282 if(taxes_not_included.length > 0) {
283 // all rows above this tax should be inclusive
284 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
285 }
286 }
287 }
288 },
289
290 _load_item_tax_rate: function(item_tax_rate) {
291 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
292 },
293
294 _get_tax_rate: function(tax, item_tax_map) {
295 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
296 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
297 tax.rate;
298 },
299
300 get_item_wise_taxes_html: function() {
301 var item_tax = {};
302 var tax_accounts = [];
303 var company_currency = this.get_company_currency();
304
305 $.each(this.get_tax_doclist(), function(i, tax) {
306 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530307 var tax_rate_precision = precision("rate", tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530308 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530309 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530310 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530311 if($.isArray(tax_data)) {
312 var tax_rate = tax_data[0] == null ? "" : (flt(tax_data[0], tax_rate_precision) + "%"),
313 tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency);
314
315 item_tax[item_code][tax.account_head] = [tax_rate, tax_amount];
316 } else {
317 item_tax[item_code][tax.account_head] = [flt(tax_data, tax_rate_precision) + "%", ""];
318 }
Anand Doshi3543f302013-05-24 19:25:01 +0530319 });
320 tax_accounts.push(tax.account_head);
321 });
322
323 var headings = $.map([wn._("Item Name")].concat(tax_accounts),
324 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
325
326 var rows = $.map(this.get_item_doclist(), function(item) {
327 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530328 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530329 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
330 item_name: item.item_name,
331 taxes: $.map(tax_accounts, function(head) {
Anand Doshi64f316b2013-07-17 15:12:22 +0530332 return item_tax_record[head] ?
333 "<td>(" + item_tax_record[head][0] + ") " + item_tax_record[head][1] + "</td>" :
334 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530335 }).join("\n")
336 });
337 }).join("\n");
338
Anand Doshi53b73422013-05-31 11:50:01 +0530339 if(!rows) return "";
Anand Doshi3543f302013-05-24 19:25:01 +0530340 return '<div style="overflow-x: scroll;"><table class="table table-bordered table-hover">\
341 <thead><tr>' + headings + '</tr></thead> \
342 <tbody>' + rows + '</tbody> \
343 </table></div>';
344 },
345
346 set_default_values: function() {
347 $.each(wn.model.get_doclist(this.frm.doctype, this.frm.docname), function(i, doc) {
348 var updated = wn.model.set_default_values(doc);
349 if(doc.parentfield) {
350 refresh_field(doc.parentfield);
351 } else {
352 refresh_field(updated);
353 }
354 });
355 },
356
Anand Doshi923d41d2013-05-28 17:23:36 +0530357 _validate_before_fetch: function(fieldname) {
358 var me = this;
359 if(!me.frm.doc[fieldname]) {
360 return (wn._("Please specify") + ": " +
361 wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
362 ". " + wn._("It is needed to fetch Item Details."));
363 }
364 return null;
365 },
366
Anand Doshi3543f302013-05-24 19:25:01 +0530367 validate_company_and_party: function(party_field) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530368 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530369 var valid = true;
Anand Doshi923d41d2013-05-28 17:23:36 +0530370 var msg = "";
Anand Doshi3543f302013-05-24 19:25:01 +0530371 $.each(["company", party_field], function(i, fieldname) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530372 var msg_for_fieldname = me._validate_before_fetch(fieldname);
373 if(msg_for_fieldname) {
374 msgprint(msg_for_fieldname);
Anand Doshi3543f302013-05-24 19:25:01 +0530375 valid = false;
Anand Doshi3543f302013-05-24 19:25:01 +0530376 }
377 });
378 return valid;
379 },
380
381 get_item_doclist: function() {
382 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
383 {parentfield: this.fname});
384 },
385
386 get_tax_doclist: function() {
387 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
388 {parentfield: this.other_fname});
389 },
390
391 validate_conversion_rate: function() {
392 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
393 var conversion_rate_label = wn.meta.get_label(this.frm.doc.doctype, "conversion_rate",
394 this.frm.doc.name);
395
396 if(this.frm.doc.conversion_rate == 0) {
397 wn.throw(wn._(conversion_rate_label) + " " + wn._("cannot be 0"));
398 }
399
400 var company_currency = this.get_company_currency();
Anand Doshi923d41d2013-05-28 17:23:36 +0530401 var valid_conversion_rate = this.frm.doc.conversion_rate ?
Anand Doshi3543f302013-05-24 19:25:01 +0530402 ((this.frm.doc.currency == company_currency && this.frm.doc.conversion_rate == 1.0) ||
403 (this.frm.doc.currency != company_currency && this.frm.doc.conversion_rate != 1.0)) :
404 false;
405
406 if(!valid_conversion_rate) {
407 wn.throw(wn._("Please enter valid") + " " + wn._(conversion_rate_label) +
408 " 1 " + this.frm.doc.currency + " = [?] " + company_currency);
409 }
410 },
411
412 calculate_taxes_and_totals: function() {
413 this.validate_conversion_rate();
414 this.frm.item_doclist = this.get_item_doclist();
415 this.frm.tax_doclist = this.get_tax_doclist();
416
417 this.calculate_item_values();
418 this.initialize_taxes();
419 this.determine_exclusive_rate && this.determine_exclusive_rate();
420 this.calculate_net_total();
421 this.calculate_taxes();
422 this.calculate_totals();
423 this._cleanup();
Anand Doshi923d41d2013-05-28 17:23:36 +0530424
Anand Doshi3543f302013-05-24 19:25:01 +0530425 this.show_item_wise_taxes();
426 },
427
428 initialize_taxes: function() {
429 var me = this;
430 $.each(this.frm.tax_doclist, function(i, tax) {
431 tax.item_wise_tax_detail = {};
432 $.each(["tax_amount", "total",
433 "tax_amount_for_current_item", "grand_total_for_current_item",
434 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
435 function(i, fieldname) { tax[fieldname] = 0.0 });
436
437 me.validate_on_previous_row(tax);
438 me.validate_inclusive_tax(tax);
439 wn.model.round_floats_in(tax);
440 });
441 },
442
443 calculate_taxes: function() {
444 var me = this;
445
446 $.each(this.frm.item_doclist, function(n, item) {
447 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
448
449 $.each(me.frm.tax_doclist, function(i, tax) {
450 // tax_amount represents the amount of tax for the current step
451 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530452
Anand Doshi3543f302013-05-24 19:25:01 +0530453 me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
454
455 // case when net total is 0 but there is an actual type charge
456 // in this case add the actual amount to tax.tax_amount
457 // and tax.grand_total_for_current_item for the first such iteration
458 if(tax.charge_type == "Actual" &&
459 !(current_tax_amount || me.frm.doc.net_total || tax.tax_amount)) {
460 var zero_net_total_adjustment = flt(tax.rate, precision("tax_amount", tax));
461 current_tax_amount += zero_net_total_adjustment;
462 }
463
464 // store tax_amount for current item as it will be used for
465 // charge type = 'On Previous Row Amount'
466 tax.tax_amount_for_current_item = current_tax_amount;
467
468 // accumulate tax amount into tax.tax_amount
469 tax.tax_amount += current_tax_amount;
470
Anand Doshi3543f302013-05-24 19:25:01 +0530471 // for buying
472 if(tax.category) {
473 // if just for valuation, do not add the tax amount in total
474 // hence, setting it as 0 for further steps
475 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
476
477 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
478 }
479
480 // Calculate tax.total viz. grand total till that step
481 // note: grand_total_for_current_item contains the contribution of
482 // item's amount, previously applied tax and the current tax on that item
483 if(i==0) {
484 tax.grand_total_for_current_item = flt(item.amount + current_tax_amount,
485 precision("total", tax));
486 } else {
487 tax.grand_total_for_current_item =
488 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
489 precision("total", tax));
490 }
491
492 // in tax.total, accumulate grand total for each item
493 tax.total += tax.grand_total_for_current_item;
494 });
495 });
496 },
497
498 get_current_tax_amount: function(item, tax, item_tax_map) {
499 var tax_rate = this._get_tax_rate(tax, item_tax_map);
500 var current_tax_amount = 0.0;
501
502 if(tax.charge_type == "Actual") {
503 // distribute the tax amount proportionally to each item row
504 var actual = flt(tax.rate, precision("tax_amount", tax));
505 current_tax_amount = this.frm.doc.net_total ?
506 ((item.amount / this.frm.doc.net_total) * actual) :
507 0.0;
508
509 } else if(tax.charge_type == "On Net Total") {
510 current_tax_amount = (tax_rate / 100.0) * item.amount;
511
512 } else if(tax.charge_type == "On Previous Row Amount") {
513 current_tax_amount = (tax_rate / 100.0) *
514 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
515
516 } else if(tax.charge_type == "On Previous Row Total") {
517 current_tax_amount = (tax_rate / 100.0) *
518 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
519
520 }
521
Anand Doshi53b73422013-05-31 11:50:01 +0530522 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
523
524 // store tax breakup for each item
525 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
526
527 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530528 },
529
530 _cleanup: function() {
531 $.each(this.frm.tax_doclist, function(i, tax) {
532 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
533 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
534 function(i, fieldname) { delete tax[fieldname]; });
535
536 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
537 });
538 },
Anand Doshifc777182013-05-27 19:29:07 +0530539
540 calculate_total_advance: function(parenttype, advance_parentfield) {
541 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
542 var advance_doclist = wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
543 {parentfield: advance_parentfield});
544 this.frm.doc.total_advance = flt(wn.utils.sum(
545 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
546 ), precision("total_advance"));
547
548 this.calculate_outstanding_amount();
549 }
550 },
Anand Doshi3543f302013-05-24 19:25:01 +0530551
552 _set_in_company_currency: function(item, print_field, base_field) {
553 // set values in base currency
554 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
555 precision(base_field, item));
556 },
Anand Doshibf3e54a2013-06-05 14:11:32 +0530557
558 get_terms: function() {
559 var me = this;
560 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530561 return this.frm.call({
Anand Doshibf3e54a2013-06-05 14:11:32 +0530562 method: "webnotes.client.get_value",
563 args: {
564 doctype: "Terms and Conditions",
565 fieldname: "terms",
566 filters: { name: this.frm.doc.tc_name },
567 },
568 });
569 }
570 },
Anand Doshi3543f302013-05-24 19:25:01 +0530571});