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