blob: 23dcaa152dedd94ad38832f9a7df616be3eac29c [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302// License: GNU General Public License v3. See license.txt
Rushabh Mehta3966f1d2012-02-23 12:35:32 +05303
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +05304
Nabin Hait613d0812015-02-23 11:58:15 +05305cur_frm.cscript.tax_table = "Sales Taxes and Charges";
Rushabh Mehta621283c2016-04-21 19:00:34 +05306{% include 'erpnext/accounts/doctype/sales_taxes_and_charges_template/sales_taxes_and_charges_template.js' %}
Nabin Hait613d0812015-02-23 11:58:15 +05307
Rushabh Mehtab09d9da2014-01-02 11:47:23 +05308
Rushabh Mehtae10cf022014-09-12 13:09:41 +05309cur_frm.email_field = "contact_email";
10
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053011frappe.provide("erpnext.selling");
Anand Doshi3543f302013-05-24 19:25:01 +053012erpnext.selling.SellingController = erpnext.TransactionController.extend({
Rushabh Mehta532eedf2016-05-19 18:22:49 +053013 setup: function() {
14 this._super();
deepeshgarg00786152eb2018-09-25 19:08:16 +053015 this.frm.add_fetch("sales_partner", "commission_rate", "commission_rate");
16 this.frm.add_fetch("sales_person", "commission_rate", "commission_rate");
Rushabh Mehta532eedf2016-05-19 18:22:49 +053017 },
18
Anand Doshi9b496142013-07-11 19:13:58 +053019 onload: function() {
20 this._super();
Anand Doshi9b496142013-07-11 19:13:58 +053021 this.setup_queries();
Rushabh Mehta30dc9a12017-11-17 14:31:09 +053022 this.frm.set_query('shipping_rule', function() {
23 return {
24 filters: {
25 "shipping_rule_type": "Selling"
26 }
27 };
28 });
Anand Doshi9b496142013-07-11 19:13:58 +053029 },
Anand Doshibd67e872014-04-11 16:51:27 +053030
Anand Doshi9b496142013-07-11 19:13:58 +053031 setup_queries: function() {
Anand Doshi99100a42013-07-04 17:13:53 +053032 var me = this;
Anand Doshibd67e872014-04-11 16:51:27 +053033
Rushabh Mehtab92087c2017-01-13 18:53:11 +053034 $.each([["customer", "customer"],
Anand Doshibd67e872014-04-11 16:51:27 +053035 ["lead", "lead"]],
Anand Doshid5d39ac2013-07-29 13:28:37 +053036 function(i, opts) {
Anand Doshibd67e872014-04-11 16:51:27 +053037 if(me.frm.fields_dict[opts[0]])
Anand Doshid5d39ac2013-07-29 13:28:37 +053038 me.frm.set_query(opts[0], erpnext.queries[opts[1]]);
39 });
Anand Doshibd67e872014-04-11 16:51:27 +053040
Rushabh Mehtab92087c2017-01-13 18:53:11 +053041 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 Darjee4f721562014-01-29 16:31:38 +053045 if(this.frm.fields_dict.taxes_and_charges) {
46 this.frm.set_query("taxes_and_charges", function() {
Saurabhf52dc072013-07-10 13:07:49 +053047 return {
48 filters: [
Rushabh Mehtaab1ed012015-05-08 11:08:48 +053049 ['Sales Taxes and Charges Template', 'company', '=', me.frm.doc.company],
50 ['Sales Taxes and Charges Template', 'docstatus', '!=', 2]
Saurabhf52dc072013-07-10 13:07:49 +053051 ]
52 }
Anand Doshi99100a42013-07-04 17:13:53 +053053 });
54 }
Anand Doshi99100a42013-07-04 17:13:53 +053055
Rushabh Mehta4a404e92013-08-09 18:11:35 +053056 if(this.frm.fields_dict.selling_price_list) {
57 this.frm.set_query("selling_price_list", function() {
Nabin Haitdc15b4f2014-01-20 16:48:49 +053058 return { filters: { selling: 1 } };
Anand Doshi720a01a2013-07-26 11:32:02 +053059 });
Anand Doshi720a01a2013-07-26 11:32:02 +053060 }
Anand Doshibd67e872014-04-11 16:51:27 +053061
Nabin Haitdd38a262014-12-26 13:15:21 +053062 if(!this.frm.fields_dict["items"]) {
Anand Doshi99100a42013-07-04 17:13:53 +053063 return;
64 }
Anand Doshibd67e872014-04-11 16:51:27 +053065
Nabin Haitdd38a262014-12-26 13:15:21 +053066 if(this.frm.fields_dict["items"].grid.get_field('item_code')) {
67 this.frm.set_query("item_code", "items", function() {
Anand Doshi9b496142013-07-11 19:13:58 +053068 return {
Rushabh Mehtad973c162016-08-16 11:32:08 +053069 query: "erpnext.controllers.queries.item_query",
70 filters: {'is_sales_item': 1}
Anand Doshi9b496142013-07-11 19:13:58 +053071 }
Anand Doshi99100a42013-07-04 17:13:53 +053072 });
73 }
Anand Doshibd67e872014-04-11 16:51:27 +053074
Rushabh Mehta698c0402016-11-22 23:16:40 +053075 if(this.frm.fields_dict["packed_items"] &&
76 this.frm.fields_dict["packed_items"].grid.get_field('batch_no')) {
Rohit Waghchauree887e922016-11-18 12:16:22 +053077 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 Waghchauree887e922016-11-18 12:16:22 +053081 },
82
Anand Doshic4a54fe2013-08-01 18:19:51 +053083 refresh: function() {
84 this._super();
Rushabh Mehtab92087c2017-01-13 18:53:11 +053085
Rushabh Mehta8d39fd92017-01-16 13:06:07 +053086 frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'customer', doctype: 'Customer'}
Rushabh Mehtab92087c2017-01-13 18:53:11 +053087
Anand Doshibd67e872014-04-11 16:51:27 +053088 this.frm.toggle_display("customer_name",
Akhilesh Darjee5ce1b8b2013-12-09 16:29:04 +053089 (this.frm.doc.customer_name && this.frm.doc.customer_name!==this.frm.doc.customer));
Nabin Haite7d15362014-12-25 16:01:55 +053090 if(this.frm.fields_dict.packed_items) {
91 var packing_list_exists = (this.frm.doc.packed_items || []).length;
Anand Doshic4a54fe2013-08-01 18:19:51 +053092 this.frm.toggle_display("packing_list", packing_list_exists ? true : false);
93 }
Nabin Hait2029ea92015-04-16 17:55:35 +053094 this.toggle_editable_price_list_rate();
Rushabh Mehta8aded132013-07-04 12:50:52 +053095 },
Anand Doshibd67e872014-04-11 16:51:27 +053096
Anand Doshi3543f302013-05-24 19:25:01 +053097 customer: function() {
Nabin Haita3dd72a2014-05-28 12:49:20 +053098 var me = this;
Nabin Haite45ec662018-08-01 17:44:34 +053099 erpnext.utils.get_party_details(this.frm, null, null, function() {
100 me.apply_price_list();
101 });
Anand Doshi3543f302013-05-24 19:25:01 +0530102 },
Anand Doshibd67e872014-04-11 16:51:27 +0530103
Nabin Haita279d782013-07-15 13:04:33 +0530104 customer_address: function() {
Rushabh Mehtab09d9da2014-01-02 11:47:23 +0530105 erpnext.utils.get_address_display(this.frm, "customer_address");
Saif Ur Rehmanfd531a62018-12-29 01:49:11 +0500106 erpnext.utils.set_taxes_from_address(this.frm, "customer_address", "customer_address", "shipping_address_name");
Nabin Haita279d782013-07-15 13:04:33 +0530107 },
Anand Doshibd67e872014-04-11 16:51:27 +0530108
Nabin Hait9d1f0772014-02-19 17:43:24 +0530109 shipping_address_name: function() {
110 erpnext.utils.get_address_display(this.frm, "shipping_address_name", "shipping_address");
Saif Ur Rehmanfd531a62018-12-29 01:49:11 +0500111 erpnext.utils.set_taxes_from_address(this.frm, "shipping_address_name", "customer_address", "shipping_address_name");
Nabin Hait9d1f0772014-02-19 17:43:24 +0530112 },
Anand Doshibd67e872014-04-11 16:51:27 +0530113
Nabin Haita3dd72a2014-05-28 12:49:20 +0530114 sales_partner: function() {
115 this.apply_pricing_rule();
116 },
117
118 campaign: function() {
119 this.apply_pricing_rule();
120 },
121
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530122 selling_price_list: function() {
Anand Doshidffec8f2014-07-01 17:45:15 +0530123 this.apply_price_list();
rohitwaghchaure8e675eb2018-02-13 17:11:06 +0530124 this.set_dynamic_labels();
Anand Doshif3096132013-05-21 19:35:06 +0530125 },
Anand Doshibd67e872014-04-11 16:51:27 +0530126
Nabin Haita7f757a2014-02-10 17:54:04 +0530127 price_list_rate: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530128 var item = frappe.get_doc(cdt, cdn);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530129 frappe.model.round_floats_in(item, ["price_list_rate", "discount_percentage"]);
Anand Doshibd67e872014-04-11 16:51:27 +0530130
mbauskara52472c2016-03-05 15:10:25 +0530131 // check if child doctype is Sales Order Item/Qutation Item and calculate the rate
132 if(in_list(["Quotation Item", "Sales Order Item", "Delivery Note Item", "Sales Invoice Item"]), cdt)
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530133 this.apply_pricing_rule_on_item(item);
mbauskara52472c2016-03-05 15:10:25 +0530134 else
135 item.rate = flt(item.price_list_rate * (1 - item.discount_percentage / 100.0),
136 precision("rate", item));
137
Anand Doshif3096132013-05-21 19:35:06 +0530138 this.calculate_taxes_and_totals();
139 },
Anand Doshibd67e872014-04-11 16:51:27 +0530140
Nabin Haita7f757a2014-02-10 17:54:04 +0530141 discount_percentage: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530142 var item = frappe.get_doc(cdt, cdn);
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530143 item.discount_amount = 0.0;
144 this.apply_discount_on_item(doc, cdt, cdn, 'discount_percentage');
145 },
146
147 discount_amount: function(doc, cdt, cdn) {
Nabin Hait593242f2019-04-05 19:35:02 +0530148 var item = frappe.get_doc(cdt, cdn);
149 item.discount_percentage = 0.0;
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530150 this.apply_discount_on_item(doc, cdt, cdn, 'discount_amount');
151 },
152
153 apply_discount_on_item: function(doc, cdt, cdn, field) {
154 var item = frappe.get_doc(cdt, cdn);
Nabin Haita7f757a2014-02-10 17:54:04 +0530155 if(!item.price_list_rate) {
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530156 item[field] = 0.0;
Anand Doshi923d41d2013-05-28 17:23:36 +0530157 } else {
Nabin Haita7f757a2014-02-10 17:54:04 +0530158 this.price_list_rate(doc, cdt, cdn);
Anand Doshi923d41d2013-05-28 17:23:36 +0530159 }
Saurabh5ada14b2016-02-26 18:02:55 +0530160 this.set_gross_profit(item);
Anand Doshif3096132013-05-21 19:35:06 +0530161 },
Anand Doshibd67e872014-04-11 16:51:27 +0530162
Anand Doshif3096132013-05-21 19:35:06 +0530163 commission_rate: function() {
164 this.calculate_commission();
165 refresh_field("total_commission");
166 },
Anand Doshibd67e872014-04-11 16:51:27 +0530167
Anand Doshif3096132013-05-21 19:35:06 +0530168 total_commission: function() {
Nabin Hait5690be12015-02-12 16:09:11 +0530169 if(this.frm.doc.base_net_total) {
170 frappe.model.round_floats_in(this.frm.doc, ["base_net_total", "total_commission"]);
Anand Doshibd67e872014-04-11 16:51:27 +0530171
Nabin Hait5690be12015-02-12 16:09:11 +0530172 if(this.frm.doc.base_net_total < this.frm.doc.total_commission) {
Pratik Vyasb52618c2014-04-14 16:25:30 +0530173 var msg = (__("[Error]") + " " +
174 __(frappe.meta.get_label(this.frm.doc.doctype, "total_commission",
Anand Doshibd67e872014-04-11 16:51:27 +0530175 this.frm.doc.name)) + " > " +
Nabin Hait5690be12015-02-12 16:09:11 +0530176 __(frappe.meta.get_label(this.frm.doc.doctype, "base_net_total", this.frm.doc.name)));
Faris Ansariab74ca72017-05-30 12:54:42 +0530177 frappe.msgprint(msg);
Anand Doshif3096132013-05-21 19:35:06 +0530178 throw msg;
179 }
Anand Doshibd67e872014-04-11 16:51:27 +0530180
181 this.frm.set_value("commission_rate",
Nabin Hait5690be12015-02-12 16:09:11 +0530182 flt(this.frm.doc.total_commission * 100.0 / this.frm.doc.base_net_total));
Anand Doshif3096132013-05-21 19:35:06 +0530183 }
184 },
Anand Doshibd67e872014-04-11 16:51:27 +0530185
Anand Doshif3096132013-05-21 19:35:06 +0530186 allocated_percentage: function(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530187 var sales_person = frappe.get_doc(cdt, cdn);
Anand Doshif3096132013-05-21 19:35:06 +0530188 if(sales_person.allocated_percentage) {
deepeshgarg00786152eb2018-09-25 19:08:16 +0530189
Anand Doshif3096132013-05-21 19:35:06 +0530190 sales_person.allocated_percentage = flt(sales_person.allocated_percentage,
191 precision("allocated_percentage", sales_person));
deepeshgarg00786152eb2018-09-25 19:08:16 +0530192
Nabin Hait5690be12015-02-12 16:09:11 +0530193 sales_person.allocated_amount = flt(this.frm.doc.base_net_total *
Anand Doshibd67e872014-04-11 16:51:27 +0530194 sales_person.allocated_percentage / 100.0,
Anand Doshif3096132013-05-21 19:35:06 +0530195 precision("allocated_amount", sales_person));
deepeshgarg00786152eb2018-09-25 19:08:16 +0530196 refresh_field(["allocated_amount"], sales_person);
Anand Doshif3096132013-05-21 19:35:06 +0530197
deepeshgarg00786152eb2018-09-25 19:08:16 +0530198 this.calculate_incentive(sales_person);
199 refresh_field(["allocated_percentage", "allocated_amount", "commission_rate","incentives"], sales_person.name,
Anand Doshif3096132013-05-21 19:35:06 +0530200 sales_person.parentfield);
deepeshgarg007d9a768a2018-12-29 19:33:44 +0530201 }
deepeshgarg00786152eb2018-09-25 19:08:16 +0530202 },
203
204 sales_person: function(doc, cdt, cdn) {
205 var row = frappe.get_doc(cdt, cdn);
206 this.calculate_incentive(row);
207 refresh_field("incentives",row.name,row.parentfield);
Anand Doshif3096132013-05-21 19:35:06 +0530208 },
Anand Doshibd67e872014-04-11 16:51:27 +0530209
Anand Doshifc777182013-05-27 19:29:07 +0530210 warehouse: function(doc, cdt, cdn) {
Sambhaji Kolate98dbccd2015-03-10 15:04:28 +0530211 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530212 var item = frappe.get_doc(cdt, cdn);
Shreyaa20157a2018-04-13 12:03:42 +0530213 if (item.serial_no && !item.batch_no) {
214 item.serial_no = null;
Anand Doshifc777182013-05-27 19:29:07 +0530215 }
Shreyaa20157a2018-04-13 12:03:42 +0530216 var has_batch_no;
217 frappe.db.get_value('Item', {'item_code': item.item_code}, 'has_batch_no', (r) => {
218 has_batch_no = r && r.has_batch_no;
219 if(item.item_code && item.warehouse) {
220 return this.frm.call({
221 method: "erpnext.stock.get_item_details.get_bin_details_and_serial_nos",
222 child: item,
223 args: {
224 item_code: item.item_code,
225 warehouse: item.warehouse,
226 has_batch_no: has_batch_no,
227 stock_qty: item.stock_qty,
228 serial_no: item.serial_no || "",
229 },
230 callback:function(r){
231 if (in_list(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
232 me.set_batch_number(cdt, cdn);
233 me.batch_no(doc, cdt, cdn);
234 }
235 }
236 });
237 }
238 })
Anand Doshifc777182013-05-27 19:29:07 +0530239 },
Anand Doshibd67e872014-04-11 16:51:27 +0530240
Anand Doshi33fe8672013-08-02 12:39:10 +0530241 toggle_editable_price_list_rate: function() {
Nabin Haitdd38a262014-12-26 13:15:21 +0530242 var df = frappe.meta.get_docfield(this.frm.doc.doctype + " Item", "price_list_rate", this.frm.doc.name);
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530243 var editable_price_list_rate = cint(frappe.defaults.get_default("editable_price_list_rate"));
Anand Doshibd67e872014-04-11 16:51:27 +0530244
Anand Doshi33fe8672013-08-02 12:39:10 +0530245 if(df && editable_price_list_rate) {
246 df.read_only = 0;
247 }
248 },
Anand Doshibd67e872014-04-11 16:51:27 +0530249
Anand Doshif3096132013-05-21 19:35:06 +0530250 calculate_commission: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530251 if(this.frm.fields_dict.commission_rate) {
252 if(this.frm.doc.commission_rate > 100) {
Pratik Vyasb52618c2014-04-14 16:25:30 +0530253 var msg = __(frappe.meta.get_label(this.frm.doc.doctype, "commission_rate", this.frm.doc.name)) +
254 " " + __("cannot be greater than 100");
Faris Ansariab74ca72017-05-30 12:54:42 +0530255 frappe.msgprint(msg);
Anand Doshi923d41d2013-05-28 17:23:36 +0530256 throw msg;
257 }
Anand Doshibd67e872014-04-11 16:51:27 +0530258
Nabin Hait5690be12015-02-12 16:09:11 +0530259 this.frm.doc.total_commission = flt(this.frm.doc.base_net_total * this.frm.doc.commission_rate / 100.0,
Anand Doshi923d41d2013-05-28 17:23:36 +0530260 precision("total_commission"));
261 }
Anand Doshif3096132013-05-21 19:35:06 +0530262 },
Anand Doshibd67e872014-04-11 16:51:27 +0530263
Anand Doshif3096132013-05-21 19:35:06 +0530264 calculate_contribution: function() {
Anand Doshi2168e392013-05-23 19:25:08 +0530265 var me = this;
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530266 $.each(this.frm.doc.doctype.sales_team || [], function(i, sales_person) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530267 frappe.model.round_floats_in(sales_person);
268 if(sales_person.allocated_percentage) {
269 sales_person.allocated_amount = flt(
270 me.frm.doc.base_net_total * sales_person.allocated_percentage / 100.0,
271 precision("allocated_amount", sales_person));
272 }
273 });
Anand Doshif3096132013-05-21 19:35:06 +0530274 },
Anand Doshibd67e872014-04-11 16:51:27 +0530275
deepeshgarg00786152eb2018-09-25 19:08:16 +0530276 calculate_incentive: function(row) {
277 if(row.allocated_amount)
278 {
279 row.incentives = flt(
280 row.allocated_amount * row.commission_rate / 100.0,
deepeshgarg007c3772f12018-09-26 15:55:43 +0530281 precision("incentives", row));
deepeshgarg00786152eb2018-09-25 19:08:16 +0530282 }
283 },
284
Sambhaji Kolate98dbccd2015-03-10 15:04:28 +0530285 batch_no: function(doc, cdt, cdn) {
286 var me = this;
287 var item = frappe.get_doc(cdt, cdn);
Shreyaa20157a2018-04-13 12:03:42 +0530288 item.serial_no = null;
289 var has_serial_no;
290 frappe.db.get_value('Item', {'item_code': item.item_code}, 'has_serial_no', (r) => {
291 has_serial_no = r && r.has_serial_no;
292 if(item.warehouse && item.item_code && item.batch_no) {
293 return this.frm.call({
294 method: "erpnext.stock.get_item_details.get_batch_qty_and_serial_no",
295 child: item,
296 args: {
297 "batch_no": item.batch_no,
298 "stock_qty": item.stock_qty,
299 "warehouse": item.warehouse,
300 "item_code": item.item_code,
301 "has_serial_no": has_serial_no
302 },
303 "fieldname": "actual_batch_qty"
304 });
305 }
306 })
Sambhaji Kolate98dbccd2015-03-10 15:04:28 +0530307 },
308
Anand Doshi2168e392013-05-23 19:25:08 +0530309 set_dynamic_labels: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530310 this._super();
Neil Trini Lasradoed8cecb2015-07-07 13:59:23 +0530311 this.set_product_bundle_help(this.frm.doc);
Nabin Hait0b157552014-06-24 17:02:45 +0530312 },
313
Neil Trini Lasradoed8cecb2015-07-07 13:59:23 +0530314 set_product_bundle_help: function(doc) {
Nabin Hait0b157552014-06-24 17:02:45 +0530315 if(!cur_frm.fields_dict.packing_list) return;
Nabin Haite7d15362014-12-25 16:01:55 +0530316 if ((doc.packed_items || []).length) {
Nabin Hait0b157552014-06-24 17:02:45 +0530317 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(true);
318
Faris Ansariab74ca72017-05-30 12:54:42 +0530319 if (in_list(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
320 var help_msg = "<div class='alert alert-warning'>" +
Neil Trini Lasradoed8cecb2015-07-07 13:59:23 +0530321 __("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 Hait0b157552014-06-24 17:02:45 +0530322 "</div>";
Neil Trini Lasradoed8cecb2015-07-07 13:59:23 +0530323 frappe.meta.get_docfield(doc.doctype, 'product_bundle_help', doc.name).options = help_msg;
Nabin Hait0b157552014-06-24 17:02:45 +0530324 }
325 } else {
326 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(false);
Faris Ansariab74ca72017-05-30 12:54:42 +0530327 if (in_list(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
Neil Trini Lasradoed8cecb2015-07-07 13:59:23 +0530328 frappe.meta.get_docfield(doc.doctype, 'product_bundle_help', doc.name).options = '';
Nabin Hait0b157552014-06-24 17:02:45 +0530329 }
330 }
Neil Trini Lasradoed8cecb2015-07-07 13:59:23 +0530331 refresh_field('product_bundle_help');
Saurabheba7aa42015-12-16 09:57:59 +0530332 },
Anand Doshi01de9452016-01-26 16:22:50 +0530333
mbauskara52472c2016-03-05 15:10:25 +0530334 margin_rate_or_amount: function(doc, cdt, cdn) {
mbauskar1e9c1642016-01-18 16:21:25 +0530335 // calculated the revised total margin and rate on margin rate changes
Faris Ansariab74ca72017-05-30 12:54:42 +0530336 var item = locals[cdt][cdn];
Rohit Waghchaure6087fe12016-04-09 14:31:09 +0530337 this.apply_pricing_rule_on_item(item)
mbauskara52472c2016-03-05 15:10:25 +0530338 this.calculate_taxes_and_totals();
mbauskar1e9c1642016-01-18 16:21:25 +0530339 cur_frm.refresh_fields();
340 },
mbauskar1e428ae2016-01-19 19:52:22 +0530341
mbauskara52472c2016-03-05 15:10:25 +0530342 margin_type: function(doc, cdt, cdn){
mbauskar1e9c1642016-01-18 16:21:25 +0530343 // calculate the revised total margin and rate on margin type changes
Faris Ansariab74ca72017-05-30 12:54:42 +0530344 var item = locals[cdt][cdn];
Nabin Haite72c98d2017-05-24 09:01:44 +0530345 if(!item.margin_type) {
346 frappe.model.set_value(cdt, cdn, "margin_rate_or_amount", 0);
347 } else {
348 this.apply_pricing_rule_on_item(item, doc,cdt, cdn)
349 this.calculate_taxes_and_totals();
350 cur_frm.refresh_fields();
351 }
Nabin Hait879e1622017-08-21 08:28:55 +0530352 },
353
354 company_address: function() {
355 var me = this;
356 if(this.frm.doc.company_address) {
357 frappe.call({
358 method: "frappe.contacts.doctype.address.address.get_address_display",
359 args: {"address_dict": this.frm.doc.company_address },
360 callback: function(r) {
361 if(r.message) {
362 me.frm.set_value("company_address_display", r.message)
363 }
364 }
365 })
366 } else {
367 this.frm.set_value("company_address_display", "");
368 }
tundebabzyacccdb32017-11-23 08:35:15 +0100369 },
370
371 conversion_factor: function(doc, cdt, cdn, dont_fetch_price_list_rate) {
372 this._super(doc, cdt, cdn, dont_fetch_price_list_rate);
tundebabzyf9b5c742018-01-25 19:21:05 +0100373 if(frappe.meta.get_docfield(cdt, "stock_qty", cdn) &&
374 in_list(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
tundebabzyacccdb32017-11-23 08:35:15 +0100375 this.set_batch_number(cdt, cdn);
376 }
377 },
378
379 qty: function(doc, cdt, cdn) {
Shreyaa20157a2018-04-13 12:03:42 +0530380 this._super(doc, cdt, cdn);
tundebabzyacccdb32017-11-23 08:35:15 +0100381 this.set_batch_number(cdt, cdn);
382 },
383
384 /* Determine appropriate batch number and set it in the form.
385 * @param {string} cdt - Document Doctype
386 * @param {string} cdn - Document name
387 */
388 set_batch_number: function(cdt, cdn) {
389 const doc = frappe.get_doc(cdt, cdn);
rohitwaghchaure15f8fe02017-11-24 10:46:27 +0530390 if (doc && doc.has_batch_no) {
tundebabzyacccdb32017-11-23 08:35:15 +0100391 this._set_batch_number(doc);
392 }
393 },
394
395 _set_batch_number: function(doc) {
396 return frappe.call({
397 method: 'erpnext.stock.doctype.batch.batch.get_batch_no',
398 args: {'item_code': doc.item_code, 'warehouse': doc.warehouse, 'qty': flt(doc.qty) * flt(doc.conversion_factor)},
399 callback: function(r) {
400 if(r.message) {
401 frappe.model.set_value(doc.doctype, doc.name, 'batch_no', r.message);
tundebabzy8ae53ca2017-11-30 07:07:21 +0100402 } else {
403 frappe.model.set_value(doc.doctype, doc.name, 'batch_no', r.message);
tundebabzyacccdb32017-11-23 08:35:15 +0100404 }
405 }
406 });
407 },
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200408
409 update_auto_repeat_reference: function(doc) {
410 if (doc.auto_repeat) {
411 frappe.call({
412 method:"frappe.desk.doctype.auto_repeat.auto_repeat.update_reference",
deepeshgarg007d9a768a2018-12-29 19:33:44 +0530413 args:{
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200414 docname: doc.auto_repeat,
415 reference:doc.name
416 },
417 callback: function(r){
418 if (r.message=="success") {
419 frappe.show_alert({message:__("Auto repeat document updated"), indicator:'green'});
420 } else {
421 frappe.show_alert({message:__("An error occurred during the update process"), indicator:'red'});
422 }
423 }
424 })
425 }
426 }
Nabin Hait0b157552014-06-24 17:02:45 +0530427});
Anand Doshi43f087c2014-08-26 14:25:53 +0530428
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +0530429frappe.ui.form.on(cur_frm.doctype,"project", function(frm) {
Nabin Hait10b155a2014-10-23 13:18:59 +0530430 if(in_list(["Delivery Note", "Sales Invoice"], frm.doc.doctype)) {
Rushabh Mehta532eedf2016-05-19 18:22:49 +0530431 if(frm.doc.project) {
432 frappe.call({
433 method:'erpnext.projects.doctype.project.project.get_cost_center_name' ,
434 args: { project: frm.doc.project },
435 callback: function(r, rt) {
436 if(!r.exc) {
437 $.each(frm.doc["items"] || [], function(i, row) {
438 if(r.message) {
439 frappe.model.set_value(row.doctype, row.name, "cost_center", r.message);
Faris Ansariab74ca72017-05-30 12:54:42 +0530440 frappe.msgprint(__("Cost Center For Item with Item Code '"+row.item_name+"' has been Changed to "+ r.message));
Rushabh Mehta532eedf2016-05-19 18:22:49 +0530441 }
442 })
443 }
Nabin Hait10b155a2014-10-23 13:18:59 +0530444 }
Rushabh Mehta532eedf2016-05-19 18:22:49 +0530445 })
446 }
Nabin Hait10b155a2014-10-23 13:18:59 +0530447 }
Nabin Haite45ec662018-08-01 17:44:34 +0530448})
deepeshgarg007d9a768a2018-12-29 19:33:44 +0530449
450frappe.ui.form.on(cur_frm.doctype, {
451 set_as_lost_dialog: function(frm) {
452 var dialog = new frappe.ui.Dialog({
453 title: __("Set as Lost"),
454 fields: [
455 {"fieldtype": "Table MultiSelect",
456 "label": __("Lost Reasons"),
457 "fieldname": "lost_reason",
458 "options": "Lost Reason Detail",
459 "reqd": 1},
460
461 {"fieldtype": "Text", "label": __("Detailed Reason"), "fieldname": "detailed_reason"},
462 ],
463 primary_action: function() {
464 var values = dialog.get_values();
465 var reasons = values["lost_reason"];
466 var detailed_reason = values["detailed_reason"];
467
468 frm.call({
469 doc: frm.doc,
470 method: 'declare_enquiry_lost',
471 args: {
472 'lost_reasons_list': reasons,
473 'detailed_reason': detailed_reason
474 },
475 callback: function(r) {
476 dialog.hide();
477 frm.reload_doc();
478 },
479 });
480 refresh_field("lost_reason");
481 },
482 primary_action_label: __('Declare Lost')
483 });
484
485 dialog.show();
486 }
487})