blob: dcc15fb5b3d2244b24145e12bd368693a17b12d7 [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() {
Suraj Shettyb6639662019-06-20 18:00:19 +053068 if (!frappe.model.can_read('Accounting Dimension')) {
Suraj Shettyda629812019-06-20 17:20:19 +053069 return
70 }
deepeshgarg007e64c3572019-05-14 08:50:45 +053071 let dimensions = await frappe.db.get_list('Accounting Dimension', {
72 fields: ['label', 'fieldname', 'document_type'],
deepeshgarg007e9c6e642019-05-26 12:34:13 +053073 filters: {
74 disabled: 0
75 }
deepeshgarg007e64c3572019-05-14 08:50:45 +053076 });
77
78 return dimensions;
Rushabh Mehta5495bc52015-05-19 12:00:34 +053079 }
Rushabh Mehtac38fc712014-04-16 17:21:25 +053080});
Rushabh Mehta7d368752014-11-24 14:16:47 +053081
Nabin Haitb072c742014-12-16 12:49:13 +053082
83$.extend(erpnext.utils, {
Rushabh Mehta559aa3a2016-09-19 16:04:58 +053084 set_party_dashboard_indicators: function(frm) {
85 if(frm.doc.__onload && frm.doc.__onload.dashboard_info) {
deepeshgarg007920dc142018-11-23 10:17:28 +053086 var company_wise_info = frm.doc.__onload.dashboard_info;
deepeshgarg007f31caff2018-11-27 15:04:12 +053087 if(company_wise_info.length > 1) {
deepeshgarg007a1cffc32018-11-23 16:51:35 +053088 company_wise_info.forEach(function(info) {
deepeshgarg00712f5cef2018-12-25 16:06:19 +053089 erpnext.utils.add_indicator_for_multicompany(frm, info);
deepeshgarg007a1cffc32018-11-23 16:51:35 +053090 });
deepeshgarg00712f5cef2018-12-25 16:06:19 +053091 } else if (company_wise_info.length === 1) {
deepeshgarg00764238ee2018-12-25 16:28:39 +053092 frm.dashboard.add_indicator(__('Annual Billing: {0}',
93 [format_currency(company_wise_info[0].billing_this_year, company_wise_info[0].currency)]), 'blue');
94 frm.dashboard.add_indicator(__('Total Unpaid: {0}',
95 [format_currency(company_wise_info[0].total_unpaid, company_wise_info[0].currency)]),
deepeshgarg0078300f5e2019-01-01 20:28:49 +053096 company_wise_info[0].total_unpaid ? 'orange' : 'green');
deepeshgarg00712f5cef2018-12-25 16:06:19 +053097
deepeshgarg00764238ee2018-12-25 16:28:39 +053098 if(company_wise_info[0].loyalty_points) {
99 frm.dashboard.add_indicator(__('Loyalty Points: {0}',
100 [company_wise_info[0].loyalty_points]), 'blue');
101 }
deepeshgarg007f31caff2018-11-27 15:04:12 +0530102 }
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530103 }
104 },
105
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530106 add_indicator_for_multicompany: function(frm, info) {
107 frm.dashboard.stats_area.removeClass('hidden');
108 frm.dashboard.stats_area_row.addClass('flex');
109 frm.dashboard.stats_area_row.css('flex-wrap', 'wrap');
110
111 var color = info.total_unpaid ? 'orange' : 'green';
112
113 var indicator = $('<div class="flex-column col-xs-6">'+
114 '<div style="margin-top:10px"><h6>'+info.company+'</h6></div>'+
115
116 '<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
117 'Annual Billing: '+format_currency(info.billing_this_year, info.currency)+'</span></div>'+
118
119 '<div class="badge-link small" style="margin-bottom:10px">'+
120 '<span class="indicator '+color+'">Total Unpaid: '
121 +format_currency(info.total_unpaid, info.currency)+'</span></div>'+
122
123
124 '</div>').appendTo(frm.dashboard.stats_area_row);
125
126 if(info.loyalty_points){
127 $('<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
128 'Loyalty Points: '+info.loyalty_points+'</span></div>').appendTo(indicator);
129 }
130
131 return indicator;
132 },
133
Shreya Shah149f7ee2018-03-27 11:29:25 +0530134 get_party_name: function(party_type) {
135 var dict = {'Customer': 'customer_name', 'Supplier': 'supplier_name', 'Employee': 'employee_name',
136 'Member': 'member_name'};
137 return dict[party_type];
138 },
139
Rushabh Mehta49f97472018-08-30 18:50:48 +0530140 copy_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname) {
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +0530141 var d = locals[dt][dn];
142 if(d[fieldname]){
143 var cl = doc[table_fieldname] || [];
144 for(var i = 0; i < cl.length; i++) {
145 if(!cl[i][fieldname]) cl[i][fieldname] = d[fieldname];
146 }
147 }
148 refresh_field(table_fieldname);
Makarand Bauskar157c3342017-05-26 21:32:33 +0530149 },
150
151 get_terms: function(tc_name, doc, callback) {
152 if(tc_name) {
153 return frappe.call({
154 method: 'erpnext.setup.doctype.terms_and_conditions.terms_and_conditions.get_terms_and_conditions',
155 args: {
156 template_name: tc_name,
157 doc: doc
158 },
159 callback: function(r) {
160 callback(r)
161 }
162 });
163 }
tundebabzy1a947db2017-08-29 13:48:27 +0100164 },
165
rohitwaghchaure713cfc72018-09-11 17:40:37 +0530166 make_bank_account: function(doctype, docname) {
167 frappe.call({
168 method: "erpnext.accounts.doctype.bank_account.bank_account.make_bank_account",
169 args: {
170 doctype: doctype,
171 docname: docname
172 },
173 freeze: true,
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
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530181 make_subscription: function(doctype, docname) {
182 frappe.call({
rohitwaghchaure79e5b072018-06-21 12:58:14 +0530183 method: "frappe.desk.doctype.auto_repeat.auto_repeat.make_auto_repeat",
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530184 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
Saif90cf2dd2018-09-30 21:46:31 +0500195 make_pricing_rule: function(doctype, docname) {
196 frappe.call({
197 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.make_pricing_rule",
198 args: {
199 doctype: doctype,
200 docname: docname
201 },
202 callback: function(r) {
203 var doclist = frappe.model.sync(r.message);
204 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
205 }
206 })
207 },
208
tundebabzy1a947db2017-08-29 13:48:27 +0100209 /**
210 * Checks if the first row of a given child table is empty
211 * @param child_table - Child table Doctype
212 * @return {Boolean}
213 **/
214 first_row_is_empty: function(child_table){
215 if($.isArray(child_table) && child_table.length > 0) {
216 return !child_table[0].item_code;
217 }
218 return false;
219 },
220
221 /**
222 * Removes the first row of a child table if it is empty
223 * @param {_Frm} frm - The current form
224 * @param {String} child_table_name - The child table field name
225 * @return {Boolean}
226 **/
227 remove_empty_first_row: function(frm, child_table_name){
228 const rows = frm['doc'][child_table_name];
229 if (this.first_row_is_empty(rows)){
230 frm['doc'][child_table_name] = rows.splice(1);
231 }
232 return rows;
233 },
Zarrar6ffdf942018-06-14 12:24:16 +0530234 get_tree_options: function(option) {
235 // get valid options for tree based on user permission & locals dict
236 let unscrub_option = frappe.model.unscrub(option);
237 let user_permission = frappe.defaults.get_user_permissions();
deepeshgarg0070d64d622019-02-20 17:55:14 +0530238 let options;
239
Zarrar6ffdf942018-06-14 12:24:16 +0530240 if(user_permission && user_permission[unscrub_option]) {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530241 options = user_permission[unscrub_option].map(perm => perm.doc);
Zarrar6ffdf942018-06-14 12:24:16 +0530242 } else {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530243 options = $.map(locals[`:${unscrub_option}`], function(c) { return c.name; }).sort();
Zarrar6ffdf942018-06-14 12:24:16 +0530244 }
deepeshgarg0070d64d622019-02-20 17:55:14 +0530245
246 // filter unique values, as there may be multiple user permissions for any value
247 return options.filter((value, index, self) => self.indexOf(value) === index);
Zarrar6ffdf942018-06-14 12:24:16 +0530248 },
249 get_tree_default: function(option) {
250 // set default for a field based on user permission
251 let options = this.get_tree_options(option);
252 if(options.includes(frappe.defaults.get_default(option))) {
253 return frappe.defaults.get_default(option);
254 } else {
255 return options[0];
256 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400257 },
258 copy_parent_value_in_all_row: function(doc, dt, dn, table_fieldname, fieldname, parent_fieldname) {
259 var d = locals[dt][dn];
260 if(d[fieldname]){
261 var cl = doc[table_fieldname] || [];
262 for(var i = 0; i < cl.length; i++) {
263 cl[i][fieldname] = doc[parent_fieldname];
264 }
265 }
266 refresh_field(table_fieldname);
267 },
268
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530269});
270
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530271erpnext.utils.select_alternate_items = function(opts) {
272 const frm = opts.frm;
273 const warehouse_field = opts.warehouse_field || 'warehouse';
274 const item_field = opts.item_field || 'item_code';
275
276 this.data = [];
277 const dialog = new frappe.ui.Dialog({
278 title: __("Select Alternate Item"),
279 fields: [
280 {fieldtype:'Section Break', label: __('Items')},
281 {
282 fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true,
283 in_place_edit: true, data: this.data,
284 get_data: () => {
285 return this.data;
286 },
287 fields: [{
288 fieldtype:'Data',
289 fieldname:"docname",
290 hidden: 1
291 }, {
292 fieldtype:'Link',
293 fieldname:"item_code",
294 options: 'Item',
295 in_list_view: 1,
296 read_only: 1,
297 label: __('Item Code')
298 }, {
299 fieldtype:'Link',
300 fieldname:"alternate_item",
301 options: 'Item',
302 default: "",
303 in_list_view: 1,
304 label: __('Alternate Item'),
305 onchange: function() {
306 const item_code = this.get_value();
307 const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value();
308 if (item_code && warehouse) {
309 frappe.call({
310 method: "erpnext.stock.utils.get_latest_stock_qty",
311 args: {
312 item_code: item_code,
313 warehouse: warehouse
314 },
315 callback: (r) => {
316 this.grid_row.on_grid_fields_dict
317 .actual_qty.set_value(r.message || 0);
318 }
319 })
320 }
321 },
322 get_query: (e) => {
323 return {
324 query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items",
325 filters: {
326 item_code: e.item_code
327 }
328 };
329 }
330 }, {
331 fieldtype:'Link',
332 fieldname:"warehouse",
333 options: 'Warehouse',
334 default: "",
335 in_list_view: 1,
336 label: __('Warehouse'),
337 onchange: function() {
338 const warehouse = this.get_value();
339 const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value();
340 if (item_code && warehouse) {
341 frappe.call({
342 method: "erpnext.stock.utils.get_latest_stock_qty",
343 args: {
344 item_code: item_code,
345 warehouse: warehouse
346 },
347 callback: (r) => {
348 this.grid_row.on_grid_fields_dict
349 .actual_qty.set_value(r.message || 0);
350 }
351 })
352 }
353 },
354 }, {
355 fieldtype:'Float',
356 fieldname:"actual_qty",
357 default: 0,
358 read_only: 1,
359 in_list_view: 1,
360 label: __('Available Qty')
361 }]
362 },
363 ],
364 primary_action: function() {
365 const args = this.get_values()["alternative_items"];
366 const alternative_items = args.filter(d => {
367 if (d.alternate_item && d.item_code != d.alternate_item) {
368 return true;
369 }
370 });
371
372 alternative_items.forEach(d => {
373 let row = frappe.get_doc(opts.child_doctype, d.docname);
Doridel Cahanape42192b2018-05-16 13:28:39 +0800374 let qty = null;
375 if (row.doctype === 'Work Order Item') {
376 qty = row.required_qty;
377 } else {
378 qty = row.qty;
379 }
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530380 row[item_field] = d.alternate_item;
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530381 frm.script_manager.trigger(item_field, row.doctype, row.name)
382 .then(() => {
383 frappe.model.set_value(row.doctype, row.name, 'qty', qty);
384 frappe.model.set_value(row.doctype, row.name,
385 opts.original_item_field, d.item_code);
386 });
387 });
388
389 refresh_field(opts.child_docname);
390 this.hide();
391 },
392 primary_action_label: __('Update')
393 });
394
395 frm.doc[opts.child_docname].forEach(d => {
396 if (!opts.condition || opts.condition(d)) {
397 dialog.fields_dict.alternative_items.df.data.push({
398 "docname": d.name,
399 "item_code": d[item_field],
400 "warehouse": d[warehouse_field],
401 "actual_qty": d.actual_qty
402 });
403 }
404 })
405
406 this.data = dialog.fields_dict.alternative_items.df.data;
407 dialog.fields_dict.alternative_items.grid.refresh();
408 dialog.show();
409}
410
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800411erpnext.utils.update_child_items = function(opts) {
412 const frm = opts.frm;
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100413 const cannot_add_row = (typeof opts.cannot_add_row === 'undefined') ? true : opts.cannot_add_row;
414 const child_docname = (typeof opts.cannot_add_row === 'undefined') ? "items" : opts.child_docname;
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800415 this.data = [];
416 const dialog = new frappe.ui.Dialog({
417 title: __("Update Items"),
418 fields: [
419 {fieldtype:'Section Break', label: __('Items')},
420 {
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100421 fieldname: "trans_items",
422 fieldtype: "Table",
423 cannot_add_rows: cannot_add_row,
424 in_place_edit: true,
425 data: this.data,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800426 get_data: () => {
427 return this.data;
428 },
429 fields: [{
430 fieldtype:'Data',
431 fieldname:"docname",
432 hidden: 0,
433 }, {
434 fieldtype:'Link',
435 fieldname:"item_code",
436 options: 'Item',
437 in_list_view: 1,
438 read_only: 1,
439 label: __('Item Code')
440 }, {
441 fieldtype:'Float',
442 fieldname:"qty",
443 default: 0,
444 read_only: 0,
445 in_list_view: 1,
446 label: __('Qty')
447 }, {
448 fieldtype:'Currency',
449 fieldname:"rate",
450 default: 0,
451 read_only: 0,
452 in_list_view: 1,
453 label: __('Rate')
454 }]
455 },
456 ],
457 primary_action: function() {
458 const trans_items = this.get_values()["trans_items"];
459 frappe.call({
460 method: 'erpnext.controllers.accounts_controller.update_child_qty_rate',
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100461 freeze: true,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800462 args: {
463 'parent_doctype': frm.doc.doctype,
464 'trans_items': trans_items,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100465 'parent_doctype_name': frm.doc.name,
466 'child_docname': child_docname
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800467 },
468 callback: function() {
469 frm.reload_doc();
470 }
471 });
472 this.hide();
473 refresh_field("items");
474 },
475 primary_action_label: __('Update')
476 });
477
478 frm.doc[opts.child_docname].forEach(d => {
479 dialog.fields_dict.trans_items.df.data.push({
480 "docname": d.name,
481 "item_code": d.item_code,
482 "qty": d.qty,
483 "rate": d.rate,
484 });
485 this.data = dialog.fields_dict.trans_items.df.data;
486 dialog.fields_dict.trans_items.grid.refresh();
487 })
488 dialog.show();
489}
490
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530491erpnext.utils.map_current_doc = function(opts) {
492 if(opts.get_query_filters) {
493 opts.get_query = function() {
494 return {filters: opts.get_query_filters};
495 }
496 }
497 var _map = function() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530498 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530499 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530500 if(!cur_frm.doc.items[0].item_code) {
501 cur_frm.doc.items = cur_frm.doc.items.splice(1);
502 }
bhupen3c7e70f2016-11-30 14:32:11 +0530503
504 // find the doctype of the items table
505 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530506
bhupen3c7e70f2016-11-30 14:32:11 +0530507 // find the link fieldname from items table for the given
508 // source_doctype
509 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530510 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530511 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
512
Nabin Hait802b4352017-01-09 15:32:20 +0530513 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530514 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530515 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530516
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530517 $.each(cur_frm.doc.items, function(i, d) {
518 opts.source_name.forEach(function(src) {
519 if(d[link_fieldname]==src) {
520 already_set = true;
521 if (item_qty_map[d.item_code])
522 item_qty_map[d.item_code] += flt(d.qty);
523 else
524 item_qty_map[d.item_code] = flt(d.qty);
525 }
526 });
527 });
528
529 if(already_set) {
530 opts.source_name.forEach(function(src) {
531 frappe.model.with_doc(opts.source_doctype, src, function(r) {
532 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
533 $.each(source_doc.items || [], function(i, row) {
534 if(row.qty > flt(item_qty_map[row.item_code])) {
535 already_set = false;
536 return false;
537 }
538 })
539 })
540
541 if(already_set) {
542 frappe.msgprint(__("You have already selected items from {0} {1}",
543 [opts.source_doctype, src]));
544 return;
545 }
546
547 })
Nabin Hait802b4352017-01-09 15:32:20 +0530548 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530549 }
550
551 return frappe.call({
552 // Sometimes we hit the limit for URL length of a GET request
553 // as we send the full target_doc. Hence this is a POST request.
554 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530555 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530556 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530557 "method": opts.method,
558 "source_names": opts.source_name,
559 "target_doc": cur_frm.doc,
Rushabh Mehta49f97472018-08-30 18:50:48 +0530560 'args': opts.args
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530561 },
562 callback: function(r) {
563 if(!r.exc) {
564 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530565 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530566 cur_frm.refresh();
567 }
568 }
569 });
570 }
571 if(opts.source_doctype) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530572 var d = new frappe.ui.form.MultiSelectDialog({
573 doctype: opts.source_doctype,
574 target: opts.target,
575 date_field: opts.date_field || undefined,
576 setters: opts.setters,
577 get_query: opts.get_query,
578 action: function(selections, args) {
579 let values = selections;
580 if(values.length === 0){
Nabin Haita11dcb62017-12-04 13:36:50 +0530581 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530582 return;
583 }
584 opts.source_name = values;
585 opts.setters = args;
586 d.dialog.hide();
587 _map();
588 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530589 });
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530590 } else if(opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530591 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530592 _map();
593 }
594}
595
Rushabh Mehta180e4352016-07-26 17:57:42 +0530596frappe.form.link_formatters['Item'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530597 if(doc && doc.item_name && doc.item_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530598 return value? value + ': ' + doc.item_name: doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530599 } else {
600 return value;
601 }
602}
603
604frappe.form.link_formatters['Employee'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530605 if(doc && doc.employee_name && doc.employee_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530606 return value? value + ': ' + doc.employee_name: doc.employee_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530607 } else {
608 return value;
609 }
610}
611
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530612// add description on posting time
613$(document).on('app_ready', function() {
614 if(!frappe.datetime.is_timezone_same()) {
615 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
616 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
617 frappe.ui.form.on(d, "onload", function(frm) {
618 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530619 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530620 });
621 });
622 }
akshay14384c22016-07-28 13:53:17 +0530623});