blob: a38e3a777cc5e6f5cbcc21f1a3bf92da194d4a61 [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();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053065
66 // Show POS button only if it is enabled from features setup
67 if(cint(sys_defaults.fs_pos_view)===1 && this.frm.doctype!="Material Request")
68 this.pos_btn();
69 },
70
71 pos_btn: function() {
72 if(this.$pos_btn)
73 this.$pos_btn.remove();
74
75 if(!this.pos_active) {
76 var btn_label = wn._("POS View"),
77 icon = "icon-desktop";
78 } else {
79 var btn_label = wn._(this.frm.doctype) + wn._(" View"),
80 icon = "icon-file-text";
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053081 }
82 var me = this;
83
84 this.$pos_btn = this.frm.add_custom_button(btn_label, function() {
85 me.toggle_pos();
86 me.pos_btn();
87 }, icon);
88 },
89
90 toggle_pos: function(show) {
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053091 // Check whether it is Selling or Buying cycle
92 var price_list = wn.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
93 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list;
94
95 if (!price_list)
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053096 msgprint(wn._("Please select Price List"))
97 else {
98 if((show===true && this.pos_active) || (show===false && !this.pos_active)) return;
99
100 // make pos
101 if(!this.pos) {
102 this.frm.layout.add_view("pos");
103 this.frm.pos = new erpnext.POS(this.frm.layout.views.pos, this.frm);
104 }
105
106 // toggle view
107 this.frm.layout.set_view(this.pos_active ? "" : "pos");
108 this.pos_active = !this.pos_active;
109
110 // refresh
111 if(this.pos_active)
112 this.frm.pos.refresh();
Akhilesh Darjeef83576b2013-09-18 18:35:12 +0530113 this.frm.refresh();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530114 }
Anand Doshi3543f302013-05-24 19:25:01 +0530115 },
116
Anand Doshi923d41d2013-05-28 17:23:36 +0530117 validate: function() {
118 this.calculate_taxes_and_totals();
119 },
120
Anand Doshi5e4e5d72013-08-06 17:23:44 +0530121 set_default_values: function() {
122 $.each(wn.model.get_doclist(this.frm.doctype, this.frm.docname), function(i, doc) {
123 var updated = wn.model.set_default_values(doc);
124 if(doc.parentfield) {
125 refresh_field(doc.parentfield);
126 } else {
127 refresh_field(updated);
128 }
129 });
130 },
131
Anand Doshi3543f302013-05-24 19:25:01 +0530132 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +0530133 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Anand Doshi17350b82013-08-01 15:45:23 +0530134 if(!this.frm.doc.currency) {
135 this.frm.set_value("currency", this.get_company_currency());
136 }
137
Nabin Hait48a0bd32013-07-23 15:31:39 +0530138 this.frm.script_manager.trigger("currency");
Anand Doshi3543f302013-05-24 19:25:01 +0530139 }
140 },
141
142 get_company_currency: function() {
143 return erpnext.get_currency(this.frm.doc.company);
144 },
145
146 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530147 var me = this;
148 this.set_dynamic_labels();
Anand Doshid52b03a2013-06-21 12:22:52 +0530149
Anand Doshi61a2f682013-06-21 17:55:31 +0530150 var company_currency = this.get_company_currency();
151 if(this.frm.doc.currency !== company_currency) {
152 this.get_exchange_rate(this.frm.doc.currency, company_currency,
153 function(exchange_rate) {
154 if(exchange_rate) {
155 me.frm.set_value("conversion_rate", exchange_rate);
156 me.conversion_rate();
157 }
158 });
159 } else {
160 this.conversion_rate();
161 }
162 },
163
164 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530165 if(this.frm.doc.currency === this.get_company_currency()) {
166 this.frm.set_value("conversion_rate", 1.0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530167 } else if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
168 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
169 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
170 }
171
172 this.calculate_taxes_and_totals();
Anand Doshi3543f302013-05-24 19:25:01 +0530173 },
174
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530175 get_price_list_currency: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +0530176 var me = this;
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530177 var fieldname = buying_or_selling.toLowerCase() + "_price_list";
178 if(this.frm.doc[fieldname]) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530179 return this.frm.call({
Anand Doshi3543f302013-05-24 19:25:01 +0530180 method: "setup.utils.get_price_list_currency",
Nabin Hait48a0bd32013-07-23 15:31:39 +0530181 args: {
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530182 price_list: this.frm.doc[fieldname],
Nabin Hait48a0bd32013-07-23 15:31:39 +0530183 },
Anand Doshi3543f302013-05-24 19:25:01 +0530184 callback: function(r) {
185 if(!r.exc) {
186 me.price_list_currency();
187 }
188 }
189 });
190 }
191 },
192
Anand Doshi61a2f682013-06-21 17:55:31 +0530193 get_exchange_rate: function(from_currency, to_currency, callback) {
194 var exchange_name = from_currency + "-" + to_currency;
195 wn.model.with_doc("Currency Exchange", exchange_name, function(name) {
196 var exchange_doc = wn.model.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530197 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530198 });
199 },
200
Anand Doshi3543f302013-05-24 19:25:01 +0530201 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530202 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530203 this.set_dynamic_labels();
Anand Doshi61a2f682013-06-21 17:55:31 +0530204
205 var company_currency = this.get_company_currency();
206 if(this.frm.doc.price_list_currency !== company_currency) {
207 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
208 function(exchange_rate) {
209 if(exchange_rate) {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530210 me.frm.set_value("plc_conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530211 me.plc_conversion_rate();
212 }
213 });
214 } else {
215 this.plc_conversion_rate();
216 }
Anand Doshi3543f302013-05-24 19:25:01 +0530217 },
218
219 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530220 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
221 this.frm.set_value("plc_conversion_rate", 1.0);
222 } else if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
223 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
224 this.calculate_taxes_and_totals();
225 }
Anand Doshi3543f302013-05-24 19:25:01 +0530226 },
227
228 qty: function(doc, cdt, cdn) {
229 this.calculate_taxes_and_totals();
230 },
231
Anand Doshi923d41d2013-05-28 17:23:36 +0530232 tax_rate: function(doc, cdt, cdn) {
233 this.calculate_taxes_and_totals();
234 },
235
236 row_id: function(doc, cdt, cdn) {
237 var tax = wn.model.get_doc(cdt, cdn);
238 try {
239 this.validate_on_previous_row(tax);
240 this.calculate_taxes_and_totals();
241 } catch(e) {
242 tax.row_id = null;
243 refresh_field("row_id", tax.name, tax.parentfield);
244 throw e;
245 }
246 },
247
Anand Doshi61a2f682013-06-21 17:55:31 +0530248 set_dynamic_labels: function() {
249 // What TODO? should we make price list system non-mandatory?
250 this.frm.toggle_reqd("plc_conversion_rate",
251 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
252
253 var company_currency = this.get_company_currency();
254 this.change_form_labels(company_currency);
255 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530256 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530257 },
258
Anand Doshi923d41d2013-05-28 17:23:36 +0530259 recalculate: function() {
260 this.calculate_taxes_and_totals();
261 },
262
263 recalculate_values: function() {
264 this.calculate_taxes_and_totals();
265 },
266
Anand Doshid0b00722013-05-28 18:54:48 +0530267 calculate_charges: function() {
268 this.calculate_taxes_and_totals();
269 },
270
Anand Doshi3543f302013-05-24 19:25:01 +0530271 included_in_print_rate: function(doc, cdt, cdn) {
272 var tax = wn.model.get_doc(cdt, cdn);
273 try {
274 this.validate_on_previous_row(tax);
275 this.validate_inclusive_tax(tax);
276 this.calculate_taxes_and_totals();
277 } catch(e) {
278 tax.included_in_print_rate = 0;
279 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
280 throw e;
281 }
282 },
283
284 validate_on_previous_row: function(tax) {
285 // validate if a valid row id is mentioned in case of
286 // On Previous Row Amount and On Previous Row Total
287 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
288 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
289 var msg = repl(wn._("Row") + " # %(idx)s [%(doctype)s]: " +
290 wn._("Please specify a valid") + " %(row_id_label)s", {
291 idx: tax.idx,
292 doctype: tax.doctype,
293 row_id_label: wn.meta.get_label(tax.doctype, "row_id", tax.name)
294 });
295 msgprint(msg);
296 throw msg;
297 }
298 },
299
300 validate_inclusive_tax: function(tax) {
301 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
302
303 var actual_type_error = function() {
304 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
305 "%(charge_type_label)s = \"%(charge_type)s\" " +
306 wn._("cannot be included in Item's rate"), {
307 idx: tax.idx,
308 doctype: tax.doctype,
309 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
310 charge_type: tax.charge_type
311 });
312 msgprint(msg);
313 throw msg;
314 };
315
316 var on_previous_row_error = function(row_range) {
317 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
318 wn._("to be included in Item's rate, it is required that: ") +
319 " [" + wn._("Row") + " # %(row_range)s] " + wn._("also be included in Item's rate"), {
320 idx: tax.idx,
321 doctype: tax.doctype,
322 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
323 charge_type: tax.charge_type,
324 inclusive_label: wn.meta.get_label(tax.doctype, "included_in_print_rate", tax.name),
325 row_range: row_range,
326 });
327
328 msgprint(msg);
329 throw msg;
330 };
331
332 if(cint(tax.included_in_print_rate)) {
333 if(tax.charge_type == "Actual") {
334 // inclusive tax cannot be of type Actual
335 actual_type_error();
336 } else if(tax.charge_type == "On Previous Row Amount" &&
337 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
338 // referred row should also be an inclusive tax
339 on_previous_row_error(tax.row_id);
340 } else if(tax.charge_type == "On Previous Row Total") {
341 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
342 function(t) { return cint(t.included_in_print_rate) ? null : t; });
343 if(taxes_not_included.length > 0) {
344 // all rows above this tax should be inclusive
345 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
346 }
347 }
348 }
349 },
350
351 _load_item_tax_rate: function(item_tax_rate) {
352 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
353 },
354
355 _get_tax_rate: function(tax, item_tax_map) {
356 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
357 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
358 tax.rate;
359 },
360
361 get_item_wise_taxes_html: function() {
362 var item_tax = {};
363 var tax_accounts = [];
364 var company_currency = this.get_company_currency();
365
366 $.each(this.get_tax_doclist(), function(i, tax) {
367 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530368 var tax_rate_precision = precision("rate", tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530369 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530370 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530371 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530372 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530373 var tax_rate = "";
374 if(tax_data[0] != null) {
375 tax_rate = (tax.charge_type === "Actual") ?
376 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
377 (flt(tax_data[0], tax_rate_precision) + "%");
378 }
379 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
380 tax_amount_precision);
Anand Doshi53b73422013-05-31 11:50:01 +0530381
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530382 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530383 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530384 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530385 }
Anand Doshi3543f302013-05-24 19:25:01 +0530386 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530387 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530388 });
389
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530390 var headings = $.map([wn._("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530391 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Anand Doshi179e3772013-09-05 16:53:57 +0530392
393 var distinct_item_names = [];
394 var distinct_items = [];
395 $.each(this.get_item_doclist(), function(i, item) {
396 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
397 distinct_item_names.push(item.item_code || item.item_name);
398 distinct_items.push(item);
399 }
400 });
Anand Doshi3543f302013-05-24 19:25:01 +0530401
Anand Doshi179e3772013-09-05 16:53:57 +0530402 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530403 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530404 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530405 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
406 item_name: item.item_name,
407 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530408 return item_tax_record[head[0]] ?
409 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530410 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530411 }).join("\n")
412 });
413 }).join("\n");
414
Anand Doshi53b73422013-05-31 11:50:01 +0530415 if(!rows) return "";
Anand Doshi3543f302013-05-24 19:25:01 +0530416 return '<div style="overflow-x: scroll;"><table class="table table-bordered table-hover">\
417 <thead><tr>' + headings + '</tr></thead> \
418 <tbody>' + rows + '</tbody> \
419 </table></div>';
420 },
421
Anand Doshi923d41d2013-05-28 17:23:36 +0530422 _validate_before_fetch: function(fieldname) {
423 var me = this;
424 if(!me.frm.doc[fieldname]) {
425 return (wn._("Please specify") + ": " +
426 wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
427 ". " + wn._("It is needed to fetch Item Details."));
428 }
429 return null;
430 },
431
Anand Doshi3543f302013-05-24 19:25:01 +0530432 validate_company_and_party: function(party_field) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530433 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530434 var valid = true;
Anand Doshi923d41d2013-05-28 17:23:36 +0530435 var msg = "";
Anand Doshi3543f302013-05-24 19:25:01 +0530436 $.each(["company", party_field], function(i, fieldname) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530437 var msg_for_fieldname = me._validate_before_fetch(fieldname);
438 if(msg_for_fieldname) {
439 msgprint(msg_for_fieldname);
Anand Doshi3543f302013-05-24 19:25:01 +0530440 valid = false;
Anand Doshi3543f302013-05-24 19:25:01 +0530441 }
442 });
443 return valid;
444 },
445
446 get_item_doclist: function() {
447 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
448 {parentfield: this.fname});
449 },
450
451 get_tax_doclist: function() {
452 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
453 {parentfield: this.other_fname});
454 },
455
456 validate_conversion_rate: function() {
457 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
458 var conversion_rate_label = wn.meta.get_label(this.frm.doc.doctype, "conversion_rate",
459 this.frm.doc.name);
460
461 if(this.frm.doc.conversion_rate == 0) {
462 wn.throw(wn._(conversion_rate_label) + " " + wn._("cannot be 0"));
463 }
464
465 var company_currency = this.get_company_currency();
Anand Doshi923d41d2013-05-28 17:23:36 +0530466 var valid_conversion_rate = this.frm.doc.conversion_rate ?
Anand Doshi3543f302013-05-24 19:25:01 +0530467 ((this.frm.doc.currency == company_currency && this.frm.doc.conversion_rate == 1.0) ||
468 (this.frm.doc.currency != company_currency && this.frm.doc.conversion_rate != 1.0)) :
469 false;
470
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530471 if(!valid_conversion_rate) {
472 wn.throw(wn._("Please enter valid") + " " + wn._(conversion_rate_label) +
473 " 1 " + this.frm.doc.currency + " = [?] " + company_currency);
474 }
Anand Doshi3543f302013-05-24 19:25:01 +0530475 },
476
477 calculate_taxes_and_totals: function() {
478 this.validate_conversion_rate();
479 this.frm.item_doclist = this.get_item_doclist();
480 this.frm.tax_doclist = this.get_tax_doclist();
481
482 this.calculate_item_values();
483 this.initialize_taxes();
484 this.determine_exclusive_rate && this.determine_exclusive_rate();
485 this.calculate_net_total();
486 this.calculate_taxes();
487 this.calculate_totals();
488 this._cleanup();
Anand Doshi923d41d2013-05-28 17:23:36 +0530489
Anand Doshi3543f302013-05-24 19:25:01 +0530490 this.show_item_wise_taxes();
491 },
492
493 initialize_taxes: function() {
494 var me = this;
495 $.each(this.frm.tax_doclist, function(i, tax) {
496 tax.item_wise_tax_detail = {};
497 $.each(["tax_amount", "total",
498 "tax_amount_for_current_item", "grand_total_for_current_item",
499 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
500 function(i, fieldname) { tax[fieldname] = 0.0 });
501
502 me.validate_on_previous_row(tax);
503 me.validate_inclusive_tax(tax);
504 wn.model.round_floats_in(tax);
505 });
506 },
507
508 calculate_taxes: function() {
509 var me = this;
510
511 $.each(this.frm.item_doclist, function(n, item) {
512 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
513
514 $.each(me.frm.tax_doclist, function(i, tax) {
515 // tax_amount represents the amount of tax for the current step
516 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530517
Anand Doshi3543f302013-05-24 19:25:01 +0530518 me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
519
520 // case when net total is 0 but there is an actual type charge
521 // in this case add the actual amount to tax.tax_amount
522 // and tax.grand_total_for_current_item for the first such iteration
523 if(tax.charge_type == "Actual" &&
524 !(current_tax_amount || me.frm.doc.net_total || tax.tax_amount)) {
525 var zero_net_total_adjustment = flt(tax.rate, precision("tax_amount", tax));
526 current_tax_amount += zero_net_total_adjustment;
527 }
528
529 // store tax_amount for current item as it will be used for
530 // charge type = 'On Previous Row Amount'
531 tax.tax_amount_for_current_item = current_tax_amount;
532
533 // accumulate tax amount into tax.tax_amount
534 tax.tax_amount += current_tax_amount;
535
Anand Doshi3543f302013-05-24 19:25:01 +0530536 // for buying
537 if(tax.category) {
538 // if just for valuation, do not add the tax amount in total
539 // hence, setting it as 0 for further steps
540 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
541
542 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
543 }
544
545 // Calculate tax.total viz. grand total till that step
546 // note: grand_total_for_current_item contains the contribution of
547 // item's amount, previously applied tax and the current tax on that item
548 if(i==0) {
549 tax.grand_total_for_current_item = flt(item.amount + current_tax_amount,
550 precision("total", tax));
551 } else {
552 tax.grand_total_for_current_item =
553 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
554 precision("total", tax));
555 }
556
557 // in tax.total, accumulate grand total for each item
558 tax.total += tax.grand_total_for_current_item;
559 });
560 });
561 },
562
563 get_current_tax_amount: function(item, tax, item_tax_map) {
564 var tax_rate = this._get_tax_rate(tax, item_tax_map);
565 var current_tax_amount = 0.0;
566
567 if(tax.charge_type == "Actual") {
568 // distribute the tax amount proportionally to each item row
569 var actual = flt(tax.rate, precision("tax_amount", tax));
570 current_tax_amount = this.frm.doc.net_total ?
571 ((item.amount / this.frm.doc.net_total) * actual) :
572 0.0;
573
574 } else if(tax.charge_type == "On Net Total") {
575 current_tax_amount = (tax_rate / 100.0) * item.amount;
576
577 } else if(tax.charge_type == "On Previous Row Amount") {
578 current_tax_amount = (tax_rate / 100.0) *
579 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
580
581 } else if(tax.charge_type == "On Previous Row Total") {
582 current_tax_amount = (tax_rate / 100.0) *
583 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
584
585 }
586
Anand Doshi53b73422013-05-31 11:50:01 +0530587 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
588
589 // store tax breakup for each item
590 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
591
592 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530593 },
594
595 _cleanup: function() {
596 $.each(this.frm.tax_doclist, function(i, tax) {
597 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
598 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
599 function(i, fieldname) { delete tax[fieldname]; });
600
601 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
602 });
603 },
Anand Doshifc777182013-05-27 19:29:07 +0530604
605 calculate_total_advance: function(parenttype, advance_parentfield) {
606 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
607 var advance_doclist = wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
608 {parentfield: advance_parentfield});
609 this.frm.doc.total_advance = flt(wn.utils.sum(
610 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
611 ), precision("total_advance"));
612
613 this.calculate_outstanding_amount();
614 }
615 },
Anand Doshi3543f302013-05-24 19:25:01 +0530616
617 _set_in_company_currency: function(item, print_field, base_field) {
618 // set values in base currency
619 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
620 precision(base_field, item));
621 },
Anand Doshibf3e54a2013-06-05 14:11:32 +0530622
623 get_terms: function() {
624 var me = this;
625 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530626 return this.frm.call({
Anand Doshibf3e54a2013-06-05 14:11:32 +0530627 method: "webnotes.client.get_value",
628 args: {
629 doctype: "Terms and Conditions",
630 fieldname: "terms",
631 filters: { name: this.frm.doc.tc_name },
632 },
633 });
634 }
635 },
Anand Doshi3543f302013-05-24 19:25:01 +0530636});