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