blob: 7db7aef51d866bc55838e0d9e995c810fbdfc757 [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302// License: GNU General Public License v3. See license.txt
Anand Doshi3543f302013-05-24 19:25:01 +05303
4wn.provide("erpnext");
Rushabh Mehta6de403f2013-12-13 14:10:14 +05305wn.require("assets/erpnext/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")
Rushabh Mehtae34090a2013-11-15 11:30:23 +053069 this.make_pos_btn();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053070 },
71
Rushabh Mehtae34090a2013-11-15 11:30:23 +053072 make_pos_btn: function() {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053073 if(!this.pos_active) {
74 var btn_label = wn._("POS View"),
75 icon = "icon-desktop";
76 } else {
77 var btn_label = wn._(this.frm.doctype) + wn._(" View"),
78 icon = "icon-file-text";
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053079 }
80 var me = this;
Rushabh Mehtae34090a2013-11-15 11:30:23 +053081
82 this.$pos_btn = this.frm.appframe.add_button(btn_label, function() {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053083 me.toggle_pos();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053084 }, icon);
85 },
86
87 toggle_pos: function(show) {
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053088 // Check whether it is Selling or Buying cycle
89 var price_list = wn.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
90 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list;
91
92 if (!price_list)
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053093 msgprint(wn._("Please select Price List"))
94 else {
95 if((show===true && this.pos_active) || (show===false && !this.pos_active)) return;
96
97 // make pos
Akhilesh Darjeef624ffa2013-09-23 12:27:16 +053098 if(!this.frm.pos) {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053099 this.frm.layout.add_view("pos");
100 this.frm.pos = new erpnext.POS(this.frm.layout.views.pos, this.frm);
101 }
102
103 // toggle view
104 this.frm.layout.set_view(this.pos_active ? "" : "pos");
105 this.pos_active = !this.pos_active;
106
107 // refresh
108 if(this.pos_active)
109 this.frm.pos.refresh();
Akhilesh Darjeef83576b2013-09-18 18:35:12 +0530110 this.frm.refresh();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530111 }
Anand Doshi3543f302013-05-24 19:25:01 +0530112 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530113
114 serial_no: function(doc, cdt, cdn) {
115 var me = this;
116 var item = wn.model.get_doc(cdt, cdn);
117
118 if (item.serial_no) {
119 if (!item.item_code) {
120 this.frm.script_manager.trigger("item_code", cdt, cdn);
121 }
122 else {
123 var sr_no = [];
124
125 // Replacing all occurences of comma with carriage return
126 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
127
128 serial_nos = serial_nos.trim().split('\n');
129
130 // Trim each string and push unique string to new list
131 for (var x=0; x<=serial_nos.length - 1; x++) {
132 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
133 sr_no.push(serial_nos[x].trim());
134 }
135 }
136
137 // Add the new list to the serial no. field in grid with each in new line
138 item.serial_no = "";
139 for (var x=0; x<=sr_no.length - 1; x++)
140 item.serial_no += sr_no[x] + '\n';
141
142 refresh_field("serial_no", item.name, item.parentfield);
143 wn.model.set_value(item.doctype, item.name, "qty", sr_no.length);
144 }
145 }
146 },
Anand Doshi3543f302013-05-24 19:25:01 +0530147
Anand Doshi923d41d2013-05-28 17:23:36 +0530148 validate: function() {
149 this.calculate_taxes_and_totals();
150 },
151
Anand Doshi5e4e5d72013-08-06 17:23:44 +0530152 set_default_values: function() {
153 $.each(wn.model.get_doclist(this.frm.doctype, this.frm.docname), function(i, doc) {
154 var updated = wn.model.set_default_values(doc);
155 if(doc.parentfield) {
156 refresh_field(doc.parentfield);
157 } else {
158 refresh_field(updated);
159 }
160 });
161 },
162
Anand Doshi3543f302013-05-24 19:25:01 +0530163 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +0530164 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530165 var company_currency = this.get_company_currency();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530166 if (!this.frm.doc.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530167 this.frm.set_value("currency", company_currency);
Anand Doshi17350b82013-08-01 15:45:23 +0530168 }
169
Akhilesh Darjee78378272013-10-11 19:06:30 +0530170 if (this.frm.doc.currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530171 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530172 }
173 if (this.frm.doc.price_list_currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530174 this.frm.set_value('plc_conversion_rate', 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530175 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530176
Nabin Hait48a0bd32013-07-23 15:31:39 +0530177 this.frm.script_manager.trigger("currency");
Anand Doshi3543f302013-05-24 19:25:01 +0530178 }
179 },
180
181 get_company_currency: function() {
182 return erpnext.get_currency(this.frm.doc.company);
183 },
184
185 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530186 var me = this;
187 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530188
Anand Doshi61a2f682013-06-21 17:55:31 +0530189 var company_currency = this.get_company_currency();
190 if(this.frm.doc.currency !== company_currency) {
191 this.get_exchange_rate(this.frm.doc.currency, company_currency,
192 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530193 me.frm.set_value("conversion_rate", exchange_rate);
194 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530195 });
196 } else {
197 this.conversion_rate();
198 }
199 },
200
201 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530202 if(this.frm.doc.currency === this.get_company_currency()) {
203 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530204 }
205 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530206 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
207 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
208 }
Nabin Haitb338b6e2013-10-15 16:55:24 +0530209 if(flt(this.frm.doc.conversion_rate)>0.0) this.calculate_taxes_and_totals();
Anand Doshi3543f302013-05-24 19:25:01 +0530210 },
211
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530212 get_price_list_currency: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +0530213 var me = this;
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530214 var fieldname = buying_or_selling.toLowerCase() + "_price_list";
215 if(this.frm.doc[fieldname]) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530216 return this.frm.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530217 method: "erpnext.setup.utils.get_price_list_currency",
Nabin Hait48a0bd32013-07-23 15:31:39 +0530218 args: {
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530219 price_list: this.frm.doc[fieldname],
Nabin Hait48a0bd32013-07-23 15:31:39 +0530220 },
Anand Doshi3543f302013-05-24 19:25:01 +0530221 callback: function(r) {
222 if(!r.exc) {
223 me.price_list_currency();
224 }
225 }
226 });
227 }
228 },
229
Anand Doshi61a2f682013-06-21 17:55:31 +0530230 get_exchange_rate: function(from_currency, to_currency, callback) {
231 var exchange_name = from_currency + "-" + to_currency;
232 wn.model.with_doc("Currency Exchange", exchange_name, function(name) {
233 var exchange_doc = wn.model.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530234 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530235 });
236 },
237
Anand Doshi3543f302013-05-24 19:25:01 +0530238 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530239 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530240 this.set_dynamic_labels();
Anand Doshi61a2f682013-06-21 17:55:31 +0530241
242 var company_currency = this.get_company_currency();
243 if(this.frm.doc.price_list_currency !== company_currency) {
244 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
245 function(exchange_rate) {
246 if(exchange_rate) {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530247 me.frm.set_value("plc_conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530248 me.plc_conversion_rate();
249 }
250 });
251 } else {
252 this.plc_conversion_rate();
253 }
Anand Doshi3543f302013-05-24 19:25:01 +0530254 },
255
256 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530257 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
258 this.frm.set_value("plc_conversion_rate", 1.0);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530259 }
260 if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
Anand Doshi61a2f682013-06-21 17:55:31 +0530261 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
262 this.calculate_taxes_and_totals();
263 }
Anand Doshi3543f302013-05-24 19:25:01 +0530264 },
265
266 qty: function(doc, cdt, cdn) {
267 this.calculate_taxes_and_totals();
268 },
269
Anand Doshi923d41d2013-05-28 17:23:36 +0530270 tax_rate: function(doc, cdt, cdn) {
271 this.calculate_taxes_and_totals();
272 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530273
Anand Doshi923d41d2013-05-28 17:23:36 +0530274 row_id: function(doc, cdt, cdn) {
275 var tax = wn.model.get_doc(cdt, cdn);
276 try {
277 this.validate_on_previous_row(tax);
278 this.calculate_taxes_and_totals();
279 } catch(e) {
280 tax.row_id = null;
281 refresh_field("row_id", tax.name, tax.parentfield);
282 throw e;
283 }
284 },
285
Anand Doshi61a2f682013-06-21 17:55:31 +0530286 set_dynamic_labels: function() {
287 // What TODO? should we make price list system non-mandatory?
288 this.frm.toggle_reqd("plc_conversion_rate",
289 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
290
291 var company_currency = this.get_company_currency();
292 this.change_form_labels(company_currency);
293 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530294 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530295 },
296
Anand Doshi923d41d2013-05-28 17:23:36 +0530297 recalculate: function() {
298 this.calculate_taxes_and_totals();
299 },
300
301 recalculate_values: function() {
302 this.calculate_taxes_and_totals();
303 },
304
Anand Doshid0b00722013-05-28 18:54:48 +0530305 calculate_charges: function() {
306 this.calculate_taxes_and_totals();
307 },
308
Anand Doshi3543f302013-05-24 19:25:01 +0530309 included_in_print_rate: function(doc, cdt, cdn) {
310 var tax = wn.model.get_doc(cdt, cdn);
311 try {
312 this.validate_on_previous_row(tax);
313 this.validate_inclusive_tax(tax);
314 this.calculate_taxes_and_totals();
315 } catch(e) {
316 tax.included_in_print_rate = 0;
317 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
318 throw e;
319 }
320 },
321
322 validate_on_previous_row: function(tax) {
323 // validate if a valid row id is mentioned in case of
324 // On Previous Row Amount and On Previous Row Total
Bárbara Perretti534af0b2013-10-08 15:00:07 -0300325 if(([wn._("On Previous Row Amount"), wn._("On Previous Row Total")].indexOf(tax.charge_type) != -1) &&
Anand Doshi3543f302013-05-24 19:25:01 +0530326 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
327 var msg = repl(wn._("Row") + " # %(idx)s [%(doctype)s]: " +
328 wn._("Please specify a valid") + " %(row_id_label)s", {
329 idx: tax.idx,
330 doctype: tax.doctype,
331 row_id_label: wn.meta.get_label(tax.doctype, "row_id", tax.name)
332 });
333 msgprint(msg);
334 throw msg;
335 }
336 },
337
338 validate_inclusive_tax: function(tax) {
339 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
340
341 var actual_type_error = function() {
342 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
343 "%(charge_type_label)s = \"%(charge_type)s\" " +
344 wn._("cannot be included in Item's rate"), {
345 idx: tax.idx,
346 doctype: tax.doctype,
347 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
348 charge_type: tax.charge_type
349 });
350 msgprint(msg);
351 throw msg;
352 };
353
354 var on_previous_row_error = function(row_range) {
355 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
356 wn._("to be included in Item's rate, it is required that: ") +
357 " [" + wn._("Row") + " # %(row_range)s] " + wn._("also be included in Item's rate"), {
358 idx: tax.idx,
359 doctype: tax.doctype,
360 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
361 charge_type: tax.charge_type,
362 inclusive_label: wn.meta.get_label(tax.doctype, "included_in_print_rate", tax.name),
363 row_range: row_range,
364 });
365
366 msgprint(msg);
367 throw msg;
368 };
369
370 if(cint(tax.included_in_print_rate)) {
371 if(tax.charge_type == "Actual") {
372 // inclusive tax cannot be of type Actual
373 actual_type_error();
374 } else if(tax.charge_type == "On Previous Row Amount" &&
375 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
376 // referred row should also be an inclusive tax
377 on_previous_row_error(tax.row_id);
378 } else if(tax.charge_type == "On Previous Row Total") {
379 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
380 function(t) { return cint(t.included_in_print_rate) ? null : t; });
381 if(taxes_not_included.length > 0) {
382 // all rows above this tax should be inclusive
383 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
384 }
385 }
386 }
387 },
388
389 _load_item_tax_rate: function(item_tax_rate) {
390 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
391 },
392
393 _get_tax_rate: function(tax, item_tax_map) {
394 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
395 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
396 tax.rate;
397 },
398
399 get_item_wise_taxes_html: function() {
400 var item_tax = {};
401 var tax_accounts = [];
402 var company_currency = this.get_company_currency();
403
404 $.each(this.get_tax_doclist(), function(i, tax) {
405 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530406 var tax_rate_precision = precision("rate", tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530407 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530408 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530409 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530410 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530411 var tax_rate = "";
412 if(tax_data[0] != null) {
413 tax_rate = (tax.charge_type === "Actual") ?
414 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
415 (flt(tax_data[0], tax_rate_precision) + "%");
416 }
417 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
418 tax_amount_precision);
Anand Doshi53b73422013-05-31 11:50:01 +0530419
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530420 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530421 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530422 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530423 }
Anand Doshi3543f302013-05-24 19:25:01 +0530424 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530425 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530426 });
427
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530428 var headings = $.map([wn._("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530429 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Anand Doshi179e3772013-09-05 16:53:57 +0530430
431 var distinct_item_names = [];
432 var distinct_items = [];
433 $.each(this.get_item_doclist(), function(i, item) {
434 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
435 distinct_item_names.push(item.item_code || item.item_name);
436 distinct_items.push(item);
437 }
438 });
Anand Doshi3543f302013-05-24 19:25:01 +0530439
Anand Doshi179e3772013-09-05 16:53:57 +0530440 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530441 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530442 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530443 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
444 item_name: item.item_name,
445 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530446 return item_tax_record[head[0]] ?
447 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530448 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530449 }).join("\n")
450 });
451 }).join("\n");
452
Anand Doshi53b73422013-05-31 11:50:01 +0530453 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530454 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
455 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530456 <thead><tr>' + headings + '</tr></thead> \
457 <tbody>' + rows + '</tbody> \
458 </table></div>';
459 },
460
Anand Doshi923d41d2013-05-28 17:23:36 +0530461 _validate_before_fetch: function(fieldname) {
462 var me = this;
463 if(!me.frm.doc[fieldname]) {
464 return (wn._("Please specify") + ": " +
465 wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
466 ". " + wn._("It is needed to fetch Item Details."));
467 }
468 return null;
469 },
470
Anand Doshi3543f302013-05-24 19:25:01 +0530471 validate_company_and_party: function(party_field) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530472 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530473 var valid = true;
Anand Doshi923d41d2013-05-28 17:23:36 +0530474 var msg = "";
Anand Doshi3543f302013-05-24 19:25:01 +0530475 $.each(["company", party_field], function(i, fieldname) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530476 var msg_for_fieldname = me._validate_before_fetch(fieldname);
477 if(msg_for_fieldname) {
478 msgprint(msg_for_fieldname);
Anand Doshi3543f302013-05-24 19:25:01 +0530479 valid = false;
Anand Doshi3543f302013-05-24 19:25:01 +0530480 }
481 });
482 return valid;
483 },
484
485 get_item_doclist: function() {
486 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
487 {parentfield: this.fname});
488 },
489
490 get_tax_doclist: function() {
491 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
492 {parentfield: this.other_fname});
493 },
494
495 validate_conversion_rate: function() {
496 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
497 var conversion_rate_label = wn.meta.get_label(this.frm.doc.doctype, "conversion_rate",
498 this.frm.doc.name);
Anand Doshi3543f302013-05-24 19:25:01 +0530499 var company_currency = this.get_company_currency();
Anand Doshi3543f302013-05-24 19:25:01 +0530500
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530501 if(!this.frm.doc.conversion_rate) {
Nabin Haite505fbe2013-11-15 13:38:23 +0530502 wn.throw(repl('%(conversion_rate_label)s' +
503 wn._(' is mandatory. Maybe Currency Exchange record is not created for ') +
504 '%(from_currency)s' + wn._(" to ") + '%(to_currency)s',
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530505 {
506 "conversion_rate_label": conversion_rate_label,
Nabin Haite505fbe2013-11-15 13:38:23 +0530507 "from_currency": this.frm.doc.currency,
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530508 "to_currency": company_currency
Nabin Haite505fbe2013-11-15 13:38:23 +0530509 }));
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530510 }
Anand Doshi3543f302013-05-24 19:25:01 +0530511 },
512
513 calculate_taxes_and_totals: function() {
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530514 this.discount_amount_applied = false;
515 this._calculate_taxes_and_totals();
516 if (wn.meta.get_docfield(this.frm.doc.doctype, "discount_amount"))
517 this.apply_discount_amount();
518 },
519
520 _calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530521 this.validate_conversion_rate();
522 this.frm.item_doclist = this.get_item_doclist();
523 this.frm.tax_doclist = this.get_tax_doclist();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530524
Anand Doshi3543f302013-05-24 19:25:01 +0530525 this.calculate_item_values();
526 this.initialize_taxes();
527 this.determine_exclusive_rate && this.determine_exclusive_rate();
528 this.calculate_net_total();
529 this.calculate_taxes();
530 this.calculate_totals();
531 this._cleanup();
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530532
Anand Doshi3543f302013-05-24 19:25:01 +0530533 this.show_item_wise_taxes();
534 },
535
536 initialize_taxes: function() {
537 var me = this;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530538
Anand Doshi3543f302013-05-24 19:25:01 +0530539 $.each(this.frm.tax_doclist, function(i, tax) {
540 tax.item_wise_tax_detail = {};
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530541 tax_fields = ["total", "tax_amount_after_discount_amount",
Anand Doshi3543f302013-05-24 19:25:01 +0530542 "tax_amount_for_current_item", "grand_total_for_current_item",
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530543 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
544
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530545 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530546 tax_fields.push("tax_amount");
547
548 $.each(tax_fields, function(i, fieldname) { tax[fieldname] = 0.0 });
Anand Doshi3543f302013-05-24 19:25:01 +0530549
550 me.validate_on_previous_row(tax);
551 me.validate_inclusive_tax(tax);
552 wn.model.round_floats_in(tax);
553 });
554 },
555
556 calculate_taxes: function() {
557 var me = this;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530558 var actual_tax_dict = {};
Anand Doshi3543f302013-05-24 19:25:01 +0530559
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530560 // maintain actual tax rate based on idx
561 $.each(this.frm.tax_doclist, function(i, tax) {
562 if (tax.charge_type == "Actual") {
563 actual_tax_dict[tax.idx] = flt(tax.rate);
564 }
565 });
566
Anand Doshi3543f302013-05-24 19:25:01 +0530567 $.each(this.frm.item_doclist, function(n, item) {
568 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530569
Anand Doshi3543f302013-05-24 19:25:01 +0530570 $.each(me.frm.tax_doclist, function(i, tax) {
571 // tax_amount represents the amount of tax for the current step
572 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530573
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530574 // Adjust divisional loss to the last item
575 if (tax.charge_type == "Actual") {
576 actual_tax_dict[tax.idx] -= current_tax_amount;
577 if (n == me.frm.item_doclist.length - 1) {
578 current_tax_amount += actual_tax_dict[tax.idx]
Anand Doshi3543f302013-05-24 19:25:01 +0530579 }
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530580 }
581
582 me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
583
Anand Doshi3543f302013-05-24 19:25:01 +0530584 // store tax_amount for current item as it will be used for
585 // charge type = 'On Previous Row Amount'
586 tax.tax_amount_for_current_item = current_tax_amount;
587
588 // accumulate tax amount into tax.tax_amount
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530589 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530590 tax.tax_amount += current_tax_amount;
591
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530592 tax.tax_amount_after_discount_amount += current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530593
Anand Doshi3543f302013-05-24 19:25:01 +0530594 // for buying
595 if(tax.category) {
596 // if just for valuation, do not add the tax amount in total
597 // hence, setting it as 0 for further steps
598 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
599
600 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
601 }
602
603 // Calculate tax.total viz. grand total till that step
604 // note: grand_total_for_current_item contains the contribution of
605 // item's amount, previously applied tax and the current tax on that item
606 if(i==0) {
607 tax.grand_total_for_current_item = flt(item.amount + current_tax_amount,
608 precision("total", tax));
609 } else {
610 tax.grand_total_for_current_item =
611 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
612 precision("total", tax));
613 }
614
615 // in tax.total, accumulate grand total for each item
616 tax.total += tax.grand_total_for_current_item;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530617
618 // set precision in the last item iteration
619 if (n == me.frm.item_doclist.length - 1) {
620 me.round_off_totals(tax);
621
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530622 // adjust Discount Amount loss in last tax iteration
623 if ((i == me.frm.tax_doclist.length - 1) && me.discount_amount_applied)
624 me.adjust_discount_amount_loss(tax);
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530625 }
Anand Doshi3543f302013-05-24 19:25:01 +0530626 });
627 });
628 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530629
630 round_off_totals: function(tax) {
631 tax.total = flt(tax.total, precision("total", tax));
632 tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax));
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530633 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount,
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530634 precision("tax_amount", tax));
635 },
636
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530637 adjust_discount_amount_loss: function(tax) {
638 var discount_amount_loss = this.frm.doc.grand_total - flt(this.frm.doc.discount_amount) - tax.total;
639 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
640 discount_amount_loss, precision("tax_amount", tax));
641 tax.total = flt(tax.total + discount_amount_loss, precision("total", tax));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530642 },
Anand Doshi3543f302013-05-24 19:25:01 +0530643
644 get_current_tax_amount: function(item, tax, item_tax_map) {
645 var tax_rate = this._get_tax_rate(tax, item_tax_map);
646 var current_tax_amount = 0.0;
647
648 if(tax.charge_type == "Actual") {
649 // distribute the tax amount proportionally to each item row
650 var actual = flt(tax.rate, precision("tax_amount", tax));
651 current_tax_amount = this.frm.doc.net_total ?
652 ((item.amount / this.frm.doc.net_total) * actual) :
653 0.0;
654
655 } else if(tax.charge_type == "On Net Total") {
656 current_tax_amount = (tax_rate / 100.0) * item.amount;
657
658 } else if(tax.charge_type == "On Previous Row Amount") {
659 current_tax_amount = (tax_rate / 100.0) *
660 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
661
662 } else if(tax.charge_type == "On Previous Row Total") {
663 current_tax_amount = (tax_rate / 100.0) *
664 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
Anand Doshi3543f302013-05-24 19:25:01 +0530665 }
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530666
Anand Doshi53b73422013-05-31 11:50:01 +0530667 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
668
669 // store tax breakup for each item
670 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
671
672 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530673 },
674
675 _cleanup: function() {
676 $.each(this.frm.tax_doclist, function(i, tax) {
677 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
678 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
679 function(i, fieldname) { delete tax[fieldname]; });
680
681 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
682 });
683 },
Anand Doshifc777182013-05-27 19:29:07 +0530684
685 calculate_total_advance: function(parenttype, advance_parentfield) {
686 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
687 var advance_doclist = wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
688 {parentfield: advance_parentfield});
689 this.frm.doc.total_advance = flt(wn.utils.sum(
690 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
691 ), precision("total_advance"));
692
693 this.calculate_outstanding_amount();
694 }
695 },
Anand Doshi3543f302013-05-24 19:25:01 +0530696
697 _set_in_company_currency: function(item, print_field, base_field) {
698 // set values in base currency
699 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
700 precision(base_field, item));
701 },
Anand Doshibf3e54a2013-06-05 14:11:32 +0530702
703 get_terms: function() {
704 var me = this;
705 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530706 return this.frm.call({
Anand Doshibf3e54a2013-06-05 14:11:32 +0530707 method: "webnotes.client.get_value",
708 args: {
709 doctype: "Terms and Conditions",
710 fieldname: "terms",
711 filters: { name: this.frm.doc.tc_name },
712 },
713 });
714 }
715 },
Anand Doshi3543f302013-05-24 19:25:01 +0530716});