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