blob: f44fb0c1a67fe94e07dae993d237d2dceeb1bfa0 [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'],
deepeshgarg007e9c6e642019-05-26 12:34:13 +053070 filters: {
71 disabled: 0
72 }
deepeshgarg007e64c3572019-05-14 08:50:45 +053073 });
74
75 return dimensions;
Rushabh Mehta5495bc52015-05-19 12:00:34 +053076 }
Rushabh Mehtac38fc712014-04-16 17:21:25 +053077});
Rushabh Mehta7d368752014-11-24 14:16:47 +053078
Nabin Haitb072c742014-12-16 12:49:13 +053079
80$.extend(erpnext.utils, {
Rushabh Mehta559aa3a2016-09-19 16:04:58 +053081 set_party_dashboard_indicators: function(frm) {
82 if(frm.doc.__onload && frm.doc.__onload.dashboard_info) {
deepeshgarg007920dc142018-11-23 10:17:28 +053083 var company_wise_info = frm.doc.__onload.dashboard_info;
deepeshgarg007f31caff2018-11-27 15:04:12 +053084 if(company_wise_info.length > 1) {
deepeshgarg007a1cffc32018-11-23 16:51:35 +053085 company_wise_info.forEach(function(info) {
deepeshgarg00712f5cef2018-12-25 16:06:19 +053086 erpnext.utils.add_indicator_for_multicompany(frm, info);
deepeshgarg007a1cffc32018-11-23 16:51:35 +053087 });
deepeshgarg00712f5cef2018-12-25 16:06:19 +053088 } else if (company_wise_info.length === 1) {
deepeshgarg00764238ee2018-12-25 16:28:39 +053089 frm.dashboard.add_indicator(__('Annual Billing: {0}',
90 [format_currency(company_wise_info[0].billing_this_year, company_wise_info[0].currency)]), 'blue');
91 frm.dashboard.add_indicator(__('Total Unpaid: {0}',
92 [format_currency(company_wise_info[0].total_unpaid, company_wise_info[0].currency)]),
deepeshgarg0078300f5e2019-01-01 20:28:49 +053093 company_wise_info[0].total_unpaid ? 'orange' : 'green');
deepeshgarg00712f5cef2018-12-25 16:06:19 +053094
deepeshgarg00764238ee2018-12-25 16:28:39 +053095 if(company_wise_info[0].loyalty_points) {
96 frm.dashboard.add_indicator(__('Loyalty Points: {0}',
97 [company_wise_info[0].loyalty_points]), 'blue');
98 }
deepeshgarg007f31caff2018-11-27 15:04:12 +053099 }
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530100 }
101 },
102
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530103 add_indicator_for_multicompany: function(frm, info) {
104 frm.dashboard.stats_area.removeClass('hidden');
105 frm.dashboard.stats_area_row.addClass('flex');
106 frm.dashboard.stats_area_row.css('flex-wrap', 'wrap');
107
108 var color = info.total_unpaid ? 'orange' : 'green';
109
110 var indicator = $('<div class="flex-column col-xs-6">'+
111 '<div style="margin-top:10px"><h6>'+info.company+'</h6></div>'+
112
113 '<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
114 'Annual Billing: '+format_currency(info.billing_this_year, info.currency)+'</span></div>'+
115
116 '<div class="badge-link small" style="margin-bottom:10px">'+
117 '<span class="indicator '+color+'">Total Unpaid: '
118 +format_currency(info.total_unpaid, info.currency)+'</span></div>'+
119
120
121 '</div>').appendTo(frm.dashboard.stats_area_row);
122
123 if(info.loyalty_points){
124 $('<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
125 'Loyalty Points: '+info.loyalty_points+'</span></div>').appendTo(indicator);
126 }
127
128 return indicator;
129 },
130
Shreya Shah149f7ee2018-03-27 11:29:25 +0530131 get_party_name: function(party_type) {
132 var dict = {'Customer': 'customer_name', 'Supplier': 'supplier_name', 'Employee': 'employee_name',
133 'Member': 'member_name'};
134 return dict[party_type];
135 },
136
Rushabh Mehta49f97472018-08-30 18:50:48 +0530137 copy_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname) {
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +0530138 var d = locals[dt][dn];
139 if(d[fieldname]){
140 var cl = doc[table_fieldname] || [];
141 for(var i = 0; i < cl.length; i++) {
142 if(!cl[i][fieldname]) cl[i][fieldname] = d[fieldname];
143 }
144 }
145 refresh_field(table_fieldname);
Makarand Bauskar157c3342017-05-26 21:32:33 +0530146 },
147
148 get_terms: function(tc_name, doc, callback) {
149 if(tc_name) {
150 return frappe.call({
151 method: 'erpnext.setup.doctype.terms_and_conditions.terms_and_conditions.get_terms_and_conditions',
152 args: {
153 template_name: tc_name,
154 doc: doc
155 },
156 callback: function(r) {
157 callback(r)
158 }
159 });
160 }
tundebabzy1a947db2017-08-29 13:48:27 +0100161 },
162
rohitwaghchaure713cfc72018-09-11 17:40:37 +0530163 make_bank_account: function(doctype, docname) {
164 frappe.call({
165 method: "erpnext.accounts.doctype.bank_account.bank_account.make_bank_account",
166 args: {
167 doctype: doctype,
168 docname: docname
169 },
170 freeze: true,
171 callback: function(r) {
172 var doclist = frappe.model.sync(r.message);
173 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
174 }
175 })
176 },
177
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530178 make_subscription: function(doctype, docname) {
179 frappe.call({
rohitwaghchaure79e5b072018-06-21 12:58:14 +0530180 method: "frappe.desk.doctype.auto_repeat.auto_repeat.make_auto_repeat",
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530181 args: {
182 doctype: doctype,
183 docname: docname
184 },
185 callback: function(r) {
186 var doclist = frappe.model.sync(r.message);
187 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
188 }
189 })
190 },
191
Saif90cf2dd2018-09-30 21:46:31 +0500192 make_pricing_rule: function(doctype, docname) {
193 frappe.call({
194 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.make_pricing_rule",
195 args: {
196 doctype: doctype,
197 docname: docname
198 },
199 callback: function(r) {
200 var doclist = frappe.model.sync(r.message);
201 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
202 }
203 })
204 },
205
tundebabzy1a947db2017-08-29 13:48:27 +0100206 /**
207 * Checks if the first row of a given child table is empty
208 * @param child_table - Child table Doctype
209 * @return {Boolean}
210 **/
211 first_row_is_empty: function(child_table){
212 if($.isArray(child_table) && child_table.length > 0) {
213 return !child_table[0].item_code;
214 }
215 return false;
216 },
217
218 /**
219 * Removes the first row of a child table if it is empty
220 * @param {_Frm} frm - The current form
221 * @param {String} child_table_name - The child table field name
222 * @return {Boolean}
223 **/
224 remove_empty_first_row: function(frm, child_table_name){
225 const rows = frm['doc'][child_table_name];
226 if (this.first_row_is_empty(rows)){
227 frm['doc'][child_table_name] = rows.splice(1);
228 }
229 return rows;
230 },
Zarrar6ffdf942018-06-14 12:24:16 +0530231 get_tree_options: function(option) {
232 // get valid options for tree based on user permission & locals dict
233 let unscrub_option = frappe.model.unscrub(option);
234 let user_permission = frappe.defaults.get_user_permissions();
deepeshgarg0070d64d622019-02-20 17:55:14 +0530235 let options;
236
Zarrar6ffdf942018-06-14 12:24:16 +0530237 if(user_permission && user_permission[unscrub_option]) {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530238 options = user_permission[unscrub_option].map(perm => perm.doc);
Zarrar6ffdf942018-06-14 12:24:16 +0530239 } else {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530240 options = $.map(locals[`:${unscrub_option}`], function(c) { return c.name; }).sort();
Zarrar6ffdf942018-06-14 12:24:16 +0530241 }
deepeshgarg0070d64d622019-02-20 17:55:14 +0530242
243 // filter unique values, as there may be multiple user permissions for any value
244 return options.filter((value, index, self) => self.indexOf(value) === index);
Zarrar6ffdf942018-06-14 12:24:16 +0530245 },
246 get_tree_default: function(option) {
247 // set default for a field based on user permission
248 let options = this.get_tree_options(option);
249 if(options.includes(frappe.defaults.get_default(option))) {
250 return frappe.defaults.get_default(option);
251 } else {
252 return options[0];
253 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400254 },
255 copy_parent_value_in_all_row: function(doc, dt, dn, table_fieldname, fieldname, parent_fieldname) {
256 var d = locals[dt][dn];
257 if(d[fieldname]){
258 var cl = doc[table_fieldname] || [];
259 for(var i = 0; i < cl.length; i++) {
260 cl[i][fieldname] = doc[parent_fieldname];
261 }
262 }
263 refresh_field(table_fieldname);
264 },
265
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530266});
267
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530268erpnext.utils.select_alternate_items = function(opts) {
269 const frm = opts.frm;
270 const warehouse_field = opts.warehouse_field || 'warehouse';
271 const item_field = opts.item_field || 'item_code';
272
273 this.data = [];
274 const dialog = new frappe.ui.Dialog({
275 title: __("Select Alternate Item"),
276 fields: [
277 {fieldtype:'Section Break', label: __('Items')},
278 {
279 fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true,
280 in_place_edit: true, data: this.data,
281 get_data: () => {
282 return this.data;
283 },
284 fields: [{
285 fieldtype:'Data',
286 fieldname:"docname",
287 hidden: 1
288 }, {
289 fieldtype:'Link',
290 fieldname:"item_code",
291 options: 'Item',
292 in_list_view: 1,
293 read_only: 1,
294 label: __('Item Code')
295 }, {
296 fieldtype:'Link',
297 fieldname:"alternate_item",
298 options: 'Item',
299 default: "",
300 in_list_view: 1,
301 label: __('Alternate Item'),
302 onchange: function() {
303 const item_code = this.get_value();
304 const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value();
305 if (item_code && warehouse) {
306 frappe.call({
307 method: "erpnext.stock.utils.get_latest_stock_qty",
308 args: {
309 item_code: item_code,
310 warehouse: warehouse
311 },
312 callback: (r) => {
313 this.grid_row.on_grid_fields_dict
314 .actual_qty.set_value(r.message || 0);
315 }
316 })
317 }
318 },
319 get_query: (e) => {
320 return {
321 query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items",
322 filters: {
323 item_code: e.item_code
324 }
325 };
326 }
327 }, {
328 fieldtype:'Link',
329 fieldname:"warehouse",
330 options: 'Warehouse',
331 default: "",
332 in_list_view: 1,
333 label: __('Warehouse'),
334 onchange: function() {
335 const warehouse = this.get_value();
336 const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value();
337 if (item_code && warehouse) {
338 frappe.call({
339 method: "erpnext.stock.utils.get_latest_stock_qty",
340 args: {
341 item_code: item_code,
342 warehouse: warehouse
343 },
344 callback: (r) => {
345 this.grid_row.on_grid_fields_dict
346 .actual_qty.set_value(r.message || 0);
347 }
348 })
349 }
350 },
351 }, {
352 fieldtype:'Float',
353 fieldname:"actual_qty",
354 default: 0,
355 read_only: 1,
356 in_list_view: 1,
357 label: __('Available Qty')
358 }]
359 },
360 ],
361 primary_action: function() {
362 const args = this.get_values()["alternative_items"];
363 const alternative_items = args.filter(d => {
364 if (d.alternate_item && d.item_code != d.alternate_item) {
365 return true;
366 }
367 });
368
369 alternative_items.forEach(d => {
370 let row = frappe.get_doc(opts.child_doctype, d.docname);
Doridel Cahanape42192b2018-05-16 13:28:39 +0800371 let qty = null;
372 if (row.doctype === 'Work Order Item') {
373 qty = row.required_qty;
374 } else {
375 qty = row.qty;
376 }
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530377 row[item_field] = d.alternate_item;
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530378 frm.script_manager.trigger(item_field, row.doctype, row.name)
379 .then(() => {
380 frappe.model.set_value(row.doctype, row.name, 'qty', qty);
381 frappe.model.set_value(row.doctype, row.name,
382 opts.original_item_field, d.item_code);
383 });
384 });
385
386 refresh_field(opts.child_docname);
387 this.hide();
388 },
389 primary_action_label: __('Update')
390 });
391
392 frm.doc[opts.child_docname].forEach(d => {
393 if (!opts.condition || opts.condition(d)) {
394 dialog.fields_dict.alternative_items.df.data.push({
395 "docname": d.name,
396 "item_code": d[item_field],
397 "warehouse": d[warehouse_field],
398 "actual_qty": d.actual_qty
399 });
400 }
401 })
402
403 this.data = dialog.fields_dict.alternative_items.df.data;
404 dialog.fields_dict.alternative_items.grid.refresh();
405 dialog.show();
406}
407
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800408erpnext.utils.update_child_items = function(opts) {
409 const frm = opts.frm;
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100410 const cannot_add_row = (typeof opts.cannot_add_row === 'undefined') ? true : opts.cannot_add_row;
411 const child_docname = (typeof opts.cannot_add_row === 'undefined') ? "items" : opts.child_docname;
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800412 this.data = [];
413 const dialog = new frappe.ui.Dialog({
414 title: __("Update Items"),
415 fields: [
416 {fieldtype:'Section Break', label: __('Items')},
417 {
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100418 fieldname: "trans_items",
419 fieldtype: "Table",
420 cannot_add_rows: cannot_add_row,
421 in_place_edit: true,
422 data: this.data,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800423 get_data: () => {
424 return this.data;
425 },
426 fields: [{
427 fieldtype:'Data',
428 fieldname:"docname",
429 hidden: 0,
430 }, {
431 fieldtype:'Link',
432 fieldname:"item_code",
433 options: 'Item',
434 in_list_view: 1,
435 read_only: 1,
436 label: __('Item Code')
437 }, {
438 fieldtype:'Float',
439 fieldname:"qty",
440 default: 0,
441 read_only: 0,
442 in_list_view: 1,
443 label: __('Qty')
444 }, {
445 fieldtype:'Currency',
446 fieldname:"rate",
447 default: 0,
448 read_only: 0,
449 in_list_view: 1,
450 label: __('Rate')
451 }]
452 },
453 ],
454 primary_action: function() {
455 const trans_items = this.get_values()["trans_items"];
456 frappe.call({
457 method: 'erpnext.controllers.accounts_controller.update_child_qty_rate',
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100458 freeze: true,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800459 args: {
460 'parent_doctype': frm.doc.doctype,
461 'trans_items': trans_items,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100462 'parent_doctype_name': frm.doc.name,
463 'child_docname': child_docname
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800464 },
465 callback: function() {
466 frm.reload_doc();
467 }
468 });
469 this.hide();
470 refresh_field("items");
471 },
472 primary_action_label: __('Update')
473 });
474
475 frm.doc[opts.child_docname].forEach(d => {
476 dialog.fields_dict.trans_items.df.data.push({
477 "docname": d.name,
478 "item_code": d.item_code,
479 "qty": d.qty,
480 "rate": d.rate,
481 });
482 this.data = dialog.fields_dict.trans_items.df.data;
483 dialog.fields_dict.trans_items.grid.refresh();
484 })
485 dialog.show();
486}
487
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530488erpnext.utils.map_current_doc = function(opts) {
489 if(opts.get_query_filters) {
490 opts.get_query = function() {
491 return {filters: opts.get_query_filters};
492 }
493 }
494 var _map = function() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530495 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530496 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530497 if(!cur_frm.doc.items[0].item_code) {
498 cur_frm.doc.items = cur_frm.doc.items.splice(1);
499 }
bhupen3c7e70f2016-11-30 14:32:11 +0530500
501 // find the doctype of the items table
502 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530503
bhupen3c7e70f2016-11-30 14:32:11 +0530504 // find the link fieldname from items table for the given
505 // source_doctype
506 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530507 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530508 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
509
Nabin Hait802b4352017-01-09 15:32:20 +0530510 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530511 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530512 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530513
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530514 $.each(cur_frm.doc.items, function(i, d) {
515 opts.source_name.forEach(function(src) {
516 if(d[link_fieldname]==src) {
517 already_set = true;
518 if (item_qty_map[d.item_code])
519 item_qty_map[d.item_code] += flt(d.qty);
520 else
521 item_qty_map[d.item_code] = flt(d.qty);
522 }
523 });
524 });
525
526 if(already_set) {
527 opts.source_name.forEach(function(src) {
528 frappe.model.with_doc(opts.source_doctype, src, function(r) {
529 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
530 $.each(source_doc.items || [], function(i, row) {
531 if(row.qty > flt(item_qty_map[row.item_code])) {
532 already_set = false;
533 return false;
534 }
535 })
536 })
537
538 if(already_set) {
539 frappe.msgprint(__("You have already selected items from {0} {1}",
540 [opts.source_doctype, src]));
541 return;
542 }
543
544 })
Nabin Hait802b4352017-01-09 15:32:20 +0530545 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530546 }
547
548 return frappe.call({
549 // Sometimes we hit the limit for URL length of a GET request
550 // as we send the full target_doc. Hence this is a POST request.
551 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530552 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530553 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530554 "method": opts.method,
555 "source_names": opts.source_name,
556 "target_doc": cur_frm.doc,
Rushabh Mehta49f97472018-08-30 18:50:48 +0530557 'args': opts.args
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530558 },
559 callback: function(r) {
560 if(!r.exc) {
561 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530562 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530563 cur_frm.refresh();
564 }
565 }
566 });
567 }
568 if(opts.source_doctype) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530569 var d = new frappe.ui.form.MultiSelectDialog({
570 doctype: opts.source_doctype,
571 target: opts.target,
572 date_field: opts.date_field || undefined,
573 setters: opts.setters,
574 get_query: opts.get_query,
575 action: function(selections, args) {
576 let values = selections;
577 if(values.length === 0){
Nabin Haita11dcb62017-12-04 13:36:50 +0530578 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530579 return;
580 }
581 opts.source_name = values;
582 opts.setters = args;
583 d.dialog.hide();
584 _map();
585 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530586 });
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530587 } else if(opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530588 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530589 _map();
590 }
591}
592
Rushabh Mehta180e4352016-07-26 17:57:42 +0530593frappe.form.link_formatters['Item'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530594 if(doc && doc.item_name && doc.item_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530595 return value? value + ': ' + doc.item_name: doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530596 } else {
597 return value;
598 }
599}
600
601frappe.form.link_formatters['Employee'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530602 if(doc && doc.employee_name && doc.employee_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530603 return value? value + ': ' + doc.employee_name: doc.employee_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530604 } else {
605 return value;
606 }
607}
608
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530609// add description on posting time
610$(document).on('app_ready', function() {
611 if(!frappe.datetime.is_timezone_same()) {
612 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
613 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
614 frappe.ui.form.on(d, "onload", function(frm) {
615 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530616 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530617 });
618 });
619 }
akshay14384c22016-07-28 13:53:17 +0530620});