blob: 1747932af656390049bada6a7bca4e8d607eaa6d [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
Anand Doshi3543f302013-05-24 19:25:01 +05303
Nabin Haitb4a3dfa2015-01-22 17:13:13 +05304erpnext.TransactionController = erpnext.taxes_and_totals.extend({
Rushabh Mehtabe2ee182016-04-29 17:22:42 +05305 setup: function() {
Rushabh Mehta2cb7a9f2016-06-15 16:45:03 +05306 this._super();
Saqibfaea8542020-03-26 13:18:57 +05307 frappe.flags.hide_serial_batch_dialog = true;
Rushabh Mehtabe2ee182016-04-29 17:22:42 +05308 frappe.ui.form.on(this.frm.doctype + " Item", "rate", function(frm, cdt, cdn) {
9 var item = frappe.get_doc(cdt, cdn);
Rushabh Mehtaf69ffeb2017-05-17 19:40:40 +053010 var has_margin_field = frappe.meta.has_field(cdt, 'margin_type');
11
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053012 frappe.model.round_floats_in(item, ["rate", "price_list_rate"]);
13
14 if(item.price_list_rate) {
Rushabh Mehtaf69ffeb2017-05-17 19:40:40 +053015 if(item.rate > item.price_list_rate && has_margin_field) {
16 // if rate is greater than price_list_rate, set margin
17 // or set discount
18 item.discount_percentage = 0;
Nabin Haite72c98d2017-05-24 09:01:44 +053019 item.margin_type = 'Amount';
Rushabh Mehta36311022017-06-20 09:30:53 +053020 item.margin_rate_or_amount = flt(item.rate - item.price_list_rate,
Nabin Haite72c98d2017-05-24 09:01:44 +053021 precision("margin_rate_or_amount", item));
22 item.rate_with_margin = item.rate;
Rushabh Mehtaf69ffeb2017-05-17 19:40:40 +053023 } else {
Rushabh Mehta36311022017-06-20 09:30:53 +053024 item.discount_percentage = flt((1 - item.rate / item.price_list_rate) * 100.0,
Rushabh Mehtaf69ffeb2017-05-17 19:40:40 +053025 precision("discount_percentage", item));
Nabin Hait593242f2019-04-05 19:35:02 +053026 item.discount_amount = flt(item.price_list_rate) - flt(item.rate);
Rushabh Mehtaf69ffeb2017-05-17 19:40:40 +053027 item.margin_type = '';
28 item.margin_rate_or_amount = 0;
29 item.rate_with_margin = 0;
30 }
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053031 } else {
32 item.discount_percentage = 0.0;
Rushabh Mehtaf69ffeb2017-05-17 19:40:40 +053033 item.margin_type = '';
34 item.margin_rate_or_amount = 0;
35 item.rate_with_margin = 0;
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053036 }
Saif Ur Rehman147fa732018-11-26 18:14:06 +050037 item.base_rate_with_margin = item.rate_with_margin * flt(frm.doc.conversion_rate);
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053038
39 cur_frm.cscript.set_gross_profit(item);
40 cur_frm.cscript.calculate_taxes_and_totals();
Vishal Dhayaguded42242d2017-11-29 16:09:59 +053041
Rushabh Mehta30dc9a12017-11-17 14:31:09 +053042 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053043
Vishal Dhayaguded42242d2017-11-29 16:09:59 +053044
45
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053046 frappe.ui.form.on(this.frm.cscript.tax_table, "rate", function(frm, cdt, cdn) {
47 cur_frm.cscript.calculate_taxes_and_totals();
Rushabh Mehta764aa922016-05-02 13:28:46 +053048 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053049
50 frappe.ui.form.on(this.frm.cscript.tax_table, "tax_amount", function(frm, cdt, cdn) {
51 cur_frm.cscript.calculate_taxes_and_totals();
Rushabh Mehta764aa922016-05-02 13:28:46 +053052 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053053
54 frappe.ui.form.on(this.frm.cscript.tax_table, "row_id", function(frm, cdt, cdn) {
55 cur_frm.cscript.calculate_taxes_and_totals();
Rushabh Mehta764aa922016-05-02 13:28:46 +053056 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053057
58 frappe.ui.form.on(this.frm.cscript.tax_table, "included_in_print_rate", function(frm, cdt, cdn) {
59 cur_frm.cscript.set_dynamic_labels();
60 cur_frm.cscript.calculate_taxes_and_totals();
Rushabh Mehta764aa922016-05-02 13:28:46 +053061 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053062
63 frappe.ui.form.on(this.frm.doctype, "apply_discount_on", function(frm) {
64 if(frm.doc.additional_discount_percentage) {
65 frm.trigger("additional_discount_percentage");
66 } else {
67 cur_frm.cscript.calculate_taxes_and_totals();
68 }
Rushabh Mehta764aa922016-05-02 13:28:46 +053069 });
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053070
71 frappe.ui.form.on(this.frm.doctype, "additional_discount_percentage", function(frm) {
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053072 if(!frm.doc.apply_discount_on) {
73 frappe.msgprint(__("Please set 'Apply Additional Discount On'"));
Rushabh Mehta233a19a2017-07-07 13:05:11 +053074 return;
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053075 }
76
77 frm.via_discount_percentage = true;
78
79 if(frm.doc.additional_discount_percentage && frm.doc.discount_amount) {
80 // Reset discount amount and net / grand total
Rushabh Mehta6705ab32017-07-07 13:14:08 +053081 frm.doc.discount_amount = 0;
82 frm.cscript.calculate_taxes_and_totals();
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053083 }
84
85 var total = flt(frm.doc[frappe.model.scrub(frm.doc.apply_discount_on)]);
86 var discount_amount = flt(total*flt(frm.doc.additional_discount_percentage) / 100,
87 precision("discount_amount"));
88
Rushabh Mehta233a19a2017-07-07 13:05:11 +053089 frm.set_value("discount_amount", discount_amount)
90 .then(() => delete frm.via_discount_percentage);
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053091 });
92
93 frappe.ui.form.on(this.frm.doctype, "discount_amount", function(frm) {
94 frm.cscript.set_dynamic_labels();
95
96 if (!frm.via_discount_percentage) {
Rushabh Mehta233a19a2017-07-07 13:05:11 +053097 frm.doc.additional_discount_percentage = 0;
Rushabh Mehtabe2ee182016-04-29 17:22:42 +053098 }
99
100 frm.cscript.calculate_taxes_and_totals();
101 });
Makarand Bauskarc196d742017-06-02 17:28:40 +0530102
Saif60ec88b2018-11-13 12:07:53 +0500103 frappe.ui.form.on(this.frm.doctype + " Item", {
104 items_add: function(frm, cdt, cdn) {
105 var item = frappe.get_doc(cdt, cdn);
106 if(!item.warehouse && frm.doc.set_warehouse) {
107 item.warehouse = frm.doc.set_warehouse;
108 }
109 }
110 });
111
Makarand Bauskarc196d742017-06-02 17:28:40 +0530112 var me = this;
113 if(this.frm.fields_dict["items"].grid.get_field('batch_no')) {
114 this.frm.set_query("batch_no", "items", function(doc, cdt, cdn) {
Rushabh Mehta30dc9a12017-11-17 14:31:09 +0530115 return me.set_query_for_batch(doc, cdt, cdn);
Makarand Bauskarc196d742017-06-02 17:28:40 +0530116 });
117 }
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530118
tundebabzy590bd0a2017-12-25 07:10:19 +0100119 if(
Faris Ansari404f39d2018-05-14 15:46:45 +0530120 this.frm.docstatus < 2
Manas Solankia7f55892018-04-02 10:32:00 +0530121 && this.frm.fields_dict["payment_terms_template"]
tundebabzy590bd0a2017-12-25 07:10:19 +0100122 && this.frm.fields_dict["payment_schedule"]
123 && this.frm.doc.payment_terms_template
124 && !this.frm.doc.payment_schedule.length
125 ){
tundedf3a1752017-09-11 11:02:57 +0100126 this.frm.trigger("payment_terms_template");
127 }
128
Nabin Haitdd38a262014-12-26 13:15:21 +0530129 if(this.frm.fields_dict["taxes"]) {
130 this["taxes_remove"] = this.calculate_taxes_and_totals;
Anand Doshia91c95f2013-08-23 13:00:47 +0530131 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530132
Nabin Haitdd38a262014-12-26 13:15:21 +0530133 if(this.frm.fields_dict["items"]) {
Vishal Dhayaguded3770182017-12-14 18:06:02 +0530134 this["items_remove"] = this.calculate_net_weight;
Anand Doshi1e4fb682013-08-23 12:56:33 +0530135 }
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530136
Neil Trini Lasradoe2d8dd02015-06-22 19:06:15 +0530137 if(this.frm.fields_dict["recurring_print_format"]) {
138 this.frm.set_query("recurring_print_format", function(doc) {
139 return{
140 filters: [
141 ['Print Format', 'doc_type', '=', cur_frm.doctype],
142 ]
Rushabh Mehta30dc9a12017-11-17 14:31:09 +0530143 };
Neil Trini Lasradoe2d8dd02015-06-22 19:06:15 +0530144 });
145 }
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530146
Nabin Hait1d218422015-07-17 15:19:02 +0530147 if(this.frm.fields_dict["return_against"]) {
148 this.frm.set_query("return_against", function(doc) {
149 var filters = {
150 "docstatus": 1,
151 "is_return": 0,
152 "company": doc.company
153 };
154 if (me.frm.fields_dict["customer"] && doc.customer) filters["customer"] = doc.customer;
155 if (me.frm.fields_dict["supplier"] && doc.supplier) filters["supplier"] = doc.supplier;
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530156
Nabin Hait1d218422015-07-17 15:19:02 +0530157 return {
158 filters: filters
Nabin Hait7b6eaee2017-10-03 01:09:46 +0530159 };
Nabin Hait1d218422015-07-17 15:19:02 +0530160 });
161 }
Rohit Waghchauree5405342019-03-31 16:42:41 +0530162
163 if(frappe.meta.get_docfield(this.frm.doc.doctype, "pricing_rules")) {
Rohit Waghchaure70b996a2019-03-31 20:33:15 +0530164 this.frm.set_indicator_formatter('pricing_rule', function(doc) {
165 return (doc.rule_applied) ? "green" : "red";
166 });
Rohit Waghchauree5405342019-03-31 16:42:41 +0530167 }
Nabin Hait7b6eaee2017-10-03 01:09:46 +0530168 },
169 onload: function() {
170 var me = this;
Rushabh Mehta6b537922017-03-14 16:59:11 +0530171
Nabin Hait7b6eaee2017-10-03 01:09:46 +0530172 if(this.frm.doc.__islocal) {
173 var currency = frappe.defaults.get_user_default("currency");
174
175 let set_value = (fieldname, value) => {
176 if(me.frm.fields_dict[fieldname] && !me.frm.doc[fieldname]) {
177 return me.frm.set_value(fieldname, value);
178 }
179 };
180
181 return frappe.run_serially([
182 () => set_value('currency', currency),
183 () => set_value('price_list_currency', currency),
184 () => set_value('status', 'Draft'),
185 () => set_value('is_subcontracted', 'No'),
186 () => {
187 if(this.frm.doc.company && !this.frm.doc.amended_from) {
188 this.frm.trigger("company");
189 }
190 }
191 ]);
192 }
Manas Solankic2cd3fd2016-11-14 21:57:05 +0530193 },
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530194
Rohit Waghchaureb4a59932018-11-26 20:02:01 +0530195 is_return: function() {
196 if(!this.frm.doc.is_return && this.frm.doc.return_against) {
197 this.frm.set_value('return_against', '');
198 }
199 },
200
Nabin Hait8af429d2016-11-16 17:21:59 +0530201 setup_quality_inspection: function() {
202 if(!in_list(["Delivery Note", "Sales Invoice", "Purchase Receipt", "Purchase Invoice"], this.frm.doc.doctype)) {
203 return;
204 }
Manas Solankic2cd3fd2016-11-14 21:57:05 +0530205 var me = this;
Rushabh Mehta6b537922017-03-14 16:59:11 +0530206 var inspection_type = in_list(["Purchase Receipt", "Purchase Invoice"], this.frm.doc.doctype)
Nabin Hait8af429d2016-11-16 17:21:59 +0530207 ? "Incoming" : "Outgoing";
Rushabh Mehta6b537922017-03-14 16:59:11 +0530208
Manas Solankia8d1bc72016-11-15 14:48:23 +0530209 var quality_inspection_field = this.frm.get_docfield("items", "quality_inspection");
Nabin Hait8af429d2016-11-16 17:21:59 +0530210 quality_inspection_field.get_route_options_for_new_doc = function(row) {
Manas Solankic2cd3fd2016-11-14 21:57:05 +0530211 if(me.frm.is_new()) return;
Manas Solankic2cd3fd2016-11-14 21:57:05 +0530212 return {
213 "inspection_type": inspection_type,
Manas Solanki34feab12016-11-16 14:36:28 +0530214 "reference_type": me.frm.doc.doctype,
Manas Solankidbc25112016-11-16 15:06:08 +0530215 "reference_name": me.frm.doc.name,
Nabin Hait8af429d2016-11-16 17:21:59 +0530216 "item_code": row.doc.item_code,
217 "description": row.doc.description,
218 "item_serial_no": row.doc.serial_no ? row.doc.serial_no.split("\n")[0] : null,
219 "batch_no": row.doc.batch_no
Manas Solankic2cd3fd2016-11-14 21:57:05 +0530220 }
221 }
Rushabh Mehta6b537922017-03-14 16:59:11 +0530222
Manas Solankic2cd3fd2016-11-14 21:57:05 +0530223 this.frm.set_query("quality_inspection", "items", function(doc, cdt, cdn) {
224 var d = locals[cdt][cdn];
225 return {
226 filters: {
227 docstatus: 1,
228 inspection_type: inspection_type,
rohitwaghchaure4e8fdf72018-02-22 11:03:48 +0530229 reference_name: doc.name,
Manas Solankic2cd3fd2016-11-14 21:57:05 +0530230 item_code: d.item_code
231 }
232 }
233 });
Anand Doshi3543f302013-05-24 19:25:01 +0530234 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530235
rohitwaghchaure713cfc72018-09-11 17:40:37 +0530236 make_payment_request: function() {
Rohit Waghchaure210f4982018-10-03 15:55:51 +0530237 var me = this;
238 const payment_request_type = (in_list(['Sales Order', 'Sales Invoice'], this.frm.doc.doctype))
rohitwaghchaure713cfc72018-09-11 17:40:37 +0530239 ? "Inward" : "Outward";
240
241 frappe.call({
242 method:"erpnext.accounts.doctype.payment_request.payment_request.make_payment_request",
243 args: {
244 dt: me.frm.doc.doctype,
245 dn: me.frm.doc.name,
246 recipient_id: me.frm.doc.contact_email,
247 payment_request_type: payment_request_type,
248 party_type: payment_request_type == 'Outward' ? "Supplier" : "Customer",
249 party: payment_request_type == 'Outward' ? me.frm.doc.supplier : me.frm.doc.customer
250 },
251 callback: function(r) {
252 if(!r.exc){
253 var doc = frappe.model.sync(r.message);
254 frappe.set_route("Form", r.message.doctype, r.message.name);
255 }
256 }
257 })
258 },
259
Anand Doshi5e4e5d72013-08-06 17:23:44 +0530260 onload_post_render: function() {
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530261 if(this.frm.doc.__islocal && !(this.frm.doc.taxes || []).length
262 && !(this.frm.doc.__onload ? this.frm.doc.__onload.load_after_mapping : false)) {
Nabin Hait7b6eaee2017-10-03 01:09:46 +0530263 frappe.after_ajax(() => this.apply_default_taxes());
Anand Doshi70f57eb2015-11-27 14:37:40 +0530264 } else if(this.frm.doc.__islocal && this.frm.doc.company && this.frm.doc["items"]
Nabin Haitbdfb0702015-10-28 15:38:08 +0530265 && !this.frm.doc.is_pos) {
Nabin Hait7b6eaee2017-10-03 01:09:46 +0530266 frappe.after_ajax(() => this.calculate_taxes_and_totals());
Anand Doshi5e4e5d72013-08-06 17:23:44 +0530267 }
Nabin Haitdd38a262014-12-26 13:15:21 +0530268 if(frappe.meta.get_docfield(this.frm.doc.doctype + " Item", "item_code")) {
Rushabh Mehta203cc962016-04-07 15:25:43 +0530269 this.setup_item_selector();
Nabin Hait3fccfaa2019-05-16 14:48:10 +0530270 this.frm.get_field("items").grid.set_multiple_add("item_code", "qty");
Rushabh Mehtab6843f22014-06-04 13:10:41 +0530271 }
Anand Doshi5e4e5d72013-08-06 17:23:44 +0530272 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530273
Anand Doshi3543f302013-05-24 19:25:01 +0530274 refresh: function() {
Anand Doshi8bde7f92014-04-24 18:11:49 +0530275 erpnext.toggle_naming_series();
Anand Doshibdee6e02013-07-09 13:03:39 +0530276 erpnext.hide_company();
Nabin Hait1f996c32013-07-29 19:00:53 +0530277 this.set_dynamic_labels();
Rushabh Mehta614e7ab2015-02-18 19:51:48 +0530278 this.setup_sms();
Rohit Waghchauref2705672018-06-12 12:20:52 +0530279 this.setup_quality_inspection();
Faris Ansari8beca682019-09-16 15:20:10 +0530280 let scan_barcode_field = this.frm.get_field('scan_barcode');
281 if (scan_barcode_field) {
282 scan_barcode_field.set_value("");
283 scan_barcode_field.set_new_description("");
284
285 if (frappe.is_mobile()) {
286 if (scan_barcode_field.$input_wrapper.find('.input-group').length) return;
287
288 let $input_group = $('<div class="input-group">');
289 scan_barcode_field.$input_wrapper.find('.control-input').append($input_group);
290 $input_group.append(scan_barcode_field.$input);
291 $(`<span class="input-group-btn" style="vertical-align: top">
292 <button class="btn btn-default border" type="button">
293 <i class="fa fa-camera text-muted"></i>
294 </button>
295 </span>`)
296 .on('click', '.btn', () => {
297 frappe.barcode.scan_barcode().then(barcode => {
298 scan_barcode_field.set_value(barcode);
299 });
300 })
301 .appendTo($input_group);
302 }
303 }
Zarrar69224152018-11-13 11:11:32 +0530304 },
305
306 scan_barcode: function() {
307 let scan_barcode_field = this.frm.fields_dict["scan_barcode"];
308
Faris Ansari5631ffc2019-02-12 17:12:48 +0530309 let show_description = function(idx, exist = null) {
310 if (exist) {
311 scan_barcode_field.set_new_description(__('Row #{0}: Qty increased by 1', [idx]));
Zarrar69224152018-11-13 11:11:32 +0530312 } else {
Faris Ansari5631ffc2019-02-12 17:12:48 +0530313 scan_barcode_field.set_new_description(__('Row #{0}: Item added', [idx]));
Zarrar69224152018-11-13 11:11:32 +0530314 }
315 }
316
317 if(this.frm.doc.scan_barcode) {
318 frappe.call({
319 method: "erpnext.selling.page.point_of_sale.point_of_sale.search_serial_or_batch_or_barcode_number",
320 args: { search_value: this.frm.doc.scan_barcode }
321 }).then(r => {
Faris Ansari5631ffc2019-02-12 17:12:48 +0530322 const data = r && r.message;
Nabin Hait34c551d2019-07-03 10:34:31 +0530323 if (!data || Object.keys(data).length === 0) {
Faris Ansari5631ffc2019-02-12 17:12:48 +0530324 scan_barcode_field.set_new_description(__('Cannot find Item with this barcode'));
325 return;
Zarrar69224152018-11-13 11:11:32 +0530326 }
Faris Ansari5631ffc2019-02-12 17:12:48 +0530327
328 let cur_grid = this.frm.fields_dict.items.grid;
329
330 let row_to_modify = null;
331 const existing_item_row = this.frm.doc.items.find(d => d.item_code === data.item_code);
332 const blank_item_row = this.frm.doc.items.find(d => !d.item_code);
333
334 if (existing_item_row) {
335 row_to_modify = existing_item_row;
336 } else if (blank_item_row) {
337 row_to_modify = blank_item_row;
Zarrar69224152018-11-13 11:11:32 +0530338 }
Faris Ansari5631ffc2019-02-12 17:12:48 +0530339
340 if (!row_to_modify) {
341 // add new row
342 row_to_modify = frappe.model.add_child(this.frm.doc, cur_grid.doctype, 'items');
343 }
344
345 show_description(row_to_modify.idx, row_to_modify.item_code);
346
Rohit Waghchaureab4ff982019-02-28 13:21:28 +0530347 this.frm.from_barcode = true;
Faris Ansari5631ffc2019-02-12 17:12:48 +0530348 frappe.model.set_value(row_to_modify.doctype, row_to_modify.name, {
349 item_code: data.item_code,
350 qty: (row_to_modify.qty || 0) + 1
351 });
352
Rohit Waghchaureab4ff982019-02-28 13:21:28 +0530353 ['serial_no', 'batch_no', 'barcode'].forEach(field => {
354 if (data[field] && frappe.meta.has_field(row_to_modify.doctype, field)) {
355 frappe.model.set_value(row_to_modify.doctype,
356 row_to_modify.name, field, data[field]);
357 }
358 });
359
360 scan_barcode_field.set_value('');
Zarrar69224152018-11-13 11:11:32 +0530361 });
Zarrar69224152018-11-13 11:11:32 +0530362 }
363 return false;
Rushabh Mehta614e7ab2015-02-18 19:51:48 +0530364 },
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530365
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530366 apply_default_taxes: function() {
367 var me = this;
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530368 var taxes_and_charges_field = frappe.meta.get_docfield(me.frm.doc.doctype, "taxes_and_charges",
369 me.frm.doc.name);
370
rohitwaghchaure1f39feb2018-01-05 12:25:45 +0530371 if (!this.frm.doc.taxes_and_charges && this.frm.doc.taxes) {
372 return;
373 }
374
375 if (taxes_and_charges_field) {
Nabin Haitbdfb0702015-10-28 15:38:08 +0530376 return frappe.call({
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530377 method: "erpnext.controllers.accounts_controller.get_default_taxes_and_charges",
378 args: {
rohitwaghchaure505661b2017-12-11 14:52:28 +0530379 "master_doctype": taxes_and_charges_field.options,
Nabin Haita2426fc2018-01-15 17:45:46 +0530380 "tax_template": me.frm.doc.taxes_and_charges,
rohitwaghchaure505661b2017-12-11 14:52:28 +0530381 "company": me.frm.doc.company
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530382 },
383 callback: function(r) {
Nabin Haita2426fc2018-01-15 17:45:46 +0530384 if(!r.exc && r.message) {
Rushabh Mehta98aa5812017-11-14 09:32:32 +0530385 frappe.run_serially([
386 () => {
387 // directly set in doc, so as not to call triggers
rohitwaghchaure1bc47fa2017-12-16 10:49:58 +0530388 if(r.message.taxes_and_charges) {
389 me.frm.doc.taxes_and_charges = r.message.taxes_and_charges;
390 }
Rushabh Mehta98aa5812017-11-14 09:32:32 +0530391
392 // set taxes table
rohitwaghchaure1bc47fa2017-12-16 10:49:58 +0530393 if(r.message.taxes) {
394 me.frm.set_value("taxes", r.message.taxes);
395 }
Rushabh Mehta98aa5812017-11-14 09:32:32 +0530396 },
Saif3c9155e2018-10-18 17:27:56 +0500397 () => me.set_dynamic_labels(),
Rushabh Mehta98aa5812017-11-14 09:32:32 +0530398 () => me.calculate_taxes_and_totals()
399 ]);
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530400 }
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530401 }
Rushabh Mehta4984bcc2015-05-21 12:51:07 +0530402 });
403 }
Nabin Haitffc7f3f2015-05-12 15:07:02 +0530404 },
Rushabh Mehta614e7ab2015-02-18 19:51:48 +0530405
406 setup_sms: function() {
407 var me = this;
Faris Ansaria5d975d2019-07-04 17:47:28 +0530408 let blacklist = ['Purchase Invoice', 'BOM'];
Nabin Hait7132bd32016-04-13 15:19:37 +0530409 if(this.frm.doc.docstatus===1 && !in_list(["Lost", "Stopped", "Closed"], this.frm.doc.status)
Faris Ansaria5d975d2019-07-04 17:47:28 +0530410 && !blacklist.includes(this.frm.doctype)) {
Rushabh Mehta614e7ab2015-02-18 19:51:48 +0530411 this.frm.page.add_menu_item(__('Send SMS'), function() { me.send_sms(); });
412 }
413 },
414
415 send_sms: function() {
Faris Ansariab74ca72017-05-30 12:54:42 +0530416 var sms_man = new erpnext.SMSManager(this.frm.doc);
Nabin Hait239c2c32015-01-15 14:21:41 +0530417 },
Anand Doshi50d7e8c2015-01-06 17:14:13 +0530418
Neil Trini Lasradod0151952015-06-09 15:21:50 +0530419 barcode: function(doc, cdt, cdn) {
420 var d = locals[cdt][cdn];
421 if(d.barcode=="" || d.barcode==null) {
422 // barcode cleared, remove item
423 d.item_code = "";
424 }
Rohit Waghchaureab4ff982019-02-28 13:21:28 +0530425
426 this.frm.from_barcode = true;
427 this.item_code(doc, cdt, cdn);
Neil Trini Lasradod0151952015-06-09 15:21:50 +0530428 },
429
Rohit Waghchaureab4ff982019-02-28 13:21:28 +0530430 item_code: function(doc, cdt, cdn) {
Nabin Hait139dc7b2014-02-12 14:53:18 +0530431 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530432 var item = frappe.get_doc(cdt, cdn);
pratu16x75f389c92017-06-26 13:38:24 +0530433 var update_stock = 0, show_batch_dialog = 0;
rohitwaghchaure5999ade2017-08-09 17:24:13 +0530434 if(['Sales Invoice'].includes(this.frm.doc.doctype)) {
pratu16x7577e4c42017-06-23 11:39:03 +0530435 update_stock = cint(me.frm.doc.update_stock);
436 show_batch_dialog = update_stock;
pratu16x7ae450fc2017-06-26 15:31:46 +0530437
438 } else if((this.frm.doc.doctype === 'Purchase Receipt' && me.frm.doc.is_return) ||
439 this.frm.doc.doctype === 'Delivery Note') {
pratu16x789d001c2017-06-26 17:45:49 +0530440 show_batch_dialog = 1;
pratu16x7577e4c42017-06-23 11:39:03 +0530441 }
Rushabh Mehta675f9c62016-06-02 16:34:00 +0530442 // clear barcode if setting item (else barcode will take priority)
Rohit Waghchaureab4ff982019-02-28 13:21:28 +0530443 if(!this.frm.from_barcode) {
Rushabh Mehta675f9c62016-06-02 16:34:00 +0530444 item.barcode = null;
445 }
Rohit Waghchaureab4ff982019-02-28 13:21:28 +0530446
447 this.frm.from_barcode = false;
Nabin Hait139dc7b2014-02-12 14:53:18 +0530448 if(item.item_code || item.barcode || item.serial_no) {
449 if(!this.validate_company_and_party()) {
pratu16x7577e4c42017-06-23 11:39:03 +0530450 this.frm.fields_dict["items"].grid.grid_rows[item.idx - 1].remove();
Nabin Hait139dc7b2014-02-12 14:53:18 +0530451 } else {
452 return this.frm.call({
453 method: "erpnext.stock.get_item_details.get_item_details",
454 child: item,
455 args: {
Rohit Waghchauree5405342019-03-31 16:42:41 +0530456 doc: me.frm.doc,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530457 args: {
458 item_code: item.item_code,
459 barcode: item.barcode,
460 serial_no: item.serial_no,
Saif Ur Rehman2689dea2019-01-09 13:43:06 +0500461 set_warehouse: me.frm.doc.set_warehouse,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530462 warehouse: item.warehouse,
Nabin Hait34c551d2019-07-03 10:34:31 +0530463 customer: me.frm.doc.customer || me.frm.doc.party_name,
464 quotation_to: me.frm.doc.quotation_to,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530465 supplier: me.frm.doc.supplier,
466 currency: me.frm.doc.currency,
pratu16x7577e4c42017-06-23 11:39:03 +0530467 update_stock: update_stock,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530468 conversion_rate: me.frm.doc.conversion_rate,
Faris Ansariab74ca72017-05-30 12:54:42 +0530469 price_list: me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
Nabin Hait139dc7b2014-02-12 14:53:18 +0530470 price_list_currency: me.frm.doc.price_list_currency,
471 plc_conversion_rate: me.frm.doc.plc_conversion_rate,
472 company: me.frm.doc.company,
473 order_type: me.frm.doc.order_type,
474 is_pos: cint(me.frm.doc.is_pos),
475 is_subcontracted: me.frm.doc.is_subcontracted,
Nabin Haita1bf43b2015-03-17 10:50:47 +0530476 transaction_date: me.frm.doc.transaction_date || me.frm.doc.posting_date,
Nabin Hait444f9562014-06-20 15:59:49 +0530477 ignore_pricing_rule: me.frm.doc.ignore_pricing_rule,
Anand Doshiafe85bd2016-01-21 21:04:16 +0530478 doctype: me.frm.doc.doctype,
479 name: me.frm.doc.name,
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +0530480 project: item.project || me.frm.doc.project,
mbauskarba412422017-04-19 12:35:51 +0530481 qty: item.qty || 1,
Rohit Waghchauredc981dc2017-04-03 14:17:08 +0530482 stock_qty: item.stock_qty,
rohitwaghchaurec037dc72017-11-28 16:11:15 +0530483 conversion_factor: item.conversion_factor,
Vishal Dhayaguded42242d2017-11-29 16:09:59 +0530484 weight_per_unit: item.weight_per_unit,
485 weight_uom: item.weight_uom,
Nabin Haite45ec662018-08-01 17:44:34 +0530486 uom : item.uom,
Rohit Waghchaure3cf24362019-06-02 16:03:05 +0530487 manufacturer: item.manufacturer,
Nabin Hait158e7dc2019-01-04 11:04:37 +0530488 stock_uom: item.stock_uom,
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400489 pos_profile: me.frm.doc.doctype == 'Sales Invoice' ? me.frm.doc.pos_profile : '',
Saif Ur Rehman67786682018-12-27 02:11:07 +0500490 cost_center: item.cost_center,
491 tax_category: me.frm.doc.tax_category,
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530492 item_tax_template: item.item_tax_template,
Deepesh Garg15ff6a52020-02-18 12:28:41 +0530493 child_docname: item.name
Nabin Hait139dc7b2014-02-12 14:53:18 +0530494 }
495 },
Anand Doshi13945092014-09-21 19:45:49 +0530496
Nabin Hait139dc7b2014-02-12 14:53:18 +0530497 callback: function(r) {
498 if(!r.exc) {
Vishal Dhayaguded3770182017-12-14 18:06:02 +0530499 frappe.run_serially([
Saif Ur Rehmanaca374e2018-12-28 04:53:25 +0500500 () => {
501 var d = locals[cdt][cdn];
Saif Ur Rehmanfd531a62018-12-29 01:49:11 +0500502 me.add_taxes_from_item_tax_template(d.item_tax_rate);
Rohit Waghchaure2ae79b82019-12-18 17:48:39 +0530503 if (d.free_item_data) {
504 me.apply_product_discount(d.free_item_data);
505 }
Saif Ur Rehmanaca374e2018-12-28 04:53:25 +0500506 },
Deepesh Garg15ff6a52020-02-18 12:28:41 +0530507 () => {
508 // for internal customer instead of pricing rule directly apply valuation rate on item
509 if (me.frm.doc.is_internal_customer || me.frm.doc.is_internal_supplier) {
510 me.get_incoming_rate(item, me.frm.posting_date, me.frm.posting_time,
511 me.frm.doc.doctype, me.frm.doc.company);
512 } else {
513 me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
514 }
515 },
516 () => {
517 if (me.frm.doc.is_internal_customer || me.frm.doc.is_internal_supplier) {
518 me.calculate_taxes_and_totals();
519 }
520 },
Vishal Dhayaguded3770182017-12-14 18:06:02 +0530521 () => me.toggle_conversion_factor(item),
522 () => {
Saqibfaea8542020-03-26 13:18:57 +0530523 if (show_batch_dialog)
524 return frappe.db.get_value("Item", item.item_code, ["has_batch_no", "has_serial_no"])
525 .then((r) => {
526 if(r.message.has_batch_no || r.message.has_serial_no) {
527 frappe.flags.hide_serial_batch_dialog = false;
528 }
529 });
530 },
531 () => {
Vishal Dhayaguded3770182017-12-14 18:06:02 +0530532 if(show_batch_dialog && !frappe.flags.hide_serial_batch_dialog) {
533 var d = locals[cdt][cdn];
534 $.each(r.message, function(k, v) {
535 if(!d[k]) d[k] = v;
536 });
Rohit Waghchaure6ab63032017-09-01 15:28:44 +0530537
Vishal Dhayaguded3770182017-12-14 18:06:02 +0530538 erpnext.show_serial_batch_selector(me.frm, d, (item) => {
539 me.frm.script_manager.trigger('qty', item.doctype, item.name);
Saqibfaea8542020-03-26 13:18:57 +0530540 if (!me.frm.doc.set_warehouse)
541 me.frm.script_manager.trigger('warehouse', item.doctype, item.name);
542 }, undefined, !frappe.flags.hide_serial_batch_dialog);
Vishal Dhayaguded3770182017-12-14 18:06:02 +0530543 }
544 },
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +0530545 () => me.conversion_factor(doc, cdt, cdn, true),
rohitwaghchaurea85ddf22019-11-19 18:47:48 +0530546 () => me.remove_pricing_rule(item)
Vishal Dhayaguded3770182017-12-14 18:06:02 +0530547 ]);
Nabin Hait139dc7b2014-02-12 14:53:18 +0530548 }
549 }
550 });
551 }
552 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530553 },
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530554
Deepesh Garg15ff6a52020-02-18 12:28:41 +0530555 get_incoming_rate: function(item, posting_date, posting_time, voucher_type, company) {
556
557 let item_args = {
558 'item_code': item.item_code,
559 'warehouse': in_list('Purchase Receipt', 'Purchase Invoice') ? item.from_warehouse : item.warehouse,
560 'posting_date': posting_date,
561 'posting_time': posting_time,
562 'qty': item.qty * item.conversion_factor,
563 'serial_no': item.serial_no,
564 'voucher_type': voucher_type,
565 'company': company,
566 'allow_zero_valuation_rate': item.allow_zero_valuation_rate
567 }
568
569 frappe.call({
570 method: 'erpnext.stock.utils.get_incoming_rate',
571 args: {
572 args: item_args
573 },
574 callback: function(r) {
575 frappe.model.set_value(item.doctype, item.name, 'rate', r.message);
576 }
577 });
578 },
579
Saif Ur Rehmanaca374e2018-12-28 04:53:25 +0500580 add_taxes_from_item_tax_template: function(item_tax_map) {
581 let me = this;
582
Saif Ur Rehmanfd531a62018-12-29 01:49:11 +0500583 if(item_tax_map && cint(frappe.defaults.get_default("add_taxes_from_item_tax_template"))) {
Saif Ur Rehmanaca374e2018-12-28 04:53:25 +0500584 if(typeof (item_tax_map) == "string") {
585 item_tax_map = JSON.parse(item_tax_map);
586 }
587
588 $.each(item_tax_map, function(tax, rate) {
589 let found = (me.frm.doc.taxes || []).find(d => d.account_head === tax);
590 if(!found) {
591 let child = frappe.model.add_child(me.frm.doc, "taxes");
592 child.charge_type = "On Net Total";
593 child.account_head = tax;
594 child.rate = 0;
595 }
596 });
597 }
598 },
599
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530600 serial_no: function(doc, cdt, cdn) {
601 var me = this;
Rushabh Mehta66d52b52014-03-27 14:17:33 +0530602 var item = frappe.get_doc(cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530603
deepeshgarg00749cca7a2019-01-25 16:41:01 +0530604 if (item && item.serial_no) {
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530605 if (!item.item_code) {
Rushabh Mehtab66edd12017-03-30 21:21:56 +0530606 this.frm.trigger("item_code", cdt, cdn);
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530607 }
608 else {
Nabin Hait79c21912017-07-12 14:10:43 +0530609 var valid_serial_nos = [];
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530610
611 // Replacing all occurences of comma with carriage return
612 var serial_nos = item.serial_no.trim().replace(/,/g, '\n');
613
614 serial_nos = serial_nos.trim().split('\n');
Rushabh Mehtaff938022014-04-15 18:40:00 +0530615
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530616 // Trim each string and push unique string to new list
617 for (var x=0; x<=serial_nos.length - 1; x++) {
Nabin Hait79c21912017-07-12 14:10:43 +0530618 if (serial_nos[x].trim() != "" && valid_serial_nos.indexOf(serial_nos[x].trim()) == -1) {
619 valid_serial_nos.push(serial_nos[x].trim());
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530620 }
621 }
622
623 // Add the new list to the serial no. field in grid with each in new line
Nabin Hait79c21912017-07-12 14:10:43 +0530624 item.serial_no = valid_serial_nos.join('\n');
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530625
626 refresh_field("serial_no", item.name, item.parentfield);
Nabin Haited376ca2018-05-03 16:24:52 +0530627 if(!doc.is_return && cint(user_defaults.set_qty_in_transactions_based_on_serial_no_input)) {
Rushabh Mehta6b537922017-03-14 16:59:11 +0530628 frappe.model.set_value(item.doctype, item.name,
Nabin Hait79c21912017-07-12 14:10:43 +0530629 "qty", valid_serial_nos.length / item.conversion_factor);
630 frappe.model.set_value(item.doctype, item.name, "stock_qty", valid_serial_nos.length);
Nabin Haitb7c0c552015-08-04 12:18:12 +0530631 }
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530632 }
633 }
634 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530635
Anand Doshi923d41d2013-05-28 17:23:36 +0530636 validate: function() {
Anand Doshi652bc072014-04-16 15:21:46 +0530637 this.calculate_taxes_and_totals(false);
Anand Doshi923d41d2013-05-28 17:23:36 +0530638 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530639
Anand Doshi3543f302013-05-24 19:25:01 +0530640 company: function() {
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530641 var me = this;
Nabin Haita731ad42016-02-04 17:21:28 +0530642 var set_pricing = function() {
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530643 if(me.frm.doc.company && me.frm.fields_dict.currency) {
644 var company_currency = me.get_company_currency();
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530645 var company_doc = frappe.get_doc(":Company", me.frm.doc.company);
tundebabzy5510d072017-09-29 10:51:59 +0100646
Nabin Hait1f10d692017-10-02 13:20:51 +0530647 if (!me.frm.doc.currency) {
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530648 me.frm.set_value("currency", company_currency);
649 }
Rushabh Mehtaff938022014-04-15 18:40:00 +0530650
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530651 if (me.frm.doc.currency == company_currency) {
652 me.frm.set_value("conversion_rate", 1.0);
653 }
654 if (me.frm.doc.price_list_currency == company_currency) {
655 me.frm.set_value('plc_conversion_rate', 1.0);
656 }
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530657 if (company_doc.default_letter_head) {
Rushabh Mehtac1857342015-05-27 12:29:57 +0530658 if(me.frm.fields_dict.letter_head) {
659 me.frm.set_value("letter_head", company_doc.default_letter_head);
660 }
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530661 }
karthikeyan57fc60212019-07-04 22:46:16 +0530662 let selling_doctypes_for_tc = ["Sales Invoice", "Quotation", "Sales Order", "Delivery Note"];
663 if (company_doc.default_selling_terms && frappe.meta.has_field(me.frm.doc.doctype, "tc_name") &&
664 selling_doctypes_for_tc.indexOf(me.frm.doc.doctype) != -1) {
665 me.frm.set_value("tc_name", company_doc.default_selling_terms);
666 }
667 let buying_doctypes_for_tc = ["Request for Quotation", "Supplier Quotation", "Purchase Order",
668 "Material Request", "Purchase Receipt"];
669 // Purchase Invoice is excluded as per issue #3345
670 if (company_doc.default_buying_terms && frappe.meta.has_field(me.frm.doc.doctype, "tc_name") &&
671 buying_doctypes_for_tc.indexOf(me.frm.doc.doctype) != -1) {
672 me.frm.set_value("tc_name", company_doc.default_buying_terms);
Rushabh Mehta5495bc52015-05-19 12:00:34 +0530673 }
Akhilesh Darjee1797cfe2013-10-11 13:34:10 +0530674
rohitwaghchaure505661b2017-12-11 14:52:28 +0530675 frappe.run_serially([
676 () => me.frm.script_manager.trigger("currency"),
Saif Ur Rehmaneeead1d2018-12-29 02:25:00 +0500677 () => me.update_item_tax_map(),
rohitwaghchaure505661b2017-12-11 14:52:28 +0530678 () => me.apply_default_taxes(),
679 () => me.apply_pricing_rule()
680 ]);
Nabin Haitdd38a262014-12-26 13:15:21 +0530681 }
Anand Doshi3543f302013-05-24 19:25:01 +0530682 }
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530683
Nabin Haita731ad42016-02-04 17:21:28 +0530684 var set_party_account = function(set_pricing) {
685 if (in_list(["Sales Invoice", "Purchase Invoice"], me.frm.doc.doctype)) {
686 if(me.frm.doc.doctype=="Sales Invoice") {
687 var party_type = "Customer";
688 var party_account_field = 'debit_to';
689 } else {
690 var party_type = "Supplier";
691 var party_account_field = 'credit_to';
692 }
Anand Doshi39a28912016-02-15 11:42:18 +0530693
Nabin Haitef864402016-02-10 14:47:25 +0530694 var party = me.frm.doc[frappe.model.scrub(party_type)];
Faris Ansarif7717b92018-02-08 11:09:54 +0530695 if(party && me.frm.doc.company) {
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530696 return frappe.call({
697 method: "erpnext.accounts.party.get_party_account",
698 args: {
699 company: me.frm.doc.company,
700 party_type: party_type,
Nabin Haitef864402016-02-10 14:47:25 +0530701 party: party
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530702 },
703 callback: function(r) {
704 if(!r.exc && r.message) {
705 me.frm.set_value(party_account_field, r.message);
706 set_pricing();
707 }
Nabin Haita731ad42016-02-04 17:21:28 +0530708 }
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530709 });
Nabin Haitef864402016-02-10 14:47:25 +0530710 } else {
711 set_pricing();
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530712 }
Nabin Haita731ad42016-02-04 17:21:28 +0530713 } else {
714 set_pricing();
715 }
Rushabh Mehta8d1c7a22016-02-10 13:08:42 +0530716
Nabin Haita731ad42016-02-04 17:21:28 +0530717 }
Nabin Haitdd38a262014-12-26 13:15:21 +0530718
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530719 if (this.frm.doc.posting_date) var date = this.frm.doc.posting_date;
720 else var date = this.frm.doc.transaction_date;
Rushabh Mehta5dfe20c2016-04-26 12:59:08 +0530721
722 if (frappe.meta.get_docfield(this.frm.doctype, "shipping_address") &&
Rohit Waghchaureb2e7a1f2016-04-11 12:10:20 +0530723 in_list(['Purchase Order', 'Purchase Receipt', 'Purchase Invoice'], this.frm.doctype)){
Faris Ansariab74ca72017-05-30 12:54:42 +0530724 erpnext.utils.get_shipping_address(this.frm, function(){
725 set_party_account(set_pricing);
726 })
727 } else {
Rohit Waghchaureb2e7a1f2016-04-11 12:10:20 +0530728 set_party_account(set_pricing);
729 }
Rushabh Mehta45ca8a42015-04-28 16:02:09 +0530730
731 if(this.frm.doc.company) {
732 erpnext.last_selected_company = this.frm.doc.company;
733 }
Neil Trini Lasradoeaae58f2014-10-09 15:00:10 +0530734 },
735
736 transaction_date: function() {
Anand Doshi87da6622015-10-19 14:59:32 +0530737 if (this.frm.doc.transaction_date) {
738 this.frm.transaction_date = this.frm.doc.transaction_date;
Nabin Hait288a18e2016-12-08 15:36:23 +0530739 frappe.ui.form.trigger(this.frm.doc.doctype, "currency");
Anand Doshi87da6622015-10-19 14:59:32 +0530740 }
Neil Trini Lasrado78fa6952014-10-03 17:43:02 +0530741 },
742
743 posting_date: function() {
Nabin Hait40e92b62015-07-09 17:12:23 +0530744 var me = this;
745 if (this.frm.doc.posting_date) {
Anand Doshi87da6622015-10-19 14:59:32 +0530746 this.frm.posting_date = this.frm.doc.posting_date;
747
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530748 if ((this.frm.doc.doctype == "Sales Invoice" && this.frm.doc.customer) ||
Nabin Hait40e92b62015-07-09 17:12:23 +0530749 (this.frm.doc.doctype == "Purchase Invoice" && this.frm.doc.supplier)) {
750 return frappe.call({
751 method: "erpnext.accounts.party.get_due_date",
752 args: {
753 "posting_date": me.frm.doc.posting_date,
754 "party_type": me.frm.doc.doctype == "Sales Invoice" ? "Customer" : "Supplier",
Shreya Shah3a9eec22018-02-16 13:05:21 +0530755 "bill_date": me.frm.doc.bill_date,
Nabin Hait40e92b62015-07-09 17:12:23 +0530756 "party": me.frm.doc.doctype == "Sales Invoice" ? me.frm.doc.customer : me.frm.doc.supplier,
757 "company": me.frm.doc.company
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530758 },
Nabin Hait40e92b62015-07-09 17:12:23 +0530759 callback: function(r, rt) {
760 if(r.message) {
Nabin Haitcfa9d1a2018-01-29 16:07:21 +0530761 me.frm.doc.due_date = r.message;
762 refresh_field("due_date");
Nabin Hait87d70272016-12-08 14:43:11 +0530763 frappe.ui.form.trigger(me.frm.doc.doctype, "currency");
tundebabzy650a1ee2018-01-02 07:32:30 +0100764 me.recalculate_terms();
Nabin Hait40e92b62015-07-09 17:12:23 +0530765 }
Nabin Hait40e92b62015-07-09 17:12:23 +0530766 }
767 })
Nabin Hait87d70272016-12-08 14:43:11 +0530768 } else {
769 frappe.ui.form.trigger(me.frm.doc.doctype, "currency");
Nabin Hait40e92b62015-07-09 17:12:23 +0530770 }
771 }
Anand Doshi3543f302013-05-24 19:25:01 +0530772 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530773
tundebabzyda221672018-01-09 06:28:44 +0100774 due_date: function() {
775 // due_date is to be changed, payment terms template and/or payment schedule must
776 // be removed as due_date is automatically changed based on payment terms
rohitwaghchaure52beb772018-01-29 17:40:44 +0530777 if (this.frm.doc.due_date && !this.frm.updating_party_details && !this.frm.doc.is_pos) {
Saurabh3959aeb2018-01-23 15:40:29 +0530778 if (this.frm.doc.payment_terms_template ||
779 (this.frm.doc.payment_schedule && this.frm.doc.payment_schedule.length)) {
tundebabzyda221672018-01-09 06:28:44 +0100780 var message1 = "";
781 var message2 = "";
782 var final_message = "Please clear the ";
783
784 if (this.frm.doc.payment_terms_template) {
785 message1 = "selected Payment Terms Template";
786 final_message = final_message + message1;
787 }
788
Nabin Hait2bfa1802018-01-17 14:39:05 +0530789 if ((this.frm.doc.payment_schedule || []).length) {
tundebabzyda221672018-01-09 06:28:44 +0100790 message2 = "Payment Schedule Table";
791 if (message1.length !== 0) message2 = " and " + message2;
792 final_message = final_message + message2;
793 }
tundebabzyda221672018-01-09 06:28:44 +0100794 frappe.msgprint(final_message);
795 }
Nabin Haitcfa9d1a2018-01-29 16:07:21 +0530796 }
tundebabzyda221672018-01-09 06:28:44 +0100797 },
798
Shreya Shah3a9eec22018-02-16 13:05:21 +0530799 bill_date: function() {
800 this.posting_date();
801 },
802
tundebabzy650a1ee2018-01-02 07:32:30 +0100803 recalculate_terms: function() {
804 const doc = this.frm.doc;
tundebabzy650a1ee2018-01-02 07:32:30 +0100805 if (doc.payment_terms_template) {
806 this.payment_terms_template();
807 } else if (doc.payment_schedule) {
808 const me = this;
tundebabzy650a1ee2018-01-02 07:32:30 +0100809 doc.payment_schedule.forEach(
810 function(term) {
811 if (term.payment_term) {
812 me.payment_term(doc, term.doctype, term.name);
813 } else {
814 frappe.model.set_value(
815 term.doctype, term.name, 'due_date',
816 doc.posting_date || doc.transaction_date
817 );
818 }
819 }
820 );
821 }
822 },
823
Anand Doshi3543f302013-05-24 19:25:01 +0530824 get_company_currency: function() {
825 return erpnext.get_currency(this.frm.doc.company);
826 },
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530827
Neil Trini Lasrado09a66c42015-07-07 16:41:37 +0530828 contact_person: function() {
829 erpnext.utils.get_contact_details(this.frm);
830 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530831
Anand Doshi3543f302013-05-24 19:25:01 +0530832 currency: function() {
Chude Osiegbu256ec172016-09-19 03:53:37 +0100833 /* manqala 19/09/2016: let the translation date be whichever of the transaction_date or posting_date is available */
Nabin Hait288a18e2016-12-08 15:36:23 +0530834 var transaction_date = this.frm.doc.transaction_date || this.frm.doc.posting_date;
Chude Osiegbu256ec172016-09-19 03:53:37 +0100835 /* end manqala */
Anand Doshi61a2f682013-06-21 17:55:31 +0530836 var me = this;
837 this.set_dynamic_labels();
Anand Doshi61a2f682013-06-21 17:55:31 +0530838 var company_currency = this.get_company_currency();
Nabin Hait69cdf592015-05-02 18:46:10 +0530839 // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
Rushabh Mehta76b13412016-07-18 14:30:18 +0530840 if(this.frm.doc.currency && this.frm.doc.currency !== company_currency
Nabin Hait39505072016-07-07 11:11:32 +0530841 && !this.frm.doc.ignore_pricing_rule) {
Shreya0db85062018-05-15 11:25:46 +0530842
Nabin Hait288a18e2016-12-08 15:36:23 +0530843 this.get_exchange_rate(transaction_date, this.frm.doc.currency, company_currency,
Anand Doshi61a2f682013-06-21 17:55:31 +0530844 function(exchange_rate) {
Nabin Haitaafd64d2019-05-27 17:15:47 +0530845 if(exchange_rate != me.frm.doc.conversion_rate) {
846 me.set_margin_amount_based_on_currency(exchange_rate);
847 me.set_actual_charges_based_on_currency(exchange_rate);
848 me.frm.set_value("conversion_rate", exchange_rate);
849 }
Anand Doshi61a2f682013-06-21 17:55:31 +0530850 });
851 } else {
Rushabh Mehtaff938022014-04-15 18:40:00 +0530852 this.conversion_rate();
Anand Doshi61a2f682013-06-21 17:55:31 +0530853 }
854 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530855
Anand Doshi61a2f682013-06-21 17:55:31 +0530856 conversion_rate: function() {
tundebabzyab5b0302017-09-21 10:20:39 +0100857 const me = this.frm;
Anand Doshi535d8332013-07-19 13:51:07 +0530858 if(this.frm.doc.currency === this.get_company_currency()) {
859 this.frm.set_value("conversion_rate", 1.0);
Akhilesh Darjeeacf65a72013-10-17 13:04:26 +0530860 }
861 if(this.frm.doc.currency === this.frm.doc.price_list_currency &&
Anand Doshi61a2f682013-06-21 17:55:31 +0530862 this.frm.doc.plc_conversion_rate !== this.frm.doc.conversion_rate) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530863 this.frm.set_value("plc_conversion_rate", this.frm.doc.conversion_rate);
Anand Doshi61a2f682013-06-21 17:55:31 +0530864 }
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530865
Nabin Haita3dd72a2014-05-28 12:49:20 +0530866 if(flt(this.frm.doc.conversion_rate)>0.0) {
Nabin Hait444f9562014-06-20 15:59:49 +0530867 if(this.frm.doc.ignore_pricing_rule) {
868 this.calculate_taxes_and_totals();
Anand Doshi51f722d2014-07-04 15:44:26 +0530869 } else if (!this.in_apply_price_list){
870 this.apply_price_list();
Nabin Hait444f9562014-06-20 15:59:49 +0530871 }
872
Nabin Haita3dd72a2014-05-28 12:49:20 +0530873 }
tundebabzyab5b0302017-09-21 10:20:39 +0100874 // Make read only if Accounts Settings doesn't allow stale rates
tundebabzybc7c3872017-11-28 18:10:05 +0100875 this.frm.set_df_property("conversion_rate", "read_only", erpnext.stale_rate_allowed() ? 0 : 1);
Anand Doshi3543f302013-05-24 19:25:01 +0530876 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530877
Rushabh Mehta30dc9a12017-11-17 14:31:09 +0530878 shipping_rule: function() {
879 var me = this;
880 if(this.frm.doc.shipping_rule) {
881 return this.frm.call({
882 doc: this.frm.doc,
883 method: "apply_shipping_rule",
884 callback: function(r) {
885 if(!r.exc) {
886 me.calculate_taxes_and_totals();
887 }
888 }
889 }).fail(() => this.frm.set_value('shipping_rule', ''));
890 }
Shreya39adc8f2018-01-22 13:08:05 +0530891 else {
892 me.calculate_taxes_and_totals();
893 }
Rushabh Mehta30dc9a12017-11-17 14:31:09 +0530894 },
895
Nabin Haitaafd64d2019-05-27 17:15:47 +0530896 set_margin_amount_based_on_currency: function(exchange_rate) {
897 if (in_list(["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"]), this.frm.doc.doctype) {
898 var me = this;
899 $.each(this.frm.doc.items || [], function(i, d) {
900 if(d.margin_type == "Amount") {
901 frappe.model.set_value(d.doctype, d.name, "margin_rate_or_amount",
902 flt(d.margin_rate_or_amount) / flt(exchange_rate));
903 }
904 });
905 }
906 },
907
908 set_actual_charges_based_on_currency: function(exchange_rate) {
Nabin Haita9c9b692017-08-31 19:40:09 +0530909 var me = this;
910 $.each(this.frm.doc.taxes || [], function(i, d) {
911 if(d.charge_type == "Actual") {
912 frappe.model.set_value(d.doctype, d.name, "tax_amount",
Nabin Haitaafd64d2019-05-27 17:15:47 +0530913 flt(d.tax_amount) / flt(exchange_rate));
Nabin Haita9c9b692017-08-31 19:40:09 +0530914 }
915 });
916 },
917
Nabin Hait288a18e2016-12-08 15:36:23 +0530918 get_exchange_rate: function(transaction_date, from_currency, to_currency, callback) {
Shreya3f778522018-05-15 16:59:20 +0530919 var args;
920 if (["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"].includes(this.frm.doctype)) {
921 args = "for_selling";
Shreya0db85062018-05-15 11:25:46 +0530922 }
Shreya3f778522018-05-15 16:59:20 +0530923 else if (["Purchase Order", "Purchase Receipt", "Purchase Invoice"].includes(this.frm.doctype)) {
924 args = "for_buying";
925 }
926
Nabin Hait17179ee2017-05-06 13:23:12 +0530927 if (!transaction_date || !from_currency || !to_currency) return;
Nabin Haitbdfb0702015-10-28 15:38:08 +0530928 return frappe.call({
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +0530929 method: "erpnext.setup.utils.get_exchange_rate",
930 args: {
Nabin Hait288a18e2016-12-08 15:36:23 +0530931 transaction_date: transaction_date,
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +0530932 from_currency: from_currency,
Shreya0db85062018-05-15 11:25:46 +0530933 to_currency: to_currency,
934 args: args
Rushabh Mehta66fa1ff2015-05-07 12:25:33 +0530935 },
936 callback: function(r) {
937 callback(flt(r.message));
938 }
Anand Doshi61a2f682013-06-21 17:55:31 +0530939 });
940 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530941
Anand Doshi3543f302013-05-24 19:25:01 +0530942 price_list_currency: function() {
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530943 var me=this;
Anand Doshi3543f302013-05-24 19:25:01 +0530944 this.set_dynamic_labels();
Nabin Haitec361ea2015-05-11 18:14:45 +0530945
946 var company_currency = this.get_company_currency();
947 // Added `ignore_pricing_rule` to determine if document is loading after mapping from another doc
948 if(this.frm.doc.price_list_currency !== company_currency && !this.frm.doc.ignore_pricing_rule) {
Chude Osiegbufbf27b52016-09-07 03:06:08 +0100949 this.get_exchange_rate(this.frm.doc.posting_date, this.frm.doc.price_list_currency, company_currency,
Nabin Haitec361ea2015-05-11 18:14:45 +0530950 function(exchange_rate) {
951 me.frm.set_value("plc_conversion_rate", exchange_rate);
952 });
953 } else {
954 this.plc_conversion_rate();
955 }
Anand Doshi3543f302013-05-24 19:25:01 +0530956 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530957
Anand Doshi3543f302013-05-24 19:25:01 +0530958 plc_conversion_rate: function() {
Anand Doshi61a2f682013-06-21 17:55:31 +0530959 if(this.frm.doc.price_list_currency === this.get_company_currency()) {
960 this.frm.set_value("plc_conversion_rate", 1.0);
Rushabh Mehtaa208c562015-08-03 13:18:54 +0530961 } else if(this.frm.doc.price_list_currency === this.frm.doc.currency
Nabin Hait1d218422015-07-17 15:19:02 +0530962 && this.frm.doc.plc_conversion_rate && cint(this.frm.doc.plc_conversion_rate) != 1 &&
Nabin Haitec361ea2015-05-11 18:14:45 +0530963 cint(this.frm.doc.plc_conversion_rate) != cint(this.frm.doc.conversion_rate)) {
Faris Ansariab74ca72017-05-30 12:54:42 +0530964 this.frm.set_value("conversion_rate", this.frm.doc.plc_conversion_rate);
Akhilesh Darjee564c6212013-10-17 11:31:11 +0530965 }
Rushabh Mehta4a3c8052015-05-19 11:10:12 +0530966
Nabin Haitec361ea2015-05-11 18:14:45 +0530967 if(!this.in_apply_price_list) {
tundebabzy6e90f492018-02-12 10:48:57 +0100968 this.apply_price_list(null, true);
Anand Doshi61a2f682013-06-21 17:55:31 +0530969 }
Anand Doshi3543f302013-05-24 19:25:01 +0530970 },
Rushabh Mehtaff938022014-04-15 18:40:00 +0530971
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530972 uom: function(doc, cdt, cdn) {
973 var me = this;
974 var item = frappe.get_doc(cdt, cdn);
975 if(item.item_code && item.uom) {
976 return this.frm.call({
977 method: "erpnext.stock.get_item_details.get_conversion_factor",
978 child: item,
979 args: {
980 item_code: item.item_code,
981 uom: item.uom
982 },
983 callback: function(r) {
984 if(!r.exc) {
985 me.conversion_factor(me.frm.doc, cdt, cdn);
986 }
987 }
988 });
989 }
990 },
991
Nabin Hait3257aee2017-05-06 12:47:14 +0530992 conversion_factor: function(doc, cdt, cdn, dont_fetch_price_list_rate) {
marinatione1439832020-02-18 10:51:54 +0530993 if(frappe.meta.get_docfield(cdt, "stock_qty", cdn)) {
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +0530994 var item = frappe.get_doc(cdt, cdn);
995 frappe.model.round_floats_in(item, ["qty", "conversion_factor"]);
996 item.stock_qty = flt(item.qty * item.conversion_factor, precision("stock_qty", item));
997 refresh_field("stock_qty", item.name, item.parentfield);
mbauskar287fe812017-04-10 19:15:57 +0530998 this.toggle_conversion_factor(item);
marinatione1439832020-02-18 10:51:54 +0530999
1000 if(doc.doctype != "Material Request") {
1001 item.total_weight = flt(item.stock_qty * item.weight_per_unit);
1002 refresh_field("total_weight", item.name, item.parentfield);
1003 this.calculate_net_weight();
1004 }
1005
rohitwaghchauree8ccc0e2017-11-21 16:17:22 +05301006 if (!dont_fetch_price_list_rate &&
1007 frappe.meta.has_field(doc.doctype, "price_list_currency")) {
1008 this.apply_price_list(item, true);
1009 }
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +05301010 }
1011 },
1012
mbauskar287fe812017-04-10 19:15:57 +05301013 toggle_conversion_factor: function(item) {
1014 // toggle read only property for conversion factor field if the uom and stock uom are same
Rushabh Mehta36311022017-06-20 09:30:53 +05301015 if(this.frm.get_field('items').grid.fields_map.conversion_factor) {
1016 this.frm.fields_dict.items.grid.toggle_enable("conversion_factor",
Javier Wong24690952017-12-29 16:36:47 +08001017 ((item.uom != item.stock_uom) && !frappe.meta.get_docfield(cur_frm.fields_dict.items.grid.doctype, "conversion_factor").read_only)? true: false);
Rushabh Mehta36311022017-06-20 09:30:53 +05301018 }
Vishal Dhayaguded3770182017-12-14 18:06:02 +05301019
mbauskar287fe812017-04-10 19:15:57 +05301020 },
1021
Anand Doshi3543f302013-05-24 19:25:01 +05301022 qty: function(doc, cdt, cdn) {
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +05301023 let item = frappe.get_doc(cdt, cdn);
Nabin Hait50cb49c2020-01-21 13:04:21 +05301024 this.conversion_factor(doc, cdt, cdn, true);
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +05301025 this.apply_pricing_rule(item, true);
Vishal Dhayaguded42242d2017-11-29 16:09:59 +05301026 },
1027
Zarrare83ff382018-09-21 15:45:40 +05301028 service_stop_date: function(frm, cdt, cdn) {
1029 var child = locals[cdt][cdn];
1030
1031 if(child.service_stop_date) {
1032 let start_date = Date.parse(child.service_start_date);
1033 let end_date = Date.parse(child.service_end_date);
1034 let stop_date = Date.parse(child.service_stop_date);
1035
1036 if(stop_date < start_date) {
1037 frappe.model.set_value(cdt, cdn, "service_stop_date", "");
1038 frappe.throw(__("Service Stop Date cannot be before Service Start Date"));
1039 } else if (stop_date > end_date) {
1040 frappe.model.set_value(cdt, cdn, "service_stop_date", "");
1041 frappe.throw(__("Service Stop Date cannot be after Service End Date"));
1042 }
1043 }
1044 },
1045
1046 service_start_date: function(frm, cdt, cdn) {
1047 var child = locals[cdt][cdn];
1048
1049 if(child.service_start_date) {
1050 frappe.call({
1051 "method": "erpnext.stock.get_item_details.calculate_service_end_date",
1052 args: {"args": child},
1053 callback: function(r) {
1054 frappe.model.set_value(cdt, cdn, "service_end_date", r.message.service_end_date);
1055 }
1056 })
1057 }
1058 },
1059
Vishal Dhayaguded3770182017-12-14 18:06:02 +05301060 calculate_net_weight: function(){
1061 /* Calculate Total Net Weight then further applied shipping rule to calculate shipping charges.*/
1062 var me = this;
1063 this.frm.doc.total_net_weight= 0.0;
1064
1065 $.each(this.frm.doc["items"] || [], function(i, item) {
1066 me.frm.doc.total_net_weight += flt(item.total_weight);
1067 });
1068 refresh_field("total_net_weight");
1069 this.shipping_rule();
Anand Doshi3543f302013-05-24 19:25:01 +05301070 },
Rushabh Mehtaff938022014-04-15 18:40:00 +05301071
Anand Doshi61a2f682013-06-21 17:55:31 +05301072 set_dynamic_labels: function() {
1073 // What TODO? should we make price list system non-mandatory?
1074 this.frm.toggle_reqd("plc_conversion_rate",
1075 !!(this.frm.doc.price_list_name && this.frm.doc.price_list_currency));
Rushabh Mehtaff938022014-04-15 18:40:00 +05301076
Saif3c9155e2018-10-18 17:27:56 +05001077 var company_currency = this.get_company_currency();
1078 this.change_form_labels(company_currency);
1079 this.change_grid_labels(company_currency);
1080 this.frm.refresh_fields();
Anand Doshi61a2f682013-06-21 17:55:31 +05301081 },
Rushabh Mehtaff938022014-04-15 18:40:00 +05301082
Nabin Hait613d0812015-02-23 11:58:15 +05301083 change_form_labels: function(company_currency) {
1084 var me = this;
Nabin Hait613d0812015-02-23 11:58:15 +05301085
Rohit Waghchaureb1b32882016-10-21 19:31:54 +05301086 this.frm.set_currency_labels(["base_total", "base_net_total", "base_total_taxes_and_charges",
Nabin Hait613d0812015-02-23 11:58:15 +05301087 "base_discount_amount", "base_grand_total", "base_rounded_total", "base_in_words",
1088 "base_taxes_and_charges_added", "base_taxes_and_charges_deducted", "total_amount_to_pay",
Rushabh Mehta6b537922017-03-14 16:59:11 +05301089 "base_paid_amount", "base_write_off_amount", "base_change_amount", "base_operating_cost",
Nabin Hait2e4de832017-09-19 14:53:16 +05301090 "base_raw_material_cost", "base_total_cost", "base_scrap_material_cost",
1091 "base_rounding_adjustment"], company_currency);
Nabin Hait613d0812015-02-23 11:58:15 +05301092
Rohit Waghchaureb1b32882016-10-21 19:31:54 +05301093 this.frm.set_currency_labels(["total", "net_total", "total_taxes_and_charges", "discount_amount",
Nabin Hait613d0812015-02-23 11:58:15 +05301094 "grand_total", "taxes_and_charges_added", "taxes_and_charges_deducted",
Nabin Hait2e4de832017-09-19 14:53:16 +05301095 "rounded_total", "in_words", "paid_amount", "write_off_amount", "operating_cost",
1096 "scrap_material_cost", "rounding_adjustment", "raw_material_cost",
1097 "total_cost"], this.frm.doc.currency);
Anand Doshi87da6622015-10-19 14:59:32 +05301098
Nabin Hait2e4de832017-09-19 14:53:16 +05301099 this.frm.set_currency_labels(["outstanding_amount", "total_advance"],
1100 this.frm.doc.party_account_currency);
Nabin Hait613d0812015-02-23 11:58:15 +05301101
1102 cur_frm.set_df_property("conversion_rate", "description", "1 " + this.frm.doc.currency
Nabin Hait2e4de832017-09-19 14:53:16 +05301103 + " = [?] " + company_currency);
Nabin Hait613d0812015-02-23 11:58:15 +05301104
1105 if(this.frm.doc.price_list_currency && this.frm.doc.price_list_currency!=company_currency) {
Nabin Hait2e4de832017-09-19 14:53:16 +05301106 cur_frm.set_df_property("plc_conversion_rate", "description", "1 "
1107 + this.frm.doc.price_list_currency + " = [?] " + company_currency);
Nabin Hait613d0812015-02-23 11:58:15 +05301108 }
1109
1110 // toggle fields
Nabin Hait2e4de832017-09-19 14:53:16 +05301111 this.frm.toggle_display(["conversion_rate", "base_total", "base_net_total",
1112 "base_total_taxes_and_charges", "base_taxes_and_charges_added", "base_taxes_and_charges_deducted",
Nabin Hait4ffd7f32015-08-27 12:28:36 +05301113 "base_grand_total", "base_rounded_total", "base_in_words", "base_discount_amount",
Nabin Hait2e4de832017-09-19 14:53:16 +05301114 "base_paid_amount", "base_write_off_amount", "base_operating_cost", "base_raw_material_cost",
1115 "base_total_cost", "base_scrap_material_cost", "base_rounding_adjustment"],
Rushabh Mehta30dc9a12017-11-17 14:31:09 +05301116 this.frm.doc.currency != company_currency);
Nabin Hait613d0812015-02-23 11:58:15 +05301117
1118 this.frm.toggle_display(["plc_conversion_rate", "price_list_currency"],
1119 this.frm.doc.price_list_currency != company_currency);
1120
Rushabh Mehta30dc9a12017-11-17 14:31:09 +05301121 var show = cint(cur_frm.doc.discount_amount) ||
Nabin Hait903c42a2015-02-24 15:24:49 +05301122 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
1123
1124 if(frappe.meta.get_docfield(cur_frm.doctype, "net_total"))
1125 cur_frm.toggle_display("net_total", show);
1126
1127 if(frappe.meta.get_docfield(cur_frm.doctype, "base_net_total"))
1128 cur_frm.toggle_display("base_net_total", (show && (me.frm.doc.currency != company_currency)));
1129
Nabin Hait613d0812015-02-23 11:58:15 +05301130 },
1131
1132 change_grid_labels: function(company_currency) {
1133 var me = this;
Nabin Hait613d0812015-02-23 11:58:15 +05301134
Rohit Waghchaureb1b32882016-10-21 19:31:54 +05301135 this.frm.set_currency_labels(["base_rate", "base_net_rate", "base_price_list_rate", "base_amount", "base_net_amount"],
1136 company_currency, "items");
1137
1138 this.frm.set_currency_labels(["rate", "net_rate", "price_list_rate", "amount", "net_amount"],
1139 this.frm.doc.currency, "items");
1140
1141 if(this.frm.fields_dict["operations"]) {
1142 this.frm.set_currency_labels(["operating_cost", "hour_rate"], this.frm.doc.currency, "operations");
1143 this.frm.set_currency_labels(["base_operating_cost", "base_hour_rate"], company_currency, "operations");
1144
1145 var item_grid = this.frm.fields_dict["operations"].grid;
1146 $.each(["base_operating_cost", "base_hour_rate"], function(i, fname) {
1147 if(frappe.meta.get_docfield(item_grid.doctype, fname))
1148 item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency);
Nabin Hait613d0812015-02-23 11:58:15 +05301149 });
1150 }
1151
Rohit Waghchaureb1b32882016-10-21 19:31:54 +05301152 if(this.frm.fields_dict["scrap_items"]) {
1153 this.frm.set_currency_labels(["rate", "amount"], this.frm.doc.currency, "scrap_items");
1154 this.frm.set_currency_labels(["base_rate", "base_amount"], company_currency, "scrap_items");
Nabin Hait613d0812015-02-23 11:58:15 +05301155
Rohit Waghchaureb1b32882016-10-21 19:31:54 +05301156 var item_grid = this.frm.fields_dict["scrap_items"].grid;
1157 $.each(["base_rate", "base_amount"], function(i, fname) {
1158 if(frappe.meta.get_docfield(item_grid.doctype, fname))
1159 item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency);
1160 });
1161 }
Nabin Hait613d0812015-02-23 11:58:15 +05301162
1163 if(this.frm.fields_dict["taxes"]) {
Rohit Waghchaureb1b32882016-10-21 19:31:54 +05301164 this.frm.set_currency_labels(["tax_amount", "total", "tax_amount_after_discount"], this.frm.doc.currency, "taxes");
Nabin Hait613d0812015-02-23 11:58:15 +05301165
Rohit Waghchaureb1b32882016-10-21 19:31:54 +05301166 this.frm.set_currency_labels(["base_tax_amount", "base_total", "base_tax_amount_after_discount"], company_currency, "taxes");
Nabin Hait613d0812015-02-23 11:58:15 +05301167 }
1168
1169 if(this.frm.fields_dict["advances"]) {
Rohit Waghchaureb1b32882016-10-21 19:31:54 +05301170 this.frm.set_currency_labels(["advance_amount", "allocated_amount"],
Nabin Haitaa015902015-10-16 13:08:33 +05301171 this.frm.doc.party_account_currency, "advances");
Nabin Hait613d0812015-02-23 11:58:15 +05301172 }
1173
1174 // toggle columns
1175 var item_grid = this.frm.fields_dict["items"].grid;
1176 $.each(["base_rate", "base_price_list_rate", "base_amount"], function(i, fname) {
1177 if(frappe.meta.get_docfield(item_grid.doctype, fname))
1178 item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency);
1179 });
1180
1181 var show = (cint(cur_frm.doc.discount_amount)) ||
1182 ((cur_frm.doc.taxes || []).filter(function(d) {return d.included_in_print_rate===1}).length);
1183
1184 $.each(["net_rate", "net_amount"], function(i, fname) {
1185 if(frappe.meta.get_docfield(item_grid.doctype, fname))
1186 item_grid.set_column_disp(fname, show);
1187 });
1188
1189 $.each(["base_net_rate", "base_net_amount"], function(i, fname) {
1190 if(frappe.meta.get_docfield(item_grid.doctype, fname))
1191 item_grid.set_column_disp(fname, (show && (me.frm.doc.currency != company_currency)));
1192 });
1193
1194 // set labels
1195 var $wrapper = $(this.frm.wrapper);
Nabin Hait613d0812015-02-23 11:58:15 +05301196 },
1197
Anand Doshi923d41d2013-05-28 17:23:36 +05301198 recalculate: function() {
1199 this.calculate_taxes_and_totals();
1200 },
Rushabh Mehtaff938022014-04-15 18:40:00 +05301201
Anand Doshi923d41d2013-05-28 17:23:36 +05301202 recalculate_values: function() {
1203 this.calculate_taxes_and_totals();
1204 },
Rushabh Mehtaff938022014-04-15 18:40:00 +05301205
Anand Doshid0b00722013-05-28 18:54:48 +05301206 calculate_charges: function() {
1207 this.calculate_taxes_and_totals();
1208 },
Rushabh Mehtaff938022014-04-15 18:40:00 +05301209
Nabin Hait444f9562014-06-20 15:59:49 +05301210 ignore_pricing_rule: function() {
Nabin Hait8c9fb762017-04-28 14:23:03 +05301211 if(this.frm.doc.ignore_pricing_rule) {
1212 var me = this;
1213 var item_list = [];
Rushabh Mehta36311022017-06-20 09:30:53 +05301214
Nabin Hait8c9fb762017-04-28 14:23:03 +05301215 $.each(this.frm.doc["items"] || [], function(i, d) {
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +05301216 if (d.item_code && !d.is_free_item) {
Nabin Hait8c9fb762017-04-28 14:23:03 +05301217 item_list.push({
1218 "doctype": d.doctype,
1219 "name": d.name,
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +05301220 "item_code": d.item_code,
1221 "pricing_rules": d.pricing_rules,
1222 "parenttype": d.parenttype,
1223 "parent": d.parent
Nabin Hait8c9fb762017-04-28 14:23:03 +05301224 })
1225 }
1226 });
1227 return this.frm.call({
1228 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.remove_pricing_rules",
1229 args: { item_list: item_list },
1230 callback: function(r) {
1231 if (!r.exc && r.message) {
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +05301232 r.message.forEach(row_item => {
rohitwaghchaurea85ddf22019-11-19 18:47:48 +05301233 me.remove_pricing_rule(row_item);
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +05301234 });
Nabin Hait8c9fb762017-04-28 14:23:03 +05301235 me._set_values_for_item_list(r.message);
1236 me.calculate_taxes_and_totals();
Rushabh Mehta30dc9a12017-11-17 14:31:09 +05301237 if(me.frm.doc.apply_discount_on) me.frm.trigger("apply_discount_on");
Nabin Hait8c9fb762017-04-28 14:23:03 +05301238 }
1239 }
1240 });
1241 } else {
1242 this.apply_pricing_rule();
1243 }
Nabin Hait444f9562014-06-20 15:59:49 +05301244 },
1245
1246 apply_pricing_rule: function(item, calculate_taxes_and_totals) {
Nabin Haita3dd72a2014-05-28 12:49:20 +05301247 var me = this;
Anand Doshi70f57eb2015-11-27 14:37:40 +05301248 var args = this._get_args(item);
Rushabh Mehtab46069d2016-01-15 16:59:26 +05301249 if (!(args.items && args.items.length)) {
Anand Doshi70f57eb2015-11-27 14:37:40 +05301250 if(calculate_taxes_and_totals) me.calculate_taxes_and_totals();
1251 return;
1252 }
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +05301253
Anand Doshidffec8f2014-07-01 17:45:15 +05301254 return this.frm.call({
1255 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.apply_pricing_rule",
Rohit Waghchauree5405342019-03-31 16:42:41 +05301256 args: { args: args, doc: me.frm.doc },
Anand Doshidffec8f2014-07-01 17:45:15 +05301257 callback: function(r) {
Anand Doshif214bfc2014-07-16 17:52:08 +05301258 if (!r.exc && r.message) {
Anand Doshidffec8f2014-07-01 17:45:15 +05301259 me._set_values_for_item_list(r.message);
Saurabh2d7af632016-02-26 11:09:20 +05301260 if(item) me.set_gross_profit(item);
Rohit Waghchauredeaee7c2016-09-22 14:02:53 +05301261 if(me.frm.doc.apply_discount_on) me.frm.trigger("apply_discount_on")
Anand Doshidffec8f2014-07-01 17:45:15 +05301262 }
1263 }
1264 });
1265 },
1266
1267 _get_args: function(item) {
1268 var me = this;
1269 return {
Rushabh Mehtab46069d2016-01-15 16:59:26 +05301270 "items": this._get_item_list(item),
Nabin Hait34c551d2019-07-03 10:34:31 +05301271 "customer": me.frm.doc.customer || me.frm.doc.party_name,
1272 "quotation_to": me.frm.doc.quotation_to,
Nabin Hait444f9562014-06-20 15:59:49 +05301273 "customer_group": me.frm.doc.customer_group,
1274 "territory": me.frm.doc.territory,
1275 "supplier": me.frm.doc.supplier,
Zlash652e080982018-04-19 18:37:53 +05301276 "supplier_group": me.frm.doc.supplier_group,
Nabin Hait444f9562014-06-20 15:59:49 +05301277 "currency": me.frm.doc.currency,
1278 "conversion_rate": me.frm.doc.conversion_rate,
1279 "price_list": me.frm.doc.selling_price_list || me.frm.doc.buying_price_list,
Nabin Haitdd82bf22015-05-11 16:49:17 +05301280 "price_list_currency": me.frm.doc.price_list_currency,
Nabin Hait444f9562014-06-20 15:59:49 +05301281 "plc_conversion_rate": me.frm.doc.plc_conversion_rate,
1282 "company": me.frm.doc.company,
1283 "transaction_date": me.frm.doc.transaction_date || me.frm.doc.posting_date,
1284 "campaign": me.frm.doc.campaign,
1285 "sales_partner": me.frm.doc.sales_partner,
1286 "ignore_pricing_rule": me.frm.doc.ignore_pricing_rule,
Rushabh Mehtab46069d2016-01-15 16:59:26 +05301287 "doctype": me.frm.doc.doctype,
Rohit Waghchaure5ecbaa42017-03-28 12:09:07 +05301288 "name": me.frm.doc.name,
Rohit Waghchauredc981dc2017-04-03 14:17:08 +05301289 "is_return": cint(me.frm.doc.is_return),
1290 "update_stock": in_list(['Sales Invoice', 'Purchase Invoice'], me.frm.doc.doctype) ? cint(me.frm.doc.update_stock) : 0,
rohitwaghchaurec037dc72017-11-28 16:11:15 +05301291 "conversion_factor": me.frm.doc.conversion_factor,
ashish-greycube74dc3c92019-08-12 13:39:25 +05301292 "pos_profile": me.frm.doc.doctype == 'Sales Invoice' ? me.frm.doc.pos_profile : '',
1293 "coupon_code": me.frm.doc.coupon_code
Nabin Hait444f9562014-06-20 15:59:49 +05301294 };
Nabin Hait444f9562014-06-20 15:59:49 +05301295 },
1296
1297 _get_item_list: function(item) {
1298 var item_list = [];
1299 var append_item = function(d) {
Anand Doshi39edcbc2014-07-28 16:00:19 +05301300 if (d.item_code) {
1301 item_list.push({
1302 "doctype": d.doctype,
1303 "name": d.name,
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +05301304 "child_docname": d.name,
Anand Doshi39edcbc2014-07-28 16:00:19 +05301305 "item_code": d.item_code,
1306 "item_group": d.item_group,
1307 "brand": d.brand,
Anand Doshi39a28912016-02-15 11:42:18 +05301308 "qty": d.qty,
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +05301309 "stock_qty": d.stock_qty,
Nabin Haite45ec662018-08-01 17:44:34 +05301310 "uom": d.uom,
Suraj Shettyc87b47a2019-04-29 23:18:47 +05301311 "stock_uom": d.stock_uom,
Anand Doshi39a28912016-02-15 11:42:18 +05301312 "parenttype": d.parenttype,
Rohit Waghchaure43b50802016-10-24 14:04:46 +05301313 "parent": d.parent,
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +05301314 "pricing_rules": d.pricing_rules,
Kanchan Chauhanaf0d6372016-11-13 21:19:09 +05301315 "warehouse": d.warehouse,
Rohit Waghchauredc981dc2017-04-03 14:17:08 +05301316 "serial_no": d.serial_no,
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +05301317 "price_list_rate": d.price_list_rate,
mbauskar287fe812017-04-10 19:15:57 +05301318 "conversion_factor": d.conversion_factor || 1.0
Anand Doshi39edcbc2014-07-28 16:00:19 +05301319 });
mbauskarc79415e2016-01-18 16:32:24 +05301320
1321 // if doctype is Quotation Item / Sales Order Iten then add Margin Type and rate in item_list
mbauskara52472c2016-03-05 15:10:25 +05301322 if (in_list(["Quotation Item", "Sales Order Item", "Delivery Note Item", "Sales Invoice Item"]), d.doctype){
Rushabh Mehta30dc9a12017-11-17 14:31:09 +05301323 item_list[0]["margin_type"] = d.margin_type;
1324 item_list[0]["margin_rate_or_amount"] = d.margin_rate_or_amount;
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +05301325 }
Anand Doshi39edcbc2014-07-28 16:00:19 +05301326 }
Nabin Hait444f9562014-06-20 15:59:49 +05301327 };
Nabin Haita3dd72a2014-05-28 12:49:20 +05301328
Nabin Hait444f9562014-06-20 15:59:49 +05301329 if (item) {
1330 append_item(item);
Nabin Haitc1367d92014-05-30 11:43:00 +05301331 } else {
Nabin Hait96339342015-01-21 17:22:11 +05301332 $.each(this.frm.doc["items"] || [], function(i, d) {
Nabin Hait444f9562014-06-20 15:59:49 +05301333 append_item(d);
Nabin Haita3dd72a2014-05-28 12:49:20 +05301334 });
1335 }
Nabin Hait444f9562014-06-20 15:59:49 +05301336 return item_list;
Nabin Haita3dd72a2014-05-28 12:49:20 +05301337 },
1338
Anand Doshidffec8f2014-07-01 17:45:15 +05301339 _set_values_for_item_list: function(children) {
Nabin Hait49a27292014-12-17 11:21:32 +05301340 var me = this;
Nabin Hait7d8fa802014-12-30 15:35:45 +05301341 var price_list_rate_changed = false;
rohitwaghchaurea85ddf22019-11-19 18:47:48 +05301342 var items_rule_dict = {};
1343
Anand Doshidea3fb02015-02-05 15:05:58 +05301344 for(var i=0, l=children.length; i<l; i++) {
1345 var d = children[i];
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +05301346 var existing_pricing_rule = frappe.model.get_value(d.doctype, d.name, "pricing_rules");
Anand Doshidea3fb02015-02-05 15:05:58 +05301347 for(var k in d) {
1348 var v = d[k];
Anand Doshidffec8f2014-07-01 17:45:15 +05301349 if (["doctype", "name"].indexOf(k)===-1) {
Nabin Hait7d8fa802014-12-30 15:35:45 +05301350 if(k=="price_list_rate") {
1351 if(flt(v) != flt(d.price_list_rate)) price_list_rate_changed = true;
1352 }
mbauskar287fe812017-04-10 19:15:57 +05301353 frappe.model.set_value(d.doctype, d.name, k, v);
Anand Doshidffec8f2014-07-01 17:45:15 +05301354 }
Anand Doshidea3fb02015-02-05 15:05:58 +05301355 }
1356
Nabin Hait49a27292014-12-17 11:21:32 +05301357 // if pricing rule set as blank from an existing value, apply price_list
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +05301358 if(!me.frm.doc.ignore_pricing_rule && existing_pricing_rule && !d.pricing_rules) {
Nabin Hait49a27292014-12-17 11:21:32 +05301359 me.apply_price_list(frappe.get_doc(d.doctype, d.name));
rohitwaghchaurea85ddf22019-11-19 18:47:48 +05301360 } else if(!d.pricing_rules) {
1361 me.remove_pricing_rule(frappe.get_doc(d.doctype, d.name));
1362 }
1363
Rohit Waghchaure21fe97e2019-12-13 16:18:38 +05301364 if (d.free_item_data) {
1365 me.apply_product_discount(d.free_item_data);
1366 }
1367
rohitwaghchaurea85ddf22019-11-19 18:47:48 +05301368 if (d.apply_rule_on_other_items) {
1369 items_rule_dict[d.name] = d;
Nabin Hait49a27292014-12-17 11:21:32 +05301370 }
Anand Doshidea3fb02015-02-05 15:05:58 +05301371 }
Nabin Hait7d8fa802014-12-30 15:35:45 +05301372
rohitwaghchaurea85ddf22019-11-19 18:47:48 +05301373 me.apply_rule_on_other_items(items_rule_dict);
1374
Anand Doshidea3fb02015-02-05 15:05:58 +05301375 if(!price_list_rate_changed) me.calculate_taxes_and_totals();
Anand Doshidffec8f2014-07-01 17:45:15 +05301376 },
1377
rohitwaghchaurea85ddf22019-11-19 18:47:48 +05301378 apply_rule_on_other_items: function(args) {
1379 const me = this;
1380 const fields = ["discount_percentage", "discount_amount", "rate"];
1381
1382 for(var k in args) {
1383 let data = args[k];
1384
1385 me.frm.doc.items.forEach(d => {
1386 if (in_list(data.apply_rule_on_other_items, d[data.apply_rule_on])) {
1387 for(var k in data) {
1388 if (in_list(fields, k)) {
1389 frappe.model.set_value(d.doctype, d.name, k, data[k]);
1390 }
1391 }
1392 }
1393 });
1394 }
1395 },
1396
Rohit Waghchaure21fe97e2019-12-13 16:18:38 +05301397 apply_product_discount: function(free_item_data) {
1398 const items = this.frm.doc.items.filter(d => (d.item_code == free_item_data.item_code
1399 && d.is_free_item)) || [];
1400
1401 if (!items.length) {
1402 let row_to_modify = frappe.model.add_child(this.frm.doc,
1403 this.frm.doc.doctype + ' Item', 'items');
1404
1405 for (let key in free_item_data) {
1406 row_to_modify[key] = free_item_data[key];
1407 }
1408 }
1409 },
1410
tundebabzy6e90f492018-02-12 10:48:57 +01001411 apply_price_list: function(item, reset_plc_conversion) {
1412 // We need to reset plc_conversion_rate sometimes because the call to
1413 // `erpnext.stock.get_item_details.apply_price_list` is sensitive to its value
1414 if (!reset_plc_conversion) {
1415 this.frm.set_value("plc_conversion_rate", "");
1416 }
1417
Anand Doshidffec8f2014-07-01 17:45:15 +05301418 var me = this;
Rushabh Mehta052bd622015-01-30 16:57:24 +05301419 var args = this._get_args(item);
Rushabh Mehtab46069d2016-01-15 16:59:26 +05301420 if (!((args.items && args.items.length) || args.price_list)) {
Anand Doshi70f57eb2015-11-27 14:37:40 +05301421 return;
1422 }
Nabin Hait37b047d2015-02-23 16:01:33 +05301423
Rushabh Mehta98aa5812017-11-14 09:32:32 +05301424 if (me.in_apply_price_list == true) return;
1425
1426 me.in_apply_price_list = true;
Anand Doshidffec8f2014-07-01 17:45:15 +05301427 return this.frm.call({
1428 method: "erpnext.stock.get_item_details.apply_price_list",
Rushabh Mehta052bd622015-01-30 16:57:24 +05301429 args: { args: args },
Anand Doshidffec8f2014-07-01 17:45:15 +05301430 callback: function(r) {
1431 if (!r.exc) {
Rushabh Mehta98aa5812017-11-14 09:32:32 +05301432 frappe.run_serially([
1433 () => me.frm.set_value("price_list_currency", r.message.parent.price_list_currency),
1434 () => me.frm.set_value("plc_conversion_rate", r.message.parent.plc_conversion_rate),
1435 () => {
1436 if(args.items.length) {
1437 me._set_values_for_item_list(r.message.children);
1438 }
1439 },
1440 () => { me.in_apply_price_list = false; }
1441 ]);
Nabin Hait37b047d2015-02-23 16:01:33 +05301442
Rushabh Mehta98aa5812017-11-14 09:32:32 +05301443 } else {
1444 me.in_apply_price_list = false;
Anand Doshidffec8f2014-07-01 17:45:15 +05301445 }
1446 }
Faris Ansari404f39d2018-05-14 15:46:45 +05301447 }).always(() => {
1448 me.in_apply_price_list = false;
Anand Doshidffec8f2014-07-01 17:45:15 +05301449 });
1450 },
1451
rohitwaghchaurea85ddf22019-11-19 18:47:48 +05301452 remove_pricing_rule: function(item) {
Rohit Waghchauree5405342019-03-31 16:42:41 +05301453 let me = this;
Rohit Waghchaure660a78b2020-01-02 17:29:50 +05301454 const fields = ["discount_percentage",
1455 "discount_amount", "margin_rate_or_amount", "rate_with_margin"];
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +05301456
rohitwaghchaurea85ddf22019-11-19 18:47:48 +05301457 if(item.remove_free_item) {
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +05301458 var items = [];
1459
1460 me.frm.doc.items.forEach(d => {
1461 if(d.item_code != item.remove_free_item || !d.is_free_item) {
1462 items.push(d);
1463 }
1464 });
1465
1466 me.frm.doc.items = items;
1467 refresh_field('items');
Rohit Waghchauree5405342019-03-31 16:42:41 +05301468 } else if(item.applied_on_items && item.apply_on) {
1469 const applied_on_items = item.applied_on_items.split(',');
1470 me.frm.doc.items.forEach(row => {
1471 if(applied_on_items.includes(row[item.apply_on])) {
1472 fields.forEach(f => {
1473 row[f] = 0;
1474 });
Rohit Waghchaure660a78b2020-01-02 17:29:50 +05301475
1476 ["pricing_rules", "margin_type"].forEach(field => {
1477 if (row[field]) {
1478 row[field] = '';
1479 }
1480 })
Rohit Waghchauree5405342019-03-31 16:42:41 +05301481 }
1482 });
1483
1484 me.trigger_price_list_rate();
Rohit Waghchaure8bfe3302019-03-18 14:34:19 +05301485 }
1486 },
1487
Rohit Waghchauree5405342019-03-31 16:42:41 +05301488 trigger_price_list_rate: function() {
1489 var me = this;
1490
1491 this.frm.doc.items.forEach(child_row => {
1492 me.frm.script_manager.trigger("price_list_rate",
1493 child_row.doctype, child_row.name);
1494 })
1495 },
1496
Nabin Hait139dc7b2014-02-12 14:53:18 +05301497 validate_company_and_party: function() {
Anand Doshi923d41d2013-05-28 17:23:36 +05301498 var me = this;
Anand Doshi3543f302013-05-24 19:25:01 +05301499 var valid = true;
Rushabh Mehtaff938022014-04-15 18:40:00 +05301500
Nabin Hait0f231062014-02-20 11:27:47 +05301501 $.each(["company", "customer"], function(i, fieldname) {
Nabin Haitae07f222017-06-02 13:57:08 +05301502 if(frappe.meta.has_field(me.frm.doc.doctype, fieldname) && me.frm.doc.doctype != "Purchase Order") {
Nabin Hait139dc7b2014-02-12 14:53:18 +05301503 if (!me.frm.doc[fieldname]) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301504 frappe.msgprint(__("Please specify") + ": " +
Rushabh Mehtaff938022014-04-15 18:40:00 +05301505 frappe.meta.get_label(me.frm.doc.doctype, fieldname, me.frm.doc.name) +
Pratik Vyasb52618c2014-04-14 16:25:30 +05301506 ". " + __("It is needed to fetch Item Details."));
Faris Ansariab74ca72017-05-30 12:54:42 +05301507 valid = false;
Nabin Hait139dc7b2014-02-12 14:53:18 +05301508 }
Anand Doshi3543f302013-05-24 19:25:01 +05301509 }
1510 });
1511 return valid;
1512 },
Rushabh Mehtaff938022014-04-15 18:40:00 +05301513
Anand Doshibf3e54a2013-06-05 14:11:32 +05301514 get_terms: function() {
1515 var me = this;
Makarand Bauskar157c3342017-05-26 21:32:33 +05301516
1517 erpnext.utils.get_terms(this.frm.doc.tc_name, this.frm.doc, function(r) {
1518 if(!r.exc) {
1519 me.frm.set_value("terms", r.message);
1520 }
1521 });
Anand Doshibf3e54a2013-06-05 14:11:32 +05301522 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +05301523
1524 taxes_and_charges: function() {
1525 var me = this;
1526 if(this.frm.doc.taxes_and_charges) {
1527 return this.frm.call({
Nabin Hait6b039142014-05-02 15:45:10 +05301528 method: "erpnext.controllers.accounts_controller.get_taxes_and_charges",
1529 args: {
Nabin Hait03f3a8a2014-05-13 16:38:31 +05301530 "master_doctype": frappe.meta.get_docfield(this.frm.doc.doctype, "taxes_and_charges",
1531 this.frm.doc.name).options,
Rohit Waghchaure72cc37e2019-04-11 18:35:15 +05301532 "master_name": this.frm.doc.taxes_and_charges
Nabin Hait6b039142014-05-02 15:45:10 +05301533 },
Akhilesh Darjee4f721562014-01-29 16:31:38 +05301534 callback: function(r) {
1535 if(!r.exc) {
rohitwaghchauree9f0e882019-07-15 18:57:25 +05301536 if(me.frm.doc.shipping_rule && me.frm.doc.taxes) {
1537 for (let tax of r.message) {
1538 me.frm.add_child("taxes", tax);
1539 }
Rohit Waghchaure72cc37e2019-04-11 18:35:15 +05301540
rohitwaghchauree9f0e882019-07-15 18:57:25 +05301541 refresh_field("taxes");
Rohit Waghchaure72cc37e2019-04-11 18:35:15 +05301542 } else {
rohitwaghchauree9f0e882019-07-15 18:57:25 +05301543 me.frm.set_value("taxes", r.message);
Rohit Waghchaure72cc37e2019-04-11 18:35:15 +05301544 me.calculate_taxes_and_totals();
Rohan Bansal90972cf2019-03-11 14:48:26 +05301545 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +05301546 }
1547 }
1548 });
Nabin Haitbc83b9c2014-05-02 17:03:18 +05301549 }
Akhilesh Darjee4f721562014-01-29 16:31:38 +05301550 },
1551
Saif Ur Rehman67786682018-12-27 02:11:07 +05001552 tax_category: function() {
1553 var me = this;
Saif Ur Rehmanfd531a62018-12-29 01:49:11 +05001554 if(me.frm.updating_party_details) return;
1555
Saif Ur Rehmane414a922018-12-28 04:59:42 +05001556 frappe.run_serially([
Saif Ur Rehmaneeead1d2018-12-29 02:25:00 +05001557 () => this.update_item_tax_map(),
Saif Ur Rehmane414a922018-12-28 04:59:42 +05001558 () => erpnext.utils.set_taxes(this.frm, "tax_category"),
1559 ]);
Saif Ur Rehman67786682018-12-27 02:11:07 +05001560 },
1561
1562 item_tax_template: function(doc, cdt, cdn) {
1563 var me = this;
Saif Ur Rehmanfd531a62018-12-29 01:49:11 +05001564 if(me.frm.updating_party_details) return;
1565
Saif Ur Rehman67786682018-12-27 02:11:07 +05001566 var item = frappe.get_doc(cdt, cdn);
1567
Saif Ur Rehmanaca374e2018-12-28 04:53:25 +05001568 if(item.item_tax_template) {
1569 return this.frm.call({
1570 method: "erpnext.stock.get_item_details.get_item_tax_map",
1571 args: {
Saif Ur Rehmaneeead1d2018-12-29 02:25:00 +05001572 company: me.frm.doc.company,
Saif Ur Rehmanaca374e2018-12-28 04:53:25 +05001573 item_tax_template: item.item_tax_template,
1574 as_json: true
1575 },
1576 callback: function(r) {
1577 if(!r.exc) {
1578 item.item_tax_rate = r.message;
1579 me.add_taxes_from_item_tax_template(item.item_tax_rate);
1580 me.calculate_taxes_and_totals();
1581 }
Saif Ur Rehman67786682018-12-27 02:11:07 +05001582 }
Saif Ur Rehmanaca374e2018-12-28 04:53:25 +05001583 });
1584 } else {
1585 item.item_tax_rate = "{}";
1586 me.calculate_taxes_and_totals();
1587 }
Saif Ur Rehman67786682018-12-27 02:11:07 +05001588 },
1589
Saif Ur Rehmaneeead1d2018-12-29 02:25:00 +05001590 update_item_tax_map: function() {
1591 var me = this;
1592 var item_codes = [];
1593 $.each(this.frm.doc.items || [], function(i, item) {
1594 if(item.item_code) {
1595 item_codes.push(item.item_code);
1596 }
1597 });
1598
1599 if(item_codes.length) {
1600 return this.frm.call({
1601 method: "erpnext.stock.get_item_details.get_item_tax_info",
1602 args: {
1603 company: me.frm.doc.company,
1604 tax_category: cstr(me.frm.doc.tax_category),
1605 item_codes: item_codes
1606 },
1607 callback: function(r) {
1608 if(!r.exc) {
1609 $.each(me.frm.doc.items || [], function(i, item) {
1610 if(item.item_code && r.message.hasOwnProperty(item.item_code)) {
1611 item.item_tax_template = r.message[item.item_code].item_tax_template;
1612 item.item_tax_rate = r.message[item.item_code].item_tax_rate;
1613 me.add_taxes_from_item_tax_template(item.item_tax_rate);
1614 } else {
1615 item.item_tax_template = "";
1616 item.item_tax_rate = "{}";
1617 }
1618 });
1619 me.calculate_taxes_and_totals();
1620 }
1621 }
1622 });
1623 }
1624 },
1625
Anand Doshi13945092014-09-21 19:45:49 +05301626 is_recurring: function() {
1627 // set default values for recurring documents
Rushabh Mehta756e5182016-03-03 13:00:17 +05301628 if(this.frm.doc.is_recurring && this.frm.doc.__islocal) {
1629 frappe.msgprint(__("Please set recurring after saving"));
1630 this.frm.set_value('is_recurring', 0);
1631 return;
1632 }
1633
Anand Doshi13945092014-09-21 19:45:49 +05301634 if(this.frm.doc.is_recurring) {
Nabin Hait68a628e2016-03-21 12:03:16 +05301635 if(!this.frm.doc.recurring_id) {
1636 this.frm.set_value('recurring_id', this.frm.doc.name);
1637 }
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +05301638
Anand Doshi13945092014-09-21 19:45:49 +05301639 var owner_email = this.frm.doc.owner=="Administrator"
1640 ? frappe.user_info("Administrator").email
1641 : this.frm.doc.owner;
1642
1643 this.frm.doc.notification_email_address = $.map([cstr(owner_email),
1644 cstr(this.frm.doc.contact_email)], function(v) { return v || null; }).join(", ");
1645 this.frm.doc.repeat_on_day_of_month = frappe.datetime.str_to_obj(this.frm.doc.posting_date).getDate();
1646 }
1647
1648 refresh_many(["notification_email_address", "repeat_on_day_of_month"]);
1649 },
1650
1651 from_date: function() {
1652 // set to_date
1653 if(this.frm.doc.from_date) {
1654 var recurring_type_map = {'Monthly': 1, 'Quarterly': 3, 'Half-yearly': 6,
1655 'Yearly': 12};
1656
1657 var months = recurring_type_map[this.frm.doc.recurring_type];
1658 if(months) {
1659 var to_date = frappe.datetime.add_months(this.frm.doc.from_date,
1660 months);
1661 this.frm.doc.to_date = frappe.datetime.add_days(to_date, -1);
1662 refresh_field('to_date');
1663 }
1664 }
Saurabhd3135532016-02-25 18:59:20 +05301665 },
Rushabh Mehtabb8c6de2016-03-29 17:52:22 +05301666
Saurabh2d7af632016-02-26 11:09:20 +05301667 set_gross_profit: function(item) {
Saurabh5ada14b2016-02-26 18:02:55 +05301668 if (this.frm.doc.doctype == "Sales Order" && item.valuation_rate) {
Faris Ansariab74ca72017-05-30 12:54:42 +05301669 var rate = flt(item.rate) * flt(this.frm.doc.conversion_rate || 1);
Kanchan Chauhan5a980ac2017-02-10 14:27:11 +05301670 item.gross_profit = flt(((rate - item.valuation_rate) * item.stock_qty), precision("amount", item));
Saurabh2d7af632016-02-26 11:09:20 +05301671 }
Rushabh Mehta203cc962016-04-07 15:25:43 +05301672 },
1673
1674 setup_item_selector: function() {
Rushabh Mehtabc928242016-05-29 23:08:22 +05301675 // TODO: remove item selector
1676
1677 return;
Faris Ansariab74ca72017-05-30 12:54:42 +05301678 // if(!this.item_selector) {
1679 // this.item_selector = new erpnext.ItemSelector({frm: this.frm});
1680 // }
Nabin Hait28a05282016-06-27 17:41:39 +05301681 },
Rushabh Mehta76b13412016-07-18 14:30:18 +05301682
Nabin Hait28a05282016-06-27 17:41:39 +05301683 get_advances: function() {
1684 if(!this.frm.is_return) {
1685 return this.frm.call({
1686 method: "set_advances",
1687 doc: this.frm.doc,
1688 callback: function(r, rt) {
1689 refresh_field("advances");
1690 }
1691 })
1692 }
Nabin Hait05aefbb2016-06-28 19:42:19 +05301693 },
Rushabh Mehta76b13412016-07-18 14:30:18 +05301694
Nabin Hait05aefbb2016-06-28 19:42:19 +05301695 make_payment_entry: function() {
1696 return frappe.call({
Rohit Waghchaureb7a55022016-10-05 16:24:41 +05301697 method: cur_frm.cscript.get_method_for_payment(),
Nabin Hait05aefbb2016-06-28 19:42:19 +05301698 args: {
1699 "dt": cur_frm.doc.doctype,
1700 "dn": cur_frm.doc.name
1701 },
1702 callback: function(r) {
1703 var doclist = frappe.model.sync(r.message);
1704 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
Nabin Hait56548cb2016-07-01 15:58:39 +05301705 // cur_frm.refresh_fields()
Nabin Hait05aefbb2016-06-28 19:42:19 +05301706 }
1707 });
Rohit Waghchaureb7a55022016-10-05 16:24:41 +05301708 },
1709
1710 get_method_for_payment: function(){
Rushabh Mehta30dc9a12017-11-17 14:31:09 +05301711 var method = "erpnext.accounts.doctype.payment_entry.payment_entry.get_payment_entry";
Rohit Waghchaure7d529892016-10-06 14:35:04 +05301712 if(cur_frm.doc.__onload && cur_frm.doc.__onload.make_payment_via_journal_entry){
Rohit Waghchaureb7a55022016-10-05 16:24:41 +05301713 if(in_list(['Sales Invoice', 'Purchase Invoice'], cur_frm.doc.doctype)){
Rushabh Mehta30dc9a12017-11-17 14:31:09 +05301714 method = "erpnext.accounts.doctype.journal_entry.journal_entry.get_payment_entry_against_invoice";
Rohit Waghchaureb7a55022016-10-05 16:24:41 +05301715 }else {
Rushabh Mehta30dc9a12017-11-17 14:31:09 +05301716 method= "erpnext.accounts.doctype.journal_entry.journal_entry.get_payment_entry_against_order";
Rohit Waghchaureb7a55022016-10-05 16:24:41 +05301717 }
1718 }
1719
1720 return method
Manas Solankicc902412016-11-10 19:15:11 +05301721 },
Makarand Bauskarc196d742017-06-02 17:28:40 +05301722
1723 set_query_for_batch: function(doc, cdt, cdn) {
1724 // Show item's batches in the dropdown of batch no
1725
1726 var me = this;
1727 var item = frappe.get_doc(cdt, cdn);
1728
1729 if(!item.item_code) {
1730 frappe.throw(__("Please enter Item Code to get batch no"));
Rushabh Mehta36311022017-06-20 09:30:53 +05301731 } else if (doc.doctype == "Purchase Receipt" ||
Makarand Bauskarc196d742017-06-02 17:28:40 +05301732 (doc.doctype == "Purchase Invoice" && doc.update_stock)) {
1733
1734 return {
1735 filters: {'item': item.item_code}
1736 }
1737 } else {
Rushabh Mehta98aa5812017-11-14 09:32:32 +05301738 let filters = {
Makarand Bauskarc196d742017-06-02 17:28:40 +05301739 'item_code': item.item_code,
1740 'posting_date': me.frm.doc.posting_date || frappe.datetime.nowdate(),
1741 }
Rohit Waghchaured8ddd1e2019-10-22 14:03:27 +05301742
1743 if (doc.is_return) {
1744 filters["is_return"] = 1;
1745 }
1746
Rushabh Mehta30dc9a12017-11-17 14:31:09 +05301747 if (item.warehouse) filters["warehouse"] = item.warehouse;
Makarand Bauskarc196d742017-06-02 17:28:40 +05301748
KanchanChauhan0fe455a2017-11-17 20:08:15 +05301749 return {
Makarand Bauskarc196d742017-06-02 17:28:40 +05301750 query : "erpnext.controllers.queries.get_batch_no",
1751 filters: filters
1752 }
1753 }
1754 },
Nabin Hait92759692017-08-15 08:23:51 +05301755
Deepesh Gargef0d26c2020-01-06 15:34:15 +05301756 set_query_for_item_tax_template: function(doc, cdt, cdn) {
1757
1758 var item = frappe.get_doc(cdt, cdn);
1759 if(!item.item_code) {
1760 frappe.throw(__("Please enter Item Code to get item taxes"));
1761 } else {
1762
1763 let filters = {
1764 'item_code': item.item_code,
1765 'valid_from': doc.transaction_date || doc.bill_date || doc.posting_date,
1766 'item_group': item.item_group,
1767 }
1768
1769 if (doc.tax_category)
1770 filters['tax_category'] = doc.tax_category;
1771
1772 return {
1773 query: "erpnext.controllers.queries.get_tax_template",
1774 filters: filters
1775 }
1776 }
1777 },
1778
Nabin Hait92759692017-08-15 08:23:51 +05301779 payment_terms_template: function() {
1780 var me = this;
tundebabzy8f44d1c2018-01-05 08:16:50 +01001781 const doc = this.frm.doc;
1782 if(doc.payment_terms_template && doc.doctype !== 'Delivery Note') {
Shreya Shah3a9eec22018-02-16 13:05:21 +05301783 var posting_date = doc.posting_date || doc.transaction_date;
Nabin Hait92759692017-08-15 08:23:51 +05301784 frappe.call({
1785 method: "erpnext.controllers.accounts_controller.get_payment_terms",
1786 args: {
tundebabzy8f44d1c2018-01-05 08:16:50 +01001787 terms_template: doc.payment_terms_template,
rohitwaghchaureda941af2018-01-17 16:23:04 +05301788 posting_date: posting_date,
Shreya Shah3a9eec22018-02-16 13:05:21 +05301789 grand_total: doc.rounded_total || doc.grand_total,
1790 bill_date: doc.bill_date
Nabin Hait92759692017-08-15 08:23:51 +05301791 },
1792 callback: function(r) {
1793 if(r.message && !r.exc) {
1794 me.frm.set_value("payment_schedule", r.message);
1795 }
1796 }
1797 })
1798 }
1799 },
1800
1801 payment_term: function(doc, cdt, cdn) {
1802 var row = locals[cdt][cdn];
1803 if(row.payment_term) {
1804 frappe.call({
1805 method: "erpnext.controllers.accounts_controller.get_payment_term_details",
1806 args: {
1807 term: row.payment_term,
Shreya Shah3a9eec22018-02-16 13:05:21 +05301808 bill_date: this.frm.doc.bill_date,
Nabin Hait92759692017-08-15 08:23:51 +05301809 posting_date: this.frm.doc.posting_date || this.frm.doc.transaction_date,
Nabin Hait0551f7b2017-11-21 19:58:16 +05301810 grand_total: this.frm.doc.rounded_total || this.frm.doc.grand_total
Nabin Hait92759692017-08-15 08:23:51 +05301811 },
1812 callback: function(r) {
1813 if(r.message && !r.exc) {
1814 for (var d in r.message) {
1815 frappe.model.set_value(cdt, cdn, d, r.message[d]);
1816 }
1817 }
1818 }
1819 })
1820 }
Manas Solankie5e87f72018-05-28 20:07:08 +05301821 },
1822
ronelvcabrera370cdc02019-12-04 18:37:11 +08001823 against_blanket_order: function(doc, cdt, cdn) {
1824 var item = locals[cdt][cdn];
1825 if(!item.against_blanket_order) {
1826 frappe.model.set_value(this.frm.doctype + " Item", item.name, "blanket_order", null);
1827 frappe.model.set_value(this.frm.doctype + " Item", item.name, "blanket_order_rate", 0.00);
1828 }
1829 },
1830
Manas Solankie5e87f72018-05-28 20:07:08 +05301831 blanket_order: function(doc, cdt, cdn) {
1832 var me = this;
1833 var item = locals[cdt][cdn];
1834 if (item.blanket_order && (item.parenttype=="Sales Order" || item.parenttype=="Purchase Order")) {
1835 frappe.call({
1836 method: "erpnext.stock.get_item_details.get_blanket_order_details",
1837 args: {
1838 args:{
1839 item_code: item.item_code,
1840 customer: doc.customer,
1841 supplier: doc.supplier,
1842 company: doc.company,
1843 transaction_date: doc.transaction_date,
1844 blanket_order: item.blanket_order
1845 }
1846 },
1847 callback: function(r) {
1848 if (!r.message) {
Manas Solanki2df1b312018-05-29 12:56:14 +05301849 frappe.throw(__("Invalid Blanket Order for the selected Customer and Item"));
Manas Solankie5e87f72018-05-28 20:07:08 +05301850 } else {
1851 frappe.run_serially([
1852 () => frappe.model.set_value(cdt, cdn, "blanket_order_rate", r.message.blanket_order_rate),
1853 () => me.frm.script_manager.trigger("price_list_rate", cdt, cdn)
1854 ]);
1855 }
1856 }
1857 })
1858 }
Saif60ec88b2018-11-13 12:07:53 +05001859 },
1860
Marica93cb64e2019-12-23 18:24:56 +05301861 set_reserve_warehouse: function() {
1862 this.autofill_warehouse("reserve_warehouse");
1863 },
1864
Saif60ec88b2018-11-13 12:07:53 +05001865 set_warehouse: function() {
Marica93cb64e2019-12-23 18:24:56 +05301866 this.autofill_warehouse("warehouse");
1867 },
1868
1869 autofill_warehouse : function (warehouse_field) {
1870 // set warehouse in all child table rows
Saif60ec88b2018-11-13 12:07:53 +05001871 var me = this;
Marica93cb64e2019-12-23 18:24:56 +05301872 let warehouse = (warehouse_field === "warehouse") ? me.frm.doc.set_warehouse : me.frm.doc.set_reserve_warehouse;
1873 let child_table = (warehouse_field === "warehouse") ? me.frm.doc.items : me.frm.doc.supplied_items;
1874 let doctype = (warehouse_field === "warehouse") ? (me.frm.doctype + " Item") : (me.frm.doctype + " Item Supplied");
1875
1876 if(warehouse) {
1877 $.each(child_table || [], function(i, item) {
1878 frappe.model.set_value(doctype, item.name, warehouse_field, warehouse);
Saif60ec88b2018-11-13 12:07:53 +05001879 });
1880 }
ashish-greycube74dc3c92019-08-12 13:39:25 +05301881 },
Marica93cb64e2019-12-23 18:24:56 +05301882
ashish-greycube74dc3c92019-08-12 13:39:25 +05301883 coupon_code: function() {
1884 var me = this;
1885 frappe.run_serially([
1886 () => this.frm.doc.ignore_pricing_rule=1,
1887 () => me.ignore_pricing_rule(),
1888 () => this.frm.doc.ignore_pricing_rule=0,
1889 () => me.apply_pricing_rule()
1890 ]);
Nabin Hait92759692017-08-15 08:23:51 +05301891 }
Nabin Hait6e740792017-05-06 12:45:16 +05301892});
pratu16x7ae450fc2017-06-26 15:31:46 +05301893
Rohan73726722019-12-24 18:19:58 +05301894erpnext.show_serial_batch_selector = function (frm, d, callback, on_close, show_dialog) {
1895 let warehouse, receiving_stock, existing_stock;
1896 if (frm.doc.is_return) {
1897 if (["Purchase Receipt", "Purchase Invoice"].includes(frm.doc.doctype)) {
1898 existing_stock = true;
1899 warehouse = d.warehouse;
1900 } else if (["Delivery Note", "Sales Invoice"].includes(frm.doc.doctype)) {
1901 receiving_stock = true;
1902 }
1903 } else {
1904 if (frm.doc.doctype == "Stock Entry") {
1905 if (frm.doc.purpose == "Material Receipt") {
1906 receiving_stock = true;
1907 } else {
1908 existing_stock = true;
1909 warehouse = d.s_warehouse;
1910 }
1911 } else {
1912 existing_stock = true;
1913 warehouse = d.warehouse;
1914 }
1915 }
1916
1917 if (!warehouse) {
1918 if (receiving_stock) {
1919 warehouse = ["like", ""];
1920 } else if (existing_stock) {
1921 warehouse = ["!=", ""];
1922 }
1923 }
1924
pratu16x7ae450fc2017-06-26 15:31:46 +05301925 frappe.require("assets/erpnext/js/utils/serial_no_batch_selector.js", function() {
pratu16x789d001c2017-06-26 17:45:49 +05301926 new erpnext.SerialNoBatchSelector({
pratu16x7ae450fc2017-06-26 15:31:46 +05301927 frm: frm,
1928 item: d,
1929 warehouse_details: {
1930 type: "Warehouse",
Rohan73726722019-12-24 18:19:58 +05301931 name: warehouse
pratu16x7ae450fc2017-06-26 15:31:46 +05301932 },
rohitwaghchaure2b95a542017-12-14 16:06:23 +05301933 callback: callback,
1934 on_close: on_close
Rohit Waghchaure6ab63032017-09-01 15:28:44 +05301935 }, show_dialog);
pratu16x7ae450fc2017-06-26 15:31:46 +05301936 });
1937}