blob: 540b5ea0ec49dc20db289d85757b845cfb5c5d69 [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
scmmishraed4b0522019-04-16 17:03:25 +053061 var me = this;
Rushabh Mehta62030e02013-08-14 18:37:28 +053062 $btn.on("click", function() {
scmmishraed4b0522019-04-16 17:03:25 +053063 me.show_serial_batch_selector(grid_row.frm, grid_row.doc);
Rushabh Mehta62030e02013-08-14 18:37:28 +053064 });
deepeshgarg007e64c3572019-05-14 08:50:45 +053065 },
66
67 get_dimension_filters: async function() {
68 let dimensions = await frappe.db.get_list('Accounting Dimension', {
69 fields: ['label', 'fieldname', 'document_type'],
70 });
71
72 return dimensions;
Rushabh Mehta5495bc52015-05-19 12:00:34 +053073 }
Rushabh Mehtac38fc712014-04-16 17:21:25 +053074});
Rushabh Mehta7d368752014-11-24 14:16:47 +053075
Nabin Haitb072c742014-12-16 12:49:13 +053076
77$.extend(erpnext.utils, {
Rushabh Mehta559aa3a2016-09-19 16:04:58 +053078 set_party_dashboard_indicators: function(frm) {
79 if(frm.doc.__onload && frm.doc.__onload.dashboard_info) {
deepeshgarg007920dc142018-11-23 10:17:28 +053080 var company_wise_info = frm.doc.__onload.dashboard_info;
deepeshgarg007f31caff2018-11-27 15:04:12 +053081 if(company_wise_info.length > 1) {
deepeshgarg007a1cffc32018-11-23 16:51:35 +053082 company_wise_info.forEach(function(info) {
deepeshgarg00712f5cef2018-12-25 16:06:19 +053083 erpnext.utils.add_indicator_for_multicompany(frm, info);
deepeshgarg007a1cffc32018-11-23 16:51:35 +053084 });
deepeshgarg00712f5cef2018-12-25 16:06:19 +053085 } else if (company_wise_info.length === 1) {
deepeshgarg00764238ee2018-12-25 16:28:39 +053086 frm.dashboard.add_indicator(__('Annual Billing: {0}',
87 [format_currency(company_wise_info[0].billing_this_year, company_wise_info[0].currency)]), 'blue');
88 frm.dashboard.add_indicator(__('Total Unpaid: {0}',
89 [format_currency(company_wise_info[0].total_unpaid, company_wise_info[0].currency)]),
deepeshgarg0078300f5e2019-01-01 20:28:49 +053090 company_wise_info[0].total_unpaid ? 'orange' : 'green');
deepeshgarg00712f5cef2018-12-25 16:06:19 +053091
deepeshgarg00764238ee2018-12-25 16:28:39 +053092 if(company_wise_info[0].loyalty_points) {
93 frm.dashboard.add_indicator(__('Loyalty Points: {0}',
94 [company_wise_info[0].loyalty_points]), 'blue');
95 }
deepeshgarg007f31caff2018-11-27 15:04:12 +053096 }
Rushabh Mehta559aa3a2016-09-19 16:04:58 +053097 }
98 },
99
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530100 add_indicator_for_multicompany: function(frm, info) {
101 frm.dashboard.stats_area.removeClass('hidden');
102 frm.dashboard.stats_area_row.addClass('flex');
103 frm.dashboard.stats_area_row.css('flex-wrap', 'wrap');
104
105 var color = info.total_unpaid ? 'orange' : 'green';
106
107 var indicator = $('<div class="flex-column col-xs-6">'+
108 '<div style="margin-top:10px"><h6>'+info.company+'</h6></div>'+
109
110 '<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
111 'Annual Billing: '+format_currency(info.billing_this_year, info.currency)+'</span></div>'+
112
113 '<div class="badge-link small" style="margin-bottom:10px">'+
114 '<span class="indicator '+color+'">Total Unpaid: '
115 +format_currency(info.total_unpaid, info.currency)+'</span></div>'+
116
117
118 '</div>').appendTo(frm.dashboard.stats_area_row);
119
120 if(info.loyalty_points){
121 $('<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
122 'Loyalty Points: '+info.loyalty_points+'</span></div>').appendTo(indicator);
123 }
124
125 return indicator;
126 },
127
Shreya Shah149f7ee2018-03-27 11:29:25 +0530128 get_party_name: function(party_type) {
129 var dict = {'Customer': 'customer_name', 'Supplier': 'supplier_name', 'Employee': 'employee_name',
130 'Member': 'member_name'};
131 return dict[party_type];
132 },
133
Rushabh Mehta49f97472018-08-30 18:50:48 +0530134 copy_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname) {
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +0530135 var d = locals[dt][dn];
136 if(d[fieldname]){
137 var cl = doc[table_fieldname] || [];
138 for(var i = 0; i < cl.length; i++) {
139 if(!cl[i][fieldname]) cl[i][fieldname] = d[fieldname];
140 }
141 }
142 refresh_field(table_fieldname);
Makarand Bauskar157c3342017-05-26 21:32:33 +0530143 },
144
145 get_terms: function(tc_name, doc, callback) {
146 if(tc_name) {
147 return frappe.call({
148 method: 'erpnext.setup.doctype.terms_and_conditions.terms_and_conditions.get_terms_and_conditions',
149 args: {
150 template_name: tc_name,
151 doc: doc
152 },
153 callback: function(r) {
154 callback(r)
155 }
156 });
157 }
tundebabzy1a947db2017-08-29 13:48:27 +0100158 },
159
rohitwaghchaure713cfc72018-09-11 17:40:37 +0530160 make_bank_account: function(doctype, docname) {
161 frappe.call({
162 method: "erpnext.accounts.doctype.bank_account.bank_account.make_bank_account",
163 args: {
164 doctype: doctype,
165 docname: docname
166 },
167 freeze: true,
168 callback: function(r) {
169 var doclist = frappe.model.sync(r.message);
170 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
171 }
172 })
173 },
174
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530175 make_subscription: function(doctype, docname) {
176 frappe.call({
rohitwaghchaure79e5b072018-06-21 12:58:14 +0530177 method: "frappe.desk.doctype.auto_repeat.auto_repeat.make_auto_repeat",
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530178 args: {
179 doctype: doctype,
180 docname: docname
181 },
182 callback: function(r) {
183 var doclist = frappe.model.sync(r.message);
184 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
185 }
186 })
187 },
188
Saif90cf2dd2018-09-30 21:46:31 +0500189 make_pricing_rule: function(doctype, docname) {
190 frappe.call({
191 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.make_pricing_rule",
192 args: {
193 doctype: doctype,
194 docname: docname
195 },
196 callback: function(r) {
197 var doclist = frappe.model.sync(r.message);
198 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
199 }
200 })
201 },
202
tundebabzy1a947db2017-08-29 13:48:27 +0100203 /**
204 * Checks if the first row of a given child table is empty
205 * @param child_table - Child table Doctype
206 * @return {Boolean}
207 **/
208 first_row_is_empty: function(child_table){
209 if($.isArray(child_table) && child_table.length > 0) {
210 return !child_table[0].item_code;
211 }
212 return false;
213 },
214
215 /**
216 * Removes the first row of a child table if it is empty
217 * @param {_Frm} frm - The current form
218 * @param {String} child_table_name - The child table field name
219 * @return {Boolean}
220 **/
221 remove_empty_first_row: function(frm, child_table_name){
222 const rows = frm['doc'][child_table_name];
223 if (this.first_row_is_empty(rows)){
224 frm['doc'][child_table_name] = rows.splice(1);
225 }
226 return rows;
227 },
Zarrar6ffdf942018-06-14 12:24:16 +0530228 get_tree_options: function(option) {
229 // get valid options for tree based on user permission & locals dict
230 let unscrub_option = frappe.model.unscrub(option);
231 let user_permission = frappe.defaults.get_user_permissions();
deepeshgarg0070d64d622019-02-20 17:55:14 +0530232 let options;
233
Zarrar6ffdf942018-06-14 12:24:16 +0530234 if(user_permission && user_permission[unscrub_option]) {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530235 options = user_permission[unscrub_option].map(perm => perm.doc);
Zarrar6ffdf942018-06-14 12:24:16 +0530236 } else {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530237 options = $.map(locals[`:${unscrub_option}`], function(c) { return c.name; }).sort();
Zarrar6ffdf942018-06-14 12:24:16 +0530238 }
deepeshgarg0070d64d622019-02-20 17:55:14 +0530239
240 // filter unique values, as there may be multiple user permissions for any value
241 return options.filter((value, index, self) => self.indexOf(value) === index);
Zarrar6ffdf942018-06-14 12:24:16 +0530242 },
243 get_tree_default: function(option) {
244 // set default for a field based on user permission
245 let options = this.get_tree_options(option);
246 if(options.includes(frappe.defaults.get_default(option))) {
247 return frappe.defaults.get_default(option);
248 } else {
249 return options[0];
250 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400251 },
252 copy_parent_value_in_all_row: function(doc, dt, dn, table_fieldname, fieldname, parent_fieldname) {
253 var d = locals[dt][dn];
254 if(d[fieldname]){
255 var cl = doc[table_fieldname] || [];
256 for(var i = 0; i < cl.length; i++) {
257 cl[i][fieldname] = doc[parent_fieldname];
258 }
259 }
260 refresh_field(table_fieldname);
261 },
262
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530263});
264
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530265erpnext.utils.select_alternate_items = function(opts) {
266 const frm = opts.frm;
267 const warehouse_field = opts.warehouse_field || 'warehouse';
268 const item_field = opts.item_field || 'item_code';
269
270 this.data = [];
271 const dialog = new frappe.ui.Dialog({
272 title: __("Select Alternate Item"),
273 fields: [
274 {fieldtype:'Section Break', label: __('Items')},
275 {
276 fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true,
277 in_place_edit: true, data: this.data,
278 get_data: () => {
279 return this.data;
280 },
281 fields: [{
282 fieldtype:'Data',
283 fieldname:"docname",
284 hidden: 1
285 }, {
286 fieldtype:'Link',
287 fieldname:"item_code",
288 options: 'Item',
289 in_list_view: 1,
290 read_only: 1,
291 label: __('Item Code')
292 }, {
293 fieldtype:'Link',
294 fieldname:"alternate_item",
295 options: 'Item',
296 default: "",
297 in_list_view: 1,
298 label: __('Alternate Item'),
299 onchange: function() {
300 const item_code = this.get_value();
301 const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value();
302 if (item_code && warehouse) {
303 frappe.call({
304 method: "erpnext.stock.utils.get_latest_stock_qty",
305 args: {
306 item_code: item_code,
307 warehouse: warehouse
308 },
309 callback: (r) => {
310 this.grid_row.on_grid_fields_dict
311 .actual_qty.set_value(r.message || 0);
312 }
313 })
314 }
315 },
316 get_query: (e) => {
317 return {
318 query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items",
319 filters: {
320 item_code: e.item_code
321 }
322 };
323 }
324 }, {
325 fieldtype:'Link',
326 fieldname:"warehouse",
327 options: 'Warehouse',
328 default: "",
329 in_list_view: 1,
330 label: __('Warehouse'),
331 onchange: function() {
332 const warehouse = this.get_value();
333 const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value();
334 if (item_code && warehouse) {
335 frappe.call({
336 method: "erpnext.stock.utils.get_latest_stock_qty",
337 args: {
338 item_code: item_code,
339 warehouse: warehouse
340 },
341 callback: (r) => {
342 this.grid_row.on_grid_fields_dict
343 .actual_qty.set_value(r.message || 0);
344 }
345 })
346 }
347 },
348 }, {
349 fieldtype:'Float',
350 fieldname:"actual_qty",
351 default: 0,
352 read_only: 1,
353 in_list_view: 1,
354 label: __('Available Qty')
355 }]
356 },
357 ],
358 primary_action: function() {
359 const args = this.get_values()["alternative_items"];
360 const alternative_items = args.filter(d => {
361 if (d.alternate_item && d.item_code != d.alternate_item) {
362 return true;
363 }
364 });
365
366 alternative_items.forEach(d => {
367 let row = frappe.get_doc(opts.child_doctype, d.docname);
Doridel Cahanape42192b2018-05-16 13:28:39 +0800368 let qty = null;
369 if (row.doctype === 'Work Order Item') {
370 qty = row.required_qty;
371 } else {
372 qty = row.qty;
373 }
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530374 row[item_field] = d.alternate_item;
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530375 frm.script_manager.trigger(item_field, row.doctype, row.name)
376 .then(() => {
377 frappe.model.set_value(row.doctype, row.name, 'qty', qty);
378 frappe.model.set_value(row.doctype, row.name,
379 opts.original_item_field, d.item_code);
380 });
381 });
382
383 refresh_field(opts.child_docname);
384 this.hide();
385 },
386 primary_action_label: __('Update')
387 });
388
389 frm.doc[opts.child_docname].forEach(d => {
390 if (!opts.condition || opts.condition(d)) {
391 dialog.fields_dict.alternative_items.df.data.push({
392 "docname": d.name,
393 "item_code": d[item_field],
394 "warehouse": d[warehouse_field],
395 "actual_qty": d.actual_qty
396 });
397 }
398 })
399
400 this.data = dialog.fields_dict.alternative_items.df.data;
401 dialog.fields_dict.alternative_items.grid.refresh();
402 dialog.show();
403}
404
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800405erpnext.utils.update_child_items = function(opts) {
406 const frm = opts.frm;
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100407 const cannot_add_row = (typeof opts.cannot_add_row === 'undefined') ? true : opts.cannot_add_row;
408 const child_docname = (typeof opts.cannot_add_row === 'undefined') ? "items" : opts.child_docname;
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800409 this.data = [];
410 const dialog = new frappe.ui.Dialog({
411 title: __("Update Items"),
412 fields: [
413 {fieldtype:'Section Break', label: __('Items')},
414 {
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100415 fieldname: "trans_items",
416 fieldtype: "Table",
417 cannot_add_rows: cannot_add_row,
418 in_place_edit: true,
419 data: this.data,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800420 get_data: () => {
421 return this.data;
422 },
423 fields: [{
424 fieldtype:'Data',
425 fieldname:"docname",
426 hidden: 0,
427 }, {
428 fieldtype:'Link',
429 fieldname:"item_code",
430 options: 'Item',
431 in_list_view: 1,
432 read_only: 1,
433 label: __('Item Code')
434 }, {
435 fieldtype:'Float',
436 fieldname:"qty",
437 default: 0,
438 read_only: 0,
439 in_list_view: 1,
440 label: __('Qty')
441 }, {
442 fieldtype:'Currency',
443 fieldname:"rate",
444 default: 0,
445 read_only: 0,
446 in_list_view: 1,
447 label: __('Rate')
448 }]
449 },
450 ],
451 primary_action: function() {
452 const trans_items = this.get_values()["trans_items"];
453 frappe.call({
454 method: 'erpnext.controllers.accounts_controller.update_child_qty_rate',
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100455 freeze: true,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800456 args: {
457 'parent_doctype': frm.doc.doctype,
458 'trans_items': trans_items,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100459 'parent_doctype_name': frm.doc.name,
460 'child_docname': child_docname
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800461 },
462 callback: function() {
463 frm.reload_doc();
464 }
465 });
466 this.hide();
467 refresh_field("items");
468 },
469 primary_action_label: __('Update')
470 });
471
472 frm.doc[opts.child_docname].forEach(d => {
473 dialog.fields_dict.trans_items.df.data.push({
474 "docname": d.name,
475 "item_code": d.item_code,
476 "qty": d.qty,
477 "rate": d.rate,
478 });
479 this.data = dialog.fields_dict.trans_items.df.data;
480 dialog.fields_dict.trans_items.grid.refresh();
481 })
482 dialog.show();
483}
484
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530485erpnext.utils.map_current_doc = function(opts) {
486 if(opts.get_query_filters) {
487 opts.get_query = function() {
488 return {filters: opts.get_query_filters};
489 }
490 }
491 var _map = function() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530492 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530493 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530494 if(!cur_frm.doc.items[0].item_code) {
495 cur_frm.doc.items = cur_frm.doc.items.splice(1);
496 }
bhupen3c7e70f2016-11-30 14:32:11 +0530497
498 // find the doctype of the items table
499 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530500
bhupen3c7e70f2016-11-30 14:32:11 +0530501 // find the link fieldname from items table for the given
502 // source_doctype
503 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530504 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530505 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
506
Nabin Hait802b4352017-01-09 15:32:20 +0530507 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530508 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530509 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530510
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530511 $.each(cur_frm.doc.items, function(i, d) {
512 opts.source_name.forEach(function(src) {
513 if(d[link_fieldname]==src) {
514 already_set = true;
515 if (item_qty_map[d.item_code])
516 item_qty_map[d.item_code] += flt(d.qty);
517 else
518 item_qty_map[d.item_code] = flt(d.qty);
519 }
520 });
521 });
522
523 if(already_set) {
524 opts.source_name.forEach(function(src) {
525 frappe.model.with_doc(opts.source_doctype, src, function(r) {
526 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
527 $.each(source_doc.items || [], function(i, row) {
528 if(row.qty > flt(item_qty_map[row.item_code])) {
529 already_set = false;
530 return false;
531 }
532 })
533 })
534
535 if(already_set) {
536 frappe.msgprint(__("You have already selected items from {0} {1}",
537 [opts.source_doctype, src]));
538 return;
539 }
540
541 })
Nabin Hait802b4352017-01-09 15:32:20 +0530542 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530543 }
544
545 return frappe.call({
546 // Sometimes we hit the limit for URL length of a GET request
547 // as we send the full target_doc. Hence this is a POST request.
548 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530549 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530550 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530551 "method": opts.method,
552 "source_names": opts.source_name,
553 "target_doc": cur_frm.doc,
Rushabh Mehta49f97472018-08-30 18:50:48 +0530554 'args': opts.args
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530555 },
556 callback: function(r) {
557 if(!r.exc) {
558 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530559 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530560 cur_frm.refresh();
561 }
562 }
563 });
564 }
565 if(opts.source_doctype) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530566 var d = new frappe.ui.form.MultiSelectDialog({
567 doctype: opts.source_doctype,
568 target: opts.target,
569 date_field: opts.date_field || undefined,
570 setters: opts.setters,
571 get_query: opts.get_query,
572 action: function(selections, args) {
573 let values = selections;
574 if(values.length === 0){
Nabin Haita11dcb62017-12-04 13:36:50 +0530575 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530576 return;
577 }
578 opts.source_name = values;
579 opts.setters = args;
580 d.dialog.hide();
581 _map();
582 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530583 });
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530584 } else if(opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530585 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530586 _map();
587 }
588}
589
Rushabh Mehta180e4352016-07-26 17:57:42 +0530590frappe.form.link_formatters['Item'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530591 if(doc && doc.item_name && doc.item_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530592 return value? value + ': ' + doc.item_name: doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530593 } else {
594 return value;
595 }
596}
597
598frappe.form.link_formatters['Employee'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530599 if(doc && doc.employee_name && doc.employee_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530600 return value? value + ': ' + doc.employee_name: doc.employee_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530601 } else {
602 return value;
603 }
604}
605
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530606// add description on posting time
607$(document).on('app_ready', function() {
608 if(!frappe.datetime.is_timezone_same()) {
609 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
610 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
611 frappe.ui.form.on(d, "onload", function(frm) {
612 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530613 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530614 });
615 });
616 }
akshay14384c22016-07-28 13:53:17 +0530617});