blob: 118594c8160c3f8615036757fe8dda15ff2e60b7 [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) + "%"),
Anand Doshie42a0222013-08-12 11:58:02 +0530315 tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
316 tax_amount_precision);
Anand Doshi53b73422013-05-31 11:50:01 +0530317
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530318 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530319 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530320 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530321 }
Anand Doshi3543f302013-05-24 19:25:01 +0530322 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530323 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530324 });
325
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530326 var headings = $.map([wn._("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530327 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
328
329 var rows = $.map(this.get_item_doclist(), function(item) {
330 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530331 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530332 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
333 item_name: item.item_name,
334 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530335 return item_tax_record[head[0]] ?
336 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530337 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530338 }).join("\n")
339 });
340 }).join("\n");
341
Anand Doshi53b73422013-05-31 11:50:01 +0530342 if(!rows) return "";
Anand Doshi3543f302013-05-24 19:25:01 +0530343 return '<div style="overflow-x: scroll;"><table class="table table-bordered table-hover">\
344 <thead><tr>' + headings + '</tr></thead> \
345 <tbody>' + rows + '</tbody> \
346 </table></div>';
347 },
348
Anand Doshi923d41d2013-05-28 17:23:36 +0530349 _validate_before_fetch: function(fieldname) {
350 var me = this;
351 if(!me.frm.doc[fieldname]) {
352 return (wn._("Please specify") + ": " +
353 wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
354 ". " + wn._("It is needed to fetch Item Details."));
355 }
356 return null;
357 },
358
Anand Doshi3543f302013-05-24 19:25:01 +0530359 validate_company_and_party: function(party_field) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530360 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530361 var valid = true;
Anand Doshi923d41d2013-05-28 17:23:36 +0530362 var msg = "";
Anand Doshi3543f302013-05-24 19:25:01 +0530363 $.each(["company", party_field], function(i, fieldname) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530364 var msg_for_fieldname = me._validate_before_fetch(fieldname);
365 if(msg_for_fieldname) {
366 msgprint(msg_for_fieldname);
Anand Doshi3543f302013-05-24 19:25:01 +0530367 valid = false;
Anand Doshi3543f302013-05-24 19:25:01 +0530368 }
369 });
370 return valid;
371 },
372
373 get_item_doclist: function() {
374 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
375 {parentfield: this.fname});
376 },
377
378 get_tax_doclist: function() {
379 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
380 {parentfield: this.other_fname});
381 },
382
383 validate_conversion_rate: function() {
384 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
385 var conversion_rate_label = wn.meta.get_label(this.frm.doc.doctype, "conversion_rate",
386 this.frm.doc.name);
387
388 if(this.frm.doc.conversion_rate == 0) {
389 wn.throw(wn._(conversion_rate_label) + " " + wn._("cannot be 0"));
390 }
391
392 var company_currency = this.get_company_currency();
Anand Doshi923d41d2013-05-28 17:23:36 +0530393 var valid_conversion_rate = this.frm.doc.conversion_rate ?
Anand Doshi3543f302013-05-24 19:25:01 +0530394 ((this.frm.doc.currency == company_currency && this.frm.doc.conversion_rate == 1.0) ||
395 (this.frm.doc.currency != company_currency && this.frm.doc.conversion_rate != 1.0)) :
396 false;
397
398 if(!valid_conversion_rate) {
399 wn.throw(wn._("Please enter valid") + " " + wn._(conversion_rate_label) +
400 " 1 " + this.frm.doc.currency + " = [?] " + company_currency);
401 }
402 },
403
404 calculate_taxes_and_totals: function() {
405 this.validate_conversion_rate();
406 this.frm.item_doclist = this.get_item_doclist();
407 this.frm.tax_doclist = this.get_tax_doclist();
408
409 this.calculate_item_values();
410 this.initialize_taxes();
411 this.determine_exclusive_rate && this.determine_exclusive_rate();
412 this.calculate_net_total();
413 this.calculate_taxes();
414 this.calculate_totals();
415 this._cleanup();
Anand Doshi923d41d2013-05-28 17:23:36 +0530416
Anand Doshi3543f302013-05-24 19:25:01 +0530417 this.show_item_wise_taxes();
418 },
419
420 initialize_taxes: function() {
421 var me = this;
422 $.each(this.frm.tax_doclist, function(i, tax) {
423 tax.item_wise_tax_detail = {};
424 $.each(["tax_amount", "total",
425 "tax_amount_for_current_item", "grand_total_for_current_item",
426 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
427 function(i, fieldname) { tax[fieldname] = 0.0 });
428
429 me.validate_on_previous_row(tax);
430 me.validate_inclusive_tax(tax);
431 wn.model.round_floats_in(tax);
432 });
433 },
434
435 calculate_taxes: function() {
436 var me = this;
437
438 $.each(this.frm.item_doclist, function(n, item) {
439 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
440
441 $.each(me.frm.tax_doclist, function(i, tax) {
442 // tax_amount represents the amount of tax for the current step
443 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530444
Anand Doshi3543f302013-05-24 19:25:01 +0530445 me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
446
447 // case when net total is 0 but there is an actual type charge
448 // in this case add the actual amount to tax.tax_amount
449 // and tax.grand_total_for_current_item for the first such iteration
450 if(tax.charge_type == "Actual" &&
451 !(current_tax_amount || me.frm.doc.net_total || tax.tax_amount)) {
452 var zero_net_total_adjustment = flt(tax.rate, precision("tax_amount", tax));
453 current_tax_amount += zero_net_total_adjustment;
454 }
455
456 // store tax_amount for current item as it will be used for
457 // charge type = 'On Previous Row Amount'
458 tax.tax_amount_for_current_item = current_tax_amount;
459
460 // accumulate tax amount into tax.tax_amount
461 tax.tax_amount += current_tax_amount;
462
Anand Doshi3543f302013-05-24 19:25:01 +0530463 // for buying
464 if(tax.category) {
465 // if just for valuation, do not add the tax amount in total
466 // hence, setting it as 0 for further steps
467 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
468
469 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
470 }
471
472 // Calculate tax.total viz. grand total till that step
473 // note: grand_total_for_current_item contains the contribution of
474 // item's amount, previously applied tax and the current tax on that item
475 if(i==0) {
476 tax.grand_total_for_current_item = flt(item.amount + current_tax_amount,
477 precision("total", tax));
478 } else {
479 tax.grand_total_for_current_item =
480 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
481 precision("total", tax));
482 }
483
484 // in tax.total, accumulate grand total for each item
485 tax.total += tax.grand_total_for_current_item;
486 });
487 });
488 },
489
490 get_current_tax_amount: function(item, tax, item_tax_map) {
491 var tax_rate = this._get_tax_rate(tax, item_tax_map);
492 var current_tax_amount = 0.0;
493
494 if(tax.charge_type == "Actual") {
495 // distribute the tax amount proportionally to each item row
496 var actual = flt(tax.rate, precision("tax_amount", tax));
497 current_tax_amount = this.frm.doc.net_total ?
498 ((item.amount / this.frm.doc.net_total) * actual) :
499 0.0;
500
501 } else if(tax.charge_type == "On Net Total") {
502 current_tax_amount = (tax_rate / 100.0) * item.amount;
503
504 } else if(tax.charge_type == "On Previous Row Amount") {
505 current_tax_amount = (tax_rate / 100.0) *
506 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
507
508 } else if(tax.charge_type == "On Previous Row Total") {
509 current_tax_amount = (tax_rate / 100.0) *
510 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
511
512 }
513
Anand Doshi53b73422013-05-31 11:50:01 +0530514 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
515
516 // store tax breakup for each item
517 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
518
519 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530520 },
521
522 _cleanup: function() {
523 $.each(this.frm.tax_doclist, function(i, tax) {
524 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
525 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
526 function(i, fieldname) { delete tax[fieldname]; });
527
528 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
529 });
530 },
Anand Doshifc777182013-05-27 19:29:07 +0530531
532 calculate_total_advance: function(parenttype, advance_parentfield) {
533 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
534 var advance_doclist = wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
535 {parentfield: advance_parentfield});
536 this.frm.doc.total_advance = flt(wn.utils.sum(
537 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
538 ), precision("total_advance"));
539
540 this.calculate_outstanding_amount();
541 }
542 },
Anand Doshi3543f302013-05-24 19:25:01 +0530543
544 _set_in_company_currency: function(item, print_field, base_field) {
545 // set values in base currency
546 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
547 precision(base_field, item));
548 },
Anand Doshibf3e54a2013-06-05 14:11:32 +0530549
550 get_terms: function() {
551 var me = this;
552 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530553 return this.frm.call({
Anand Doshibf3e54a2013-06-05 14:11:32 +0530554 method: "webnotes.client.get_value",
555 args: {
556 doctype: "Terms and Conditions",
557 fieldname: "terms",
558 filters: { name: this.frm.doc.tc_name },
559 },
560 });
561 }
562 },
Anand Doshi3543f302013-05-24 19:25:01 +0530563});