blob: e460982f7047ad68151c49d50cd86da359cfcbcd [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
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530117 get_price_list_currency: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +0530118 var me = this;
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530119 var fieldname = buying_or_selling.toLowerCase() + "_price_list";
120 if(this.frm.doc[fieldname]) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530121 return this.frm.call({
Anand Doshi3543f302013-05-24 19:25:01 +0530122 method: "setup.utils.get_price_list_currency",
Nabin Hait48a0bd32013-07-23 15:31:39 +0530123 args: {
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530124 price_list: this.frm.doc[fieldname],
Nabin Hait48a0bd32013-07-23 15:31:39 +0530125 },
Anand Doshi3543f302013-05-24 19:25:01 +0530126 callback: function(r) {
127 if(!r.exc) {
128 me.price_list_currency();
129 }
130 }
131 });
132 }
133 },
134
Anand Doshi61a2f682013-06-21 17:55:31 +0530135 get_exchange_rate: function(from_currency, to_currency, callback) {
136 var exchange_name = from_currency + "-" + to_currency;
137 wn.model.with_doc("Currency Exchange", exchange_name, function(name) {
138 var exchange_doc = wn.model.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530139 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530140 });
141 },
142
Anand Doshi3543f302013-05-24 19:25:01 +0530143 price_list_currency: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530144 this.set_dynamic_labels();
Anand Doshi61a2f682013-06-21 17:55:31 +0530145
146 var company_currency = this.get_company_currency();
147 if(this.frm.doc.price_list_currency !== company_currency) {
148 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
149 function(exchange_rate) {
150 if(exchange_rate) {
151 me.frm.set_value("price_list_currency", exchange_rate);
152 me.plc_conversion_rate();
153 }
154 });
155 } else {
156 this.plc_conversion_rate();
157 }
Anand Doshi3543f302013-05-24 19:25:01 +0530158 },
159
160 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530161 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
162 this.frm.set_value("plc_conversion_rate", 1.0);
163 } else if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
164 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
165 this.calculate_taxes_and_totals();
166 }
Anand Doshi3543f302013-05-24 19:25:01 +0530167 },
168
169 qty: function(doc, cdt, cdn) {
170 this.calculate_taxes_and_totals();
171 },
172
Anand Doshi923d41d2013-05-28 17:23:36 +0530173 tax_rate: function(doc, cdt, cdn) {
174 this.calculate_taxes_and_totals();
175 },
176
177 row_id: function(doc, cdt, cdn) {
178 var tax = wn.model.get_doc(cdt, cdn);
179 try {
180 this.validate_on_previous_row(tax);
181 this.calculate_taxes_and_totals();
182 } catch(e) {
183 tax.row_id = null;
184 refresh_field("row_id", tax.name, tax.parentfield);
185 throw e;
186 }
187 },
188
Anand Doshi61a2f682013-06-21 17:55:31 +0530189 set_dynamic_labels: function() {
190 // What TODO? should we make price list system non-mandatory?
191 this.frm.toggle_reqd("plc_conversion_rate",
192 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
193
194 var company_currency = this.get_company_currency();
195 this.change_form_labels(company_currency);
196 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530197 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530198 },
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
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530317 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530318 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530319 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530320 }
Anand Doshi3543f302013-05-24 19:25:01 +0530321 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530322 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530323 });
324
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530325 var headings = $.map([wn._("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530326 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 Doshiaa71b5b2013-08-05 13:00:53 +0530334 return item_tax_record[head[0]] ?
335 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530336 "<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
Anand Doshi923d41d2013-05-28 17:23:36 +0530348 _validate_before_fetch: function(fieldname) {
349 var me = this;
350 if(!me.frm.doc[fieldname]) {
351 return (wn._("Please specify") + ": " +
352 wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
353 ". " + wn._("It is needed to fetch Item Details."));
354 }
355 return null;
356 },
357
Anand Doshi3543f302013-05-24 19:25:01 +0530358 validate_company_and_party: function(party_field) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530359 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530360 var valid = true;
Anand Doshi923d41d2013-05-28 17:23:36 +0530361 var msg = "";
Anand Doshi3543f302013-05-24 19:25:01 +0530362 $.each(["company", party_field], function(i, fieldname) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530363 var msg_for_fieldname = me._validate_before_fetch(fieldname);
364 if(msg_for_fieldname) {
365 msgprint(msg_for_fieldname);
Anand Doshi3543f302013-05-24 19:25:01 +0530366 valid = false;
Anand Doshi3543f302013-05-24 19:25:01 +0530367 }
368 });
369 return valid;
370 },
371
372 get_item_doclist: function() {
373 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
374 {parentfield: this.fname});
375 },
376
377 get_tax_doclist: function() {
378 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
379 {parentfield: this.other_fname});
380 },
381
382 validate_conversion_rate: function() {
383 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
384 var conversion_rate_label = wn.meta.get_label(this.frm.doc.doctype, "conversion_rate",
385 this.frm.doc.name);
386
387 if(this.frm.doc.conversion_rate == 0) {
388 wn.throw(wn._(conversion_rate_label) + " " + wn._("cannot be 0"));
389 }
390
391 var company_currency = this.get_company_currency();
Anand Doshi923d41d2013-05-28 17:23:36 +0530392 var valid_conversion_rate = this.frm.doc.conversion_rate ?
Anand Doshi3543f302013-05-24 19:25:01 +0530393 ((this.frm.doc.currency == company_currency && this.frm.doc.conversion_rate == 1.0) ||
394 (this.frm.doc.currency != company_currency && this.frm.doc.conversion_rate != 1.0)) :
395 false;
396
397 if(!valid_conversion_rate) {
398 wn.throw(wn._("Please enter valid") + " " + wn._(conversion_rate_label) +
399 " 1 " + this.frm.doc.currency + " = [?] " + company_currency);
400 }
401 },
402
403 calculate_taxes_and_totals: function() {
404 this.validate_conversion_rate();
405 this.frm.item_doclist = this.get_item_doclist();
406 this.frm.tax_doclist = this.get_tax_doclist();
407
408 this.calculate_item_values();
409 this.initialize_taxes();
410 this.determine_exclusive_rate && this.determine_exclusive_rate();
411 this.calculate_net_total();
412 this.calculate_taxes();
413 this.calculate_totals();
414 this._cleanup();
Anand Doshi923d41d2013-05-28 17:23:36 +0530415
Anand Doshi3543f302013-05-24 19:25:01 +0530416 this.show_item_wise_taxes();
417 },
418
419 initialize_taxes: function() {
420 var me = this;
421 $.each(this.frm.tax_doclist, function(i, tax) {
422 tax.item_wise_tax_detail = {};
423 $.each(["tax_amount", "total",
424 "tax_amount_for_current_item", "grand_total_for_current_item",
425 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
426 function(i, fieldname) { tax[fieldname] = 0.0 });
427
428 me.validate_on_previous_row(tax);
429 me.validate_inclusive_tax(tax);
430 wn.model.round_floats_in(tax);
431 });
432 },
433
434 calculate_taxes: function() {
435 var me = this;
436
437 $.each(this.frm.item_doclist, function(n, item) {
438 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
439
440 $.each(me.frm.tax_doclist, function(i, tax) {
441 // tax_amount represents the amount of tax for the current step
442 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530443
Anand Doshi3543f302013-05-24 19:25:01 +0530444 me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
445
446 // case when net total is 0 but there is an actual type charge
447 // in this case add the actual amount to tax.tax_amount
448 // and tax.grand_total_for_current_item for the first such iteration
449 if(tax.charge_type == "Actual" &&
450 !(current_tax_amount || me.frm.doc.net_total || tax.tax_amount)) {
451 var zero_net_total_adjustment = flt(tax.rate, precision("tax_amount", tax));
452 current_tax_amount += zero_net_total_adjustment;
453 }
454
455 // store tax_amount for current item as it will be used for
456 // charge type = 'On Previous Row Amount'
457 tax.tax_amount_for_current_item = current_tax_amount;
458
459 // accumulate tax amount into tax.tax_amount
460 tax.tax_amount += current_tax_amount;
461
Anand Doshi3543f302013-05-24 19:25:01 +0530462 // for buying
463 if(tax.category) {
464 // if just for valuation, do not add the tax amount in total
465 // hence, setting it as 0 for further steps
466 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
467
468 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
469 }
470
471 // Calculate tax.total viz. grand total till that step
472 // note: grand_total_for_current_item contains the contribution of
473 // item's amount, previously applied tax and the current tax on that item
474 if(i==0) {
475 tax.grand_total_for_current_item = flt(item.amount + current_tax_amount,
476 precision("total", tax));
477 } else {
478 tax.grand_total_for_current_item =
479 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
480 precision("total", tax));
481 }
482
483 // in tax.total, accumulate grand total for each item
484 tax.total += tax.grand_total_for_current_item;
485 });
486 });
487 },
488
489 get_current_tax_amount: function(item, tax, item_tax_map) {
490 var tax_rate = this._get_tax_rate(tax, item_tax_map);
491 var current_tax_amount = 0.0;
492
493 if(tax.charge_type == "Actual") {
494 // distribute the tax amount proportionally to each item row
495 var actual = flt(tax.rate, precision("tax_amount", tax));
496 current_tax_amount = this.frm.doc.net_total ?
497 ((item.amount / this.frm.doc.net_total) * actual) :
498 0.0;
499
500 } else if(tax.charge_type == "On Net Total") {
501 current_tax_amount = (tax_rate / 100.0) * item.amount;
502
503 } else if(tax.charge_type == "On Previous Row Amount") {
504 current_tax_amount = (tax_rate / 100.0) *
505 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
506
507 } else if(tax.charge_type == "On Previous Row Total") {
508 current_tax_amount = (tax_rate / 100.0) *
509 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
510
511 }
512
Anand Doshi53b73422013-05-31 11:50:01 +0530513 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
514
515 // store tax breakup for each item
516 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
517
518 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530519 },
520
521 _cleanup: function() {
522 $.each(this.frm.tax_doclist, function(i, tax) {
523 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
524 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
525 function(i, fieldname) { delete tax[fieldname]; });
526
527 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
528 });
529 },
Anand Doshifc777182013-05-27 19:29:07 +0530530
531 calculate_total_advance: function(parenttype, advance_parentfield) {
532 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
533 var advance_doclist = wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
534 {parentfield: advance_parentfield});
535 this.frm.doc.total_advance = flt(wn.utils.sum(
536 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
537 ), precision("total_advance"));
538
539 this.calculate_outstanding_amount();
540 }
541 },
Anand Doshi3543f302013-05-24 19:25:01 +0530542
543 _set_in_company_currency: function(item, print_field, base_field) {
544 // set values in base currency
545 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
546 precision(base_field, item));
547 },
Anand Doshibf3e54a2013-06-05 14:11:32 +0530548
549 get_terms: function() {
550 var me = this;
551 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530552 return this.frm.call({
Anand Doshibf3e54a2013-06-05 14:11:32 +0530553 method: "webnotes.client.get_value",
554 args: {
555 doctype: "Terms and Conditions",
556 fieldname: "terms",
557 filters: { name: this.frm.doc.tc_name },
558 },
559 });
560 }
561 },
Anand Doshi3543f302013-05-24 19:25:01 +0530562});