blob: 6860d6a23848cdef39febb88d82420861b6ae192 [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 });
Rushabh Mehta5495bc52015-05-19 12:00:34 +053065 }
Rushabh Mehtac38fc712014-04-16 17:21:25 +053066});
Rushabh Mehta7d368752014-11-24 14:16:47 +053067
Nabin Haitb072c742014-12-16 12:49:13 +053068
69$.extend(erpnext.utils, {
Rushabh Mehta559aa3a2016-09-19 16:04:58 +053070 set_party_dashboard_indicators: function(frm) {
71 if(frm.doc.__onload && frm.doc.__onload.dashboard_info) {
deepeshgarg007920dc142018-11-23 10:17:28 +053072 var company_wise_info = frm.doc.__onload.dashboard_info;
deepeshgarg007f31caff2018-11-27 15:04:12 +053073 if(company_wise_info.length > 1) {
deepeshgarg007a1cffc32018-11-23 16:51:35 +053074 company_wise_info.forEach(function(info) {
deepeshgarg00712f5cef2018-12-25 16:06:19 +053075 erpnext.utils.add_indicator_for_multicompany(frm, info);
deepeshgarg007a1cffc32018-11-23 16:51:35 +053076 });
deepeshgarg00712f5cef2018-12-25 16:06:19 +053077 } else if (company_wise_info.length === 1) {
deepeshgarg00764238ee2018-12-25 16:28:39 +053078 frm.dashboard.add_indicator(__('Annual Billing: {0}',
79 [format_currency(company_wise_info[0].billing_this_year, company_wise_info[0].currency)]), 'blue');
80 frm.dashboard.add_indicator(__('Total Unpaid: {0}',
81 [format_currency(company_wise_info[0].total_unpaid, company_wise_info[0].currency)]),
deepeshgarg0078300f5e2019-01-01 20:28:49 +053082 company_wise_info[0].total_unpaid ? 'orange' : 'green');
deepeshgarg00712f5cef2018-12-25 16:06:19 +053083
deepeshgarg00764238ee2018-12-25 16:28:39 +053084 if(company_wise_info[0].loyalty_points) {
85 frm.dashboard.add_indicator(__('Loyalty Points: {0}',
86 [company_wise_info[0].loyalty_points]), 'blue');
87 }
deepeshgarg007f31caff2018-11-27 15:04:12 +053088 }
Rushabh Mehta559aa3a2016-09-19 16:04:58 +053089 }
90 },
91
deepeshgarg00712f5cef2018-12-25 16:06:19 +053092 add_indicator_for_multicompany: function(frm, info) {
93 frm.dashboard.stats_area.removeClass('hidden');
94 frm.dashboard.stats_area_row.addClass('flex');
95 frm.dashboard.stats_area_row.css('flex-wrap', 'wrap');
96
97 var color = info.total_unpaid ? 'orange' : 'green';
98
99 var indicator = $('<div class="flex-column col-xs-6">'+
100 '<div style="margin-top:10px"><h6>'+info.company+'</h6></div>'+
101
102 '<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
103 'Annual Billing: '+format_currency(info.billing_this_year, info.currency)+'</span></div>'+
104
105 '<div class="badge-link small" style="margin-bottom:10px">'+
106 '<span class="indicator '+color+'">Total Unpaid: '
107 +format_currency(info.total_unpaid, info.currency)+'</span></div>'+
108
109
110 '</div>').appendTo(frm.dashboard.stats_area_row);
111
112 if(info.loyalty_points){
113 $('<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
114 'Loyalty Points: '+info.loyalty_points+'</span></div>').appendTo(indicator);
115 }
116
117 return indicator;
118 },
119
Shreya Shah149f7ee2018-03-27 11:29:25 +0530120 get_party_name: function(party_type) {
121 var dict = {'Customer': 'customer_name', 'Supplier': 'supplier_name', 'Employee': 'employee_name',
122 'Member': 'member_name'};
123 return dict[party_type];
124 },
125
Rushabh Mehta49f97472018-08-30 18:50:48 +0530126 copy_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname) {
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +0530127 var d = locals[dt][dn];
128 if(d[fieldname]){
129 var cl = doc[table_fieldname] || [];
130 for(var i = 0; i < cl.length; i++) {
131 if(!cl[i][fieldname]) cl[i][fieldname] = d[fieldname];
132 }
133 }
134 refresh_field(table_fieldname);
Makarand Bauskar157c3342017-05-26 21:32:33 +0530135 },
136
137 get_terms: function(tc_name, doc, callback) {
138 if(tc_name) {
139 return frappe.call({
140 method: 'erpnext.setup.doctype.terms_and_conditions.terms_and_conditions.get_terms_and_conditions',
141 args: {
142 template_name: tc_name,
143 doc: doc
144 },
145 callback: function(r) {
146 callback(r)
147 }
148 });
149 }
tundebabzy1a947db2017-08-29 13:48:27 +0100150 },
151
rohitwaghchaure713cfc72018-09-11 17:40:37 +0530152 make_bank_account: function(doctype, docname) {
153 frappe.call({
154 method: "erpnext.accounts.doctype.bank_account.bank_account.make_bank_account",
155 args: {
156 doctype: doctype,
157 docname: docname
158 },
159 freeze: true,
160 callback: function(r) {
161 var doclist = frappe.model.sync(r.message);
162 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
163 }
164 })
165 },
166
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530167 make_subscription: function(doctype, docname) {
168 frappe.call({
rohitwaghchaure79e5b072018-06-21 12:58:14 +0530169 method: "frappe.desk.doctype.auto_repeat.auto_repeat.make_auto_repeat",
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530170 args: {
171 doctype: doctype,
172 docname: docname
173 },
174 callback: function(r) {
175 var doclist = frappe.model.sync(r.message);
176 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
177 }
178 })
179 },
180
Saif90cf2dd2018-09-30 21:46:31 +0500181 make_pricing_rule: function(doctype, docname) {
182 frappe.call({
183 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.make_pricing_rule",
184 args: {
185 doctype: doctype,
186 docname: docname
187 },
188 callback: function(r) {
189 var doclist = frappe.model.sync(r.message);
190 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
191 }
192 })
193 },
194
tundebabzy1a947db2017-08-29 13:48:27 +0100195 /**
196 * Checks if the first row of a given child table is empty
197 * @param child_table - Child table Doctype
198 * @return {Boolean}
199 **/
200 first_row_is_empty: function(child_table){
201 if($.isArray(child_table) && child_table.length > 0) {
202 return !child_table[0].item_code;
203 }
204 return false;
205 },
206
207 /**
208 * Removes the first row of a child table if it is empty
209 * @param {_Frm} frm - The current form
210 * @param {String} child_table_name - The child table field name
211 * @return {Boolean}
212 **/
213 remove_empty_first_row: function(frm, child_table_name){
214 const rows = frm['doc'][child_table_name];
215 if (this.first_row_is_empty(rows)){
216 frm['doc'][child_table_name] = rows.splice(1);
217 }
218 return rows;
219 },
Zarrar6ffdf942018-06-14 12:24:16 +0530220 get_tree_options: function(option) {
221 // get valid options for tree based on user permission & locals dict
222 let unscrub_option = frappe.model.unscrub(option);
223 let user_permission = frappe.defaults.get_user_permissions();
deepeshgarg0070d64d622019-02-20 17:55:14 +0530224 let options;
225
Zarrar6ffdf942018-06-14 12:24:16 +0530226 if(user_permission && user_permission[unscrub_option]) {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530227 options = user_permission[unscrub_option].map(perm => perm.doc);
Zarrar6ffdf942018-06-14 12:24:16 +0530228 } else {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530229 options = $.map(locals[`:${unscrub_option}`], function(c) { return c.name; }).sort();
Zarrar6ffdf942018-06-14 12:24:16 +0530230 }
deepeshgarg0070d64d622019-02-20 17:55:14 +0530231
232 // filter unique values, as there may be multiple user permissions for any value
233 return options.filter((value, index, self) => self.indexOf(value) === index);
Zarrar6ffdf942018-06-14 12:24:16 +0530234 },
235 get_tree_default: function(option) {
236 // set default for a field based on user permission
237 let options = this.get_tree_options(option);
238 if(options.includes(frappe.defaults.get_default(option))) {
239 return frappe.defaults.get_default(option);
240 } else {
241 return options[0];
242 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400243 },
244 copy_parent_value_in_all_row: function(doc, dt, dn, table_fieldname, fieldname, parent_fieldname) {
245 var d = locals[dt][dn];
246 if(d[fieldname]){
247 var cl = doc[table_fieldname] || [];
248 for(var i = 0; i < cl.length; i++) {
249 cl[i][fieldname] = doc[parent_fieldname];
250 }
251 }
252 refresh_field(table_fieldname);
253 },
254
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530255});
256
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530257erpnext.utils.select_alternate_items = function(opts) {
258 const frm = opts.frm;
259 const warehouse_field = opts.warehouse_field || 'warehouse';
260 const item_field = opts.item_field || 'item_code';
261
262 this.data = [];
263 const dialog = new frappe.ui.Dialog({
264 title: __("Select Alternate Item"),
265 fields: [
266 {fieldtype:'Section Break', label: __('Items')},
267 {
268 fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true,
269 in_place_edit: true, data: this.data,
270 get_data: () => {
271 return this.data;
272 },
273 fields: [{
274 fieldtype:'Data',
275 fieldname:"docname",
276 hidden: 1
277 }, {
278 fieldtype:'Link',
279 fieldname:"item_code",
280 options: 'Item',
281 in_list_view: 1,
282 read_only: 1,
283 label: __('Item Code')
284 }, {
285 fieldtype:'Link',
286 fieldname:"alternate_item",
287 options: 'Item',
288 default: "",
289 in_list_view: 1,
290 label: __('Alternate Item'),
291 onchange: function() {
292 const item_code = this.get_value();
293 const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value();
294 if (item_code && warehouse) {
295 frappe.call({
296 method: "erpnext.stock.utils.get_latest_stock_qty",
297 args: {
298 item_code: item_code,
299 warehouse: warehouse
300 },
301 callback: (r) => {
302 this.grid_row.on_grid_fields_dict
303 .actual_qty.set_value(r.message || 0);
304 }
305 })
306 }
307 },
308 get_query: (e) => {
309 return {
310 query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items",
311 filters: {
312 item_code: e.item_code
313 }
314 };
315 }
316 }, {
317 fieldtype:'Link',
318 fieldname:"warehouse",
319 options: 'Warehouse',
320 default: "",
321 in_list_view: 1,
322 label: __('Warehouse'),
323 onchange: function() {
324 const warehouse = this.get_value();
325 const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value();
326 if (item_code && warehouse) {
327 frappe.call({
328 method: "erpnext.stock.utils.get_latest_stock_qty",
329 args: {
330 item_code: item_code,
331 warehouse: warehouse
332 },
333 callback: (r) => {
334 this.grid_row.on_grid_fields_dict
335 .actual_qty.set_value(r.message || 0);
336 }
337 })
338 }
339 },
340 }, {
341 fieldtype:'Float',
342 fieldname:"actual_qty",
343 default: 0,
344 read_only: 1,
345 in_list_view: 1,
346 label: __('Available Qty')
347 }]
348 },
349 ],
350 primary_action: function() {
351 const args = this.get_values()["alternative_items"];
352 const alternative_items = args.filter(d => {
353 if (d.alternate_item && d.item_code != d.alternate_item) {
354 return true;
355 }
356 });
357
358 alternative_items.forEach(d => {
359 let row = frappe.get_doc(opts.child_doctype, d.docname);
Doridel Cahanape42192b2018-05-16 13:28:39 +0800360 let qty = null;
361 if (row.doctype === 'Work Order Item') {
362 qty = row.required_qty;
363 } else {
364 qty = row.qty;
365 }
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530366 row[item_field] = d.alternate_item;
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530367 frm.script_manager.trigger(item_field, row.doctype, row.name)
368 .then(() => {
369 frappe.model.set_value(row.doctype, row.name, 'qty', qty);
370 frappe.model.set_value(row.doctype, row.name,
371 opts.original_item_field, d.item_code);
372 });
373 });
374
375 refresh_field(opts.child_docname);
376 this.hide();
377 },
378 primary_action_label: __('Update')
379 });
380
381 frm.doc[opts.child_docname].forEach(d => {
382 if (!opts.condition || opts.condition(d)) {
383 dialog.fields_dict.alternative_items.df.data.push({
384 "docname": d.name,
385 "item_code": d[item_field],
386 "warehouse": d[warehouse_field],
387 "actual_qty": d.actual_qty
388 });
389 }
390 })
391
392 this.data = dialog.fields_dict.alternative_items.df.data;
393 dialog.fields_dict.alternative_items.grid.refresh();
394 dialog.show();
395}
396
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800397erpnext.utils.update_child_items = function(opts) {
398 const frm = opts.frm;
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100399 const cannot_add_row = (typeof opts.cannot_add_row === 'undefined') ? true : opts.cannot_add_row;
400 const child_docname = (typeof opts.cannot_add_row === 'undefined') ? "items" : opts.child_docname;
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800401 this.data = [];
402 const dialog = new frappe.ui.Dialog({
403 title: __("Update Items"),
404 fields: [
405 {fieldtype:'Section Break', label: __('Items')},
406 {
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100407 fieldname: "trans_items",
408 fieldtype: "Table",
409 cannot_add_rows: cannot_add_row,
410 in_place_edit: true,
411 data: this.data,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800412 get_data: () => {
413 return this.data;
414 },
415 fields: [{
416 fieldtype:'Data',
417 fieldname:"docname",
418 hidden: 0,
419 }, {
420 fieldtype:'Link',
421 fieldname:"item_code",
422 options: 'Item',
423 in_list_view: 1,
424 read_only: 1,
425 label: __('Item Code')
426 }, {
427 fieldtype:'Float',
428 fieldname:"qty",
429 default: 0,
430 read_only: 0,
431 in_list_view: 1,
432 label: __('Qty')
433 }, {
434 fieldtype:'Currency',
435 fieldname:"rate",
436 default: 0,
437 read_only: 0,
438 in_list_view: 1,
439 label: __('Rate')
440 }]
441 },
442 ],
443 primary_action: function() {
444 const trans_items = this.get_values()["trans_items"];
445 frappe.call({
446 method: 'erpnext.controllers.accounts_controller.update_child_qty_rate',
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100447 freeze: true,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800448 args: {
449 'parent_doctype': frm.doc.doctype,
450 'trans_items': trans_items,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100451 'parent_doctype_name': frm.doc.name,
452 'child_docname': child_docname
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800453 },
454 callback: function() {
455 frm.reload_doc();
456 }
457 });
458 this.hide();
459 refresh_field("items");
460 },
461 primary_action_label: __('Update')
462 });
463
464 frm.doc[opts.child_docname].forEach(d => {
465 dialog.fields_dict.trans_items.df.data.push({
466 "docname": d.name,
467 "item_code": d.item_code,
468 "qty": d.qty,
469 "rate": d.rate,
470 });
471 this.data = dialog.fields_dict.trans_items.df.data;
472 dialog.fields_dict.trans_items.grid.refresh();
473 })
474 dialog.show();
475}
476
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530477erpnext.utils.map_current_doc = function(opts) {
478 if(opts.get_query_filters) {
479 opts.get_query = function() {
480 return {filters: opts.get_query_filters};
481 }
482 }
483 var _map = function() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530484 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530485 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530486 if(!cur_frm.doc.items[0].item_code) {
487 cur_frm.doc.items = cur_frm.doc.items.splice(1);
488 }
bhupen3c7e70f2016-11-30 14:32:11 +0530489
490 // find the doctype of the items table
491 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530492
bhupen3c7e70f2016-11-30 14:32:11 +0530493 // find the link fieldname from items table for the given
494 // source_doctype
495 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530496 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530497 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
498
Nabin Hait802b4352017-01-09 15:32:20 +0530499 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530500 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530501 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530502
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530503 $.each(cur_frm.doc.items, function(i, d) {
504 opts.source_name.forEach(function(src) {
505 if(d[link_fieldname]==src) {
506 already_set = true;
507 if (item_qty_map[d.item_code])
508 item_qty_map[d.item_code] += flt(d.qty);
509 else
510 item_qty_map[d.item_code] = flt(d.qty);
511 }
512 });
513 });
514
515 if(already_set) {
516 opts.source_name.forEach(function(src) {
517 frappe.model.with_doc(opts.source_doctype, src, function(r) {
518 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
519 $.each(source_doc.items || [], function(i, row) {
520 if(row.qty > flt(item_qty_map[row.item_code])) {
521 already_set = false;
522 return false;
523 }
524 })
525 })
526
527 if(already_set) {
528 frappe.msgprint(__("You have already selected items from {0} {1}",
529 [opts.source_doctype, src]));
530 return;
531 }
532
533 })
Nabin Hait802b4352017-01-09 15:32:20 +0530534 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530535 }
536
537 return frappe.call({
538 // Sometimes we hit the limit for URL length of a GET request
539 // as we send the full target_doc. Hence this is a POST request.
540 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530541 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530542 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530543 "method": opts.method,
544 "source_names": opts.source_name,
545 "target_doc": cur_frm.doc,
Rushabh Mehta49f97472018-08-30 18:50:48 +0530546 'args': opts.args
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530547 },
548 callback: function(r) {
549 if(!r.exc) {
550 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530551 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530552 cur_frm.refresh();
553 }
554 }
555 });
556 }
557 if(opts.source_doctype) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530558 var d = new frappe.ui.form.MultiSelectDialog({
559 doctype: opts.source_doctype,
560 target: opts.target,
561 date_field: opts.date_field || undefined,
562 setters: opts.setters,
563 get_query: opts.get_query,
564 action: function(selections, args) {
565 let values = selections;
566 if(values.length === 0){
Nabin Haita11dcb62017-12-04 13:36:50 +0530567 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530568 return;
569 }
570 opts.source_name = values;
571 opts.setters = args;
572 d.dialog.hide();
573 _map();
574 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530575 });
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530576 } else if(opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530577 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530578 _map();
579 }
580}
581
Rushabh Mehta180e4352016-07-26 17:57:42 +0530582frappe.form.link_formatters['Item'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530583 if(doc && doc.item_name && doc.item_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530584 return value? value + ': ' + doc.item_name: doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530585 } else {
586 return value;
587 }
588}
589
590frappe.form.link_formatters['Employee'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530591 if(doc && doc.employee_name && doc.employee_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530592 return value? value + ': ' + doc.employee_name: doc.employee_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530593 } else {
594 return value;
595 }
596}
597
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530598// add description on posting time
599$(document).on('app_ready', function() {
600 if(!frappe.datetime.is_timezone_same()) {
601 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
602 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
603 frappe.ui.form.on(d, "onload", function(frm) {
604 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530605 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530606 });
607 });
608 }
akshay14384c22016-07-28 13:53:17 +0530609});