blob: 542a1c141188118a568b548b01d5337224799ca8 [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");
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")
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() {
Akhilesh Darjeeb09dc462013-12-23 15:49:08 +0530149 this.flat_discount_applied = false;
Anand Doshi923d41d2013-05-28 17:23:36 +0530150 this.calculate_taxes_and_totals();
Akhilesh Darjeeb09dc462013-12-23 15:49:08 +0530151 this.apply_flat_discount && this.apply_flat_discount();
Anand Doshi923d41d2013-05-28 17:23:36 +0530152 },
153
Anand Doshi5e4e5d72013-08-06 17:23:44 +0530154 set_default_values: function() {
155 $.each(wn.model.get_doclist(this.frm.doctype, this.frm.docname), function(i, doc) {
156 var updated = wn.model.set_default_values(doc);
157 if(doc.parentfield) {
158 refresh_field(doc.parentfield);
159 } else {
160 refresh_field(updated);
161 }
162 });
163 },
164
Anand Doshi3543f302013-05-24 19:25:01 +0530165 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +0530166 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530167 var company_currency = this.get_company_currency();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530168 if (!this.frm.doc.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530169 this.frm.set_value("currency", company_currency);
Anand Doshi17350b82013-08-01 15:45:23 +0530170 }
171
Akhilesh Darjee78378272013-10-11 19:06:30 +0530172 if (this.frm.doc.currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530173 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530174 }
175 if (this.frm.doc.price_list_currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530176 this.frm.set_value('plc_conversion_rate', 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530177 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530178
Nabin Hait48a0bd32013-07-23 15:31:39 +0530179 this.frm.script_manager.trigger("currency");
Anand Doshi3543f302013-05-24 19:25:01 +0530180 }
181 },
182
183 get_company_currency: function() {
184 return erpnext.get_currency(this.frm.doc.company);
185 },
186
187 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530188 var me = this;
189 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530190
Anand Doshi61a2f682013-06-21 17:55:31 +0530191 var company_currency = this.get_company_currency();
192 if(this.frm.doc.currency !== company_currency) {
193 this.get_exchange_rate(this.frm.doc.currency, company_currency,
194 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530195 me.frm.set_value("conversion_rate", exchange_rate);
196 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530197 });
198 } else {
199 this.conversion_rate();
200 }
201 },
202
203 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530204 if(this.frm.doc.currency === this.get_company_currency()) {
205 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530206 }
207 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530208 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
209 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
210 }
Nabin Haitb338b6e2013-10-15 16:55:24 +0530211 if(flt(this.frm.doc.conversion_rate)>0.0) this.calculate_taxes_and_totals();
Anand Doshi3543f302013-05-24 19:25:01 +0530212 },
213
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530214 get_price_list_currency: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +0530215 var me = this;
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530216 var fieldname = buying_or_selling.toLowerCase() + "_price_list";
217 if(this.frm.doc[fieldname]) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530218 return this.frm.call({
Anand Doshi3543f302013-05-24 19:25:01 +0530219 method: "setup.utils.get_price_list_currency",
Nabin Hait48a0bd32013-07-23 15:31:39 +0530220 args: {
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530221 price_list: this.frm.doc[fieldname],
Nabin Hait48a0bd32013-07-23 15:31:39 +0530222 },
Anand Doshi3543f302013-05-24 19:25:01 +0530223 callback: function(r) {
224 if(!r.exc) {
225 me.price_list_currency();
226 }
227 }
228 });
229 }
230 },
231
Anand Doshi61a2f682013-06-21 17:55:31 +0530232 get_exchange_rate: function(from_currency, to_currency, callback) {
233 var exchange_name = from_currency + "-" + to_currency;
234 wn.model.with_doc("Currency Exchange", exchange_name, function(name) {
235 var exchange_doc = wn.model.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530236 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530237 });
238 },
239
Anand Doshi3543f302013-05-24 19:25:01 +0530240 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530241 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530242 this.set_dynamic_labels();
Anand Doshi61a2f682013-06-21 17:55:31 +0530243
244 var company_currency = this.get_company_currency();
245 if(this.frm.doc.price_list_currency !== company_currency) {
246 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
247 function(exchange_rate) {
248 if(exchange_rate) {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530249 me.frm.set_value("plc_conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530250 me.plc_conversion_rate();
251 }
252 });
253 } else {
254 this.plc_conversion_rate();
255 }
Anand Doshi3543f302013-05-24 19:25:01 +0530256 },
257
258 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530259 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
260 this.frm.set_value("plc_conversion_rate", 1.0);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530261 }
262 if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
Anand Doshi61a2f682013-06-21 17:55:31 +0530263 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
264 this.calculate_taxes_and_totals();
265 }
Anand Doshi3543f302013-05-24 19:25:01 +0530266 },
267
268 qty: function(doc, cdt, cdn) {
269 this.calculate_taxes_and_totals();
270 },
271
Anand Doshi923d41d2013-05-28 17:23:36 +0530272 tax_rate: function(doc, cdt, cdn) {
273 this.calculate_taxes_and_totals();
274 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530275
Anand Doshi923d41d2013-05-28 17:23:36 +0530276 row_id: function(doc, cdt, cdn) {
277 var tax = wn.model.get_doc(cdt, cdn);
278 try {
279 this.validate_on_previous_row(tax);
280 this.calculate_taxes_and_totals();
281 } catch(e) {
282 tax.row_id = null;
283 refresh_field("row_id", tax.name, tax.parentfield);
284 throw e;
285 }
286 },
287
Anand Doshi61a2f682013-06-21 17:55:31 +0530288 set_dynamic_labels: function() {
289 // What TODO? should we make price list system non-mandatory?
290 this.frm.toggle_reqd("plc_conversion_rate",
291 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
292
293 var company_currency = this.get_company_currency();
294 this.change_form_labels(company_currency);
295 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530296 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530297 },
298
Anand Doshi923d41d2013-05-28 17:23:36 +0530299 recalculate: function() {
300 this.calculate_taxes_and_totals();
301 },
302
303 recalculate_values: function() {
304 this.calculate_taxes_and_totals();
305 },
306
Anand Doshid0b00722013-05-28 18:54:48 +0530307 calculate_charges: function() {
308 this.calculate_taxes_and_totals();
309 },
310
Anand Doshi3543f302013-05-24 19:25:01 +0530311 included_in_print_rate: function(doc, cdt, cdn) {
312 var tax = wn.model.get_doc(cdt, cdn);
313 try {
314 this.validate_on_previous_row(tax);
315 this.validate_inclusive_tax(tax);
316 this.calculate_taxes_and_totals();
317 } catch(e) {
318 tax.included_in_print_rate = 0;
319 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
320 throw e;
321 }
322 },
323
324 validate_on_previous_row: function(tax) {
325 // validate if a valid row id is mentioned in case of
326 // On Previous Row Amount and On Previous Row Total
Bárbara Perretti534af0b2013-10-08 15:00:07 -0300327 if(([wn._("On Previous Row Amount"), wn._("On Previous Row Total")].indexOf(tax.charge_type) != -1) &&
Anand Doshi3543f302013-05-24 19:25:01 +0530328 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
329 var msg = repl(wn._("Row") + " # %(idx)s [%(doctype)s]: " +
330 wn._("Please specify a valid") + " %(row_id_label)s", {
331 idx: tax.idx,
332 doctype: tax.doctype,
333 row_id_label: wn.meta.get_label(tax.doctype, "row_id", tax.name)
334 });
335 msgprint(msg);
336 throw msg;
337 }
338 },
339
340 validate_inclusive_tax: function(tax) {
341 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
342
343 var actual_type_error = function() {
344 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
345 "%(charge_type_label)s = \"%(charge_type)s\" " +
346 wn._("cannot be included in Item's rate"), {
347 idx: tax.idx,
348 doctype: tax.doctype,
349 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
350 charge_type: tax.charge_type
351 });
352 msgprint(msg);
353 throw msg;
354 };
355
356 var on_previous_row_error = function(row_range) {
357 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
358 wn._("to be included in Item's rate, it is required that: ") +
359 " [" + wn._("Row") + " # %(row_range)s] " + wn._("also be included in Item's rate"), {
360 idx: tax.idx,
361 doctype: tax.doctype,
362 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
363 charge_type: tax.charge_type,
364 inclusive_label: wn.meta.get_label(tax.doctype, "included_in_print_rate", tax.name),
365 row_range: row_range,
366 });
367
368 msgprint(msg);
369 throw msg;
370 };
371
372 if(cint(tax.included_in_print_rate)) {
373 if(tax.charge_type == "Actual") {
374 // inclusive tax cannot be of type Actual
375 actual_type_error();
376 } else if(tax.charge_type == "On Previous Row Amount" &&
377 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
378 // referred row should also be an inclusive tax
379 on_previous_row_error(tax.row_id);
380 } else if(tax.charge_type == "On Previous Row Total") {
381 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
382 function(t) { return cint(t.included_in_print_rate) ? null : t; });
383 if(taxes_not_included.length > 0) {
384 // all rows above this tax should be inclusive
385 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
386 }
387 }
388 }
389 },
390
391 _load_item_tax_rate: function(item_tax_rate) {
392 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
393 },
394
395 _get_tax_rate: function(tax, item_tax_map) {
396 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
397 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
398 tax.rate;
399 },
400
401 get_item_wise_taxes_html: function() {
402 var item_tax = {};
403 var tax_accounts = [];
404 var company_currency = this.get_company_currency();
405
406 $.each(this.get_tax_doclist(), function(i, tax) {
407 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530408 var tax_rate_precision = precision("rate", tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530409 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530410 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530411 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530412 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530413 var tax_rate = "";
414 if(tax_data[0] != null) {
415 tax_rate = (tax.charge_type === "Actual") ?
416 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
417 (flt(tax_data[0], tax_rate_precision) + "%");
418 }
419 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
420 tax_amount_precision);
Anand Doshi53b73422013-05-31 11:50:01 +0530421
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530422 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530423 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530424 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530425 }
Anand Doshi3543f302013-05-24 19:25:01 +0530426 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530427 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530428 });
429
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530430 var headings = $.map([wn._("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530431 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Anand Doshi179e3772013-09-05 16:53:57 +0530432
433 var distinct_item_names = [];
434 var distinct_items = [];
435 $.each(this.get_item_doclist(), function(i, item) {
436 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
437 distinct_item_names.push(item.item_code || item.item_name);
438 distinct_items.push(item);
439 }
440 });
Anand Doshi3543f302013-05-24 19:25:01 +0530441
Anand Doshi179e3772013-09-05 16:53:57 +0530442 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530443 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530444 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530445 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
446 item_name: item.item_name,
447 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530448 return item_tax_record[head[0]] ?
449 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530450 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530451 }).join("\n")
452 });
453 }).join("\n");
454
Anand Doshi53b73422013-05-31 11:50:01 +0530455 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530456 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
457 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530458 <thead><tr>' + headings + '</tr></thead> \
459 <tbody>' + rows + '</tbody> \
460 </table></div>';
461 },
462
Anand Doshi923d41d2013-05-28 17:23:36 +0530463 _validate_before_fetch: function(fieldname) {
464 var me = this;
465 if(!me.frm.doc[fieldname]) {
466 return (wn._("Please specify") + ": " +
467 wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
468 ". " + wn._("It is needed to fetch Item Details."));
469 }
470 return null;
471 },
472
Anand Doshi3543f302013-05-24 19:25:01 +0530473 validate_company_and_party: function(party_field) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530474 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530475 var valid = true;
Anand Doshi923d41d2013-05-28 17:23:36 +0530476 var msg = "";
Anand Doshi3543f302013-05-24 19:25:01 +0530477 $.each(["company", party_field], function(i, fieldname) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530478 var msg_for_fieldname = me._validate_before_fetch(fieldname);
479 if(msg_for_fieldname) {
480 msgprint(msg_for_fieldname);
Anand Doshi3543f302013-05-24 19:25:01 +0530481 valid = false;
Anand Doshi3543f302013-05-24 19:25:01 +0530482 }
483 });
484 return valid;
485 },
486
487 get_item_doclist: function() {
488 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
489 {parentfield: this.fname});
490 },
491
492 get_tax_doclist: function() {
493 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
494 {parentfield: this.other_fname});
495 },
496
497 validate_conversion_rate: function() {
498 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
499 var conversion_rate_label = wn.meta.get_label(this.frm.doc.doctype, "conversion_rate",
500 this.frm.doc.name);
Anand Doshi3543f302013-05-24 19:25:01 +0530501 var company_currency = this.get_company_currency();
Anand Doshi3543f302013-05-24 19:25:01 +0530502
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530503 if(!this.frm.doc.conversion_rate) {
Nabin Haite505fbe2013-11-15 13:38:23 +0530504 wn.throw(repl('%(conversion_rate_label)s' +
505 wn._(' is mandatory. Maybe Currency Exchange record is not created for ') +
506 '%(from_currency)s' + wn._(" to ") + '%(to_currency)s',
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530507 {
508 "conversion_rate_label": conversion_rate_label,
Nabin Haite505fbe2013-11-15 13:38:23 +0530509 "from_currency": this.frm.doc.currency,
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530510 "to_currency": company_currency
Nabin Haite505fbe2013-11-15 13:38:23 +0530511 }));
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530512 }
Anand Doshi3543f302013-05-24 19:25:01 +0530513 },
514
515 calculate_taxes_and_totals: function() {
516 this.validate_conversion_rate();
517 this.frm.item_doclist = this.get_item_doclist();
518 this.frm.tax_doclist = this.get_tax_doclist();
Akhilesh Darjeec53dbc72013-12-25 19:46:20 +0530519
Anand Doshi3543f302013-05-24 19:25:01 +0530520 this.calculate_item_values();
521 this.initialize_taxes();
522 this.determine_exclusive_rate && this.determine_exclusive_rate();
523 this.calculate_net_total();
524 this.calculate_taxes();
525 this.calculate_totals();
526 this._cleanup();
Anand Doshi923d41d2013-05-28 17:23:36 +0530527
Anand Doshi3543f302013-05-24 19:25:01 +0530528 this.show_item_wise_taxes();
529 },
530
531 initialize_taxes: function() {
532 var me = this;
Akhilesh Darjeeb09dc462013-12-23 15:49:08 +0530533
Anand Doshi3543f302013-05-24 19:25:01 +0530534 $.each(this.frm.tax_doclist, function(i, tax) {
535 tax.item_wise_tax_detail = {};
Akhilesh Darjeec53dbc72013-12-25 19:46:20 +0530536 tax_fields = ["total", "tax_amount_after_flat_discount",
537 "tax_amount_for_current_item", "grand_total_for_current_item",
Akhilesh Darjeeb09dc462013-12-23 15:49:08 +0530538 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
539
Akhilesh Darjeec53dbc72013-12-25 19:46:20 +0530540 if (!me.flat_discount_applied)
Akhilesh Darjeeb09dc462013-12-23 15:49:08 +0530541 tax_fields.push("tax_amount");
542
543 $.each(tax_fields, function(i, fieldname) { tax[fieldname] = 0.0 });
Anand Doshi3543f302013-05-24 19:25:01 +0530544
545 me.validate_on_previous_row(tax);
546 me.validate_inclusive_tax(tax);
547 wn.model.round_floats_in(tax);
548 });
549 },
550
551 calculate_taxes: function() {
552 var me = this;
Akhilesh Darjeec53dbc72013-12-25 19:46:20 +0530553 var actual_tax_dict = {};
Anand Doshi3543f302013-05-24 19:25:01 +0530554
Akhilesh Darjeec53dbc72013-12-25 19:46:20 +0530555 // maintain actual tax rate based on idx
556 $.each(this.frm.tax_doclist, function(i, tax) {
557 if (tax.charge_type == "Actual") {
558 actual_tax_dict[tax.idx] = flt(tax.rate);
559 }
560 });
561
Anand Doshi3543f302013-05-24 19:25:01 +0530562 $.each(this.frm.item_doclist, function(n, item) {
563 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
Akhilesh Darjeec53dbc72013-12-25 19:46:20 +0530564
Anand Doshi3543f302013-05-24 19:25:01 +0530565 $.each(me.frm.tax_doclist, function(i, tax) {
566 // tax_amount represents the amount of tax for the current step
567 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530568
Akhilesh Darjeec53dbc72013-12-25 19:46:20 +0530569 // Adjust divisional loss to the last item
570 if (tax.charge_type == "Actual") {
571 actual_tax_dict[tax.idx] -= current_tax_amount;
572 if (n == me.frm.item_doclist.length - 1) {
573 current_tax_amount += actual_tax_dict[tax.idx]
Anand Doshi3543f302013-05-24 19:25:01 +0530574 }
Akhilesh Darjeec53dbc72013-12-25 19:46:20 +0530575 }
576
577 me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
578
Anand Doshi3543f302013-05-24 19:25:01 +0530579 // store tax_amount for current item as it will be used for
580 // charge type = 'On Previous Row Amount'
581 tax.tax_amount_for_current_item = current_tax_amount;
582
583 // accumulate tax amount into tax.tax_amount
Akhilesh Darjeec53dbc72013-12-25 19:46:20 +0530584 if (!me.flat_discount_applied)
Akhilesh Darjeeb09dc462013-12-23 15:49:08 +0530585 tax.tax_amount += current_tax_amount;
Akhilesh Darjeec53dbc72013-12-25 19:46:20 +0530586
587 tax.tax_amount_after_flat_discount += current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530588
Anand Doshi3543f302013-05-24 19:25:01 +0530589 // for buying
590 if(tax.category) {
591 // if just for valuation, do not add the tax amount in total
592 // hence, setting it as 0 for further steps
593 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
594
595 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
596 }
597
598 // Calculate tax.total viz. grand total till that step
599 // note: grand_total_for_current_item contains the contribution of
600 // item's amount, previously applied tax and the current tax on that item
601 if(i==0) {
602 tax.grand_total_for_current_item = flt(item.amount + current_tax_amount,
603 precision("total", tax));
604 } else {
605 tax.grand_total_for_current_item =
606 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
607 precision("total", tax));
608 }
609
610 // in tax.total, accumulate grand total for each item
611 tax.total += tax.grand_total_for_current_item;
Akhilesh Darjeec53dbc72013-12-25 19:46:20 +0530612
613 // set precision in the last item iteration
614 if (n == me.frm.item_doclist.length - 1) {
615 me.round_off_totals(tax);
616
617 // adjust flat discount loss in last tax iteration
618 if ((i == me.frm.tax_doclist.length - 1) && me.flat_discount_applied)
619 me.adjust_flat_discount_loss(tax);
620 }
Anand Doshi3543f302013-05-24 19:25:01 +0530621 });
622 });
623 },
Akhilesh Darjeec53dbc72013-12-25 19:46:20 +0530624
625 round_off_totals: function(tax) {
626 tax.total = flt(tax.total, precision("total", tax));
627 tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax));
628 tax.tax_amount_after_flat_discount = flt(tax.tax_amount_after_flat_discount,
629 precision("tax_amount", tax));
630 },
631
632 adjust_flat_discount_loss: function(tax) {
633 var flat_discount_loss = this.frm.doc.grand_total - this.frm.doc.flat_discount - tax.total;
634 tax.tax_amount_after_flat_discount = flt(tax.tax_amount_after_flat_discount +
635 flat_discount_loss, precision("tax_amount", tax));
636 tax.total = flt(tax.total + flat_discount_loss, precision("total", tax));
637 },
Anand Doshi3543f302013-05-24 19:25:01 +0530638
639 get_current_tax_amount: function(item, tax, item_tax_map) {
640 var tax_rate = this._get_tax_rate(tax, item_tax_map);
641 var current_tax_amount = 0.0;
642
643 if(tax.charge_type == "Actual") {
644 // distribute the tax amount proportionally to each item row
645 var actual = flt(tax.rate, precision("tax_amount", tax));
646 current_tax_amount = this.frm.doc.net_total ?
647 ((item.amount / this.frm.doc.net_total) * actual) :
648 0.0;
649
650 } else if(tax.charge_type == "On Net Total") {
651 current_tax_amount = (tax_rate / 100.0) * item.amount;
652
653 } else if(tax.charge_type == "On Previous Row Amount") {
654 current_tax_amount = (tax_rate / 100.0) *
655 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
656
657 } else if(tax.charge_type == "On Previous Row Total") {
658 current_tax_amount = (tax_rate / 100.0) *
659 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
Anand Doshi3543f302013-05-24 19:25:01 +0530660 }
Akhilesh Darjeeb09dc462013-12-23 15:49:08 +0530661
Anand Doshi53b73422013-05-31 11:50:01 +0530662 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
663
664 // store tax breakup for each item
665 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
666
667 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530668 },
669
670 _cleanup: function() {
671 $.each(this.frm.tax_doclist, function(i, tax) {
672 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
673 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
674 function(i, fieldname) { delete tax[fieldname]; });
675
676 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
677 });
678 },
Anand Doshifc777182013-05-27 19:29:07 +0530679
680 calculate_total_advance: function(parenttype, advance_parentfield) {
681 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
682 var advance_doclist = wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
683 {parentfield: advance_parentfield});
684 this.frm.doc.total_advance = flt(wn.utils.sum(
685 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
686 ), precision("total_advance"));
687
688 this.calculate_outstanding_amount();
689 }
690 },
Anand Doshi3543f302013-05-24 19:25:01 +0530691
692 _set_in_company_currency: function(item, print_field, base_field) {
693 // set values in base currency
694 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
695 precision(base_field, item));
696 },
Anand Doshibf3e54a2013-06-05 14:11:32 +0530697
698 get_terms: function() {
699 var me = this;
700 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530701 return this.frm.call({
Anand Doshibf3e54a2013-06-05 14:11:32 +0530702 method: "webnotes.client.get_value",
703 args: {
704 doctype: "Terms and Conditions",
705 fieldname: "terms",
706 filters: { name: this.frm.doc.tc_name },
707 },
708 });
709 }
710 },
Anand Doshi3543f302013-05-24 19:25:01 +0530711});