blob: e8db09765329f8c48e60377876bd62c8dc756ef1 [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
Alanc6dc9ea2021-05-07 20:30:04 +053051 setup_serial_or_batch_no: function() {
52 let grid_row = cur_frm.open_grid_row();
53 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
Alanc6dc9ea2021-05-07 20:30:04 +053056 frappe.model.get_value('Item', {'name': grid_row.doc.item_code},
57 ['has_serial_no', 'has_batch_no'], ({has_serial_no, has_batch_no}) => {
58 Object.assign(grid_row.doc, {has_serial_no, has_batch_no});
Rushabh Mehtac38fc712014-04-16 17:21:25 +053059
Alanc6dc9ea2021-05-07 20:30:04 +053060 if (has_serial_no) {
61 attach_selector_button(__("Add Serial No"),
62 grid_row.grid_form.fields_dict.serial_no.$wrapper, this, grid_row);
63 } else if (has_batch_no) {
64 attach_selector_button(__("Pick Batch No"),
65 grid_row.grid_form.fields_dict.batch_no.$wrapper, this, grid_row);
Marica29a2e162019-11-12 14:43:41 +053066 }
Alanc6dc9ea2021-05-07 20:30:04 +053067 }
68 );
deepeshgarg007e64c3572019-05-14 08:50:45 +053069 },
Nabin Hait1ed1c4e2019-11-25 12:33:40 +053070
71 route_to_adjustment_jv: (args) => {
72 frappe.model.with_doctype('Journal Entry', () => {
73 // route to adjustment Journal Entry to handle Account Balance and Stock Value mismatch
74 let journal_entry = frappe.model.get_new_doc('Journal Entry');
75
76 args.accounts.forEach((je_account) => {
77 let child_row = frappe.model.add_child(journal_entry, "accounts");
78 child_row.account = je_account.account;
79 child_row.debit_in_account_currency = je_account.debit_in_account_currency;
80 child_row.credit_in_account_currency = je_account.credit_in_account_currency;
81 child_row.party_type = "" ;
82 });
83 frappe.set_route('Form','Journal Entry', journal_entry.name);
84 });
Frappe PR Bot255b99e2021-08-24 20:19:22 +053085 },
86
Ankush Menatd37541d2021-12-10 12:04:10 +053087 route_to_pending_reposts: (args) => {
88 frappe.set_route('List', 'Repost Item Valuation', args);
89 },
90
Frappe PR Bot255b99e2021-08-24 20:19:22 +053091 proceed_save_with_reminders_frequency_change: () => {
92 frappe.ui.hide_open_dialog();
Ankush Menatb147b852021-09-01 16:45:57 +053093
Frappe PR Bot255b99e2021-08-24 20:19:22 +053094 frappe.call({
Ankush Menatb147b852021-09-01 16:45:57 +053095 method: 'erpnext.hr.doctype.hr_settings.hr_settings.set_proceed_with_frequency_change',
Frappe PR Bot255b99e2021-08-24 20:19:22 +053096 callback: () => {
97 cur_frm.save();
98 }
99 });
Nabin Hait1ed1c4e2019-11-25 12:33:40 +0530100 }
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530101});
Rushabh Mehta7d368752014-11-24 14:16:47 +0530102
Nabin Haitb072c742014-12-16 12:49:13 +0530103
104$.extend(erpnext.utils, {
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530105 set_party_dashboard_indicators: function(frm) {
106 if(frm.doc.__onload && frm.doc.__onload.dashboard_info) {
deepeshgarg007920dc142018-11-23 10:17:28 +0530107 var company_wise_info = frm.doc.__onload.dashboard_info;
deepeshgarg007f31caff2018-11-27 15:04:12 +0530108 if(company_wise_info.length > 1) {
deepeshgarg007a1cffc32018-11-23 16:51:35 +0530109 company_wise_info.forEach(function(info) {
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530110 erpnext.utils.add_indicator_for_multicompany(frm, info);
deepeshgarg007a1cffc32018-11-23 16:51:35 +0530111 });
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530112 } else if (company_wise_info.length === 1) {
deepeshgarg00764238ee2018-12-25 16:28:39 +0530113 frm.dashboard.add_indicator(__('Annual Billing: {0}',
114 [format_currency(company_wise_info[0].billing_this_year, company_wise_info[0].currency)]), 'blue');
115 frm.dashboard.add_indicator(__('Total Unpaid: {0}',
116 [format_currency(company_wise_info[0].total_unpaid, company_wise_info[0].currency)]),
deepeshgarg0078300f5e2019-01-01 20:28:49 +0530117 company_wise_info[0].total_unpaid ? 'orange' : 'green');
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530118
deepeshgarg00764238ee2018-12-25 16:28:39 +0530119 if(company_wise_info[0].loyalty_points) {
120 frm.dashboard.add_indicator(__('Loyalty Points: {0}',
121 [company_wise_info[0].loyalty_points]), 'blue');
122 }
deepeshgarg007f31caff2018-11-27 15:04:12 +0530123 }
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530124 }
125 },
126
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530127 add_indicator_for_multicompany: function(frm, info) {
128 frm.dashboard.stats_area.removeClass('hidden');
129 frm.dashboard.stats_area_row.addClass('flex');
130 frm.dashboard.stats_area_row.css('flex-wrap', 'wrap');
131
132 var color = info.total_unpaid ? 'orange' : 'green';
133
134 var indicator = $('<div class="flex-column col-xs-6">'+
135 '<div style="margin-top:10px"><h6>'+info.company+'</h6></div>'+
136
137 '<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
138 'Annual Billing: '+format_currency(info.billing_this_year, info.currency)+'</span></div>'+
139
140 '<div class="badge-link small" style="margin-bottom:10px">'+
141 '<span class="indicator '+color+'">Total Unpaid: '
142 +format_currency(info.total_unpaid, info.currency)+'</span></div>'+
143
144
145 '</div>').appendTo(frm.dashboard.stats_area_row);
146
147 if(info.loyalty_points){
148 $('<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
149 'Loyalty Points: '+info.loyalty_points+'</span></div>').appendTo(indicator);
150 }
151
152 return indicator;
153 },
154
Shreya Shah149f7ee2018-03-27 11:29:25 +0530155 get_party_name: function(party_type) {
156 var dict = {'Customer': 'customer_name', 'Supplier': 'supplier_name', 'Employee': 'employee_name',
157 'Member': 'member_name'};
158 return dict[party_type];
159 },
160
Rushabh Mehta49f97472018-08-30 18:50:48 +0530161 copy_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname) {
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +0530162 var d = locals[dt][dn];
163 if(d[fieldname]){
164 var cl = doc[table_fieldname] || [];
165 for(var i = 0; i < cl.length; i++) {
166 if(!cl[i][fieldname]) cl[i][fieldname] = d[fieldname];
167 }
168 }
169 refresh_field(table_fieldname);
Makarand Bauskar157c3342017-05-26 21:32:33 +0530170 },
171
172 get_terms: function(tc_name, doc, callback) {
173 if(tc_name) {
174 return frappe.call({
175 method: 'erpnext.setup.doctype.terms_and_conditions.terms_and_conditions.get_terms_and_conditions',
176 args: {
177 template_name: tc_name,
178 doc: doc
179 },
180 callback: function(r) {
181 callback(r)
182 }
183 });
184 }
tundebabzy1a947db2017-08-29 13:48:27 +0100185 },
186
rohitwaghchaure713cfc72018-09-11 17:40:37 +0530187 make_bank_account: function(doctype, docname) {
188 frappe.call({
189 method: "erpnext.accounts.doctype.bank_account.bank_account.make_bank_account",
190 args: {
191 doctype: doctype,
192 docname: docname
193 },
194 freeze: true,
195 callback: function(r) {
196 var doclist = frappe.model.sync(r.message);
197 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
198 }
199 })
200 },
201
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530202 add_dimensions: function(report_name, index) {
203 let filters = frappe.query_reports[report_name].filters;
204
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530205 frappe.call({
206 method: "erpnext.accounts.doctype.accounting_dimension.accounting_dimension.get_dimensions",
207 callback: function(r) {
208 let accounting_dimensions = r.message[0];
209 accounting_dimensions.forEach((dimension) => {
210 let found = filters.some(el => el.fieldname === dimension['fieldname']);
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530211
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530212 if (!found) {
Deepesh Garg23ab5c52020-12-31 11:29:06 +0530213 filters.splice(index, 0, {
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530214 "fieldname": dimension["fieldname"],
215 "label": __(dimension["label"]),
Rohit Waghchaure69be22b2022-05-14 17:19:34 +0530216 "fieldtype": "MultiSelectList",
217 get_data: function(txt) {
218 return frappe.db.get_link_options(dimension["document_type"], txt);
219 },
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530220 });
221 }
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530222 });
223 }
224 });
225 },
226
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530227 make_subscription: function(doctype, docname) {
228 frappe.call({
Rucha Mahabal65a627c2019-07-17 13:50:32 +0530229 method: "frappe.automation.doctype.auto_repeat.auto_repeat.make_auto_repeat",
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530230 args: {
231 doctype: doctype,
232 docname: docname
233 },
234 callback: function(r) {
235 var doclist = frappe.model.sync(r.message);
236 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
237 }
238 })
239 },
240
Saif90cf2dd2018-09-30 21:46:31 +0500241 make_pricing_rule: function(doctype, docname) {
242 frappe.call({
243 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.make_pricing_rule",
244 args: {
245 doctype: doctype,
246 docname: docname
247 },
248 callback: function(r) {
249 var doclist = frappe.model.sync(r.message);
250 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
251 }
252 })
253 },
254
tundebabzy1a947db2017-08-29 13:48:27 +0100255 /**
256 * Checks if the first row of a given child table is empty
257 * @param child_table - Child table Doctype
258 * @return {Boolean}
259 **/
260 first_row_is_empty: function(child_table){
261 if($.isArray(child_table) && child_table.length > 0) {
262 return !child_table[0].item_code;
263 }
264 return false;
265 },
266
267 /**
268 * Removes the first row of a child table if it is empty
269 * @param {_Frm} frm - The current form
270 * @param {String} child_table_name - The child table field name
271 * @return {Boolean}
272 **/
273 remove_empty_first_row: function(frm, child_table_name){
274 const rows = frm['doc'][child_table_name];
275 if (this.first_row_is_empty(rows)){
276 frm['doc'][child_table_name] = rows.splice(1);
277 }
278 return rows;
279 },
Zarrar6ffdf942018-06-14 12:24:16 +0530280 get_tree_options: function(option) {
281 // get valid options for tree based on user permission & locals dict
282 let unscrub_option = frappe.model.unscrub(option);
283 let user_permission = frappe.defaults.get_user_permissions();
deepeshgarg0070d64d622019-02-20 17:55:14 +0530284 let options;
285
Zarrar6ffdf942018-06-14 12:24:16 +0530286 if(user_permission && user_permission[unscrub_option]) {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530287 options = user_permission[unscrub_option].map(perm => perm.doc);
Zarrar6ffdf942018-06-14 12:24:16 +0530288 } else {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530289 options = $.map(locals[`:${unscrub_option}`], function(c) { return c.name; }).sort();
Zarrar6ffdf942018-06-14 12:24:16 +0530290 }
deepeshgarg0070d64d622019-02-20 17:55:14 +0530291
292 // filter unique values, as there may be multiple user permissions for any value
293 return options.filter((value, index, self) => self.indexOf(value) === index);
Zarrar6ffdf942018-06-14 12:24:16 +0530294 },
295 get_tree_default: function(option) {
296 // set default for a field based on user permission
297 let options = this.get_tree_options(option);
298 if(options.includes(frappe.defaults.get_default(option))) {
299 return frappe.defaults.get_default(option);
300 } else {
301 return options[0];
302 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400303 },
Andy Zhubffe9332021-04-12 22:49:26 +1200304 overrides_parent_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname, parent_fieldname) {
Marica24e45162021-04-14 18:53:15 +0530305 if (doc[parent_fieldname]) {
Andy Zhubffe9332021-04-12 22:49:26 +1200306 let cl = doc[table_fieldname] || [];
Marica24e45162021-04-14 18:53:15 +0530307 for (let i = 0; i < cl.length; i++) {
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400308 cl[i][fieldname] = doc[parent_fieldname];
309 }
Andy Zhubffe9332021-04-12 22:49:26 +1200310 frappe.refresh_field(table_fieldname);
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400311 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400312 },
Nabin Hait34c551d2019-07-03 10:34:31 +0530313 create_new_doc: function (doctype, update_fields) {
314 frappe.model.with_doctype(doctype, function() {
315 var new_doc = frappe.model.get_new_doc(doctype);
316 for (let [key, value] of Object.entries(update_fields)) {
317 new_doc[key] = value;
318 }
319 frappe.ui.form.make_quick_entry(doctype, null, null, new_doc);
320 });
321 }
322
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530323});
324
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530325erpnext.utils.select_alternate_items = function(opts) {
326 const frm = opts.frm;
327 const warehouse_field = opts.warehouse_field || 'warehouse';
328 const item_field = opts.item_field || 'item_code';
329
330 this.data = [];
331 const dialog = new frappe.ui.Dialog({
332 title: __("Select Alternate Item"),
333 fields: [
334 {fieldtype:'Section Break', label: __('Items')},
335 {
336 fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true,
337 in_place_edit: true, data: this.data,
338 get_data: () => {
339 return this.data;
340 },
341 fields: [{
342 fieldtype:'Data',
343 fieldname:"docname",
344 hidden: 1
345 }, {
346 fieldtype:'Link',
347 fieldname:"item_code",
348 options: 'Item',
349 in_list_view: 1,
350 read_only: 1,
351 label: __('Item Code')
352 }, {
353 fieldtype:'Link',
354 fieldname:"alternate_item",
355 options: 'Item',
356 default: "",
357 in_list_view: 1,
358 label: __('Alternate Item'),
359 onchange: function() {
360 const item_code = this.get_value();
361 const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value();
362 if (item_code && warehouse) {
363 frappe.call({
364 method: "erpnext.stock.utils.get_latest_stock_qty",
365 args: {
366 item_code: item_code,
367 warehouse: warehouse
368 },
369 callback: (r) => {
370 this.grid_row.on_grid_fields_dict
371 .actual_qty.set_value(r.message || 0);
372 }
373 })
374 }
375 },
376 get_query: (e) => {
377 return {
378 query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items",
379 filters: {
380 item_code: e.item_code
381 }
382 };
383 }
384 }, {
385 fieldtype:'Link',
386 fieldname:"warehouse",
387 options: 'Warehouse',
388 default: "",
389 in_list_view: 1,
390 label: __('Warehouse'),
391 onchange: function() {
392 const warehouse = this.get_value();
393 const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value();
394 if (item_code && warehouse) {
395 frappe.call({
396 method: "erpnext.stock.utils.get_latest_stock_qty",
397 args: {
398 item_code: item_code,
399 warehouse: warehouse
400 },
401 callback: (r) => {
402 this.grid_row.on_grid_fields_dict
403 .actual_qty.set_value(r.message || 0);
404 }
405 })
406 }
407 },
408 }, {
409 fieldtype:'Float',
410 fieldname:"actual_qty",
411 default: 0,
412 read_only: 1,
413 in_list_view: 1,
414 label: __('Available Qty')
415 }]
416 },
417 ],
418 primary_action: function() {
419 const args = this.get_values()["alternative_items"];
420 const alternative_items = args.filter(d => {
421 if (d.alternate_item && d.item_code != d.alternate_item) {
422 return true;
423 }
424 });
425
426 alternative_items.forEach(d => {
427 let row = frappe.get_doc(opts.child_doctype, d.docname);
Doridel Cahanape42192b2018-05-16 13:28:39 +0800428 let qty = null;
429 if (row.doctype === 'Work Order Item') {
430 qty = row.required_qty;
431 } else {
432 qty = row.qty;
433 }
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530434 row[item_field] = d.alternate_item;
mergify[bot]c1a0f642021-12-17 19:06:19 +0530435 frappe.model.set_value(row.doctype, row.name, 'qty', qty);
436 frappe.model.set_value(row.doctype, row.name, opts.original_item_field, d.item_code);
437 frm.trigger(item_field, row.doctype, row.name);
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530438 });
439
440 refresh_field(opts.child_docname);
441 this.hide();
442 },
443 primary_action_label: __('Update')
444 });
445
446 frm.doc[opts.child_docname].forEach(d => {
447 if (!opts.condition || opts.condition(d)) {
448 dialog.fields_dict.alternative_items.df.data.push({
449 "docname": d.name,
450 "item_code": d[item_field],
451 "warehouse": d[warehouse_field],
452 "actual_qty": d.actual_qty
453 });
454 }
455 })
456
457 this.data = dialog.fields_dict.alternative_items.df.data;
458 dialog.fields_dict.alternative_items.grid.refresh();
459 dialog.show();
460}
461
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800462erpnext.utils.update_child_items = function(opts) {
463 const frm = opts.frm;
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100464 const cannot_add_row = (typeof opts.cannot_add_row === 'undefined') ? true : opts.cannot_add_row;
465 const child_docname = (typeof opts.cannot_add_row === 'undefined') ? "items" : opts.child_docname;
Saqib56fea7d2020-10-09 21:19:25 +0530466 const child_meta = frappe.get_meta(`${frm.doc.doctype} Item`);
467 const get_precision = (fieldname) => child_meta.fields.find(f => f.fieldname == fieldname).precision;
468
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800469 this.data = [];
Saqib Ansarif53299e2020-04-15 22:08:12 +0530470 const fields = [{
471 fieldtype:'Data',
472 fieldname:"docname",
473 read_only: 1,
474 hidden: 1,
475 }, {
476 fieldtype:'Link',
477 fieldname:"item_code",
478 options: 'Item',
479 in_list_view: 1,
480 read_only: 0,
481 disabled: 0,
Afshandc7280e2021-08-18 17:44:40 +0530482 label: __('Item Code'),
483 get_query: function() {
484 let filters;
485 if (frm.doc.doctype == 'Sales Order') {
486 filters = {"is_sales_item": 1};
487 } else if (frm.doc.doctype == 'Purchase Order') {
Sagar Sharmad074c932022-03-31 19:57:42 +0530488 if (frm.doc.is_subcontracted) {
Afshandc7280e2021-08-18 17:44:40 +0530489 filters = {"is_sub_contracted_item": 1};
490 } else {
491 filters = {"is_purchase_item": 1};
492 }
493 }
494 return {
495 query: "erpnext.controllers.queries.item_query",
496 filters: filters
497 };
498 }
Saqib Ansarif53299e2020-04-15 22:08:12 +0530499 }, {
Saqib61314242020-09-15 11:14:31 +0530500 fieldtype:'Link',
501 fieldname:'uom',
502 options: 'UOM',
503 read_only: 0,
504 label: __('UOM'),
505 reqd: 1,
506 onchange: function () {
507 frappe.call({
508 method: "erpnext.stock.get_item_details.get_conversion_factor",
509 args: { item_code: this.doc.item_code, uom: this.value },
510 callback: r => {
511 if(!r.exc) {
512 if (this.doc.conversion_factor == r.message.conversion_factor) return;
marination4f395cc2020-09-24 12:43:41 +0530513
Saqib61314242020-09-15 11:14:31 +0530514 const docname = this.doc.docname;
515 dialog.fields_dict.trans_items.df.data.some(doc => {
516 if (doc.docname == docname) {
517 doc.conversion_factor = r.message.conversion_factor;
518 dialog.fields_dict.trans_items.grid.refresh();
519 return true;
520 }
521 })
522 }
523 }
524 });
525 }
526 }, {
Saqib Ansarif53299e2020-04-15 22:08:12 +0530527 fieldtype:'Float',
528 fieldname:"qty",
529 default: 0,
530 read_only: 0,
531 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530532 label: __('Qty'),
533 precision: get_precision("qty")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530534 }, {
535 fieldtype:'Currency',
536 fieldname:"rate",
Afshan84184552021-02-24 16:51:11 +0530537 options: "currency",
Saqib Ansarif53299e2020-04-15 22:08:12 +0530538 default: 0,
539 read_only: 0,
540 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530541 label: __('Rate'),
542 precision: get_precision("rate")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530543 }];
544
545 if (frm.doc.doctype == 'Sales Order' || frm.doc.doctype == 'Purchase Order' ) {
546 fields.splice(2, 0, {
547 fieldtype: 'Date',
548 fieldname: frm.doc.doctype == 'Sales Order' ? "delivery_date" : "schedule_date",
549 in_list_view: 1,
Saqib Ansaric579b082020-05-29 22:21:50 +0530550 label: frm.doc.doctype == 'Sales Order' ? __("Delivery Date") : __("Reqd by date"),
551 reqd: 1
Saqib Ansarif53299e2020-04-15 22:08:12 +0530552 })
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530553 fields.splice(3, 0, {
554 fieldtype: 'Float',
555 fieldname: "conversion_factor",
556 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530557 label: __("Conversion Factor"),
558 precision: get_precision('conversion_factor')
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530559 })
Saqib Ansarif53299e2020-04-15 22:08:12 +0530560 }
561
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800562 const dialog = new frappe.ui.Dialog({
563 title: __("Update Items"),
564 fields: [
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800565 {
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100566 fieldname: "trans_items",
567 fieldtype: "Table",
Maricafc96c1a2020-03-06 10:57:59 +0530568 label: "Items",
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100569 cannot_add_rows: cannot_add_row,
Saqibbc919e22020-11-05 17:38:35 +0530570 in_place_edit: false,
Maricafc96c1a2020-03-06 10:57:59 +0530571 reqd: 1,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100572 data: this.data,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800573 get_data: () => {
574 return this.data;
575 },
Saqib Ansarif53299e2020-04-15 22:08:12 +0530576 fields: fields
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800577 },
578 ],
579 primary_action: function() {
Ankush Menat6de7b8e2021-08-24 12:18:40 +0530580 const trans_items = this.get_values()["trans_items"].filter((item) => !!item.item_code);
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800581 frappe.call({
582 method: 'erpnext.controllers.accounts_controller.update_child_qty_rate',
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100583 freeze: true,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800584 args: {
585 'parent_doctype': frm.doc.doctype,
586 'trans_items': trans_items,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100587 'parent_doctype_name': frm.doc.name,
588 'child_docname': child_docname
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800589 },
590 callback: function() {
591 frm.reload_doc();
592 }
593 });
594 this.hide();
595 refresh_field("items");
596 },
597 primary_action_label: __('Update')
598 });
599
600 frm.doc[opts.child_docname].forEach(d => {
601 dialog.fields_dict.trans_items.df.data.push({
602 "docname": d.name,
Prssanna Desai4f72c0b2019-12-23 18:27:12 +0530603 "name": d.name,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800604 "item_code": d.item_code,
Saqib Ansarif53299e2020-04-15 22:08:12 +0530605 "delivery_date": d.delivery_date,
606 "schedule_date": d.schedule_date,
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530607 "conversion_factor": d.conversion_factor,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800608 "qty": d.qty,
609 "rate": d.rate,
Saqib61314242020-09-15 11:14:31 +0530610 "uom": d.uom
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800611 });
612 this.data = dialog.fields_dict.trans_items.df.data;
613 dialog.fields_dict.trans_items.grid.refresh();
614 })
615 dialog.show();
616}
617
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530618erpnext.utils.map_current_doc = function(opts) {
Sagar Vora78777d62021-03-09 20:35:08 +0530619 function _map() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530620 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530621 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530622 if(!cur_frm.doc.items[0].item_code) {
623 cur_frm.doc.items = cur_frm.doc.items.splice(1);
624 }
bhupen3c7e70f2016-11-30 14:32:11 +0530625
626 // find the doctype of the items table
627 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530628
bhupen3c7e70f2016-11-30 14:32:11 +0530629 // find the link fieldname from items table for the given
630 // source_doctype
631 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530632 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530633 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
634
Nabin Hait802b4352017-01-09 15:32:20 +0530635 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530636 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530637 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530638
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530639 $.each(cur_frm.doc.items, function(i, d) {
640 opts.source_name.forEach(function(src) {
641 if(d[link_fieldname]==src) {
642 already_set = true;
643 if (item_qty_map[d.item_code])
644 item_qty_map[d.item_code] += flt(d.qty);
645 else
646 item_qty_map[d.item_code] = flt(d.qty);
647 }
648 });
649 });
650
651 if(already_set) {
652 opts.source_name.forEach(function(src) {
653 frappe.model.with_doc(opts.source_doctype, src, function(r) {
654 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
655 $.each(source_doc.items || [], function(i, row) {
656 if(row.qty > flt(item_qty_map[row.item_code])) {
657 already_set = false;
658 return false;
659 }
660 })
661 })
662
663 if(already_set) {
664 frappe.msgprint(__("You have already selected items from {0} {1}",
665 [opts.source_doctype, src]));
666 return;
667 }
668
669 })
Nabin Hait802b4352017-01-09 15:32:20 +0530670 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530671 }
672
673 return frappe.call({
674 // Sometimes we hit the limit for URL length of a GET request
675 // as we send the full target_doc. Hence this is a POST request.
676 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530677 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530678 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530679 "method": opts.method,
680 "source_names": opts.source_name,
681 "target_doc": cur_frm.doc,
Maricadb002702020-02-17 15:58:08 +0530682 "args": opts.args
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530683 },
684 callback: function(r) {
685 if(!r.exc) {
686 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530687 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530688 cur_frm.refresh();
689 }
690 }
691 });
692 }
Sagar Vora78777d62021-03-09 20:35:08 +0530693
694 let query_args = {};
695 if (opts.get_query_filters) {
696 query_args.filters = opts.get_query_filters;
697 }
698
699 if (opts.get_query_method) {
700 query_args.query = opts.get_query_method;
701 }
702
703 if (query_args.filters || query_args.query) {
704 opts.get_query = () => query_args;
705 }
706
707 if (opts.source_doctype) {
708 const d = new frappe.ui.form.MultiSelectDialog({
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530709 doctype: opts.source_doctype,
710 target: opts.target,
711 date_field: opts.date_field || undefined,
712 setters: opts.setters,
713 get_query: opts.get_query,
marination4f395cc2020-09-24 12:43:41 +0530714 add_filters_group: 1,
Saqib7292f542021-09-13 12:13:43 +0530715 allow_child_item_selection: opts.allow_child_item_selection,
716 child_fieldname: opts.child_fielname,
717 child_columns: opts.child_columns,
Rohit Waghchaure69ffddf2021-10-07 17:33:58 +0530718 size: opts.size,
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530719 action: function(selections, args) {
720 let values = selections;
Saqibe03d9aa2021-09-17 13:03:27 +0530721 if (values.length === 0) {
Nabin Haita11dcb62017-12-04 13:36:50 +0530722 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530723 return;
724 }
725 opts.source_name = values;
Saqibe03d9aa2021-09-17 13:03:27 +0530726 if (opts.allow_child_item_selection) {
727 // args contains filtered child docnames
728 opts.args = args;
729 }
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530730 d.dialog.hide();
731 _map();
732 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530733 });
Sagar Vora78777d62021-03-09 20:35:08 +0530734
735 return d;
736 }
737
738 if (opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530739 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530740 _map();
741 }
742}
743
Rushabh Mehta180e4352016-07-26 17:57:42 +0530744frappe.form.link_formatters['Item'] = function(value, doc) {
Alan157b3882021-04-21 21:00:22 +0530745 if (doc && value && doc.item_name && doc.item_name !== value && doc.item_code === value) {
marination824f48f2020-10-12 20:08:03 +0530746 return value + ': ' + doc.item_name;
747 } else if (!value && doc.doctype && doc.item_name) {
748 // format blank value in child table
749 return doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530750 } else {
marination824f48f2020-10-12 20:08:03 +0530751 // if value is blank in report view or item code and name are the same, return as is
Rushabh Mehta180e4352016-07-26 17:57:42 +0530752 return value;
753 }
754}
755
756frappe.form.link_formatters['Employee'] = function(value, doc) {
Rucha Mahabal5ba1bc12021-11-23 09:30:30 +0530757 if (doc && value && doc.employee_name && doc.employee_name !== value && doc.employee === value) {
758 return value + ': ' + doc.employee_name;
759 } else if (!value && doc.doctype && doc.employee_name) {
760 // format blank value in child table
761 return doc.employee;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530762 } else {
Rucha Mahabal5ba1bc12021-11-23 09:30:30 +0530763 // if value is blank in report view or project name and name are the same, return as is
Rushabh Mehta180e4352016-07-26 17:57:42 +0530764 return value;
765 }
766}
767
Rucha Mahabal59961f72021-05-20 23:43:19 +0530768frappe.form.link_formatters['Project'] = function(value, doc) {
769 if (doc && value && doc.project_name && doc.project_name !== value && doc.project === value) {
770 return value + ': ' + doc.project_name;
771 } else if (!value && doc.doctype && doc.project_name) {
772 // format blank value in child table
773 return doc.project;
774 } else {
775 // if value is blank in report view or project name and name are the same, return as is
776 return value;
777 }
778};
779
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530780// add description on posting time
781$(document).on('app_ready', function() {
782 if(!frappe.datetime.is_timezone_same()) {
783 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
784 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
785 frappe.ui.form.on(d, "onload", function(frm) {
786 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530787 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530788 });
789 });
790 }
akshay14384c22016-07-28 13:53:17 +0530791});
Alanc6dc9ea2021-05-07 20:30:04 +0530792
Himanshuec25d592021-06-14 19:05:52 +0530793// Show SLA dashboard
794$(document).on('app_ready', function() {
795 frappe.call({
796 method: 'erpnext.support.doctype.service_level_agreement.service_level_agreement.get_sla_doctypes',
797 callback: function(r) {
798 if (!r.message)
799 return;
800
801 $.each(r.message, function(_i, d) {
802 frappe.ui.form.on(d, {
803 onload: function(frm) {
804 if (!frm.doc.service_level_agreement)
805 return;
806
807 frappe.call({
808 method: 'erpnext.support.doctype.service_level_agreement.service_level_agreement.get_service_level_agreement_filters',
809 args: {
810 doctype: frm.doc.doctype,
811 name: frm.doc.service_level_agreement,
812 customer: frm.doc.customer
813 },
814 callback: function (r) {
815 if (r && r.message) {
816 frm.set_query('priority', function() {
817 return {
818 filters: {
819 'name': ['in', r.message.priority],
820 }
821 };
822 });
823 frm.set_query('service_level_agreement', function() {
824 return {
825 filters: {
826 'name': ['in', r.message.service_level_agreements],
827 }
828 };
829 });
830 }
831 }
832 });
833 },
834
835 refresh: function(frm) {
836 if (frm.doc.status !== 'Closed' && frm.doc.service_level_agreement
Saqib Ansari476e81a2021-12-06 18:55:30 +0530837 && ['First Response Due', 'Resolution Due'].includes(frm.doc.agreement_status)) {
Himanshuec25d592021-06-14 19:05:52 +0530838 frappe.call({
839 'method': 'frappe.client.get',
840 args: {
841 doctype: 'Service Level Agreement',
842 name: frm.doc.service_level_agreement
843 },
844 callback: function(data) {
845 let statuses = data.message.pause_sla_on;
846 const hold_statuses = [];
847 $.each(statuses, (_i, entry) => {
848 hold_statuses.push(entry.status);
849 });
850 if (hold_statuses.includes(frm.doc.status)) {
851 frm.dashboard.clear_headline();
852 let message = {'indicator': 'orange', 'msg': __('SLA is on hold since {0}', [moment(frm.doc.on_hold_since).fromNow(true)])};
853 frm.dashboard.set_headline_alert(
854 '<div class="row">' +
855 '<div class="col-xs-12">' +
856 '<span class="indicator whitespace-nowrap '+ message.indicator +'"><span>'+ message.msg +'</span></span> ' +
857 '</div>' +
858 '</div>'
859 );
860 } else {
861 set_time_to_resolve_and_response(frm, data.message.apply_sla_for_resolution);
862 }
863 }
864 });
865 } else if (frm.doc.service_level_agreement) {
866 frm.dashboard.clear_headline();
867
868 let agreement_status = (frm.doc.agreement_status == 'Fulfilled') ?
869 {'indicator': 'green', 'msg': 'Service Level Agreement has been fulfilled'} :
870 {'indicator': 'red', 'msg': 'Service Level Agreement Failed'};
871
872 frm.dashboard.set_headline_alert(
873 '<div class="row">' +
874 '<div class="col-xs-12">' +
875 '<span class="indicator whitespace-nowrap '+ agreement_status.indicator +'"><span class="hidden-xs">'+ agreement_status.msg +'</span></span> ' +
876 '</div>' +
877 '</div>'
878 );
879 }
880 },
881 });
882 });
883 }
884 });
885});
886
887function set_time_to_resolve_and_response(frm, apply_sla_for_resolution) {
888 frm.dashboard.clear_headline();
889
Saqib24114772021-12-15 11:45:34 +0530890 let time_to_respond;
Saqib Ansari476e81a2021-12-06 18:55:30 +0530891 if (!frm.doc.first_responded_on) {
Himanshuec25d592021-06-14 19:05:52 +0530892 time_to_respond = get_time_left(frm.doc.response_by, frm.doc.agreement_status);
Saqib24114772021-12-15 11:45:34 +0530893 } else {
894 time_to_respond = get_status(frm.doc.response_by, frm.doc.first_responded_on);
Himanshuec25d592021-06-14 19:05:52 +0530895 }
896
897 let alert = `
898 <div class="row">
899 <div class="col-xs-12 col-sm-6">
900 <span class="indicator whitespace-nowrap ${time_to_respond.indicator}">
901 <span>Time to Respond: ${time_to_respond.diff_display}</span>
902 </span>
903 </div>`;
904
905
906 if (apply_sla_for_resolution) {
Saqib24114772021-12-15 11:45:34 +0530907 let time_to_resolve;
Saqib Ansari476e81a2021-12-06 18:55:30 +0530908 if (!frm.doc.resolution_date) {
Himanshuec25d592021-06-14 19:05:52 +0530909 time_to_resolve = get_time_left(frm.doc.resolution_by, frm.doc.agreement_status);
Saqib24114772021-12-15 11:45:34 +0530910 } else {
911 time_to_resolve = get_status(frm.doc.resolution_by, frm.doc.resolution_date);
Himanshuec25d592021-06-14 19:05:52 +0530912 }
913
914 alert += `
915 <div class="col-xs-12 col-sm-6">
916 <span class="indicator whitespace-nowrap ${time_to_resolve.indicator}">
917 <span>Time to Resolve: ${time_to_resolve.diff_display}</span>
918 </span>
919 </div>`;
920 }
921
922 alert += '</div>';
923
924 frm.dashboard.set_headline_alert(alert);
925}
926
927function get_time_left(timestamp, agreement_status) {
928 const diff = moment(timestamp).diff(moment());
929 const diff_display = diff >= 44500 ? moment.duration(diff).humanize() : 'Failed';
930 let indicator = (diff_display == 'Failed' && agreement_status != 'Fulfilled') ? 'red' : 'green';
931 return {'diff_display': diff_display, 'indicator': indicator};
932}
933
Saqib24114772021-12-15 11:45:34 +0530934function get_status(expected, actual) {
935 const time_left = moment(expected).diff(moment(actual));
Saqib Ansari476e81a2021-12-06 18:55:30 +0530936 if (time_left >= 0) {
Himanshuec25d592021-06-14 19:05:52 +0530937 return {'diff_display': 'Fulfilled', 'indicator': 'green'};
938 } else {
939 return {'diff_display': 'Failed', 'indicator': 'red'};
940 }
941}
942
Alanc6dc9ea2021-05-07 20:30:04 +0530943function attach_selector_button(inner_text, append_loction, context, grid_row) {
944 let $btn_div = $("<div>").css({"margin-bottom": "10px", "margin-top": "10px"})
945 .appendTo(append_loction);
946 let $btn = $(`<button class="btn btn-sm btn-default">${inner_text}</button>`)
947 .appendTo($btn_div);
948
949 $btn.on("click", function() {
950 context.show_serial_batch_selector(grid_row.frm, grid_row.doc, "", "", true);
951 });
952}