Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | // Copyright (c) 2015, Frappe 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 | |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 5 | cur_frm.cscript.tax_table = "Sales Taxes and Charges"; |
Rushabh Mehta | 621283c | 2016-04-21 19:00:34 +0530 | [diff] [blame] | 6 | {% include 'erpnext/accounts/doctype/sales_taxes_and_charges_template/sales_taxes_and_charges_template.js' %} |
Nabin Hait | 613d081 | 2015-02-23 11:58:15 +0530 | [diff] [blame] | 7 | |
Rushabh Mehta | b09d9da | 2014-01-02 11:47:23 +0530 | [diff] [blame] | 8 | |
Rushabh Mehta | e10cf02 | 2014-09-12 13:09:41 +0530 | [diff] [blame] | 9 | cur_frm.email_field = "contact_email"; |
| 10 | |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 11 | frappe.provide("erpnext.selling"); |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 12 | erpnext.selling.SellingController = erpnext.TransactionController.extend({ |
Rushabh Mehta | 532eedf | 2016-05-19 18:22:49 +0530 | [diff] [blame] | 13 | setup: function() { |
| 14 | this._super(); |
Rushabh Mehta | 532eedf | 2016-05-19 18:22:49 +0530 | [diff] [blame] | 15 | }, |
| 16 | |
Anand Doshi | 9b49614 | 2013-07-11 19:13:58 +0530 | [diff] [blame] | 17 | onload: function() { |
| 18 | this._super(); |
Anand Doshi | 9b49614 | 2013-07-11 19:13:58 +0530 | [diff] [blame] | 19 | this.setup_queries(); |
Rushabh Mehta | 30dc9a1 | 2017-11-17 14:31:09 +0530 | [diff] [blame] | 20 | this.frm.set_query('shipping_rule', function() { |
| 21 | return { |
| 22 | filters: { |
| 23 | "shipping_rule_type": "Selling" |
| 24 | } |
| 25 | }; |
| 26 | }); |
Anand Doshi | 9b49614 | 2013-07-11 19:13:58 +0530 | [diff] [blame] | 27 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 28 | |
Anand Doshi | 9b49614 | 2013-07-11 19:13:58 +0530 | [diff] [blame] | 29 | setup_queries: function() { |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 30 | var me = this; |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 31 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 32 | this.frm.add_fetch("sales_partner", "commission_rate", "commission_rate"); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 33 | |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 34 | $.each([["customer", "customer"], |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 35 | ["lead", "lead"]], |
Anand Doshi | d5d39ac | 2013-07-29 13:28:37 +0530 | [diff] [blame] | 36 | function(i, opts) { |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 37 | if(me.frm.fields_dict[opts[0]]) |
Anand Doshi | d5d39ac | 2013-07-29 13:28:37 +0530 | [diff] [blame] | 38 | me.frm.set_query(opts[0], erpnext.queries[opts[1]]); |
| 39 | }); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 40 | |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 41 | me.frm.set_query('contact_person', erpnext.queries.contact_query); |
| 42 | me.frm.set_query('customer_address', erpnext.queries.address_query); |
| 43 | me.frm.set_query('shipping_address_name', erpnext.queries.address_query); |
| 44 | |
Akhilesh Darjee | 4f72156 | 2014-01-29 16:31:38 +0530 | [diff] [blame] | 45 | if(this.frm.fields_dict.taxes_and_charges) { |
| 46 | this.frm.set_query("taxes_and_charges", function() { |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 47 | return { |
| 48 | filters: [ |
Rushabh Mehta | ab1ed01 | 2015-05-08 11:08:48 +0530 | [diff] [blame] | 49 | ['Sales Taxes and Charges Template', 'company', '=', me.frm.doc.company], |
| 50 | ['Sales Taxes and Charges Template', 'docstatus', '!=', 2] |
Saurabh | f52dc07 | 2013-07-10 13:07:49 +0530 | [diff] [blame] | 51 | ] |
| 52 | } |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 53 | }); |
| 54 | } |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 55 | |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 56 | if(this.frm.fields_dict.selling_price_list) { |
| 57 | this.frm.set_query("selling_price_list", function() { |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 58 | return { filters: { selling: 1 } }; |
Anand Doshi | 720a01a | 2013-07-26 11:32:02 +0530 | [diff] [blame] | 59 | }); |
Anand Doshi | 720a01a | 2013-07-26 11:32:02 +0530 | [diff] [blame] | 60 | } |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 61 | |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 62 | if(!this.frm.fields_dict["items"]) { |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 63 | return; |
| 64 | } |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 65 | |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 66 | if(this.frm.fields_dict["items"].grid.get_field('item_code')) { |
| 67 | this.frm.set_query("item_code", "items", function() { |
Anand Doshi | 9b49614 | 2013-07-11 19:13:58 +0530 | [diff] [blame] | 68 | return { |
Rushabh Mehta | d973c16 | 2016-08-16 11:32:08 +0530 | [diff] [blame] | 69 | query: "erpnext.controllers.queries.item_query", |
| 70 | filters: {'is_sales_item': 1} |
Anand Doshi | 9b49614 | 2013-07-11 19:13:58 +0530 | [diff] [blame] | 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 | |
Rushabh Mehta | 698c040 | 2016-11-22 23:16:40 +0530 | [diff] [blame] | 75 | if(this.frm.fields_dict["packed_items"] && |
| 76 | this.frm.fields_dict["packed_items"].grid.get_field('batch_no')) { |
Rohit Waghchaure | e887e92 | 2016-11-18 12:16:22 +0530 | [diff] [blame] | 77 | this.frm.set_query("batch_no", "packed_items", function(doc, cdt, cdn) { |
| 78 | return me.set_query_for_batch(doc, cdt, cdn) |
| 79 | }); |
| 80 | } |
Rohit Waghchaure | e887e92 | 2016-11-18 12:16:22 +0530 | [diff] [blame] | 81 | }, |
| 82 | |
Anand Doshi | c4a54fe | 2013-08-01 18:19:51 +0530 | [diff] [blame] | 83 | refresh: function() { |
| 84 | this._super(); |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 85 | |
Rushabh Mehta | 8d39fd9 | 2017-01-16 13:06:07 +0530 | [diff] [blame] | 86 | frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'customer', doctype: 'Customer'} |
Rushabh Mehta | b92087c | 2017-01-13 18:53:11 +0530 | [diff] [blame] | 87 | |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 88 | this.frm.toggle_display("customer_name", |
Akhilesh Darjee | 5ce1b8b | 2013-12-09 16:29:04 +0530 | [diff] [blame] | 89 | (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] | 90 | if(this.frm.fields_dict.packed_items) { |
| 91 | var packing_list_exists = (this.frm.doc.packed_items || []).length; |
Anand Doshi | c4a54fe | 2013-08-01 18:19:51 +0530 | [diff] [blame] | 92 | this.frm.toggle_display("packing_list", packing_list_exists ? true : false); |
| 93 | } |
Nabin Hait | 2029ea9 | 2015-04-16 17:55:35 +0530 | [diff] [blame] | 94 | this.toggle_editable_price_list_rate(); |
Rushabh Mehta | 8aded13 | 2013-07-04 12:50:52 +0530 | [diff] [blame] | 95 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 96 | |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 97 | customer: function() { |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 98 | var me = this; |
Rushabh Mehta | dab1172 | 2017-07-17 15:31:17 +0530 | [diff] [blame] | 99 | erpnext.utils.get_party_details(this.frm, null, null, |
| 100 | function(){ me.apply_pricing_rule() }); |
Anand Doshi | 3543f30 | 2013-05-24 19:25:01 +0530 | [diff] [blame] | 101 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 102 | |
Nabin Hait | a279d78 | 2013-07-15 13:04:33 +0530 | [diff] [blame] | 103 | customer_address: function() { |
Rushabh Mehta | b09d9da | 2014-01-02 11:47:23 +0530 | [diff] [blame] | 104 | erpnext.utils.get_address_display(this.frm, "customer_address"); |
Nabin Hait | a279d78 | 2013-07-15 13:04:33 +0530 | [diff] [blame] | 105 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 106 | |
Nabin Hait | 9d1f077 | 2014-02-19 17:43:24 +0530 | [diff] [blame] | 107 | shipping_address_name: function() { |
| 108 | erpnext.utils.get_address_display(this.frm, "shipping_address_name", "shipping_address"); |
| 109 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 110 | |
Nabin Hait | a3dd72a | 2014-05-28 12:49:20 +0530 | [diff] [blame] | 111 | sales_partner: function() { |
| 112 | this.apply_pricing_rule(); |
| 113 | }, |
| 114 | |
| 115 | campaign: function() { |
| 116 | this.apply_pricing_rule(); |
| 117 | }, |
| 118 | |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 119 | selling_price_list: function() { |
Anand Doshi | dffec8f | 2014-07-01 17:45:15 +0530 | [diff] [blame] | 120 | this.apply_price_list(); |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 121 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 122 | |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 123 | price_list_rate: function(doc, cdt, cdn) { |
Rushabh Mehta | 66d52b5 | 2014-03-27 14:17:33 +0530 | [diff] [blame] | 124 | var item = frappe.get_doc(cdt, cdn); |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 125 | frappe.model.round_floats_in(item, ["price_list_rate", "discount_percentage"]); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 126 | |
mbauskar | a52472c | 2016-03-05 15:10:25 +0530 | [diff] [blame] | 127 | // check if child doctype is Sales Order Item/Qutation Item and calculate the rate |
| 128 | if(in_list(["Quotation Item", "Sales Order Item", "Delivery Note Item", "Sales Invoice Item"]), cdt) |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 129 | this.apply_pricing_rule_on_item(item); |
mbauskar | a52472c | 2016-03-05 15:10:25 +0530 | [diff] [blame] | 130 | else |
| 131 | item.rate = flt(item.price_list_rate * (1 - item.discount_percentage / 100.0), |
| 132 | precision("rate", item)); |
| 133 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 134 | this.calculate_taxes_and_totals(); |
| 135 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 136 | |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 137 | discount_percentage: function(doc, cdt, cdn) { |
Rushabh Mehta | 66d52b5 | 2014-03-27 14:17:33 +0530 | [diff] [blame] | 138 | var item = frappe.get_doc(cdt, cdn); |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 139 | if(!item.price_list_rate) { |
| 140 | item.discount_percentage = 0.0; |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 141 | } else { |
Nabin Hait | a7f757a | 2014-02-10 17:54:04 +0530 | [diff] [blame] | 142 | this.price_list_rate(doc, cdt, cdn); |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 143 | } |
Saurabh | 5ada14b | 2016-02-26 18:02:55 +0530 | [diff] [blame] | 144 | this.set_gross_profit(item); |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 145 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 146 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 147 | commission_rate: function() { |
| 148 | this.calculate_commission(); |
| 149 | refresh_field("total_commission"); |
| 150 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 151 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 152 | total_commission: function() { |
Nabin Hait | 5690be1 | 2015-02-12 16:09:11 +0530 | [diff] [blame] | 153 | if(this.frm.doc.base_net_total) { |
| 154 | frappe.model.round_floats_in(this.frm.doc, ["base_net_total", "total_commission"]); |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 155 | |
Nabin Hait | 5690be1 | 2015-02-12 16:09:11 +0530 | [diff] [blame] | 156 | if(this.frm.doc.base_net_total < this.frm.doc.total_commission) { |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 157 | var msg = (__("[Error]") + " " + |
| 158 | __(frappe.meta.get_label(this.frm.doc.doctype, "total_commission", |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 159 | this.frm.doc.name)) + " > " + |
Nabin Hait | 5690be1 | 2015-02-12 16:09:11 +0530 | [diff] [blame] | 160 | __(frappe.meta.get_label(this.frm.doc.doctype, "base_net_total", this.frm.doc.name))); |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 161 | frappe.msgprint(msg); |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 162 | throw msg; |
| 163 | } |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 164 | |
| 165 | this.frm.set_value("commission_rate", |
Nabin Hait | 5690be1 | 2015-02-12 16:09:11 +0530 | [diff] [blame] | 166 | flt(this.frm.doc.total_commission * 100.0 / this.frm.doc.base_net_total)); |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 167 | } |
| 168 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 169 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 170 | allocated_percentage: function(doc, cdt, cdn) { |
Rushabh Mehta | 66d52b5 | 2014-03-27 14:17:33 +0530 | [diff] [blame] | 171 | var sales_person = frappe.get_doc(cdt, cdn); |
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 | if(sales_person.allocated_percentage) { |
| 174 | sales_person.allocated_percentage = flt(sales_person.allocated_percentage, |
| 175 | precision("allocated_percentage", sales_person)); |
Nabin Hait | 5690be1 | 2015-02-12 16:09:11 +0530 | [diff] [blame] | 176 | sales_person.allocated_amount = flt(this.frm.doc.base_net_total * |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 177 | sales_person.allocated_percentage / 100.0, |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 178 | precision("allocated_amount", sales_person)); |
| 179 | |
| 180 | refresh_field(["allocated_percentage", "allocated_amount"], sales_person.name, |
| 181 | sales_person.parentfield); |
| 182 | } |
| 183 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 184 | |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 185 | warehouse: function(doc, cdt, cdn) { |
Sambhaji Kolate | 98dbccd | 2015-03-10 15:04:28 +0530 | [diff] [blame] | 186 | var me = this; |
Rushabh Mehta | 66d52b5 | 2014-03-27 14:17:33 +0530 | [diff] [blame] | 187 | var item = frappe.get_doc(cdt, cdn); |
Rushabh Mehta | be2ee18 | 2016-04-29 17:22:42 +0530 | [diff] [blame] | 188 | |
Anand Doshi | 1dc95ed | 2013-07-23 13:36:38 +0530 | [diff] [blame] | 189 | if(item.item_code && item.warehouse) { |
Anand Doshi | 1fac2a9 | 2013-07-29 19:30:39 +0530 | [diff] [blame] | 190 | return this.frm.call({ |
Kanchan Chauhan | af0d637 | 2016-11-13 21:19:09 +0530 | [diff] [blame] | 191 | method: "erpnext.stock.get_item_details.get_bin_details_and_serial_nos", |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 192 | child: item, |
| 193 | args: { |
| 194 | item_code: item.item_code, |
Anand Doshi | 1dc95ed | 2013-07-23 13:36:38 +0530 | [diff] [blame] | 195 | warehouse: item.warehouse, |
Kanchan Chauhan | 5a980ac | 2017-02-10 14:27:11 +0530 | [diff] [blame] | 196 | stock_qty: item.stock_qty, |
Nabin Hait | 1d90b41 | 2016-12-24 10:42:23 +0530 | [diff] [blame] | 197 | serial_no: item.serial_no || "" |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 198 | }, |
Saurabh | d313553 | 2016-02-25 18:59:20 +0530 | [diff] [blame] | 199 | callback:function(r){ |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 200 | if (in_list(['Delivery Note', 'Sales Invoice'], doc.doctype)) { |
tundebabzy | 8ae53ca | 2017-11-30 07:07:21 +0100 | [diff] [blame] | 201 | me.set_batch_number(cdt, cdn); |
Saurabh | d313553 | 2016-02-25 18:59:20 +0530 | [diff] [blame] | 202 | me.batch_no(doc, cdt, cdn); |
| 203 | } |
| 204 | } |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 205 | }); |
| 206 | } |
| 207 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 208 | |
Anand Doshi | 33fe867 | 2013-08-02 12:39:10 +0530 | [diff] [blame] | 209 | toggle_editable_price_list_rate: function() { |
Nabin Hait | dd38a26 | 2014-12-26 13:15:21 +0530 | [diff] [blame] | 210 | 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] | 211 | 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] | 212 | |
Anand Doshi | 33fe867 | 2013-08-02 12:39:10 +0530 | [diff] [blame] | 213 | if(df && editable_price_list_rate) { |
| 214 | df.read_only = 0; |
| 215 | } |
| 216 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 217 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 218 | calculate_commission: function() { |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 219 | if(this.frm.fields_dict.commission_rate) { |
| 220 | if(this.frm.doc.commission_rate > 100) { |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 221 | var msg = __(frappe.meta.get_label(this.frm.doc.doctype, "commission_rate", this.frm.doc.name)) + |
| 222 | " " + __("cannot be greater than 100"); |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 223 | frappe.msgprint(msg); |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 224 | throw msg; |
| 225 | } |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 226 | |
Nabin Hait | 5690be1 | 2015-02-12 16:09:11 +0530 | [diff] [blame] | 227 | this.frm.doc.total_commission = flt(this.frm.doc.base_net_total * this.frm.doc.commission_rate / 100.0, |
Anand Doshi | 923d41d | 2013-05-28 17:23:36 +0530 | [diff] [blame] | 228 | precision("total_commission")); |
| 229 | } |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 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 | calculate_contribution: function() { |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 233 | var me = this; |
Rushabh Mehta | aa355af | 2014-03-26 18:24:30 +0530 | [diff] [blame] | 234 | $.each(this.frm.doc.doctype.sales_team || [], function(i, sales_person) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 235 | frappe.model.round_floats_in(sales_person); |
| 236 | if(sales_person.allocated_percentage) { |
| 237 | sales_person.allocated_amount = flt( |
| 238 | me.frm.doc.base_net_total * sales_person.allocated_percentage / 100.0, |
| 239 | precision("allocated_amount", sales_person)); |
| 240 | } |
| 241 | }); |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 242 | }, |
Anand Doshi | bd67e87 | 2014-04-11 16:51:27 +0530 | [diff] [blame] | 243 | |
Sambhaji Kolate | 98dbccd | 2015-03-10 15:04:28 +0530 | [diff] [blame] | 244 | batch_no: function(doc, cdt, cdn) { |
| 245 | var me = this; |
| 246 | var item = frappe.get_doc(cdt, cdn); |
Nabin Hait | 2ed71ba | 2015-03-20 15:06:30 +0530 | [diff] [blame] | 247 | |
Sambhaji Kolate | a4fa96c | 2015-03-30 16:04:55 +0530 | [diff] [blame] | 248 | if(item.warehouse && item.item_code && item.batch_no) { |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 249 | return this.frm.call({ |
| 250 | method: "erpnext.stock.get_item_details.get_batch_qty", |
| 251 | child: item, |
| 252 | args: { |
| 253 | "batch_no": item.batch_no, |
| 254 | "warehouse": item.warehouse, |
| 255 | "item_code": item.item_code |
| 256 | }, |
| 257 | "fieldname": "actual_batch_qty" |
| 258 | }); |
Sambhaji Kolate | feff40a | 2015-03-11 11:50:28 +0530 | [diff] [blame] | 259 | } |
Sambhaji Kolate | 98dbccd | 2015-03-10 15:04:28 +0530 | [diff] [blame] | 260 | }, |
| 261 | |
Anand Doshi | 2168e39 | 2013-05-23 19:25:08 +0530 | [diff] [blame] | 262 | set_dynamic_labels: function() { |
Anand Doshi | 61a2f68 | 2013-06-21 17:55:31 +0530 | [diff] [blame] | 263 | this._super(); |
Neil Trini Lasrado | ed8cecb | 2015-07-07 13:59:23 +0530 | [diff] [blame] | 264 | this.set_product_bundle_help(this.frm.doc); |
Nabin Hait | 0b15755 | 2014-06-24 17:02:45 +0530 | [diff] [blame] | 265 | }, |
| 266 | |
Neil Trini Lasrado | ed8cecb | 2015-07-07 13:59:23 +0530 | [diff] [blame] | 267 | set_product_bundle_help: function(doc) { |
Nabin Hait | 0b15755 | 2014-06-24 17:02:45 +0530 | [diff] [blame] | 268 | if(!cur_frm.fields_dict.packing_list) return; |
Nabin Hait | e7d1536 | 2014-12-25 16:01:55 +0530 | [diff] [blame] | 269 | if ((doc.packed_items || []).length) { |
Nabin Hait | 0b15755 | 2014-06-24 17:02:45 +0530 | [diff] [blame] | 270 | $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(true); |
| 271 | |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 272 | if (in_list(['Delivery Note', 'Sales Invoice'], doc.doctype)) { |
| 273 | var help_msg = "<div class='alert alert-warning'>" + |
Neil Trini Lasrado | ed8cecb | 2015-07-07 13:59:23 +0530 | [diff] [blame] | 274 | __("For 'Product Bundle' 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 'Product Bundle' 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] | 275 | "</div>"; |
Neil Trini Lasrado | ed8cecb | 2015-07-07 13:59:23 +0530 | [diff] [blame] | 276 | frappe.meta.get_docfield(doc.doctype, 'product_bundle_help', doc.name).options = help_msg; |
Nabin Hait | 0b15755 | 2014-06-24 17:02:45 +0530 | [diff] [blame] | 277 | } |
| 278 | } else { |
| 279 | $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(false); |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 280 | if (in_list(['Delivery Note', 'Sales Invoice'], doc.doctype)) { |
Neil Trini Lasrado | ed8cecb | 2015-07-07 13:59:23 +0530 | [diff] [blame] | 281 | frappe.meta.get_docfield(doc.doctype, 'product_bundle_help', doc.name).options = ''; |
Nabin Hait | 0b15755 | 2014-06-24 17:02:45 +0530 | [diff] [blame] | 282 | } |
| 283 | } |
Neil Trini Lasrado | ed8cecb | 2015-07-07 13:59:23 +0530 | [diff] [blame] | 284 | refresh_field('product_bundle_help'); |
Saurabh | eba7aa4 | 2015-12-16 09:57:59 +0530 | [diff] [blame] | 285 | }, |
Anand Doshi | 01de945 | 2016-01-26 16:22:50 +0530 | [diff] [blame] | 286 | |
Saurabh | eba7aa4 | 2015-12-16 09:57:59 +0530 | [diff] [blame] | 287 | make_payment_request: function() { |
Saurabh | eba7aa4 | 2015-12-16 09:57:59 +0530 | [diff] [blame] | 288 | frappe.call({ |
| 289 | method:"erpnext.accounts.doctype.payment_request.payment_request.make_payment_request", |
| 290 | args: { |
| 291 | "dt": cur_frm.doc.doctype, |
| 292 | "dn": cur_frm.doc.name, |
Saurabh | b109ee9 | 2015-12-16 20:48:40 +0530 | [diff] [blame] | 293 | "recipient_id": cur_frm.doc.contact_email |
Saurabh | eba7aa4 | 2015-12-16 09:57:59 +0530 | [diff] [blame] | 294 | }, |
| 295 | callback: function(r) { |
Saurabh | cb98d9e | 2015-12-17 09:48:44 +0530 | [diff] [blame] | 296 | if(!r.exc){ |
Saurabh | 3ba2267 | 2015-12-21 18:24:49 +0530 | [diff] [blame] | 297 | var doc = frappe.model.sync(r.message); |
Saurabh | 3ba2267 | 2015-12-21 18:24:49 +0530 | [diff] [blame] | 298 | frappe.set_route("Form", r.message.doctype, r.message.name); |
Saurabh | cb98d9e | 2015-12-17 09:48:44 +0530 | [diff] [blame] | 299 | } |
Saurabh | eba7aa4 | 2015-12-16 09:57:59 +0530 | [diff] [blame] | 300 | } |
| 301 | }) |
mbauskar | 1e9c164 | 2016-01-18 16:21:25 +0530 | [diff] [blame] | 302 | }, |
mbauskar | 1e428ae | 2016-01-19 19:52:22 +0530 | [diff] [blame] | 303 | |
mbauskar | a52472c | 2016-03-05 15:10:25 +0530 | [diff] [blame] | 304 | margin_rate_or_amount: function(doc, cdt, cdn) { |
mbauskar | 1e9c164 | 2016-01-18 16:21:25 +0530 | [diff] [blame] | 305 | // calculated the revised total margin and rate on margin rate changes |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 306 | var item = locals[cdt][cdn]; |
Rohit Waghchaure | 6087fe1 | 2016-04-09 14:31:09 +0530 | [diff] [blame] | 307 | this.apply_pricing_rule_on_item(item) |
mbauskar | a52472c | 2016-03-05 15:10:25 +0530 | [diff] [blame] | 308 | this.calculate_taxes_and_totals(); |
mbauskar | 1e9c164 | 2016-01-18 16:21:25 +0530 | [diff] [blame] | 309 | cur_frm.refresh_fields(); |
| 310 | }, |
mbauskar | 1e428ae | 2016-01-19 19:52:22 +0530 | [diff] [blame] | 311 | |
mbauskar | a52472c | 2016-03-05 15:10:25 +0530 | [diff] [blame] | 312 | margin_type: function(doc, cdt, cdn){ |
mbauskar | 1e9c164 | 2016-01-18 16:21:25 +0530 | [diff] [blame] | 313 | // calculate the revised total margin and rate on margin type changes |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 314 | var item = locals[cdt][cdn]; |
Nabin Hait | e72c98d | 2017-05-24 09:01:44 +0530 | [diff] [blame] | 315 | if(!item.margin_type) { |
| 316 | frappe.model.set_value(cdt, cdn, "margin_rate_or_amount", 0); |
| 317 | } else { |
| 318 | this.apply_pricing_rule_on_item(item, doc,cdt, cdn) |
| 319 | this.calculate_taxes_and_totals(); |
| 320 | cur_frm.refresh_fields(); |
| 321 | } |
Nabin Hait | 879e162 | 2017-08-21 08:28:55 +0530 | [diff] [blame] | 322 | }, |
| 323 | |
| 324 | company_address: function() { |
| 325 | var me = this; |
| 326 | if(this.frm.doc.company_address) { |
| 327 | frappe.call({ |
| 328 | method: "frappe.contacts.doctype.address.address.get_address_display", |
| 329 | args: {"address_dict": this.frm.doc.company_address }, |
| 330 | callback: function(r) { |
| 331 | if(r.message) { |
| 332 | me.frm.set_value("company_address_display", r.message) |
| 333 | } |
| 334 | } |
| 335 | }) |
| 336 | } else { |
| 337 | this.frm.set_value("company_address_display", ""); |
| 338 | } |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 339 | }, |
| 340 | |
| 341 | conversion_factor: function(doc, cdt, cdn, dont_fetch_price_list_rate) { |
| 342 | this._super(doc, cdt, cdn, dont_fetch_price_list_rate); |
| 343 | if(frappe.meta.get_docfield(cdt, "stock_qty", cdn)) { |
| 344 | this.set_batch_number(cdt, cdn); |
| 345 | } |
| 346 | }, |
| 347 | |
| 348 | qty: function(doc, cdt, cdn) { |
| 349 | this._super(doc, cdt, cdn); |
| 350 | this.set_batch_number(cdt, cdn); |
| 351 | }, |
| 352 | |
| 353 | /* Determine appropriate batch number and set it in the form. |
| 354 | * @param {string} cdt - Document Doctype |
| 355 | * @param {string} cdn - Document name |
| 356 | */ |
| 357 | set_batch_number: function(cdt, cdn) { |
| 358 | const doc = frappe.get_doc(cdt, cdn); |
rohitwaghchaure | 15f8fe0 | 2017-11-24 10:46:27 +0530 | [diff] [blame] | 359 | if (doc && doc.has_batch_no) { |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 360 | this._set_batch_number(doc); |
| 361 | } |
| 362 | }, |
| 363 | |
| 364 | _set_batch_number: function(doc) { |
| 365 | return frappe.call({ |
| 366 | method: 'erpnext.stock.doctype.batch.batch.get_batch_no', |
| 367 | args: {'item_code': doc.item_code, 'warehouse': doc.warehouse, 'qty': flt(doc.qty) * flt(doc.conversion_factor)}, |
| 368 | callback: function(r) { |
| 369 | if(r.message) { |
| 370 | frappe.model.set_value(doc.doctype, doc.name, 'batch_no', r.message); |
tundebabzy | 8ae53ca | 2017-11-30 07:07:21 +0100 | [diff] [blame] | 371 | } else { |
| 372 | frappe.model.set_value(doc.doctype, doc.name, 'batch_no', r.message); |
tundebabzy | acccdb3 | 2017-11-23 08:35:15 +0100 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | }); |
| 376 | }, |
Nabin Hait | 0b15755 | 2014-06-24 17:02:45 +0530 | [diff] [blame] | 377 | }); |
Anand Doshi | 43f087c | 2014-08-26 14:25:53 +0530 | [diff] [blame] | 378 | |
Neil Trini Lasrado | 6e343e2 | 2016-03-09 17:02:59 +0530 | [diff] [blame] | 379 | frappe.ui.form.on(cur_frm.doctype,"project", function(frm) { |
Nabin Hait | 10b155a | 2014-10-23 13:18:59 +0530 | [diff] [blame] | 380 | if(in_list(["Delivery Note", "Sales Invoice"], frm.doc.doctype)) { |
Rushabh Mehta | 532eedf | 2016-05-19 18:22:49 +0530 | [diff] [blame] | 381 | if(frm.doc.project) { |
| 382 | frappe.call({ |
| 383 | method:'erpnext.projects.doctype.project.project.get_cost_center_name' , |
| 384 | args: { project: frm.doc.project }, |
| 385 | callback: function(r, rt) { |
| 386 | if(!r.exc) { |
| 387 | $.each(frm.doc["items"] || [], function(i, row) { |
| 388 | if(r.message) { |
| 389 | frappe.model.set_value(row.doctype, row.name, "cost_center", r.message); |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 390 | frappe.msgprint(__("Cost Center For Item with Item Code '"+row.item_name+"' has been Changed to "+ r.message)); |
Rushabh Mehta | 532eedf | 2016-05-19 18:22:49 +0530 | [diff] [blame] | 391 | } |
| 392 | }) |
| 393 | } |
Nabin Hait | 10b155a | 2014-10-23 13:18:59 +0530 | [diff] [blame] | 394 | } |
Rushabh Mehta | 532eedf | 2016-05-19 18:22:49 +0530 | [diff] [blame] | 395 | }) |
| 396 | } |
Nabin Hait | 10b155a | 2014-10-23 13:18:59 +0530 | [diff] [blame] | 397 | } |
Kanchan Chauhan | af0d637 | 2016-11-13 21:19:09 +0530 | [diff] [blame] | 398 | }) |