blob: 1e03833bd6a8efc159ca10b5061621e2b02f87cf [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() {
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({
Anand Doshi3543f302013-05-24 19:25:01 +0530217 method: "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 });
Akhilesh Darjee806017c2014-01-07 18:37:38 +0530333 wn.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530334 }
335 },
336
337 validate_inclusive_tax: function(tax) {
338 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
339
340 var actual_type_error = function() {
341 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
342 "%(charge_type_label)s = \"%(charge_type)s\" " +
343 wn._("cannot be included in Item's rate"), {
344 idx: tax.idx,
345 doctype: tax.doctype,
346 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
347 charge_type: tax.charge_type
348 });
Akhilesh Darjee806017c2014-01-07 18:37:38 +0530349 wn.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530350 };
351
352 var on_previous_row_error = function(row_range) {
353 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
354 wn._("to be included in Item's rate, it is required that: ") +
355 " [" + wn._("Row") + " # %(row_range)s] " + wn._("also be included in Item's rate"), {
356 idx: tax.idx,
357 doctype: tax.doctype,
358 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
359 charge_type: tax.charge_type,
360 inclusive_label: wn.meta.get_label(tax.doctype, "included_in_print_rate", tax.name),
361 row_range: row_range,
362 });
363
Akhilesh Darjee806017c2014-01-07 18:37:38 +0530364 wn.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530365 };
366
367 if(cint(tax.included_in_print_rate)) {
368 if(tax.charge_type == "Actual") {
369 // inclusive tax cannot be of type Actual
370 actual_type_error();
371 } else if(tax.charge_type == "On Previous Row Amount" &&
372 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
373 // referred row should also be an inclusive tax
374 on_previous_row_error(tax.row_id);
375 } else if(tax.charge_type == "On Previous Row Total") {
376 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
377 function(t) { return cint(t.included_in_print_rate) ? null : t; });
378 if(taxes_not_included.length > 0) {
379 // all rows above this tax should be inclusive
380 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
381 }
382 }
383 }
384 },
385
386 _load_item_tax_rate: function(item_tax_rate) {
387 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
388 },
389
390 _get_tax_rate: function(tax, item_tax_map) {
391 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
392 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
393 tax.rate;
394 },
395
396 get_item_wise_taxes_html: function() {
397 var item_tax = {};
398 var tax_accounts = [];
399 var company_currency = this.get_company_currency();
400
401 $.each(this.get_tax_doclist(), function(i, tax) {
402 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530403 var tax_rate_precision = precision("rate", tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530404 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530405 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530406 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530407 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530408 var tax_rate = "";
409 if(tax_data[0] != null) {
410 tax_rate = (tax.charge_type === "Actual") ?
411 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
412 (flt(tax_data[0], tax_rate_precision) + "%");
413 }
414 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
415 tax_amount_precision);
Anand Doshi53b73422013-05-31 11:50:01 +0530416
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530417 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530418 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530419 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530420 }
Anand Doshi3543f302013-05-24 19:25:01 +0530421 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530422 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530423 });
424
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530425 var headings = $.map([wn._("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530426 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Anand Doshi179e3772013-09-05 16:53:57 +0530427
428 var distinct_item_names = [];
429 var distinct_items = [];
430 $.each(this.get_item_doclist(), function(i, item) {
431 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
432 distinct_item_names.push(item.item_code || item.item_name);
433 distinct_items.push(item);
434 }
435 });
Anand Doshi3543f302013-05-24 19:25:01 +0530436
Anand Doshi179e3772013-09-05 16:53:57 +0530437 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530438 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530439 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530440 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
441 item_name: item.item_name,
442 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530443 return item_tax_record[head[0]] ?
444 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530445 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530446 }).join("\n")
447 });
448 }).join("\n");
449
Anand Doshi53b73422013-05-31 11:50:01 +0530450 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530451 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
452 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530453 <thead><tr>' + headings + '</tr></thead> \
454 <tbody>' + rows + '</tbody> \
455 </table></div>';
456 },
457
Anand Doshi923d41d2013-05-28 17:23:36 +0530458 _validate_before_fetch: function(fieldname) {
459 var me = this;
460 if(!me.frm.doc[fieldname]) {
461 return (wn._("Please specify") + ": " +
462 wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
463 ". " + wn._("It is needed to fetch Item Details."));
464 }
465 return null;
466 },
467
Anand Doshi3543f302013-05-24 19:25:01 +0530468 validate_company_and_party: function(party_field) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530469 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530470 var valid = true;
Anand Doshi923d41d2013-05-28 17:23:36 +0530471 var msg = "";
Anand Doshi3543f302013-05-24 19:25:01 +0530472 $.each(["company", party_field], function(i, fieldname) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530473 var msg_for_fieldname = me._validate_before_fetch(fieldname);
474 if(msg_for_fieldname) {
475 msgprint(msg_for_fieldname);
Anand Doshi3543f302013-05-24 19:25:01 +0530476 valid = false;
Anand Doshi3543f302013-05-24 19:25:01 +0530477 }
478 });
479 return valid;
480 },
481
482 get_item_doclist: function() {
483 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
484 {parentfield: this.fname});
485 },
486
487 get_tax_doclist: function() {
488 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
489 {parentfield: this.other_fname});
490 },
491
492 validate_conversion_rate: function() {
493 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
494 var conversion_rate_label = wn.meta.get_label(this.frm.doc.doctype, "conversion_rate",
495 this.frm.doc.name);
Anand Doshi3543f302013-05-24 19:25:01 +0530496 var company_currency = this.get_company_currency();
Anand Doshi3543f302013-05-24 19:25:01 +0530497
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530498 if(!this.frm.doc.conversion_rate) {
Nabin Haite505fbe2013-11-15 13:38:23 +0530499 wn.throw(repl('%(conversion_rate_label)s' +
500 wn._(' is mandatory. Maybe Currency Exchange record is not created for ') +
501 '%(from_currency)s' + wn._(" to ") + '%(to_currency)s',
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530502 {
503 "conversion_rate_label": conversion_rate_label,
Nabin Haite505fbe2013-11-15 13:38:23 +0530504 "from_currency": this.frm.doc.currency,
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530505 "to_currency": company_currency
Nabin Haite505fbe2013-11-15 13:38:23 +0530506 }));
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530507 }
Anand Doshi3543f302013-05-24 19:25:01 +0530508 },
509
510 calculate_taxes_and_totals: function() {
511 this.validate_conversion_rate();
512 this.frm.item_doclist = this.get_item_doclist();
513 this.frm.tax_doclist = this.get_tax_doclist();
514
515 this.calculate_item_values();
516 this.initialize_taxes();
517 this.determine_exclusive_rate && this.determine_exclusive_rate();
518 this.calculate_net_total();
519 this.calculate_taxes();
520 this.calculate_totals();
521 this._cleanup();
Anand Doshi923d41d2013-05-28 17:23:36 +0530522
Anand Doshi3543f302013-05-24 19:25:01 +0530523 this.show_item_wise_taxes();
524 },
525
526 initialize_taxes: function() {
527 var me = this;
528 $.each(this.frm.tax_doclist, function(i, tax) {
529 tax.item_wise_tax_detail = {};
530 $.each(["tax_amount", "total",
531 "tax_amount_for_current_item", "grand_total_for_current_item",
532 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
533 function(i, fieldname) { tax[fieldname] = 0.0 });
534
535 me.validate_on_previous_row(tax);
536 me.validate_inclusive_tax(tax);
537 wn.model.round_floats_in(tax);
538 });
539 },
540
541 calculate_taxes: function() {
542 var me = this;
Nabin Hait3a193702014-01-14 17:44:34 +0530543 var actual_tax_dict = {};
544
545 // maintain actual tax rate based on idx
546 $.each(this.frm.tax_doclist, function(i, tax) {
547 if (tax.charge_type == "Actual") {
548 actual_tax_dict[tax.idx] = flt(tax.rate);
549 }
550 });
Anand Doshi3543f302013-05-24 19:25:01 +0530551
552 $.each(this.frm.item_doclist, function(n, item) {
553 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
554
555 $.each(me.frm.tax_doclist, function(i, tax) {
556 // tax_amount represents the amount of tax for the current step
557 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530558
Anand Doshi3543f302013-05-24 19:25:01 +0530559 me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
Nabin Hait3a193702014-01-14 17:44:34 +0530560
561 // Adjust divisional loss to the last item
562 if (tax.charge_type == "Actual") {
563 actual_tax_dict[tax.idx] -= current_tax_amount;
564 if (n == me.frm.item_doclist.length - 1) {
565 current_tax_amount += actual_tax_dict[tax.idx]
Anand Doshi3543f302013-05-24 19:25:01 +0530566 }
Nabin Hait3a193702014-01-14 17:44:34 +0530567 }
568
Anand Doshi3543f302013-05-24 19:25:01 +0530569
570 // store tax_amount for current item as it will be used for
571 // charge type = 'On Previous Row Amount'
572 tax.tax_amount_for_current_item = current_tax_amount;
573
574 // accumulate tax amount into tax.tax_amount
575 tax.tax_amount += current_tax_amount;
576
Anand Doshi3543f302013-05-24 19:25:01 +0530577 // for buying
578 if(tax.category) {
579 // if just for valuation, do not add the tax amount in total
580 // hence, setting it as 0 for further steps
581 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
582
583 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
584 }
585
586 // Calculate tax.total viz. grand total till that step
587 // note: grand_total_for_current_item contains the contribution of
588 // item's amount, previously applied tax and the current tax on that item
589 if(i==0) {
590 tax.grand_total_for_current_item = flt(item.amount + current_tax_amount,
591 precision("total", tax));
592 } else {
593 tax.grand_total_for_current_item =
594 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
595 precision("total", tax));
596 }
597
598 // in tax.total, accumulate grand total for each item
599 tax.total += tax.grand_total_for_current_item;
Nabin Hait3a193702014-01-14 17:44:34 +0530600
601 if (n == me.frm.item_doclist.length - 1) {
602 tax.total = flt(tax.total, precision("total", tax));
603 tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax));
604 }
Anand Doshi3543f302013-05-24 19:25:01 +0530605 });
606 });
607 },
608
609 get_current_tax_amount: function(item, tax, item_tax_map) {
610 var tax_rate = this._get_tax_rate(tax, item_tax_map);
611 var current_tax_amount = 0.0;
612
613 if(tax.charge_type == "Actual") {
614 // distribute the tax amount proportionally to each item row
615 var actual = flt(tax.rate, precision("tax_amount", tax));
616 current_tax_amount = this.frm.doc.net_total ?
617 ((item.amount / this.frm.doc.net_total) * actual) :
618 0.0;
619
620 } else if(tax.charge_type == "On Net Total") {
621 current_tax_amount = (tax_rate / 100.0) * item.amount;
622
623 } else if(tax.charge_type == "On Previous Row Amount") {
624 current_tax_amount = (tax_rate / 100.0) *
625 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
626
627 } else if(tax.charge_type == "On Previous Row Total") {
628 current_tax_amount = (tax_rate / 100.0) *
629 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
630
631 }
632
Anand Doshi53b73422013-05-31 11:50:01 +0530633 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
634
635 // store tax breakup for each item
636 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
637
638 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530639 },
640
641 _cleanup: function() {
642 $.each(this.frm.tax_doclist, function(i, tax) {
643 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
644 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
645 function(i, fieldname) { delete tax[fieldname]; });
646
647 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
648 });
649 },
Anand Doshifc777182013-05-27 19:29:07 +0530650
651 calculate_total_advance: function(parenttype, advance_parentfield) {
652 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
653 var advance_doclist = wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
654 {parentfield: advance_parentfield});
655 this.frm.doc.total_advance = flt(wn.utils.sum(
656 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
657 ), precision("total_advance"));
658
659 this.calculate_outstanding_amount();
660 }
661 },
Anand Doshi3543f302013-05-24 19:25:01 +0530662
663 _set_in_company_currency: function(item, print_field, base_field) {
664 // set values in base currency
665 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
666 precision(base_field, item));
667 },
Anand Doshibf3e54a2013-06-05 14:11:32 +0530668
669 get_terms: function() {
670 var me = this;
671 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530672 return this.frm.call({
Anand Doshibf3e54a2013-06-05 14:11:32 +0530673 method: "webnotes.client.get_value",
674 args: {
675 doctype: "Terms and Conditions",
676 fieldname: "terms",
677 filters: { name: this.frm.doc.tc_name },
678 },
679 });
680 }
681 },
Anand Doshi3543f302013-05-24 19:25:01 +0530682});