Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 1 | // ERPNext - web based ERP (http://erpnext.com) |
| 2 | // Copyright (C) 2012 Web Notes Technologies Pvt Ltd |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | wn.provide("erpnext"); |
| 18 | |
| 19 | erpnext.TransactionController = wn.ui.form.Controller.extend({ |
| 20 | onload: function() { |
| 21 | if(this.frm.doc.__islocal) { |
| 22 | var me = this, |
| 23 | today = get_today(), |
| 24 | currency = wn.defaults.get_default("currency"); |
| 25 | |
| 26 | $.each({ |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 27 | posting_date: today, |
| 28 | due_date: today, |
| 29 | transaction_date: today, |
| 30 | currency: currency, |
| 31 | price_list_currency: currency, |
| 32 | status: "Draft", |
| 33 | company: wn.defaults.get_default("company"), |
| 34 | fiscal_year: wn.defaults.get_default("fiscal_year"), |
| 35 | is_subcontracted: "No", |
| 36 | conversion_rate: 1.0, |
| 37 | plc_conversion_rate: 1.0 |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 38 | }, function(fieldname, value) { |
| 39 | if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname]) |
| 40 | me.frm.set_value(fieldname, value); |
| 41 | }); |
Anand Doshi | 535d833 | 2013-07-19 13:51:07 +0530 | [diff] [blame] | 42 | |
| 43 | me.frm.script_manager.trigger("company"); |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 44 | } |
| 45 | }, |
| 46 | |
| 47 | refresh: function() { |
| 48 | this.frm.clear_custom_buttons(); |
| 49 | erpnext.hide_naming_series(); |
Anand Doshi | bdee6e0 | 2013-07-09 13:03:39 +0530 | [diff] [blame] | 50 | erpnext.hide_company(); |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 51 | this.show_item_wise_taxes(); |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 52 | this.frm.fields_dict.currency ? this.currency() : this.set_dynamic_labels(); |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 53 | }, |
| 54 | |
| 55 | onload_post_render: function() { |
| 56 | if(this.frm.doc.__islocal && this.frm.doc.company) { |
| 57 | var me = this; |
| 58 | this.frm.call({ |
| 59 | doc: this.frm.doc, |
| 60 | method: "onload_post_render", |
| 61 | freeze: true, |
| 62 | callback: function(r) { |
| 63 | // remove this call when using client side mapper |
| 64 | me.set_default_values(); |
| 65 | me.frm.refresh(); |
| 66 | } |
| 67 | }); |
| 68 | } |
| 69 | }, |
| 70 | |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 71 | validate: function() { |
| 72 | this.calculate_taxes_and_totals(); |
| 73 | }, |
| 74 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 75 | company: function() { |
Anand Doshi | d52b03a | 2013-06-21 12:22:52 +0530 | [diff] [blame] | 76 | if(this.frm.doc.company && this.frm.fields_dict.currency) { |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 77 | var company_currency = this.get_company_currency(); |
Nabin Hait | 48a0bd3 | 2013-07-23 15:31:39 +0530 | [diff] [blame] | 78 | this.frm.set_value("currency", company_currency); |
| 79 | this.frm.script_manager.trigger("currency"); |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 80 | } |
| 81 | }, |
| 82 | |
| 83 | get_company_currency: function() { |
| 84 | return erpnext.get_currency(this.frm.doc.company); |
| 85 | }, |
| 86 | |
| 87 | currency: function() { |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 88 | var me = this; |
| 89 | this.set_dynamic_labels(); |
Anand Doshi | d52b03a | 2013-06-21 12:22:52 +0530 | [diff] [blame] | 90 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 91 | var company_currency = this.get_company_currency(); |
| 92 | if(this.frm.doc.currency !== company_currency) { |
| 93 | this.get_exchange_rate(this.frm.doc.currency, company_currency, |
| 94 | function(exchange_rate) { |
| 95 | if(exchange_rate) { |
| 96 | me.frm.set_value("conversion_rate", exchange_rate); |
| 97 | me.conversion_rate(); |
| 98 | } |
| 99 | }); |
| 100 | } else { |
| 101 | this.conversion_rate(); |
| 102 | } |
| 103 | }, |
| 104 | |
| 105 | conversion_rate: function() { |
Anand Doshi | 535d833 | 2013-07-19 13:51:07 +0530 | [diff] [blame] | 106 | if(this.frm.doc.currency === this.get_company_currency()) { |
| 107 | this.frm.set_value("conversion_rate", 1.0); |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 108 | } else if(this.frm.doc.currency === this.frm.doc.price_list_currency && |
| 109 | this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) { |
| 110 | this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate); |
| 111 | } |
| 112 | |
| 113 | this.calculate_taxes_and_totals(); |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 114 | }, |
| 115 | |
Anand Doshi | 060d924 | 2013-06-12 17:40:36 +0530 | [diff] [blame] | 116 | price_list_name: function(buying_or_selling) { |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 117 | var me = this; |
| 118 | if(this.frm.doc.price_list_name) { |
| 119 | this.frm.call({ |
| 120 | method: "setup.utils.get_price_list_currency", |
Nabin Hait | 48a0bd3 | 2013-07-23 15:31:39 +0530 | [diff] [blame] | 121 | args: { |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 122 | price_list_name: this.frm.doc.price_list_name, |
Nabin Hait | 48a0bd3 | 2013-07-23 15:31:39 +0530 | [diff] [blame] | 123 | }, |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 124 | callback: function(r) { |
| 125 | if(!r.exc) { |
| 126 | me.price_list_currency(); |
| 127 | } |
| 128 | } |
| 129 | }); |
| 130 | } |
| 131 | }, |
| 132 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 133 | get_exchange_rate: function(from_currency, to_currency, callback) { |
| 134 | var exchange_name = from_currency + "-" + to_currency; |
| 135 | wn.model.with_doc("Currency Exchange", exchange_name, function(name) { |
| 136 | var exchange_doc = wn.model.get_doc("Currency Exchange", exchange_name); |
Nabin Hait | 13f2e4d | 2013-06-25 10:56:39 +0530 | [diff] [blame] | 137 | callback(exchange_doc ? flt(exchange_doc.exchange_rate) : 0); |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 138 | }); |
| 139 | }, |
| 140 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 141 | price_list_currency: function() { |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 142 | this.set_dynamic_labels(); |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 143 | |
| 144 | var company_currency = this.get_company_currency(); |
| 145 | if(this.frm.doc.price_list_currency !== company_currency) { |
| 146 | this.get_exchange_rate(this.frm.doc.price_list_currency, company_currency, |
| 147 | function(exchange_rate) { |
| 148 | if(exchange_rate) { |
| 149 | me.frm.set_value("price_list_currency", exchange_rate); |
| 150 | me.plc_conversion_rate(); |
| 151 | } |
| 152 | }); |
| 153 | } else { |
| 154 | this.plc_conversion_rate(); |
| 155 | } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 156 | }, |
| 157 | |
| 158 | plc_conversion_rate: function() { |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 159 | if(this.frm.doc.price_list_currency === this.get_company_currency()) { |
| 160 | this.frm.set_value("plc_conversion_rate", 1.0); |
| 161 | } else if(this.frm.doc.price_list_currency === this.frm.doc.currency) { |
| 162 | this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate); |
| 163 | this.calculate_taxes_and_totals(); |
| 164 | } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 165 | }, |
| 166 | |
| 167 | qty: function(doc, cdt, cdn) { |
| 168 | this.calculate_taxes_and_totals(); |
| 169 | }, |
| 170 | |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 171 | tax_rate: function(doc, cdt, cdn) { |
| 172 | this.calculate_taxes_and_totals(); |
| 173 | }, |
| 174 | |
| 175 | row_id: function(doc, cdt, cdn) { |
| 176 | var tax = wn.model.get_doc(cdt, cdn); |
| 177 | try { |
| 178 | this.validate_on_previous_row(tax); |
| 179 | this.calculate_taxes_and_totals(); |
| 180 | } catch(e) { |
| 181 | tax.row_id = null; |
| 182 | refresh_field("row_id", tax.name, tax.parentfield); |
| 183 | throw e; |
| 184 | } |
| 185 | }, |
| 186 | |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 187 | set_dynamic_labels: function() { |
| 188 | // What TODO? should we make price list system non-mandatory? |
| 189 | this.frm.toggle_reqd("plc_conversion_rate", |
| 190 | !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency)); |
| 191 | |
| 192 | var company_currency = this.get_company_currency(); |
| 193 | this.change_form_labels(company_currency); |
| 194 | this.change_grid_labels(company_currency); |
| 195 | }, |
| 196 | |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 197 | recalculate: function() { |
| 198 | this.calculate_taxes_and_totals(); |
| 199 | }, |
| 200 | |
| 201 | recalculate_values: function() { |
| 202 | this.calculate_taxes_and_totals(); |
| 203 | }, |
| 204 | |
Anand Doshi | d0b0072 | 2013-05-28 18:54:48 +0530 | [diff] [blame] | 205 | calculate_charges: function() { |
| 206 | this.calculate_taxes_and_totals(); |
| 207 | }, |
| 208 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 209 | included_in_print_rate: function(doc, cdt, cdn) { |
| 210 | var tax = wn.model.get_doc(cdt, cdn); |
| 211 | try { |
| 212 | this.validate_on_previous_row(tax); |
| 213 | this.validate_inclusive_tax(tax); |
| 214 | this.calculate_taxes_and_totals(); |
| 215 | } catch(e) { |
| 216 | tax.included_in_print_rate = 0; |
| 217 | refresh_field("included_in_print_rate", tax.name, tax.parentfield); |
| 218 | throw e; |
| 219 | } |
| 220 | }, |
| 221 | |
| 222 | validate_on_previous_row: function(tax) { |
| 223 | // validate if a valid row id is mentioned in case of |
| 224 | // On Previous Row Amount and On Previous Row Total |
| 225 | if((["On Previous Row Amount", "On Previous Row Total"].indexOf(tax.charge_type) != -1) && |
| 226 | (!tax.row_id || cint(tax.row_id) >= tax.idx)) { |
| 227 | var msg = repl(wn._("Row") + " # %(idx)s [%(doctype)s]: " + |
| 228 | wn._("Please specify a valid") + " %(row_id_label)s", { |
| 229 | idx: tax.idx, |
| 230 | doctype: tax.doctype, |
| 231 | row_id_label: wn.meta.get_label(tax.doctype, "row_id", tax.name) |
| 232 | }); |
| 233 | msgprint(msg); |
| 234 | throw msg; |
| 235 | } |
| 236 | }, |
| 237 | |
| 238 | validate_inclusive_tax: function(tax) { |
| 239 | if(!this.frm.tax_doclist) this.frm.tax_doclist = this.get_tax_doclist(); |
| 240 | |
| 241 | var actual_type_error = function() { |
| 242 | var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " + |
| 243 | "%(charge_type_label)s = \"%(charge_type)s\" " + |
| 244 | wn._("cannot be included in Item's rate"), { |
| 245 | idx: tax.idx, |
| 246 | doctype: tax.doctype, |
| 247 | charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name), |
| 248 | charge_type: tax.charge_type |
| 249 | }); |
| 250 | msgprint(msg); |
| 251 | throw msg; |
| 252 | }; |
| 253 | |
| 254 | var on_previous_row_error = function(row_range) { |
| 255 | var msg = repl(wn._("For row") + " # %(idx)s [%(doctype)s]: " + |
| 256 | wn._("to be included in Item's rate, it is required that: ") + |
| 257 | " [" + wn._("Row") + " # %(row_range)s] " + wn._("also be included in Item's rate"), { |
| 258 | idx: tax.idx, |
| 259 | doctype: tax.doctype, |
| 260 | charge_type_label: wn.meta.get_label(tax.doctype, "charge_type", tax.name), |
| 261 | charge_type: tax.charge_type, |
| 262 | inclusive_label: wn.meta.get_label(tax.doctype, "included_in_print_rate", tax.name), |
| 263 | row_range: row_range, |
| 264 | }); |
| 265 | |
| 266 | msgprint(msg); |
| 267 | throw msg; |
| 268 | }; |
| 269 | |
| 270 | if(cint(tax.included_in_print_rate)) { |
| 271 | if(tax.charge_type == "Actual") { |
| 272 | // inclusive tax cannot be of type Actual |
| 273 | actual_type_error(); |
| 274 | } else if(tax.charge_type == "On Previous Row Amount" && |
| 275 | !cint(this.frm.tax_doclist[tax.row_id - 1].included_in_print_rate)) { |
| 276 | // referred row should also be an inclusive tax |
| 277 | on_previous_row_error(tax.row_id); |
| 278 | } else if(tax.charge_type == "On Previous Row Total") { |
| 279 | var taxes_not_included = $.map(this.frm.tax_doclist.slice(0, tax.row_id), |
| 280 | function(t) { return cint(t.included_in_print_rate) ? null : t; }); |
| 281 | if(taxes_not_included.length > 0) { |
| 282 | // all rows above this tax should be inclusive |
| 283 | on_previous_row_error(tax.row_id == 1 ? "1" : "1 - " + tax.row_id); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | }, |
| 288 | |
| 289 | _load_item_tax_rate: function(item_tax_rate) { |
| 290 | return item_tax_rate ? JSON.parse(item_tax_rate) : {}; |
| 291 | }, |
| 292 | |
| 293 | _get_tax_rate: function(tax, item_tax_map) { |
| 294 | return (keys(item_tax_map).indexOf(tax.account_head) != -1) ? |
| 295 | flt(item_tax_map[tax.account_head], precision("rate", tax)) : |
| 296 | tax.rate; |
| 297 | }, |
| 298 | |
| 299 | get_item_wise_taxes_html: function() { |
| 300 | var item_tax = {}; |
| 301 | var tax_accounts = []; |
| 302 | var company_currency = this.get_company_currency(); |
| 303 | |
| 304 | $.each(this.get_tax_doclist(), function(i, tax) { |
| 305 | var tax_amount_precision = precision("tax_amount", tax); |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 306 | var tax_rate_precision = precision("rate", tax); |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 307 | $.each(JSON.parse(tax.item_wise_tax_detail || '{}'), |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 308 | function(item_code, tax_data) { |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 309 | if(!item_tax[item_code]) item_tax[item_code] = {}; |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 310 | if($.isArray(tax_data)) { |
| 311 | var tax_rate = tax_data[0] == null ? "" : (flt(tax_data[0], tax_rate_precision) + "%"), |
| 312 | tax_amount = format_currency(flt(tax_data[1], tax_amount_precision), company_currency); |
| 313 | |
| 314 | item_tax[item_code][tax.account_head] = [tax_rate, tax_amount]; |
| 315 | } else { |
| 316 | item_tax[item_code][tax.account_head] = [flt(tax_data, tax_rate_precision) + "%", ""]; |
| 317 | } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 318 | }); |
| 319 | tax_accounts.push(tax.account_head); |
| 320 | }); |
| 321 | |
| 322 | var headings = $.map([wn._("Item Name")].concat(tax_accounts), |
| 323 | function(head) { return '<th style="min-width: 100px;">' + (head || "") + "</th>" }).join("\n"); |
| 324 | |
| 325 | var rows = $.map(this.get_item_doclist(), function(item) { |
| 326 | var item_tax_record = item_tax[item.item_code || item.item_name]; |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 327 | if(!item_tax_record) { return null; } |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 328 | return repl("<tr><td>%(item_name)s</td>%(taxes)s</tr>", { |
| 329 | item_name: item.item_name, |
| 330 | taxes: $.map(tax_accounts, function(head) { |
Anand Doshi | 64f316b | 2013-07-17 15:12:22 +0530 | [diff] [blame] | 331 | return item_tax_record[head] ? |
| 332 | "<td>(" + item_tax_record[head][0] + ") " + item_tax_record[head][1] + "</td>" : |
| 333 | "<td></td>"; |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 334 | }).join("\n") |
| 335 | }); |
| 336 | }).join("\n"); |
| 337 | |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 338 | if(!rows) return ""; |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 339 | return '<div style="overflow-x: scroll;"><table class="table table-bordered table-hover">\ |
| 340 | <thead><tr>' + headings + '</tr></thead> \ |
| 341 | <tbody>' + rows + '</tbody> \ |
| 342 | </table></div>'; |
| 343 | }, |
| 344 | |
| 345 | set_default_values: function() { |
| 346 | $.each(wn.model.get_doclist(this.frm.doctype, this.frm.docname), function(i, doc) { |
| 347 | var updated = wn.model.set_default_values(doc); |
| 348 | if(doc.parentfield) { |
| 349 | refresh_field(doc.parentfield); |
| 350 | } else { |
| 351 | refresh_field(updated); |
| 352 | } |
| 353 | }); |
| 354 | }, |
| 355 | |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 356 | _validate_before_fetch: function(fieldname) { |
| 357 | var me = this; |
| 358 | if(!me.frm.doc[fieldname]) { |
| 359 | return (wn._("Please specify") + ": " + |
| 360 | wn.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) + |
| 361 | ". " + wn._("It is needed to fetch Item Details.")); |
| 362 | } |
| 363 | return null; |
| 364 | }, |
| 365 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 366 | validate_company_and_party: function(party_field) { |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 367 | var me = this; |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 368 | var valid = true; |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 369 | var msg = ""; |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 370 | $.each(["company", party_field], function(i, fieldname) { |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 371 | var msg_for_fieldname = me._validate_before_fetch(fieldname); |
| 372 | if(msg_for_fieldname) { |
| 373 | msgprint(msg_for_fieldname); |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 374 | valid = false; |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 375 | } |
| 376 | }); |
| 377 | return valid; |
| 378 | }, |
| 379 | |
| 380 | get_item_doclist: function() { |
| 381 | return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name, |
| 382 | {parentfield: this.fname}); |
| 383 | }, |
| 384 | |
| 385 | get_tax_doclist: function() { |
| 386 | return wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name, |
| 387 | {parentfield: this.other_fname}); |
| 388 | }, |
| 389 | |
| 390 | validate_conversion_rate: function() { |
| 391 | this.frm.doc.conversion_rate = flt(this.frm.doc.conversion_rate, precision("conversion_rate")); |
| 392 | var conversion_rate_label = wn.meta.get_label(this.frm.doc.doctype, "conversion_rate", |
| 393 | this.frm.doc.name); |
| 394 | |
| 395 | if(this.frm.doc.conversion_rate == 0) { |
| 396 | wn.throw(wn._(conversion_rate_label) + " " + wn._("cannot be 0")); |
| 397 | } |
| 398 | |
| 399 | var company_currency = this.get_company_currency(); |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 400 | var valid_conversion_rate = this.frm.doc.conversion_rate ? |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 401 | ((this.frm.doc.currency == company_currency && this.frm.doc.conversion_rate == 1.0) || |
| 402 | (this.frm.doc.currency != company_currency && this.frm.doc.conversion_rate != 1.0)) : |
| 403 | false; |
| 404 | |
| 405 | if(!valid_conversion_rate) { |
| 406 | wn.throw(wn._("Please enter valid") + " " + wn._(conversion_rate_label) + |
| 407 | " 1 " + this.frm.doc.currency + " = [?] " + company_currency); |
| 408 | } |
| 409 | }, |
| 410 | |
| 411 | calculate_taxes_and_totals: function() { |
| 412 | this.validate_conversion_rate(); |
| 413 | this.frm.item_doclist = this.get_item_doclist(); |
| 414 | this.frm.tax_doclist = this.get_tax_doclist(); |
| 415 | |
| 416 | this.calculate_item_values(); |
| 417 | this.initialize_taxes(); |
| 418 | this.determine_exclusive_rate && this.determine_exclusive_rate(); |
| 419 | this.calculate_net_total(); |
| 420 | this.calculate_taxes(); |
| 421 | this.calculate_totals(); |
| 422 | this._cleanup(); |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 423 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 424 | this.show_item_wise_taxes(); |
| 425 | }, |
| 426 | |
| 427 | initialize_taxes: function() { |
| 428 | var me = this; |
| 429 | $.each(this.frm.tax_doclist, function(i, tax) { |
| 430 | tax.item_wise_tax_detail = {}; |
| 431 | $.each(["tax_amount", "total", |
| 432 | "tax_amount_for_current_item", "grand_total_for_current_item", |
| 433 | "tax_fraction_for_current_item", "grand_total_fraction_for_current_item"], |
| 434 | function(i, fieldname) { tax[fieldname] = 0.0 }); |
| 435 | |
| 436 | me.validate_on_previous_row(tax); |
| 437 | me.validate_inclusive_tax(tax); |
| 438 | wn.model.round_floats_in(tax); |
| 439 | }); |
| 440 | }, |
| 441 | |
| 442 | calculate_taxes: function() { |
| 443 | var me = this; |
| 444 | |
| 445 | $.each(this.frm.item_doclist, function(n, item) { |
| 446 | var item_tax_map = me._load_item_tax_rate(item.item_tax_rate); |
| 447 | |
| 448 | $.each(me.frm.tax_doclist, function(i, tax) { |
| 449 | // tax_amount represents the amount of tax for the current step |
| 450 | var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map); |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 451 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 452 | me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount); |
| 453 | |
| 454 | // case when net total is 0 but there is an actual type charge |
| 455 | // in this case add the actual amount to tax.tax_amount |
| 456 | // and tax.grand_total_for_current_item for the first such iteration |
| 457 | if(tax.charge_type == "Actual" && |
| 458 | !(current_tax_amount || me.frm.doc.net_total || tax.tax_amount)) { |
| 459 | var zero_net_total_adjustment = flt(tax.rate, precision("tax_amount", tax)); |
| 460 | current_tax_amount += zero_net_total_adjustment; |
| 461 | } |
| 462 | |
| 463 | // store tax_amount for current item as it will be used for |
| 464 | // charge type = 'On Previous Row Amount' |
| 465 | tax.tax_amount_for_current_item = current_tax_amount; |
| 466 | |
| 467 | // accumulate tax amount into tax.tax_amount |
| 468 | tax.tax_amount += current_tax_amount; |
| 469 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 470 | // for buying |
| 471 | if(tax.category) { |
| 472 | // if just for valuation, do not add the tax amount in total |
| 473 | // hence, setting it as 0 for further steps |
| 474 | current_tax_amount = (tax.category == "Valuation") ? 0.0 : current_tax_amount; |
| 475 | |
| 476 | current_tax_amount *= (tax.add_deduct_tax == "Deduct") ? -1.0 : 1.0; |
| 477 | } |
| 478 | |
| 479 | // Calculate tax.total viz. grand total till that step |
| 480 | // note: grand_total_for_current_item contains the contribution of |
| 481 | // item's amount, previously applied tax and the current tax on that item |
| 482 | if(i==0) { |
| 483 | tax.grand_total_for_current_item = flt(item.amount + current_tax_amount, |
| 484 | precision("total", tax)); |
| 485 | } else { |
| 486 | tax.grand_total_for_current_item = |
| 487 | flt(me.frm.tax_doclist[i-1].grand_total_for_current_item + current_tax_amount, |
| 488 | precision("total", tax)); |
| 489 | } |
| 490 | |
| 491 | // in tax.total, accumulate grand total for each item |
| 492 | tax.total += tax.grand_total_for_current_item; |
| 493 | }); |
| 494 | }); |
| 495 | }, |
| 496 | |
| 497 | get_current_tax_amount: function(item, tax, item_tax_map) { |
| 498 | var tax_rate = this._get_tax_rate(tax, item_tax_map); |
| 499 | var current_tax_amount = 0.0; |
| 500 | |
| 501 | if(tax.charge_type == "Actual") { |
| 502 | // distribute the tax amount proportionally to each item row |
| 503 | var actual = flt(tax.rate, precision("tax_amount", tax)); |
| 504 | current_tax_amount = this.frm.doc.net_total ? |
| 505 | ((item.amount / this.frm.doc.net_total) * actual) : |
| 506 | 0.0; |
| 507 | |
| 508 | } else if(tax.charge_type == "On Net Total") { |
| 509 | current_tax_amount = (tax_rate / 100.0) * item.amount; |
| 510 | |
| 511 | } else if(tax.charge_type == "On Previous Row Amount") { |
| 512 | current_tax_amount = (tax_rate / 100.0) * |
| 513 | this.frm.tax_doclist[cint(tax.row_id) - 1].tax_amount_for_current_item; |
| 514 | |
| 515 | } else if(tax.charge_type == "On Previous Row Total") { |
| 516 | current_tax_amount = (tax_rate / 100.0) * |
| 517 | this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item; |
| 518 | |
| 519 | } |
| 520 | |
Anand Doshi | 53b7342 | 2013-05-31 11:50:01 +0530 | [diff] [blame] | 521 | current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax)); |
| 522 | |
| 523 | // store tax breakup for each item |
| 524 | tax.item_wise_tax_detail[item.item_code || item.item_name] = [tax_rate, current_tax_amount]; |
| 525 | |
| 526 | return current_tax_amount; |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 527 | }, |
| 528 | |
| 529 | _cleanup: function() { |
| 530 | $.each(this.frm.tax_doclist, function(i, tax) { |
| 531 | $.each(["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) { delete tax[fieldname]; }); |
| 534 | |
| 535 | tax.item_wise_tax_detail = JSON.stringify(tax.item_wise_tax_detail); |
| 536 | }); |
| 537 | }, |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 538 | |
| 539 | calculate_total_advance: function(parenttype, advance_parentfield) { |
| 540 | if(this.frm.doc.doctype == parenttype && this.frm.doc.docstatus < 2) { |
| 541 | var advance_doclist = wn.model.get_doclist(this.frm.doc.doctype, this.frm.doc.name, |
| 542 | {parentfield: advance_parentfield}); |
| 543 | this.frm.doc.total_advance = flt(wn.utils.sum( |
| 544 | $.map(advance_doclist, function(adv) { return adv.allocated_amount }) |
| 545 | ), precision("total_advance")); |
| 546 | |
| 547 | this.calculate_outstanding_amount(); |
| 548 | } |
| 549 | }, |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 550 | |
| 551 | _set_in_company_currency: function(item, print_field, base_field) { |
| 552 | // set values in base currency |
| 553 | item[base_field] = flt(item[print_field] * this.frm.doc.conversion_rate, |
| 554 | precision(base_field, item)); |
| 555 | }, |
Anand Doshi | bf3e54a | 2013-06-05 14:11:32 +0530 | [diff] [blame] | 556 | |
| 557 | get_terms: function() { |
| 558 | var me = this; |
| 559 | if(this.frm.doc.tc_name) { |
| 560 | this.frm.call({ |
| 561 | method: "webnotes.client.get_value", |
| 562 | args: { |
| 563 | doctype: "Terms and Conditions", |
| 564 | fieldname: "terms", |
| 565 | filters: { name: this.frm.doc.tc_name }, |
| 566 | }, |
| 567 | }); |
| 568 | } |
| 569 | }, |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 570 | }); |