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