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