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