blob: 6f43d9ef8c6b01f34659e6bac377596de5fa6518 [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
Marica29a2e162019-11-12 14:43:41 +053066 frappe.model.get_value('Item', {'name':grid_row.doc.item_code}, 'has_serial_no',
67 (data) => {
68 if(data) {
69 grid_row.doc.has_serial_no = data.has_serial_no;
70 me.show_serial_batch_selector(grid_row.frm, grid_row.doc,
71 callback, on_close, true);
72 }
73 }
74 );
Rushabh Mehta62030e02013-08-14 18:37:28 +053075 });
deepeshgarg007e64c3572019-05-14 08:50:45 +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({
Rucha Mahabal65a627c2019-07-17 13:50:32 +0530180 method: "frappe.automation.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
Nabin Hait34c551d2019-07-03 10:34:31 +0530266 create_new_doc: function (doctype, update_fields) {
267 frappe.model.with_doctype(doctype, function() {
268 var new_doc = frappe.model.get_new_doc(doctype);
269 for (let [key, value] of Object.entries(update_fields)) {
270 new_doc[key] = value;
271 }
272 frappe.ui.form.make_quick_entry(doctype, null, null, new_doc);
273 });
274 }
275
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530276});
277
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530278erpnext.utils.select_alternate_items = function(opts) {
279 const frm = opts.frm;
280 const warehouse_field = opts.warehouse_field || 'warehouse';
281 const item_field = opts.item_field || 'item_code';
282
283 this.data = [];
284 const dialog = new frappe.ui.Dialog({
285 title: __("Select Alternate Item"),
286 fields: [
287 {fieldtype:'Section Break', label: __('Items')},
288 {
289 fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true,
290 in_place_edit: true, data: this.data,
291 get_data: () => {
292 return this.data;
293 },
294 fields: [{
295 fieldtype:'Data',
296 fieldname:"docname",
297 hidden: 1
298 }, {
299 fieldtype:'Link',
300 fieldname:"item_code",
301 options: 'Item',
302 in_list_view: 1,
303 read_only: 1,
304 label: __('Item Code')
305 }, {
306 fieldtype:'Link',
307 fieldname:"alternate_item",
308 options: 'Item',
309 default: "",
310 in_list_view: 1,
311 label: __('Alternate Item'),
312 onchange: function() {
313 const item_code = this.get_value();
314 const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value();
315 if (item_code && warehouse) {
316 frappe.call({
317 method: "erpnext.stock.utils.get_latest_stock_qty",
318 args: {
319 item_code: item_code,
320 warehouse: warehouse
321 },
322 callback: (r) => {
323 this.grid_row.on_grid_fields_dict
324 .actual_qty.set_value(r.message || 0);
325 }
326 })
327 }
328 },
329 get_query: (e) => {
330 return {
331 query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items",
332 filters: {
333 item_code: e.item_code
334 }
335 };
336 }
337 }, {
338 fieldtype:'Link',
339 fieldname:"warehouse",
340 options: 'Warehouse',
341 default: "",
342 in_list_view: 1,
343 label: __('Warehouse'),
344 onchange: function() {
345 const warehouse = this.get_value();
346 const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value();
347 if (item_code && warehouse) {
348 frappe.call({
349 method: "erpnext.stock.utils.get_latest_stock_qty",
350 args: {
351 item_code: item_code,
352 warehouse: warehouse
353 },
354 callback: (r) => {
355 this.grid_row.on_grid_fields_dict
356 .actual_qty.set_value(r.message || 0);
357 }
358 })
359 }
360 },
361 }, {
362 fieldtype:'Float',
363 fieldname:"actual_qty",
364 default: 0,
365 read_only: 1,
366 in_list_view: 1,
367 label: __('Available Qty')
368 }]
369 },
370 ],
371 primary_action: function() {
372 const args = this.get_values()["alternative_items"];
373 const alternative_items = args.filter(d => {
374 if (d.alternate_item && d.item_code != d.alternate_item) {
375 return true;
376 }
377 });
378
379 alternative_items.forEach(d => {
380 let row = frappe.get_doc(opts.child_doctype, d.docname);
Doridel Cahanape42192b2018-05-16 13:28:39 +0800381 let qty = null;
382 if (row.doctype === 'Work Order Item') {
383 qty = row.required_qty;
384 } else {
385 qty = row.qty;
386 }
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530387 row[item_field] = d.alternate_item;
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530388 frm.script_manager.trigger(item_field, row.doctype, row.name)
389 .then(() => {
390 frappe.model.set_value(row.doctype, row.name, 'qty', qty);
391 frappe.model.set_value(row.doctype, row.name,
392 opts.original_item_field, d.item_code);
393 });
394 });
395
396 refresh_field(opts.child_docname);
397 this.hide();
398 },
399 primary_action_label: __('Update')
400 });
401
402 frm.doc[opts.child_docname].forEach(d => {
403 if (!opts.condition || opts.condition(d)) {
404 dialog.fields_dict.alternative_items.df.data.push({
405 "docname": d.name,
406 "item_code": d[item_field],
407 "warehouse": d[warehouse_field],
408 "actual_qty": d.actual_qty
409 });
410 }
411 })
412
413 this.data = dialog.fields_dict.alternative_items.df.data;
414 dialog.fields_dict.alternative_items.grid.refresh();
415 dialog.show();
416}
417
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800418erpnext.utils.update_child_items = function(opts) {
419 const frm = opts.frm;
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100420 const cannot_add_row = (typeof opts.cannot_add_row === 'undefined') ? true : opts.cannot_add_row;
421 const child_docname = (typeof opts.cannot_add_row === 'undefined') ? "items" : opts.child_docname;
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800422 this.data = [];
423 const dialog = new frappe.ui.Dialog({
424 title: __("Update Items"),
425 fields: [
426 {fieldtype:'Section Break', label: __('Items')},
427 {
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100428 fieldname: "trans_items",
429 fieldtype: "Table",
430 cannot_add_rows: cannot_add_row,
431 in_place_edit: true,
432 data: this.data,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800433 get_data: () => {
434 return this.data;
435 },
436 fields: [{
437 fieldtype:'Data',
438 fieldname:"docname",
439 hidden: 0,
440 }, {
441 fieldtype:'Link',
442 fieldname:"item_code",
443 options: 'Item',
444 in_list_view: 1,
445 read_only: 1,
446 label: __('Item Code')
447 }, {
448 fieldtype:'Float',
449 fieldname:"qty",
450 default: 0,
451 read_only: 0,
452 in_list_view: 1,
453 label: __('Qty')
454 }, {
455 fieldtype:'Currency',
456 fieldname:"rate",
457 default: 0,
458 read_only: 0,
459 in_list_view: 1,
460 label: __('Rate')
461 }]
462 },
463 ],
464 primary_action: function() {
465 const trans_items = this.get_values()["trans_items"];
466 frappe.call({
467 method: 'erpnext.controllers.accounts_controller.update_child_qty_rate',
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100468 freeze: true,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800469 args: {
470 'parent_doctype': frm.doc.doctype,
471 'trans_items': trans_items,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100472 'parent_doctype_name': frm.doc.name,
473 'child_docname': child_docname
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800474 },
475 callback: function() {
476 frm.reload_doc();
477 }
478 });
479 this.hide();
480 refresh_field("items");
481 },
482 primary_action_label: __('Update')
483 });
484
485 frm.doc[opts.child_docname].forEach(d => {
486 dialog.fields_dict.trans_items.df.data.push({
487 "docname": d.name,
488 "item_code": d.item_code,
489 "qty": d.qty,
490 "rate": d.rate,
491 });
492 this.data = dialog.fields_dict.trans_items.df.data;
493 dialog.fields_dict.trans_items.grid.refresh();
494 })
495 dialog.show();
496}
497
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530498erpnext.utils.map_current_doc = function(opts) {
499 if(opts.get_query_filters) {
500 opts.get_query = function() {
501 return {filters: opts.get_query_filters};
502 }
503 }
504 var _map = function() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530505 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530506 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530507 if(!cur_frm.doc.items[0].item_code) {
508 cur_frm.doc.items = cur_frm.doc.items.splice(1);
509 }
bhupen3c7e70f2016-11-30 14:32:11 +0530510
511 // find the doctype of the items table
512 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530513
bhupen3c7e70f2016-11-30 14:32:11 +0530514 // find the link fieldname from items table for the given
515 // source_doctype
516 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530517 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530518 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
519
Nabin Hait802b4352017-01-09 15:32:20 +0530520 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530521 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530522 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530523
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530524 $.each(cur_frm.doc.items, function(i, d) {
525 opts.source_name.forEach(function(src) {
526 if(d[link_fieldname]==src) {
527 already_set = true;
528 if (item_qty_map[d.item_code])
529 item_qty_map[d.item_code] += flt(d.qty);
530 else
531 item_qty_map[d.item_code] = flt(d.qty);
532 }
533 });
534 });
535
536 if(already_set) {
537 opts.source_name.forEach(function(src) {
538 frappe.model.with_doc(opts.source_doctype, src, function(r) {
539 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
540 $.each(source_doc.items || [], function(i, row) {
541 if(row.qty > flt(item_qty_map[row.item_code])) {
542 already_set = false;
543 return false;
544 }
545 })
546 })
547
548 if(already_set) {
549 frappe.msgprint(__("You have already selected items from {0} {1}",
550 [opts.source_doctype, src]));
551 return;
552 }
553
554 })
Nabin Hait802b4352017-01-09 15:32:20 +0530555 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530556 }
557
558 return frappe.call({
559 // Sometimes we hit the limit for URL length of a GET request
560 // as we send the full target_doc. Hence this is a POST request.
561 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530562 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530563 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530564 "method": opts.method,
565 "source_names": opts.source_name,
566 "target_doc": cur_frm.doc,
Rushabh Mehta49f97472018-08-30 18:50:48 +0530567 'args': opts.args
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530568 },
569 callback: function(r) {
570 if(!r.exc) {
571 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530572 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530573 cur_frm.refresh();
574 }
575 }
576 });
577 }
578 if(opts.source_doctype) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530579 var d = new frappe.ui.form.MultiSelectDialog({
580 doctype: opts.source_doctype,
581 target: opts.target,
582 date_field: opts.date_field || undefined,
583 setters: opts.setters,
584 get_query: opts.get_query,
585 action: function(selections, args) {
586 let values = selections;
587 if(values.length === 0){
Nabin Haita11dcb62017-12-04 13:36:50 +0530588 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530589 return;
590 }
591 opts.source_name = values;
592 opts.setters = args;
593 d.dialog.hide();
594 _map();
595 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530596 });
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530597 } else if(opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530598 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530599 _map();
600 }
601}
602
Rushabh Mehta180e4352016-07-26 17:57:42 +0530603frappe.form.link_formatters['Item'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530604 if(doc && doc.item_name && doc.item_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530605 return value? value + ': ' + doc.item_name: doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530606 } else {
607 return value;
608 }
609}
610
611frappe.form.link_formatters['Employee'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530612 if(doc && doc.employee_name && doc.employee_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530613 return value? value + ': ' + doc.employee_name: doc.employee_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530614 } else {
615 return value;
616 }
617}
618
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530619// add description on posting time
620$(document).on('app_ready', function() {
621 if(!frappe.datetime.is_timezone_same()) {
622 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
623 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
624 frappe.ui.form.on(d, "onload", function(frm) {
625 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530626 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530627 });
628 });
629 }
akshay14384c22016-07-28 13:53:17 +0530630});