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