blob: d77819f7d7f98d3e731c63b4141c21f8c265d051 [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 },
Nabin Hait139dc7b2014-02-12 14:53:18 +0530113
114
115 item_code: function(doc, cdt, cdn) {
116 var me = this;
117 var item = wn.model.get_doc(cdt, cdn);
118 if(item.item_code || item.barcode || item.serial_no) {
119 if(!this.validate_company_and_party()) {
120 cur_frm.fields_dict[me.frm.cscript.fname].grid.grid_rows[item.idx - 1].remove();
121 } else {
122 return this.frm.call({
123 method: "erpnext.stock.get_item_details.get_item_details",
124 child: item,
125 args: {
126 args: {
127 item_code: item.item_code,
128 barcode: item.barcode,
129 serial_no: item.serial_no,
130 warehouse: item.warehouse,
131 doctype: me.frm.doc.doctype,
132 docname: me.frm.doc.name,
133 customer: me.frm.doc.customer,
134 supplier: me.frm.doc.supplier,
135 currency: me.frm.doc.currency,
136 conversion_rate: me.frm.doc.conversion_rate,
137 price_list: me.frm.doc.selling_price_list ||
138 me.frm.doc.buying_price_list,
139 price_list_currency: me.frm.doc.price_list_currency,
140 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
141 company: me.frm.doc.company,
142 order_type: me.frm.doc.order_type,
143 is_pos: cint(me.frm.doc.is_pos),
144 is_subcontracted: me.frm.doc.is_subcontracted,
145 transaction_date: me.frm.doc.transaction_date
146 }
147 },
148 callback: function(r) {
149 if(!r.exc) {
150 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
151 }
152 }
153 });
154 }
155 }
156 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530157
158 serial_no: function(doc, cdt, cdn) {
159 var me = this;
160 var item = wn.model.get_doc(cdt, cdn);
161
162 if (item.serial_no) {
163 if (!item.item_code) {
164 this.frm.script_manager.trigger("item_code", cdt, cdn);
165 }
166 else {
167 var sr_no = [];
168
169 // Replacing all occurences of comma with carriage return
170 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
171
172 serial_nos = serial_nos.trim().split('\n');
173
174 // Trim each string and push unique string to new list
175 for (var x=0; x<=serial_nos.length - 1; x++) {
176 if (serial_nos[x].trim() != "" && sr_no.indexOf(serial_nos[x].trim()) == -1) {
177 sr_no.push(serial_nos[x].trim());
178 }
179 }
180
181 // Add the new list to the serial no. field in grid with each in new line
182 item.serial_no = "";
183 for (var x=0; x<=sr_no.length - 1; x++)
184 item.serial_no += sr_no[x] + '\n';
185
186 refresh_field("serial_no", item.name, item.parentfield);
187 wn.model.set_value(item.doctype, item.name, "qty", sr_no.length);
188 }
189 }
190 },
Anand Doshi3543f302013-05-24 19:25:01 +0530191
Anand Doshi923d41d2013-05-28 17:23:36 +0530192 validate: function() {
193 this.calculate_taxes_and_totals();
194 },
195
Anand Doshi5e4e5d72013-08-06 17:23:44 +0530196 set_default_values: function() {
197 $.each(wn.model.get_doclist(this.frm.doctype, this.frm.docname), function(i, doc) {
198 var updated = wn.model.set_default_values(doc);
199 if(doc.parentfield) {
200 refresh_field(doc.parentfield);
201 } else {
202 refresh_field(updated);
203 }
204 });
205 },
206
Anand Doshi3543f302013-05-24 19:25:01 +0530207 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +0530208 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530209 var company_currency = this.get_company_currency();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530210 if (!this.frm.doc.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530211 this.frm.set_value("currency", company_currency);
Anand Doshi17350b82013-08-01 15:45:23 +0530212 }
213
Akhilesh Darjee78378272013-10-11 19:06:30 +0530214 if (this.frm.doc.currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530215 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530216 }
217 if (this.frm.doc.price_list_currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530218 this.frm.set_value('plc_conversion_rate', 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530219 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530220
Nabin Hait48a0bd32013-07-23 15:31:39 +0530221 this.frm.script_manager.trigger("currency");
Anand Doshi3543f302013-05-24 19:25:01 +0530222 }
223 },
224
225 get_company_currency: function() {
226 return erpnext.get_currency(this.frm.doc.company);
227 },
228
229 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530230 var me = this;
231 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530232
Anand Doshi61a2f682013-06-21 17:55:31 +0530233 var company_currency = this.get_company_currency();
234 if(this.frm.doc.currency !== company_currency) {
235 this.get_exchange_rate(this.frm.doc.currency, company_currency,
236 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530237 me.frm.set_value("conversion_rate", exchange_rate);
238 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530239 });
240 } else {
241 this.conversion_rate();
242 }
243 },
244
245 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530246 if(this.frm.doc.currency === this.get_company_currency()) {
247 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530248 }
249 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530250 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
251 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
252 }
Nabin Haitb338b6e2013-10-15 16:55:24 +0530253 if(flt(this.frm.doc.conversion_rate)>0.0) this.calculate_taxes_and_totals();
Anand Doshi3543f302013-05-24 19:25:01 +0530254 },
255
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530256 get_price_list_currency: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +0530257 var me = this;
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530258 var fieldname = buying_or_selling.toLowerCase() + "_price_list";
259 if(this.frm.doc[fieldname]) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530260 return this.frm.call({
Rushabh Mehta1f847992013-12-12 19:12:19 +0530261 method: "erpnext.setup.utils.get_price_list_currency",
Nabin Hait48a0bd32013-07-23 15:31:39 +0530262 args: {
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530263 price_list: this.frm.doc[fieldname],
Nabin Hait48a0bd32013-07-23 15:31:39 +0530264 },
Anand Doshi3543f302013-05-24 19:25:01 +0530265 callback: function(r) {
266 if(!r.exc) {
267 me.price_list_currency();
268 }
269 }
270 });
271 }
272 },
273
Anand Doshi61a2f682013-06-21 17:55:31 +0530274 get_exchange_rate: function(from_currency, to_currency, callback) {
275 var exchange_name = from_currency + "-" + to_currency;
276 wn.model.with_doc("Currency Exchange", exchange_name, function(name) {
277 var exchange_doc = wn.model.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530278 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530279 });
280 },
281
Anand Doshi3543f302013-05-24 19:25:01 +0530282 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530283 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530284 this.set_dynamic_labels();
Anand Doshi61a2f682013-06-21 17:55:31 +0530285
286 var company_currency = this.get_company_currency();
287 if(this.frm.doc.price_list_currency !== company_currency) {
288 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
289 function(exchange_rate) {
290 if(exchange_rate) {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530291 me.frm.set_value("plc_conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530292 me.plc_conversion_rate();
293 }
294 });
295 } else {
296 this.plc_conversion_rate();
297 }
Anand Doshi3543f302013-05-24 19:25:01 +0530298 },
299
300 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530301 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
302 this.frm.set_value("plc_conversion_rate", 1.0);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530303 }
304 if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
Anand Doshi61a2f682013-06-21 17:55:31 +0530305 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
306 this.calculate_taxes_and_totals();
307 }
Anand Doshi3543f302013-05-24 19:25:01 +0530308 },
309
310 qty: function(doc, cdt, cdn) {
311 this.calculate_taxes_and_totals();
312 },
313
Anand Doshi923d41d2013-05-28 17:23:36 +0530314 tax_rate: function(doc, cdt, cdn) {
315 this.calculate_taxes_and_totals();
316 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530317
Anand Doshi923d41d2013-05-28 17:23:36 +0530318 row_id: function(doc, cdt, cdn) {
319 var tax = wn.model.get_doc(cdt, cdn);
320 try {
321 this.validate_on_previous_row(tax);
322 this.calculate_taxes_and_totals();
323 } catch(e) {
324 tax.row_id = null;
325 refresh_field("row_id", tax.name, tax.parentfield);
326 throw e;
327 }
328 },
329
Anand Doshi61a2f682013-06-21 17:55:31 +0530330 set_dynamic_labels: function() {
331 // What TODO? should we make price list system non-mandatory?
332 this.frm.toggle_reqd("plc_conversion_rate",
333 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
334
335 var company_currency = this.get_company_currency();
336 this.change_form_labels(company_currency);
337 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530338 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530339 },
340
Anand Doshi923d41d2013-05-28 17:23:36 +0530341 recalculate: function() {
342 this.calculate_taxes_and_totals();
343 },
344
345 recalculate_values: function() {
346 this.calculate_taxes_and_totals();
347 },
348
Anand Doshid0b00722013-05-28 18:54:48 +0530349 calculate_charges: function() {
350 this.calculate_taxes_and_totals();
351 },
352
Anand Doshi3543f302013-05-24 19:25:01 +0530353 included_in_print_rate: function(doc, cdt, cdn) {
354 var tax = wn.model.get_doc(cdt, cdn);
355 try {
356 this.validate_on_previous_row(tax);
357 this.validate_inclusive_tax(tax);
358 this.calculate_taxes_and_totals();
359 } catch(e) {
360 tax.included_in_print_rate = 0;
361 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
362 throw e;
363 }
364 },
365
366 validate_on_previous_row: function(tax) {
367 // validate if a valid row id is mentioned in case of
368 // On Previous Row Amount and On Previous Row Total
Bárbara Perretti534af0b2013-10-08 15:00:07 -0300369 if(([wn._("On Previous Row Amount"), wn._("On Previous Row Total")].indexOf(tax.charge_type) != -1) &&
Anand Doshi3543f302013-05-24 19:25:01 +0530370 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
371 var msg = repl(wn._("Row") + " # %(idx)s [%(doctype)s]: " +
372 wn._("Please specify a valid") + " %(row_id_label)s", {
373 idx: tax.idx,
374 doctype: tax.doctype,
375 row_id_label: wn.meta.get_label(tax.doctype, "row_id", tax.name)
376 });
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530377 wn.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530378 }
379 },
380
381 validate_inclusive_tax: function(tax) {
382 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
383
384 var actual_type_error = function() {
385 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
386 "%(charge_type_label)s = \"%(charge_type)s\" " +
387 wn._("cannot be included in Item's rate"), {
388 idx: tax.idx,
389 doctype: tax.doctype,
390 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
391 charge_type: tax.charge_type
392 });
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530393 wn.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530394 };
395
396 var on_previous_row_error = function(row_range) {
397 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
398 wn._("to be included in Item's rate, it is required that: ") +
399 " [" + wn._("Row") + " # %(row_range)s] " + wn._("also be included in Item's rate"), {
400 idx: tax.idx,
401 doctype: tax.doctype,
402 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
403 charge_type: tax.charge_type,
404 inclusive_label: wn.meta.get_label(tax.doctype, "included_in_print_rate", tax.name),
405 row_range: row_range,
406 });
407
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530408 wn.throw(msg);
Anand Doshi3543f302013-05-24 19:25:01 +0530409 };
410
411 if(cint(tax.included_in_print_rate)) {
412 if(tax.charge_type == "Actual") {
413 // inclusive tax cannot be of type Actual
414 actual_type_error();
415 } else if(tax.charge_type == "On Previous Row Amount" &&
416 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
417 // referred row should also be an inclusive tax
418 on_previous_row_error(tax.row_id);
419 } else if(tax.charge_type == "On Previous Row Total") {
420 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
421 function(t) { return cint(t.included_in_print_rate) ? null : t; });
422 if(taxes_not_included.length > 0) {
423 // all rows above this tax should be inclusive
424 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
425 }
426 }
427 }
428 },
429
430 _load_item_tax_rate: function(item_tax_rate) {
431 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
432 },
433
434 _get_tax_rate: function(tax, item_tax_map) {
435 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
436 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
437 tax.rate;
438 },
439
440 get_item_wise_taxes_html: function() {
441 var item_tax = {};
442 var tax_accounts = [];
443 var company_currency = this.get_company_currency();
444
445 $.each(this.get_tax_doclist(), function(i, tax) {
446 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530447 var tax_rate_precision = precision("rate", tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530448 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530449 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530450 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530451 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530452 var tax_rate = "";
453 if(tax_data[0] != null) {
454 tax_rate = (tax.charge_type === "Actual") ?
455 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
456 (flt(tax_data[0], tax_rate_precision) + "%");
457 }
458 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
459 tax_amount_precision);
Anand Doshi53b73422013-05-31 11:50:01 +0530460
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530461 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530462 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530463 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530464 }
Anand Doshi3543f302013-05-24 19:25:01 +0530465 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530466 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530467 });
468
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530469 var headings = $.map([wn._("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530470 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Anand Doshi179e3772013-09-05 16:53:57 +0530471
472 var distinct_item_names = [];
473 var distinct_items = [];
474 $.each(this.get_item_doclist(), function(i, item) {
475 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
476 distinct_item_names.push(item.item_code || item.item_name);
477 distinct_items.push(item);
478 }
479 });
Anand Doshi3543f302013-05-24 19:25:01 +0530480
Anand Doshi179e3772013-09-05 16:53:57 +0530481 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530482 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530483 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530484 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
485 item_name: item.item_name,
486 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530487 return item_tax_record[head[0]] ?
488 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530489 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530490 }).join("\n")
491 });
492 }).join("\n");
493
Anand Doshi53b73422013-05-31 11:50:01 +0530494 if(!rows) return "";
Rushabh Mehta7d8b1892013-11-08 16:27:18 +0530495 return '<p><a href="#" onclick="$(\'.tax-break-up\').toggleClass(\'hide\'); return false;">Show / Hide tax break-up</a><br><br></p>\
496 <div class="tax-break-up hide" style="overflow-x: auto;"><table class="table table-bordered table-hover">\
Anand Doshi3543f302013-05-24 19:25:01 +0530497 <thead><tr>' + headings + '</tr></thead> \
498 <tbody>' + rows + '</tbody> \
499 </table></div>';
500 },
501
Nabin Hait139dc7b2014-02-12 14:53:18 +0530502 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530503 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530504 var valid = true;
Nabin Hait139dc7b2014-02-12 14:53:18 +0530505
506 $.each(["company", "customer", "supplier"], function(i, fieldname) {
507 if(wn.meta.has_field(me.frm.doc.doctype, fieldname)) {
508 if (!me.frm.doc[fieldname]) {
509 msgprint(wn._("Please specify") + ": " +
510 wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
511 ". " + wn._("It is needed to fetch Item Details."));
512 valid = false;
513 }
Anand Doshi3543f302013-05-24 19:25:01 +0530514 }
515 });
516 return valid;
517 },
518
519 get_item_doclist: function() {
520 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
521 {parentfield: this.fname});
522 },
523
524 get_tax_doclist: function() {
525 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
526 {parentfield: this.other_fname});
527 },
528
529 validate_conversion_rate: function() {
530 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
531 var conversion_rate_label = wn.meta.get_label(this.frm.doc.doctype, "conversion_rate",
532 this.frm.doc.name);
Anand Doshi3543f302013-05-24 19:25:01 +0530533 var company_currency = this.get_company_currency();
Anand Doshi3543f302013-05-24 19:25:01 +0530534
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530535 if(!this.frm.doc.conversion_rate) {
Nabin Haite505fbe2013-11-15 13:38:23 +0530536 wn.throw(repl('%(conversion_rate_label)s' +
537 wn._(' is mandatory. Maybe Currency Exchange record is not created for ') +
538 '%(from_currency)s' + wn._(" to ") + '%(to_currency)s',
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530539 {
540 "conversion_rate_label": conversion_rate_label,
Nabin Haite505fbe2013-11-15 13:38:23 +0530541 "from_currency": this.frm.doc.currency,
Akhilesh Darjeee0b32822013-11-12 19:27:16 +0530542 "to_currency": company_currency
Nabin Haite505fbe2013-11-15 13:38:23 +0530543 }));
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530544 }
Anand Doshi3543f302013-05-24 19:25:01 +0530545 },
546
547 calculate_taxes_and_totals: function() {
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530548 this.discount_amount_applied = false;
549 this._calculate_taxes_and_totals();
550 if (wn.meta.get_docfield(this.frm.doc.doctype, "discount_amount"))
551 this.apply_discount_amount();
552 },
553
554 _calculate_taxes_and_totals: function() {
Anand Doshi3543f302013-05-24 19:25:01 +0530555 this.validate_conversion_rate();
556 this.frm.item_doclist = this.get_item_doclist();
557 this.frm.tax_doclist = this.get_tax_doclist();
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530558
Anand Doshi3543f302013-05-24 19:25:01 +0530559 this.calculate_item_values();
560 this.initialize_taxes();
561 this.determine_exclusive_rate && this.determine_exclusive_rate();
562 this.calculate_net_total();
563 this.calculate_taxes();
564 this.calculate_totals();
565 this._cleanup();
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530566
Anand Doshi3543f302013-05-24 19:25:01 +0530567 this.show_item_wise_taxes();
568 },
569
570 initialize_taxes: function() {
571 var me = this;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530572
Anand Doshi3543f302013-05-24 19:25:01 +0530573 $.each(this.frm.tax_doclist, function(i, tax) {
574 tax.item_wise_tax_detail = {};
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530575 tax_fields = ["total", "tax_amount_after_discount_amount",
Anand Doshi3543f302013-05-24 19:25:01 +0530576 "tax_amount_for_current_item", "grand_total_for_current_item",
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530577 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
578
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530579 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530580 tax_fields.push("tax_amount");
581
582 $.each(tax_fields, function(i, fieldname) { tax[fieldname] = 0.0 });
Anand Doshi3543f302013-05-24 19:25:01 +0530583
584 me.validate_on_previous_row(tax);
585 me.validate_inclusive_tax(tax);
586 wn.model.round_floats_in(tax);
587 });
588 },
589
590 calculate_taxes: function() {
591 var me = this;
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530592 var actual_tax_dict = {};
Anand Doshi3543f302013-05-24 19:25:01 +0530593
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530594 // maintain actual tax rate based on idx
595 $.each(this.frm.tax_doclist, function(i, tax) {
596 if (tax.charge_type == "Actual") {
597 actual_tax_dict[tax.idx] = flt(tax.rate);
598 }
599 });
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530600
Anand Doshi3543f302013-05-24 19:25:01 +0530601 $.each(this.frm.item_doclist, function(n, item) {
602 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530603
Anand Doshi3543f302013-05-24 19:25:01 +0530604 $.each(me.frm.tax_doclist, function(i, tax) {
605 // tax_amount represents the amount of tax for the current step
606 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530607
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530608 // Adjust divisional loss to the last item
609 if (tax.charge_type == "Actual") {
610 actual_tax_dict[tax.idx] -= current_tax_amount;
611 if (n == me.frm.item_doclist.length - 1) {
612 current_tax_amount += actual_tax_dict[tax.idx]
Anand Doshi3543f302013-05-24 19:25:01 +0530613 }
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530614 }
615
Anand Doshi3543f302013-05-24 19:25:01 +0530616 // store tax_amount for current item as it will be used for
617 // charge type = 'On Previous Row Amount'
618 tax.tax_amount_for_current_item = current_tax_amount;
619
620 // accumulate tax amount into tax.tax_amount
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530621 if (!me.discount_amount_applied)
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530622 tax.tax_amount += current_tax_amount;
623
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530624 tax.tax_amount_after_discount_amount += current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530625
Anand Doshi3543f302013-05-24 19:25:01 +0530626 // for buying
627 if(tax.category) {
628 // if just for valuation, do not add the tax amount in total
629 // hence, setting it as 0 for further steps
630 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
631
632 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
633 }
634
635 // Calculate tax.total viz. grand total till that step
636 // note: grand_total_for_current_item contains the contribution of
637 // item's amount, previously applied tax and the current tax on that item
638 if(i==0) {
Nabin Hait1eb56012014-02-10 19:20:15 +0530639 tax.grand_total_for_current_item = flt(item.base_amount + current_tax_amount,
Anand Doshi3543f302013-05-24 19:25:01 +0530640 precision("total", tax));
641 } else {
642 tax.grand_total_for_current_item =
643 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
644 precision("total", tax));
645 }
646
647 // in tax.total, accumulate grand total for each item
648 tax.total += tax.grand_total_for_current_item;
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530649
650 // set precision in the last item iteration
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530651 if (n == me.frm.item_doclist.length - 1) {
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530652 me.round_off_totals(tax);
653
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530654 // adjust Discount Amount loss in last tax iteration
655 if ((i == me.frm.tax_doclist.length - 1) && me.discount_amount_applied)
656 me.adjust_discount_amount_loss(tax);
Nabin Haitdc15b4f2014-01-20 16:48:49 +0530657 }
Anand Doshi3543f302013-05-24 19:25:01 +0530658 });
659 });
660 },
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530661
662 round_off_totals: function(tax) {
663 tax.total = flt(tax.total, precision("total", tax));
664 tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax));
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530665 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount,
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530666 precision("tax_amount", tax));
667 },
668
Akhilesh Darjee57738a02014-01-03 18:15:07 +0530669 adjust_discount_amount_loss: function(tax) {
670 var discount_amount_loss = this.frm.doc.grand_total - flt(this.frm.doc.discount_amount) - tax.total;
671 tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
672 discount_amount_loss, precision("tax_amount", tax));
673 tax.total = flt(tax.total + discount_amount_loss, precision("total", tax));
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530674 },
Anand Doshi3543f302013-05-24 19:25:01 +0530675
676 get_current_tax_amount: function(item, tax, item_tax_map) {
677 var tax_rate = this._get_tax_rate(tax, item_tax_map);
678 var current_tax_amount = 0.0;
679
680 if(tax.charge_type == "Actual") {
681 // distribute the tax amount proportionally to each item row
682 var actual = flt(tax.rate, precision("tax_amount", tax));
683 current_tax_amount = this.frm.doc.net_total ?
Nabin Hait1eb56012014-02-10 19:20:15 +0530684 ((item.base_amount / this.frm.doc.net_total) * actual) :
Anand Doshi3543f302013-05-24 19:25:01 +0530685 0.0;
686
687 } else if(tax.charge_type == "On Net Total") {
Nabin Hait1eb56012014-02-10 19:20:15 +0530688 current_tax_amount = (tax_rate / 100.0) * item.base_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530689
690 } else if(tax.charge_type == "On Previous Row Amount") {
691 current_tax_amount = (tax_rate / 100.0) *
692 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
693
694 } else if(tax.charge_type == "On Previous Row Total") {
695 current_tax_amount = (tax_rate / 100.0) *
696 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
Anand Doshi3543f302013-05-24 19:25:01 +0530697 }
Akhilesh Darjeed203aea2013-12-27 17:49:57 +0530698
Anand Doshi53b73422013-05-31 11:50:01 +0530699 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
700
701 // store tax breakup for each item
702 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
703
704 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530705 },
706
707 _cleanup: function() {
708 $.each(this.frm.tax_doclist, function(i, tax) {
709 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
710 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
711 function(i, fieldname) { delete tax[fieldname]; });
712
713 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
714 });
715 },
Anand Doshifc777182013-05-27 19:29:07 +0530716
717 calculate_total_advance: function(parenttype, advance_parentfield) {
718 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
719 var advance_doclist = wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
720 {parentfield: advance_parentfield});
721 this.frm.doc.total_advance = flt(wn.utils.sum(
722 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
723 ), precision("total_advance"));
724
725 this.calculate_outstanding_amount();
726 }
727 },
Anand Doshi3543f302013-05-24 19:25:01 +0530728
729 _set_in_company_currency: function(item, print_field, base_field) {
730 // set values in base currency
731 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
732 precision(base_field, item));
733 },
Anand Doshibf3e54a2013-06-05 14:11:32 +0530734
735 get_terms: function() {
736 var me = this;
737 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530738 return this.frm.call({
Anand Doshibf3e54a2013-06-05 14:11:32 +0530739 method: "webnotes.client.get_value",
740 args: {
741 doctype: "Terms and Conditions",
742 fieldname: "terms",
743 filters: { name: this.frm.doc.tc_name },
744 },
745 });
746 }
747 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +0530748
749 taxes_and_charges: function() {
750 var me = this;
751 if(this.frm.doc.taxes_and_charges) {
752 return this.frm.call({
753 doc: this.frm.doc,
754 method: "get_other_charges",
755 callback: function(r) {
756 if(!r.exc) {
757 me.calculate_taxes_and_totals();
758 }
759 }
760 });
761 }
762 },
763
764 show_item_wise_taxes: function() {
765 if(this.frm.fields_dict.other_charges_calculation) {
766 $(this.get_item_wise_taxes_html())
767 .appendTo($(this.frm.fields_dict.other_charges_calculation.wrapper).empty());
768 }
769 },
Anand Doshi3543f302013-05-24 19:25:01 +0530770});