blob: b7df5b6876858b77317b2b89125af8c0d93c5565 [file] [log] [blame]
Anand Doshi3543f302013-05-24 19:25:01 +05301// ERPNext - web based ERP (http://erpnext.com)
2// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17wn.provide("erpnext");
18
19erpnext.TransactionController = wn.ui.form.Controller.extend({
20 onload: function() {
21 if(this.frm.doc.__islocal) {
22 var me = this,
23 today = get_today(),
24 currency = wn.defaults.get_default("currency");
25
26 $.each({
Anand Doshifc777182013-05-27 19:29:07 +053027 posting_date: today,
28 due_date: today,
29 transaction_date: today,
30 currency: currency,
31 price_list_currency: currency,
32 status: "Draft",
33 company: wn.defaults.get_default("company"),
34 fiscal_year: wn.defaults.get_default("fiscal_year"),
35 is_subcontracted: "No",
36 conversion_rate: 1.0,
37 plc_conversion_rate: 1.0
Anand Doshi3543f302013-05-24 19:25:01 +053038 }, function(fieldname, value) {
39 if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname])
40 me.frm.set_value(fieldname, value);
41 });
42 }
43 },
44
45 refresh: function() {
46 this.frm.clear_custom_buttons();
47 erpnext.hide_naming_series();
48 this.show_item_wise_taxes();
Anand Doshi923d41d2013-05-28 17:23:36 +053049 this.frm.fields_dict.currency ? this.currency() : this.set_dynamic_labels();
Anand Doshi3543f302013-05-24 19:25:01 +053050 },
51
52 onload_post_render: function() {
53 if(this.frm.doc.__islocal && this.frm.doc.company) {
54 var me = this;
55 this.frm.call({
56 doc: this.frm.doc,
57 method: "onload_post_render",
58 freeze: true,
59 callback: function(r) {
60 // remove this call when using client side mapper
61 me.set_default_values();
62 me.frm.refresh();
63 }
64 });
65 }
66 },
67
Anand Doshi923d41d2013-05-28 17:23:36 +053068 validate: function() {
69 this.calculate_taxes_and_totals();
70 },
71
Anand Doshi3543f302013-05-24 19:25:01 +053072 company: function() {
73 if(this.frm.doc.company) {
74 var me = this;
75 var company_currency = this.get_company_currency();
76 $.each(["currency", "price_list_currency"], function(i, fieldname) {
77 if(!me.doc[fieldname]) {
78 me.frm.set_value(fieldname, company_currency);
79 }
80 });
81 this.price_list_currency();
82 }
83 },
84
85 get_company_currency: function() {
86 return erpnext.get_currency(this.frm.doc.company);
87 },
88
89 currency: function() {
90 this.price_list_currency();
91 },
92
93 price_list_name: function(use_for) {
94 var me = this;
95 if(this.frm.doc.price_list_name) {
96 this.frm.call({
97 method: "setup.utils.get_price_list_currency",
98 args: {args: {
99 price_list_name: this.frm.doc.price_list_name,
100 use_for: use_for
101 }},
102 callback: function(r) {
103 if(!r.exc) {
104 me.price_list_currency();
105 }
106 }
107 });
108 }
109 },
110
111 price_list_currency: function() {
112 // What TODO? should we make price list system non-mandatory?
113 this.frm.toggle_reqd("plc_conversion_rate",
114 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
115
116 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
117 this.frm.set_value("plc_conversion_rate", 1.0);
118 } else if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
119 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
120 }
121 this.set_dynamic_labels();
122 },
123
124 plc_conversion_rate: function() {
125 this.price_list_currency();
126 },
127
128 conversion_rate: function() {
129 this.price_list_currency();
130 this.calculate_taxes_and_totals();
131 },
132
133 qty: function(doc, cdt, cdn) {
134 this.calculate_taxes_and_totals();
135 },
136
Anand Doshi923d41d2013-05-28 17:23:36 +0530137 tax_rate: function(doc, cdt, cdn) {
138 this.calculate_taxes_and_totals();
139 },
140
141 row_id: function(doc, cdt, cdn) {
142 var tax = wn.model.get_doc(cdt, cdn);
143 try {
144 this.validate_on_previous_row(tax);
145 this.calculate_taxes_and_totals();
146 } catch(e) {
147 tax.row_id = null;
148 refresh_field("row_id", tax.name, tax.parentfield);
149 throw e;
150 }
151 },
152
153 recalculate: function() {
154 this.calculate_taxes_and_totals();
155 },
156
157 recalculate_values: function() {
158 this.calculate_taxes_and_totals();
159 },
160
Anand Doshid0b00722013-05-28 18:54:48 +0530161 calculate_charges: function() {
162 this.calculate_taxes_and_totals();
163 },
164
Anand Doshi3543f302013-05-24 19:25:01 +0530165 included_in_print_rate: function(doc, cdt, cdn) {
166 var tax = wn.model.get_doc(cdt, cdn);
167 try {
168 this.validate_on_previous_row(tax);
169 this.validate_inclusive_tax(tax);
170 this.calculate_taxes_and_totals();
171 } catch(e) {
172 tax.included_in_print_rate = 0;
173 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
174 throw e;
175 }
176 },
177
178 validate_on_previous_row: function(tax) {
179 // validate if a valid row id is mentioned in case of
180 // On Previous Row Amount and On Previous Row Total
181 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
182 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
183 var msg = repl(wn._("Row") + " # %(idx)s [%(doctype)s]: " +
184 wn._("Please specify a valid") + " %(row_id_label)s", {
185 idx: tax.idx,
186 doctype: tax.doctype,
187 row_id_label: wn.meta.get_label(tax.doctype, "row_id", tax.name)
188 });
189 msgprint(msg);
190 throw msg;
191 }
192 },
193
194 validate_inclusive_tax: function(tax) {
195 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
196
197 var actual_type_error = function() {
198 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
199 "%(charge_type_label)s = \"%(charge_type)s\" " +
200 wn._("cannot be included in Item's rate"), {
201 idx: tax.idx,
202 doctype: tax.doctype,
203 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
204 charge_type: tax.charge_type
205 });
206 msgprint(msg);
207 throw msg;
208 };
209
210 var on_previous_row_error = function(row_range) {
211 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
212 wn._("to be included in Item's rate, it is required that: ") +
213 " [" + wn._("Row") + " # %(row_range)s] " + wn._("also be included in Item's rate"), {
214 idx: tax.idx,
215 doctype: tax.doctype,
216 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
217 charge_type: tax.charge_type,
218 inclusive_label: wn.meta.get_label(tax.doctype, "included_in_print_rate", tax.name),
219 row_range: row_range,
220 });
221
222 msgprint(msg);
223 throw msg;
224 };
225
226 if(cint(tax.included_in_print_rate)) {
227 if(tax.charge_type == "Actual") {
228 // inclusive tax cannot be of type Actual
229 actual_type_error();
230 } else if(tax.charge_type == "On Previous Row Amount" &&
231 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
232 // referred row should also be an inclusive tax
233 on_previous_row_error(tax.row_id);
234 } else if(tax.charge_type == "On Previous Row Total") {
235 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
236 function(t) { return cint(t.included_in_print_rate) ? null : t; });
237 if(taxes_not_included.length > 0) {
238 // all rows above this tax should be inclusive
239 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
240 }
241 }
242 }
243 },
244
245 _load_item_tax_rate: function(item_tax_rate) {
246 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
247 },
248
249 _get_tax_rate: function(tax, item_tax_map) {
250 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
251 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
252 tax.rate;
253 },
254
255 get_item_wise_taxes_html: function() {
256 var item_tax = {};
257 var tax_accounts = [];
258 var company_currency = this.get_company_currency();
259
260 $.each(this.get_tax_doclist(), function(i, tax) {
261 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530262 var tax_rate_precision = precision("rate", tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530263 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530264 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530265 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530266 if($.isArray(tax_data)) {
267 var tax_rate = tax_data[0] == null ? "" : (flt(tax_data[0], tax_rate_precision) + "%"),
268 tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency);
269
270 item_tax[item_code][tax.account_head] = [tax_rate, tax_amount];
271 } else {
272 item_tax[item_code][tax.account_head] = [flt(tax_data, tax_rate_precision) + "%", ""];
273 }
Anand Doshi3543f302013-05-24 19:25:01 +0530274 });
275 tax_accounts.push(tax.account_head);
276 });
277
278 var headings = $.map([wn._("Item Name")].concat(tax_accounts),
279 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
280
281 var rows = $.map(this.get_item_doclist(), function(item) {
282 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530283 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530284 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
285 item_name: item.item_name,
286 taxes: $.map(tax_accounts, function(head) {
Anand Doshi53b73422013-05-31 11:50:01 +0530287 return "<td>(" + item_tax_record[head][0] + ") " + item_tax_record[head][1] + "</td>"
Anand Doshi3543f302013-05-24 19:25:01 +0530288 }).join("\n")
289 });
290 }).join("\n");
291
Anand Doshi53b73422013-05-31 11:50:01 +0530292 if(!rows) return "";
Anand Doshi3543f302013-05-24 19:25:01 +0530293 return '<div style="overflow-x: scroll;"><table class="table table-bordered table-hover">\
294 <thead><tr>' + headings + '</tr></thead> \
295 <tbody>' + rows + '</tbody> \
296 </table></div>';
297 },
298
299 set_default_values: function() {
300 $.each(wn.model.get_doclist(this.frm.doctype, this.frm.docname), function(i, doc) {
301 var updated = wn.model.set_default_values(doc);
302 if(doc.parentfield) {
303 refresh_field(doc.parentfield);
304 } else {
305 refresh_field(updated);
306 }
307 });
308 },
309
Anand Doshi923d41d2013-05-28 17:23:36 +0530310 _validate_before_fetch: function(fieldname) {
311 var me = this;
312 if(!me.frm.doc[fieldname]) {
313 return (wn._("Please specify") + ": " +
314 wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
315 ". " + wn._("It is needed to fetch Item Details."));
316 }
317 return null;
318 },
319
Anand Doshi3543f302013-05-24 19:25:01 +0530320 validate_company_and_party: function(party_field) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530321 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530322 var valid = true;
Anand Doshi923d41d2013-05-28 17:23:36 +0530323 var msg = "";
Anand Doshi3543f302013-05-24 19:25:01 +0530324 $.each(["company", party_field], function(i, fieldname) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530325 var msg_for_fieldname = me._validate_before_fetch(fieldname);
326 if(msg_for_fieldname) {
327 msgprint(msg_for_fieldname);
Anand Doshi3543f302013-05-24 19:25:01 +0530328 valid = false;
Anand Doshi3543f302013-05-24 19:25:01 +0530329 }
330 });
331 return valid;
332 },
333
334 get_item_doclist: function() {
335 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
336 {parentfield: this.fname});
337 },
338
339 get_tax_doclist: function() {
340 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
341 {parentfield: this.other_fname});
342 },
343
344 validate_conversion_rate: function() {
345 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
346 var conversion_rate_label = wn.meta.get_label(this.frm.doc.doctype, "conversion_rate",
347 this.frm.doc.name);
348
349 if(this.frm.doc.conversion_rate == 0) {
350 wn.throw(wn._(conversion_rate_label) + " " + wn._("cannot be 0"));
351 }
352
353 var company_currency = this.get_company_currency();
Anand Doshi923d41d2013-05-28 17:23:36 +0530354 var valid_conversion_rate = this.frm.doc.conversion_rate ?
Anand Doshi3543f302013-05-24 19:25:01 +0530355 ((this.frm.doc.currency == company_currency && this.frm.doc.conversion_rate == 1.0) ||
356 (this.frm.doc.currency != company_currency && this.frm.doc.conversion_rate != 1.0)) :
357 false;
358
359 if(!valid_conversion_rate) {
360 wn.throw(wn._("Please enter valid") + " " + wn._(conversion_rate_label) +
361 " 1 " + this.frm.doc.currency + " = [?] " + company_currency);
362 }
363 },
364
365 calculate_taxes_and_totals: function() {
366 this.validate_conversion_rate();
367 this.frm.item_doclist = this.get_item_doclist();
368 this.frm.tax_doclist = this.get_tax_doclist();
369
370 this.calculate_item_values();
371 this.initialize_taxes();
372 this.determine_exclusive_rate && this.determine_exclusive_rate();
373 this.calculate_net_total();
374 this.calculate_taxes();
375 this.calculate_totals();
376 this._cleanup();
Anand Doshi923d41d2013-05-28 17:23:36 +0530377
Anand Doshi3543f302013-05-24 19:25:01 +0530378 this.show_item_wise_taxes();
379 },
380
381 initialize_taxes: function() {
382 var me = this;
383 $.each(this.frm.tax_doclist, function(i, tax) {
384 tax.item_wise_tax_detail = {};
385 $.each(["tax_amount", "total",
386 "tax_amount_for_current_item", "grand_total_for_current_item",
387 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
388 function(i, fieldname) { tax[fieldname] = 0.0 });
389
390 me.validate_on_previous_row(tax);
391 me.validate_inclusive_tax(tax);
392 wn.model.round_floats_in(tax);
393 });
394 },
395
396 calculate_taxes: function() {
397 var me = this;
398
399 $.each(this.frm.item_doclist, function(n, item) {
400 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
401
402 $.each(me.frm.tax_doclist, function(i, tax) {
403 // tax_amount represents the amount of tax for the current step
404 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530405
Anand Doshi3543f302013-05-24 19:25:01 +0530406 me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
407
408 // case when net total is 0 but there is an actual type charge
409 // in this case add the actual amount to tax.tax_amount
410 // and tax.grand_total_for_current_item for the first such iteration
411 if(tax.charge_type == "Actual" &&
412 !(current_tax_amount || me.frm.doc.net_total || tax.tax_amount)) {
413 var zero_net_total_adjustment = flt(tax.rate, precision("tax_amount", tax));
414 current_tax_amount += zero_net_total_adjustment;
415 }
416
417 // store tax_amount for current item as it will be used for
418 // charge type = 'On Previous Row Amount'
419 tax.tax_amount_for_current_item = current_tax_amount;
420
421 // accumulate tax amount into tax.tax_amount
422 tax.tax_amount += current_tax_amount;
423
Anand Doshi3543f302013-05-24 19:25:01 +0530424 // for buying
425 if(tax.category) {
426 // if just for valuation, do not add the tax amount in total
427 // hence, setting it as 0 for further steps
428 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
429
430 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
431 }
432
433 // Calculate tax.total viz. grand total till that step
434 // note: grand_total_for_current_item contains the contribution of
435 // item's amount, previously applied tax and the current tax on that item
436 if(i==0) {
437 tax.grand_total_for_current_item = flt(item.amount + current_tax_amount,
438 precision("total", tax));
439 } else {
440 tax.grand_total_for_current_item =
441 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
442 precision("total", tax));
443 }
444
445 // in tax.total, accumulate grand total for each item
446 tax.total += tax.grand_total_for_current_item;
447 });
448 });
449 },
450
451 get_current_tax_amount: function(item, tax, item_tax_map) {
452 var tax_rate = this._get_tax_rate(tax, item_tax_map);
453 var current_tax_amount = 0.0;
454
455 if(tax.charge_type == "Actual") {
456 // distribute the tax amount proportionally to each item row
457 var actual = flt(tax.rate, precision("tax_amount", tax));
458 current_tax_amount = this.frm.doc.net_total ?
459 ((item.amount / this.frm.doc.net_total) * actual) :
460 0.0;
461
462 } else if(tax.charge_type == "On Net Total") {
463 current_tax_amount = (tax_rate / 100.0) * item.amount;
464
465 } else if(tax.charge_type == "On Previous Row Amount") {
466 current_tax_amount = (tax_rate / 100.0) *
467 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
468
469 } else if(tax.charge_type == "On Previous Row Total") {
470 current_tax_amount = (tax_rate / 100.0) *
471 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
472
473 }
474
Anand Doshi53b73422013-05-31 11:50:01 +0530475 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
476
477 // store tax breakup for each item
478 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
479
480 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530481 },
482
483 _cleanup: function() {
484 $.each(this.frm.tax_doclist, function(i, tax) {
485 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
486 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
487 function(i, fieldname) { delete tax[fieldname]; });
488
489 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
490 });
491 },
Anand Doshifc777182013-05-27 19:29:07 +0530492
493 calculate_total_advance: function(parenttype, advance_parentfield) {
494 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
495 var advance_doclist = wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
496 {parentfield: advance_parentfield});
497 this.frm.doc.total_advance = flt(wn.utils.sum(
498 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
499 ), precision("total_advance"));
500
501 this.calculate_outstanding_amount();
502 }
503 },
Anand Doshi3543f302013-05-24 19:25:01 +0530504
505 _set_in_company_currency: function(item, print_field, base_field) {
506 // set values in base currency
507 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
508 precision(base_field, item));
509 },
Anand Doshibf3e54a2013-06-05 14:11:32 +0530510
511 get_terms: function() {
512 var me = this;
513 if(this.frm.doc.tc_name) {
514 this.frm.call({
515 method: "webnotes.client.get_value",
516 args: {
517 doctype: "Terms and Conditions",
518 fieldname: "terms",
519 filters: { name: this.frm.doc.tc_name },
520 },
521 });
522 }
523 },
Anand Doshi3543f302013-05-24 19:25:01 +0530524});