blob: e12d1084e98f372348a6cebd6d6a259a0e9bd283 [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
Akhilesh Darjeef624ffa2013-09-23 12:27:16 +0530101 if(!this.frm.pos) {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530102 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 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530235
236 // serial_no: function(doc, cdt, cdn) {
237 // var me = this;
238 // var item = wn.model.get_doc(cdt, cdn);
239 // if (!item.item_code) {
240 // wn.call({
241 // method: 'accounts.doctype.sales_invoice.pos.get_item_from_serial_no',
242 // args: {serial_no: this.serial_no.$input.val()},
243 // callback: function(r) {
244 // if (r.message) {
245 // var item_code = r.message[0].item_code;
246 // var child = wn.model.add_child(me.frm.doc, this.frm.doctype + " Item",
247 // this.frm.cscript.fname);
248 // child.item_code = item_code;
249 // me.frm.cscript.item_code(me.frm.doc, child.doctype, child.name);
250 // }
251 // else
252 // msgprint(wn._("Invalid Serial No."));
253 // me.refresh();
254 // }
255 // });
256 // }
257 // },
Anand Doshi923d41d2013-05-28 17:23:36 +0530258
259 row_id: function(doc, cdt, cdn) {
260 var tax = wn.model.get_doc(cdt, cdn);
261 try {
262 this.validate_on_previous_row(tax);
263 this.calculate_taxes_and_totals();
264 } catch(e) {
265 tax.row_id = null;
266 refresh_field("row_id", tax.name, tax.parentfield);
267 throw e;
268 }
269 },
270
Anand Doshi61a2f682013-06-21 17:55:31 +0530271 set_dynamic_labels: function() {
272 // What TODO? should we make price list system non-mandatory?
273 this.frm.toggle_reqd("plc_conversion_rate",
274 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
275
276 var company_currency = this.get_company_currency();
277 this.change_form_labels(company_currency);
278 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530279 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530280 },
281
Anand Doshi923d41d2013-05-28 17:23:36 +0530282 recalculate: function() {
283 this.calculate_taxes_and_totals();
284 },
285
286 recalculate_values: function() {
287 this.calculate_taxes_and_totals();
288 },
289
Anand Doshid0b00722013-05-28 18:54:48 +0530290 calculate_charges: function() {
291 this.calculate_taxes_and_totals();
292 },
293
Anand Doshi3543f302013-05-24 19:25:01 +0530294 included_in_print_rate: function(doc, cdt, cdn) {
295 var tax = wn.model.get_doc(cdt, cdn);
296 try {
297 this.validate_on_previous_row(tax);
298 this.validate_inclusive_tax(tax);
299 this.calculate_taxes_and_totals();
300 } catch(e) {
301 tax.included_in_print_rate = 0;
302 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
303 throw e;
304 }
305 },
306
307 validate_on_previous_row: function(tax) {
308 // validate if a valid row id is mentioned in case of
309 // On Previous Row Amount and On Previous Row Total
310 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
311 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
312 var msg = repl(wn._("Row") + " # %(idx)s [%(doctype)s]: " +
313 wn._("Please specify a valid") + " %(row_id_label)s", {
314 idx: tax.idx,
315 doctype: tax.doctype,
316 row_id_label: wn.meta.get_label(tax.doctype, "row_id", tax.name)
317 });
318 msgprint(msg);
319 throw msg;
320 }
321 },
322
323 validate_inclusive_tax: function(tax) {
324 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
325
326 var actual_type_error = function() {
327 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
328 "%(charge_type_label)s = \"%(charge_type)s\" " +
329 wn._("cannot be included in Item's rate"), {
330 idx: tax.idx,
331 doctype: tax.doctype,
332 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
333 charge_type: tax.charge_type
334 });
335 msgprint(msg);
336 throw msg;
337 };
338
339 var on_previous_row_error = function(row_range) {
340 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
341 wn._("to be included in Item's rate, it is required that: ") +
342 " [" + wn._("Row") + " # %(row_range)s] " + wn._("also be included in Item's rate"), {
343 idx: tax.idx,
344 doctype: tax.doctype,
345 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
346 charge_type: tax.charge_type,
347 inclusive_label: wn.meta.get_label(tax.doctype, "included_in_print_rate", tax.name),
348 row_range: row_range,
349 });
350
351 msgprint(msg);
352 throw msg;
353 };
354
355 if(cint(tax.included_in_print_rate)) {
356 if(tax.charge_type == "Actual") {
357 // inclusive tax cannot be of type Actual
358 actual_type_error();
359 } else if(tax.charge_type == "On Previous Row Amount" &&
360 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
361 // referred row should also be an inclusive tax
362 on_previous_row_error(tax.row_id);
363 } else if(tax.charge_type == "On Previous Row Total") {
364 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
365 function(t) { return cint(t.included_in_print_rate) ? null : t; });
366 if(taxes_not_included.length > 0) {
367 // all rows above this tax should be inclusive
368 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
369 }
370 }
371 }
372 },
373
374 _load_item_tax_rate: function(item_tax_rate) {
375 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
376 },
377
378 _get_tax_rate: function(tax, item_tax_map) {
379 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
380 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
381 tax.rate;
382 },
383
384 get_item_wise_taxes_html: function() {
385 var item_tax = {};
386 var tax_accounts = [];
387 var company_currency = this.get_company_currency();
388
389 $.each(this.get_tax_doclist(), function(i, tax) {
390 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530391 var tax_rate_precision = precision("rate", tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530392 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530393 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530394 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530395 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530396 var tax_rate = "";
397 if(tax_data[0] != null) {
398 tax_rate = (tax.charge_type === "Actual") ?
399 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
400 (flt(tax_data[0], tax_rate_precision) + "%");
401 }
402 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
403 tax_amount_precision);
Anand Doshi53b73422013-05-31 11:50:01 +0530404
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530405 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530406 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530407 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530408 }
Anand Doshi3543f302013-05-24 19:25:01 +0530409 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530410 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530411 });
412
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530413 var headings = $.map([wn._("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530414 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Anand Doshi179e3772013-09-05 16:53:57 +0530415
416 var distinct_item_names = [];
417 var distinct_items = [];
418 $.each(this.get_item_doclist(), function(i, item) {
419 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
420 distinct_item_names.push(item.item_code || item.item_name);
421 distinct_items.push(item);
422 }
423 });
Anand Doshi3543f302013-05-24 19:25:01 +0530424
Anand Doshi179e3772013-09-05 16:53:57 +0530425 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530426 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530427 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530428 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
429 item_name: item.item_name,
430 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530431 return item_tax_record[head[0]] ?
432 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530433 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530434 }).join("\n")
435 });
436 }).join("\n");
437
Anand Doshi53b73422013-05-31 11:50:01 +0530438 if(!rows) return "";
Anand Doshi3543f302013-05-24 19:25:01 +0530439 return '<div style="overflow-x: scroll;"><table class="table table-bordered table-hover">\
440 <thead><tr>' + headings + '</tr></thead> \
441 <tbody>' + rows + '</tbody> \
442 </table></div>';
443 },
444
Anand Doshi923d41d2013-05-28 17:23:36 +0530445 _validate_before_fetch: function(fieldname) {
446 var me = this;
447 if(!me.frm.doc[fieldname]) {
448 return (wn._("Please specify") + ": " +
449 wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
450 ". " + wn._("It is needed to fetch Item Details."));
451 }
452 return null;
453 },
454
Anand Doshi3543f302013-05-24 19:25:01 +0530455 validate_company_and_party: function(party_field) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530456 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530457 var valid = true;
Anand Doshi923d41d2013-05-28 17:23:36 +0530458 var msg = "";
Anand Doshi3543f302013-05-24 19:25:01 +0530459 $.each(["company", party_field], function(i, fieldname) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530460 var msg_for_fieldname = me._validate_before_fetch(fieldname);
461 if(msg_for_fieldname) {
462 msgprint(msg_for_fieldname);
Anand Doshi3543f302013-05-24 19:25:01 +0530463 valid = false;
Anand Doshi3543f302013-05-24 19:25:01 +0530464 }
465 });
466 return valid;
467 },
468
469 get_item_doclist: function() {
470 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
471 {parentfield: this.fname});
472 },
473
474 get_tax_doclist: function() {
475 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
476 {parentfield: this.other_fname});
477 },
478
479 validate_conversion_rate: function() {
480 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
481 var conversion_rate_label = wn.meta.get_label(this.frm.doc.doctype, "conversion_rate",
482 this.frm.doc.name);
483
484 if(this.frm.doc.conversion_rate == 0) {
485 wn.throw(wn._(conversion_rate_label) + " " + wn._("cannot be 0"));
486 }
487
488 var company_currency = this.get_company_currency();
Anand Doshi923d41d2013-05-28 17:23:36 +0530489 var valid_conversion_rate = this.frm.doc.conversion_rate ?
Anand Doshi3543f302013-05-24 19:25:01 +0530490 ((this.frm.doc.currency == company_currency && this.frm.doc.conversion_rate == 1.0) ||
491 (this.frm.doc.currency != company_currency && this.frm.doc.conversion_rate != 1.0)) :
492 false;
493
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530494 if(!valid_conversion_rate) {
495 wn.throw(wn._("Please enter valid") + " " + wn._(conversion_rate_label) +
496 " 1 " + this.frm.doc.currency + " = [?] " + company_currency);
497 }
Anand Doshi3543f302013-05-24 19:25:01 +0530498 },
499
500 calculate_taxes_and_totals: function() {
501 this.validate_conversion_rate();
502 this.frm.item_doclist = this.get_item_doclist();
503 this.frm.tax_doclist = this.get_tax_doclist();
504
505 this.calculate_item_values();
506 this.initialize_taxes();
507 this.determine_exclusive_rate && this.determine_exclusive_rate();
508 this.calculate_net_total();
509 this.calculate_taxes();
510 this.calculate_totals();
511 this._cleanup();
Anand Doshi923d41d2013-05-28 17:23:36 +0530512
Anand Doshi3543f302013-05-24 19:25:01 +0530513 this.show_item_wise_taxes();
514 },
515
516 initialize_taxes: function() {
517 var me = this;
518 $.each(this.frm.tax_doclist, function(i, tax) {
519 tax.item_wise_tax_detail = {};
520 $.each(["tax_amount", "total",
521 "tax_amount_for_current_item", "grand_total_for_current_item",
522 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
523 function(i, fieldname) { tax[fieldname] = 0.0 });
524
525 me.validate_on_previous_row(tax);
526 me.validate_inclusive_tax(tax);
527 wn.model.round_floats_in(tax);
528 });
529 },
530
531 calculate_taxes: function() {
532 var me = this;
533
534 $.each(this.frm.item_doclist, function(n, item) {
535 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
536
537 $.each(me.frm.tax_doclist, function(i, tax) {
538 // tax_amount represents the amount of tax for the current step
539 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530540
Anand Doshi3543f302013-05-24 19:25:01 +0530541 me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
542
543 // case when net total is 0 but there is an actual type charge
544 // in this case add the actual amount to tax.tax_amount
545 // and tax.grand_total_for_current_item for the first such iteration
546 if(tax.charge_type == "Actual" &&
547 !(current_tax_amount || me.frm.doc.net_total || tax.tax_amount)) {
548 var zero_net_total_adjustment = flt(tax.rate, precision("tax_amount", tax));
549 current_tax_amount += zero_net_total_adjustment;
550 }
551
552 // store tax_amount for current item as it will be used for
553 // charge type = 'On Previous Row Amount'
554 tax.tax_amount_for_current_item = current_tax_amount;
555
556 // accumulate tax amount into tax.tax_amount
557 tax.tax_amount += current_tax_amount;
558
Anand Doshi3543f302013-05-24 19:25:01 +0530559 // for buying
560 if(tax.category) {
561 // if just for valuation, do not add the tax amount in total
562 // hence, setting it as 0 for further steps
563 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
564
565 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
566 }
567
568 // Calculate tax.total viz. grand total till that step
569 // note: grand_total_for_current_item contains the contribution of
570 // item's amount, previously applied tax and the current tax on that item
571 if(i==0) {
572 tax.grand_total_for_current_item = flt(item.amount + current_tax_amount,
573 precision("total", tax));
574 } else {
575 tax.grand_total_for_current_item =
576 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
577 precision("total", tax));
578 }
579
580 // in tax.total, accumulate grand total for each item
581 tax.total += tax.grand_total_for_current_item;
582 });
583 });
584 },
585
586 get_current_tax_amount: function(item, tax, item_tax_map) {
587 var tax_rate = this._get_tax_rate(tax, item_tax_map);
588 var current_tax_amount = 0.0;
589
590 if(tax.charge_type == "Actual") {
591 // distribute the tax amount proportionally to each item row
592 var actual = flt(tax.rate, precision("tax_amount", tax));
593 current_tax_amount = this.frm.doc.net_total ?
594 ((item.amount / this.frm.doc.net_total) * actual) :
595 0.0;
596
597 } else if(tax.charge_type == "On Net Total") {
598 current_tax_amount = (tax_rate / 100.0) * item.amount;
599
600 } else if(tax.charge_type == "On Previous Row Amount") {
601 current_tax_amount = (tax_rate / 100.0) *
602 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
603
604 } else if(tax.charge_type == "On Previous Row Total") {
605 current_tax_amount = (tax_rate / 100.0) *
606 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
607
608 }
609
Anand Doshi53b73422013-05-31 11:50:01 +0530610 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
611
612 // store tax breakup for each item
613 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
614
615 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530616 },
617
618 _cleanup: function() {
619 $.each(this.frm.tax_doclist, function(i, tax) {
620 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
621 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
622 function(i, fieldname) { delete tax[fieldname]; });
623
624 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
625 });
626 },
Anand Doshifc777182013-05-27 19:29:07 +0530627
628 calculate_total_advance: function(parenttype, advance_parentfield) {
629 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
630 var advance_doclist = wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
631 {parentfield: advance_parentfield});
632 this.frm.doc.total_advance = flt(wn.utils.sum(
633 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
634 ), precision("total_advance"));
635
636 this.calculate_outstanding_amount();
637 }
638 },
Anand Doshi3543f302013-05-24 19:25:01 +0530639
640 _set_in_company_currency: function(item, print_field, base_field) {
641 // set values in base currency
642 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
643 precision(base_field, item));
644 },
Anand Doshibf3e54a2013-06-05 14:11:32 +0530645
646 get_terms: function() {
647 var me = this;
648 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530649 return this.frm.call({
Anand Doshibf3e54a2013-06-05 14:11:32 +0530650 method: "webnotes.client.get_value",
651 args: {
652 doctype: "Terms and Conditions",
653 fieldname: "terms",
654 filters: { name: this.frm.doc.tc_name },
655 },
656 });
657 }
658 },
Anand Doshi3543f302013-05-24 19:25:01 +0530659});