blob: fb4f35081c281a693e9363df500d15138acbe49d [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
Rushabh Mehta62030e02013-08-14 18:37:28 +053061 $btn.on("click", function() {
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053062 var d = new frappe.ui.Dialog({
Pratik Vyasb52618c2014-04-14 16:25:30 +053063 title: __("Add Serial No"),
Rushabh Mehta62030e02013-08-14 18:37:28 +053064 fields: [
65 {
66 "fieldtype": "Link",
Rohit Waghchaure2087e572017-02-27 13:43:04 +053067 "fieldname": "serial_no",
Rushabh Mehta62030e02013-08-14 18:37:28 +053068 "options": "Serial No",
Pratik Vyasb52618c2014-04-14 16:25:30 +053069 "label": __("Serial No"),
Neil Trini Lasrado3d61bbb2015-03-12 12:51:50 +053070 "get_query": function () {
Rushabh Mehta45ca8a42015-04-28 16:02:09 +053071 return {
72 filters: {
Shreya14bd43d2018-04-27 16:24:05 +053073 item_code:grid_row.doc.item_code,
Nabin Haitb7c0c552015-08-04 12:18:12 +053074 warehouse:cur_frm.doc.is_return ? null : grid_row.doc.warehouse
Neil Trini Lasrado3d61bbb2015-03-12 12:51:50 +053075 }
76 }
Rushabh Mehta62030e02013-08-14 18:37:28 +053077 }
78 },
79 {
80 "fieldtype": "Button",
Kanchan Chauhan69818702017-02-27 12:53:05 +053081 "fieldname": "add",
Pratik Vyasb52618c2014-04-14 16:25:30 +053082 "label": __("Add")
Rushabh Mehta62030e02013-08-14 18:37:28 +053083 }
84 ]
85 });
Rushabh Mehtac38fc712014-04-16 17:21:25 +053086
Rushabh Mehta62030e02013-08-14 18:37:28 +053087 d.get_input("add").on("click", function() {
88 var serial_no = d.get_value("serial_no");
89 if(serial_no) {
90 var val = (grid_row.doc.serial_no || "").split("\n").concat([serial_no]).join("\n");
akshay14384c22016-07-28 13:53:17 +053091 grid_row.grid_form.fields_dict.serial_no.set_model_value(val.trim());
Rushabh Mehta62030e02013-08-14 18:37:28 +053092 }
93 d.hide();
94 return false;
95 });
Rushabh Mehtac38fc712014-04-16 17:21:25 +053096
Rushabh Mehta62030e02013-08-14 18:37:28 +053097 d.show();
98 });
Rushabh Mehta5495bc52015-05-19 12:00:34 +053099 }
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530100});
Rushabh Mehta7d368752014-11-24 14:16:47 +0530101
Nabin Haitb072c742014-12-16 12:49:13 +0530102
103$.extend(erpnext.utils, {
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530104 set_party_dashboard_indicators: function(frm) {
105 if(frm.doc.__onload && frm.doc.__onload.dashboard_info) {
deepeshgarg007920dc142018-11-23 10:17:28 +0530106 var company_wise_info = frm.doc.__onload.dashboard_info;
deepeshgarg007f31caff2018-11-27 15:04:12 +0530107 if(company_wise_info.length > 1) {
deepeshgarg007a1cffc32018-11-23 16:51:35 +0530108 company_wise_info.forEach(function(info) {
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530109 erpnext.utils.add_indicator_for_multicompany(frm, info);
deepeshgarg007a1cffc32018-11-23 16:51:35 +0530110 });
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530111 } else if (company_wise_info.length === 1) {
deepeshgarg00764238ee2018-12-25 16:28:39 +0530112 frm.dashboard.add_indicator(__('Annual Billing: {0}',
113 [format_currency(company_wise_info[0].billing_this_year, company_wise_info[0].currency)]), 'blue');
114 frm.dashboard.add_indicator(__('Total Unpaid: {0}',
115 [format_currency(company_wise_info[0].total_unpaid, company_wise_info[0].currency)]),
deepeshgarg0078300f5e2019-01-01 20:28:49 +0530116 company_wise_info[0].total_unpaid ? 'orange' : 'green');
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530117
deepeshgarg00764238ee2018-12-25 16:28:39 +0530118 if(company_wise_info[0].loyalty_points) {
119 frm.dashboard.add_indicator(__('Loyalty Points: {0}',
120 [company_wise_info[0].loyalty_points]), 'blue');
121 }
deepeshgarg007f31caff2018-11-27 15:04:12 +0530122 }
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530123 }
124 },
125
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530126 add_indicator_for_multicompany: function(frm, info) {
127 frm.dashboard.stats_area.removeClass('hidden');
128 frm.dashboard.stats_area_row.addClass('flex');
129 frm.dashboard.stats_area_row.css('flex-wrap', 'wrap');
130
131 var color = info.total_unpaid ? 'orange' : 'green';
132
133 var indicator = $('<div class="flex-column col-xs-6">'+
134 '<div style="margin-top:10px"><h6>'+info.company+'</h6></div>'+
135
136 '<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
137 'Annual Billing: '+format_currency(info.billing_this_year, info.currency)+'</span></div>'+
138
139 '<div class="badge-link small" style="margin-bottom:10px">'+
140 '<span class="indicator '+color+'">Total Unpaid: '
141 +format_currency(info.total_unpaid, info.currency)+'</span></div>'+
142
143
144 '</div>').appendTo(frm.dashboard.stats_area_row);
145
146 if(info.loyalty_points){
147 $('<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
148 'Loyalty Points: '+info.loyalty_points+'</span></div>').appendTo(indicator);
149 }
150
151 return indicator;
152 },
153
Shreya Shah149f7ee2018-03-27 11:29:25 +0530154 get_party_name: function(party_type) {
155 var dict = {'Customer': 'customer_name', 'Supplier': 'supplier_name', 'Employee': 'employee_name',
156 'Member': 'member_name'};
157 return dict[party_type];
158 },
159
Rushabh Mehta49f97472018-08-30 18:50:48 +0530160 copy_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname) {
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +0530161 var d = locals[dt][dn];
162 if(d[fieldname]){
163 var cl = doc[table_fieldname] || [];
164 for(var i = 0; i < cl.length; i++) {
165 if(!cl[i][fieldname]) cl[i][fieldname] = d[fieldname];
166 }
167 }
168 refresh_field(table_fieldname);
Makarand Bauskar157c3342017-05-26 21:32:33 +0530169 },
170
171 get_terms: function(tc_name, doc, callback) {
172 if(tc_name) {
173 return frappe.call({
174 method: 'erpnext.setup.doctype.terms_and_conditions.terms_and_conditions.get_terms_and_conditions',
175 args: {
176 template_name: tc_name,
177 doc: doc
178 },
179 callback: function(r) {
180 callback(r)
181 }
182 });
183 }
tundebabzy1a947db2017-08-29 13:48:27 +0100184 },
185
rohitwaghchaure713cfc72018-09-11 17:40:37 +0530186 make_bank_account: function(doctype, docname) {
187 frappe.call({
188 method: "erpnext.accounts.doctype.bank_account.bank_account.make_bank_account",
189 args: {
190 doctype: doctype,
191 docname: docname
192 },
193 freeze: true,
194 callback: function(r) {
195 var doclist = frappe.model.sync(r.message);
196 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
197 }
198 })
199 },
200
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530201 make_subscription: function(doctype, docname) {
202 frappe.call({
rohitwaghchaure79e5b072018-06-21 12:58:14 +0530203 method: "frappe.desk.doctype.auto_repeat.auto_repeat.make_auto_repeat",
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530204 args: {
205 doctype: doctype,
206 docname: docname
207 },
208 callback: function(r) {
209 var doclist = frappe.model.sync(r.message);
210 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
211 }
212 })
213 },
214
Saif90cf2dd2018-09-30 21:46:31 +0500215 make_pricing_rule: function(doctype, docname) {
216 frappe.call({
217 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.make_pricing_rule",
218 args: {
219 doctype: doctype,
220 docname: docname
221 },
222 callback: function(r) {
223 var doclist = frappe.model.sync(r.message);
224 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
225 }
226 })
227 },
228
tundebabzy1a947db2017-08-29 13:48:27 +0100229 /**
230 * Checks if the first row of a given child table is empty
231 * @param child_table - Child table Doctype
232 * @return {Boolean}
233 **/
234 first_row_is_empty: function(child_table){
235 if($.isArray(child_table) && child_table.length > 0) {
236 return !child_table[0].item_code;
237 }
238 return false;
239 },
240
241 /**
242 * Removes the first row of a child table if it is empty
243 * @param {_Frm} frm - The current form
244 * @param {String} child_table_name - The child table field name
245 * @return {Boolean}
246 **/
247 remove_empty_first_row: function(frm, child_table_name){
248 const rows = frm['doc'][child_table_name];
249 if (this.first_row_is_empty(rows)){
250 frm['doc'][child_table_name] = rows.splice(1);
251 }
252 return rows;
253 },
Zarrar6ffdf942018-06-14 12:24:16 +0530254 get_tree_options: function(option) {
255 // get valid options for tree based on user permission & locals dict
256 let unscrub_option = frappe.model.unscrub(option);
257 let user_permission = frappe.defaults.get_user_permissions();
scmmishra14d70ce2019-03-14 12:31:25 +0530258 let options;
259
Zarrar6ffdf942018-06-14 12:24:16 +0530260 if(user_permission && user_permission[unscrub_option]) {
scmmishra14d70ce2019-03-14 12:31:25 +0530261 options = user_permission[unscrub_option].map(perm => perm.doc);
Zarrar6ffdf942018-06-14 12:24:16 +0530262 } else {
scmmishra14d70ce2019-03-14 12:31:25 +0530263 options = $.map(locals[`:${unscrub_option}`], function(c) { return c.name; }).sort();
Zarrar6ffdf942018-06-14 12:24:16 +0530264 }
scmmishra14d70ce2019-03-14 12:31:25 +0530265
266 // filter unique values, as there may be multiple user permissions for any value
267 return options.filter((value, index, self) => self.indexOf(value) === index);
Zarrar6ffdf942018-06-14 12:24:16 +0530268 },
269 get_tree_default: function(option) {
270 // set default for a field based on user permission
271 let options = this.get_tree_options(option);
272 if(options.includes(frappe.defaults.get_default(option))) {
273 return frappe.defaults.get_default(option);
274 } else {
275 return options[0];
276 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400277 },
278 copy_parent_value_in_all_row: function(doc, dt, dn, table_fieldname, fieldname, parent_fieldname) {
279 var d = locals[dt][dn];
280 if(d[fieldname]){
281 var cl = doc[table_fieldname] || [];
282 for(var i = 0; i < cl.length; i++) {
283 cl[i][fieldname] = doc[parent_fieldname];
284 }
285 }
286 refresh_field(table_fieldname);
287 },
288
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530289});
290
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530291erpnext.utils.select_alternate_items = function(opts) {
292 const frm = opts.frm;
293 const warehouse_field = opts.warehouse_field || 'warehouse';
294 const item_field = opts.item_field || 'item_code';
295
296 this.data = [];
297 const dialog = new frappe.ui.Dialog({
298 title: __("Select Alternate Item"),
299 fields: [
300 {fieldtype:'Section Break', label: __('Items')},
301 {
302 fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true,
303 in_place_edit: true, data: this.data,
304 get_data: () => {
305 return this.data;
306 },
307 fields: [{
308 fieldtype:'Data',
309 fieldname:"docname",
310 hidden: 1
311 }, {
312 fieldtype:'Link',
313 fieldname:"item_code",
314 options: 'Item',
315 in_list_view: 1,
316 read_only: 1,
317 label: __('Item Code')
318 }, {
319 fieldtype:'Link',
320 fieldname:"alternate_item",
321 options: 'Item',
322 default: "",
323 in_list_view: 1,
324 label: __('Alternate Item'),
325 onchange: function() {
326 const item_code = this.get_value();
327 const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value();
328 if (item_code && warehouse) {
329 frappe.call({
330 method: "erpnext.stock.utils.get_latest_stock_qty",
331 args: {
332 item_code: item_code,
333 warehouse: warehouse
334 },
335 callback: (r) => {
336 this.grid_row.on_grid_fields_dict
337 .actual_qty.set_value(r.message || 0);
338 }
339 })
340 }
341 },
342 get_query: (e) => {
343 return {
344 query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items",
345 filters: {
346 item_code: e.item_code
347 }
348 };
349 }
350 }, {
351 fieldtype:'Link',
352 fieldname:"warehouse",
353 options: 'Warehouse',
354 default: "",
355 in_list_view: 1,
356 label: __('Warehouse'),
357 onchange: function() {
358 const warehouse = this.get_value();
359 const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value();
360 if (item_code && warehouse) {
361 frappe.call({
362 method: "erpnext.stock.utils.get_latest_stock_qty",
363 args: {
364 item_code: item_code,
365 warehouse: warehouse
366 },
367 callback: (r) => {
368 this.grid_row.on_grid_fields_dict
369 .actual_qty.set_value(r.message || 0);
370 }
371 })
372 }
373 },
374 }, {
375 fieldtype:'Float',
376 fieldname:"actual_qty",
377 default: 0,
378 read_only: 1,
379 in_list_view: 1,
380 label: __('Available Qty')
381 }]
382 },
383 ],
384 primary_action: function() {
385 const args = this.get_values()["alternative_items"];
386 const alternative_items = args.filter(d => {
387 if (d.alternate_item && d.item_code != d.alternate_item) {
388 return true;
389 }
390 });
391
392 alternative_items.forEach(d => {
393 let row = frappe.get_doc(opts.child_doctype, d.docname);
Doridel Cahanape42192b2018-05-16 13:28:39 +0800394 let qty = null;
395 if (row.doctype === 'Work Order Item') {
396 qty = row.required_qty;
397 } else {
398 qty = row.qty;
399 }
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530400 row[item_field] = d.alternate_item;
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530401 frm.script_manager.trigger(item_field, row.doctype, row.name)
402 .then(() => {
403 frappe.model.set_value(row.doctype, row.name, 'qty', qty);
404 frappe.model.set_value(row.doctype, row.name,
405 opts.original_item_field, d.item_code);
406 });
407 });
408
409 refresh_field(opts.child_docname);
410 this.hide();
411 },
412 primary_action_label: __('Update')
413 });
414
415 frm.doc[opts.child_docname].forEach(d => {
416 if (!opts.condition || opts.condition(d)) {
417 dialog.fields_dict.alternative_items.df.data.push({
418 "docname": d.name,
419 "item_code": d[item_field],
420 "warehouse": d[warehouse_field],
421 "actual_qty": d.actual_qty
422 });
423 }
424 })
425
426 this.data = dialog.fields_dict.alternative_items.df.data;
427 dialog.fields_dict.alternative_items.grid.refresh();
428 dialog.show();
429}
430
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800431erpnext.utils.update_child_items = function(opts) {
432 const frm = opts.frm;
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100433 const cannot_add_row = (typeof opts.cannot_add_row === 'undefined') ? true : opts.cannot_add_row;
434 const child_docname = (typeof opts.cannot_add_row === 'undefined') ? "items" : opts.child_docname;
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800435 this.data = [];
436 const dialog = new frappe.ui.Dialog({
437 title: __("Update Items"),
438 fields: [
439 {fieldtype:'Section Break', label: __('Items')},
440 {
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100441 fieldname: "trans_items",
442 fieldtype: "Table",
443 cannot_add_rows: cannot_add_row,
444 in_place_edit: true,
445 data: this.data,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800446 get_data: () => {
447 return this.data;
448 },
449 fields: [{
450 fieldtype:'Data',
451 fieldname:"docname",
452 hidden: 0,
453 }, {
454 fieldtype:'Link',
455 fieldname:"item_code",
456 options: 'Item',
457 in_list_view: 1,
458 read_only: 1,
459 label: __('Item Code')
460 }, {
461 fieldtype:'Float',
462 fieldname:"qty",
463 default: 0,
464 read_only: 0,
465 in_list_view: 1,
466 label: __('Qty')
467 }, {
468 fieldtype:'Currency',
469 fieldname:"rate",
470 default: 0,
471 read_only: 0,
472 in_list_view: 1,
473 label: __('Rate')
474 }]
475 },
476 ],
477 primary_action: function() {
478 const trans_items = this.get_values()["trans_items"];
479 frappe.call({
480 method: 'erpnext.controllers.accounts_controller.update_child_qty_rate',
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100481 freeze: true,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800482 args: {
483 'parent_doctype': frm.doc.doctype,
484 'trans_items': trans_items,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100485 'parent_doctype_name': frm.doc.name,
486 'child_docname': child_docname
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800487 },
488 callback: function() {
489 frm.reload_doc();
490 }
491 });
492 this.hide();
493 refresh_field("items");
494 },
495 primary_action_label: __('Update')
496 });
497
498 frm.doc[opts.child_docname].forEach(d => {
499 dialog.fields_dict.trans_items.df.data.push({
500 "docname": d.name,
501 "item_code": d.item_code,
502 "qty": d.qty,
503 "rate": d.rate,
504 });
505 this.data = dialog.fields_dict.trans_items.df.data;
506 dialog.fields_dict.trans_items.grid.refresh();
507 })
508 dialog.show();
509}
510
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530511erpnext.utils.map_current_doc = function(opts) {
512 if(opts.get_query_filters) {
513 opts.get_query = function() {
514 return {filters: opts.get_query_filters};
515 }
516 }
517 var _map = function() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530518 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530519 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530520 if(!cur_frm.doc.items[0].item_code) {
521 cur_frm.doc.items = cur_frm.doc.items.splice(1);
522 }
bhupen3c7e70f2016-11-30 14:32:11 +0530523
524 // find the doctype of the items table
525 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530526
bhupen3c7e70f2016-11-30 14:32:11 +0530527 // find the link fieldname from items table for the given
528 // source_doctype
529 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530530 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530531 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
532
Nabin Hait802b4352017-01-09 15:32:20 +0530533 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530534 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530535 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530536
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530537 $.each(cur_frm.doc.items, function(i, d) {
538 opts.source_name.forEach(function(src) {
539 if(d[link_fieldname]==src) {
540 already_set = true;
541 if (item_qty_map[d.item_code])
542 item_qty_map[d.item_code] += flt(d.qty);
543 else
544 item_qty_map[d.item_code] = flt(d.qty);
545 }
546 });
547 });
548
549 if(already_set) {
550 opts.source_name.forEach(function(src) {
551 frappe.model.with_doc(opts.source_doctype, src, function(r) {
552 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
553 $.each(source_doc.items || [], function(i, row) {
554 if(row.qty > flt(item_qty_map[row.item_code])) {
555 already_set = false;
556 return false;
557 }
558 })
559 })
560
561 if(already_set) {
562 frappe.msgprint(__("You have already selected items from {0} {1}",
563 [opts.source_doctype, src]));
564 return;
565 }
566
567 })
Nabin Hait802b4352017-01-09 15:32:20 +0530568 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530569 }
570
571 return frappe.call({
572 // Sometimes we hit the limit for URL length of a GET request
573 // as we send the full target_doc. Hence this is a POST request.
574 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530575 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530576 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530577 "method": opts.method,
578 "source_names": opts.source_name,
579 "target_doc": cur_frm.doc,
Rushabh Mehta49f97472018-08-30 18:50:48 +0530580 'args': opts.args
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530581 },
582 callback: function(r) {
583 if(!r.exc) {
584 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530585 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530586 cur_frm.refresh();
587 }
588 }
589 });
590 }
591 if(opts.source_doctype) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530592 var d = new frappe.ui.form.MultiSelectDialog({
593 doctype: opts.source_doctype,
594 target: opts.target,
595 date_field: opts.date_field || undefined,
596 setters: opts.setters,
597 get_query: opts.get_query,
598 action: function(selections, args) {
599 let values = selections;
600 if(values.length === 0){
Nabin Haita11dcb62017-12-04 13:36:50 +0530601 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530602 return;
603 }
604 opts.source_name = values;
605 opts.setters = args;
606 d.dialog.hide();
607 _map();
608 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530609 });
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530610 } else if(opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530611 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530612 _map();
613 }
614}
615
Rushabh Mehta180e4352016-07-26 17:57:42 +0530616frappe.form.link_formatters['Item'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530617 if(doc && doc.item_name && doc.item_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530618 return value? value + ': ' + doc.item_name: doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530619 } else {
620 return value;
621 }
622}
623
624frappe.form.link_formatters['Employee'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530625 if(doc && doc.employee_name && doc.employee_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530626 return value? value + ': ' + doc.employee_name: doc.employee_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530627 } else {
628 return value;
629 }
630}
631
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530632// add description on posting time
633$(document).on('app_ready', function() {
634 if(!frappe.datetime.is_timezone_same()) {
635 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
636 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
637 frappe.ui.form.on(d, "onload", function(frm) {
638 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530639 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530640 });
641 });
642 }
akshay14384c22016-07-28 13:53:17 +0530643});