blob: cf48be4128e9226dde848c102ba25fb47840579e [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 Shetty01f56b82019-06-20 18:07:58 +053069 return [];
Suraj Shettyda629812019-06-20 17:20:19 +053070 }
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
Nabin Hait34c551d2019-07-03 10:34:31 +0530269 create_new_doc: function (doctype, update_fields) {
270 frappe.model.with_doctype(doctype, function() {
271 var new_doc = frappe.model.get_new_doc(doctype);
272 for (let [key, value] of Object.entries(update_fields)) {
273 new_doc[key] = value;
274 }
275 frappe.ui.form.make_quick_entry(doctype, null, null, new_doc);
276 });
277 }
278
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530279});
280
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530281erpnext.utils.select_alternate_items = function(opts) {
282 const frm = opts.frm;
283 const warehouse_field = opts.warehouse_field || 'warehouse';
284 const item_field = opts.item_field || 'item_code';
285
286 this.data = [];
287 const dialog = new frappe.ui.Dialog({
288 title: __("Select Alternate Item"),
289 fields: [
290 {fieldtype:'Section Break', label: __('Items')},
291 {
292 fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true,
293 in_place_edit: true, data: this.data,
294 get_data: () => {
295 return this.data;
296 },
297 fields: [{
298 fieldtype:'Data',
299 fieldname:"docname",
300 hidden: 1
301 }, {
302 fieldtype:'Link',
303 fieldname:"item_code",
304 options: 'Item',
305 in_list_view: 1,
306 read_only: 1,
307 label: __('Item Code')
308 }, {
309 fieldtype:'Link',
310 fieldname:"alternate_item",
311 options: 'Item',
312 default: "",
313 in_list_view: 1,
314 label: __('Alternate Item'),
315 onchange: function() {
316 const item_code = this.get_value();
317 const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value();
318 if (item_code && warehouse) {
319 frappe.call({
320 method: "erpnext.stock.utils.get_latest_stock_qty",
321 args: {
322 item_code: item_code,
323 warehouse: warehouse
324 },
325 callback: (r) => {
326 this.grid_row.on_grid_fields_dict
327 .actual_qty.set_value(r.message || 0);
328 }
329 })
330 }
331 },
332 get_query: (e) => {
333 return {
334 query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items",
335 filters: {
336 item_code: e.item_code
337 }
338 };
339 }
340 }, {
341 fieldtype:'Link',
342 fieldname:"warehouse",
343 options: 'Warehouse',
344 default: "",
345 in_list_view: 1,
346 label: __('Warehouse'),
347 onchange: function() {
348 const warehouse = this.get_value();
349 const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value();
350 if (item_code && warehouse) {
351 frappe.call({
352 method: "erpnext.stock.utils.get_latest_stock_qty",
353 args: {
354 item_code: item_code,
355 warehouse: warehouse
356 },
357 callback: (r) => {
358 this.grid_row.on_grid_fields_dict
359 .actual_qty.set_value(r.message || 0);
360 }
361 })
362 }
363 },
364 }, {
365 fieldtype:'Float',
366 fieldname:"actual_qty",
367 default: 0,
368 read_only: 1,
369 in_list_view: 1,
370 label: __('Available Qty')
371 }]
372 },
373 ],
374 primary_action: function() {
375 const args = this.get_values()["alternative_items"];
376 const alternative_items = args.filter(d => {
377 if (d.alternate_item && d.item_code != d.alternate_item) {
378 return true;
379 }
380 });
381
382 alternative_items.forEach(d => {
383 let row = frappe.get_doc(opts.child_doctype, d.docname);
Doridel Cahanape42192b2018-05-16 13:28:39 +0800384 let qty = null;
385 if (row.doctype === 'Work Order Item') {
386 qty = row.required_qty;
387 } else {
388 qty = row.qty;
389 }
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530390 row[item_field] = d.alternate_item;
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530391 frm.script_manager.trigger(item_field, row.doctype, row.name)
392 .then(() => {
393 frappe.model.set_value(row.doctype, row.name, 'qty', qty);
394 frappe.model.set_value(row.doctype, row.name,
395 opts.original_item_field, d.item_code);
396 });
397 });
398
399 refresh_field(opts.child_docname);
400 this.hide();
401 },
402 primary_action_label: __('Update')
403 });
404
405 frm.doc[opts.child_docname].forEach(d => {
406 if (!opts.condition || opts.condition(d)) {
407 dialog.fields_dict.alternative_items.df.data.push({
408 "docname": d.name,
409 "item_code": d[item_field],
410 "warehouse": d[warehouse_field],
411 "actual_qty": d.actual_qty
412 });
413 }
414 })
415
416 this.data = dialog.fields_dict.alternative_items.df.data;
417 dialog.fields_dict.alternative_items.grid.refresh();
418 dialog.show();
419}
420
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800421erpnext.utils.update_child_items = function(opts) {
422 const frm = opts.frm;
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100423 const cannot_add_row = (typeof opts.cannot_add_row === 'undefined') ? true : opts.cannot_add_row;
424 const child_docname = (typeof opts.cannot_add_row === 'undefined') ? "items" : opts.child_docname;
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800425 this.data = [];
426 const dialog = new frappe.ui.Dialog({
427 title: __("Update Items"),
428 fields: [
429 {fieldtype:'Section Break', label: __('Items')},
430 {
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100431 fieldname: "trans_items",
432 fieldtype: "Table",
433 cannot_add_rows: cannot_add_row,
434 in_place_edit: true,
435 data: this.data,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800436 get_data: () => {
437 return this.data;
438 },
439 fields: [{
440 fieldtype:'Data',
441 fieldname:"docname",
442 hidden: 0,
443 }, {
444 fieldtype:'Link',
445 fieldname:"item_code",
446 options: 'Item',
447 in_list_view: 1,
448 read_only: 1,
449 label: __('Item Code')
450 }, {
451 fieldtype:'Float',
452 fieldname:"qty",
453 default: 0,
454 read_only: 0,
455 in_list_view: 1,
456 label: __('Qty')
457 }, {
458 fieldtype:'Currency',
459 fieldname:"rate",
460 default: 0,
461 read_only: 0,
462 in_list_view: 1,
463 label: __('Rate')
464 }]
465 },
466 ],
467 primary_action: function() {
468 const trans_items = this.get_values()["trans_items"];
469 frappe.call({
470 method: 'erpnext.controllers.accounts_controller.update_child_qty_rate',
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100471 freeze: true,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800472 args: {
473 'parent_doctype': frm.doc.doctype,
474 'trans_items': trans_items,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100475 'parent_doctype_name': frm.doc.name,
476 'child_docname': child_docname
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800477 },
478 callback: function() {
479 frm.reload_doc();
480 }
481 });
482 this.hide();
483 refresh_field("items");
484 },
485 primary_action_label: __('Update')
486 });
487
488 frm.doc[opts.child_docname].forEach(d => {
489 dialog.fields_dict.trans_items.df.data.push({
490 "docname": d.name,
491 "item_code": d.item_code,
492 "qty": d.qty,
493 "rate": d.rate,
494 });
495 this.data = dialog.fields_dict.trans_items.df.data;
496 dialog.fields_dict.trans_items.grid.refresh();
497 })
498 dialog.show();
499}
500
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530501erpnext.utils.map_current_doc = function(opts) {
502 if(opts.get_query_filters) {
503 opts.get_query = function() {
504 return {filters: opts.get_query_filters};
505 }
506 }
507 var _map = function() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530508 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530509 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530510 if(!cur_frm.doc.items[0].item_code) {
511 cur_frm.doc.items = cur_frm.doc.items.splice(1);
512 }
bhupen3c7e70f2016-11-30 14:32:11 +0530513
514 // find the doctype of the items table
515 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530516
bhupen3c7e70f2016-11-30 14:32:11 +0530517 // find the link fieldname from items table for the given
518 // source_doctype
519 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530520 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530521 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
522
Nabin Hait802b4352017-01-09 15:32:20 +0530523 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530524 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530525 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530526
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530527 $.each(cur_frm.doc.items, function(i, d) {
528 opts.source_name.forEach(function(src) {
529 if(d[link_fieldname]==src) {
530 already_set = true;
531 if (item_qty_map[d.item_code])
532 item_qty_map[d.item_code] += flt(d.qty);
533 else
534 item_qty_map[d.item_code] = flt(d.qty);
535 }
536 });
537 });
538
539 if(already_set) {
540 opts.source_name.forEach(function(src) {
541 frappe.model.with_doc(opts.source_doctype, src, function(r) {
542 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
543 $.each(source_doc.items || [], function(i, row) {
544 if(row.qty > flt(item_qty_map[row.item_code])) {
545 already_set = false;
546 return false;
547 }
548 })
549 })
550
551 if(already_set) {
552 frappe.msgprint(__("You have already selected items from {0} {1}",
553 [opts.source_doctype, src]));
554 return;
555 }
556
557 })
Nabin Hait802b4352017-01-09 15:32:20 +0530558 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530559 }
560
561 return frappe.call({
562 // Sometimes we hit the limit for URL length of a GET request
563 // as we send the full target_doc. Hence this is a POST request.
564 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530565 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530566 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530567 "method": opts.method,
568 "source_names": opts.source_name,
569 "target_doc": cur_frm.doc,
Rushabh Mehta49f97472018-08-30 18:50:48 +0530570 'args': opts.args
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530571 },
572 callback: function(r) {
573 if(!r.exc) {
574 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530575 cur_frm.dirty();
Himanshuea4c42d2019-06-28 14:13:13 +0530576 erpnext.utils.clear_duplicates();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530577 cur_frm.refresh();
578 }
579 }
580 });
581 }
582 if(opts.source_doctype) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530583 var d = new frappe.ui.form.MultiSelectDialog({
584 doctype: opts.source_doctype,
585 target: opts.target,
586 date_field: opts.date_field || undefined,
587 setters: opts.setters,
588 get_query: opts.get_query,
589 action: function(selections, args) {
590 let values = selections;
591 if(values.length === 0){
Nabin Haita11dcb62017-12-04 13:36:50 +0530592 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530593 return;
594 }
595 opts.source_name = values;
596 opts.setters = args;
597 d.dialog.hide();
598 _map();
599 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530600 });
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530601 } else if(opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530602 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530603 _map();
604 }
605}
606
Himanshuea4c42d2019-06-28 14:13:13 +0530607erpnext.utils.clear_duplicates = function() {
Mangesh-Khairnare4934902019-06-28 19:22:51 +0530608 if(!cur_frm.doc.items) return;
Himanshuea4c42d2019-06-28 14:13:13 +0530609 const unique_items = new Map();
610 /*
611 Create a Map of items with
612 item_code => [qty, warehouse, batch_no]
613 */
614 let items = [];
615
616 for (let item of cur_frm.doc.items) {
617 if (!(unique_items.has(item.item_code) && unique_items.get(item.item_code)[0] === item.qty &&
618 unique_items.get(item.item_code)[1] === item.warehouse && unique_items.get(item.item_code)[2] === item.batch_no &&
619 unique_items.get(item.item_code)[3] === item.delivery_date && unique_items.get(item.item_code)[4] === item.required_date &&
620 unique_items.get(item.item_code)[5] === item.rate)) {
621
622 unique_items.set(item.item_code, [item.qty, item.warehouse, item.batch_no, item.delivery_date, item.required_date, item.rate]);
623 items.push(item);
624 }
625 }
626 cur_frm.doc.items = items;
627}
628
Rushabh Mehta180e4352016-07-26 17:57:42 +0530629frappe.form.link_formatters['Item'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530630 if(doc && doc.item_name && doc.item_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530631 return value? value + ': ' + doc.item_name: doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530632 } else {
633 return value;
634 }
635}
636
637frappe.form.link_formatters['Employee'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530638 if(doc && doc.employee_name && doc.employee_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530639 return value? value + ': ' + doc.employee_name: doc.employee_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530640 } else {
641 return value;
642 }
643}
644
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530645// add description on posting time
646$(document).on('app_ready', function() {
647 if(!frappe.datetime.is_timezone_same()) {
648 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
649 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
650 frappe.ui.form.on(d, "onload", function(frm) {
651 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530652 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530653 });
654 });
655 }
akshay14384c22016-07-28 13:53:17 +0530656});