blob: 18716a99c94a81ba5ecca13e42058e6257e8daa5 [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302// License: GNU General Public License v3. See license.txt
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05303frappe.provide("erpnext");
Nabin Haitb072c742014-12-16 12:49:13 +05304frappe.provide("erpnext.utils");
Anand Doshie65e6212012-11-19 15:43:18 +05305
Anand Doshibdee6e02013-07-09 13:03:39 +05306$.extend(erpnext, {
7 get_currency: function(company) {
8 if(!company && cur_frm)
9 company = cur_frm.doc.company;
10 if(company)
Rushabh Mehta66d52b52014-03-27 14:17:33 +053011 return frappe.get_doc(":Company", company).default_currency || frappe.boot.sysdefaults.currency;
Anand Doshibdee6e02013-07-09 13:03:39 +053012 else
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053013 return frappe.boot.sysdefaults.currency;
Anand Doshibdee6e02013-07-09 13:03:39 +053014 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053015
tundebabzyc8978252018-02-12 10:34:50 +010016 get_presentation_currency_list: () => {
17 const docs = frappe.boot.docs;
Zarrare25dcd22018-06-21 10:53:12 +053018 let currency_list = docs.filter(d => d.doctype === ":Currency").map(d => d.name);
19 currency_list.unshift("");
tundebabzyc8978252018-02-12 10:34:50 +010020 return currency_list;
21 },
22
Anand Doshi8bde7f92014-04-24 18:11:49 +053023 toggle_naming_series: function() {
Rushabh Mehtac38fc712014-04-16 17:21:25 +053024 if(cur_frm.fields_dict.naming_series) {
Anand Doshibdee6e02013-07-09 13:03:39 +053025 cur_frm.toggle_display("naming_series", cur_frm.doc.__islocal?true:false);
26 }
27 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053028
Anand Doshibdee6e02013-07-09 13:03:39 +053029 hide_company: function() {
30 if(cur_frm.fields_dict.company) {
Rushabh Mehta89418412013-10-07 18:22:29 +053031 var companies = Object.keys(locals[":Company"] || {});
Anand Doshibdee6e02013-07-09 13:03:39 +053032 if(companies.length === 1) {
33 if(!cur_frm.doc.company) cur_frm.set_value("company", companies[0]);
34 cur_frm.toggle_display("company", false);
Rushabh Mehta45ca8a42015-04-28 16:02:09 +053035 } else if(erpnext.last_selected_company) {
36 if(!cur_frm.doc.company) cur_frm.set_value("company", erpnext.last_selected_company);
Anand Doshibdee6e02013-07-09 13:03:39 +053037 }
38 }
Anand Doshi1ffc5b52013-07-15 18:09:43 +053039 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053040
Rohit Waghchauree9ff1912017-06-19 12:54:59 +053041 is_perpetual_inventory_enabled: function(company) {
42 if(company) {
43 return frappe.get_doc(":Company", company).enable_perpetual_inventory
44 }
45 },
46
Makarand Bauskare6712c12017-10-25 12:17:40 +053047 stale_rate_allowed: () => {
rohitwaghchaure7677ff02017-11-02 18:12:14 +053048 return cint(frappe.boot.sysdefaults.allow_stale);
Makarand Bauskare6712c12017-10-25 12:17:40 +053049 },
50
Rushabh Mehta2a21bc92015-02-25 15:08:42 +053051 setup_serial_no: function() {
52 var grid_row = cur_frm.open_grid_row();
akshay14384c22016-07-28 13:53:17 +053053 if(!grid_row || !grid_row.grid_form.fields_dict.serial_no ||
54 grid_row.grid_form.fields_dict.serial_no.get_status()!=="Write") return;
Rushabh Mehtac38fc712014-04-16 17:21:25 +053055
Pratik Vyasb52618c2014-04-14 16:25:30 +053056 var $btn = $('<button class="btn btn-sm btn-default">'+__("Add Serial No")+'</button>')
Rushabh Mehta62030e02013-08-14 18:37:28 +053057 .appendTo($("<div>")
Rushabh Mehta2a21bc92015-02-25 15:08:42 +053058 .css({"margin-bottom": "10px", "margin-top": "10px"})
akshay14384c22016-07-28 13:53:17 +053059 .appendTo(grid_row.grid_form.fields_dict.serial_no.$wrapper));
Rushabh Mehtac38fc712014-04-16 17:21:25 +053060
Rushabh Mehta62030e02013-08-14 18:37:28 +053061 $btn.on("click", function() {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053062 var d = new frappe.ui.Dialog({
Pratik Vyasb52618c2014-04-14 16:25:30 +053063 title: __("Add Serial No"),
Rushabh Mehta62030e02013-08-14 18:37:28 +053064 fields: [
65 {
66 "fieldtype": "Link",
Rohit Waghchaure2087e572017-02-27 13:43:04 +053067 "fieldname": "serial_no",
Rushabh Mehta62030e02013-08-14 18:37:28 +053068 "options": "Serial No",
Pratik Vyasb52618c2014-04-14 16:25:30 +053069 "label": __("Serial No"),
Neil Trini Lasrado3d61bbb2015-03-12 12:51:50 +053070 "get_query": function () {
Rushabh Mehta45ca8a42015-04-28 16:02:09 +053071 return {
72 filters: {
Shreya14bd43d2018-04-27 16:24:05 +053073 item_code:grid_row.doc.item_code,
Nabin Haitb7c0c552015-08-04 12:18:12 +053074 warehouse:cur_frm.doc.is_return ? null : grid_row.doc.warehouse
Neil Trini Lasrado3d61bbb2015-03-12 12:51:50 +053075 }
76 }
Rushabh Mehta62030e02013-08-14 18:37:28 +053077 }
78 },
79 {
80 "fieldtype": "Button",
Kanchan Chauhan69818702017-02-27 12:53:05 +053081 "fieldname": "add",
Pratik Vyasb52618c2014-04-14 16:25:30 +053082 "label": __("Add")
Rushabh Mehta62030e02013-08-14 18:37:28 +053083 }
84 ]
85 });
Rushabh Mehtac38fc712014-04-16 17:21:25 +053086
Rushabh Mehta62030e02013-08-14 18:37:28 +053087 d.get_input("add").on("click", function() {
88 var serial_no = d.get_value("serial_no");
89 if(serial_no) {
90 var val = (grid_row.doc.serial_no || "").split("\n").concat([serial_no]).join("\n");
akshay14384c22016-07-28 13:53:17 +053091 grid_row.grid_form.fields_dict.serial_no.set_model_value(val.trim());
Rushabh Mehta62030e02013-08-14 18:37:28 +053092 }
93 d.hide();
94 return false;
95 });
Rushabh Mehtac38fc712014-04-16 17:21:25 +053096
Rushabh Mehta62030e02013-08-14 18:37:28 +053097 d.show();
98 });
Rushabh Mehta5495bc52015-05-19 12:00:34 +053099 }
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530100});
Rushabh Mehta7d368752014-11-24 14:16:47 +0530101
Nabin Haitb072c742014-12-16 12:49:13 +0530102
103$.extend(erpnext.utils, {
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530104 set_party_dashboard_indicators: function(frm) {
105 if(frm.doc.__onload && frm.doc.__onload.dashboard_info) {
deepeshgarg007920dc142018-11-23 10:17:28 +0530106 var company_wise_info = frm.doc.__onload.dashboard_info;
deepeshgarg007f31caff2018-11-27 15:04:12 +0530107 if(company_wise_info.length > 1) {
deepeshgarg007a1cffc32018-11-23 16:51:35 +0530108 frm.dashboard.stats_area.removeClass('hidden');
109 frm.dashboard.stats_area_row.append(
110 '<div class="col-xs-4"><h6>Company</h6></div>'+
111 '</div><div class="col-xs-4"><h6>Annual Billing</h6></div>' +
112 '<div class="col-xs-4"><h6>Total Unpaid</h6></div>'
113 );
114 company_wise_info.forEach(function(info) {
115 frm.dashboard.stats_area_row.append(
116 '<div class="col-xs-4 small" style="margin-bottom:10px">'+info.company+'</div>' +
deepeshgarg007f31caff2018-11-27 15:04:12 +0530117 '</div><div class="col-xs-4 small" style="margin-bottom:10px">'+format_currency(info.billing_this_year, info.currency)+'</div>' +
118 '<div class="col-xs-4 small" style="margin-bottom:10px">'+format_currency(info.billing_this_year, info.currency)+'</div>'
deepeshgarg007a1cffc32018-11-23 16:51:35 +0530119 );
120 });
121 }
deepeshgarg007f31caff2018-11-27 15:04:12 +0530122 else {
123 frm.dashboard.stats_area.removeClass('hidden');
124 frm.dashboard.stats_area_row.append(
125 '</div><div class="col-xs-6 small" style="margin-bottom:10px">Annual Billing: '+format_currency(company_wise_info[0].billing_this_year, company_wise_info[0].currency)+'</div>' +
126 '<div class="col-xs-6 small" style="margin-bottom:10px">Total Unpaid: '+format_currency(company_wise_info[0].billing_this_year, company_wise_info[0].currency)+'</div>'
127 );
128 }
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530129 }
130 },
131
Shreya Shah149f7ee2018-03-27 11:29:25 +0530132 get_party_name: function(party_type) {
133 var dict = {'Customer': 'customer_name', 'Supplier': 'supplier_name', 'Employee': 'employee_name',
134 'Member': 'member_name'};
135 return dict[party_type];
136 },
137
Rushabh Mehta49f97472018-08-30 18:50:48 +0530138 copy_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname) {
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +0530139 var d = locals[dt][dn];
140 if(d[fieldname]){
141 var cl = doc[table_fieldname] || [];
142 for(var i = 0; i < cl.length; i++) {
143 if(!cl[i][fieldname]) cl[i][fieldname] = d[fieldname];
144 }
145 }
146 refresh_field(table_fieldname);
Makarand Bauskar157c3342017-05-26 21:32:33 +0530147 },
148
149 get_terms: function(tc_name, doc, callback) {
150 if(tc_name) {
151 return frappe.call({
152 method: 'erpnext.setup.doctype.terms_and_conditions.terms_and_conditions.get_terms_and_conditions',
153 args: {
154 template_name: tc_name,
155 doc: doc
156 },
157 callback: function(r) {
158 callback(r)
159 }
160 });
161 }
tundebabzy1a947db2017-08-29 13:48:27 +0100162 },
163
rohitwaghchaure713cfc72018-09-11 17:40:37 +0530164 make_bank_account: function(doctype, docname) {
165 frappe.call({
166 method: "erpnext.accounts.doctype.bank_account.bank_account.make_bank_account",
167 args: {
168 doctype: doctype,
169 docname: docname
170 },
171 freeze: true,
172 callback: function(r) {
173 var doclist = frappe.model.sync(r.message);
174 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
175 }
176 })
177 },
178
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530179 make_subscription: function(doctype, docname) {
180 frappe.call({
rohitwaghchaure79e5b072018-06-21 12:58:14 +0530181 method: "frappe.desk.doctype.auto_repeat.auto_repeat.make_auto_repeat",
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530182 args: {
183 doctype: doctype,
184 docname: docname
185 },
186 callback: function(r) {
187 var doclist = frappe.model.sync(r.message);
188 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
189 }
190 })
191 },
192
Saif90cf2dd2018-09-30 21:46:31 +0500193 make_pricing_rule: function(doctype, docname) {
194 frappe.call({
195 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.make_pricing_rule",
196 args: {
197 doctype: doctype,
198 docname: docname
199 },
200 callback: function(r) {
201 var doclist = frappe.model.sync(r.message);
202 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
203 }
204 })
205 },
206
tundebabzy1a947db2017-08-29 13:48:27 +0100207 /**
208 * Checks if the first row of a given child table is empty
209 * @param child_table - Child table Doctype
210 * @return {Boolean}
211 **/
212 first_row_is_empty: function(child_table){
213 if($.isArray(child_table) && child_table.length > 0) {
214 return !child_table[0].item_code;
215 }
216 return false;
217 },
218
219 /**
220 * Removes the first row of a child table if it is empty
221 * @param {_Frm} frm - The current form
222 * @param {String} child_table_name - The child table field name
223 * @return {Boolean}
224 **/
225 remove_empty_first_row: function(frm, child_table_name){
226 const rows = frm['doc'][child_table_name];
227 if (this.first_row_is_empty(rows)){
228 frm['doc'][child_table_name] = rows.splice(1);
229 }
230 return rows;
231 },
Zarrar6ffdf942018-06-14 12:24:16 +0530232 get_tree_options: function(option) {
233 // get valid options for tree based on user permission & locals dict
234 let unscrub_option = frappe.model.unscrub(option);
235 let user_permission = frappe.defaults.get_user_permissions();
236 if(user_permission && user_permission[unscrub_option]) {
237 return user_permission[unscrub_option]["docs"];
238 } else {
239 return $.map(locals[`:${unscrub_option}`], function(c) { return c.name; }).sort();
240 }
241 },
242 get_tree_default: function(option) {
243 // set default for a field based on user permission
244 let options = this.get_tree_options(option);
245 if(options.includes(frappe.defaults.get_default(option))) {
246 return frappe.defaults.get_default(option);
247 } else {
248 return options[0];
249 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400250 },
251 copy_parent_value_in_all_row: function(doc, dt, dn, table_fieldname, fieldname, parent_fieldname) {
252 var d = locals[dt][dn];
253 if(d[fieldname]){
254 var cl = doc[table_fieldname] || [];
255 for(var i = 0; i < cl.length; i++) {
256 cl[i][fieldname] = doc[parent_fieldname];
257 }
258 }
259 refresh_field(table_fieldname);
260 },
261
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530262});
263
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530264erpnext.utils.select_alternate_items = function(opts) {
265 const frm = opts.frm;
266 const warehouse_field = opts.warehouse_field || 'warehouse';
267 const item_field = opts.item_field || 'item_code';
268
269 this.data = [];
270 const dialog = new frappe.ui.Dialog({
271 title: __("Select Alternate Item"),
272 fields: [
273 {fieldtype:'Section Break', label: __('Items')},
274 {
275 fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true,
276 in_place_edit: true, data: this.data,
277 get_data: () => {
278 return this.data;
279 },
280 fields: [{
281 fieldtype:'Data',
282 fieldname:"docname",
283 hidden: 1
284 }, {
285 fieldtype:'Link',
286 fieldname:"item_code",
287 options: 'Item',
288 in_list_view: 1,
289 read_only: 1,
290 label: __('Item Code')
291 }, {
292 fieldtype:'Link',
293 fieldname:"alternate_item",
294 options: 'Item',
295 default: "",
296 in_list_view: 1,
297 label: __('Alternate Item'),
298 onchange: function() {
299 const item_code = this.get_value();
300 const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value();
301 if (item_code && warehouse) {
302 frappe.call({
303 method: "erpnext.stock.utils.get_latest_stock_qty",
304 args: {
305 item_code: item_code,
306 warehouse: warehouse
307 },
308 callback: (r) => {
309 this.grid_row.on_grid_fields_dict
310 .actual_qty.set_value(r.message || 0);
311 }
312 })
313 }
314 },
315 get_query: (e) => {
316 return {
317 query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items",
318 filters: {
319 item_code: e.item_code
320 }
321 };
322 }
323 }, {
324 fieldtype:'Link',
325 fieldname:"warehouse",
326 options: 'Warehouse',
327 default: "",
328 in_list_view: 1,
329 label: __('Warehouse'),
330 onchange: function() {
331 const warehouse = this.get_value();
332 const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value();
333 if (item_code && warehouse) {
334 frappe.call({
335 method: "erpnext.stock.utils.get_latest_stock_qty",
336 args: {
337 item_code: item_code,
338 warehouse: warehouse
339 },
340 callback: (r) => {
341 this.grid_row.on_grid_fields_dict
342 .actual_qty.set_value(r.message || 0);
343 }
344 })
345 }
346 },
347 }, {
348 fieldtype:'Float',
349 fieldname:"actual_qty",
350 default: 0,
351 read_only: 1,
352 in_list_view: 1,
353 label: __('Available Qty')
354 }]
355 },
356 ],
357 primary_action: function() {
358 const args = this.get_values()["alternative_items"];
359 const alternative_items = args.filter(d => {
360 if (d.alternate_item && d.item_code != d.alternate_item) {
361 return true;
362 }
363 });
364
365 alternative_items.forEach(d => {
366 let row = frappe.get_doc(opts.child_doctype, d.docname);
Doridel Cahanape42192b2018-05-16 13:28:39 +0800367 let qty = null;
368 if (row.doctype === 'Work Order Item') {
369 qty = row.required_qty;
370 } else {
371 qty = row.qty;
372 }
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530373 row[item_field] = d.alternate_item;
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530374 frm.script_manager.trigger(item_field, row.doctype, row.name)
375 .then(() => {
376 frappe.model.set_value(row.doctype, row.name, 'qty', qty);
377 frappe.model.set_value(row.doctype, row.name,
378 opts.original_item_field, d.item_code);
379 });
380 });
381
382 refresh_field(opts.child_docname);
383 this.hide();
384 },
385 primary_action_label: __('Update')
386 });
387
388 frm.doc[opts.child_docname].forEach(d => {
389 if (!opts.condition || opts.condition(d)) {
390 dialog.fields_dict.alternative_items.df.data.push({
391 "docname": d.name,
392 "item_code": d[item_field],
393 "warehouse": d[warehouse_field],
394 "actual_qty": d.actual_qty
395 });
396 }
397 })
398
399 this.data = dialog.fields_dict.alternative_items.df.data;
400 dialog.fields_dict.alternative_items.grid.refresh();
401 dialog.show();
402}
403
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800404erpnext.utils.update_child_items = function(opts) {
405 const frm = opts.frm;
406
407 this.data = [];
408 const dialog = new frappe.ui.Dialog({
409 title: __("Update Items"),
410 fields: [
411 {fieldtype:'Section Break', label: __('Items')},
412 {
413 fieldname: "trans_items", fieldtype: "Table", cannot_add_rows: true,
414 in_place_edit: true, data: this.data,
415 get_data: () => {
416 return this.data;
417 },
418 fields: [{
419 fieldtype:'Data',
420 fieldname:"docname",
421 hidden: 0,
422 }, {
423 fieldtype:'Link',
424 fieldname:"item_code",
425 options: 'Item',
426 in_list_view: 1,
427 read_only: 1,
428 label: __('Item Code')
429 }, {
430 fieldtype:'Float',
431 fieldname:"qty",
432 default: 0,
433 read_only: 0,
434 in_list_view: 1,
435 label: __('Qty')
436 }, {
437 fieldtype:'Currency',
438 fieldname:"rate",
439 default: 0,
440 read_only: 0,
441 in_list_view: 1,
442 label: __('Rate')
443 }]
444 },
445 ],
446 primary_action: function() {
447 const trans_items = this.get_values()["trans_items"];
448 frappe.call({
449 method: 'erpnext.controllers.accounts_controller.update_child_qty_rate',
450 args: {
451 'parent_doctype': frm.doc.doctype,
452 'trans_items': trans_items,
453 'parent_doctype_name': frm.doc.name
454 },
455 callback: function() {
456 frm.reload_doc();
457 }
458 });
459 this.hide();
460 refresh_field("items");
461 },
462 primary_action_label: __('Update')
463 });
464
465 frm.doc[opts.child_docname].forEach(d => {
466 dialog.fields_dict.trans_items.df.data.push({
467 "docname": d.name,
468 "item_code": d.item_code,
469 "qty": d.qty,
470 "rate": d.rate,
471 });
472 this.data = dialog.fields_dict.trans_items.df.data;
473 dialog.fields_dict.trans_items.grid.refresh();
474 })
475 dialog.show();
476}
477
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530478erpnext.utils.map_current_doc = function(opts) {
479 if(opts.get_query_filters) {
480 opts.get_query = function() {
481 return {filters: opts.get_query_filters};
482 }
483 }
484 var _map = function() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530485 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530486 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530487 if(!cur_frm.doc.items[0].item_code) {
488 cur_frm.doc.items = cur_frm.doc.items.splice(1);
489 }
bhupen3c7e70f2016-11-30 14:32:11 +0530490
491 // find the doctype of the items table
492 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530493
bhupen3c7e70f2016-11-30 14:32:11 +0530494 // find the link fieldname from items table for the given
495 // source_doctype
496 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530497 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530498 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
499
Nabin Hait802b4352017-01-09 15:32:20 +0530500 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530501 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530502 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530503
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530504 $.each(cur_frm.doc.items, function(i, d) {
505 opts.source_name.forEach(function(src) {
506 if(d[link_fieldname]==src) {
507 already_set = true;
508 if (item_qty_map[d.item_code])
509 item_qty_map[d.item_code] += flt(d.qty);
510 else
511 item_qty_map[d.item_code] = flt(d.qty);
512 }
513 });
514 });
515
516 if(already_set) {
517 opts.source_name.forEach(function(src) {
518 frappe.model.with_doc(opts.source_doctype, src, function(r) {
519 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
520 $.each(source_doc.items || [], function(i, row) {
521 if(row.qty > flt(item_qty_map[row.item_code])) {
522 already_set = false;
523 return false;
524 }
525 })
526 })
527
528 if(already_set) {
529 frappe.msgprint(__("You have already selected items from {0} {1}",
530 [opts.source_doctype, src]));
531 return;
532 }
533
534 })
Nabin Hait802b4352017-01-09 15:32:20 +0530535 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530536 }
537
538 return frappe.call({
539 // Sometimes we hit the limit for URL length of a GET request
540 // as we send the full target_doc. Hence this is a POST request.
541 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530542 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530543 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530544 "method": opts.method,
545 "source_names": opts.source_name,
546 "target_doc": cur_frm.doc,
Rushabh Mehta49f97472018-08-30 18:50:48 +0530547 'args': opts.args
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530548 },
549 callback: function(r) {
550 if(!r.exc) {
551 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530552 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530553 cur_frm.refresh();
554 }
555 }
556 });
557 }
558 if(opts.source_doctype) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530559 var d = new frappe.ui.form.MultiSelectDialog({
560 doctype: opts.source_doctype,
561 target: opts.target,
562 date_field: opts.date_field || undefined,
563 setters: opts.setters,
564 get_query: opts.get_query,
565 action: function(selections, args) {
566 let values = selections;
567 if(values.length === 0){
Nabin Haita11dcb62017-12-04 13:36:50 +0530568 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530569 return;
570 }
571 opts.source_name = values;
572 opts.setters = args;
573 d.dialog.hide();
574 _map();
575 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530576 });
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530577 } else if(opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530578 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530579 _map();
580 }
581}
582
Rushabh Mehta180e4352016-07-26 17:57:42 +0530583frappe.form.link_formatters['Item'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530584 if(doc && doc.item_name && doc.item_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530585 return value? value + ': ' + doc.item_name: doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530586 } else {
587 return value;
588 }
589}
590
591frappe.form.link_formatters['Employee'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530592 if(doc && doc.employee_name && doc.employee_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530593 return value? value + ': ' + doc.employee_name: doc.employee_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530594 } else {
595 return value;
596 }
597}
598
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530599// add description on posting time
600$(document).on('app_ready', function() {
601 if(!frappe.datetime.is_timezone_same()) {
602 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
603 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
604 frappe.ui.form.on(d, "onload", function(frm) {
605 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530606 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530607 });
608 });
609 }
akshay14384c22016-07-28 13:53:17 +0530610});