blob: e32fe1533eee741a90ffa0f15978abfa79c9a12d [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 }
Akhilesh Darjee78378272013-10-11 19:06:30 +0530178
Anand Doshi61a2f682013-06-21 17:55:31 +0530179 this.calculate_taxes_and_totals();
Anand Doshi3543f302013-05-24 19:25:01 +0530180 },
181
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530182 get_price_list_currency: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +0530183 var me = this;
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530184 var fieldname = buying_or_selling.toLowerCase() + "_price_list";
185 if(this.frm.doc[fieldname]) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530186 return this.frm.call({
Anand Doshi3543f302013-05-24 19:25:01 +0530187 method: "setup.utils.get_price_list_currency",
Nabin Hait48a0bd32013-07-23 15:31:39 +0530188 args: {
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530189 price_list: this.frm.doc[fieldname],
Nabin Hait48a0bd32013-07-23 15:31:39 +0530190 },
Anand Doshi3543f302013-05-24 19:25:01 +0530191 callback: function(r) {
192 if(!r.exc) {
193 me.price_list_currency();
194 }
195 }
196 });
197 }
198 },
199
Anand Doshi61a2f682013-06-21 17:55:31 +0530200 get_exchange_rate: function(from_currency, to_currency, callback) {
201 var exchange_name = from_currency + "-" + to_currency;
202 wn.model.with_doc("Currency Exchange", exchange_name, function(name) {
203 var exchange_doc = wn.model.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530204 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530205 });
206 },
207
Anand Doshi3543f302013-05-24 19:25:01 +0530208 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530209 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530210 this.set_dynamic_labels();
Anand Doshi61a2f682013-06-21 17:55:31 +0530211
212 var company_currency = this.get_company_currency();
213 if(this.frm.doc.price_list_currency !== company_currency) {
214 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
215 function(exchange_rate) {
216 if(exchange_rate) {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530217 me.frm.set_value("plc_conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530218 me.plc_conversion_rate();
219 }
220 });
221 } else {
222 this.plc_conversion_rate();
223 }
Anand Doshi3543f302013-05-24 19:25:01 +0530224 },
225
226 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530227 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
228 this.frm.set_value("plc_conversion_rate", 1.0);
229 } else if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
230 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
231 this.calculate_taxes_and_totals();
232 }
Anand Doshi3543f302013-05-24 19:25:01 +0530233 },
234
235 qty: function(doc, cdt, cdn) {
236 this.calculate_taxes_and_totals();
237 },
238
Anand Doshi923d41d2013-05-28 17:23:36 +0530239 tax_rate: function(doc, cdt, cdn) {
240 this.calculate_taxes_and_totals();
241 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530242
Anand Doshi923d41d2013-05-28 17:23:36 +0530243 row_id: function(doc, cdt, cdn) {
244 var tax = wn.model.get_doc(cdt, cdn);
245 try {
246 this.validate_on_previous_row(tax);
247 this.calculate_taxes_and_totals();
248 } catch(e) {
249 tax.row_id = null;
250 refresh_field("row_id", tax.name, tax.parentfield);
251 throw e;
252 }
253 },
254
Anand Doshi61a2f682013-06-21 17:55:31 +0530255 set_dynamic_labels: function() {
256 // What TODO? should we make price list system non-mandatory?
257 this.frm.toggle_reqd("plc_conversion_rate",
258 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
259
260 var company_currency = this.get_company_currency();
261 this.change_form_labels(company_currency);
262 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530263 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530264 },
265
Anand Doshi923d41d2013-05-28 17:23:36 +0530266 recalculate: function() {
267 this.calculate_taxes_and_totals();
268 },
269
270 recalculate_values: function() {
271 this.calculate_taxes_and_totals();
272 },
273
Anand Doshid0b00722013-05-28 18:54:48 +0530274 calculate_charges: function() {
275 this.calculate_taxes_and_totals();
276 },
277
Anand Doshi3543f302013-05-24 19:25:01 +0530278 included_in_print_rate: function(doc, cdt, cdn) {
279 var tax = wn.model.get_doc(cdt, cdn);
280 try {
281 this.validate_on_previous_row(tax);
282 this.validate_inclusive_tax(tax);
283 this.calculate_taxes_and_totals();
284 } catch(e) {
285 tax.included_in_print_rate = 0;
286 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
287 throw e;
288 }
289 },
290
291 validate_on_previous_row: function(tax) {
292 // validate if a valid row id is mentioned in case of
293 // On Previous Row Amount and On Previous Row Total
294 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
295 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
296 var msg = repl(wn._("Row") + " # %(idx)s [%(doctype)s]: " +
297 wn._("Please specify a valid") + " %(row_id_label)s", {
298 idx: tax.idx,
299 doctype: tax.doctype,
300 row_id_label: wn.meta.get_label(tax.doctype, "row_id", tax.name)
301 });
302 msgprint(msg);
303 throw msg;
304 }
305 },
306
307 validate_inclusive_tax: function(tax) {
308 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
309
310 var actual_type_error = function() {
311 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
312 "%(charge_type_label)s = \"%(charge_type)s\" " +
313 wn._("cannot be included in Item's rate"), {
314 idx: tax.idx,
315 doctype: tax.doctype,
316 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
317 charge_type: tax.charge_type
318 });
319 msgprint(msg);
320 throw msg;
321 };
322
323 var on_previous_row_error = function(row_range) {
324 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
325 wn._("to be included in Item's rate, it is required that: ") +
326 " [" + wn._("Row") + " # %(row_range)s] " + wn._("also be included in Item's rate"), {
327 idx: tax.idx,
328 doctype: tax.doctype,
329 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
330 charge_type: tax.charge_type,
331 inclusive_label: wn.meta.get_label(tax.doctype, "included_in_print_rate", tax.name),
332 row_range: row_range,
333 });
334
335 msgprint(msg);
336 throw msg;
337 };
338
339 if(cint(tax.included_in_print_rate)) {
340 if(tax.charge_type == "Actual") {
341 // inclusive tax cannot be of type Actual
342 actual_type_error();
343 } else if(tax.charge_type == "On Previous Row Amount" &&
344 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
345 // referred row should also be an inclusive tax
346 on_previous_row_error(tax.row_id);
347 } else if(tax.charge_type == "On Previous Row Total") {
348 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
349 function(t) { return cint(t.included_in_print_rate) ? null : t; });
350 if(taxes_not_included.length > 0) {
351 // all rows above this tax should be inclusive
352 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
353 }
354 }
355 }
356 },
357
358 _load_item_tax_rate: function(item_tax_rate) {
359 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
360 },
361
362 _get_tax_rate: function(tax, item_tax_map) {
363 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
364 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
365 tax.rate;
366 },
367
368 get_item_wise_taxes_html: function() {
369 var item_tax = {};
370 var tax_accounts = [];
371 var company_currency = this.get_company_currency();
372
373 $.each(this.get_tax_doclist(), function(i, tax) {
374 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530375 var tax_rate_precision = precision("rate", tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530376 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530377 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530378 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530379 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530380 var tax_rate = "";
381 if(tax_data[0] != null) {
382 tax_rate = (tax.charge_type === "Actual") ?
383 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
384 (flt(tax_data[0], tax_rate_precision) + "%");
385 }
386 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
387 tax_amount_precision);
Anand Doshi53b73422013-05-31 11:50:01 +0530388
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530389 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530390 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530391 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530392 }
Anand Doshi3543f302013-05-24 19:25:01 +0530393 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530394 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530395 });
396
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530397 var headings = $.map([wn._("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530398 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Anand Doshi179e3772013-09-05 16:53:57 +0530399
400 var distinct_item_names = [];
401 var distinct_items = [];
402 $.each(this.get_item_doclist(), function(i, item) {
403 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
404 distinct_item_names.push(item.item_code || item.item_name);
405 distinct_items.push(item);
406 }
407 });
Anand Doshi3543f302013-05-24 19:25:01 +0530408
Anand Doshi179e3772013-09-05 16:53:57 +0530409 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530410 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530411 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530412 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
413 item_name: item.item_name,
414 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530415 return item_tax_record[head[0]] ?
416 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530417 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530418 }).join("\n")
419 });
420 }).join("\n");
421
Anand Doshi53b73422013-05-31 11:50:01 +0530422 if(!rows) return "";
Anand Doshi3543f302013-05-24 19:25:01 +0530423 return '<div style="overflow-x: scroll;"><table class="table table-bordered table-hover">\
424 <thead><tr>' + headings + '</tr></thead> \
425 <tbody>' + rows + '</tbody> \
426 </table></div>';
427 },
428
Anand Doshi923d41d2013-05-28 17:23:36 +0530429 _validate_before_fetch: function(fieldname) {
430 var me = this;
431 if(!me.frm.doc[fieldname]) {
432 return (wn._("Please specify") + ": " +
433 wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
434 ". " + wn._("It is needed to fetch Item Details."));
435 }
436 return null;
437 },
438
Anand Doshi3543f302013-05-24 19:25:01 +0530439 validate_company_and_party: function(party_field) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530440 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530441 var valid = true;
Anand Doshi923d41d2013-05-28 17:23:36 +0530442 var msg = "";
Anand Doshi3543f302013-05-24 19:25:01 +0530443 $.each(["company", party_field], function(i, fieldname) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530444 var msg_for_fieldname = me._validate_before_fetch(fieldname);
445 if(msg_for_fieldname) {
446 msgprint(msg_for_fieldname);
Anand Doshi3543f302013-05-24 19:25:01 +0530447 valid = false;
Anand Doshi3543f302013-05-24 19:25:01 +0530448 }
449 });
450 return valid;
451 },
452
453 get_item_doclist: function() {
454 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
455 {parentfield: this.fname});
456 },
457
458 get_tax_doclist: function() {
459 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
460 {parentfield: this.other_fname});
461 },
462
463 validate_conversion_rate: function() {
464 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
465 var conversion_rate_label = wn.meta.get_label(this.frm.doc.doctype, "conversion_rate",
466 this.frm.doc.name);
467
468 if(this.frm.doc.conversion_rate == 0) {
469 wn.throw(wn._(conversion_rate_label) + " " + wn._("cannot be 0"));
470 }
471
472 var company_currency = this.get_company_currency();
Anand Doshi3543f302013-05-24 19:25:01 +0530473
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530474 if(!this.frm.doc.conversion_rate) {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530475 wn.throw(wn._("Please enter valid") + " " + wn._(conversion_rate_label) +
476 " 1 " + this.frm.doc.currency + " = [?] " + company_currency);
477 }
Anand Doshi3543f302013-05-24 19:25:01 +0530478 },
479
480 calculate_taxes_and_totals: function() {
481 this.validate_conversion_rate();
482 this.frm.item_doclist = this.get_item_doclist();
483 this.frm.tax_doclist = this.get_tax_doclist();
484
485 this.calculate_item_values();
486 this.initialize_taxes();
487 this.determine_exclusive_rate && this.determine_exclusive_rate();
488 this.calculate_net_total();
489 this.calculate_taxes();
490 this.calculate_totals();
491 this._cleanup();
Anand Doshi923d41d2013-05-28 17:23:36 +0530492
Anand Doshi3543f302013-05-24 19:25:01 +0530493 this.show_item_wise_taxes();
494 },
495
496 initialize_taxes: function() {
497 var me = this;
498 $.each(this.frm.tax_doclist, function(i, tax) {
499 tax.item_wise_tax_detail = {};
500 $.each(["tax_amount", "total",
501 "tax_amount_for_current_item", "grand_total_for_current_item",
502 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
503 function(i, fieldname) { tax[fieldname] = 0.0 });
504
505 me.validate_on_previous_row(tax);
506 me.validate_inclusive_tax(tax);
507 wn.model.round_floats_in(tax);
508 });
509 },
510
511 calculate_taxes: function() {
512 var me = this;
513
514 $.each(this.frm.item_doclist, function(n, item) {
515 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
516
517 $.each(me.frm.tax_doclist, function(i, tax) {
518 // tax_amount represents the amount of tax for the current step
519 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530520
Anand Doshi3543f302013-05-24 19:25:01 +0530521 me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
522
523 // case when net total is 0 but there is an actual type charge
524 // in this case add the actual amount to tax.tax_amount
525 // and tax.grand_total_for_current_item for the first such iteration
526 if(tax.charge_type == "Actual" &&
527 !(current_tax_amount || me.frm.doc.net_total || tax.tax_amount)) {
528 var zero_net_total_adjustment = flt(tax.rate, precision("tax_amount", tax));
529 current_tax_amount += zero_net_total_adjustment;
530 }
531
532 // store tax_amount for current item as it will be used for
533 // charge type = 'On Previous Row Amount'
534 tax.tax_amount_for_current_item = current_tax_amount;
535
536 // accumulate tax amount into tax.tax_amount
537 tax.tax_amount += current_tax_amount;
538
Anand Doshi3543f302013-05-24 19:25:01 +0530539 // for buying
540 if(tax.category) {
541 // if just for valuation, do not add the tax amount in total
542 // hence, setting it as 0 for further steps
543 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
544
545 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
546 }
547
548 // Calculate tax.total viz. grand total till that step
549 // note: grand_total_for_current_item contains the contribution of
550 // item's amount, previously applied tax and the current tax on that item
551 if(i==0) {
552 tax.grand_total_for_current_item = flt(item.amount + current_tax_amount,
553 precision("total", tax));
554 } else {
555 tax.grand_total_for_current_item =
556 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
557 precision("total", tax));
558 }
559
560 // in tax.total, accumulate grand total for each item
561 tax.total += tax.grand_total_for_current_item;
562 });
563 });
564 },
565
566 get_current_tax_amount: function(item, tax, item_tax_map) {
567 var tax_rate = this._get_tax_rate(tax, item_tax_map);
568 var current_tax_amount = 0.0;
569
570 if(tax.charge_type == "Actual") {
571 // distribute the tax amount proportionally to each item row
572 var actual = flt(tax.rate, precision("tax_amount", tax));
573 current_tax_amount = this.frm.doc.net_total ?
574 ((item.amount / this.frm.doc.net_total) * actual) :
575 0.0;
576
577 } else if(tax.charge_type == "On Net Total") {
578 current_tax_amount = (tax_rate / 100.0) * item.amount;
579
580 } else if(tax.charge_type == "On Previous Row Amount") {
581 current_tax_amount = (tax_rate / 100.0) *
582 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
583
584 } else if(tax.charge_type == "On Previous Row Total") {
585 current_tax_amount = (tax_rate / 100.0) *
586 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
587
588 }
589
Anand Doshi53b73422013-05-31 11:50:01 +0530590 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
591
592 // store tax breakup for each item
593 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
594
595 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530596 },
597
598 _cleanup: function() {
599 $.each(this.frm.tax_doclist, function(i, tax) {
600 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
601 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
602 function(i, fieldname) { delete tax[fieldname]; });
603
604 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
605 });
606 },
Anand Doshifc777182013-05-27 19:29:07 +0530607
608 calculate_total_advance: function(parenttype, advance_parentfield) {
609 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
610 var advance_doclist = wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
611 {parentfield: advance_parentfield});
612 this.frm.doc.total_advance = flt(wn.utils.sum(
613 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
614 ), precision("total_advance"));
615
616 this.calculate_outstanding_amount();
617 }
618 },
Anand Doshi3543f302013-05-24 19:25:01 +0530619
620 _set_in_company_currency: function(item, print_field, base_field) {
621 // set values in base currency
622 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
623 precision(base_field, item));
624 },
Anand Doshibf3e54a2013-06-05 14:11:32 +0530625
626 get_terms: function() {
627 var me = this;
628 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530629 return this.frm.call({
Anand Doshibf3e54a2013-06-05 14:11:32 +0530630 method: "webnotes.client.get_value",
631 args: {
632 doctype: "Terms and Conditions",
633 fieldname: "terms",
634 filters: { name: this.frm.doc.tc_name },
635 },
636 });
637 }
638 },
Anand Doshi3543f302013-05-24 19:25:01 +0530639});