blob: 0ff957a820b8c5e2f320abbf7a0417c83926df71 [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() {
Anand Doshi1e4fb682013-08-23 12:56:33 +05309 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +053010 if(this.frm.doc.__islocal) {
Anand Doshi1e4fb682013-08-23 12:56:33 +053011 var today = get_today(),
Anand Doshi3543f302013-05-24 19:25:01 +053012 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 }
Anand Doshi1e4fb682013-08-23 12:56:33 +053033
34 if(this.other_fname) {
Anand Doshia91c95f2013-08-23 13:00:47 +053035 this[this.other_fname + "_remove"] = this.calculate_taxes_and_totals;
36 }
37
38 if(this.fname) {
39 this[this.fname + "_remove"] = this.calculate_taxes_and_totals;
Anand Doshi1e4fb682013-08-23 12:56:33 +053040 }
Anand Doshi3543f302013-05-24 19:25:01 +053041 },
42
Anand Doshi5e4e5d72013-08-06 17:23:44 +053043 onload_post_render: function() {
44 if(this.frm.doc.__islocal && this.frm.doc.company && !this.frm.doc.customer) {
45 var me = this;
46 return this.frm.call({
47 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();
Anand Doshi79251492013-08-09 00:20:34 +053053 me.set_dynamic_labels();
Anand Doshi5e4e5d72013-08-06 17:23:44 +053054 }
55 });
56 }
57 },
58
Anand Doshi3543f302013-05-24 19:25:01 +053059 refresh: function() {
60 this.frm.clear_custom_buttons();
61 erpnext.hide_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +053062 erpnext.hide_company();
Anand Doshi3543f302013-05-24 19:25:01 +053063 this.show_item_wise_taxes();
Nabin Hait1f996c32013-07-29 19:00:53 +053064 this.set_dynamic_labels();
Anand Doshi3543f302013-05-24 19:25:01 +053065 },
66
Anand Doshi923d41d2013-05-28 17:23:36 +053067 validate: function() {
68 this.calculate_taxes_and_totals();
69 },
70
Anand Doshi5e4e5d72013-08-06 17:23:44 +053071 set_default_values: function() {
72 $.each(wn.model.get_doclist(this.frm.doctype, this.frm.docname), function(i, doc) {
73 var updated = wn.model.set_default_values(doc);
74 if(doc.parentfield) {
75 refresh_field(doc.parentfield);
76 } else {
77 refresh_field(updated);
78 }
79 });
80 },
81
Anand Doshi3543f302013-05-24 19:25:01 +053082 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +053083 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Anand Doshi17350b82013-08-01 15:45:23 +053084 if(!this.frm.doc.currency) {
85 this.frm.set_value("currency", this.get_company_currency());
86 }
87
Nabin Hait48a0bd32013-07-23 15:31:39 +053088 this.frm.script_manager.trigger("currency");
Anand Doshi3543f302013-05-24 19:25:01 +053089 }
90 },
91
92 get_company_currency: function() {
93 return erpnext.get_currency(this.frm.doc.company);
94 },
95
96 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +053097 var me = this;
98 this.set_dynamic_labels();
Anand Doshid52b03a2013-06-21 12:22:52 +053099
Anand Doshi61a2f682013-06-21 17:55:31 +0530100 var company_currency = this.get_company_currency();
101 if(this.frm.doc.currency !== company_currency) {
102 this.get_exchange_rate(this.frm.doc.currency, company_currency,
103 function(exchange_rate) {
104 if(exchange_rate) {
105 me.frm.set_value("conversion_rate", exchange_rate);
106 me.conversion_rate();
107 }
108 });
109 } else {
110 this.conversion_rate();
111 }
112 },
113
114 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530115 if(this.frm.doc.currency === this.get_company_currency()) {
116 this.frm.set_value("conversion_rate", 1.0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530117 } else if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
118 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
119 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
120 }
121
122 this.calculate_taxes_and_totals();
Anand Doshi3543f302013-05-24 19:25:01 +0530123 },
124
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530125 get_price_list_currency: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +0530126 var me = this;
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530127 var fieldname = buying_or_selling.toLowerCase() + "_price_list";
128 if(this.frm.doc[fieldname]) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530129 return this.frm.call({
Anand Doshi3543f302013-05-24 19:25:01 +0530130 method: "setup.utils.get_price_list_currency",
Nabin Hait48a0bd32013-07-23 15:31:39 +0530131 args: {
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530132 price_list: this.frm.doc[fieldname],
Nabin Hait48a0bd32013-07-23 15:31:39 +0530133 },
Anand Doshi3543f302013-05-24 19:25:01 +0530134 callback: function(r) {
135 if(!r.exc) {
136 me.price_list_currency();
137 }
138 }
139 });
140 }
141 },
142
Anand Doshi61a2f682013-06-21 17:55:31 +0530143 get_exchange_rate: function(from_currency, to_currency, callback) {
144 var exchange_name = from_currency + "-" + to_currency;
145 wn.model.with_doc("Currency Exchange", exchange_name, function(name) {
146 var exchange_doc = wn.model.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530147 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530148 });
149 },
150
Anand Doshi3543f302013-05-24 19:25:01 +0530151 price_list_currency: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530152 this.set_dynamic_labels();
Anand Doshi61a2f682013-06-21 17:55:31 +0530153
154 var company_currency = this.get_company_currency();
155 if(this.frm.doc.price_list_currency !== company_currency) {
156 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
157 function(exchange_rate) {
158 if(exchange_rate) {
159 me.frm.set_value("price_list_currency", exchange_rate);
160 me.plc_conversion_rate();
161 }
162 });
163 } else {
164 this.plc_conversion_rate();
165 }
Anand Doshi3543f302013-05-24 19:25:01 +0530166 },
167
168 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530169 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
170 this.frm.set_value("plc_conversion_rate", 1.0);
171 } else if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
172 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
173 this.calculate_taxes_and_totals();
174 }
Anand Doshi3543f302013-05-24 19:25:01 +0530175 },
176
177 qty: function(doc, cdt, cdn) {
178 this.calculate_taxes_and_totals();
179 },
180
Anand Doshi923d41d2013-05-28 17:23:36 +0530181 tax_rate: function(doc, cdt, cdn) {
182 this.calculate_taxes_and_totals();
183 },
184
185 row_id: function(doc, cdt, cdn) {
186 var tax = wn.model.get_doc(cdt, cdn);
187 try {
188 this.validate_on_previous_row(tax);
189 this.calculate_taxes_and_totals();
190 } catch(e) {
191 tax.row_id = null;
192 refresh_field("row_id", tax.name, tax.parentfield);
193 throw e;
194 }
195 },
196
Anand Doshi61a2f682013-06-21 17:55:31 +0530197 set_dynamic_labels: function() {
198 // What TODO? should we make price list system non-mandatory?
199 this.frm.toggle_reqd("plc_conversion_rate",
200 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
201
202 var company_currency = this.get_company_currency();
203 this.change_form_labels(company_currency);
204 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530205 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530206 },
207
Anand Doshi923d41d2013-05-28 17:23:36 +0530208 recalculate: function() {
209 this.calculate_taxes_and_totals();
210 },
211
212 recalculate_values: function() {
213 this.calculate_taxes_and_totals();
214 },
215
Anand Doshid0b00722013-05-28 18:54:48 +0530216 calculate_charges: function() {
217 this.calculate_taxes_and_totals();
218 },
219
Anand Doshi3543f302013-05-24 19:25:01 +0530220 included_in_print_rate: function(doc, cdt, cdn) {
221 var tax = wn.model.get_doc(cdt, cdn);
222 try {
223 this.validate_on_previous_row(tax);
224 this.validate_inclusive_tax(tax);
225 this.calculate_taxes_and_totals();
226 } catch(e) {
227 tax.included_in_print_rate = 0;
228 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
229 throw e;
230 }
231 },
232
233 validate_on_previous_row: function(tax) {
234 // validate if a valid row id is mentioned in case of
235 // On Previous Row Amount and On Previous Row Total
236 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
237 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
238 var msg = repl(wn._("Row") + " # %(idx)s [%(doctype)s]: " +
239 wn._("Please specify a valid") + " %(row_id_label)s", {
240 idx: tax.idx,
241 doctype: tax.doctype,
242 row_id_label: wn.meta.get_label(tax.doctype, "row_id", tax.name)
243 });
244 msgprint(msg);
245 throw msg;
246 }
247 },
248
249 validate_inclusive_tax: function(tax) {
250 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
251
252 var actual_type_error = function() {
253 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
254 "%(charge_type_label)s = \"%(charge_type)s\" " +
255 wn._("cannot be included in Item's rate"), {
256 idx: tax.idx,
257 doctype: tax.doctype,
258 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
259 charge_type: tax.charge_type
260 });
261 msgprint(msg);
262 throw msg;
263 };
264
265 var on_previous_row_error = function(row_range) {
266 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
267 wn._("to be included in Item's rate, it is required that: ") +
268 " [" + wn._("Row") + " # %(row_range)s] " + wn._("also be included in Item's rate"), {
269 idx: tax.idx,
270 doctype: tax.doctype,
271 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
272 charge_type: tax.charge_type,
273 inclusive_label: wn.meta.get_label(tax.doctype, "included_in_print_rate", tax.name),
274 row_range: row_range,
275 });
276
277 msgprint(msg);
278 throw msg;
279 };
280
281 if(cint(tax.included_in_print_rate)) {
282 if(tax.charge_type == "Actual") {
283 // inclusive tax cannot be of type Actual
284 actual_type_error();
285 } else if(tax.charge_type == "On Previous Row Amount" &&
286 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
287 // referred row should also be an inclusive tax
288 on_previous_row_error(tax.row_id);
289 } else if(tax.charge_type == "On Previous Row Total") {
290 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
291 function(t) { return cint(t.included_in_print_rate) ? null : t; });
292 if(taxes_not_included.length > 0) {
293 // all rows above this tax should be inclusive
294 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
295 }
296 }
297 }
298 },
299
300 _load_item_tax_rate: function(item_tax_rate) {
301 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
302 },
303
304 _get_tax_rate: function(tax, item_tax_map) {
305 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
306 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
307 tax.rate;
308 },
309
310 get_item_wise_taxes_html: function() {
311 var item_tax = {};
312 var tax_accounts = [];
313 var company_currency = this.get_company_currency();
314
315 $.each(this.get_tax_doclist(), function(i, tax) {
316 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530317 var tax_rate_precision = precision("rate", tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530318 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530319 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530320 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530321 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530322 var tax_rate = "";
323 if(tax_data[0] != null) {
324 tax_rate = (tax.charge_type === "Actual") ?
325 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
326 (flt(tax_data[0], tax_rate_precision) + "%");
327 }
328 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
329 tax_amount_precision);
Anand Doshi53b73422013-05-31 11:50:01 +0530330
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530331 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530332 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530333 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530334 }
Anand Doshi3543f302013-05-24 19:25:01 +0530335 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530336 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530337 });
338
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530339 var headings = $.map([wn._("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530340 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Anand Doshi179e3772013-09-05 16:53:57 +0530341
342 var distinct_item_names = [];
343 var distinct_items = [];
344 $.each(this.get_item_doclist(), function(i, item) {
345 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
346 distinct_item_names.push(item.item_code || item.item_name);
347 distinct_items.push(item);
348 }
349 });
Anand Doshi3543f302013-05-24 19:25:01 +0530350
Anand Doshi179e3772013-09-05 16:53:57 +0530351 console.log(distinct_items);
352
353 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530354 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530355 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530356 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
357 item_name: item.item_name,
358 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530359 return item_tax_record[head[0]] ?
360 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530361 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530362 }).join("\n")
363 });
364 }).join("\n");
365
Anand Doshi53b73422013-05-31 11:50:01 +0530366 if(!rows) return "";
Anand Doshi3543f302013-05-24 19:25:01 +0530367 return '<div style="overflow-x: scroll;"><table class="table table-bordered table-hover">\
368 <thead><tr>' + headings + '</tr></thead> \
369 <tbody>' + rows + '</tbody> \
370 </table></div>';
371 },
372
Anand Doshi923d41d2013-05-28 17:23:36 +0530373 _validate_before_fetch: function(fieldname) {
374 var me = this;
375 if(!me.frm.doc[fieldname]) {
376 return (wn._("Please specify") + ": " +
377 wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
378 ". " + wn._("It is needed to fetch Item Details."));
379 }
380 return null;
381 },
382
Anand Doshi3543f302013-05-24 19:25:01 +0530383 validate_company_and_party: function(party_field) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530384 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530385 var valid = true;
Anand Doshi923d41d2013-05-28 17:23:36 +0530386 var msg = "";
Anand Doshi3543f302013-05-24 19:25:01 +0530387 $.each(["company", party_field], function(i, fieldname) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530388 var msg_for_fieldname = me._validate_before_fetch(fieldname);
389 if(msg_for_fieldname) {
390 msgprint(msg_for_fieldname);
Anand Doshi3543f302013-05-24 19:25:01 +0530391 valid = false;
Anand Doshi3543f302013-05-24 19:25:01 +0530392 }
393 });
394 return valid;
395 },
396
397 get_item_doclist: function() {
398 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
399 {parentfield: this.fname});
400 },
401
402 get_tax_doclist: function() {
403 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
404 {parentfield: this.other_fname});
405 },
406
407 validate_conversion_rate: function() {
408 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
409 var conversion_rate_label = wn.meta.get_label(this.frm.doc.doctype, "conversion_rate",
410 this.frm.doc.name);
411
412 if(this.frm.doc.conversion_rate == 0) {
413 wn.throw(wn._(conversion_rate_label) + " " + wn._("cannot be 0"));
414 }
415
416 var company_currency = this.get_company_currency();
Anand Doshi923d41d2013-05-28 17:23:36 +0530417 var valid_conversion_rate = this.frm.doc.conversion_rate ?
Anand Doshi3543f302013-05-24 19:25:01 +0530418 ((this.frm.doc.currency == company_currency && this.frm.doc.conversion_rate == 1.0) ||
419 (this.frm.doc.currency != company_currency && this.frm.doc.conversion_rate != 1.0)) :
420 false;
421
422 if(!valid_conversion_rate) {
423 wn.throw(wn._("Please enter valid") + " " + wn._(conversion_rate_label) +
424 " 1 " + this.frm.doc.currency + " = [?] " + company_currency);
425 }
426 },
427
428 calculate_taxes_and_totals: function() {
429 this.validate_conversion_rate();
430 this.frm.item_doclist = this.get_item_doclist();
431 this.frm.tax_doclist = this.get_tax_doclist();
432
433 this.calculate_item_values();
434 this.initialize_taxes();
435 this.determine_exclusive_rate && this.determine_exclusive_rate();
436 this.calculate_net_total();
437 this.calculate_taxes();
438 this.calculate_totals();
439 this._cleanup();
Anand Doshi923d41d2013-05-28 17:23:36 +0530440
Anand Doshi3543f302013-05-24 19:25:01 +0530441 this.show_item_wise_taxes();
442 },
443
444 initialize_taxes: function() {
445 var me = this;
446 $.each(this.frm.tax_doclist, function(i, tax) {
447 tax.item_wise_tax_detail = {};
448 $.each(["tax_amount", "total",
449 "tax_amount_for_current_item", "grand_total_for_current_item",
450 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
451 function(i, fieldname) { tax[fieldname] = 0.0 });
452
453 me.validate_on_previous_row(tax);
454 me.validate_inclusive_tax(tax);
455 wn.model.round_floats_in(tax);
456 });
457 },
458
459 calculate_taxes: function() {
460 var me = this;
461
462 $.each(this.frm.item_doclist, function(n, item) {
463 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
464
465 $.each(me.frm.tax_doclist, function(i, tax) {
466 // tax_amount represents the amount of tax for the current step
467 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530468
Anand Doshi3543f302013-05-24 19:25:01 +0530469 me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
470
471 // case when net total is 0 but there is an actual type charge
472 // in this case add the actual amount to tax.tax_amount
473 // and tax.grand_total_for_current_item for the first such iteration
474 if(tax.charge_type == "Actual" &&
475 !(current_tax_amount || me.frm.doc.net_total || tax.tax_amount)) {
476 var zero_net_total_adjustment = flt(tax.rate, precision("tax_amount", tax));
477 current_tax_amount += zero_net_total_adjustment;
478 }
479
480 // store tax_amount for current item as it will be used for
481 // charge type = 'On Previous Row Amount'
482 tax.tax_amount_for_current_item = current_tax_amount;
483
484 // accumulate tax amount into tax.tax_amount
485 tax.tax_amount += current_tax_amount;
486
Anand Doshi3543f302013-05-24 19:25:01 +0530487 // for buying
488 if(tax.category) {
489 // if just for valuation, do not add the tax amount in total
490 // hence, setting it as 0 for further steps
491 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
492
493 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
494 }
495
496 // Calculate tax.total viz. grand total till that step
497 // note: grand_total_for_current_item contains the contribution of
498 // item's amount, previously applied tax and the current tax on that item
499 if(i==0) {
500 tax.grand_total_for_current_item = flt(item.amount + current_tax_amount,
501 precision("total", tax));
502 } else {
503 tax.grand_total_for_current_item =
504 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
505 precision("total", tax));
506 }
507
508 // in tax.total, accumulate grand total for each item
509 tax.total += tax.grand_total_for_current_item;
510 });
511 });
512 },
513
514 get_current_tax_amount: function(item, tax, item_tax_map) {
515 var tax_rate = this._get_tax_rate(tax, item_tax_map);
516 var current_tax_amount = 0.0;
517
518 if(tax.charge_type == "Actual") {
519 // distribute the tax amount proportionally to each item row
520 var actual = flt(tax.rate, precision("tax_amount", tax));
521 current_tax_amount = this.frm.doc.net_total ?
522 ((item.amount / this.frm.doc.net_total) * actual) :
523 0.0;
524
525 } else if(tax.charge_type == "On Net Total") {
526 current_tax_amount = (tax_rate / 100.0) * item.amount;
527
528 } else if(tax.charge_type == "On Previous Row Amount") {
529 current_tax_amount = (tax_rate / 100.0) *
530 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
531
532 } else if(tax.charge_type == "On Previous Row Total") {
533 current_tax_amount = (tax_rate / 100.0) *
534 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
535
536 }
537
Anand Doshi53b73422013-05-31 11:50:01 +0530538 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
539
540 // store tax breakup for each item
541 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
542
543 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530544 },
545
546 _cleanup: function() {
547 $.each(this.frm.tax_doclist, function(i, tax) {
548 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
549 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
550 function(i, fieldname) { delete tax[fieldname]; });
551
552 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
553 });
554 },
Anand Doshifc777182013-05-27 19:29:07 +0530555
556 calculate_total_advance: function(parenttype, advance_parentfield) {
557 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
558 var advance_doclist = wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
559 {parentfield: advance_parentfield});
560 this.frm.doc.total_advance = flt(wn.utils.sum(
561 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
562 ), precision("total_advance"));
563
564 this.calculate_outstanding_amount();
565 }
566 },
Anand Doshi3543f302013-05-24 19:25:01 +0530567
568 _set_in_company_currency: function(item, print_field, base_field) {
569 // set values in base currency
570 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
571 precision(base_field, item));
572 },
Anand Doshibf3e54a2013-06-05 14:11:32 +0530573
574 get_terms: function() {
575 var me = this;
576 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530577 return this.frm.call({
Anand Doshibf3e54a2013-06-05 14:11:32 +0530578 method: "webnotes.client.get_value",
579 args: {
580 doctype: "Terms and Conditions",
581 fieldname: "terms",
582 filters: { name: this.frm.doc.tc_name },
583 },
584 });
585 }
586 },
Anand Doshi3543f302013-05-24 19:25:01 +0530587});