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