blob: 78f792d0185e4a067ecaa57fa61a1a71d1775d4c [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);
262 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
263 function(item_code, tax_amount) {
264 if(!item_tax[item_code]) item_tax[item_code] = {};
265 item_tax[item_code][tax.account_head] = flt(tax_amount, tax_amount_precision);
266 });
267 tax_accounts.push(tax.account_head);
268 });
269
270 var headings = $.map([wn._("Item Name")].concat(tax_accounts),
271 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
272
273 var rows = $.map(this.get_item_doclist(), function(item) {
274 var item_tax_record = item_tax[item.item_code || item.item_name];
275 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
276 item_name: item.item_name,
277 taxes: $.map(tax_accounts, function(head) {
278 return "<td>" + format_currency(item_tax_record[head], company_currency) + "</td>"
279 }).join("\n")
280 });
281 }).join("\n");
282
283 return '<div style="overflow-x: scroll;"><table class="table table-bordered table-hover">\
284 <thead><tr>' + headings + '</tr></thead> \
285 <tbody>' + rows + '</tbody> \
286 </table></div>';
287 },
288
289 set_default_values: function() {
290 $.each(wn.model.get_doclist(this.frm.doctype, this.frm.docname), function(i, doc) {
291 var updated = wn.model.set_default_values(doc);
292 if(doc.parentfield) {
293 refresh_field(doc.parentfield);
294 } else {
295 refresh_field(updated);
296 }
297 });
298 },
299
Anand Doshi923d41d2013-05-28 17:23:36 +0530300 _validate_before_fetch: function(fieldname) {
301 var me = this;
302 if(!me.frm.doc[fieldname]) {
303 return (wn._("Please specify") + ": " +
304 wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
305 ". " + wn._("It is needed to fetch Item Details."));
306 }
307 return null;
308 },
309
Anand Doshi3543f302013-05-24 19:25:01 +0530310 validate_company_and_party: function(party_field) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530311 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530312 var valid = true;
Anand Doshi923d41d2013-05-28 17:23:36 +0530313 var msg = "";
Anand Doshi3543f302013-05-24 19:25:01 +0530314 $.each(["company", party_field], function(i, fieldname) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530315 var msg_for_fieldname = me._validate_before_fetch(fieldname);
316 if(msg_for_fieldname) {
317 msgprint(msg_for_fieldname);
Anand Doshi3543f302013-05-24 19:25:01 +0530318 valid = false;
Anand Doshi3543f302013-05-24 19:25:01 +0530319 }
320 });
321 return valid;
322 },
323
324 get_item_doclist: function() {
325 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
326 {parentfield: this.fname});
327 },
328
329 get_tax_doclist: function() {
330 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
331 {parentfield: this.other_fname});
332 },
333
334 validate_conversion_rate: function() {
335 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
336 var conversion_rate_label = wn.meta.get_label(this.frm.doc.doctype, "conversion_rate",
337 this.frm.doc.name);
338
339 if(this.frm.doc.conversion_rate == 0) {
340 wn.throw(wn._(conversion_rate_label) + " " + wn._("cannot be 0"));
341 }
342
343 var company_currency = this.get_company_currency();
Anand Doshi923d41d2013-05-28 17:23:36 +0530344 var valid_conversion_rate = this.frm.doc.conversion_rate ?
Anand Doshi3543f302013-05-24 19:25:01 +0530345 ((this.frm.doc.currency == company_currency && this.frm.doc.conversion_rate == 1.0) ||
346 (this.frm.doc.currency != company_currency && this.frm.doc.conversion_rate != 1.0)) :
347 false;
348
349 if(!valid_conversion_rate) {
350 wn.throw(wn._("Please enter valid") + " " + wn._(conversion_rate_label) +
351 " 1 " + this.frm.doc.currency + " = [?] " + company_currency);
352 }
353 },
354
355 calculate_taxes_and_totals: function() {
356 this.validate_conversion_rate();
357 this.frm.item_doclist = this.get_item_doclist();
358 this.frm.tax_doclist = this.get_tax_doclist();
359
360 this.calculate_item_values();
361 this.initialize_taxes();
362 this.determine_exclusive_rate && this.determine_exclusive_rate();
363 this.calculate_net_total();
364 this.calculate_taxes();
365 this.calculate_totals();
366 this._cleanup();
Anand Doshi923d41d2013-05-28 17:23:36 +0530367
Anand Doshi3543f302013-05-24 19:25:01 +0530368 this.show_item_wise_taxes();
369 },
370
371 initialize_taxes: function() {
372 var me = this;
373 $.each(this.frm.tax_doclist, function(i, tax) {
374 tax.item_wise_tax_detail = {};
375 $.each(["tax_amount", "total",
376 "tax_amount_for_current_item", "grand_total_for_current_item",
377 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
378 function(i, fieldname) { tax[fieldname] = 0.0 });
379
380 me.validate_on_previous_row(tax);
381 me.validate_inclusive_tax(tax);
382 wn.model.round_floats_in(tax);
383 });
384 },
385
386 calculate_taxes: function() {
387 var me = this;
388
389 $.each(this.frm.item_doclist, function(n, item) {
390 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
391
392 $.each(me.frm.tax_doclist, function(i, tax) {
393 // tax_amount represents the amount of tax for the current step
394 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
395
396 me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
397
398 // case when net total is 0 but there is an actual type charge
399 // in this case add the actual amount to tax.tax_amount
400 // and tax.grand_total_for_current_item for the first such iteration
401 if(tax.charge_type == "Actual" &&
402 !(current_tax_amount || me.frm.doc.net_total || tax.tax_amount)) {
403 var zero_net_total_adjustment = flt(tax.rate, precision("tax_amount", tax));
404 current_tax_amount += zero_net_total_adjustment;
405 }
406
407 // store tax_amount for current item as it will be used for
408 // charge type = 'On Previous Row Amount'
409 tax.tax_amount_for_current_item = current_tax_amount;
410
411 // accumulate tax amount into tax.tax_amount
412 tax.tax_amount += current_tax_amount;
413
414 // store tax breakup for each item
415 tax.item_wise_tax_detail[item.item_code || item.item_name] = current_tax_amount;
416
417 // for buying
418 if(tax.category) {
419 // if just for valuation, do not add the tax amount in total
420 // hence, setting it as 0 for further steps
421 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
422
423 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
424 }
425
426 // Calculate tax.total viz. grand total till that step
427 // note: grand_total_for_current_item contains the contribution of
428 // item's amount, previously applied tax and the current tax on that item
429 if(i==0) {
430 tax.grand_total_for_current_item = flt(item.amount + current_tax_amount,
431 precision("total", tax));
432 } else {
433 tax.grand_total_for_current_item =
434 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
435 precision("total", tax));
436 }
437
438 // in tax.total, accumulate grand total for each item
439 tax.total += tax.grand_total_for_current_item;
440 });
441 });
442 },
443
444 get_current_tax_amount: function(item, tax, item_tax_map) {
445 var tax_rate = this._get_tax_rate(tax, item_tax_map);
446 var current_tax_amount = 0.0;
447
448 if(tax.charge_type == "Actual") {
449 // distribute the tax amount proportionally to each item row
450 var actual = flt(tax.rate, precision("tax_amount", tax));
451 current_tax_amount = this.frm.doc.net_total ?
452 ((item.amount / this.frm.doc.net_total) * actual) :
453 0.0;
454
455 } else if(tax.charge_type == "On Net Total") {
456 current_tax_amount = (tax_rate / 100.0) * item.amount;
457
458 } else if(tax.charge_type == "On Previous Row Amount") {
459 current_tax_amount = (tax_rate / 100.0) *
460 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
461
462 } else if(tax.charge_type == "On Previous Row Total") {
463 current_tax_amount = (tax_rate / 100.0) *
464 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
465
466 }
467
468 return flt(current_tax_amount, precision("tax_amount", tax));
469 },
470
471 _cleanup: function() {
472 $.each(this.frm.tax_doclist, function(i, tax) {
473 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
474 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
475 function(i, fieldname) { delete tax[fieldname]; });
476
477 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
478 });
479 },
Anand Doshifc777182013-05-27 19:29:07 +0530480
481 calculate_total_advance: function(parenttype, advance_parentfield) {
482 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
483 var advance_doclist = wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
484 {parentfield: advance_parentfield});
485 this.frm.doc.total_advance = flt(wn.utils.sum(
486 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
487 ), precision("total_advance"));
488
489 this.calculate_outstanding_amount();
490 }
491 },
Anand Doshi3543f302013-05-24 19:25:01 +0530492
493 _set_in_company_currency: function(item, print_field, base_field) {
494 // set values in base currency
495 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
496 precision(base_field, item));
497 },
498});