blob: 2886e1ca8f794e9d810da7bc6aebe1d9c439655b [file] [log] [blame]
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05301// Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
2// License: GNU General Public License v3. See license.txt
Anand Doshi3543f302013-05-24 19:25:01 +05303
4wn.provide("erpnext");
Anand Doshia648f462013-07-30 14:42:15 +05305wn.require("app/js/controllers/stock_controller.js");
Anand Doshi3543f302013-05-24 19:25:01 +05306
Anand Doshia648f462013-07-30 14:42:15 +05307erpnext.TransactionController = erpnext.stock.StockController.extend({
Anand Doshi3543f302013-05-24 19:25:01 +05308 onload: function() {
Anand Doshi1e4fb682013-08-23 12:56:33 +05309 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +053010 if(this.frm.doc.__islocal) {
Anand Doshi1e4fb682013-08-23 12:56:33 +053011 var today = get_today(),
Anand Doshi3543f302013-05-24 19:25:01 +053012 currency = wn.defaults.get_default("currency");
13
14 $.each({
Anand Doshifc777182013-05-27 19:29:07 +053015 posting_date: today,
16 due_date: today,
17 transaction_date: today,
18 currency: currency,
19 price_list_currency: currency,
20 status: "Draft",
21 company: wn.defaults.get_default("company"),
22 fiscal_year: wn.defaults.get_default("fiscal_year"),
23 is_subcontracted: "No",
Anand Doshi3543f302013-05-24 19:25:01 +053024 }, function(fieldname, value) {
25 if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname])
26 me.frm.set_value(fieldname, value);
Nabin Hait096d3632013-10-17 17:01:14 +053027 });
Anand Doshi3543f302013-05-24 19:25:01 +053028 }
Nabin Hait096d3632013-10-17 17:01:14 +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() {
Nabin Hait096d3632013-10-17 17:01:14 +053040 var me = this;
41 if(this.frm.doc.__islocal && this.frm.doc.company && !this.frm.doc.is_pos) {
42 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();
Nabin Hait096d3632013-10-17 17:01:14 +053051 me.calculate_taxes_and_totals();
Akhilesh Darjee78378272013-10-11 19:06:30 +053052 }
53 });
Nabin Hait096d3632013-10-17 17:01:14 +053054 } else {
55 this.calculate_taxes_and_totals();
56 }
Anand Doshi5e4e5d72013-08-06 17:23:44 +053057 }
58 },
59
Anand Doshi3543f302013-05-24 19:25:01 +053060 refresh: function() {
61 this.frm.clear_custom_buttons();
62 erpnext.hide_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +053063 erpnext.hide_company();
Anand Doshi3543f302013-05-24 19:25:01 +053064 this.show_item_wise_taxes();
Nabin Hait1f996c32013-07-29 19:00:53 +053065 this.set_dynamic_labels();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053066
67 // Show POS button only if it is enabled from features setup
68 if(cint(sys_defaults.fs_pos_view)===1 && this.frm.doctype!="Material Request")
69 this.pos_btn();
70 },
71
72 pos_btn: function() {
73 if(this.$pos_btn)
74 this.$pos_btn.remove();
75
76 if(!this.pos_active) {
77 var btn_label = wn._("POS View"),
78 icon = "icon-desktop";
79 } else {
80 var btn_label = wn._(this.frm.doctype) + wn._(" View"),
81 icon = "icon-file-text";
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053082 }
83 var me = this;
84
85 this.$pos_btn = this.frm.add_custom_button(btn_label, function() {
86 me.toggle_pos();
87 me.pos_btn();
88 }, icon);
89 },
90
91 toggle_pos: function(show) {
Akhilesh Darjeef83576b2013-09-18 18:35:12 +053092 // Check whether it is Selling or Buying cycle
93 var price_list = wn.meta.has_field(cur_frm.doc.doctype, "selling_price_list") ?
94 this.frm.doc.selling_price_list : this.frm.doc.buying_price_list;
95
96 if (!price_list)
Akhilesh Darjee6773bc22013-09-17 11:56:17 +053097 msgprint(wn._("Please select Price List"))
98 else {
99 if((show===true && this.pos_active) || (show===false && !this.pos_active)) return;
100
101 // make pos
Akhilesh Darjeef624ffa2013-09-23 12:27:16 +0530102 if(!this.frm.pos) {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530103 this.frm.layout.add_view("pos");
104 this.frm.pos = new erpnext.POS(this.frm.layout.views.pos, this.frm);
105 }
106
107 // toggle view
108 this.frm.layout.set_view(this.pos_active ? "" : "pos");
109 this.pos_active = !this.pos_active;
110
111 // refresh
112 if(this.pos_active)
113 this.frm.pos.refresh();
Akhilesh Darjeef83576b2013-09-18 18:35:12 +0530114 this.frm.refresh();
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530115 }
Anand Doshi3543f302013-05-24 19:25:01 +0530116 },
117
Anand Doshi923d41d2013-05-28 17:23:36 +0530118 validate: function() {
119 this.calculate_taxes_and_totals();
120 },
121
Anand Doshi5e4e5d72013-08-06 17:23:44 +0530122 set_default_values: function() {
123 $.each(wn.model.get_doclist(this.frm.doctype, this.frm.docname), function(i, doc) {
124 var updated = wn.model.set_default_values(doc);
125 if(doc.parentfield) {
126 refresh_field(doc.parentfield);
127 } else {
128 refresh_field(updated);
129 }
130 });
131 },
132
Anand Doshi3543f302013-05-24 19:25:01 +0530133 company: function() {
Anand Doshid52b03a2013-06-21 12:22:52 +0530134 if(this.frm.doc.company && this.frm.fields_dict.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530135 var company_currency = this.get_company_currency();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530136 if (!this.frm.doc.currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530137 this.frm.set_value("currency", company_currency);
Anand Doshi17350b82013-08-01 15:45:23 +0530138 }
139
Akhilesh Darjee78378272013-10-11 19:06:30 +0530140 if (this.frm.doc.currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530141 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530142 }
143 if (this.frm.doc.price_list_currency == company_currency) {
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530144 this.frm.set_value('plc_conversion_rate', 1.0);
Akhilesh Darjee78378272013-10-11 19:06:30 +0530145 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530146
Nabin Hait48a0bd32013-07-23 15:31:39 +0530147 this.frm.script_manager.trigger("currency");
Anand Doshi3543f302013-05-24 19:25:01 +0530148 }
149 },
150
151 get_company_currency: function() {
152 return erpnext.get_currency(this.frm.doc.company);
153 },
154
155 currency: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530156 var me = this;
157 this.set_dynamic_labels();
Akhilesh Darjee78378272013-10-11 19:06:30 +0530158
Anand Doshi61a2f682013-06-21 17:55:31 +0530159 var company_currency = this.get_company_currency();
160 if(this.frm.doc.currency !== company_currency) {
161 this.get_exchange_rate(this.frm.doc.currency, company_currency,
162 function(exchange_rate) {
Akhilesh Darjee78378272013-10-11 19:06:30 +0530163 me.frm.set_value("conversion_rate", exchange_rate);
164 me.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530165 });
166 } else {
167 this.conversion_rate();
168 }
169 },
170
171 conversion_rate: function() {
Anand Doshi535d8332013-07-19 13:51:07 +0530172 if(this.frm.doc.currency === this.get_company_currency()) {
173 this.frm.set_value("conversion_rate", 1.0);
Nabin Hait096d3632013-10-17 17:01:14 +0530174 }
175 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530176 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
177 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
178 }
Nabin Hait096d3632013-10-17 17:01:14 +0530179 if(flt(this.frm.doc.conversion_rate)>0.0) this.calculate_taxes_and_totals();
Anand Doshi3543f302013-05-24 19:25:01 +0530180 },
181
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530182 get_price_list_currency: function(buying_or_selling) {
Anand Doshi3543f302013-05-24 19:25:01 +0530183 var me = this;
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530184 var fieldname = buying_or_selling.toLowerCase() + "_price_list";
185 if(this.frm.doc[fieldname]) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530186 return this.frm.call({
Anand Doshi3543f302013-05-24 19:25:01 +0530187 method: "setup.utils.get_price_list_currency",
Nabin Hait48a0bd32013-07-23 15:31:39 +0530188 args: {
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530189 price_list: this.frm.doc[fieldname],
Nabin Hait48a0bd32013-07-23 15:31:39 +0530190 },
Anand Doshi3543f302013-05-24 19:25:01 +0530191 callback: function(r) {
192 if(!r.exc) {
193 me.price_list_currency();
194 }
195 }
196 });
197 }
198 },
199
Anand Doshi61a2f682013-06-21 17:55:31 +0530200 get_exchange_rate: function(from_currency, to_currency, callback) {
201 var exchange_name = from_currency + "-" + to_currency;
202 wn.model.with_doc("Currency Exchange", exchange_name, function(name) {
203 var exchange_doc = wn.model.get_doc("Currency Exchange", exchange_name);
Nabin Hait13f2e4d2013-06-25 10:56:39 +0530204 callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0);
Anand Doshi61a2f682013-06-21 17:55:31 +0530205 });
206 },
207
Anand Doshi3543f302013-05-24 19:25:01 +0530208 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530209 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530210 this.set_dynamic_labels();
Anand Doshi61a2f682013-06-21 17:55:31 +0530211
212 var company_currency = this.get_company_currency();
213 if(this.frm.doc.price_list_currency !== company_currency) {
214 this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency,
215 function(exchange_rate) {
216 if(exchange_rate) {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530217 me.frm.set_value("plc_conversion_rate", exchange_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530218 me.plc_conversion_rate();
219 }
220 });
221 } else {
222 this.plc_conversion_rate();
223 }
Anand Doshi3543f302013-05-24 19:25:01 +0530224 },
225
226 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530227 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
228 this.frm.set_value("plc_conversion_rate", 1.0);
Nabin Hait096d3632013-10-17 17:01:14 +0530229 }
230 if(this.frm.doc.price_list_currency === this.frm.doc.currency) {
Anand Doshi61a2f682013-06-21 17:55:31 +0530231 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
232 this.calculate_taxes_and_totals();
233 }
Anand Doshi3543f302013-05-24 19:25:01 +0530234 },
235
236 qty: function(doc, cdt, cdn) {
237 this.calculate_taxes_and_totals();
238 },
239
Anand Doshi923d41d2013-05-28 17:23:36 +0530240 tax_rate: function(doc, cdt, cdn) {
241 this.calculate_taxes_and_totals();
242 },
Akhilesh Darjee10dce342013-09-25 11:09:33 +0530243
Anand Doshi923d41d2013-05-28 17:23:36 +0530244 row_id: function(doc, cdt, cdn) {
245 var tax = wn.model.get_doc(cdt, cdn);
246 try {
247 this.validate_on_previous_row(tax);
248 this.calculate_taxes_and_totals();
249 } catch(e) {
250 tax.row_id = null;
251 refresh_field("row_id", tax.name, tax.parentfield);
252 throw e;
253 }
254 },
255
Anand Doshi61a2f682013-06-21 17:55:31 +0530256 set_dynamic_labels: function() {
257 // What TODO? should we make price list system non-mandatory?
258 this.frm.toggle_reqd("plc_conversion_rate",
259 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
260
261 var company_currency = this.get_company_currency();
262 this.change_form_labels(company_currency);
263 this.change_grid_labels(company_currency);
Anand Doshi5013dcb2013-08-05 12:16:04 +0530264 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +0530265 },
266
Anand Doshi923d41d2013-05-28 17:23:36 +0530267 recalculate: function() {
268 this.calculate_taxes_and_totals();
269 },
270
271 recalculate_values: function() {
272 this.calculate_taxes_and_totals();
273 },
274
Anand Doshid0b00722013-05-28 18:54:48 +0530275 calculate_charges: function() {
276 this.calculate_taxes_and_totals();
277 },
278
Anand Doshi3543f302013-05-24 19:25:01 +0530279 included_in_print_rate: function(doc, cdt, cdn) {
280 var tax = wn.model.get_doc(cdt, cdn);
281 try {
282 this.validate_on_previous_row(tax);
283 this.validate_inclusive_tax(tax);
284 this.calculate_taxes_and_totals();
285 } catch(e) {
286 tax.included_in_print_rate = 0;
287 refresh_field("included_in_print_rate", tax.name, tax.parentfield);
288 throw e;
289 }
290 },
291
292 validate_on_previous_row: function(tax) {
293 // validate if a valid row id is mentioned in case of
294 // On Previous Row Amount and On Previous Row Total
295 if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) &&
296 (!tax.row_id || cint(tax.row_id) >= tax.idx)) {
297 var msg = repl(wn._("Row") + " # %(idx)s [%(doctype)s]: " +
298 wn._("Please specify a valid") + " %(row_id_label)s", {
299 idx: tax.idx,
300 doctype: tax.doctype,
301 row_id_label: wn.meta.get_label(tax.doctype, "row_id", tax.name)
302 });
303 msgprint(msg);
304 throw msg;
305 }
306 },
307
308 validate_inclusive_tax: function(tax) {
309 if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist();
310
311 var actual_type_error = function() {
312 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
313 "%(charge_type_label)s = \"%(charge_type)s\" " +
314 wn._("cannot be included in Item's rate"), {
315 idx: tax.idx,
316 doctype: tax.doctype,
317 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
318 charge_type: tax.charge_type
319 });
320 msgprint(msg);
321 throw msg;
322 };
323
324 var on_previous_row_error = function(row_range) {
325 var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " +
326 wn._("to be included in Item's rate, it is required that: ") +
327 " [" + wn._("Row") + " # %(row_range)s] " + wn._("also be included in Item's rate"), {
328 idx: tax.idx,
329 doctype: tax.doctype,
330 charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name),
331 charge_type: tax.charge_type,
332 inclusive_label: wn.meta.get_label(tax.doctype, "included_in_print_rate", tax.name),
333 row_range: row_range,
334 });
335
336 msgprint(msg);
337 throw msg;
338 };
339
340 if(cint(tax.included_in_print_rate)) {
341 if(tax.charge_type == "Actual") {
342 // inclusive tax cannot be of type Actual
343 actual_type_error();
344 } else if(tax.charge_type == "On Previous Row Amount" &&
345 !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) {
346 // referred row should also be an inclusive tax
347 on_previous_row_error(tax.row_id);
348 } else if(tax.charge_type == "On Previous Row Total") {
349 var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id),
350 function(t) { return cint(t.included_in_print_rate) ? null : t; });
351 if(taxes_not_included.length > 0) {
352 // all rows above this tax should be inclusive
353 on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id);
354 }
355 }
356 }
357 },
358
359 _load_item_tax_rate: function(item_tax_rate) {
360 return item_tax_rate ? JSON.parse(item_tax_rate) : {};
361 },
362
363 _get_tax_rate: function(tax, item_tax_map) {
364 return (keys(item_tax_map).indexOf(tax.account_head) != -1) ?
365 flt(item_tax_map[tax.account_head], precision("rate", tax)) :
366 tax.rate;
367 },
368
369 get_item_wise_taxes_html: function() {
370 var item_tax = {};
371 var tax_accounts = [];
372 var company_currency = this.get_company_currency();
373
374 $.each(this.get_tax_doclist(), function(i, tax) {
375 var tax_amount_precision = precision("tax_amount", tax);
Anand Doshi53b73422013-05-31 11:50:01 +0530376 var tax_rate_precision = precision("rate", tax);
Anand Doshi3543f302013-05-24 19:25:01 +0530377 $.each(JSON.parse(tax.item_wise_tax_detail || '{}'),
Anand Doshi53b73422013-05-31 11:50:01 +0530378 function(item_code, tax_data) {
Anand Doshi3543f302013-05-24 19:25:01 +0530379 if(!item_tax[item_code]) item_tax[item_code] = {};
Anand Doshi53b73422013-05-31 11:50:01 +0530380 if($.isArray(tax_data)) {
Anand Doshi1e4fb682013-08-23 12:56:33 +0530381 var tax_rate = "";
382 if(tax_data[0] != null) {
383 tax_rate = (tax.charge_type === "Actual") ?
384 format_currency(flt(tax_data[0], tax_amount_precision), company_currency, tax_amount_precision) :
385 (flt(tax_data[0], tax_rate_precision) + "%");
386 }
387 var tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency,
388 tax_amount_precision);
Anand Doshi53b73422013-05-31 11:50:01 +0530389
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530390 item_tax[item_code][tax.name] = [tax_rate, tax_amount];
Anand Doshi53b73422013-05-31 11:50:01 +0530391 } else {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530392 item_tax[item_code][tax.name] = [flt(tax_data, tax_rate_precision) + "%", ""];
Anand Doshi53b73422013-05-31 11:50:01 +0530393 }
Anand Doshi3543f302013-05-24 19:25:01 +0530394 });
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530395 tax_accounts.push([tax.name, tax.account_head]);
Anand Doshi3543f302013-05-24 19:25:01 +0530396 });
397
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530398 var headings = $.map([wn._("Item Name")].concat($.map(tax_accounts, function(head) { return head[1]; })),
Anand Doshi3543f302013-05-24 19:25:01 +0530399 function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n");
Anand Doshi179e3772013-09-05 16:53:57 +0530400
401 var distinct_item_names = [];
402 var distinct_items = [];
403 $.each(this.get_item_doclist(), function(i, item) {
404 if(distinct_item_names.indexOf(item.item_code || item.item_name)===-1) {
405 distinct_item_names.push(item.item_code || item.item_name);
406 distinct_items.push(item);
407 }
408 });
Anand Doshi3543f302013-05-24 19:25:01 +0530409
Anand Doshi179e3772013-09-05 16:53:57 +0530410 var rows = $.map(distinct_items, function(item) {
Anand Doshi3543f302013-05-24 19:25:01 +0530411 var item_tax_record = item_tax[item.item_code || item.item_name];
Anand Doshi53b73422013-05-31 11:50:01 +0530412 if(!item_tax_record) { return null; }
Anand Doshi3543f302013-05-24 19:25:01 +0530413 return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", {
414 item_name: item.item_name,
415 taxes: $.map(tax_accounts, function(head) {
Anand Doshiaa71b5b2013-08-05 13:00:53 +0530416 return item_tax_record[head[0]] ?
417 "<td>(" + item_tax_record[head[0]][0] + ") " + item_tax_record[head[0]][1] + "</td>" :
Anand Doshi64f316b2013-07-17 15:12:22 +0530418 "<td></td>";
Anand Doshi3543f302013-05-24 19:25:01 +0530419 }).join("\n")
420 });
421 }).join("\n");
422
Anand Doshi53b73422013-05-31 11:50:01 +0530423 if(!rows) return "";
Anand Doshi3543f302013-05-24 19:25:01 +0530424 return '<div style="overflow-x: scroll;"><table class="table table-bordered table-hover">\
425 <thead><tr>' + headings + '</tr></thead> \
426 <tbody>' + rows + '</tbody> \
427 </table></div>';
428 },
429
Anand Doshi923d41d2013-05-28 17:23:36 +0530430 _validate_before_fetch: function(fieldname) {
431 var me = this;
432 if(!me.frm.doc[fieldname]) {
433 return (wn._("Please specify") + ": " +
434 wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
435 ". " + wn._("It is needed to fetch Item Details."));
436 }
437 return null;
438 },
439
Anand Doshi3543f302013-05-24 19:25:01 +0530440 validate_company_and_party: function(party_field) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530441 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +0530442 var valid = true;
Anand Doshi923d41d2013-05-28 17:23:36 +0530443 var msg = "";
Anand Doshi3543f302013-05-24 19:25:01 +0530444 $.each(["company", party_field], function(i, fieldname) {
Anand Doshi923d41d2013-05-28 17:23:36 +0530445 var msg_for_fieldname = me._validate_before_fetch(fieldname);
446 if(msg_for_fieldname) {
447 msgprint(msg_for_fieldname);
Anand Doshi3543f302013-05-24 19:25:01 +0530448 valid = false;
Anand Doshi3543f302013-05-24 19:25:01 +0530449 }
450 });
451 return valid;
452 },
453
454 get_item_doclist: function() {
455 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
456 {parentfield: this.fname});
457 },
458
459 get_tax_doclist: function() {
460 return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
461 {parentfield: this.other_fname});
462 },
463
464 validate_conversion_rate: function() {
465 this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate"));
466 var conversion_rate_label = wn.meta.get_label(this.frm.doc.doctype, "conversion_rate",
467 this.frm.doc.name);
468
469 if(this.frm.doc.conversion_rate == 0) {
470 wn.throw(wn._(conversion_rate_label) + " " + wn._("cannot be 0"));
471 }
472
473 var company_currency = this.get_company_currency();
Anand Doshi3543f302013-05-24 19:25:01 +0530474
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530475 if(!this.frm.doc.conversion_rate) {
Akhilesh Darjee6773bc22013-09-17 11:56:17 +0530476 wn.throw(wn._("Please enter valid") + " " + wn._(conversion_rate_label) +
477 " 1 " + this.frm.doc.currency + " = [?] " + company_currency);
478 }
Anand Doshi3543f302013-05-24 19:25:01 +0530479 },
480
481 calculate_taxes_and_totals: function() {
482 this.validate_conversion_rate();
483 this.frm.item_doclist = this.get_item_doclist();
484 this.frm.tax_doclist = this.get_tax_doclist();
485
486 this.calculate_item_values();
487 this.initialize_taxes();
488 this.determine_exclusive_rate && this.determine_exclusive_rate();
489 this.calculate_net_total();
490 this.calculate_taxes();
491 this.calculate_totals();
492 this._cleanup();
Anand Doshi923d41d2013-05-28 17:23:36 +0530493
Anand Doshi3543f302013-05-24 19:25:01 +0530494 this.show_item_wise_taxes();
495 },
496
497 initialize_taxes: function() {
498 var me = this;
499 $.each(this.frm.tax_doclist, function(i, tax) {
500 tax.item_wise_tax_detail = {};
501 $.each(["tax_amount", "total",
502 "tax_amount_for_current_item", "grand_total_for_current_item",
503 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
504 function(i, fieldname) { tax[fieldname] = 0.0 });
505
506 me.validate_on_previous_row(tax);
507 me.validate_inclusive_tax(tax);
508 wn.model.round_floats_in(tax);
509 });
510 },
511
512 calculate_taxes: function() {
513 var me = this;
514
515 $.each(this.frm.item_doclist, function(n, item) {
516 var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
517
518 $.each(me.frm.tax_doclist, function(i, tax) {
519 // tax_amount represents the amount of tax for the current step
520 var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
Anand Doshi53b73422013-05-31 11:50:01 +0530521
Anand Doshi3543f302013-05-24 19:25:01 +0530522 me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
523
524 // case when net total is 0 but there is an actual type charge
525 // in this case add the actual amount to tax.tax_amount
526 // and tax.grand_total_for_current_item for the first such iteration
527 if(tax.charge_type == "Actual" &&
528 !(current_tax_amount || me.frm.doc.net_total || tax.tax_amount)) {
529 var zero_net_total_adjustment = flt(tax.rate, precision("tax_amount", tax));
530 current_tax_amount += zero_net_total_adjustment;
531 }
532
533 // store tax_amount for current item as it will be used for
534 // charge type = 'On Previous Row Amount'
535 tax.tax_amount_for_current_item = current_tax_amount;
536
537 // accumulate tax amount into tax.tax_amount
538 tax.tax_amount += current_tax_amount;
539
Anand Doshi3543f302013-05-24 19:25:01 +0530540 // for buying
541 if(tax.category) {
542 // if just for valuation, do not add the tax amount in total
543 // hence, setting it as 0 for further steps
544 current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount;
545
546 current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0;
547 }
548
549 // Calculate tax.total viz. grand total till that step
550 // note: grand_total_for_current_item contains the contribution of
551 // item's amount, previously applied tax and the current tax on that item
552 if(i==0) {
553 tax.grand_total_for_current_item = flt(item.amount + current_tax_amount,
554 precision("total", tax));
555 } else {
556 tax.grand_total_for_current_item =
557 flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount,
558 precision("total", tax));
559 }
560
561 // in tax.total, accumulate grand total for each item
562 tax.total += tax.grand_total_for_current_item;
563 });
564 });
565 },
566
567 get_current_tax_amount: function(item, tax, item_tax_map) {
568 var tax_rate = this._get_tax_rate(tax, item_tax_map);
569 var current_tax_amount = 0.0;
570
571 if(tax.charge_type == "Actual") {
572 // distribute the tax amount proportionally to each item row
573 var actual = flt(tax.rate, precision("tax_amount", tax));
574 current_tax_amount = this.frm.doc.net_total ?
575 ((item.amount / this.frm.doc.net_total) * actual) :
576 0.0;
577
578 } else if(tax.charge_type == "On Net Total") {
579 current_tax_amount = (tax_rate / 100.0) * item.amount;
580
581 } else if(tax.charge_type == "On Previous Row Amount") {
582 current_tax_amount = (tax_rate / 100.0) *
583 this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item;
584
585 } else if(tax.charge_type == "On Previous Row Total") {
586 current_tax_amount = (tax_rate / 100.0) *
587 this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
588
589 }
590
Anand Doshi53b73422013-05-31 11:50:01 +0530591 current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
592
593 // store tax breakup for each item
594 tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount];
595
596 return current_tax_amount;
Anand Doshi3543f302013-05-24 19:25:01 +0530597 },
598
599 _cleanup: function() {
600 $.each(this.frm.tax_doclist, function(i, tax) {
601 $.each(["tax_amount_for_current_item", "grand_total_for_current_item",
602 "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
603 function(i, fieldname) { delete tax[fieldname]; });
604
605 tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail);
606 });
607 },
Anand Doshifc777182013-05-27 19:29:07 +0530608
609 calculate_total_advance: function(parenttype, advance_parentfield) {
610 if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) {
611 var advance_doclist = wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name,
612 {parentfield: advance_parentfield});
613 this.frm.doc.total_advance = flt(wn.utils.sum(
614 $.map(advance_doclist, function(adv) { return adv.allocated_amount })
615 ), precision("total_advance"));
616
617 this.calculate_outstanding_amount();
618 }
619 },
Anand Doshi3543f302013-05-24 19:25:01 +0530620
621 _set_in_company_currency: function(item, print_field, base_field) {
622 // set values in base currency
623 item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate,
624 precision(base_field, item));
625 },
Anand Doshibf3e54a2013-06-05 14:11:32 +0530626
627 get_terms: function() {
628 var me = this;
629 if(this.frm.doc.tc_name) {
Anand Doshi1fac2a92013-07-29 19:30:39 +0530630 return this.frm.call({
Anand Doshibf3e54a2013-06-05 14:11:32 +0530631 method: "webnotes.client.get_value",
632 args: {
633 doctype: "Terms and Conditions",
634 fieldname: "terms",
635 filters: { name: this.frm.doc.tc_name },
636 },
637 });
638 }
639 },
Anand Doshi3543f302013-05-24 19:25:01 +0530640});