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