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