blob: dff13845456cf1f1669169b1f2c6d1bffb6358a8 [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);
Akhilesh Darjee61b5c592013-10-15 20:24:34 +053027 });
Anand Doshi3543f302013-05-24 19:25:01 +053028 }
Anand Doshi1e4fb682013-08-23 12:56:33 +053029
30 if(this.other_fname) {
Anand Doshia91c95f2013-08-23 13:00:47 +053031 this[this.other_fname + "_remove"] = this.calculate_taxes_and_totals;
32 }
33
34 if(this.fname) {
35 this[this.fname + "_remove"] = this.calculate_taxes_and_totals;
Anand Doshi1e4fb682013-08-23 12:56:33 +053036 }
Anand Doshi3543f302013-05-24 19:25:01 +053037 },
38
Anand Doshi5e4e5d72013-08-06 17:23:44 +053039 onload_post_render: function() {
Akhilesh Darjee61b5c592013-10-15 20:24:34 +053040 var me = this;
41 if(this.frm.doc.__islocal && this.frm.doc.company && !this.frm.doc.is_pos) {
42 if(!this.frm.doc.customer) {
Akhilesh Darjee78378272013-10-11 19:06:30 +053043 return this.frm.call({
44 doc: this.frm.doc,
45 method: "onload_post_render",
46 freeze: true,
47 callback: function(r) {
48 // remove this call when using client side mapper
49 me.set_default_values();
50 me.set_dynamic_labels();
Akhilesh Darjee61b5c592013-10-15 20:24:34 +053051 me.calculate_taxes_and_totals()
Akhilesh Darjee78378272013-10-11 19:06:30 +053052 }
53 });
Akhilesh Darjee61b5c592013-10-15 20:24:34 +053054 } else {
55 this.calculate_taxes_and_totals();
56 }
Anand Doshi5e4e5d72013-08-06 17:23:44 +053057 }
58 },
59
Anand Doshi3543f302013-05-24 19:25:01 +053060 refresh: function() {
61 this.frm.clear_custom_buttons();
62 erpnext.hide_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +053063 erpnext.hide_company();
Anand Doshi3543f302013-05-24 19:25:01 +053064 this.show_item_wise_taxes();
Nabin Hait1f996c32013-07-29 19:00:53 +053065 this.set_dynamic_labels();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053066
67 // Show POS button only if it is enabled from features setup
68 if(cint(sys_defaults.fs_pos_view)===1 && this.frm.doctype!="Material Request")
69 this.pos_btn();
70 },
71
72 pos_btn: function() {
73 if(this.$pos_btn)
74 this.$pos_btn.remove();
75
76 if(!this.pos_active) {
77 var btn_label = wn._("POS View"),
78 icon = "icon-desktop";
79 } else {
80 var btn_label = wn._(this.frm.doctype) + wn._(" View"),
81 icon = "icon-file-text";
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053082 }
83 var me = this;
84
85 this.$pos_btn = this.frm.add_custom_button(btn_label, function() {
86 me.toggle_pos();
87 me.pos_btn();
88 }, icon);
89 },
90
91 toggle_pos: function(show) {
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053092 // Check whether it is Selling or Buying cycle
93 var price_list = wn.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
94 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list;
95
96 if (!price_list)
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053097 msgprint(wn._("Please select Price List"))
98 else {
99 if((show===true && this.pos_active) || (show===false && !this.pos_active)) return;
100
101 // make pos
Akhilesh Darjeef624ffa2013-09-23 12:27:16 +0530102 if(!this.frm.pos) {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530103 this.frm.layout.add_view("pos");
104 this.frm.pos = new erpnext.POS(this.frm.layout.views.pos, this.frm);
105 }
106
107 // toggle view
108 this.frm.layout.set_view(this.pos_active ? "" : "pos");
109 this.pos_active = !this.pos_active;
110
111 // refresh
112 if(this.pos_active)
113 this.frm.pos.refresh();
Akhilesh Darjeef83576b2013-09-18 18:35:12 +0530114 this.frm.refresh();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530115 }
Anand Doshi3543f302013-05-24 19:25:01 +0530116 },
117
Anand Doshi923d41d2013-05-28 17:23:36 +0530118 validate: function() {
119 this.calculate_taxes_and_totals();
120 },
121
Anand Doshi5e4e5d72013-08-06 17:23:44 +0530122 set_default_values: function() {
123 $.each(wn.model.get_doclist(this.frm.doctype, this.frm.docname), function(i, doc) {
124 var updated = wn.model.set_default_values(doc);
125 if(doc.parentfield) {
126 refresh_field(doc.parentfield);
127 } else {
128 refresh_field(updated);
129 }
130 });
131 },
132
Anand Doshi3543f302013-05-24 19:25:01 +0530133 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +0530134 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530135 var company_currency = this.get_company_currency();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530136 if (!this.frm.doc.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530137 this.frm.set_value("currency", company_currency);
Anand Doshi17350b82013-08-01 15:45:23 +0530138 }
139
Akhilesh Darjee78378272013-10-11 19:06:30 +0530140 if (this.frm.doc.currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530141 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530142 }
143 if (this.frm.doc.price_list_currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530144 this.frm.set_value('plc_conversion_rate', 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530145 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530146
Nabin Hait48a0bd32013-07-23 15:31:39 +0530147 this.frm.script_manager.trigger("currency");
Anand Doshi3543f302013-05-24 19:25:01 +0530148 }
149 },
150
151 get_company_currency: function() {
152 return erpnext.get_currency(this.frm.doc.company);
153 },
154
155 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530156 var me = this;
157 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530158
Anand Doshi61a2f682013-06-21 17:55:31 +0530159 var company_currency = this.get_company_currency();
160 if(this.frm.doc.currency !== company_currency) {
161 this.get_exchange_rate(this.frm.doc.currency, company_currency,
162 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530163 me.frm.set_value("conversion_rate", exchange_rate);
164 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530165 });
166 } else {
167 this.conversion_rate();
168 }
169 },
170
171 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530172 if(this.frm.doc.currency === this.get_company_currency()) {
173 this.frm.set_value("conversion_rate", 1.0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530174 } else if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
175 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
176 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
177 }
Nabin Haitb338b6e2013-10-15 16:55:24 +0530178 if(flt(this.frm.doc.conversion_rate)>0.0) this.calculate_taxes_and_totals();
Anand Doshi3543f302013-05-24 19:25:01 +0530179 },
180
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530181 get_price_list_currency: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +0530182 var me = this;
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530183 var fieldname = buying_or_selling.toLowerCase() + "_price_list";
184 if(this.frm.doc[fieldname]) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530185 return this.frm.call({
Anand Doshi3543f302013-05-24 19:25:01 +0530186 method: "setup.utils.get_price_list_currency",
Nabin Hait48a0bd32013-07-23 15:31:39 +0530187 args: {
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530188 price_list: this.frm.doc[fieldname],
Nabin Hait48a0bd32013-07-23 15:31:39 +0530189 },
Anand Doshi3543f302013-05-24 19:25:01 +0530190 callback: function(r) {
191 if(!r.exc) {
192 me.price_list_currency();
193 }
194 }
195 });
196 }
197 },
198
Anand Doshi61a2f682013-06-21 17:55:31 +0530199 get_exchange_rate: function(from_currency, to_currency, callback) {
200 var exchange_name = from_currency + "-" + to_currency;
201 wn.model.with_doc("Currency Exchange", exchange_name, function(name) {
202 var exchange_doc = wn.model.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530203 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530204 });
205 },
206
Anand Doshi3543f302013-05-24 19:25:01 +0530207 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530208 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530209 this.set_dynamic_labels();
Anand Doshi61a2f682013-06-21 17:55:31 +0530210
211 var company_currency = this.get_company_currency();
212 if(this.frm.doc.price_list_currency !== company_currency) {
213 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
214 function(exchange_rate) {
215 if(exchange_rate) {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530216 me.frm.set_value("plc_conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530217 me.plc_conversion_rate();
218 }
219 });
220 } else {
221 this.plc_conversion_rate();
222 }
Anand Doshi3543f302013-05-24 19:25:01 +0530223 },
224
225 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530226 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
227 this.frm.set_value("plc_conversion_rate", 1.0);
228 } else if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
229 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
230 this.calculate_taxes_and_totals();
231 }
Anand Doshi3543f302013-05-24 19:25:01 +0530232 },
233
234 qty: function(doc, cdt, cdn) {
235 this.calculate_taxes_and_totals();
236 },
237
Anand Doshi923d41d2013-05-28 17:23:36 +0530238 tax_rate: function(doc, cdt, cdn) {
239 this.calculate_taxes_and_totals();
240 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530241
Anand Doshi923d41d2013-05-28 17:23:36 +0530242 row_id: function(doc, cdt, cdn) {
243 var tax = wn.model.get_doc(cdt, cdn);
244 try {
245 this.validate_on_previous_row(tax);
246 this.calculate_taxes_and_totals();
247 } catch(e) {
248 tax.row_id = null;
249 refresh_field("row_id", tax.name, tax.parentfield);
250 throw e;
251 }
252 },
253
Anand Doshi61a2f682013-06-21 17:55:31 +0530254 set_dynamic_labels: function() {
255 // What TODO? should we make price list system non-mandatory?
256 this.frm.toggle_reqd("plc_conversion_rate",
257 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
258
259 var company_currency = this.get_company_currency();
260 this.change_form_labels(company_currency);
261 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530262 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530263 },
264
Anand Doshi923d41d2013-05-28 17:23:36 +0530265 recalculate: function() {
266 this.calculate_taxes_and_totals();
267 },
268
269 recalculate_values: function() {
270 this.calculate_taxes_and_totals();
271 },
272
Anand Doshid0b00722013-05-28 18:54:48 +0530273 calculate_charges: function() {
274 this.calculate_taxes_and_totals();
275 },
276
Anand Doshi3543f302013-05-24 19:25:01 +0530277 included_in_print_rate: function(doc, cdt, cdn) {
278 var tax = wn.model.get_doc(cdt, cdn);
279 try {
280 this.validate_on_previous_row(tax);
281 this.validate_inclusive_tax(tax);
282 this.calculate_taxes_and_totals();
283 } catch(e) {
284 tax.included_in_print_rate = 0;
285 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
286 throw e;
287 }
288 },
289
290 validate_on_previous_row: function(tax) {
291 // validate if a valid row id is mentioned in case of
292 // On Previous Row Amount and On Previous Row Total
293 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
294 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
295 var msg = repl(wn._("Row") + " # %(idx)s [%(doctype)s]: " +
296 wn._("Please specify a valid") + " %(row_id_label)s", {
297 idx: tax.idx,
298 doctype: tax.doctype,
299 row_id_label: wn.meta.get_label(tax.doctype, "row_id", tax.name)
300 });
301 msgprint(msg);
302 throw msg;
303 }
304 },
305
306 validate_inclusive_tax: function(tax) {
307 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
308
309 var actual_type_error = function() {
310 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
311 "%(charge_type_label)s = \"%(charge_type)s\" " +
312 wn._("cannot be included in Item's rate"), {
313 idx: tax.idx,
314 doctype: tax.doctype,
315 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
316 charge_type: tax.charge_type
317 });
318 msgprint(msg);
319 throw msg;
320 };
321
322 var on_previous_row_error = function(row_range) {
323 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
324 wn._("to be included in Item's rate, it is required that: ") +
325 " [" + wn._("Row") + " # %(row_range)s] " + wn._("also be included in Item's rate"), {
326 idx: tax.idx,
327 doctype: tax.doctype,
328 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
329 charge_type: tax.charge_type,
330 inclusive_label: wn.meta.get_label(tax.doctype, "included_in_print_rate", tax.name),
331 row_range: row_range,
332 });
333
334 msgprint(msg);
335 throw msg;
336 };
337
338 if(cint(tax.included_in_print_rate)) {
339 if(tax.charge_type == "Actual") {
340 // inclusive tax cannot be of type Actual
341 actual_type_error();
342 } else if(tax.charge_type == "On Previous Row Amount" &&
343 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
344 // referred row should also be an inclusive tax
345 on_previous_row_error(tax.row_id);
346 } else if(tax.charge_type == "On Previous Row Total") {
347 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
348 function(t) { return cint(t.included_in_print_rate) ? null : t; });
349 if(taxes_not_included.length > 0) {
350 // all rows above this tax should be inclusive
351 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
352 }
353 }
354 }
355 },
356
357 _load_item_tax_rate: function(item_tax_rate) {
358 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
359 },
360
361 _get_tax_rate: function(tax, item_tax_map) {
362 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
363 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
364 tax.rate;
365 },
366
367 get_item_wise_taxes_html: function() {
368 var item_tax = {};
369 var tax_accounts = [];
370 var company_currency = this.get_company_currency();
371
372 $.each(this.get_tax_doclist(), function(i, tax) {
373 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530374 var tax_rate_precision = precision("rate", tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530375 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530376 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530377 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530378 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530379 var tax_rate = "";
380 if(tax_data[0] != null) {
381 tax_rate = (tax.charge_type === "Actual") ?
382 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
383 (flt(tax_data[0], tax_rate_precision) + "%");
384 }
385 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
386 tax_amount_precision);
Anand Doshi53b73422013-05-31 11:50:01 +0530387
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530388 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530389 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530390 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530391 }
Anand Doshi3543f302013-05-24 19:25:01 +0530392 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530393 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530394 });
395
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530396 var headings = $.map([wn._("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530397 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Anand Doshi179e3772013-09-05 16:53:57 +0530398
399 var distinct_item_names = [];
400 var distinct_items = [];
401 $.each(this.get_item_doclist(), function(i, item) {
402 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
403 distinct_item_names.push(item.item_code || item.item_name);
404 distinct_items.push(item);
405 }
406 });
Anand Doshi3543f302013-05-24 19:25:01 +0530407
Anand Doshi179e3772013-09-05 16:53:57 +0530408 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530409 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530410 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530411 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
412 item_name: item.item_name,
413 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530414 return item_tax_record[head[0]] ?
415 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530416 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530417 }).join("\n")
418 });
419 }).join("\n");
420
Anand Doshi53b73422013-05-31 11:50:01 +0530421 if(!rows) return "";
Anand Doshi3543f302013-05-24 19:25:01 +0530422 return '<div style="overflow-x: scroll;"><table class="table table-bordered table-hover">\
423 <thead><tr>' + headings + '</tr></thead> \
424 <tbody>' + rows + '</tbody> \
425 </table></div>';
426 },
427
Anand Doshi923d41d2013-05-28 17:23:36 +0530428 _validate_before_fetch: function(fieldname) {
429 var me = this;
430 if(!me.frm.doc[fieldname]) {
431 return (wn._("Please specify") + ": " +
432 wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
433 ". " + wn._("It is needed to fetch Item Details."));
434 }
435 return null;
436 },
437
Anand Doshi3543f302013-05-24 19:25:01 +0530438 validate_company_and_party: function(party_field) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530439 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530440 var valid = true;
Anand Doshi923d41d2013-05-28 17:23:36 +0530441 var msg = "";
Anand Doshi3543f302013-05-24 19:25:01 +0530442 $.each(["company", party_field], function(i, fieldname) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530443 var msg_for_fieldname = me._validate_before_fetch(fieldname);
444 if(msg_for_fieldname) {
445 msgprint(msg_for_fieldname);
Anand Doshi3543f302013-05-24 19:25:01 +0530446 valid = false;
Anand Doshi3543f302013-05-24 19:25:01 +0530447 }
448 });
449 return valid;
450 },
451
452 get_item_doclist: function() {
453 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
454 {parentfield: this.fname});
455 },
456
457 get_tax_doclist: function() {
458 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
459 {parentfield: this.other_fname});
460 },
461
462 validate_conversion_rate: function() {
463 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
464 var conversion_rate_label = wn.meta.get_label(this.frm.doc.doctype, "conversion_rate",
465 this.frm.doc.name);
466
467 if(this.frm.doc.conversion_rate == 0) {
468 wn.throw(wn._(conversion_rate_label) + " " + wn._("cannot be 0"));
469 }
470
471 var company_currency = this.get_company_currency();
Anand Doshi3543f302013-05-24 19:25:01 +0530472
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530473 if(!this.frm.doc.conversion_rate) {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530474 wn.throw(wn._("Please enter valid") + " " + wn._(conversion_rate_label) +
475 " 1 " + this.frm.doc.currency + " = [?] " + company_currency);
476 }
Anand Doshi3543f302013-05-24 19:25:01 +0530477 },
478
479 calculate_taxes_and_totals: function() {
480 this.validate_conversion_rate();
481 this.frm.item_doclist = this.get_item_doclist();
482 this.frm.tax_doclist = this.get_tax_doclist();
483
484 this.calculate_item_values();
485 this.initialize_taxes();
486 this.determine_exclusive_rate && this.determine_exclusive_rate();
487 this.calculate_net_total();
488 this.calculate_taxes();
489 this.calculate_totals();
490 this._cleanup();
Anand Doshi923d41d2013-05-28 17:23:36 +0530491
Anand Doshi3543f302013-05-24 19:25:01 +0530492 this.show_item_wise_taxes();
493 },
494
495 initialize_taxes: function() {
496 var me = this;
497 $.each(this.frm.tax_doclist, function(i, tax) {
498 tax.item_wise_tax_detail = {};
499 $.each(["tax_amount", "total",
500 "tax_amount_for_current_item", "grand_total_for_current_item",
501 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
502 function(i, fieldname) { tax[fieldname] = 0.0 });
503
504 me.validate_on_previous_row(tax);
505 me.validate_inclusive_tax(tax);
506 wn.model.round_floats_in(tax);
507 });
508 },
509
510 calculate_taxes: function() {
511 var me = this;
512
513 $.each(this.frm.item_doclist, function(n, item) {
514 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
515
516 $.each(me.frm.tax_doclist, function(i, tax) {
517 // tax_amount represents the amount of tax for the current step
518 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530519
Anand Doshi3543f302013-05-24 19:25:01 +0530520 me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
521
522 // case when net total is 0 but there is an actual type charge
523 // in this case add the actual amount to tax.tax_amount
524 // and tax.grand_total_for_current_item for the first such iteration
525 if(tax.charge_type == "Actual" &&
526 !(current_tax_amount || me.frm.doc.net_total || tax.tax_amount)) {
527 var zero_net_total_adjustment = flt(tax.rate, precision("tax_amount", tax));
528 current_tax_amount += zero_net_total_adjustment;
529 }
530
531 // store tax_amount for current item as it will be used for
532 // charge type = 'On Previous Row Amount'
533 tax.tax_amount_for_current_item = current_tax_amount;
534
535 // accumulate tax amount into tax.tax_amount
536 tax.tax_amount += current_tax_amount;
537
Anand Doshi3543f302013-05-24 19:25:01 +0530538 // for buying
539 if(tax.category) {
540 // if just for valuation, do not add the tax amount in total
541 // hence, setting it as 0 for further steps
542 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
543
544 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
545 }
546
547 // Calculate tax.total viz. grand total till that step
548 // note: grand_total_for_current_item contains the contribution of
549 // item's amount, previously applied tax and the current tax on that item
550 if(i==0) {
551 tax.grand_total_for_current_item = flt(item.amount + current_tax_amount,
552 precision("total", tax));
553 } else {
554 tax.grand_total_for_current_item =
555 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
556 precision("total", tax));
557 }
558
559 // in tax.total, accumulate grand total for each item
560 tax.total += tax.grand_total_for_current_item;
561 });
562 });
563 },
564
565 get_current_tax_amount: function(item, tax, item_tax_map) {
566 var tax_rate = this._get_tax_rate(tax, item_tax_map);
567 var current_tax_amount = 0.0;
568
569 if(tax.charge_type == "Actual") {
570 // distribute the tax amount proportionally to each item row
571 var actual = flt(tax.rate, precision("tax_amount", tax));
572 current_tax_amount = this.frm.doc.net_total ?
573 ((item.amount / this.frm.doc.net_total) * actual) :
574 0.0;
575
576 } else if(tax.charge_type == "On Net Total") {
577 current_tax_amount = (tax_rate / 100.0) * item.amount;
578
579 } else if(tax.charge_type == "On Previous Row Amount") {
580 current_tax_amount = (tax_rate / 100.0) *
581 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
582
583 } else if(tax.charge_type == "On Previous Row Total") {
584 current_tax_amount = (tax_rate / 100.0) *
585 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
586
587 }
588
Anand Doshi53b73422013-05-31 11:50:01 +0530589 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
590
591 // store tax breakup for each item
592 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
593
594 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530595 },
596
597 _cleanup: function() {
598 $.each(this.frm.tax_doclist, function(i, tax) {
599 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
600 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
601 function(i, fieldname) { delete tax[fieldname]; });
602
603 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
604 });
605 },
Anand Doshifc777182013-05-27 19:29:07 +0530606
607 calculate_total_advance: function(parenttype, advance_parentfield) {
608 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
609 var advance_doclist = wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
610 {parentfield: advance_parentfield});
611 this.frm.doc.total_advance = flt(wn.utils.sum(
612 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
613 ), precision("total_advance"));
614
615 this.calculate_outstanding_amount();
616 }
617 },
Anand Doshi3543f302013-05-24 19:25:01 +0530618
619 _set_in_company_currency: function(item, print_field, base_field) {
620 // set values in base currency
621 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
622 precision(base_field, item));
623 },
Anand Doshibf3e54a2013-06-05 14:11:32 +0530624
625 get_terms: function() {
626 var me = this;
627 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530628 return this.frm.call({
Anand Doshibf3e54a2013-06-05 14:11:32 +0530629 method: "webnotes.client.get_value",
630 args: {
631 doctype: "Terms and Conditions",
632 fieldname: "terms",
633 filters: { name: this.frm.doc.tc_name },
634 },
635 });
636 }
637 },
Anand Doshi3543f302013-05-24 19:25:01 +0530638});