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