blob: c4601dd0c9855ace6adf1577a2b61f94d9c7c776 [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
Anand Doshi5e4e5d72013-08-06 17:23:44 +053035 onload_post_render: function() {
36 if(this.frm.doc.__islocal && this.frm.doc.company && !this.frm.doc.customer) {
37 var me = this;
38 return this.frm.call({
39 doc: this.frm.doc,
40 method: "onload_post_render",
41 freeze: true,
42 callback: function(r) {
43 // remove this call when using client side mapper
44 me.set_default_values();
Anand Doshi79251492013-08-09 00:20:34 +053045 me.set_dynamic_labels();
Anand Doshi5e4e5d72013-08-06 17:23:44 +053046 }
47 });
48 }
49 },
50
Anand Doshi3543f302013-05-24 19:25:01 +053051 refresh: function() {
52 this.frm.clear_custom_buttons();
53 erpnext.hide_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +053054 erpnext.hide_company();
Anand Doshi3543f302013-05-24 19:25:01 +053055 this.show_item_wise_taxes();
Nabin Hait1f996c32013-07-29 19:00:53 +053056 this.set_dynamic_labels();
Anand Doshi3543f302013-05-24 19:25:01 +053057 },
58
Anand Doshi923d41d2013-05-28 17:23:36 +053059 validate: function() {
60 this.calculate_taxes_and_totals();
61 },
62
Anand Doshi5e4e5d72013-08-06 17:23:44 +053063 set_default_values: function() {
64 $.each(wn.model.get_doclist(this.frm.doctype, this.frm.docname), function(i, doc) {
65 var updated = wn.model.set_default_values(doc);
66 if(doc.parentfield) {
67 refresh_field(doc.parentfield);
68 } else {
69 refresh_field(updated);
70 }
71 });
72 },
73
Anand Doshi3543f302013-05-24 19:25:01 +053074 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +053075 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Anand Doshi17350b82013-08-01 15:45:23 +053076 if(!this.frm.doc.currency) {
77 this.frm.set_value("currency", this.get_company_currency());
78 }
79
Nabin Hait48a0bd32013-07-23 15:31:39 +053080 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);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530196 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530197 },
198
Anand Doshi923d41d2013-05-28 17:23:36 +0530199 recalculate: function() {
200 this.calculate_taxes_and_totals();
201 },
202
203 recalculate_values: function() {
204 this.calculate_taxes_and_totals();
205 },
206
Anand Doshid0b00722013-05-28 18:54:48 +0530207 calculate_charges: function() {
208 this.calculate_taxes_and_totals();
209 },
210
Anand Doshi3543f302013-05-24 19:25:01 +0530211 included_in_print_rate: function(doc, cdt, cdn) {
212 var tax = wn.model.get_doc(cdt, cdn);
213 try {
214 this.validate_on_previous_row(tax);
215 this.validate_inclusive_tax(tax);
216 this.calculate_taxes_and_totals();
217 } catch(e) {
218 tax.included_in_print_rate = 0;
219 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
220 throw e;
221 }
222 },
223
224 validate_on_previous_row: function(tax) {
225 // validate if a valid row id is mentioned in case of
226 // On Previous Row Amount and On Previous Row Total
227 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
228 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
229 var msg = repl(wn._("Row") + " # %(idx)s [%(doctype)s]: " +
230 wn._("Please specify a valid") + " %(row_id_label)s", {
231 idx: tax.idx,
232 doctype: tax.doctype,
233 row_id_label: wn.meta.get_label(tax.doctype, "row_id", tax.name)
234 });
235 msgprint(msg);
236 throw msg;
237 }
238 },
239
240 validate_inclusive_tax: function(tax) {
241 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
242
243 var actual_type_error = function() {
244 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
245 "%(charge_type_label)s = \"%(charge_type)s\" " +
246 wn._("cannot be included in Item's rate"), {
247 idx: tax.idx,
248 doctype: tax.doctype,
249 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
250 charge_type: tax.charge_type
251 });
252 msgprint(msg);
253 throw msg;
254 };
255
256 var on_previous_row_error = function(row_range) {
257 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
258 wn._("to be included in Item's rate, it is required that: ") +
259 " [" + wn._("Row") + " # %(row_range)s] " + wn._("also be included in Item's rate"), {
260 idx: tax.idx,
261 doctype: tax.doctype,
262 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
263 charge_type: tax.charge_type,
264 inclusive_label: wn.meta.get_label(tax.doctype, "included_in_print_rate", tax.name),
265 row_range: row_range,
266 });
267
268 msgprint(msg);
269 throw msg;
270 };
271
272 if(cint(tax.included_in_print_rate)) {
273 if(tax.charge_type == "Actual") {
274 // inclusive tax cannot be of type Actual
275 actual_type_error();
276 } else if(tax.charge_type == "On Previous Row Amount" &&
277 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
278 // referred row should also be an inclusive tax
279 on_previous_row_error(tax.row_id);
280 } else if(tax.charge_type == "On Previous Row Total") {
281 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
282 function(t) { return cint(t.included_in_print_rate) ? null : t; });
283 if(taxes_not_included.length > 0) {
284 // all rows above this tax should be inclusive
285 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
286 }
287 }
288 }
289 },
290
291 _load_item_tax_rate: function(item_tax_rate) {
292 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
293 },
294
295 _get_tax_rate: function(tax, item_tax_map) {
296 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
297 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
298 tax.rate;
299 },
300
301 get_item_wise_taxes_html: function() {
302 var item_tax = {};
303 var tax_accounts = [];
304 var company_currency = this.get_company_currency();
305
306 $.each(this.get_tax_doclist(), function(i, tax) {
307 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530308 var tax_rate_precision = precision("rate", tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530309 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530310 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530311 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530312 if($.isArray(tax_data)) {
313 var tax_rate = tax_data[0] == null ? "" : (flt(tax_data[0], tax_rate_precision) + "%"),
314 tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency);
315
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530316 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530317 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530318 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530319 }
Anand Doshi3543f302013-05-24 19:25:01 +0530320 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530321 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530322 });
323
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530324 var headings = $.map([wn._("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530325 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
326
327 var rows = $.map(this.get_item_doclist(), function(item) {
328 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530329 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530330 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
331 item_name: item.item_name,
332 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530333 return item_tax_record[head[0]] ?
334 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530335 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530336 }).join("\n")
337 });
338 }).join("\n");
339
Anand Doshi53b73422013-05-31 11:50:01 +0530340 if(!rows) return "";
Anand Doshi3543f302013-05-24 19:25:01 +0530341 return '<div style="overflow-x: scroll;"><table class="table table-bordered table-hover">\
342 <thead><tr>' + headings + '</tr></thead> \
343 <tbody>' + rows + '</tbody> \
344 </table></div>';
345 },
346
Anand Doshi923d41d2013-05-28 17:23:36 +0530347 _validate_before_fetch: function(fieldname) {
348 var me = this;
349 if(!me.frm.doc[fieldname]) {
350 return (wn._("Please specify") + ": " +
351 wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
352 ". " + wn._("It is needed to fetch Item Details."));
353 }
354 return null;
355 },
356
Anand Doshi3543f302013-05-24 19:25:01 +0530357 validate_company_and_party: function(party_field) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530358 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530359 var valid = true;
Anand Doshi923d41d2013-05-28 17:23:36 +0530360 var msg = "";
Anand Doshi3543f302013-05-24 19:25:01 +0530361 $.each(["company", party_field], function(i, fieldname) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530362 var msg_for_fieldname = me._validate_before_fetch(fieldname);
363 if(msg_for_fieldname) {
364 msgprint(msg_for_fieldname);
Anand Doshi3543f302013-05-24 19:25:01 +0530365 valid = false;
Anand Doshi3543f302013-05-24 19:25:01 +0530366 }
367 });
368 return valid;
369 },
370
371 get_item_doclist: function() {
372 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
373 {parentfield: this.fname});
374 },
375
376 get_tax_doclist: function() {
377 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
378 {parentfield: this.other_fname});
379 },
380
381 validate_conversion_rate: function() {
382 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
383 var conversion_rate_label = wn.meta.get_label(this.frm.doc.doctype, "conversion_rate",
384 this.frm.doc.name);
385
386 if(this.frm.doc.conversion_rate == 0) {
387 wn.throw(wn._(conversion_rate_label) + " " + wn._("cannot be 0"));
388 }
389
390 var company_currency = this.get_company_currency();
Anand Doshi923d41d2013-05-28 17:23:36 +0530391 var valid_conversion_rate = this.frm.doc.conversion_rate ?
Anand Doshi3543f302013-05-24 19:25:01 +0530392 ((this.frm.doc.currency == company_currency && this.frm.doc.conversion_rate == 1.0) ||
393 (this.frm.doc.currency != company_currency && this.frm.doc.conversion_rate != 1.0)) :
394 false;
395
396 if(!valid_conversion_rate) {
397 wn.throw(wn._("Please enter valid") + " " + wn._(conversion_rate_label) +
398 " 1 " + this.frm.doc.currency + " = [?] " + company_currency);
399 }
400 },
401
402 calculate_taxes_and_totals: function() {
403 this.validate_conversion_rate();
404 this.frm.item_doclist = this.get_item_doclist();
405 this.frm.tax_doclist = this.get_tax_doclist();
406
407 this.calculate_item_values();
408 this.initialize_taxes();
409 this.determine_exclusive_rate && this.determine_exclusive_rate();
410 this.calculate_net_total();
411 this.calculate_taxes();
412 this.calculate_totals();
413 this._cleanup();
Anand Doshi923d41d2013-05-28 17:23:36 +0530414
Anand Doshi3543f302013-05-24 19:25:01 +0530415 this.show_item_wise_taxes();
416 },
417
418 initialize_taxes: function() {
419 var me = this;
420 $.each(this.frm.tax_doclist, function(i, tax) {
421 tax.item_wise_tax_detail = {};
422 $.each(["tax_amount", "total",
423 "tax_amount_for_current_item", "grand_total_for_current_item",
424 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
425 function(i, fieldname) { tax[fieldname] = 0.0 });
426
427 me.validate_on_previous_row(tax);
428 me.validate_inclusive_tax(tax);
429 wn.model.round_floats_in(tax);
430 });
431 },
432
433 calculate_taxes: function() {
434 var me = this;
435
436 $.each(this.frm.item_doclist, function(n, item) {
437 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
438
439 $.each(me.frm.tax_doclist, function(i, tax) {
440 // tax_amount represents the amount of tax for the current step
441 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530442
Anand Doshi3543f302013-05-24 19:25:01 +0530443 me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
444
445 // case when net total is 0 but there is an actual type charge
446 // in this case add the actual amount to tax.tax_amount
447 // and tax.grand_total_for_current_item for the first such iteration
448 if(tax.charge_type == "Actual" &&
449 !(current_tax_amount || me.frm.doc.net_total || tax.tax_amount)) {
450 var zero_net_total_adjustment = flt(tax.rate, precision("tax_amount", tax));
451 current_tax_amount += zero_net_total_adjustment;
452 }
453
454 // store tax_amount for current item as it will be used for
455 // charge type = 'On Previous Row Amount'
456 tax.tax_amount_for_current_item = current_tax_amount;
457
458 // accumulate tax amount into tax.tax_amount
459 tax.tax_amount += current_tax_amount;
460
Anand Doshi3543f302013-05-24 19:25:01 +0530461 // for buying
462 if(tax.category) {
463 // if just for valuation, do not add the tax amount in total
464 // hence, setting it as 0 for further steps
465 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
466
467 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
468 }
469
470 // Calculate tax.total viz. grand total till that step
471 // note: grand_total_for_current_item contains the contribution of
472 // item's amount, previously applied tax and the current tax on that item
473 if(i==0) {
474 tax.grand_total_for_current_item = flt(item.amount + current_tax_amount,
475 precision("total", tax));
476 } else {
477 tax.grand_total_for_current_item =
478 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
479 precision("total", tax));
480 }
481
482 // in tax.total, accumulate grand total for each item
483 tax.total += tax.grand_total_for_current_item;
484 });
485 });
486 },
487
488 get_current_tax_amount: function(item, tax, item_tax_map) {
489 var tax_rate = this._get_tax_rate(tax, item_tax_map);
490 var current_tax_amount = 0.0;
491
492 if(tax.charge_type == "Actual") {
493 // distribute the tax amount proportionally to each item row
494 var actual = flt(tax.rate, precision("tax_amount", tax));
495 current_tax_amount = this.frm.doc.net_total ?
496 ((item.amount / this.frm.doc.net_total) * actual) :
497 0.0;
498
499 } else if(tax.charge_type == "On Net Total") {
500 current_tax_amount = (tax_rate / 100.0) * item.amount;
501
502 } else if(tax.charge_type == "On Previous Row Amount") {
503 current_tax_amount = (tax_rate / 100.0) *
504 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
505
506 } else if(tax.charge_type == "On Previous Row Total") {
507 current_tax_amount = (tax_rate / 100.0) *
508 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
509
510 }
511
Anand Doshi53b73422013-05-31 11:50:01 +0530512 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
513
514 // store tax breakup for each item
515 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
516
517 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530518 },
519
520 _cleanup: function() {
521 $.each(this.frm.tax_doclist, function(i, tax) {
522 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
523 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
524 function(i, fieldname) { delete tax[fieldname]; });
525
526 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
527 });
528 },
Anand Doshifc777182013-05-27 19:29:07 +0530529
530 calculate_total_advance: function(parenttype, advance_parentfield) {
531 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
532 var advance_doclist = wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
533 {parentfield: advance_parentfield});
534 this.frm.doc.total_advance = flt(wn.utils.sum(
535 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
536 ), precision("total_advance"));
537
538 this.calculate_outstanding_amount();
539 }
540 },
Anand Doshi3543f302013-05-24 19:25:01 +0530541
542 _set_in_company_currency: function(item, print_field, base_field) {
543 // set values in base currency
544 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
545 precision(base_field, item));
546 },
Anand Doshibf3e54a2013-06-05 14:11:32 +0530547
548 get_terms: function() {
549 var me = this;
550 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530551 return this.frm.call({
Anand Doshibf3e54a2013-06-05 14:11:32 +0530552 method: "webnotes.client.get_value",
553 args: {
554 doctype: "Terms and Conditions",
555 fieldname: "terms",
556 filters: { name: this.frm.doc.tc_name },
557 },
558 });
559 }
560 },
Anand Doshi3543f302013-05-24 19:25:01 +0530561});