blob: 0a363a04fdee449735962a18aee82d66650ab82e [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 },
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({
Rucha Mahabal65a627c2019-07-17 13:50:32 +0530169 method: "frappe.automation.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
Nabin Hait34c551d2019-07-03 10:34:31 +0530255 create_new_doc: function (doctype, update_fields) {
256 frappe.model.with_doctype(doctype, function() {
257 var new_doc = frappe.model.get_new_doc(doctype);
258 for (let [key, value] of Object.entries(update_fields)) {
259 new_doc[key] = value;
260 }
261 frappe.ui.form.make_quick_entry(doctype, null, null, new_doc);
262 });
263 }
264
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530265});
266
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530267erpnext.utils.select_alternate_items = function(opts) {
268 const frm = opts.frm;
269 const warehouse_field = opts.warehouse_field || 'warehouse';
270 const item_field = opts.item_field || 'item_code';
271
272 this.data = [];
273 const dialog = new frappe.ui.Dialog({
274 title: __("Select Alternate Item"),
275 fields: [
276 {fieldtype:'Section Break', label: __('Items')},
277 {
278 fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true,
279 in_place_edit: true, data: this.data,
280 get_data: () => {
281 return this.data;
282 },
283 fields: [{
284 fieldtype:'Data',
285 fieldname:"docname",
286 hidden: 1
287 }, {
288 fieldtype:'Link',
289 fieldname:"item_code",
290 options: 'Item',
291 in_list_view: 1,
292 read_only: 1,
293 label: __('Item Code')
294 }, {
295 fieldtype:'Link',
296 fieldname:"alternate_item",
297 options: 'Item',
298 default: "",
299 in_list_view: 1,
300 label: __('Alternate Item'),
301 onchange: function() {
302 const item_code = this.get_value();
303 const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value();
304 if (item_code && warehouse) {
305 frappe.call({
306 method: "erpnext.stock.utils.get_latest_stock_qty",
307 args: {
308 item_code: item_code,
309 warehouse: warehouse
310 },
311 callback: (r) => {
312 this.grid_row.on_grid_fields_dict
313 .actual_qty.set_value(r.message || 0);
314 }
315 })
316 }
317 },
318 get_query: (e) => {
319 return {
320 query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items",
321 filters: {
322 item_code: e.item_code
323 }
324 };
325 }
326 }, {
327 fieldtype:'Link',
328 fieldname:"warehouse",
329 options: 'Warehouse',
330 default: "",
331 in_list_view: 1,
332 label: __('Warehouse'),
333 onchange: function() {
334 const warehouse = this.get_value();
335 const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value();
336 if (item_code && warehouse) {
337 frappe.call({
338 method: "erpnext.stock.utils.get_latest_stock_qty",
339 args: {
340 item_code: item_code,
341 warehouse: warehouse
342 },
343 callback: (r) => {
344 this.grid_row.on_grid_fields_dict
345 .actual_qty.set_value(r.message || 0);
346 }
347 })
348 }
349 },
350 }, {
351 fieldtype:'Float',
352 fieldname:"actual_qty",
353 default: 0,
354 read_only: 1,
355 in_list_view: 1,
356 label: __('Available Qty')
357 }]
358 },
359 ],
360 primary_action: function() {
361 const args = this.get_values()["alternative_items"];
362 const alternative_items = args.filter(d => {
363 if (d.alternate_item && d.item_code != d.alternate_item) {
364 return true;
365 }
366 });
367
368 alternative_items.forEach(d => {
369 let row = frappe.get_doc(opts.child_doctype, d.docname);
Doridel Cahanape42192b2018-05-16 13:28:39 +0800370 let qty = null;
371 if (row.doctype === 'Work Order Item') {
372 qty = row.required_qty;
373 } else {
374 qty = row.qty;
375 }
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530376 row[item_field] = d.alternate_item;
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530377 frm.script_manager.trigger(item_field, row.doctype, row.name)
378 .then(() => {
379 frappe.model.set_value(row.doctype, row.name, 'qty', qty);
380 frappe.model.set_value(row.doctype, row.name,
381 opts.original_item_field, d.item_code);
382 });
383 });
384
385 refresh_field(opts.child_docname);
386 this.hide();
387 },
388 primary_action_label: __('Update')
389 });
390
391 frm.doc[opts.child_docname].forEach(d => {
392 if (!opts.condition || opts.condition(d)) {
393 dialog.fields_dict.alternative_items.df.data.push({
394 "docname": d.name,
395 "item_code": d[item_field],
396 "warehouse": d[warehouse_field],
397 "actual_qty": d.actual_qty
398 });
399 }
400 })
401
402 this.data = dialog.fields_dict.alternative_items.df.data;
403 dialog.fields_dict.alternative_items.grid.refresh();
404 dialog.show();
405}
406
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800407erpnext.utils.update_child_items = function(opts) {
408 const frm = opts.frm;
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100409 const cannot_add_row = (typeof opts.cannot_add_row === 'undefined') ? true : opts.cannot_add_row;
410 const child_docname = (typeof opts.cannot_add_row === 'undefined') ? "items" : opts.child_docname;
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800411 this.data = [];
412 const dialog = new frappe.ui.Dialog({
413 title: __("Update Items"),
414 fields: [
415 {fieldtype:'Section Break', label: __('Items')},
416 {
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100417 fieldname: "trans_items",
418 fieldtype: "Table",
419 cannot_add_rows: cannot_add_row,
420 in_place_edit: true,
421 data: this.data,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800422 get_data: () => {
423 return this.data;
424 },
425 fields: [{
426 fieldtype:'Data',
427 fieldname:"docname",
428 hidden: 0,
429 }, {
430 fieldtype:'Link',
431 fieldname:"item_code",
432 options: 'Item',
433 in_list_view: 1,
434 read_only: 1,
435 label: __('Item Code')
436 }, {
437 fieldtype:'Float',
438 fieldname:"qty",
439 default: 0,
440 read_only: 0,
441 in_list_view: 1,
442 label: __('Qty')
443 }, {
444 fieldtype:'Currency',
445 fieldname:"rate",
446 default: 0,
447 read_only: 0,
448 in_list_view: 1,
449 label: __('Rate')
450 }]
451 },
452 ],
453 primary_action: function() {
454 const trans_items = this.get_values()["trans_items"];
455 frappe.call({
456 method: 'erpnext.controllers.accounts_controller.update_child_qty_rate',
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100457 freeze: true,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800458 args: {
459 'parent_doctype': frm.doc.doctype,
460 'trans_items': trans_items,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100461 'parent_doctype_name': frm.doc.name,
462 'child_docname': child_docname
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800463 },
464 callback: function() {
465 frm.reload_doc();
466 }
467 });
468 this.hide();
469 refresh_field("items");
470 },
471 primary_action_label: __('Update')
472 });
473
474 frm.doc[opts.child_docname].forEach(d => {
475 dialog.fields_dict.trans_items.df.data.push({
476 "docname": d.name,
477 "item_code": d.item_code,
478 "qty": d.qty,
479 "rate": d.rate,
480 });
481 this.data = dialog.fields_dict.trans_items.df.data;
482 dialog.fields_dict.trans_items.grid.refresh();
483 })
484 dialog.show();
485}
486
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530487erpnext.utils.map_current_doc = function(opts) {
488 if(opts.get_query_filters) {
489 opts.get_query = function() {
490 return {filters: opts.get_query_filters};
491 }
492 }
493 var _map = function() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530494 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530495 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530496 if(!cur_frm.doc.items[0].item_code) {
497 cur_frm.doc.items = cur_frm.doc.items.splice(1);
498 }
bhupen3c7e70f2016-11-30 14:32:11 +0530499
500 // find the doctype of the items table
501 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530502
bhupen3c7e70f2016-11-30 14:32:11 +0530503 // find the link fieldname from items table for the given
504 // source_doctype
505 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530506 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530507 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
508
Nabin Hait802b4352017-01-09 15:32:20 +0530509 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530510 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530511 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530512
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530513 $.each(cur_frm.doc.items, function(i, d) {
514 opts.source_name.forEach(function(src) {
515 if(d[link_fieldname]==src) {
516 already_set = true;
517 if (item_qty_map[d.item_code])
518 item_qty_map[d.item_code] += flt(d.qty);
519 else
520 item_qty_map[d.item_code] = flt(d.qty);
521 }
522 });
523 });
524
525 if(already_set) {
526 opts.source_name.forEach(function(src) {
527 frappe.model.with_doc(opts.source_doctype, src, function(r) {
528 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
529 $.each(source_doc.items || [], function(i, row) {
530 if(row.qty > flt(item_qty_map[row.item_code])) {
531 already_set = false;
532 return false;
533 }
534 })
535 })
536
537 if(already_set) {
538 frappe.msgprint(__("You have already selected items from {0} {1}",
539 [opts.source_doctype, src]));
540 return;
541 }
542
543 })
Nabin Hait802b4352017-01-09 15:32:20 +0530544 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530545 }
546
547 return frappe.call({
548 // Sometimes we hit the limit for URL length of a GET request
549 // as we send the full target_doc. Hence this is a POST request.
550 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530551 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530552 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530553 "method": opts.method,
554 "source_names": opts.source_name,
555 "target_doc": cur_frm.doc,
Rushabh Mehta49f97472018-08-30 18:50:48 +0530556 'args': opts.args
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530557 },
558 callback: function(r) {
559 if(!r.exc) {
560 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530561 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530562 cur_frm.refresh();
563 }
564 }
565 });
566 }
567 if(opts.source_doctype) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530568 var d = new frappe.ui.form.MultiSelectDialog({
569 doctype: opts.source_doctype,
570 target: opts.target,
571 date_field: opts.date_field || undefined,
572 setters: opts.setters,
573 get_query: opts.get_query,
574 action: function(selections, args) {
575 let values = selections;
576 if(values.length === 0){
Nabin Haita11dcb62017-12-04 13:36:50 +0530577 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530578 return;
579 }
580 opts.source_name = values;
581 opts.setters = args;
582 d.dialog.hide();
583 _map();
584 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530585 });
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530586 } else if(opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530587 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530588 _map();
589 }
590}
591
Rushabh Mehta180e4352016-07-26 17:57:42 +0530592frappe.form.link_formatters['Item'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530593 if(doc && doc.item_name && doc.item_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530594 return value? value + ': ' + doc.item_name: doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530595 } else {
596 return value;
597 }
598}
599
600frappe.form.link_formatters['Employee'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530601 if(doc && doc.employee_name && doc.employee_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530602 return value? value + ': ' + doc.employee_name: doc.employee_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530603 } else {
604 return value;
605 }
606}
607
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530608// add description on posting time
609$(document).on('app_ready', function() {
610 if(!frappe.datetime.is_timezone_same()) {
611 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
612 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
613 frappe.ui.form.on(d, "onload", function(frm) {
614 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530615 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530616 });
617 });
618 }
akshay14384c22016-07-28 13:53:17 +0530619});