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