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 |
Rushabh Mehta | 3966f1d | 2012-02-23 12:35:32 +0530 | [diff] [blame] | 3 | |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 4 | // Preset |
| 5 | // ------ |
| 6 | // cur_frm.cscript.tname - Details table name |
| 7 | // cur_frm.cscript.fname - Details fieldname |
Rushabh Mehta | 6de403f | 2013-12-13 14:10:14 +0530 | [diff] [blame] | 8 | // cur_frm.cscript.other_fname - fieldname |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 9 | // cur_frm.cscript.sales_team_fname - Sales Team fieldname |
| 10 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 11 | frappe.provide("erpnext.selling"); |
| 12 | frappe.require("assets/erpnext/js/transaction.js"); |
Rushabh Mehta | b09d9da | 2014-01-02 11:47:23 +0530 | [diff] [blame] | 13 | |
Rushabh Mehta | 6de403f | 2013-12-13 14:10:14 +0530 | [diff] [blame] | 14 | {% include "public/js/controllers/accounts.js" %} |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 15 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 16 | erpnext.selling.SellingController = erpnext.TransactionController.extend({ |
Anand Doshi | 9b49614 | 2013-07-11 19:13:58 +0530 | [diff] [blame] | 17 | onload: function() { |
| 18 | this._super(); |
| 19 | this.toggle_rounded_total(); |
| 20 | this.setup_queries(); |
Anand Doshi | 33fe867 | 2013-08-02 12:39:10 +0530 | [diff] [blame] | 21 | this.toggle_editable_price_list_rate(); |
Anand Doshi | 9b49614 | 2013-07-11 19:13:58 +0530 | [diff] [blame] | 22 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 23 | |
Rushabh Mehta | b6843f2 | 2014-06-04 13:10:41 +0530 | [diff] [blame] | 24 | onload_post_render: function() { |
Rushabh Mehta | 880b3ef | 2014-06-06 11:45:11 +0530 | [diff] [blame] | 25 | 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] | 26 | }, |
| 27 | |
Anand Doshi | 9b49614 | 2013-07-11 19:13:58 +0530 | [diff] [blame] | 28 | setup_queries: function() { |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 29 | var me = this; |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 30 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 31 | this.frm.add_fetch("sales_partner", "commission_rate", "commission_rate"); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 32 | |
| 33 | $.each([["customer_address", "customer_filter"], |
Anand Doshi | d5d39ac | 2013-07-29 13:28:37 +0530 | [diff] [blame] | 34 | ["shipping_address_name", "customer_filter"], |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 35 | ["contact_person", "customer_filter"], |
| 36 | ["customer", "customer"], |
| 37 | ["lead", "lead"]], |
Anand Doshi | d5d39ac | 2013-07-29 13:28:37 +0530 | [diff] [blame] | 38 | function(i, opts) { |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 39 | if(me.frm.fields_dict[opts[0]]) |
Anand Doshi | d5d39ac | 2013-07-29 13:28:37 +0530 | [diff] [blame] | 40 | me.frm.set_query(opts[0], erpnext.queries[opts[1]]); |
| 41 | }); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 42 | |
Akhilesh Darjee | 4f72156 | 2014-01-29 16:31:38 +0530 | [diff] [blame] | 43 | if(this.frm.fields_dict.taxes_and_charges) { |
| 44 | this.frm.set_query("taxes_and_charges", function() { |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 45 | return { |
| 46 | filters: [ |
| 47 | ['Sales Taxes and Charges Master', 'company', '=', me.frm.doc.company], |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 48 | ['Sales Taxes and Charges Master', 'docstatus', '!=', 2] |
| 49 | ] |
| 50 | } |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 51 | }); |
| 52 | } |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 53 | |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 54 | if(this.frm.fields_dict.selling_price_list) { |
| 55 | this.frm.set_query("selling_price_list", function() { |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 56 | return { filters: { selling: 1 } }; |
Anand Doshi | 720a01a | 2013-07-26 11:32:02 +0530 | [diff] [blame] | 57 | }); |
Anand Doshi | 720a01a | 2013-07-26 11:32:02 +0530 | [diff] [blame] | 58 | } |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 59 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 60 | if(!this.fname) { |
| 61 | return; |
| 62 | } |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 63 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 64 | if(this.frm.fields_dict[this.fname].grid.get_field('item_code')) { |
| 65 | this.frm.set_query("item_code", this.fname, function() { |
Anand Doshi | 9b49614 | 2013-07-11 19:13:58 +0530 | [diff] [blame] | 66 | return { |
Rushabh Mehta | 6de403f | 2013-12-13 14:10:14 +0530 | [diff] [blame] | 67 | query: "erpnext.controllers.queries.item_query", |
Anand Doshi | 9b49614 | 2013-07-11 19:13:58 +0530 | [diff] [blame] | 68 | filters: (me.frm.doc.order_type === "Maintenance" ? |
| 69 | {'is_service_item': 'Yes'}: |
| 70 | {'is_sales_item': 'Yes' }) |
| 71 | } |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 72 | }); |
| 73 | } |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 74 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 75 | if(this.frm.fields_dict[this.fname].grid.get_field('batch_no')) { |
| 76 | this.frm.set_query("batch_no", this.fname, function(doc, cdt, cdn) { |
Rushabh Mehta | 66d52b5 | 2014-03-27 14:17:33 +0530 | [diff] [blame] | 77 | var item = frappe.get_doc(cdt, cdn); |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 78 | if(!item.item_code) { |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 79 | frappe.throw(__("Please enter Item Code to get batch no")); |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 80 | } else { |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 81 | filters = { |
| 82 | 'item_code': item.item_code, |
| 83 | 'posting_date': me.frm.doc.posting_date, |
| 84 | } |
| 85 | if(item.warehouse) filters["warehouse"] = item.warehouse |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 86 | |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 87 | return { |
Akhilesh Darjee | 4f72156 | 2014-01-29 16:31:38 +0530 | [diff] [blame] | 88 | query : "erpnext.controllers.queries.get_batch_no", |
Nabin Hait | d1fd1e2 | 2013-10-18 12:29:11 +0530 | [diff] [blame] | 89 | filters: filters |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | }); |
| 93 | } |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 94 | |
Anand Doshi | ed69892 | 2013-07-23 15:16:50 +0530 | [diff] [blame] | 95 | if(this.frm.fields_dict.sales_team && this.frm.fields_dict.sales_team.grid.get_field("sales_person")) { |
Anand Doshi | d5d39ac | 2013-07-29 13:28:37 +0530 | [diff] [blame] | 96 | this.frm.set_query("sales_person", "sales_team", erpnext.queries.not_a_group_filter); |
Anand Doshi | ed69892 | 2013-07-23 15:16:50 +0530 | [diff] [blame] | 97 | } |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 98 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 99 | |
Anand Doshi | c4a54fe | 2013-08-01 18:19:51 +0530 | [diff] [blame] | 100 | refresh: function() { |
| 101 | this._super(); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 102 | this.frm.toggle_display("customer_name", |
Akhilesh Darjee | 5ce1b8b | 2013-12-09 16:29:04 +0530 | [diff] [blame] | 103 | (this.frm.doc.customer_name && this.frm.doc.customer_name!==this.frm.doc.customer)); |
Anand Doshi | c4a54fe | 2013-08-01 18:19:51 +0530 | [diff] [blame] | 104 | if(this.frm.fields_dict.packing_details) { |
Rushabh Mehta | aa355af | 2014-03-26 18:24:30 +0530 | [diff] [blame] | 105 | var packing_list_exists = (this.frm.doc.packing_details || []).length; |
Anand Doshi | c4a54fe | 2013-08-01 18:19:51 +0530 | [diff] [blame] | 106 | this.frm.toggle_display("packing_list", packing_list_exists ? true : false); |
| 107 | } |
Rushabh Mehta | 8aded13 | 2013-07-04 12:50:52 +0530 | [diff] [blame] | 108 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 109 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 110 | customer: function() { |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 111 | var me = this; |
| 112 | erpnext.utils.get_party_details(this.frm, null, null, function(){me.apply_pricing_rule()}); |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 113 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 114 | |
Nabin Hait | a279d78 | 2013-07-15 13:04:33 +0530 | [diff] [blame] | 115 | customer_address: function() { |
Rushabh Mehta | b09d9da | 2014-01-02 11:47:23 +0530 | [diff] [blame] | 116 | erpnext.utils.get_address_display(this.frm, "customer_address"); |
Nabin Hait | a279d78 | 2013-07-15 13:04:33 +0530 | [diff] [blame] | 117 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 118 | |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 119 | shipping_address_name: function() { |
| 120 | erpnext.utils.get_address_display(this.frm, "shipping_address_name", "shipping_address"); |
| 121 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 122 | |
Nabin Hait | a279d78 | 2013-07-15 13:04:33 +0530 | [diff] [blame] | 123 | contact_person: function() { |
Rushabh Mehta | b09d9da | 2014-01-02 11:47:23 +0530 | [diff] [blame] | 124 | erpnext.utils.get_contact_details(this.frm); |
Nabin Hait | a279d78 | 2013-07-15 13:04:33 +0530 | [diff] [blame] | 125 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 126 | |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 127 | sales_partner: function() { |
| 128 | this.apply_pricing_rule(); |
| 129 | }, |
| 130 | |
| 131 | campaign: function() { |
| 132 | this.apply_pricing_rule(); |
| 133 | }, |
| 134 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 135 | barcode: function(doc, cdt, cdn) { |
| 136 | this.item_code(doc, cdt, cdn); |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 137 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 138 | |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 139 | selling_price_list: function() { |
| 140 | this.get_price_list_currency("Selling"); |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 141 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 142 | |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 143 | price_list_rate: function(doc, cdt, cdn) { |
Rushabh Mehta | 66d52b5 | 2014-03-27 14:17:33 +0530 | [diff] [blame] | 144 | var item = frappe.get_doc(cdt, cdn); |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 145 | frappe.model.round_floats_in(item, ["price_list_rate", "discount_percentage"]); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 146 | |
Nabin Hait | 7979f7e | 2014-02-10 18:26:49 +0530 | [diff] [blame] | 147 | item.rate = flt(item.price_list_rate * (1 - item.discount_percentage / 100.0), |
| 148 | precision("rate", item)); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 149 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 150 | this.calculate_taxes_and_totals(); |
| 151 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 152 | |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 153 | discount_percentage: function(doc, cdt, cdn) { |
Rushabh Mehta | 66d52b5 | 2014-03-27 14:17:33 +0530 | [diff] [blame] | 154 | var item = frappe.get_doc(cdt, cdn); |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 155 | if(!item.price_list_rate) { |
| 156 | item.discount_percentage = 0.0; |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 157 | } else { |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 158 | this.price_list_rate(doc, cdt, cdn); |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 159 | } |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 160 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 161 | |
Nabin Hait | 7979f7e | 2014-02-10 18:26:49 +0530 | [diff] [blame] | 162 | rate: function(doc, cdt, cdn) { |
Rushabh Mehta | 66d52b5 | 2014-03-27 14:17:33 +0530 | [diff] [blame] | 163 | var item = frappe.get_doc(cdt, cdn); |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 164 | frappe.model.round_floats_in(item, ["rate", "price_list_rate"]); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 165 | |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 166 | if(item.price_list_rate) { |
Nabin Hait | 7979f7e | 2014-02-10 18:26:49 +0530 | [diff] [blame] | 167 | item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0, |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 168 | precision("discount_percentage", item)); |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 169 | } else { |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 170 | item.discount_percentage = 0.0; |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 171 | } |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 172 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 173 | this.calculate_taxes_and_totals(); |
| 174 | }, |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 175 | |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 176 | discount_amount: function() { |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 177 | this.calculate_taxes_and_totals(); |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 178 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 179 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 180 | commission_rate: function() { |
| 181 | this.calculate_commission(); |
| 182 | refresh_field("total_commission"); |
| 183 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 184 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 185 | total_commission: function() { |
| 186 | if(this.frm.doc.net_total) { |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 187 | frappe.model.round_floats_in(this.frm.doc, ["net_total", "total_commission"]); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 188 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 189 | if(this.frm.doc.net_total < this.frm.doc.total_commission) { |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 190 | var msg = (__("[Error]") + " " + |
| 191 | __(frappe.meta.get_label(this.frm.doc.doctype, "total_commission", |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 192 | this.frm.doc.name)) + " > " + |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 193 | __(frappe.meta.get_label(this.frm.doc.doctype, "net_total", this.frm.doc.name))); |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 194 | msgprint(msg); |
| 195 | throw msg; |
| 196 | } |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 197 | |
| 198 | this.frm.set_value("commission_rate", |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 199 | flt(this.frm.doc.total_commission * 100.0 / this.frm.doc.net_total)); |
| 200 | } |
| 201 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 202 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 203 | allocated_percentage: function(doc, cdt, cdn) { |
Rushabh Mehta | 66d52b5 | 2014-03-27 14:17:33 +0530 | [diff] [blame] | 204 | var sales_person = frappe.get_doc(cdt, cdn); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 205 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 206 | if(sales_person.allocated_percentage) { |
| 207 | sales_person.allocated_percentage = flt(sales_person.allocated_percentage, |
| 208 | precision("allocated_percentage", sales_person)); |
| 209 | sales_person.allocated_amount = flt(this.frm.doc.net_total * |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 210 | sales_person.allocated_percentage / 100.0, |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 211 | precision("allocated_amount", sales_person)); |
| 212 | |
| 213 | refresh_field(["allocated_percentage", "allocated_amount"], sales_person.name, |
| 214 | sales_person.parentfield); |
| 215 | } |
| 216 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 217 | |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 218 | warehouse: function(doc, cdt, cdn) { |
Rushabh Mehta | 66d52b5 | 2014-03-27 14:17:33 +0530 | [diff] [blame] | 219 | var item = frappe.get_doc(cdt, cdn); |
Anand Doshi | 1dc95ed | 2013-07-23 13:36:38 +0530 | [diff] [blame] | 220 | if(item.item_code && item.warehouse) { |
Anand Doshi | 1fac2a9 | 2013-07-29 19:30:39 +0530 | [diff] [blame] | 221 | return this.frm.call({ |
Nabin Hait | 0984f22 | 2014-05-30 19:10:12 +0530 | [diff] [blame] | 222 | method: "erpnext.stock.get_item_details.get_available_qty", |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 223 | child: item, |
| 224 | args: { |
| 225 | item_code: item.item_code, |
Anand Doshi | 1dc95ed | 2013-07-23 13:36:38 +0530 | [diff] [blame] | 226 | warehouse: item.warehouse, |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 227 | }, |
| 228 | }); |
| 229 | } |
| 230 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 231 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 232 | toggle_rounded_total: function() { |
| 233 | var me = this; |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 234 | if(cint(frappe.defaults.get_global_default("disable_rounded_total"))) { |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 235 | $.each(["rounded_total", "rounded_total_export"], function(i, fieldname) { |
| 236 | me.frm.set_df_property(fieldname, "print_hide", 1); |
| 237 | me.frm.toggle_display(fieldname, false); |
| 238 | }); |
| 239 | } |
| 240 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 241 | |
Anand Doshi | 33fe867 | 2013-08-02 12:39:10 +0530 | [diff] [blame] | 242 | toggle_editable_price_list_rate: function() { |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 243 | var df = frappe.meta.get_docfield(this.tname, "price_list_rate", this.frm.doc.name); |
| 244 | var editable_price_list_rate = cint(frappe.defaults.get_default("editable_price_list_rate")); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 245 | |
Anand Doshi | 33fe867 | 2013-08-02 12:39:10 +0530 | [diff] [blame] | 246 | if(df && editable_price_list_rate) { |
| 247 | df.read_only = 0; |
| 248 | } |
| 249 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 250 | |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 251 | calculate_taxes_and_totals: function(update_paid_amount) { |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 252 | this._super(); |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 253 | this.calculate_total_advance("Sales Invoice", "advance_adjustment_details", update_paid_amount); |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 254 | this.calculate_commission(); |
| 255 | this.calculate_contribution(); |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 256 | |
| 257 | // TODO check for custom_recalc in custom scripts of server |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 258 | |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 259 | this.frm.refresh_fields(); |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 260 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 261 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 262 | calculate_item_values: function() { |
| 263 | var me = this; |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 264 | |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 265 | if (!this.discount_amount_applied) { |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 266 | $.each(this.frm.item_doclist, function(i, item) { |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 267 | frappe.model.round_floats_in(item); |
Nabin Hait | 1eb5601 | 2014-02-10 19:20:15 +0530 | [diff] [blame] | 268 | item.amount = flt(item.rate * item.qty, precision("amount", item)); |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 269 | |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 270 | me._set_in_company_currency(item, "price_list_rate", "base_price_list_rate"); |
Nabin Hait | 7979f7e | 2014-02-10 18:26:49 +0530 | [diff] [blame] | 271 | me._set_in_company_currency(item, "rate", "base_rate"); |
Nabin Hait | 1eb5601 | 2014-02-10 19:20:15 +0530 | [diff] [blame] | 272 | me._set_in_company_currency(item, "amount", "base_amount"); |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 273 | }); |
| 274 | } |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 275 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 276 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 277 | determine_exclusive_rate: function() { |
| 278 | var me = this; |
| 279 | $.each(me.frm.item_doclist, function(n, item) { |
| 280 | var item_tax_map = me._load_item_tax_rate(item.item_tax_rate); |
| 281 | var cumulated_tax_fraction = 0.0; |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 282 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 283 | $.each(me.frm.tax_doclist, function(i, tax) { |
| 284 | tax.tax_fraction_for_current_item = me.get_current_tax_fraction(tax, item_tax_map); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 285 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 286 | if(i==0) { |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 287 | tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item; |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 288 | } else { |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 289 | tax.grand_total_fraction_for_current_item = |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 290 | me.frm.tax_doclist[i-1].grand_total_fraction_for_current_item + |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 291 | tax.tax_fraction_for_current_item; |
| 292 | } |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 293 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 294 | cumulated_tax_fraction += tax.tax_fraction_for_current_item; |
| 295 | }); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 296 | |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 297 | if(cumulated_tax_fraction && !me.discount_amount_applied) { |
Nabin Hait | 1eb5601 | 2014-02-10 19:20:15 +0530 | [diff] [blame] | 298 | item.base_amount = flt( |
| 299 | (item.amount * me.frm.doc.conversion_rate) / (1 + cumulated_tax_fraction), |
| 300 | precision("base_amount", item)); |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 301 | |
Nabin Hait | 1eb5601 | 2014-02-10 19:20:15 +0530 | [diff] [blame] | 302 | item.base_rate = flt(item.base_amount / item.qty, precision("base_rate", item)); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 303 | |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 304 | if(item.discount_percentage == 100) { |
Nabin Hait | 7979f7e | 2014-02-10 18:26:49 +0530 | [diff] [blame] | 305 | item.base_price_list_rate = item.base_rate; |
| 306 | item.base_rate = 0.0; |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 307 | } else { |
Nabin Hait | 7979f7e | 2014-02-10 18:26:49 +0530 | [diff] [blame] | 308 | item.base_price_list_rate = flt(item.base_rate / (1 - item.discount_percentage / 100.0), |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 309 | precision("base_price_list_rate", item)); |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | }); |
| 313 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 314 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 315 | get_current_tax_fraction: function(tax, item_tax_map) { |
| 316 | // Get tax fraction for calculating tax exclusive amount |
| 317 | // from tax inclusive amount |
| 318 | var current_tax_fraction = 0.0; |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 319 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 320 | if(cint(tax.included_in_print_rate)) { |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 321 | var tax_rate = this._get_tax_rate(tax, item_tax_map); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 322 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 323 | if(tax.charge_type == "On Net Total") { |
| 324 | current_tax_fraction = (tax_rate / 100.0); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 325 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 326 | } else if(tax.charge_type == "On Previous Row Amount") { |
| 327 | current_tax_fraction = (tax_rate / 100.0) * |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 328 | this.frm.tax_doclist[cint(tax.row_id) - 1].tax_fraction_for_current_item; |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 329 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 330 | } else if(tax.charge_type == "On Previous Row Total") { |
| 331 | current_tax_fraction = (tax_rate / 100.0) * |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 332 | this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_fraction_for_current_item; |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 333 | } |
| 334 | } |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 335 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 336 | return current_tax_fraction; |
| 337 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 338 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 339 | calculate_net_total: function() { |
| 340 | var me = this; |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 341 | this.frm.doc.net_total = this.frm.doc.net_total_export = 0.0; |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 342 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 343 | $.each(this.frm.item_doclist, function(i, item) { |
Nabin Hait | 1eb5601 | 2014-02-10 19:20:15 +0530 | [diff] [blame] | 344 | me.frm.doc.net_total += item.base_amount; |
| 345 | me.frm.doc.net_total_export += item.amount; |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 346 | }); |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 347 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 348 | frappe.model.round_floats_in(this.frm.doc, ["net_total", "net_total_export"]); |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 349 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 350 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 351 | calculate_totals: function() { |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 352 | var me = this; |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 353 | var tax_count = this.frm.tax_doclist.length; |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 354 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 355 | this.frm.doc.grand_total = flt( |
| 356 | tax_count ? this.frm.tax_doclist[tax_count - 1].total : this.frm.doc.net_total, |
| 357 | precision("grand_total")); |
| 358 | this.frm.doc.grand_total_export = flt(this.frm.doc.grand_total / this.frm.doc.conversion_rate, |
| 359 | precision("grand_total_export")); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 360 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 361 | this.frm.doc.other_charges_total = flt(this.frm.doc.grand_total - this.frm.doc.net_total, |
| 362 | precision("other_charges_total")); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 363 | this.frm.doc.other_charges_total_export = flt(this.frm.doc.grand_total_export - |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 364 | this.frm.doc.net_total_export + flt(this.frm.doc.discount_amount), |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 365 | precision("other_charges_total_export")); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 366 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 367 | this.frm.doc.rounded_total = Math.round(this.frm.doc.grand_total); |
| 368 | this.frm.doc.rounded_total_export = Math.round(this.frm.doc.grand_total_export); |
| 369 | }, |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 370 | |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 371 | apply_discount_amount: function() { |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 372 | var me = this; |
| 373 | var distributed_amount = 0.0; |
| 374 | |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 375 | if (this.frm.doc.discount_amount) { |
| 376 | var grand_total_for_discount_amount = this.get_grand_total_for_discount_amount(); |
| 377 | // calculate item amount after Discount Amount |
| 378 | if (grand_total_for_discount_amount) { |
| 379 | $.each(this.frm.item_doclist, function(i, item) { |
Nabin Hait | 1eb5601 | 2014-02-10 19:20:15 +0530 | [diff] [blame] | 380 | distributed_amount = flt(me.frm.doc.discount_amount) * item.base_amount / grand_total_for_discount_amount; |
| 381 | item.base_amount = flt(item.base_amount - distributed_amount, precision("base_amount", item)); |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 382 | }); |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 383 | |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 384 | this.discount_amount_applied = true; |
| 385 | this._calculate_taxes_and_totals(); |
| 386 | } |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 387 | } |
| 388 | }, |
| 389 | |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 390 | get_grand_total_for_discount_amount: function() { |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 391 | var me = this; |
| 392 | var total_actual_tax = 0.0; |
| 393 | var actual_taxes_dict = {}; |
| 394 | |
| 395 | $.each(this.frm.tax_doclist, function(i, tax) { |
| 396 | if (tax.charge_type == "Actual") |
| 397 | actual_taxes_dict[tax.idx] = tax.tax_amount; |
| 398 | else if (actual_taxes_dict[tax.row_id] !== null) { |
| 399 | actual_tax_amount = flt(actual_taxes_dict[tax.row_id]) * flt(tax.rate) / 100; |
| 400 | actual_taxes_dict[tax.idx] = actual_tax_amount; |
| 401 | } |
| 402 | }); |
| 403 | |
| 404 | $.each(actual_taxes_dict, function(key, value) { |
| 405 | if (value) |
| 406 | total_actual_tax += value; |
| 407 | }); |
| 408 | |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 409 | grand_total_for_discount_amount = flt(this.frm.doc.grand_total - total_actual_tax, |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 410 | precision("grand_total")); |
Akhilesh Darjee | 57738a0 | 2014-01-03 18:15:07 +0530 | [diff] [blame] | 411 | return grand_total_for_discount_amount; |
Akhilesh Darjee | d203aea | 2013-12-27 17:49:57 +0530 | [diff] [blame] | 412 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 413 | |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 414 | calculate_outstanding_amount: function(update_paid_amount) { |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 415 | // NOTE: |
Akhilesh Darjee | 38e8f98 | 2013-09-05 12:59:33 +0530 | [diff] [blame] | 416 | // paid_amount and write_off_amount is only for POS Invoice |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 417 | // total_advance is only for non POS Invoice |
Anand Doshi | 29ea5d0 | 2013-07-05 17:23:14 +0530 | [diff] [blame] | 418 | if(this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.docstatus==0) { |
Nabin Hait | 5410680 | 2014-03-13 17:26:41 +0530 | [diff] [blame] | 419 | frappe.model.round_floats_in(this.frm.doc, ["grand_total", "total_advance", "write_off_amount", |
| 420 | "paid_amount"]); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 421 | var total_amount_to_pay = this.frm.doc.grand_total - this.frm.doc.write_off_amount |
Nabin Hait | 5410680 | 2014-03-13 17:26:41 +0530 | [diff] [blame] | 422 | - this.frm.doc.total_advance; |
Nabin Hait | 0c9d422 | 2014-03-25 19:35:41 +0530 | [diff] [blame] | 423 | if(this.frm.doc.is_pos) { |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 424 | if(!this.frm.doc.paid_amount || update_paid_amount===undefined || update_paid_amount) { |
| 425 | this.frm.doc.paid_amount = flt(total_amount_to_pay); |
| 426 | } |
Nabin Hait | 0c9d422 | 2014-03-25 19:35:41 +0530 | [diff] [blame] | 427 | } else { |
| 428 | this.frm.doc.paid_amount = 0 |
| 429 | } |
Nabin Hait | 482ffac | 2014-03-13 16:32:43 +0530 | [diff] [blame] | 430 | |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 431 | this.frm.set_value("outstanding_amount", flt(total_amount_to_pay |
Nabin Hait | 5410680 | 2014-03-13 17:26:41 +0530 | [diff] [blame] | 432 | - this.frm.doc.paid_amount, precision("outstanding_amount"))); |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 433 | } |
| 434 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 435 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 436 | calculate_commission: function() { |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 437 | if(this.frm.fields_dict.commission_rate) { |
| 438 | if(this.frm.doc.commission_rate > 100) { |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 439 | var msg = __(frappe.meta.get_label(this.frm.doc.doctype, "commission_rate", this.frm.doc.name)) + |
| 440 | " " + __("cannot be greater than 100"); |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 441 | msgprint(msg); |
| 442 | throw msg; |
| 443 | } |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 444 | |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 445 | this.frm.doc.total_commission = flt(this.frm.doc.net_total * this.frm.doc.commission_rate / 100.0, |
| 446 | precision("total_commission")); |
| 447 | } |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 448 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 449 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 450 | calculate_contribution: function() { |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 451 | var me = this; |
Rushabh Mehta | aa355af | 2014-03-26 18:24:30 +0530 | [diff] [blame] | 452 | $.each(this.frm.doc.doctype.sales_team || [], function(i, sales_person) { |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 453 | frappe.model.round_floats_in(sales_person); |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 454 | if(sales_person.allocated_percentage) { |
| 455 | sales_person.allocated_amount = flt( |
| 456 | me.frm.doc.net_total * sales_person.allocated_percentage / 100.0, |
| 457 | precision("allocated_amount", sales_person)); |
| 458 | } |
| 459 | }); |
| 460 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 461 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 462 | _cleanup: function() { |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 463 | this._super(); |
| 464 | this.frm.doc.in_words = this.frm.doc.in_words_export = ""; |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 465 | }, |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 466 | |
Anand Doshi | cefccb9 | 2013-07-15 18:28:14 +0530 | [diff] [blame] | 467 | shipping_rule: function() { |
| 468 | var me = this; |
| 469 | if(this.frm.doc.shipping_rule) { |
Anand Doshi | 1fac2a9 | 2013-07-29 19:30:39 +0530 | [diff] [blame] | 470 | return this.frm.call({ |
Anand Doshi | cefccb9 | 2013-07-15 18:28:14 +0530 | [diff] [blame] | 471 | doc: this.frm.doc, |
| 472 | method: "apply_shipping_rule", |
| 473 | callback: function(r) { |
| 474 | if(!r.exc) { |
| 475 | me.calculate_taxes_and_totals(); |
| 476 | } |
| 477 | } |
| 478 | }) |
| 479 | } |
| 480 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 481 | |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 482 | set_dynamic_labels: function() { |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 483 | this._super(); |
Anand Doshi | 9d04924 | 2013-06-05 20:46:56 +0530 | [diff] [blame] | 484 | set_sales_bom_help(this.frm.doc); |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 485 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 486 | |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 487 | change_form_labels: function(company_currency) { |
| 488 | var me = this; |
| 489 | var field_label_map = {}; |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 490 | |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 491 | var setup_field_label_map = function(fields_list, currency) { |
| 492 | $.each(fields_list, function(i, fname) { |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 493 | var docfield = frappe.meta.docfield_map[me.frm.doc.doctype][fname]; |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 494 | if(docfield) { |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 495 | var label = __(docfield.label || "").replace(/\([^\)]*\)/g, ""); |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 496 | field_label_map[fname] = label.trim() + " (" + currency + ")"; |
| 497 | } |
| 498 | }); |
| 499 | }; |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 500 | setup_field_label_map(["net_total", "other_charges_total", "grand_total", |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 501 | "rounded_total", "in_words", |
| 502 | "outstanding_amount", "total_advance", "paid_amount", "write_off_amount"], |
| 503 | company_currency); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 504 | |
| 505 | setup_field_label_map(["net_total_export", "other_charges_total_export", "grand_total_export", |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 506 | "rounded_total_export", "in_words_export"], this.frm.doc.currency); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 507 | |
| 508 | cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency |
Rushabh Mehta | ef58455 | 2013-11-02 14:47:11 +0530 | [diff] [blame] | 509 | + " = [?] " + company_currency) |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 510 | |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 511 | if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) { |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 512 | cur_frm.set_df_property("plc_conversion_rate", "description", "1 " + this.frm.doc.price_list_currency |
Rushabh Mehta | ef58455 | 2013-11-02 14:47:11 +0530 | [diff] [blame] | 513 | + " = [?] " + company_currency) |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 514 | } |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 515 | |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 516 | // toggle fields |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 517 | this.frm.toggle_display(["conversion_rate", "net_total", "other_charges_total", |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 518 | "grand_total", "rounded_total", "in_words"], |
| 519 | this.frm.doc.currency != company_currency); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 520 | |
| 521 | this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"], |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 522 | this.frm.doc.price_list_currency != company_currency); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 523 | |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 524 | // set labels |
| 525 | $.each(field_label_map, function(fname, label) { |
| 526 | me.frm.fields_dict[fname].set_label(label); |
| 527 | }); |
| 528 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 529 | |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 530 | change_grid_labels: function(company_currency) { |
| 531 | var me = this; |
| 532 | var field_label_map = {}; |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 533 | |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 534 | var setup_field_label_map = function(fields_list, currency, parentfield) { |
| 535 | var grid_doctype = me.frm.fields_dict[parentfield].grid.doctype; |
| 536 | $.each(fields_list, function(i, fname) { |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 537 | var docfield = frappe.meta.docfield_map[grid_doctype][fname]; |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 538 | if(docfield) { |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 539 | var label = __(docfield.label || "").replace(/\([^\)]*\)/g, ""); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 540 | field_label_map[grid_doctype + "-" + fname] = |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 541 | label.trim() + " (" + currency + ")"; |
| 542 | } |
| 543 | }); |
| 544 | } |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 545 | |
Nabin Hait | 1eb5601 | 2014-02-10 19:20:15 +0530 | [diff] [blame] | 546 | setup_field_label_map(["base_rate", "base_price_list_rate", "base_amount"], |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 547 | company_currency, this.fname); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 548 | |
Nabin Hait | 1eb5601 | 2014-02-10 19:20:15 +0530 | [diff] [blame] | 549 | setup_field_label_map(["rate", "price_list_rate", "amount"], |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 550 | this.frm.doc.currency, this.fname); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 551 | |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 552 | setup_field_label_map(["tax_amount", "total"], company_currency, "other_charges"); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 553 | |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 554 | if(this.frm.fields_dict["advance_allocation_details"]) { |
| 555 | setup_field_label_map(["advance_amount", "allocated_amount"], company_currency, |
| 556 | "advance_allocation_details"); |
| 557 | } |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 558 | |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 559 | // toggle columns |
| 560 | var item_grid = this.frm.fields_dict[this.fname].grid; |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 561 | var show = (this.frm.doc.currency != company_currency) || |
| 562 | ((cur_frm.doc.other_charges || []).filter( |
Rushabh Mehta | aa355af | 2014-03-26 18:24:30 +0530 | [diff] [blame] | 563 | function(d) { return d.included_in_print_rate===1}).length); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 564 | |
Nabin Hait | 1eb5601 | 2014-02-10 19:20:15 +0530 | [diff] [blame] | 565 | $.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) { |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 566 | if(frappe.meta.get_docfield(item_grid.doctype, fname)) |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 567 | item_grid.set_column_disp(fname, show); |
| 568 | }); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 569 | |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 570 | // set labels |
| 571 | var $wrapper = $(this.frm.wrapper); |
| 572 | $.each(field_label_map, function(fname, label) { |
Anand Doshi | 5013dcb | 2013-08-05 12:16:04 +0530 | [diff] [blame] | 573 | fname = fname.split("-"); |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 574 | var df = frappe.meta.get_docfield(fname[0], fname[1], me.frm.doc.name); |
Anand Doshi | 5013dcb | 2013-08-05 12:16:04 +0530 | [diff] [blame] | 575 | if(df) df.label = label; |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 576 | }); |
| 577 | }, |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 578 | }); |
| 579 | |
Nabin Hait | cfc6bb1 | 2012-07-11 13:14:52 +0530 | [diff] [blame] | 580 | // Help for Sales BOM items |
| 581 | var set_sales_bom_help = function(doc) { |
Rushabh Mehta | 2eef40b | 2012-07-16 14:16:57 +0530 | [diff] [blame] | 582 | if(!cur_frm.fields_dict.packing_list) return; |
Rushabh Mehta | 66d52b5 | 2014-03-27 14:17:33 +0530 | [diff] [blame] | 583 | if ((doc.packing_details || []).length) { |
Nabin Hait | cfc6bb1 | 2012-07-11 13:14:52 +0530 | [diff] [blame] | 584 | $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(true); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 585 | |
Nabin Hait | cfc6bb1 | 2012-07-11 13:14:52 +0530 | [diff] [blame] | 586 | if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) { |
Bárbara Perretti | 4098c26 | 2013-09-27 17:05:17 -0300 | [diff] [blame] | 587 | help_msg = "<div class='alert alert-warning'>" + |
Pratik Vyas | e1eeb22 | 2014-04-15 10:50:36 +0530 | [diff] [blame] | 588 | __("For 'Sales BOM' items, warehouse, serial no and batch no will be considered from the 'Packing List' table. If warehouse and batch no are same for all packing items for any 'Sales BOM' item, those values can be entered in the main item table, values will be copied to 'Packing List' table.")+ |
Bárbara Perretti | 4098c26 | 2013-09-27 17:05:17 -0300 | [diff] [blame] | 589 | "</div>"; |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 590 | frappe.meta.get_docfield(doc.doctype, 'sales_bom_help', doc.name).options = help_msg; |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 591 | } |
Nabin Hait | cfc6bb1 | 2012-07-11 13:14:52 +0530 | [diff] [blame] | 592 | } else { |
| 593 | $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(false); |
| 594 | if (inList(['Delivery Note', 'Sales Invoice'], doc.doctype)) { |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 595 | frappe.meta.get_docfield(doc.doctype, 'sales_bom_help', doc.name).options = ''; |
Nabin Hait | cfc6bb1 | 2012-07-11 13:14:52 +0530 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | refresh_field('sales_bom_help'); |
| 599 | } |