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