blob: 79ea53623feb1e2852feed69de4fff5ea9a15726 [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",
Anand Doshi3543f302013-05-24 19:25:01 +053024 }, function(fieldname, value) {
25 if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname])
26 me.frm.set_value(fieldname, value);
27 });
Anand Doshi535d8332013-07-19 13:51:07 +053028
29 me.frm.script_manager.trigger("company");
Anand Doshi3543f302013-05-24 19:25:01 +053030 }
Anand Doshi1e4fb682013-08-23 12:56:33 +053031
32 if(this.other_fname) {
Anand Doshia91c95f2013-08-23 13:00:47 +053033 this[this.other_fname + "_remove"] = this.calculate_taxes_and_totals;
34 }
35
36 if(this.fname) {
37 this[this.fname + "_remove"] = this.calculate_taxes_and_totals;
Anand Doshi1e4fb682013-08-23 12:56:33 +053038 }
Anand Doshi3543f302013-05-24 19:25:01 +053039 },
40
Anand Doshi5e4e5d72013-08-06 17:23:44 +053041 onload_post_render: function() {
Akhilesh Darjee78378272013-10-11 19:06:30 +053042 if(this.frm.doc.__islocal && this.frm.doc.company &&
43 !this.frm.doc.customer && !this.frm.doc.is_pos) {
44 var me = this;
45 return this.frm.call({
46 doc: this.frm.doc,
47 method: "onload_post_render",
48 freeze: true,
49 callback: function(r) {
50 // remove this call when using client side mapper
51 me.set_default_values();
52 me.set_dynamic_labels();
53 }
54 });
Anand Doshi5e4e5d72013-08-06 17:23:44 +053055 }
56 },
57
Anand Doshi3543f302013-05-24 19:25:01 +053058 refresh: function() {
59 this.frm.clear_custom_buttons();
60 erpnext.hide_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +053061 erpnext.hide_company();
Anand Doshi3543f302013-05-24 19:25:01 +053062 this.show_item_wise_taxes();
Nabin Hait1f996c32013-07-29 19:00:53 +053063 this.set_dynamic_labels();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053064
65 // Show POS button only if it is enabled from features setup
66 if(cint(sys_defaults.fs_pos_view)===1 && this.frm.doctype!="Material Request")
67 this.pos_btn();
68 },
69
70 pos_btn: function() {
71 if(this.$pos_btn)
72 this.$pos_btn.remove();
73
74 if(!this.pos_active) {
75 var btn_label = wn._("POS View"),
76 icon = "icon-desktop";
77 } else {
78 var btn_label = wn._(this.frm.doctype) + wn._(" View"),
79 icon = "icon-file-text";
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053080 }
81 var me = this;
82
83 this.$pos_btn = this.frm.add_custom_button(btn_label, function() {
84 me.toggle_pos();
85 me.pos_btn();
86 }, icon);
87 },
88
89 toggle_pos: function(show) {
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053090 // Check whether it is Selling or Buying cycle
91 var price_list = wn.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
92 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list;
93
94 if (!price_list)
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053095 msgprint(wn._("Please select Price List"))
96 else {
97 if((show===true && this.pos_active) || (show===false && !this.pos_active)) return;
98
99 // make pos
Akhilesh Darjeef624ffa2013-09-23 12:27:16 +0530100 if(!this.frm.pos) {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530101 this.frm.layout.add_view("pos");
102 this.frm.pos = new erpnext.POS(this.frm.layout.views.pos, this.frm);
103 }
104
105 // toggle view
106 this.frm.layout.set_view(this.pos_active ? "" : "pos");
107 this.pos_active = !this.pos_active;
108
109 // refresh
110 if(this.pos_active)
111 this.frm.pos.refresh();
Akhilesh Darjeef83576b2013-09-18 18:35:12 +0530112 this.frm.refresh();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530113 }
Anand Doshi3543f302013-05-24 19:25:01 +0530114 },
115
Anand Doshi923d41d2013-05-28 17:23:36 +0530116 validate: function() {
117 this.calculate_taxes_and_totals();
118 },
119
Anand Doshi5e4e5d72013-08-06 17:23:44 +0530120 set_default_values: function() {
121 $.each(wn.model.get_doclist(this.frm.doctype, this.frm.docname), function(i, doc) {
122 var updated = wn.model.set_default_values(doc);
123 if(doc.parentfield) {
124 refresh_field(doc.parentfield);
125 } else {
126 refresh_field(updated);
127 }
128 });
129 },
130
Anand Doshi3543f302013-05-24 19:25:01 +0530131 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +0530132 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530133 var company_currency = this.get_company_currency();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530134 if (!this.frm.doc.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530135 this.frm.set_value("currency", company_currency);
Anand Doshi17350b82013-08-01 15:45:23 +0530136 }
137
Akhilesh Darjee78378272013-10-11 19:06:30 +0530138 if (this.frm.doc.currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530139 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530140 }
141 if (this.frm.doc.price_list_currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530142 this.frm.set_value('plc_conversion_rate', 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530143 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530144
Nabin Hait48a0bd32013-07-23 15:31:39 +0530145 this.frm.script_manager.trigger("currency");
Anand Doshi3543f302013-05-24 19:25:01 +0530146 }
147 },
148
149 get_company_currency: function() {
150 return erpnext.get_currency(this.frm.doc.company);
151 },
152
153 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530154 var me = this;
155 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530156
Anand Doshi61a2f682013-06-21 17:55:31 +0530157 var company_currency = this.get_company_currency();
158 if(this.frm.doc.currency !== company_currency) {
159 this.get_exchange_rate(this.frm.doc.currency, company_currency,
160 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530161 me.frm.set_value("conversion_rate", exchange_rate);
162 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530163 });
164 } else {
165 this.conversion_rate();
166 }
167 },
168
169 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530170 if(this.frm.doc.currency === this.get_company_currency()) {
171 this.frm.set_value("conversion_rate", 1.0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530172 } else if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
173 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
174 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
175 }
Akhilesh Darjee78378272013-10-11 19:06:30 +0530176
Anand Doshi61a2f682013-06-21 17:55:31 +0530177 this.calculate_taxes_and_totals();
Anand Doshi3543f302013-05-24 19:25:01 +0530178 },
179
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530180 get_price_list_currency: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +0530181 var me = this;
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530182 var fieldname = buying_or_selling.toLowerCase() + "_price_list";
183 if(this.frm.doc[fieldname]) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530184 return this.frm.call({
Anand Doshi3543f302013-05-24 19:25:01 +0530185 method: "setup.utils.get_price_list_currency",
Nabin Hait48a0bd32013-07-23 15:31:39 +0530186 args: {
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530187 price_list: this.frm.doc[fieldname],
Nabin Hait48a0bd32013-07-23 15:31:39 +0530188 },
Anand Doshi3543f302013-05-24 19:25:01 +0530189 callback: function(r) {
190 if(!r.exc) {
191 me.price_list_currency();
192 }
193 }
194 });
195 }
196 },
197
Anand Doshi61a2f682013-06-21 17:55:31 +0530198 get_exchange_rate: function(from_currency, to_currency, callback) {
199 var exchange_name = from_currency + "-" + to_currency;
200 wn.model.with_doc("Currency Exchange", exchange_name, function(name) {
201 var exchange_doc = wn.model.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530202 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530203 });
204 },
205
Anand Doshi3543f302013-05-24 19:25:01 +0530206 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530207 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530208 this.set_dynamic_labels();
Anand Doshi61a2f682013-06-21 17:55:31 +0530209
210 var company_currency = this.get_company_currency();
211 if(this.frm.doc.price_list_currency !== company_currency) {
212 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
213 function(exchange_rate) {
214 if(exchange_rate) {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530215 me.frm.set_value("plc_conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530216 me.plc_conversion_rate();
217 }
218 });
219 } else {
220 this.plc_conversion_rate();
221 }
Anand Doshi3543f302013-05-24 19:25:01 +0530222 },
223
224 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530225 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
226 this.frm.set_value("plc_conversion_rate", 1.0);
227 } else if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
228 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
229 this.calculate_taxes_and_totals();
230 }
Anand Doshi3543f302013-05-24 19:25:01 +0530231 },
232
233 qty: function(doc, cdt, cdn) {
234 this.calculate_taxes_and_totals();
235 },
236
Anand Doshi923d41d2013-05-28 17:23:36 +0530237 tax_rate: function(doc, cdt, cdn) {
238 this.calculate_taxes_and_totals();
239 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530240
Anand Doshi923d41d2013-05-28 17:23:36 +0530241 row_id: function(doc, cdt, cdn) {
242 var tax = wn.model.get_doc(cdt, cdn);
243 try {
244 this.validate_on_previous_row(tax);
245 this.calculate_taxes_and_totals();
246 } catch(e) {
247 tax.row_id = null;
248 refresh_field("row_id", tax.name, tax.parentfield);
249 throw e;
250 }
251 },
252
Anand Doshi61a2f682013-06-21 17:55:31 +0530253 set_dynamic_labels: function() {
254 // What TODO? should we make price list system non-mandatory?
255 this.frm.toggle_reqd("plc_conversion_rate",
256 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
257
258 var company_currency = this.get_company_currency();
259 this.change_form_labels(company_currency);
260 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530261 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530262 },
263
Anand Doshi923d41d2013-05-28 17:23:36 +0530264 recalculate: function() {
265 this.calculate_taxes_and_totals();
266 },
267
268 recalculate_values: function() {
269 this.calculate_taxes_and_totals();
270 },
271
Anand Doshid0b00722013-05-28 18:54:48 +0530272 calculate_charges: function() {
273 this.calculate_taxes_and_totals();
274 },
275
Anand Doshi3543f302013-05-24 19:25:01 +0530276 included_in_print_rate: function(doc, cdt, cdn) {
277 var tax = wn.model.get_doc(cdt, cdn);
278 try {
279 this.validate_on_previous_row(tax);
280 this.validate_inclusive_tax(tax);
281 this.calculate_taxes_and_totals();
282 } catch(e) {
283 tax.included_in_print_rate = 0;
284 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
285 throw e;
286 }
287 },
288
289 validate_on_previous_row: function(tax) {
290 // validate if a valid row id is mentioned in case of
291 // On Previous Row Amount and On Previous Row Total
292 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
293 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
294 var msg = repl(wn._("Row") + " # %(idx)s [%(doctype)s]: " +
295 wn._("Please specify a valid") + " %(row_id_label)s", {
296 idx: tax.idx,
297 doctype: tax.doctype,
298 row_id_label: wn.meta.get_label(tax.doctype, "row_id", tax.name)
299 });
300 msgprint(msg);
301 throw msg;
302 }
303 },
304
305 validate_inclusive_tax: function(tax) {
306 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
307
308 var actual_type_error = function() {
309 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
310 "%(charge_type_label)s = \"%(charge_type)s\" " +
311 wn._("cannot be included in Item's rate"), {
312 idx: tax.idx,
313 doctype: tax.doctype,
314 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
315 charge_type: tax.charge_type
316 });
317 msgprint(msg);
318 throw msg;
319 };
320
321 var on_previous_row_error = function(row_range) {
322 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
323 wn._("to be included in Item's rate, it is required that: ") +
324 " [" + wn._("Row") + " # %(row_range)s] " + wn._("also be included in Item's rate"), {
325 idx: tax.idx,
326 doctype: tax.doctype,
327 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
328 charge_type: tax.charge_type,
329 inclusive_label: wn.meta.get_label(tax.doctype, "included_in_print_rate", tax.name),
330 row_range: row_range,
331 });
332
333 msgprint(msg);
334 throw msg;
335 };
336
337 if(cint(tax.included_in_print_rate)) {
338 if(tax.charge_type == "Actual") {
339 // inclusive tax cannot be of type Actual
340 actual_type_error();
341 } else if(tax.charge_type == "On Previous Row Amount" &&
342 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
343 // referred row should also be an inclusive tax
344 on_previous_row_error(tax.row_id);
345 } else if(tax.charge_type == "On Previous Row Total") {
346 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
347 function(t) { return cint(t.included_in_print_rate) ? null : t; });
348 if(taxes_not_included.length > 0) {
349 // all rows above this tax should be inclusive
350 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
351 }
352 }
353 }
354 },
355
356 _load_item_tax_rate: function(item_tax_rate) {
357 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
358 },
359
360 _get_tax_rate: function(tax, item_tax_map) {
361 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
362 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
363 tax.rate;
364 },
365
366 get_item_wise_taxes_html: function() {
367 var item_tax = {};
368 var tax_accounts = [];
369 var company_currency = this.get_company_currency();
370
371 $.each(this.get_tax_doclist(), function(i, tax) {
372 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530373 var tax_rate_precision = precision("rate", tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530374 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530375 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530376 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530377 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530378 var tax_rate = "";
379 if(tax_data[0] != null) {
380 tax_rate = (tax.charge_type === "Actual") ?
381 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
382 (flt(tax_data[0], tax_rate_precision) + "%");
383 }
384 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
385 tax_amount_precision);
Anand Doshi53b73422013-05-31 11:50:01 +0530386
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530387 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530388 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530389 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530390 }
Anand Doshi3543f302013-05-24 19:25:01 +0530391 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530392 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530393 });
394
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530395 var headings = $.map([wn._("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530396 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Anand Doshi179e3772013-09-05 16:53:57 +0530397
398 var distinct_item_names = [];
399 var distinct_items = [];
400 $.each(this.get_item_doclist(), function(i, item) {
401 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
402 distinct_item_names.push(item.item_code || item.item_name);
403 distinct_items.push(item);
404 }
405 });
Anand Doshi3543f302013-05-24 19:25:01 +0530406
Anand Doshi179e3772013-09-05 16:53:57 +0530407 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530408 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530409 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530410 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
411 item_name: item.item_name,
412 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530413 return item_tax_record[head[0]] ?
414 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530415 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530416 }).join("\n")
417 });
418 }).join("\n");
419
Anand Doshi53b73422013-05-31 11:50:01 +0530420 if(!rows) return "";
Anand Doshi3543f302013-05-24 19:25:01 +0530421 return '<div style="overflow-x: scroll;"><table class="table table-bordered table-hover">\
422 <thead><tr>' + headings + '</tr></thead> \
423 <tbody>' + rows + '</tbody> \
424 </table></div>';
425 },
426
Anand Doshi923d41d2013-05-28 17:23:36 +0530427 _validate_before_fetch: function(fieldname) {
428 var me = this;
429 if(!me.frm.doc[fieldname]) {
430 return (wn._("Please specify") + ": " +
431 wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
432 ". " + wn._("It is needed to fetch Item Details."));
433 }
434 return null;
435 },
436
Anand Doshi3543f302013-05-24 19:25:01 +0530437 validate_company_and_party: function(party_field) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530438 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530439 var valid = true;
Anand Doshi923d41d2013-05-28 17:23:36 +0530440 var msg = "";
Anand Doshi3543f302013-05-24 19:25:01 +0530441 $.each(["company", party_field], function(i, fieldname) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530442 var msg_for_fieldname = me._validate_before_fetch(fieldname);
443 if(msg_for_fieldname) {
444 msgprint(msg_for_fieldname);
Anand Doshi3543f302013-05-24 19:25:01 +0530445 valid = false;
Anand Doshi3543f302013-05-24 19:25:01 +0530446 }
447 });
448 return valid;
449 },
450
451 get_item_doclist: function() {
452 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
453 {parentfield: this.fname});
454 },
455
456 get_tax_doclist: function() {
457 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
458 {parentfield: this.other_fname});
459 },
460
461 validate_conversion_rate: function() {
462 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
463 var conversion_rate_label = wn.meta.get_label(this.frm.doc.doctype, "conversion_rate",
464 this.frm.doc.name);
465
466 if(this.frm.doc.conversion_rate == 0) {
467 wn.throw(wn._(conversion_rate_label) + " " + wn._("cannot be 0"));
468 }
469
470 var company_currency = this.get_company_currency();
Anand Doshi3543f302013-05-24 19:25:01 +0530471
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530472 if(!this.frm.doc.conversion_rate) {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530473 wn.throw(wn._("Please enter valid") + " " + wn._(conversion_rate_label) +
474 " 1 " + this.frm.doc.currency + " = [?] " + company_currency);
475 }
Anand Doshi3543f302013-05-24 19:25:01 +0530476 },
477
478 calculate_taxes_and_totals: function() {
479 this.validate_conversion_rate();
480 this.frm.item_doclist = this.get_item_doclist();
481 this.frm.tax_doclist = this.get_tax_doclist();
482
483 this.calculate_item_values();
484 this.initialize_taxes();
485 this.determine_exclusive_rate && this.determine_exclusive_rate();
486 this.calculate_net_total();
487 this.calculate_taxes();
488 this.calculate_totals();
489 this._cleanup();
Anand Doshi923d41d2013-05-28 17:23:36 +0530490
Anand Doshi3543f302013-05-24 19:25:01 +0530491 this.show_item_wise_taxes();
492 },
493
494 initialize_taxes: function() {
495 var me = this;
496 $.each(this.frm.tax_doclist, function(i, tax) {
497 tax.item_wise_tax_detail = {};
498 $.each(["tax_amount", "total",
499 "tax_amount_for_current_item", "grand_total_for_current_item",
500 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
501 function(i, fieldname) { tax[fieldname] = 0.0 });
502
503 me.validate_on_previous_row(tax);
504 me.validate_inclusive_tax(tax);
505 wn.model.round_floats_in(tax);
506 });
507 },
508
509 calculate_taxes: function() {
510 var me = this;
511
512 $.each(this.frm.item_doclist, function(n, item) {
513 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
514
515 $.each(me.frm.tax_doclist, function(i, tax) {
516 // tax_amount represents the amount of tax for the current step
517 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530518
Anand Doshi3543f302013-05-24 19:25:01 +0530519 me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
520
521 // case when net total is 0 but there is an actual type charge
522 // in this case add the actual amount to tax.tax_amount
523 // and tax.grand_total_for_current_item for the first such iteration
524 if(tax.charge_type == "Actual" &&
525 !(current_tax_amount || me.frm.doc.net_total || tax.tax_amount)) {
526 var zero_net_total_adjustment = flt(tax.rate, precision("tax_amount", tax));
527 current_tax_amount += zero_net_total_adjustment;
528 }
529
530 // store tax_amount for current item as it will be used for
531 // charge type = 'On Previous Row Amount'
532 tax.tax_amount_for_current_item = current_tax_amount;
533
534 // accumulate tax amount into tax.tax_amount
535 tax.tax_amount += current_tax_amount;
536
Anand Doshi3543f302013-05-24 19:25:01 +0530537 // for buying
538 if(tax.category) {
539 // if just for valuation, do not add the tax amount in total
540 // hence, setting it as 0 for further steps
541 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
542
543 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
544 }
545
546 // Calculate tax.total viz. grand total till that step
547 // note: grand_total_for_current_item contains the contribution of
548 // item's amount, previously applied tax and the current tax on that item
549 if(i==0) {
550 tax.grand_total_for_current_item = flt(item.amount + current_tax_amount,
551 precision("total", tax));
552 } else {
553 tax.grand_total_for_current_item =
554 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
555 precision("total", tax));
556 }
557
558 // in tax.total, accumulate grand total for each item
559 tax.total += tax.grand_total_for_current_item;
560 });
561 });
562 },
563
564 get_current_tax_amount: function(item, tax, item_tax_map) {
565 var tax_rate = this._get_tax_rate(tax, item_tax_map);
566 var current_tax_amount = 0.0;
567
568 if(tax.charge_type == "Actual") {
569 // distribute the tax amount proportionally to each item row
570 var actual = flt(tax.rate, precision("tax_amount", tax));
571 current_tax_amount = this.frm.doc.net_total ?
572 ((item.amount / this.frm.doc.net_total) * actual) :
573 0.0;
574
575 } else if(tax.charge_type == "On Net Total") {
576 current_tax_amount = (tax_rate / 100.0) * item.amount;
577
578 } else if(tax.charge_type == "On Previous Row Amount") {
579 current_tax_amount = (tax_rate / 100.0) *
580 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
581
582 } else if(tax.charge_type == "On Previous Row Total") {
583 current_tax_amount = (tax_rate / 100.0) *
584 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
585
586 }
587
Anand Doshi53b73422013-05-31 11:50:01 +0530588 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
589
590 // store tax breakup for each item
591 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
592
593 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530594 },
595
596 _cleanup: function() {
597 $.each(this.frm.tax_doclist, function(i, tax) {
598 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
599 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
600 function(i, fieldname) { delete tax[fieldname]; });
601
602 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
603 });
604 },
Anand Doshifc777182013-05-27 19:29:07 +0530605
606 calculate_total_advance: function(parenttype, advance_parentfield) {
607 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
608 var advance_doclist = wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
609 {parentfield: advance_parentfield});
610 this.frm.doc.total_advance = flt(wn.utils.sum(
611 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
612 ), precision("total_advance"));
613
614 this.calculate_outstanding_amount();
615 }
616 },
Anand Doshi3543f302013-05-24 19:25:01 +0530617
618 _set_in_company_currency: function(item, print_field, base_field) {
619 // set values in base currency
620 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
621 precision(base_field, item));
622 },
Anand Doshibf3e54a2013-06-05 14:11:32 +0530623
624 get_terms: function() {
625 var me = this;
626 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530627 return this.frm.call({
Anand Doshibf3e54a2013-06-05 14:11:32 +0530628 method: "webnotes.client.get_value",
629 args: {
630 doctype: "Terms and Conditions",
631 fieldname: "terms",
632 filters: { name: this.frm.doc.tc_name },
633 },
634 });
635 }
636 },
Anand Doshi3543f302013-05-24 19:25:01 +0530637});