blob: 9df014785143abce9c2ae371c0e1cb69f5fec122 [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() {
Anand Doshid52b03a2013-06-21 12:22:52 +053073 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Anand Doshi3543f302013-05-24 19:25:01 +053074 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() {
Anand Doshid52b03a2013-06-21 12:22:52 +053090 if(this.frm.doc.currency === this.get_company_currency())
91 this.frm.set_value("conversion_rate", 1.0);
92
Anand Doshi3543f302013-05-24 19:25:01 +053093 this.price_list_currency();
94 },
95
Anand Doshi060d9242013-06-12 17:40:36 +053096 price_list_name: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +053097 var me = this;
98 if(this.frm.doc.price_list_name) {
99 this.frm.call({
100 method: "setup.utils.get_price_list_currency",
101 args: {args: {
102 price_list_name: this.frm.doc.price_list_name,
Anand Doshi060d9242013-06-12 17:40:36 +0530103 buying_or_selling: buying_or_selling
Anand Doshi3543f302013-05-24 19:25:01 +0530104 }},
105 callback: function(r) {
106 if(!r.exc) {
107 me.price_list_currency();
108 }
109 }
110 });
111 }
112 },
113
114 price_list_currency: function() {
115 // What TODO? should we make price list system non-mandatory?
116 this.frm.toggle_reqd("plc_conversion_rate",
117 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
118
119 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
120 this.frm.set_value("plc_conversion_rate", 1.0);
121 } else if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
122 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
123 }
124 this.set_dynamic_labels();
125 },
126
127 plc_conversion_rate: function() {
128 this.price_list_currency();
129 },
130
131 conversion_rate: function() {
132 this.price_list_currency();
133 this.calculate_taxes_and_totals();
134 },
135
136 qty: function(doc, cdt, cdn) {
137 this.calculate_taxes_and_totals();
138 },
139
Anand Doshi923d41d2013-05-28 17:23:36 +0530140 tax_rate: function(doc, cdt, cdn) {
141 this.calculate_taxes_and_totals();
142 },
143
144 row_id: function(doc, cdt, cdn) {
145 var tax = wn.model.get_doc(cdt, cdn);
146 try {
147 this.validate_on_previous_row(tax);
148 this.calculate_taxes_and_totals();
149 } catch(e) {
150 tax.row_id = null;
151 refresh_field("row_id", tax.name, tax.parentfield);
152 throw e;
153 }
154 },
155
156 recalculate: function() {
157 this.calculate_taxes_and_totals();
158 },
159
160 recalculate_values: function() {
161 this.calculate_taxes_and_totals();
162 },
163
Anand Doshid0b00722013-05-28 18:54:48 +0530164 calculate_charges: function() {
165 this.calculate_taxes_and_totals();
166 },
167
Anand Doshi3543f302013-05-24 19:25:01 +0530168 included_in_print_rate: function(doc, cdt, cdn) {
169 var tax = wn.model.get_doc(cdt, cdn);
170 try {
171 this.validate_on_previous_row(tax);
172 this.validate_inclusive_tax(tax);
173 this.calculate_taxes_and_totals();
174 } catch(e) {
175 tax.included_in_print_rate = 0;
176 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
177 throw e;
178 }
179 },
180
181 validate_on_previous_row: function(tax) {
182 // validate if a valid row id is mentioned in case of
183 // On Previous Row Amount and On Previous Row Total
184 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
185 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
186 var msg = repl(wn._("Row") + " # %(idx)s [%(doctype)s]: " +
187 wn._("Please specify a valid") + " %(row_id_label)s", {
188 idx: tax.idx,
189 doctype: tax.doctype,
190 row_id_label: wn.meta.get_label(tax.doctype, "row_id", tax.name)
191 });
192 msgprint(msg);
193 throw msg;
194 }
195 },
196
197 validate_inclusive_tax: function(tax) {
198 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
199
200 var actual_type_error = function() {
201 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
202 "%(charge_type_label)s = \"%(charge_type)s\" " +
203 wn._("cannot be included in Item's rate"), {
204 idx: tax.idx,
205 doctype: tax.doctype,
206 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
207 charge_type: tax.charge_type
208 });
209 msgprint(msg);
210 throw msg;
211 };
212
213 var on_previous_row_error = function(row_range) {
214 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
215 wn._("to be included in Item's rate, it is required that: ") +
216 " [" + wn._("Row") + " # %(row_range)s] " + wn._("also be included in Item's rate"), {
217 idx: tax.idx,
218 doctype: tax.doctype,
219 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
220 charge_type: tax.charge_type,
221 inclusive_label: wn.meta.get_label(tax.doctype, "included_in_print_rate", tax.name),
222 row_range: row_range,
223 });
224
225 msgprint(msg);
226 throw msg;
227 };
228
229 if(cint(tax.included_in_print_rate)) {
230 if(tax.charge_type == "Actual") {
231 // inclusive tax cannot be of type Actual
232 actual_type_error();
233 } else if(tax.charge_type == "On Previous Row Amount" &&
234 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
235 // referred row should also be an inclusive tax
236 on_previous_row_error(tax.row_id);
237 } else if(tax.charge_type == "On Previous Row Total") {
238 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
239 function(t) { return cint(t.included_in_print_rate) ? null : t; });
240 if(taxes_not_included.length > 0) {
241 // all rows above this tax should be inclusive
242 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
243 }
244 }
245 }
246 },
247
248 _load_item_tax_rate: function(item_tax_rate) {
249 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
250 },
251
252 _get_tax_rate: function(tax, item_tax_map) {
253 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
254 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
255 tax.rate;
256 },
257
258 get_item_wise_taxes_html: function() {
259 var item_tax = {};
260 var tax_accounts = [];
261 var company_currency = this.get_company_currency();
262
263 $.each(this.get_tax_doclist(), function(i, tax) {
264 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530265 var tax_rate_precision = precision("rate", tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530266 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530267 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530268 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530269 if($.isArray(tax_data)) {
270 var tax_rate = tax_data[0] == null ? "" : (flt(tax_data[0], tax_rate_precision) + "%"),
271 tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency);
272
273 item_tax[item_code][tax.account_head] = [tax_rate, tax_amount];
274 } else {
275 item_tax[item_code][tax.account_head] = [flt(tax_data, tax_rate_precision) + "%", ""];
276 }
Anand Doshi3543f302013-05-24 19:25:01 +0530277 });
278 tax_accounts.push(tax.account_head);
279 });
280
281 var headings = $.map([wn._("Item Name")].concat(tax_accounts),
282 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
283
284 var rows = $.map(this.get_item_doclist(), function(item) {
285 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530286 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530287 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
288 item_name: item.item_name,
289 taxes: $.map(tax_accounts, function(head) {
Anand Doshi53b73422013-05-31 11:50:01 +0530290 return "<td>(" + item_tax_record[head][0] + ") " + item_tax_record[head][1] + "</td>"
Anand Doshi3543f302013-05-24 19:25:01 +0530291 }).join("\n")
292 });
293 }).join("\n");
294
Anand Doshi53b73422013-05-31 11:50:01 +0530295 if(!rows) return "";
Anand Doshi3543f302013-05-24 19:25:01 +0530296 return '<div style="overflow-x: scroll;"><table class="table table-bordered table-hover">\
297 <thead><tr>' + headings + '</tr></thead> \
298 <tbody>' + rows + '</tbody> \
299 </table></div>';
300 },
301
302 set_default_values: function() {
303 $.each(wn.model.get_doclist(this.frm.doctype, this.frm.docname), function(i, doc) {
304 var updated = wn.model.set_default_values(doc);
305 if(doc.parentfield) {
306 refresh_field(doc.parentfield);
307 } else {
308 refresh_field(updated);
309 }
310 });
311 },
312
Anand Doshi923d41d2013-05-28 17:23:36 +0530313 _validate_before_fetch: function(fieldname) {
314 var me = this;
315 if(!me.frm.doc[fieldname]) {
316 return (wn._("Please specify") + ": " +
317 wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
318 ". " + wn._("It is needed to fetch Item Details."));
319 }
320 return null;
321 },
322
Anand Doshi3543f302013-05-24 19:25:01 +0530323 validate_company_and_party: function(party_field) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530324 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530325 var valid = true;
Anand Doshi923d41d2013-05-28 17:23:36 +0530326 var msg = "";
Anand Doshi3543f302013-05-24 19:25:01 +0530327 $.each(["company", party_field], function(i, fieldname) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530328 var msg_for_fieldname = me._validate_before_fetch(fieldname);
329 if(msg_for_fieldname) {
330 msgprint(msg_for_fieldname);
Anand Doshi3543f302013-05-24 19:25:01 +0530331 valid = false;
Anand Doshi3543f302013-05-24 19:25:01 +0530332 }
333 });
334 return valid;
335 },
336
337 get_item_doclist: function() {
338 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
339 {parentfield: this.fname});
340 },
341
342 get_tax_doclist: function() {
343 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
344 {parentfield: this.other_fname});
345 },
346
347 validate_conversion_rate: function() {
348 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
349 var conversion_rate_label = wn.meta.get_label(this.frm.doc.doctype, "conversion_rate",
350 this.frm.doc.name);
351
352 if(this.frm.doc.conversion_rate == 0) {
353 wn.throw(wn._(conversion_rate_label) + " " + wn._("cannot be 0"));
354 }
355
356 var company_currency = this.get_company_currency();
Anand Doshi923d41d2013-05-28 17:23:36 +0530357 var valid_conversion_rate = this.frm.doc.conversion_rate ?
Anand Doshi3543f302013-05-24 19:25:01 +0530358 ((this.frm.doc.currency == company_currency && this.frm.doc.conversion_rate == 1.0) ||
359 (this.frm.doc.currency != company_currency && this.frm.doc.conversion_rate != 1.0)) :
360 false;
361
362 if(!valid_conversion_rate) {
363 wn.throw(wn._("Please enter valid") + " " + wn._(conversion_rate_label) +
364 " 1 " + this.frm.doc.currency + " = [?] " + company_currency);
365 }
366 },
367
368 calculate_taxes_and_totals: function() {
369 this.validate_conversion_rate();
370 this.frm.item_doclist = this.get_item_doclist();
371 this.frm.tax_doclist = this.get_tax_doclist();
372
373 this.calculate_item_values();
374 this.initialize_taxes();
375 this.determine_exclusive_rate && this.determine_exclusive_rate();
376 this.calculate_net_total();
377 this.calculate_taxes();
378 this.calculate_totals();
379 this._cleanup();
Anand Doshi923d41d2013-05-28 17:23:36 +0530380
Anand Doshi3543f302013-05-24 19:25:01 +0530381 this.show_item_wise_taxes();
382 },
383
384 initialize_taxes: function() {
385 var me = this;
386 $.each(this.frm.tax_doclist, function(i, tax) {
387 tax.item_wise_tax_detail = {};
388 $.each(["tax_amount", "total",
389 "tax_amount_for_current_item", "grand_total_for_current_item",
390 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
391 function(i, fieldname) { tax[fieldname] = 0.0 });
392
393 me.validate_on_previous_row(tax);
394 me.validate_inclusive_tax(tax);
395 wn.model.round_floats_in(tax);
396 });
397 },
398
399 calculate_taxes: function() {
400 var me = this;
401
402 $.each(this.frm.item_doclist, function(n, item) {
403 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
404
405 $.each(me.frm.tax_doclist, function(i, tax) {
406 // tax_amount represents the amount of tax for the current step
407 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530408
Anand Doshi3543f302013-05-24 19:25:01 +0530409 me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
410
411 // case when net total is 0 but there is an actual type charge
412 // in this case add the actual amount to tax.tax_amount
413 // and tax.grand_total_for_current_item for the first such iteration
414 if(tax.charge_type == "Actual" &&
415 !(current_tax_amount || me.frm.doc.net_total || tax.tax_amount)) {
416 var zero_net_total_adjustment = flt(tax.rate, precision("tax_amount", tax));
417 current_tax_amount += zero_net_total_adjustment;
418 }
419
420 // store tax_amount for current item as it will be used for
421 // charge type = 'On Previous Row Amount'
422 tax.tax_amount_for_current_item = current_tax_amount;
423
424 // accumulate tax amount into tax.tax_amount
425 tax.tax_amount += current_tax_amount;
426
Anand Doshi3543f302013-05-24 19:25:01 +0530427 // for buying
428 if(tax.category) {
429 // if just for valuation, do not add the tax amount in total
430 // hence, setting it as 0 for further steps
431 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
432
433 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
434 }
435
436 // Calculate tax.total viz. grand total till that step
437 // note: grand_total_for_current_item contains the contribution of
438 // item's amount, previously applied tax and the current tax on that item
439 if(i==0) {
440 tax.grand_total_for_current_item = flt(item.amount + current_tax_amount,
441 precision("total", tax));
442 } else {
443 tax.grand_total_for_current_item =
444 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
445 precision("total", tax));
446 }
447
448 // in tax.total, accumulate grand total for each item
449 tax.total += tax.grand_total_for_current_item;
450 });
451 });
452 },
453
454 get_current_tax_amount: function(item, tax, item_tax_map) {
455 var tax_rate = this._get_tax_rate(tax, item_tax_map);
456 var current_tax_amount = 0.0;
457
458 if(tax.charge_type == "Actual") {
459 // distribute the tax amount proportionally to each item row
460 var actual = flt(tax.rate, precision("tax_amount", tax));
461 current_tax_amount = this.frm.doc.net_total ?
462 ((item.amount / this.frm.doc.net_total) * actual) :
463 0.0;
464
465 } else if(tax.charge_type == "On Net Total") {
466 current_tax_amount = (tax_rate / 100.0) * item.amount;
467
468 } else if(tax.charge_type == "On Previous Row Amount") {
469 current_tax_amount = (tax_rate / 100.0) *
470 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
471
472 } else if(tax.charge_type == "On Previous Row Total") {
473 current_tax_amount = (tax_rate / 100.0) *
474 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
475
476 }
477
Anand Doshi53b73422013-05-31 11:50:01 +0530478 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
479
480 // store tax breakup for each item
481 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
482
483 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530484 },
485
486 _cleanup: function() {
487 $.each(this.frm.tax_doclist, function(i, tax) {
488 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
489 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
490 function(i, fieldname) { delete tax[fieldname]; });
491
492 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
493 });
494 },
Anand Doshifc777182013-05-27 19:29:07 +0530495
496 calculate_total_advance: function(parenttype, advance_parentfield) {
497 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
498 var advance_doclist = wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
499 {parentfield: advance_parentfield});
500 this.frm.doc.total_advance = flt(wn.utils.sum(
501 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
502 ), precision("total_advance"));
503
504 this.calculate_outstanding_amount();
505 }
506 },
Anand Doshi3543f302013-05-24 19:25:01 +0530507
508 _set_in_company_currency: function(item, print_field, base_field) {
509 // set values in base currency
510 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
511 precision(base_field, item));
512 },
Anand Doshibf3e54a2013-06-05 14:11:32 +0530513
514 get_terms: function() {
515 var me = this;
516 if(this.frm.doc.tc_name) {
517 this.frm.call({
518 method: "webnotes.client.get_value",
519 args: {
520 doctype: "Terms and Conditions",
521 fieldname: "terms",
522 filters: { name: this.frm.doc.tc_name },
523 },
524 });
525 }
526 },
Anand Doshi3543f302013-05-24 19:25:01 +0530527});