blob: c0a7506ea901a041f2808cb9751016ae1a36ed73 [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
43 onload_post_render: function() {
44 if(this.frm.doc.__islocal && this.frm.doc.company) {
45 var me = this;
Anand Doshi1fac2a92013-07-29 19:30:39 +053046 return this.frm.call({
Anand Doshi3543f302013-05-24 19:25:01 +053047 doc: this.frm.doc,
48 method: "onload_post_render",
49 freeze: true,
50 callback: function(r) {
51 // remove this call when using client side mapper
52 me.set_default_values();
53 me.frm.refresh();
54 }
55 });
56 }
57 },
58
Anand Doshi923d41d2013-05-28 17:23:36 +053059 validate: function() {
60 this.calculate_taxes_and_totals();
61 },
62
Anand Doshi3543f302013-05-24 19:25:01 +053063 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +053064 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Anand Doshi17350b82013-08-01 15:45:23 +053065 if(!this.frm.doc.currency) {
66 this.frm.set_value("currency", this.get_company_currency());
67 }
68
Nabin Hait48a0bd32013-07-23 15:31:39 +053069 this.frm.script_manager.trigger("currency");
Anand Doshi3543f302013-05-24 19:25:01 +053070 }
71 },
72
73 get_company_currency: function() {
74 return erpnext.get_currency(this.frm.doc.company);
75 },
76
77 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +053078 var me = this;
79 this.set_dynamic_labels();
Anand Doshid52b03a2013-06-21 12:22:52 +053080
Anand Doshi61a2f682013-06-21 17:55:31 +053081 var company_currency = this.get_company_currency();
82 if(this.frm.doc.currency !== company_currency) {
83 this.get_exchange_rate(this.frm.doc.currency, company_currency,
84 function(exchange_rate) {
85 if(exchange_rate) {
86 me.frm.set_value("conversion_rate", exchange_rate);
87 me.conversion_rate();
88 }
89 });
90 } else {
91 this.conversion_rate();
92 }
93 },
94
95 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +053096 if(this.frm.doc.currency === this.get_company_currency()) {
97 this.frm.set_value("conversion_rate", 1.0);
Anand Doshi61a2f682013-06-21 17:55:31 +053098 } else if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
99 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
100 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
101 }
102
103 this.calculate_taxes_and_totals();
Anand Doshi3543f302013-05-24 19:25:01 +0530104 },
105
Anand Doshi060d9242013-06-12 17:40:36 +0530106 price_list_name: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +0530107 var me = this;
108 if(this.frm.doc.price_list_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530109 return this.frm.call({
Anand Doshi3543f302013-05-24 19:25:01 +0530110 method: "setup.utils.get_price_list_currency",
Nabin Hait48a0bd32013-07-23 15:31:39 +0530111 args: {
Anand Doshi3543f302013-05-24 19:25:01 +0530112 price_list_name: this.frm.doc.price_list_name,
Nabin Hait48a0bd32013-07-23 15:31:39 +0530113 },
Anand Doshi3543f302013-05-24 19:25:01 +0530114 callback: function(r) {
115 if(!r.exc) {
116 me.price_list_currency();
117 }
118 }
119 });
120 }
121 },
122
Anand Doshi61a2f682013-06-21 17:55:31 +0530123 get_exchange_rate: function(from_currency, to_currency, callback) {
124 var exchange_name = from_currency + "-" + to_currency;
125 wn.model.with_doc("Currency Exchange", exchange_name, function(name) {
126 var exchange_doc = wn.model.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530127 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530128 });
129 },
130
Anand Doshi3543f302013-05-24 19:25:01 +0530131 price_list_currency: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530132 this.set_dynamic_labels();
Anand Doshi61a2f682013-06-21 17:55:31 +0530133
134 var company_currency = this.get_company_currency();
135 if(this.frm.doc.price_list_currency !== company_currency) {
136 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
137 function(exchange_rate) {
138 if(exchange_rate) {
139 me.frm.set_value("price_list_currency", exchange_rate);
140 me.plc_conversion_rate();
141 }
142 });
143 } else {
144 this.plc_conversion_rate();
145 }
Anand Doshi3543f302013-05-24 19:25:01 +0530146 },
147
148 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530149 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
150 this.frm.set_value("plc_conversion_rate", 1.0);
151 } else if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
152 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
153 this.calculate_taxes_and_totals();
154 }
Anand Doshi3543f302013-05-24 19:25:01 +0530155 },
156
157 qty: function(doc, cdt, cdn) {
158 this.calculate_taxes_and_totals();
159 },
160
Anand Doshi923d41d2013-05-28 17:23:36 +0530161 tax_rate: function(doc, cdt, cdn) {
162 this.calculate_taxes_and_totals();
163 },
164
165 row_id: function(doc, cdt, cdn) {
166 var tax = wn.model.get_doc(cdt, cdn);
167 try {
168 this.validate_on_previous_row(tax);
169 this.calculate_taxes_and_totals();
170 } catch(e) {
171 tax.row_id = null;
172 refresh_field("row_id", tax.name, tax.parentfield);
173 throw e;
174 }
175 },
176
Anand Doshi61a2f682013-06-21 17:55:31 +0530177 set_dynamic_labels: function() {
178 // What TODO? should we make price list system non-mandatory?
179 this.frm.toggle_reqd("plc_conversion_rate",
180 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
181
182 var company_currency = this.get_company_currency();
183 this.change_form_labels(company_currency);
184 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530185 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530186 },
187
Anand Doshi923d41d2013-05-28 17:23:36 +0530188 recalculate: function() {
189 this.calculate_taxes_and_totals();
190 },
191
192 recalculate_values: function() {
193 this.calculate_taxes_and_totals();
194 },
195
Anand Doshid0b00722013-05-28 18:54:48 +0530196 calculate_charges: function() {
197 this.calculate_taxes_and_totals();
198 },
199
Anand Doshi3543f302013-05-24 19:25:01 +0530200 included_in_print_rate: function(doc, cdt, cdn) {
201 var tax = wn.model.get_doc(cdt, cdn);
202 try {
203 this.validate_on_previous_row(tax);
204 this.validate_inclusive_tax(tax);
205 this.calculate_taxes_and_totals();
206 } catch(e) {
207 tax.included_in_print_rate = 0;
208 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
209 throw e;
210 }
211 },
212
213 validate_on_previous_row: function(tax) {
214 // validate if a valid row id is mentioned in case of
215 // On Previous Row Amount and On Previous Row Total
216 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
217 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
218 var msg = repl(wn._("Row") + " # %(idx)s [%(doctype)s]: " +
219 wn._("Please specify a valid") + " %(row_id_label)s", {
220 idx: tax.idx,
221 doctype: tax.doctype,
222 row_id_label: wn.meta.get_label(tax.doctype, "row_id", tax.name)
223 });
224 msgprint(msg);
225 throw msg;
226 }
227 },
228
229 validate_inclusive_tax: function(tax) {
230 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
231
232 var actual_type_error = function() {
233 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
234 "%(charge_type_label)s = \"%(charge_type)s\" " +
235 wn._("cannot be included in Item's rate"), {
236 idx: tax.idx,
237 doctype: tax.doctype,
238 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
239 charge_type: tax.charge_type
240 });
241 msgprint(msg);
242 throw msg;
243 };
244
245 var on_previous_row_error = function(row_range) {
246 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
247 wn._("to be included in Item's rate, it is required that: ") +
248 " [" + wn._("Row") + " # %(row_range)s] " + wn._("also be included in Item's rate"), {
249 idx: tax.idx,
250 doctype: tax.doctype,
251 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
252 charge_type: tax.charge_type,
253 inclusive_label: wn.meta.get_label(tax.doctype, "included_in_print_rate", tax.name),
254 row_range: row_range,
255 });
256
257 msgprint(msg);
258 throw msg;
259 };
260
261 if(cint(tax.included_in_print_rate)) {
262 if(tax.charge_type == "Actual") {
263 // inclusive tax cannot be of type Actual
264 actual_type_error();
265 } else if(tax.charge_type == "On Previous Row Amount" &&
266 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
267 // referred row should also be an inclusive tax
268 on_previous_row_error(tax.row_id);
269 } else if(tax.charge_type == "On Previous Row Total") {
270 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
271 function(t) { return cint(t.included_in_print_rate) ? null : t; });
272 if(taxes_not_included.length > 0) {
273 // all rows above this tax should be inclusive
274 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
275 }
276 }
277 }
278 },
279
280 _load_item_tax_rate: function(item_tax_rate) {
281 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
282 },
283
284 _get_tax_rate: function(tax, item_tax_map) {
285 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
286 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
287 tax.rate;
288 },
289
290 get_item_wise_taxes_html: function() {
291 var item_tax = {};
292 var tax_accounts = [];
293 var company_currency = this.get_company_currency();
294
295 $.each(this.get_tax_doclist(), function(i, tax) {
296 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530297 var tax_rate_precision = precision("rate", tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530298 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530299 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530300 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530301 if($.isArray(tax_data)) {
302 var tax_rate = tax_data[0] == null ? "" : (flt(tax_data[0], tax_rate_precision) + "%"),
303 tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency);
304
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530305 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530306 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530307 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530308 }
Anand Doshi3543f302013-05-24 19:25:01 +0530309 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530310 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530311 });
312
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530313 var headings = $.map([wn._("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530314 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
315
316 var rows = $.map(this.get_item_doclist(), function(item) {
317 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530318 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530319 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
320 item_name: item.item_name,
321 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530322 return item_tax_record[head[0]] ?
323 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530324 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530325 }).join("\n")
326 });
327 }).join("\n");
328
Anand Doshi53b73422013-05-31 11:50:01 +0530329 if(!rows) return "";
Anand Doshi3543f302013-05-24 19:25:01 +0530330 return '<div style="overflow-x: scroll;"><table class="table table-bordered table-hover">\
331 <thead><tr>' + headings + '</tr></thead> \
332 <tbody>' + rows + '</tbody> \
333 </table></div>';
334 },
335
336 set_default_values: function() {
337 $.each(wn.model.get_doclist(this.frm.doctype, this.frm.docname), function(i, doc) {
338 var updated = wn.model.set_default_values(doc);
339 if(doc.parentfield) {
340 refresh_field(doc.parentfield);
341 } else {
342 refresh_field(updated);
343 }
344 });
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});