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