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