blob: ffc5e6ad36519f3bcad1b12fba261ce383ccc38d [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() {
Rohit Waghchauredadd0492019-08-02 19:11:31 +053063 let callback = '';
64 let on_close = '';
65
66 if (grid_row.doc.serial_no) {
67 grid_row.doc.has_serial_no = true;
68 }
69
70 me.show_serial_batch_selector(grid_row.frm, grid_row.doc,
71 callback, on_close, true);
Rushabh Mehta62030e02013-08-14 18:37:28 +053072 });
deepeshgarg007e64c3572019-05-14 08:50:45 +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({
Rucha Mahabal65a627c2019-07-17 13:50:32 +0530177 method: "frappe.automation.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
Nabin Hait34c551d2019-07-03 10:34:31 +0530263 create_new_doc: function (doctype, update_fields) {
264 frappe.model.with_doctype(doctype, function() {
265 var new_doc = frappe.model.get_new_doc(doctype);
266 for (let [key, value] of Object.entries(update_fields)) {
267 new_doc[key] = value;
268 }
269 frappe.ui.form.make_quick_entry(doctype, null, null, new_doc);
270 });
271 }
272
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530273});
274
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530275erpnext.utils.select_alternate_items = function(opts) {
276 const frm = opts.frm;
277 const warehouse_field = opts.warehouse_field || 'warehouse';
278 const item_field = opts.item_field || 'item_code';
279
280 this.data = [];
281 const dialog = new frappe.ui.Dialog({
282 title: __("Select Alternate Item"),
283 fields: [
284 {fieldtype:'Section Break', label: __('Items')},
285 {
286 fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true,
287 in_place_edit: true, data: this.data,
288 get_data: () => {
289 return this.data;
290 },
291 fields: [{
292 fieldtype:'Data',
293 fieldname:"docname",
294 hidden: 1
295 }, {
296 fieldtype:'Link',
297 fieldname:"item_code",
298 options: 'Item',
299 in_list_view: 1,
300 read_only: 1,
301 label: __('Item Code')
302 }, {
303 fieldtype:'Link',
304 fieldname:"alternate_item",
305 options: 'Item',
306 default: "",
307 in_list_view: 1,
308 label: __('Alternate Item'),
309 onchange: function() {
310 const item_code = this.get_value();
311 const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value();
312 if (item_code && warehouse) {
313 frappe.call({
314 method: "erpnext.stock.utils.get_latest_stock_qty",
315 args: {
316 item_code: item_code,
317 warehouse: warehouse
318 },
319 callback: (r) => {
320 this.grid_row.on_grid_fields_dict
321 .actual_qty.set_value(r.message || 0);
322 }
323 })
324 }
325 },
326 get_query: (e) => {
327 return {
328 query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items",
329 filters: {
330 item_code: e.item_code
331 }
332 };
333 }
334 }, {
335 fieldtype:'Link',
336 fieldname:"warehouse",
337 options: 'Warehouse',
338 default: "",
339 in_list_view: 1,
340 label: __('Warehouse'),
341 onchange: function() {
342 const warehouse = this.get_value();
343 const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value();
344 if (item_code && warehouse) {
345 frappe.call({
346 method: "erpnext.stock.utils.get_latest_stock_qty",
347 args: {
348 item_code: item_code,
349 warehouse: warehouse
350 },
351 callback: (r) => {
352 this.grid_row.on_grid_fields_dict
353 .actual_qty.set_value(r.message || 0);
354 }
355 })
356 }
357 },
358 }, {
359 fieldtype:'Float',
360 fieldname:"actual_qty",
361 default: 0,
362 read_only: 1,
363 in_list_view: 1,
364 label: __('Available Qty')
365 }]
366 },
367 ],
368 primary_action: function() {
369 const args = this.get_values()["alternative_items"];
370 const alternative_items = args.filter(d => {
371 if (d.alternate_item && d.item_code != d.alternate_item) {
372 return true;
373 }
374 });
375
376 alternative_items.forEach(d => {
377 let row = frappe.get_doc(opts.child_doctype, d.docname);
Doridel Cahanape42192b2018-05-16 13:28:39 +0800378 let qty = null;
379 if (row.doctype === 'Work Order Item') {
380 qty = row.required_qty;
381 } else {
382 qty = row.qty;
383 }
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530384 row[item_field] = d.alternate_item;
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530385 frm.script_manager.trigger(item_field, row.doctype, row.name)
386 .then(() => {
387 frappe.model.set_value(row.doctype, row.name, 'qty', qty);
388 frappe.model.set_value(row.doctype, row.name,
389 opts.original_item_field, d.item_code);
390 });
391 });
392
393 refresh_field(opts.child_docname);
394 this.hide();
395 },
396 primary_action_label: __('Update')
397 });
398
399 frm.doc[opts.child_docname].forEach(d => {
400 if (!opts.condition || opts.condition(d)) {
401 dialog.fields_dict.alternative_items.df.data.push({
402 "docname": d.name,
403 "item_code": d[item_field],
404 "warehouse": d[warehouse_field],
405 "actual_qty": d.actual_qty
406 });
407 }
408 })
409
410 this.data = dialog.fields_dict.alternative_items.df.data;
411 dialog.fields_dict.alternative_items.grid.refresh();
412 dialog.show();
413}
414
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800415erpnext.utils.update_child_items = function(opts) {
416 const frm = opts.frm;
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100417 const cannot_add_row = (typeof opts.cannot_add_row === 'undefined') ? true : opts.cannot_add_row;
418 const child_docname = (typeof opts.cannot_add_row === 'undefined') ? "items" : opts.child_docname;
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800419 this.data = [];
420 const dialog = new frappe.ui.Dialog({
421 title: __("Update Items"),
422 fields: [
423 {fieldtype:'Section Break', label: __('Items')},
424 {
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100425 fieldname: "trans_items",
426 fieldtype: "Table",
427 cannot_add_rows: cannot_add_row,
428 in_place_edit: true,
429 data: this.data,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800430 get_data: () => {
431 return this.data;
432 },
433 fields: [{
434 fieldtype:'Data',
435 fieldname:"docname",
436 hidden: 0,
437 }, {
438 fieldtype:'Link',
439 fieldname:"item_code",
440 options: 'Item',
441 in_list_view: 1,
442 read_only: 1,
443 label: __('Item Code')
444 }, {
445 fieldtype:'Float',
446 fieldname:"qty",
447 default: 0,
448 read_only: 0,
449 in_list_view: 1,
450 label: __('Qty')
451 }, {
452 fieldtype:'Currency',
453 fieldname:"rate",
454 default: 0,
455 read_only: 0,
456 in_list_view: 1,
457 label: __('Rate')
458 }]
459 },
460 ],
461 primary_action: function() {
462 const trans_items = this.get_values()["trans_items"];
463 frappe.call({
464 method: 'erpnext.controllers.accounts_controller.update_child_qty_rate',
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100465 freeze: true,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800466 args: {
467 'parent_doctype': frm.doc.doctype,
468 'trans_items': trans_items,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100469 'parent_doctype_name': frm.doc.name,
470 'child_docname': child_docname
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800471 },
472 callback: function() {
473 frm.reload_doc();
474 }
475 });
476 this.hide();
477 refresh_field("items");
478 },
479 primary_action_label: __('Update')
480 });
481
482 frm.doc[opts.child_docname].forEach(d => {
483 dialog.fields_dict.trans_items.df.data.push({
484 "docname": d.name,
485 "item_code": d.item_code,
486 "qty": d.qty,
487 "rate": d.rate,
488 });
489 this.data = dialog.fields_dict.trans_items.df.data;
490 dialog.fields_dict.trans_items.grid.refresh();
491 })
492 dialog.show();
493}
494
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530495erpnext.utils.map_current_doc = function(opts) {
496 if(opts.get_query_filters) {
497 opts.get_query = function() {
498 return {filters: opts.get_query_filters};
499 }
500 }
501 var _map = function() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530502 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530503 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530504 if(!cur_frm.doc.items[0].item_code) {
505 cur_frm.doc.items = cur_frm.doc.items.splice(1);
506 }
bhupen3c7e70f2016-11-30 14:32:11 +0530507
508 // find the doctype of the items table
509 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530510
bhupen3c7e70f2016-11-30 14:32:11 +0530511 // find the link fieldname from items table for the given
512 // source_doctype
513 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530514 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530515 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
516
Nabin Hait802b4352017-01-09 15:32:20 +0530517 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530518 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530519 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530520
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530521 $.each(cur_frm.doc.items, function(i, d) {
522 opts.source_name.forEach(function(src) {
523 if(d[link_fieldname]==src) {
524 already_set = true;
525 if (item_qty_map[d.item_code])
526 item_qty_map[d.item_code] += flt(d.qty);
527 else
528 item_qty_map[d.item_code] = flt(d.qty);
529 }
530 });
531 });
532
533 if(already_set) {
534 opts.source_name.forEach(function(src) {
535 frappe.model.with_doc(opts.source_doctype, src, function(r) {
536 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
537 $.each(source_doc.items || [], function(i, row) {
538 if(row.qty > flt(item_qty_map[row.item_code])) {
539 already_set = false;
540 return false;
541 }
542 })
543 })
544
545 if(already_set) {
546 frappe.msgprint(__("You have already selected items from {0} {1}",
547 [opts.source_doctype, src]));
548 return;
549 }
550
551 })
Nabin Hait802b4352017-01-09 15:32:20 +0530552 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530553 }
554
555 return frappe.call({
556 // Sometimes we hit the limit for URL length of a GET request
557 // as we send the full target_doc. Hence this is a POST request.
558 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530559 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530560 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530561 "method": opts.method,
562 "source_names": opts.source_name,
563 "target_doc": cur_frm.doc,
Rushabh Mehta49f97472018-08-30 18:50:48 +0530564 'args': opts.args
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530565 },
566 callback: function(r) {
567 if(!r.exc) {
568 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530569 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530570 cur_frm.refresh();
571 }
572 }
573 });
574 }
575 if(opts.source_doctype) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530576 var d = new frappe.ui.form.MultiSelectDialog({
577 doctype: opts.source_doctype,
578 target: opts.target,
579 date_field: opts.date_field || undefined,
580 setters: opts.setters,
581 get_query: opts.get_query,
582 action: function(selections, args) {
583 let values = selections;
584 if(values.length === 0){
Nabin Haita11dcb62017-12-04 13:36:50 +0530585 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530586 return;
587 }
588 opts.source_name = values;
589 opts.setters = args;
590 d.dialog.hide();
591 _map();
592 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530593 });
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530594 } else if(opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530595 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530596 _map();
597 }
598}
599
Rushabh Mehta180e4352016-07-26 17:57:42 +0530600frappe.form.link_formatters['Item'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530601 if(doc && doc.item_name && doc.item_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530602 return value? value + ': ' + doc.item_name: doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530603 } else {
604 return value;
605 }
606}
607
608frappe.form.link_formatters['Employee'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530609 if(doc && doc.employee_name && doc.employee_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530610 return value? value + ': ' + doc.employee_name: doc.employee_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530611 } else {
612 return value;
613 }
614}
615
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530616// add description on posting time
617$(document).on('app_ready', function() {
618 if(!frappe.datetime.is_timezone_same()) {
619 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
620 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
621 frappe.ui.form.on(d, "onload", function(frm) {
622 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530623 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530624 });
625 });
626 }
akshay14384c22016-07-28 13:53:17 +0530627});