blob: dc4698f7a9f9a235a3459f48b608dd6ecefe01cd [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 }
Akhilesh Darjee564c6212013-10-17 11:31:11 +053029
Anand Doshi1e4fb682013-08-23 12:56:33 +053030 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) {
Akhilesh Darjee04638a52013-10-17 13:19:46 +053042 if(!this.frm.doc.customer || !this.frm.doc.supplier) {
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 Darjeeacf65a72013-10-17 13:04:26 +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 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530117
118 serial_no: function(doc, cdt, cdn) {
119 var me = this;
120 var item = wn.model.get_doc(cdt, cdn);
121
122 if (item.serial_no) {
123 if (!item.item_code) {
124 this.frm.script_manager.trigger("item_code", cdt, cdn);
125 }
126 else {
127 var sr_no = [];
128
129 // Replacing all occurences of comma with carriage return
130 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
131
132 serial_nos = serial_nos.trim().split('\n');
133
134 // Trim each string and push unique string to new list
135 for (var x=0; x<=serial_nos.length - 1; x++) {
136 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
137 sr_no.push(serial_nos[x].trim());
138 }
139 }
140
141 // Add the new list to the serial no. field in grid with each in new line
142 item.serial_no = "";
143 for (var x=0; x<=sr_no.length - 1; x++)
144 item.serial_no += sr_no[x] + '\n';
145
146 refresh_field("serial_no", item.name, item.parentfield);
147 wn.model.set_value(item.doctype, item.name, "qty", sr_no.length);
148 }
149 }
150 },
Anand Doshi3543f302013-05-24 19:25:01 +0530151
Anand Doshi923d41d2013-05-28 17:23:36 +0530152 validate: function() {
153 this.calculate_taxes_and_totals();
154 },
155
Anand Doshi5e4e5d72013-08-06 17:23:44 +0530156 set_default_values: function() {
157 $.each(wn.model.get_doclist(this.frm.doctype, this.frm.docname), function(i, doc) {
158 var updated = wn.model.set_default_values(doc);
159 if(doc.parentfield) {
160 refresh_field(doc.parentfield);
161 } else {
162 refresh_field(updated);
163 }
164 });
165 },
166
Anand Doshi3543f302013-05-24 19:25:01 +0530167 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +0530168 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530169 var company_currency = this.get_company_currency();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530170 if (!this.frm.doc.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530171 this.frm.set_value("currency", company_currency);
Anand Doshi17350b82013-08-01 15:45:23 +0530172 }
173
Akhilesh Darjee78378272013-10-11 19:06:30 +0530174 if (this.frm.doc.currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530175 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530176 }
177 if (this.frm.doc.price_list_currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530178 this.frm.set_value('plc_conversion_rate', 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530179 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530180
Nabin Hait48a0bd32013-07-23 15:31:39 +0530181 this.frm.script_manager.trigger("currency");
Anand Doshi3543f302013-05-24 19:25:01 +0530182 }
183 },
184
185 get_company_currency: function() {
186 return erpnext.get_currency(this.frm.doc.company);
187 },
188
189 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530190 var me = this;
191 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530192
Anand Doshi61a2f682013-06-21 17:55:31 +0530193 var company_currency = this.get_company_currency();
194 if(this.frm.doc.currency !== company_currency) {
195 this.get_exchange_rate(this.frm.doc.currency, company_currency,
196 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530197 me.frm.set_value("conversion_rate", exchange_rate);
198 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530199 });
200 } else {
201 this.conversion_rate();
202 }
203 },
204
205 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530206 if(this.frm.doc.currency === this.get_company_currency()) {
207 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530208 }
209 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530210 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
211 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
212 }
Nabin Haitb338b6e2013-10-15 16:55:24 +0530213 if(flt(this.frm.doc.conversion_rate)>0.0) this.calculate_taxes_and_totals();
Anand Doshi3543f302013-05-24 19:25:01 +0530214 },
215
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530216 get_price_list_currency: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +0530217 var me = this;
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530218 var fieldname = buying_or_selling.toLowerCase() + "_price_list";
219 if(this.frm.doc[fieldname]) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530220 return this.frm.call({
Anand Doshi3543f302013-05-24 19:25:01 +0530221 method: "setup.utils.get_price_list_currency",
Nabin Hait48a0bd32013-07-23 15:31:39 +0530222 args: {
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530223 price_list: this.frm.doc[fieldname],
Nabin Hait48a0bd32013-07-23 15:31:39 +0530224 },
Anand Doshi3543f302013-05-24 19:25:01 +0530225 callback: function(r) {
226 if(!r.exc) {
227 me.price_list_currency();
228 }
229 }
230 });
231 }
232 },
233
Anand Doshi61a2f682013-06-21 17:55:31 +0530234 get_exchange_rate: function(from_currency, to_currency, callback) {
235 var exchange_name = from_currency + "-" + to_currency;
236 wn.model.with_doc("Currency Exchange", exchange_name, function(name) {
237 var exchange_doc = wn.model.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530238 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530239 });
240 },
241
Anand Doshi3543f302013-05-24 19:25:01 +0530242 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530243 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530244 this.set_dynamic_labels();
Anand Doshi61a2f682013-06-21 17:55:31 +0530245
246 var company_currency = this.get_company_currency();
247 if(this.frm.doc.price_list_currency !== company_currency) {
248 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
249 function(exchange_rate) {
250 if(exchange_rate) {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530251 me.frm.set_value("plc_conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530252 me.plc_conversion_rate();
253 }
254 });
255 } else {
256 this.plc_conversion_rate();
257 }
Anand Doshi3543f302013-05-24 19:25:01 +0530258 },
259
260 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530261 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
262 this.frm.set_value("plc_conversion_rate", 1.0);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530263 }
264 if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
Anand Doshi61a2f682013-06-21 17:55:31 +0530265 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
266 this.calculate_taxes_and_totals();
267 }
Anand Doshi3543f302013-05-24 19:25:01 +0530268 },
269
270 qty: function(doc, cdt, cdn) {
271 this.calculate_taxes_and_totals();
272 },
273
Anand Doshi923d41d2013-05-28 17:23:36 +0530274 tax_rate: function(doc, cdt, cdn) {
275 this.calculate_taxes_and_totals();
276 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530277
Anand Doshi923d41d2013-05-28 17:23:36 +0530278 row_id: function(doc, cdt, cdn) {
279 var tax = wn.model.get_doc(cdt, cdn);
280 try {
281 this.validate_on_previous_row(tax);
282 this.calculate_taxes_and_totals();
283 } catch(e) {
284 tax.row_id = null;
285 refresh_field("row_id", tax.name, tax.parentfield);
286 throw e;
287 }
288 },
289
Anand Doshi61a2f682013-06-21 17:55:31 +0530290 set_dynamic_labels: function() {
291 // What TODO? should we make price list system non-mandatory?
292 this.frm.toggle_reqd("plc_conversion_rate",
293 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
294
295 var company_currency = this.get_company_currency();
296 this.change_form_labels(company_currency);
297 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530298 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530299 },
300
Anand Doshi923d41d2013-05-28 17:23:36 +0530301 recalculate: function() {
302 this.calculate_taxes_and_totals();
303 },
304
305 recalculate_values: function() {
306 this.calculate_taxes_and_totals();
307 },
308
Anand Doshid0b00722013-05-28 18:54:48 +0530309 calculate_charges: function() {
310 this.calculate_taxes_and_totals();
311 },
312
Anand Doshi3543f302013-05-24 19:25:01 +0530313 included_in_print_rate: function(doc, cdt, cdn) {
314 var tax = wn.model.get_doc(cdt, cdn);
315 try {
316 this.validate_on_previous_row(tax);
317 this.validate_inclusive_tax(tax);
318 this.calculate_taxes_and_totals();
319 } catch(e) {
320 tax.included_in_print_rate = 0;
321 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
322 throw e;
323 }
324 },
325
326 validate_on_previous_row: function(tax) {
327 // validate if a valid row id is mentioned in case of
328 // On Previous Row Amount and On Previous Row Total
Bárbara Perretti534af0b2013-10-08 15:00:07 -0300329 if(([wn._("On Previous Row Amount"), wn._("On Previous Row Total")].indexOf(tax.charge_type) != -1) &&
Anand Doshi3543f302013-05-24 19:25:01 +0530330 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
331 var msg = repl(wn._("Row") + " # %(idx)s [%(doctype)s]: " +
332 wn._("Please specify a valid") + " %(row_id_label)s", {
333 idx: tax.idx,
334 doctype: tax.doctype,
335 row_id_label: wn.meta.get_label(tax.doctype, "row_id", tax.name)
336 });
337 msgprint(msg);
338 throw msg;
339 }
340 },
341
342 validate_inclusive_tax: function(tax) {
343 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
344
345 var actual_type_error = function() {
346 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
347 "%(charge_type_label)s = \"%(charge_type)s\" " +
348 wn._("cannot be included in Item's rate"), {
349 idx: tax.idx,
350 doctype: tax.doctype,
351 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
352 charge_type: tax.charge_type
353 });
354 msgprint(msg);
355 throw msg;
356 };
357
358 var on_previous_row_error = function(row_range) {
359 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
360 wn._("to be included in Item's rate, it is required that: ") +
361 " [" + wn._("Row") + " # %(row_range)s] " + wn._("also be included in Item's rate"), {
362 idx: tax.idx,
363 doctype: tax.doctype,
364 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
365 charge_type: tax.charge_type,
366 inclusive_label: wn.meta.get_label(tax.doctype, "included_in_print_rate", tax.name),
367 row_range: row_range,
368 });
369
370 msgprint(msg);
371 throw msg;
372 };
373
374 if(cint(tax.included_in_print_rate)) {
375 if(tax.charge_type == "Actual") {
376 // inclusive tax cannot be of type Actual
377 actual_type_error();
378 } else if(tax.charge_type == "On Previous Row Amount" &&
379 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
380 // referred row should also be an inclusive tax
381 on_previous_row_error(tax.row_id);
382 } else if(tax.charge_type == "On Previous Row Total") {
383 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
384 function(t) { return cint(t.included_in_print_rate) ? null : t; });
385 if(taxes_not_included.length > 0) {
386 // all rows above this tax should be inclusive
387 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
388 }
389 }
390 }
391 },
392
393 _load_item_tax_rate: function(item_tax_rate) {
394 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
395 },
396
397 _get_tax_rate: function(tax, item_tax_map) {
398 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
399 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
400 tax.rate;
401 },
402
403 get_item_wise_taxes_html: function() {
404 var item_tax = {};
405 var tax_accounts = [];
406 var company_currency = this.get_company_currency();
407
408 $.each(this.get_tax_doclist(), function(i, tax) {
409 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530410 var tax_rate_precision = precision("rate", tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530411 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530412 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530413 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530414 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530415 var tax_rate = "";
416 if(tax_data[0] != null) {
417 tax_rate = (tax.charge_type === "Actual") ?
418 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
419 (flt(tax_data[0], tax_rate_precision) + "%");
420 }
421 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
422 tax_amount_precision);
Anand Doshi53b73422013-05-31 11:50:01 +0530423
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530424 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530425 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530426 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530427 }
Anand Doshi3543f302013-05-24 19:25:01 +0530428 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530429 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530430 });
431
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530432 var headings = $.map([wn._("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530433 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Anand Doshi179e3772013-09-05 16:53:57 +0530434
435 var distinct_item_names = [];
436 var distinct_items = [];
437 $.each(this.get_item_doclist(), function(i, item) {
438 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
439 distinct_item_names.push(item.item_code || item.item_name);
440 distinct_items.push(item);
441 }
442 });
Anand Doshi3543f302013-05-24 19:25:01 +0530443
Anand Doshi179e3772013-09-05 16:53:57 +0530444 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530445 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530446 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530447 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
448 item_name: item.item_name,
449 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530450 return item_tax_record[head[0]] ?
451 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530452 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530453 }).join("\n")
454 });
455 }).join("\n");
456
Anand Doshi53b73422013-05-31 11:50:01 +0530457 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530458 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
459 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530460 <thead><tr>' + headings + '</tr></thead> \
461 <tbody>' + rows + '</tbody> \
462 </table></div>';
463 },
464
Anand Doshi923d41d2013-05-28 17:23:36 +0530465 _validate_before_fetch: function(fieldname) {
466 var me = this;
467 if(!me.frm.doc[fieldname]) {
468 return (wn._("Please specify") + ": " +
469 wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
470 ". " + wn._("It is needed to fetch Item Details."));
471 }
472 return null;
473 },
474
Anand Doshi3543f302013-05-24 19:25:01 +0530475 validate_company_and_party: function(party_field) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530476 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530477 var valid = true;
Anand Doshi923d41d2013-05-28 17:23:36 +0530478 var msg = "";
Anand Doshi3543f302013-05-24 19:25:01 +0530479 $.each(["company", party_field], function(i, fieldname) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530480 var msg_for_fieldname = me._validate_before_fetch(fieldname);
481 if(msg_for_fieldname) {
482 msgprint(msg_for_fieldname);
Anand Doshi3543f302013-05-24 19:25:01 +0530483 valid = false;
Anand Doshi3543f302013-05-24 19:25:01 +0530484 }
485 });
486 return valid;
487 },
488
489 get_item_doclist: function() {
490 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
491 {parentfield: this.fname});
492 },
493
494 get_tax_doclist: function() {
495 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
496 {parentfield: this.other_fname});
497 },
498
499 validate_conversion_rate: function() {
500 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
501 var conversion_rate_label = wn.meta.get_label(this.frm.doc.doctype, "conversion_rate",
502 this.frm.doc.name);
503
504 if(this.frm.doc.conversion_rate == 0) {
505 wn.throw(wn._(conversion_rate_label) + " " + wn._("cannot be 0"));
506 }
507
508 var company_currency = this.get_company_currency();
Anand Doshi3543f302013-05-24 19:25:01 +0530509
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530510 if(!this.frm.doc.conversion_rate) {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530511 wn.throw(wn._("Please enter valid") + " " + wn._(conversion_rate_label) +
512 " 1 " + this.frm.doc.currency + " = [?] " + company_currency);
513 }
Anand Doshi3543f302013-05-24 19:25:01 +0530514 },
515
516 calculate_taxes_and_totals: function() {
517 this.validate_conversion_rate();
518 this.frm.item_doclist = this.get_item_doclist();
519 this.frm.tax_doclist = this.get_tax_doclist();
520
521 this.calculate_item_values();
522 this.initialize_taxes();
523 this.determine_exclusive_rate && this.determine_exclusive_rate();
524 this.calculate_net_total();
525 this.calculate_taxes();
526 this.calculate_totals();
527 this._cleanup();
Anand Doshi923d41d2013-05-28 17:23:36 +0530528
Anand Doshi3543f302013-05-24 19:25:01 +0530529 this.show_item_wise_taxes();
530 },
531
532 initialize_taxes: function() {
533 var me = this;
534 $.each(this.frm.tax_doclist, function(i, tax) {
535 tax.item_wise_tax_detail = {};
536 $.each(["tax_amount", "total",
537 "tax_amount_for_current_item", "grand_total_for_current_item",
538 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
539 function(i, fieldname) { tax[fieldname] = 0.0 });
540
541 me.validate_on_previous_row(tax);
542 me.validate_inclusive_tax(tax);
543 wn.model.round_floats_in(tax);
544 });
545 },
546
547 calculate_taxes: function() {
548 var me = this;
549
550 $.each(this.frm.item_doclist, function(n, item) {
551 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
552
553 $.each(me.frm.tax_doclist, function(i, tax) {
554 // tax_amount represents the amount of tax for the current step
555 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530556
Anand Doshi3543f302013-05-24 19:25:01 +0530557 me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
558
559 // case when net total is 0 but there is an actual type charge
560 // in this case add the actual amount to tax.tax_amount
561 // and tax.grand_total_for_current_item for the first such iteration
562 if(tax.charge_type == "Actual" &&
563 !(current_tax_amount || me.frm.doc.net_total || tax.tax_amount)) {
564 var zero_net_total_adjustment = flt(tax.rate, precision("tax_amount", tax));
565 current_tax_amount += zero_net_total_adjustment;
566 }
567
568 // store tax_amount for current item as it will be used for
569 // charge type = 'On Previous Row Amount'
570 tax.tax_amount_for_current_item = current_tax_amount;
571
572 // accumulate tax amount into tax.tax_amount
573 tax.tax_amount += current_tax_amount;
574
Anand Doshi3543f302013-05-24 19:25:01 +0530575 // for buying
576 if(tax.category) {
577 // if just for valuation, do not add the tax amount in total
578 // hence, setting it as 0 for further steps
579 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
580
581 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
582 }
583
584 // Calculate tax.total viz. grand total till that step
585 // note: grand_total_for_current_item contains the contribution of
586 // item's amount, previously applied tax and the current tax on that item
587 if(i==0) {
588 tax.grand_total_for_current_item = flt(item.amount + current_tax_amount,
589 precision("total", tax));
590 } else {
591 tax.grand_total_for_current_item =
592 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
593 precision("total", tax));
594 }
595
596 // in tax.total, accumulate grand total for each item
597 tax.total += tax.grand_total_for_current_item;
598 });
599 });
600 },
601
602 get_current_tax_amount: function(item, tax, item_tax_map) {
603 var tax_rate = this._get_tax_rate(tax, item_tax_map);
604 var current_tax_amount = 0.0;
605
606 if(tax.charge_type == "Actual") {
607 // distribute the tax amount proportionally to each item row
608 var actual = flt(tax.rate, precision("tax_amount", tax));
609 current_tax_amount = this.frm.doc.net_total ?
610 ((item.amount / this.frm.doc.net_total) * actual) :
611 0.0;
612
613 } else if(tax.charge_type == "On Net Total") {
614 current_tax_amount = (tax_rate / 100.0) * item.amount;
615
616 } else if(tax.charge_type == "On Previous Row Amount") {
617 current_tax_amount = (tax_rate / 100.0) *
618 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
619
620 } else if(tax.charge_type == "On Previous Row Total") {
621 current_tax_amount = (tax_rate / 100.0) *
622 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
623
624 }
625
Anand Doshi53b73422013-05-31 11:50:01 +0530626 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
627
628 // store tax breakup for each item
629 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
630
631 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530632 },
633
634 _cleanup: function() {
635 $.each(this.frm.tax_doclist, function(i, tax) {
636 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
637 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
638 function(i, fieldname) { delete tax[fieldname]; });
639
640 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
641 });
642 },
Anand Doshifc777182013-05-27 19:29:07 +0530643
644 calculate_total_advance: function(parenttype, advance_parentfield) {
645 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
646 var advance_doclist = wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
647 {parentfield: advance_parentfield});
648 this.frm.doc.total_advance = flt(wn.utils.sum(
649 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
650 ), precision("total_advance"));
651
652 this.calculate_outstanding_amount();
653 }
654 },
Anand Doshi3543f302013-05-24 19:25:01 +0530655
656 _set_in_company_currency: function(item, print_field, base_field) {
657 // set values in base currency
658 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
659 precision(base_field, item));
660 },
Anand Doshibf3e54a2013-06-05 14:11:32 +0530661
662 get_terms: function() {
663 var me = this;
664 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530665 return this.frm.call({
Anand Doshibf3e54a2013-06-05 14:11:32 +0530666 method: "webnotes.client.get_value",
667 args: {
668 doctype: "Terms and Conditions",
669 fieldname: "terms",
670 filters: { name: this.frm.doc.tc_name },
671 },
672 });
673 }
674 },
Anand Doshi3543f302013-05-24 19:25:01 +0530675});