blob: f515baf31bc041e8e1bf0a1a87e06968569d1329 [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");
Faris Ansari1fe891b2021-04-23 08:04:00 +053012erpnext.selling.SellingController = class SellingController extends erpnext.TransactionController {
13 setup() {
14 super.setup();
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");
Faris Ansari1fe891b2021-04-23 08:04:00 +053017 }
Rushabh Mehta532eedf2016-05-19 18:22:49 +053018
Faris Ansari1fe891b2021-04-23 08:04:00 +053019 onload() {
20 super.onload();
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 });
Subin Tom5265ba32021-07-13 14:58:17 +053029 }
Anand Doshibd67e872014-04-11 16:51:27 +053030
Faris Ansari1fe891b2021-04-23 08:04:00 +053031 setup_queries() {
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
Anand Doshi99100a42013-07-04 17:13:53 +053045
Rushabh Mehta4a404e92013-08-09 18:11:35 +053046 if(this.frm.fields_dict.selling_price_list) {
47 this.frm.set_query("selling_price_list", function() {
Nabin Haitdc15b4f2014-01-20 16:48:49 +053048 return { filters: { selling: 1 } };
Anand Doshi720a01a2013-07-26 11:32:02 +053049 });
Anand Doshi720a01a2013-07-26 11:32:02 +053050 }
Anand Doshibd67e872014-04-11 16:51:27 +053051
karthikeyan57fc60212019-07-04 22:46:16 +053052 if(this.frm.fields_dict.tc_name) {
53 this.frm.set_query("tc_name", function() {
54 return { filters: { selling: 1 } };
55 });
56 }
57
Nabin Haitdd38a262014-12-26 13:15:21 +053058 if(!this.frm.fields_dict["items"]) {
Anand Doshi99100a42013-07-04 17:13:53 +053059 return;
60 }
Anand Doshibd67e872014-04-11 16:51:27 +053061
Nabin Haitdd38a262014-12-26 13:15:21 +053062 if(this.frm.fields_dict["items"].grid.get_field('item_code')) {
63 this.frm.set_query("item_code", "items", function() {
Anand Doshi9b496142013-07-11 19:13:58 +053064 return {
Rushabh Mehtad973c162016-08-16 11:32:08 +053065 query: "erpnext.controllers.queries.item_query",
66 filters: {'is_sales_item': 1}
Anand Doshi9b496142013-07-11 19:13:58 +053067 }
Anand Doshi99100a42013-07-04 17:13:53 +053068 });
69 }
Anand Doshibd67e872014-04-11 16:51:27 +053070
Rushabh Mehta698c0402016-11-22 23:16:40 +053071 if(this.frm.fields_dict["packed_items"] &&
72 this.frm.fields_dict["packed_items"].grid.get_field('batch_no')) {
Rohit Waghchauree887e922016-11-18 12:16:22 +053073 this.frm.set_query("batch_no", "packed_items", function(doc, cdt, cdn) {
74 return me.set_query_for_batch(doc, cdt, cdn)
75 });
76 }
Deepesh Gargef0d26c2020-01-06 15:34:15 +053077
78 if(this.frm.fields_dict["items"].grid.get_field('item_code')) {
79 this.frm.set_query("item_tax_template", "items", function(doc, cdt, cdn) {
80 return me.set_query_for_item_tax_template(doc, cdt, cdn)
81 });
82 }
83
Faris Ansari1fe891b2021-04-23 08:04:00 +053084 }
Rohit Waghchauree887e922016-11-18 12:16:22 +053085
Faris Ansari1fe891b2021-04-23 08:04:00 +053086 refresh() {
87 super.refresh();
Subin Tom5265ba32021-07-13 14:58:17 +053088
Rushabh Mehta8d39fd92017-01-16 13:06:07 +053089 frappe.dynamic_link = {doc: this.frm.doc, fieldname: 'customer', doctype: 'Customer'}
Rushabh Mehtab92087c2017-01-13 18:53:11 +053090
Anand Doshibd67e872014-04-11 16:51:27 +053091 this.frm.toggle_display("customer_name",
Akhilesh Darjee5ce1b8b2013-12-09 16:29:04 +053092 (this.frm.doc.customer_name && this.frm.doc.customer_name!==this.frm.doc.customer));
Nabin Haite7d15362014-12-25 16:01:55 +053093 if(this.frm.fields_dict.packed_items) {
94 var packing_list_exists = (this.frm.doc.packed_items || []).length;
Anand Doshic4a54fe2013-08-01 18:19:51 +053095 this.frm.toggle_display("packing_list", packing_list_exists ? true : false);
96 }
Nabin Hait2029ea92015-04-16 17:55:35 +053097 this.toggle_editable_price_list_rate();
Faris Ansari1fe891b2021-04-23 08:04:00 +053098 }
Anand Doshibd67e872014-04-11 16:51:27 +053099
Faris Ansari1fe891b2021-04-23 08:04:00 +0530100 customer() {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530101 var me = this;
Nabin Haite45ec662018-08-01 17:44:34 +0530102 erpnext.utils.get_party_details(this.frm, null, null, function() {
103 me.apply_price_list();
104 });
Faris Ansari1fe891b2021-04-23 08:04:00 +0530105 }
Anand Doshibd67e872014-04-11 16:51:27 +0530106
Faris Ansari1fe891b2021-04-23 08:04:00 +0530107 customer_address() {
Rushabh Mehtab09d9da2014-01-02 11:47:23 +0530108 erpnext.utils.get_address_display(this.frm, "customer_address");
Saif Ur Rehmanfd531a62018-12-29 01:49:11 +0500109 erpnext.utils.set_taxes_from_address(this.frm, "customer_address", "customer_address", "shipping_address_name");
Faris Ansari1fe891b2021-04-23 08:04:00 +0530110 }
Anand Doshibd67e872014-04-11 16:51:27 +0530111
Faris Ansari1fe891b2021-04-23 08:04:00 +0530112 shipping_address_name() {
Nabin Hait9d1f0772014-02-19 17:43:24 +0530113 erpnext.utils.get_address_display(this.frm, "shipping_address_name", "shipping_address");
Saif Ur Rehmanfd531a62018-12-29 01:49:11 +0500114 erpnext.utils.set_taxes_from_address(this.frm, "shipping_address_name", "customer_address", "shipping_address_name");
Faris Ansari1fe891b2021-04-23 08:04:00 +0530115 }
Anand Doshibd67e872014-04-11 16:51:27 +0530116
Subin Tom5265ba32021-07-13 14:58:17 +0530117 dispatch_address_name() {
118 erpnext.utils.get_address_display(this.frm, "dispatch_address_name", "dispatch_address");
119 }
120
Faris Ansari1fe891b2021-04-23 08:04:00 +0530121 sales_partner() {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530122 this.apply_pricing_rule();
Faris Ansari1fe891b2021-04-23 08:04:00 +0530123 }
Nabin Haita3dd72a2014-05-28 12:49:20 +0530124
Faris Ansari1fe891b2021-04-23 08:04:00 +0530125 campaign() {
Nabin Haita3dd72a2014-05-28 12:49:20 +0530126 this.apply_pricing_rule();
Faris Ansari1fe891b2021-04-23 08:04:00 +0530127 }
Nabin Haita3dd72a2014-05-28 12:49:20 +0530128
Faris Ansari1fe891b2021-04-23 08:04:00 +0530129 selling_price_list() {
Anand Doshidffec8f2014-07-01 17:45:15 +0530130 this.apply_price_list();
rohitwaghchaure8e675eb2018-02-13 17:11:06 +0530131 this.set_dynamic_labels();
Faris Ansari1fe891b2021-04-23 08:04:00 +0530132 }
Anand Doshibd67e872014-04-11 16:51:27 +0530133
Faris Ansari1fe891b2021-04-23 08:04:00 +0530134 discount_percentage(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530135 var item = frappe.get_doc(cdt, cdn);
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530136 item.discount_amount = 0.0;
137 this.apply_discount_on_item(doc, cdt, cdn, 'discount_percentage');
Faris Ansari1fe891b2021-04-23 08:04:00 +0530138 }
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530139
Faris Ansari1fe891b2021-04-23 08:04:00 +0530140 discount_amount(doc, cdt, cdn) {
deepeshgarg007a9ea49c2019-07-04 15:56:34 +0530141
142 if(doc.name === cdn) {
143 return;
144 }
145
Nabin Hait593242f2019-04-05 19:35:02 +0530146 var item = frappe.get_doc(cdt, cdn);
147 item.discount_percentage = 0.0;
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530148 this.apply_discount_on_item(doc, cdt, cdn, 'discount_amount');
Faris Ansari1fe891b2021-04-23 08:04:00 +0530149 }
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530150
Faris Ansari1fe891b2021-04-23 08:04:00 +0530151 apply_discount_on_item(doc, cdt, cdn, field) {
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530152 var item = frappe.get_doc(cdt, cdn);
Nabin Haita7f757a2014-02-10 17:54:04 +0530153 if(!item.price_list_rate) {
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530154 item[field] = 0.0;
Anand Doshi923d41d2013-05-28 17:23:36 +0530155 } else {
Nabin Haita7f757a2014-02-10 17:54:04 +0530156 this.price_list_rate(doc, cdt, cdn);
Anand Doshi923d41d2013-05-28 17:23:36 +0530157 }
Saurabh5ada14b2016-02-26 18:02:55 +0530158 this.set_gross_profit(item);
Faris Ansari1fe891b2021-04-23 08:04:00 +0530159 }
Anand Doshibd67e872014-04-11 16:51:27 +0530160
Faris Ansari1fe891b2021-04-23 08:04:00 +0530161 commission_rate() {
Anand Doshif3096132013-05-21 19:35:06 +0530162 this.calculate_commission();
163 refresh_field("total_commission");
Faris Ansari1fe891b2021-04-23 08:04:00 +0530164 }
Anand Doshibd67e872014-04-11 16:51:27 +0530165
Faris Ansari1fe891b2021-04-23 08:04:00 +0530166 total_commission() {
Nabin Hait5690be12015-02-12 16:09:11 +0530167 if(this.frm.doc.base_net_total) {
168 frappe.model.round_floats_in(this.frm.doc, ["base_net_total", "total_commission"]);
Anand Doshibd67e872014-04-11 16:51:27 +0530169
Nabin Hait5690be12015-02-12 16:09:11 +0530170 if(this.frm.doc.base_net_total < this.frm.doc.total_commission) {
Pratik Vyasb52618c2014-04-14 16:25:30 +0530171 var msg = (__("[Error]") + " " +
172 __(frappe.meta.get_label(this.frm.doc.doctype, "total_commission",
Anand Doshibd67e872014-04-11 16:51:27 +0530173 this.frm.doc.name)) + " > " +
Nabin Hait5690be12015-02-12 16:09:11 +0530174 __(frappe.meta.get_label(this.frm.doc.doctype, "base_net_total", this.frm.doc.name)));
Faris Ansariab74ca72017-05-30 12:54:42 +0530175 frappe.msgprint(msg);
Anand Doshif3096132013-05-21 19:35:06 +0530176 throw msg;
177 }
Anand Doshibd67e872014-04-11 16:51:27 +0530178
179 this.frm.set_value("commission_rate",
Nabin Hait5690be12015-02-12 16:09:11 +0530180 flt(this.frm.doc.total_commission * 100.0 / this.frm.doc.base_net_total));
Anand Doshif3096132013-05-21 19:35:06 +0530181 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530182 }
Anand Doshibd67e872014-04-11 16:51:27 +0530183
Faris Ansari1fe891b2021-04-23 08:04:00 +0530184 allocated_percentage(doc, cdt, cdn) {
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530185 var sales_person = frappe.get_doc(cdt, cdn);
Anand Doshif3096132013-05-21 19:35:06 +0530186 if(sales_person.allocated_percentage) {
deepeshgarg00786152eb2018-09-25 19:08:16 +0530187
Anand Doshif3096132013-05-21 19:35:06 +0530188 sales_person.allocated_percentage = flt(sales_person.allocated_percentage,
189 precision("allocated_percentage", sales_person));
deepeshgarg00786152eb2018-09-25 19:08:16 +0530190
Nabin Hait5690be12015-02-12 16:09:11 +0530191 sales_person.allocated_amount = flt(this.frm.doc.base_net_total *
Anand Doshibd67e872014-04-11 16:51:27 +0530192 sales_person.allocated_percentage / 100.0,
Anand Doshif3096132013-05-21 19:35:06 +0530193 precision("allocated_amount", sales_person));
deepeshgarg00786152eb2018-09-25 19:08:16 +0530194 refresh_field(["allocated_amount"], sales_person);
Anand Doshif3096132013-05-21 19:35:06 +0530195
deepeshgarg00786152eb2018-09-25 19:08:16 +0530196 this.calculate_incentive(sales_person);
197 refresh_field(["allocated_percentage", "allocated_amount", "commission_rate","incentives"], sales_person.name,
Anand Doshif3096132013-05-21 19:35:06 +0530198 sales_person.parentfield);
deepeshgarg007d9a768a2018-12-29 19:33:44 +0530199 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530200 }
deepeshgarg00786152eb2018-09-25 19:08:16 +0530201
Faris Ansari1fe891b2021-04-23 08:04:00 +0530202 sales_person(doc, cdt, cdn) {
deepeshgarg00786152eb2018-09-25 19:08:16 +0530203 var row = frappe.get_doc(cdt, cdn);
204 this.calculate_incentive(row);
205 refresh_field("incentives",row.name,row.parentfield);
Faris Ansari1fe891b2021-04-23 08:04:00 +0530206 }
Anand Doshibd67e872014-04-11 16:51:27 +0530207
Faris Ansari1fe891b2021-04-23 08:04:00 +0530208 warehouse(doc, cdt, cdn) {
Sambhaji Kolate98dbccd2015-03-10 15:04:28 +0530209 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530210 var item = frappe.get_doc(cdt, cdn);
Saqib14cdb242020-04-16 16:21:35 +0530211
212 if (item.serial_no && item.qty === item.serial_no.split(`\n`).length) {
213 return;
214 }
215
Shreyaa20157a2018-04-13 12:03:42 +0530216 if (item.serial_no && !item.batch_no) {
217 item.serial_no = null;
Anand Doshifc777182013-05-27 19:29:07 +0530218 }
Saqib14cdb242020-04-16 16:21:35 +0530219
Shreyaa20157a2018-04-13 12:03:42 +0530220 var has_batch_no;
221 frappe.db.get_value('Item', {'item_code': item.item_code}, 'has_batch_no', (r) => {
222 has_batch_no = r && r.has_batch_no;
223 if(item.item_code && item.warehouse) {
224 return this.frm.call({
225 method: "erpnext.stock.get_item_details.get_bin_details_and_serial_nos",
226 child: item,
227 args: {
228 item_code: item.item_code,
229 warehouse: item.warehouse,
Rohit Waghchaure6daab3c2019-09-19 17:01:26 +0530230 has_batch_no: has_batch_no || 0,
Shreyaa20157a2018-04-13 12:03:42 +0530231 stock_qty: item.stock_qty,
232 serial_no: item.serial_no || "",
233 },
234 callback:function(r){
235 if (in_list(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
Deepesh Gargfb29ffc2019-06-28 14:08:08 +0530236
237 if (doc.doctype === 'Sales Invoice' && (!doc.update_stock)) return;
238
Shreyaa20157a2018-04-13 12:03:42 +0530239 me.set_batch_number(cdt, cdn);
240 me.batch_no(doc, cdt, cdn);
241 }
242 }
243 });
244 }
245 })
Faris Ansari1fe891b2021-04-23 08:04:00 +0530246 }
Anand Doshibd67e872014-04-11 16:51:27 +0530247
Faris Ansari1fe891b2021-04-23 08:04:00 +0530248 toggle_editable_price_list_rate() {
Nabin Haitdd38a262014-12-26 13:15:21 +0530249 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 +0530250 var editable_price_list_rate = cint(frappe.defaults.get_default("editable_price_list_rate"));
Anand Doshibd67e872014-04-11 16:51:27 +0530251
Anand Doshi33fe8672013-08-02 12:39:10 +0530252 if(df && editable_price_list_rate) {
253 df.read_only = 0;
254 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530255 }
Anand Doshibd67e872014-04-11 16:51:27 +0530256
Faris Ansari1fe891b2021-04-23 08:04:00 +0530257 calculate_commission() {
Anand Doshi923d41d2013-05-28 17:23:36 +0530258 if(this.frm.fields_dict.commission_rate) {
259 if(this.frm.doc.commission_rate > 100) {
Pratik Vyasb52618c2014-04-14 16:25:30 +0530260 var msg = __(frappe.meta.get_label(this.frm.doc.doctype, "commission_rate", this.frm.doc.name)) +
261 " " + __("cannot be greater than 100");
Faris Ansariab74ca72017-05-30 12:54:42 +0530262 frappe.msgprint(msg);
Anand Doshi923d41d2013-05-28 17:23:36 +0530263 throw msg;
264 }
Anand Doshibd67e872014-04-11 16:51:27 +0530265
Nabin Hait5690be12015-02-12 16:09:11 +0530266 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 +0530267 precision("total_commission"));
268 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530269 }
Anand Doshibd67e872014-04-11 16:51:27 +0530270
Faris Ansari1fe891b2021-04-23 08:04:00 +0530271 calculate_contribution() {
Anand Doshi2168e392013-05-23 19:25:08 +0530272 var me = this;
Rushabh Mehtaaa355af2014-03-26 18:24:30 +0530273 $.each(this.frm.doc.doctype.sales_team || [], function(i, sales_person) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530274 frappe.model.round_floats_in(sales_person);
275 if(sales_person.allocated_percentage) {
276 sales_person.allocated_amount = flt(
277 me.frm.doc.base_net_total * sales_person.allocated_percentage / 100.0,
278 precision("allocated_amount", sales_person));
279 }
280 });
Faris Ansari1fe891b2021-04-23 08:04:00 +0530281 }
Anand Doshibd67e872014-04-11 16:51:27 +0530282
Faris Ansari1fe891b2021-04-23 08:04:00 +0530283 calculate_incentive(row) {
deepeshgarg00786152eb2018-09-25 19:08:16 +0530284 if(row.allocated_amount)
285 {
286 row.incentives = flt(
287 row.allocated_amount * row.commission_rate / 100.0,
deepeshgarg007c3772f12018-09-26 15:55:43 +0530288 precision("incentives", row));
deepeshgarg00786152eb2018-09-25 19:08:16 +0530289 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530290 }
deepeshgarg00786152eb2018-09-25 19:08:16 +0530291
Faris Ansari1fe891b2021-04-23 08:04:00 +0530292 batch_no(doc, cdt, cdn) {
Sambhaji Kolate98dbccd2015-03-10 15:04:28 +0530293 var me = this;
294 var item = frappe.get_doc(cdt, cdn);
Saqiba6f98d42020-07-23 18:51:26 +0530295
296 if (item.serial_no) {
297 return;
298 }
299
Shreyaa20157a2018-04-13 12:03:42 +0530300 item.serial_no = null;
301 var has_serial_no;
302 frappe.db.get_value('Item', {'item_code': item.item_code}, 'has_serial_no', (r) => {
303 has_serial_no = r && r.has_serial_no;
304 if(item.warehouse && item.item_code && item.batch_no) {
305 return this.frm.call({
306 method: "erpnext.stock.get_item_details.get_batch_qty_and_serial_no",
307 child: item,
308 args: {
309 "batch_no": item.batch_no,
Maricaf0e87f72019-11-27 15:49:41 +0530310 "stock_qty": item.stock_qty || item.qty, //if stock_qty field is not available fetch qty (in case of Packed Items table)
Shreyaa20157a2018-04-13 12:03:42 +0530311 "warehouse": item.warehouse,
312 "item_code": item.item_code,
313 "has_serial_no": has_serial_no
314 },
315 "fieldname": "actual_batch_qty"
316 });
317 }
318 })
Faris Ansari1fe891b2021-04-23 08:04:00 +0530319 }
Sambhaji Kolate98dbccd2015-03-10 15:04:28 +0530320
Faris Ansari1fe891b2021-04-23 08:04:00 +0530321 set_dynamic_labels() {
322 super.set_dynamic_labels();
Neil Trini Lasradoed8cecb2015-07-07 13:59:23 +0530323 this.set_product_bundle_help(this.frm.doc);
Faris Ansari1fe891b2021-04-23 08:04:00 +0530324 }
Nabin Hait0b157552014-06-24 17:02:45 +0530325
Faris Ansari1fe891b2021-04-23 08:04:00 +0530326 set_product_bundle_help(doc) {
Nabin Hait0b157552014-06-24 17:02:45 +0530327 if(!cur_frm.fields_dict.packing_list) return;
Nabin Haite7d15362014-12-25 16:01:55 +0530328 if ((doc.packed_items || []).length) {
Nabin Hait0b157552014-06-24 17:02:45 +0530329 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(true);
330
Faris Ansariab74ca72017-05-30 12:54:42 +0530331 if (in_list(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
332 var help_msg = "<div class='alert alert-warning'>" +
Neil Trini Lasradoed8cecb2015-07-07 13:59:23 +0530333 __("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 +0530334 "</div>";
Neil Trini Lasradoed8cecb2015-07-07 13:59:23 +0530335 frappe.meta.get_docfield(doc.doctype, 'product_bundle_help', doc.name).options = help_msg;
Nabin Hait0b157552014-06-24 17:02:45 +0530336 }
337 } else {
338 $(cur_frm.fields_dict.packing_list.row.wrapper).toggle(false);
Faris Ansariab74ca72017-05-30 12:54:42 +0530339 if (in_list(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
Neil Trini Lasradoed8cecb2015-07-07 13:59:23 +0530340 frappe.meta.get_docfield(doc.doctype, 'product_bundle_help', doc.name).options = '';
Nabin Hait0b157552014-06-24 17:02:45 +0530341 }
342 }
Neil Trini Lasradoed8cecb2015-07-07 13:59:23 +0530343 refresh_field('product_bundle_help');
Faris Ansari1fe891b2021-04-23 08:04:00 +0530344 }
Anand Doshi01de9452016-01-26 16:22:50 +0530345
Faris Ansari1fe891b2021-04-23 08:04:00 +0530346 company_address() {
Nabin Hait879e1622017-08-21 08:28:55 +0530347 var me = this;
348 if(this.frm.doc.company_address) {
349 frappe.call({
350 method: "frappe.contacts.doctype.address.address.get_address_display",
351 args: {"address_dict": this.frm.doc.company_address },
352 callback: function(r) {
353 if(r.message) {
354 me.frm.set_value("company_address_display", r.message)
355 }
356 }
357 })
358 } else {
359 this.frm.set_value("company_address_display", "");
360 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530361 }
tundebabzyacccdb32017-11-23 08:35:15 +0100362
Faris Ansari1fe891b2021-04-23 08:04:00 +0530363 conversion_factor(doc, cdt, cdn, dont_fetch_price_list_rate) {
364 super.conversion_factor(doc, cdt, cdn, dont_fetch_price_list_rate);
tundebabzyf9b5c742018-01-25 19:21:05 +0100365 if(frappe.meta.get_docfield(cdt, "stock_qty", cdn) &&
366 in_list(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
Deepesh Gargfb29ffc2019-06-28 14:08:08 +0530367 if (doc.doctype === 'Sales Invoice' && (!doc.update_stock)) return;
368 this.set_batch_number(cdt, cdn);
369 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530370 }
tundebabzyacccdb32017-11-23 08:35:15 +0100371
Faris Ansari1fe891b2021-04-23 08:04:00 +0530372 batch_no(doc, cdt, cdn) {
373 super.batch_no(doc, cdt, cdn);
374 }
Deepesh Gargbd9bdc52021-01-28 12:05:57 +0530375
Faris Ansari1fe891b2021-04-23 08:04:00 +0530376 qty(doc, cdt, cdn) {
377 super.qty(doc, cdt, cdn);
Deepesh Gargfb29ffc2019-06-28 14:08:08 +0530378
379 if(in_list(['Delivery Note', 'Sales Invoice'], doc.doctype)) {
380 if (doc.doctype === 'Sales Invoice' && (!doc.update_stock)) return;
381 this.set_batch_number(cdt, cdn);
382 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530383 }
tundebabzyacccdb32017-11-23 08:35:15 +0100384
385 /* Determine appropriate batch number and set it in the form.
386 * @param {string} cdt - Document Doctype
387 * @param {string} cdn - Document name
388 */
Faris Ansari1fe891b2021-04-23 08:04:00 +0530389 set_batch_number(cdt, cdn) {
tundebabzyacccdb32017-11-23 08:35:15 +0100390 const doc = frappe.get_doc(cdt, cdn);
Saqibfaea8542020-03-26 13:18:57 +0530391 if (doc && doc.has_batch_no && doc.warehouse) {
tundebabzyacccdb32017-11-23 08:35:15 +0100392 this._set_batch_number(doc);
393 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530394 }
tundebabzyacccdb32017-11-23 08:35:15 +0100395
Faris Ansari1fe891b2021-04-23 08:04:00 +0530396 _set_batch_number(doc) {
Saqibfaea8542020-03-26 13:18:57 +0530397 let args = {'item_code': doc.item_code, 'warehouse': doc.warehouse, 'qty': flt(doc.qty) * flt(doc.conversion_factor)};
398 if (doc.has_serial_no && doc.serial_no) {
399 args['serial_no'] = doc.serial_no
400 }
marination8a30cb22020-04-23 00:18:21 +0530401
tundebabzyacccdb32017-11-23 08:35:15 +0100402 return frappe.call({
403 method: 'erpnext.stock.doctype.batch.batch.get_batch_no',
Saqibfaea8542020-03-26 13:18:57 +0530404 args: args,
tundebabzyacccdb32017-11-23 08:35:15 +0100405 callback: function(r) {
406 if(r.message) {
407 frappe.model.set_value(doc.doctype, doc.name, 'batch_no', r.message);
tundebabzy8ae53ca2017-11-30 07:07:21 +0100408 } else {
409 frappe.model.set_value(doc.doctype, doc.name, 'batch_no', r.message);
tundebabzyacccdb32017-11-23 08:35:15 +0100410 }
411 }
412 });
Faris Ansari1fe891b2021-04-23 08:04:00 +0530413 }
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200414
Faris Ansari1fe891b2021-04-23 08:04:00 +0530415 update_auto_repeat_reference(doc) {
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200416 if (doc.auto_repeat) {
417 frappe.call({
Rucha Mahabal65a627c2019-07-17 13:50:32 +0530418 method:"frappe.automation.doctype.auto_repeat.auto_repeat.update_reference",
deepeshgarg007d9a768a2018-12-29 19:33:44 +0530419 args:{
Charles-Henri Decultot8b223102018-06-28 10:57:58 +0200420 docname: doc.auto_repeat,
421 reference:doc.name
422 },
423 callback: function(r){
424 if (r.message=="success") {
425 frappe.show_alert({message:__("Auto repeat document updated"), indicator:'green'});
426 } else {
427 frappe.show_alert({message:__("An error occurred during the update process"), indicator:'red'});
428 }
429 }
430 })
431 }
432 }
Faris Ansari1fe891b2021-04-23 08:04:00 +0530433};
Anand Doshi43f087c2014-08-26 14:25:53 +0530434
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +0530435frappe.ui.form.on(cur_frm.doctype,"project", function(frm) {
Nabin Hait10b155a2014-10-23 13:18:59 +0530436 if(in_list(["Delivery Note", "Sales Invoice"], frm.doc.doctype)) {
Rushabh Mehta532eedf2016-05-19 18:22:49 +0530437 if(frm.doc.project) {
438 frappe.call({
439 method:'erpnext.projects.doctype.project.project.get_cost_center_name' ,
440 args: { project: frm.doc.project },
441 callback: function(r, rt) {
442 if(!r.exc) {
443 $.each(frm.doc["items"] || [], function(i, row) {
444 if(r.message) {
445 frappe.model.set_value(row.doctype, row.name, "cost_center", r.message);
Mohammad Hasnain Mohsin Rajane15b6a92020-11-25 15:36:41 +0530446 frappe.msgprint(__("Cost Center For Item with Item Code {0} has been Changed to {1}", [row.item_name, r.message]));
Rushabh Mehta532eedf2016-05-19 18:22:49 +0530447 }
448 })
449 }
Nabin Hait10b155a2014-10-23 13:18:59 +0530450 }
Rushabh Mehta532eedf2016-05-19 18:22:49 +0530451 })
452 }
Nabin Hait10b155a2014-10-23 13:18:59 +0530453 }
Nabin Haite45ec662018-08-01 17:44:34 +0530454})
deepeshgarg007d9a768a2018-12-29 19:33:44 +0530455
456frappe.ui.form.on(cur_frm.doctype, {
457 set_as_lost_dialog: function(frm) {
458 var dialog = new frappe.ui.Dialog({
459 title: __("Set as Lost"),
460 fields: [
Anupam Kumare447c792020-08-13 09:20:36 +0530461 {
462 "fieldtype": "Table MultiSelect",
463 "label": __("Lost Reasons"),
464 "fieldname": "lost_reason",
465 "options": frm.doctype === 'Opportunity' ? 'Opportunity Lost Reason Detail': 'Quotation Lost Reason Detail',
466 "reqd": 1
467 },
468 {
469 "fieldtype": "Text",
470 "label": __("Detailed Reason"),
471 "fieldname": "detailed_reason"
472 },
deepeshgarg007d9a768a2018-12-29 19:33:44 +0530473 ],
474 primary_action: function() {
475 var values = dialog.get_values();
476 var reasons = values["lost_reason"];
477 var detailed_reason = values["detailed_reason"];
478
479 frm.call({
480 doc: frm.doc,
481 method: 'declare_enquiry_lost',
482 args: {
483 'lost_reasons_list': reasons,
484 'detailed_reason': detailed_reason
485 },
486 callback: function(r) {
487 dialog.hide();
488 frm.reload_doc();
489 },
490 });
491 refresh_field("lost_reason");
492 },
493 primary_action_label: __('Declare Lost')
494 });
495
496 dialog.show();
497 }
498})