blob: c11d123982f729106b5b8e032cac242f61837db4 [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 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053090});
Rushabh Mehta7d368752014-11-24 14:16:47 +053091
Nabin Haitb072c742014-12-16 12:49:13 +053092
93$.extend(erpnext.utils, {
Rushabh Mehta559aa3a2016-09-19 16:04:58 +053094 set_party_dashboard_indicators: function(frm) {
95 if(frm.doc.__onload && frm.doc.__onload.dashboard_info) {
deepeshgarg007920dc142018-11-23 10:17:28 +053096 var company_wise_info = frm.doc.__onload.dashboard_info;
deepeshgarg007f31caff2018-11-27 15:04:12 +053097 if(company_wise_info.length > 1) {
deepeshgarg007a1cffc32018-11-23 16:51:35 +053098 company_wise_info.forEach(function(info) {
deepeshgarg00712f5cef2018-12-25 16:06:19 +053099 erpnext.utils.add_indicator_for_multicompany(frm, info);
deepeshgarg007a1cffc32018-11-23 16:51:35 +0530100 });
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530101 } else if (company_wise_info.length === 1) {
deepeshgarg00764238ee2018-12-25 16:28:39 +0530102 frm.dashboard.add_indicator(__('Annual Billing: {0}',
103 [format_currency(company_wise_info[0].billing_this_year, company_wise_info[0].currency)]), 'blue');
104 frm.dashboard.add_indicator(__('Total Unpaid: {0}',
105 [format_currency(company_wise_info[0].total_unpaid, company_wise_info[0].currency)]),
deepeshgarg0078300f5e2019-01-01 20:28:49 +0530106 company_wise_info[0].total_unpaid ? 'orange' : 'green');
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530107
deepeshgarg00764238ee2018-12-25 16:28:39 +0530108 if(company_wise_info[0].loyalty_points) {
109 frm.dashboard.add_indicator(__('Loyalty Points: {0}',
110 [company_wise_info[0].loyalty_points]), 'blue');
111 }
deepeshgarg007f31caff2018-11-27 15:04:12 +0530112 }
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530113 }
114 },
115
Rohit Waghchaure708eefb2023-07-10 16:54:55 +0530116 view_serial_batch_nos: function(frm) {
117 let bundle_ids = frm.doc.items.filter(d => d.serial_and_batch_bundle);
118
119 if (bundle_ids?.length) {
120 frm.add_custom_button(__('Serial / Batch Nos'), () => {
121 frappe.route_options = {
122 "voucher_no": frm.doc.name,
123 "voucher_type": frm.doc.doctype,
124 "from_date": frm.doc.posting_date || frm.doc.transaction_date,
125 "to_date": frm.doc.posting_date || frm.doc.transaction_date,
126 "company": frm.doc.company,
127 };
128 frappe.set_route("query-report", "Serial and Batch Summary");
129 }, __('View'));
130 }
131 },
132
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530133 add_indicator_for_multicompany: function(frm, info) {
Deepak867f2c62022-05-17 11:52:52 +0530134 frm.dashboard.stats_area.show();
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530135 frm.dashboard.stats_area_row.addClass('flex');
136 frm.dashboard.stats_area_row.css('flex-wrap', 'wrap');
137
138 var color = info.total_unpaid ? 'orange' : 'green';
139
140 var indicator = $('<div class="flex-column col-xs-6">'+
141 '<div style="margin-top:10px"><h6>'+info.company+'</h6></div>'+
142
143 '<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
144 'Annual Billing: '+format_currency(info.billing_this_year, info.currency)+'</span></div>'+
145
146 '<div class="badge-link small" style="margin-bottom:10px">'+
147 '<span class="indicator '+color+'">Total Unpaid: '
148 +format_currency(info.total_unpaid, info.currency)+'</span></div>'+
149
150
151 '</div>').appendTo(frm.dashboard.stats_area_row);
152
153 if(info.loyalty_points){
154 $('<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
155 'Loyalty Points: '+info.loyalty_points+'</span></div>').appendTo(indicator);
156 }
157
158 return indicator;
159 },
160
Shreya Shah149f7ee2018-03-27 11:29:25 +0530161 get_party_name: function(party_type) {
162 var dict = {'Customer': 'customer_name', 'Supplier': 'supplier_name', 'Employee': 'employee_name',
163 'Member': 'member_name'};
164 return dict[party_type];
165 },
166
Rushabh Mehta49f97472018-08-30 18:50:48 +0530167 copy_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname) {
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +0530168 var d = locals[dt][dn];
169 if(d[fieldname]){
170 var cl = doc[table_fieldname] || [];
171 for(var i = 0; i < cl.length; i++) {
172 if(!cl[i][fieldname]) cl[i][fieldname] = d[fieldname];
173 }
174 }
175 refresh_field(table_fieldname);
Makarand Bauskar157c3342017-05-26 21:32:33 +0530176 },
177
178 get_terms: function(tc_name, doc, callback) {
179 if(tc_name) {
180 return frappe.call({
181 method: 'erpnext.setup.doctype.terms_and_conditions.terms_and_conditions.get_terms_and_conditions',
182 args: {
183 template_name: tc_name,
184 doc: doc
185 },
186 callback: function(r) {
187 callback(r)
188 }
189 });
190 }
tundebabzy1a947db2017-08-29 13:48:27 +0100191 },
192
rohitwaghchaure713cfc72018-09-11 17:40:37 +0530193 make_bank_account: function(doctype, docname) {
194 frappe.call({
195 method: "erpnext.accounts.doctype.bank_account.bank_account.make_bank_account",
196 args: {
197 doctype: doctype,
198 docname: docname
199 },
200 freeze: true,
201 callback: function(r) {
202 var doclist = frappe.model.sync(r.message);
203 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
204 }
205 })
206 },
207
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530208 add_dimensions: function(report_name, index) {
209 let filters = frappe.query_reports[report_name].filters;
210
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530211 frappe.call({
212 method: "erpnext.accounts.doctype.accounting_dimension.accounting_dimension.get_dimensions",
213 callback: function(r) {
214 let accounting_dimensions = r.message[0];
215 accounting_dimensions.forEach((dimension) => {
216 let found = filters.some(el => el.fieldname === dimension['fieldname']);
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530217
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530218 if (!found) {
Deepesh Garg23ab5c52020-12-31 11:29:06 +0530219 filters.splice(index, 0, {
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530220 "fieldname": dimension["fieldname"],
221 "label": __(dimension["label"]),
Rohit Waghchaure69be22b2022-05-14 17:19:34 +0530222 "fieldtype": "MultiSelectList",
223 get_data: function(txt) {
224 return frappe.db.get_link_options(dimension["document_type"], txt);
225 },
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530226 });
227 }
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530228 });
229 }
230 });
231 },
232
Rohit Waghchauredbec5cf2022-06-22 12:24:08 +0530233 add_inventory_dimensions: function(report_name, index) {
234 let filters = frappe.query_reports[report_name].filters;
235
236 frappe.call({
237 method: "erpnext.stock.doctype.inventory_dimension.inventory_dimension.get_inventory_dimensions",
238 callback: function(r) {
239 if (r.message && r.message.length) {
240 r.message.forEach((dimension) => {
Rohit Waghchaure0e388ba2023-02-20 12:20:03 +0530241 let existing_filter = filters.filter(el => el.fieldname === dimension['fieldname']);
Rohit Waghchauredbec5cf2022-06-22 12:24:08 +0530242
Rohit Waghchaure0e388ba2023-02-20 12:20:03 +0530243 if (!existing_filter.length) {
Rohit Waghchauredbec5cf2022-06-22 12:24:08 +0530244 filters.splice(index, 0, {
245 "fieldname": dimension["fieldname"],
Rohit Waghchaureef7def82022-09-07 14:40:18 +0530246 "label": __(dimension["doctype"]),
Rohit Waghchauredbec5cf2022-06-22 12:24:08 +0530247 "fieldtype": "MultiSelectList",
248 get_data: function(txt) {
249 return frappe.db.get_link_options(dimension["doctype"], txt);
250 },
251 });
Rohit Waghchaure0e388ba2023-02-20 12:20:03 +0530252 } else {
253 existing_filter[0]['fieldtype'] = "MultiSelectList";
254 existing_filter[0]['get_data'] = function(txt) {
255 return frappe.db.get_link_options(dimension["doctype"], txt);
256 }
Rohit Waghchauredbec5cf2022-06-22 12:24:08 +0530257 }
258 });
259 }
260 }
261 });
262 },
263
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530264 make_subscription: function(doctype, docname) {
265 frappe.call({
Rucha Mahabal65a627c2019-07-17 13:50:32 +0530266 method: "frappe.automation.doctype.auto_repeat.auto_repeat.make_auto_repeat",
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530267 args: {
268 doctype: doctype,
269 docname: docname
270 },
271 callback: function(r) {
272 var doclist = frappe.model.sync(r.message);
273 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
274 }
275 })
276 },
277
Saif90cf2dd2018-09-30 21:46:31 +0500278 make_pricing_rule: function(doctype, docname) {
279 frappe.call({
280 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.make_pricing_rule",
281 args: {
282 doctype: doctype,
283 docname: docname
284 },
285 callback: function(r) {
286 var doclist = frappe.model.sync(r.message);
287 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
288 }
289 })
290 },
291
tundebabzy1a947db2017-08-29 13:48:27 +0100292 /**
293 * Checks if the first row of a given child table is empty
294 * @param child_table - Child table Doctype
295 * @return {Boolean}
296 **/
297 first_row_is_empty: function(child_table){
298 if($.isArray(child_table) && child_table.length > 0) {
299 return !child_table[0].item_code;
300 }
301 return false;
302 },
303
304 /**
305 * Removes the first row of a child table if it is empty
306 * @param {_Frm} frm - The current form
307 * @param {String} child_table_name - The child table field name
308 * @return {Boolean}
309 **/
310 remove_empty_first_row: function(frm, child_table_name){
311 const rows = frm['doc'][child_table_name];
312 if (this.first_row_is_empty(rows)){
313 frm['doc'][child_table_name] = rows.splice(1);
314 }
315 return rows;
316 },
Zarrar6ffdf942018-06-14 12:24:16 +0530317 get_tree_options: function(option) {
318 // get valid options for tree based on user permission & locals dict
319 let unscrub_option = frappe.model.unscrub(option);
320 let user_permission = frappe.defaults.get_user_permissions();
deepeshgarg0070d64d622019-02-20 17:55:14 +0530321 let options;
322
Zarrar6ffdf942018-06-14 12:24:16 +0530323 if(user_permission && user_permission[unscrub_option]) {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530324 options = user_permission[unscrub_option].map(perm => perm.doc);
Zarrar6ffdf942018-06-14 12:24:16 +0530325 } else {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530326 options = $.map(locals[`:${unscrub_option}`], function(c) { return c.name; }).sort();
Zarrar6ffdf942018-06-14 12:24:16 +0530327 }
deepeshgarg0070d64d622019-02-20 17:55:14 +0530328
329 // filter unique values, as there may be multiple user permissions for any value
330 return options.filter((value, index, self) => self.indexOf(value) === index);
Zarrar6ffdf942018-06-14 12:24:16 +0530331 },
332 get_tree_default: function(option) {
333 // set default for a field based on user permission
334 let options = this.get_tree_options(option);
335 if(options.includes(frappe.defaults.get_default(option))) {
336 return frappe.defaults.get_default(option);
337 } else {
338 return options[0];
339 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400340 },
Andy Zhubffe9332021-04-12 22:49:26 +1200341 overrides_parent_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname, parent_fieldname) {
Marica24e45162021-04-14 18:53:15 +0530342 if (doc[parent_fieldname]) {
Andy Zhubffe9332021-04-12 22:49:26 +1200343 let cl = doc[table_fieldname] || [];
Marica24e45162021-04-14 18:53:15 +0530344 for (let i = 0; i < cl.length; i++) {
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400345 cl[i][fieldname] = doc[parent_fieldname];
346 }
Andy Zhubffe9332021-04-12 22:49:26 +1200347 frappe.refresh_field(table_fieldname);
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400348 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400349 },
Nabin Hait34c551d2019-07-03 10:34:31 +0530350 create_new_doc: function (doctype, update_fields) {
351 frappe.model.with_doctype(doctype, function() {
352 var new_doc = frappe.model.get_new_doc(doctype);
353 for (let [key, value] of Object.entries(update_fields)) {
354 new_doc[key] = value;
355 }
356 frappe.ui.form.make_quick_entry(doctype, null, null, new_doc);
357 });
Ankush Menat0b86b1b2022-12-08 16:40:13 +0530358 },
Nabin Hait34c551d2019-07-03 10:34:31 +0530359
Ankush Menat0b86b1b2022-12-08 16:40:13 +0530360 // check if payments app is installed on site, if not warn user.
361 check_payments_app: () => {
362 if (frappe.boot.versions && !frappe.boot.versions.payments) {
363 const marketplace_link = '<a href="https://frappecloud.com/marketplace/apps/payments">Marketplace</a>'
364 const github_link = '<a href="https://github.com/frappe/payments/">GitHub</a>'
365 const msg = __("payments app is not installed. Please install it from {0} or {1}", [marketplace_link, github_link])
366 frappe.msgprint(msg);
367 }
368
369 },
Rohit Waghchaure1c2fe082023-06-15 12:54:43 +0530370
371 pick_serial_and_batch_bundle(frm, cdt, cdn, type_of_transaction, warehouse_field) {
372 let item_row = frappe.get_doc(cdt, cdn);
373 item_row.type_of_transaction = type_of_transaction;
374
375 frappe.db.get_value("Item", item_row.item_code, ["has_batch_no", "has_serial_no"])
376 .then((r) => {
377 item_row.has_batch_no = r.message.has_batch_no;
378 item_row.has_serial_no = r.message.has_serial_no;
379
380 frappe.require("assets/erpnext/js/utils/serial_no_batch_selector.js", function() {
381 new erpnext.SerialBatchPackageSelector(frm, item_row, (r) => {
382 if (r) {
383 let update_values = {
384 "serial_and_batch_bundle": r.name,
385 "qty": Math.abs(r.total_qty)
386 }
387
388 if (!warehouse_field) {
389 warehouse_field = "warehouse";
390 }
391
392 if (r.warehouse) {
393 update_values[warehouse_field] = r.warehouse;
394 }
395
396 frappe.model.set_value(item_row.doctype, item_row.name, update_values);
397 }
398 });
399 });
400 });
Deepesh Garg62706072023-07-16 12:58:42 +0530401 },
402
Deepesh Garg23410612023-07-27 21:03:32 +0530403 get_fiscal_year: function(date, with_dates=false) {
Deepesh Garg3759a412023-07-19 13:17:12 +0530404 if(!date) {
405 date = frappe.datetime.get_today();
406 }
407
Deepesh Garg62706072023-07-16 12:58:42 +0530408 let fiscal_year = '';
409 frappe.call({
410 method: "erpnext.accounts.utils.get_fiscal_year",
411 args: {
412 date: date
413 },
414 async: false,
415 callback: function(r) {
416 if (r.message) {
Deepesh Garg4496a672023-07-24 11:58:16 +0530417 if (with_dates)
418 fiscal_year = r.message;
419 else
420 fiscal_year = r.message[0];
Deepesh Garg62706072023-07-16 12:58:42 +0530421 }
422 }
423 });
424 return fiscal_year;
Rohit Waghchaure1c2fe082023-06-15 12:54:43 +0530425 }
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530426});
427
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530428erpnext.utils.select_alternate_items = function(opts) {
429 const frm = opts.frm;
430 const warehouse_field = opts.warehouse_field || 'warehouse';
431 const item_field = opts.item_field || 'item_code';
432
433 this.data = [];
434 const dialog = new frappe.ui.Dialog({
435 title: __("Select Alternate Item"),
436 fields: [
437 {fieldtype:'Section Break', label: __('Items')},
438 {
439 fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true,
440 in_place_edit: true, data: this.data,
441 get_data: () => {
442 return this.data;
443 },
444 fields: [{
445 fieldtype:'Data',
446 fieldname:"docname",
447 hidden: 1
448 }, {
449 fieldtype:'Link',
450 fieldname:"item_code",
451 options: 'Item',
452 in_list_view: 1,
453 read_only: 1,
454 label: __('Item Code')
455 }, {
456 fieldtype:'Link',
457 fieldname:"alternate_item",
458 options: 'Item',
459 default: "",
460 in_list_view: 1,
461 label: __('Alternate Item'),
462 onchange: function() {
463 const item_code = this.get_value();
464 const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value();
465 if (item_code && warehouse) {
466 frappe.call({
467 method: "erpnext.stock.utils.get_latest_stock_qty",
468 args: {
469 item_code: item_code,
470 warehouse: warehouse
471 },
472 callback: (r) => {
473 this.grid_row.on_grid_fields_dict
474 .actual_qty.set_value(r.message || 0);
475 }
476 })
477 }
478 },
479 get_query: (e) => {
480 return {
481 query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items",
482 filters: {
483 item_code: e.item_code
484 }
485 };
486 }
487 }, {
488 fieldtype:'Link',
489 fieldname:"warehouse",
490 options: 'Warehouse',
491 default: "",
492 in_list_view: 1,
493 label: __('Warehouse'),
494 onchange: function() {
495 const warehouse = this.get_value();
496 const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value();
497 if (item_code && warehouse) {
498 frappe.call({
499 method: "erpnext.stock.utils.get_latest_stock_qty",
500 args: {
501 item_code: item_code,
502 warehouse: warehouse
503 },
504 callback: (r) => {
505 this.grid_row.on_grid_fields_dict
506 .actual_qty.set_value(r.message || 0);
507 }
508 })
509 }
510 },
511 }, {
512 fieldtype:'Float',
513 fieldname:"actual_qty",
514 default: 0,
515 read_only: 1,
516 in_list_view: 1,
517 label: __('Available Qty')
518 }]
519 },
520 ],
521 primary_action: function() {
522 const args = this.get_values()["alternative_items"];
523 const alternative_items = args.filter(d => {
524 if (d.alternate_item && d.item_code != d.alternate_item) {
525 return true;
526 }
527 });
528
529 alternative_items.forEach(d => {
530 let row = frappe.get_doc(opts.child_doctype, d.docname);
Doridel Cahanape42192b2018-05-16 13:28:39 +0800531 let qty = null;
532 if (row.doctype === 'Work Order Item') {
533 qty = row.required_qty;
534 } else {
535 qty = row.qty;
536 }
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530537 row[item_field] = d.alternate_item;
mergify[bot]c1a0f642021-12-17 19:06:19 +0530538 frappe.model.set_value(row.doctype, row.name, 'qty', qty);
539 frappe.model.set_value(row.doctype, row.name, opts.original_item_field, d.item_code);
540 frm.trigger(item_field, row.doctype, row.name);
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530541 });
542
543 refresh_field(opts.child_docname);
544 this.hide();
545 },
546 primary_action_label: __('Update')
547 });
548
549 frm.doc[opts.child_docname].forEach(d => {
550 if (!opts.condition || opts.condition(d)) {
551 dialog.fields_dict.alternative_items.df.data.push({
552 "docname": d.name,
553 "item_code": d[item_field],
554 "warehouse": d[warehouse_field],
555 "actual_qty": d.actual_qty
556 });
557 }
558 })
559
560 this.data = dialog.fields_dict.alternative_items.df.data;
561 dialog.fields_dict.alternative_items.grid.refresh();
562 dialog.show();
563}
564
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800565erpnext.utils.update_child_items = function(opts) {
566 const frm = opts.frm;
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100567 const cannot_add_row = (typeof opts.cannot_add_row === 'undefined') ? true : opts.cannot_add_row;
568 const child_docname = (typeof opts.cannot_add_row === 'undefined') ? "items" : opts.child_docname;
Saqib56fea7d2020-10-09 21:19:25 +0530569 const child_meta = frappe.get_meta(`${frm.doc.doctype} Item`);
570 const get_precision = (fieldname) => child_meta.fields.find(f => f.fieldname == fieldname).precision;
571
Devin Slauenwhitea835c1a2023-01-26 16:53:05 -0500572 this.data = frm.doc[opts.child_docname].map((d) => {
573 return {
574 "docname": d.name,
575 "name": d.name,
576 "item_code": d.item_code,
577 "delivery_date": d.delivery_date,
578 "schedule_date": d.schedule_date,
579 "conversion_factor": d.conversion_factor,
580 "qty": d.qty,
581 "rate": d.rate,
s-aga-r9588bb72023-08-21 22:23:57 +0530582 "uom": d.uom,
583 "fg_item": d.fg_item,
584 "fg_item_qty": d.fg_item_qty,
Devin Slauenwhitea835c1a2023-01-26 16:53:05 -0500585 }
586 });
587
Saqib Ansarif53299e2020-04-15 22:08:12 +0530588 const fields = [{
589 fieldtype:'Data',
590 fieldname:"docname",
591 read_only: 1,
592 hidden: 1,
593 }, {
594 fieldtype:'Link',
595 fieldname:"item_code",
596 options: 'Item',
597 in_list_view: 1,
598 read_only: 0,
599 disabled: 0,
Afshandc7280e2021-08-18 17:44:40 +0530600 label: __('Item Code'),
601 get_query: function() {
602 let filters;
603 if (frm.doc.doctype == 'Sales Order') {
604 filters = {"is_sales_item": 1};
605 } else if (frm.doc.doctype == 'Purchase Order') {
Sagar Sharmad074c932022-03-31 19:57:42 +0530606 if (frm.doc.is_subcontracted) {
s-aga-r6d89b2f2022-06-18 15:46:59 +0530607 if (frm.doc.is_old_subcontracting_flow) {
608 filters = {"is_sub_contracted_item": 1};
609 } else {
610 filters = {"is_stock_item": 0};
611 }
Afshandc7280e2021-08-18 17:44:40 +0530612 } else {
613 filters = {"is_purchase_item": 1};
614 }
615 }
616 return {
617 query: "erpnext.controllers.queries.item_query",
618 filters: filters
619 };
620 }
Saqib Ansarif53299e2020-04-15 22:08:12 +0530621 }, {
Saqib61314242020-09-15 11:14:31 +0530622 fieldtype:'Link',
623 fieldname:'uom',
624 options: 'UOM',
625 read_only: 0,
626 label: __('UOM'),
627 reqd: 1,
628 onchange: function () {
629 frappe.call({
630 method: "erpnext.stock.get_item_details.get_conversion_factor",
631 args: { item_code: this.doc.item_code, uom: this.value },
632 callback: r => {
633 if(!r.exc) {
634 if (this.doc.conversion_factor == r.message.conversion_factor) return;
marination4f395cc2020-09-24 12:43:41 +0530635
Saqib61314242020-09-15 11:14:31 +0530636 const docname = this.doc.docname;
637 dialog.fields_dict.trans_items.df.data.some(doc => {
638 if (doc.docname == docname) {
639 doc.conversion_factor = r.message.conversion_factor;
640 dialog.fields_dict.trans_items.grid.refresh();
641 return true;
642 }
643 })
644 }
645 }
646 });
647 }
648 }, {
Saqib Ansarif53299e2020-04-15 22:08:12 +0530649 fieldtype:'Float',
650 fieldname:"qty",
651 default: 0,
652 read_only: 0,
653 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530654 label: __('Qty'),
655 precision: get_precision("qty")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530656 }, {
657 fieldtype:'Currency',
658 fieldname:"rate",
Afshan84184552021-02-24 16:51:11 +0530659 options: "currency",
Saqib Ansarif53299e2020-04-15 22:08:12 +0530660 default: 0,
661 read_only: 0,
662 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530663 label: __('Rate'),
664 precision: get_precision("rate")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530665 }];
666
667 if (frm.doc.doctype == 'Sales Order' || frm.doc.doctype == 'Purchase Order' ) {
668 fields.splice(2, 0, {
669 fieldtype: 'Date',
670 fieldname: frm.doc.doctype == 'Sales Order' ? "delivery_date" : "schedule_date",
671 in_list_view: 1,
Saqib Ansaric579b082020-05-29 22:21:50 +0530672 label: frm.doc.doctype == 'Sales Order' ? __("Delivery Date") : __("Reqd by date"),
673 reqd: 1
Saqib Ansarif53299e2020-04-15 22:08:12 +0530674 })
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530675 fields.splice(3, 0, {
676 fieldtype: 'Float',
677 fieldname: "conversion_factor",
Saqib56fea7d2020-10-09 21:19:25 +0530678 label: __("Conversion Factor"),
679 precision: get_precision('conversion_factor')
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530680 })
Saqib Ansarif53299e2020-04-15 22:08:12 +0530681 }
682
s-aga-rfaf9f132023-08-21 15:51:57 +0530683 if (frm.doc.doctype == 'Purchase Order' && frm.doc.is_subcontracted && !frm.doc.is_old_subcontracting_flow) {
684 fields.push({
685 fieldtype:'Link',
686 fieldname:'fg_item',
687 options: 'Item',
688 reqd: 1,
689 in_list_view: 0,
690 read_only: 0,
691 disabled: 0,
692 label: __('Finished Good Item'),
693 get_query: () => {
694 return {
695 filters: {
696 'is_stock_item': 1,
697 'is_sub_contracted_item': 1,
698 'default_bom': ['!=', '']
699 }
700 }
701 },
702 }, {
703 fieldtype:'Float',
704 fieldname:'fg_item_qty',
705 reqd: 1,
706 default: 0,
707 read_only: 0,
708 in_list_view: 0,
709 label: __('Finished Good Item Qty'),
710 precision: get_precision('fg_item_qty')
711 })
712 }
713
Deepesh Garg40772542023-07-22 23:07:18 +0530714 let dialog = new frappe.ui.Dialog({
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800715 title: __("Update Items"),
Raffael Meyerd5fe1432023-07-14 08:57:35 +0200716 size: "extra-large",
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800717 fields: [
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800718 {
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100719 fieldname: "trans_items",
720 fieldtype: "Table",
Maricafc96c1a2020-03-06 10:57:59 +0530721 label: "Items",
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100722 cannot_add_rows: cannot_add_row,
Saqibbc919e22020-11-05 17:38:35 +0530723 in_place_edit: false,
Maricafc96c1a2020-03-06 10:57:59 +0530724 reqd: 1,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100725 data: this.data,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800726 get_data: () => {
727 return this.data;
728 },
Saqib Ansarif53299e2020-04-15 22:08:12 +0530729 fields: fields
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800730 },
731 ],
732 primary_action: function() {
Ankush Menat6de7b8e2021-08-24 12:18:40 +0530733 const trans_items = this.get_values()["trans_items"].filter((item) => !!item.item_code);
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800734 frappe.call({
735 method: 'erpnext.controllers.accounts_controller.update_child_qty_rate',
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100736 freeze: true,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800737 args: {
738 'parent_doctype': frm.doc.doctype,
739 'trans_items': trans_items,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100740 'parent_doctype_name': frm.doc.name,
741 'child_docname': child_docname
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800742 },
743 callback: function() {
744 frm.reload_doc();
745 }
746 });
747 this.hide();
748 refresh_field("items");
749 },
750 primary_action_label: __('Update')
Deepesh Garg40772542023-07-22 23:07:18 +0530751 })
752
753 dialog.show();
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800754}
755
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530756erpnext.utils.map_current_doc = function(opts) {
Sagar Vora78777d62021-03-09 20:35:08 +0530757 function _map() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530758 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530759 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530760 if(!cur_frm.doc.items[0].item_code) {
761 cur_frm.doc.items = cur_frm.doc.items.splice(1);
762 }
bhupen3c7e70f2016-11-30 14:32:11 +0530763
764 // find the doctype of the items table
765 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530766
bhupen3c7e70f2016-11-30 14:32:11 +0530767 // find the link fieldname from items table for the given
768 // source_doctype
769 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530770 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530771 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
772
Nabin Hait802b4352017-01-09 15:32:20 +0530773 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530774 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530775 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530776
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530777 $.each(cur_frm.doc.items, function(i, d) {
778 opts.source_name.forEach(function(src) {
779 if(d[link_fieldname]==src) {
780 already_set = true;
781 if (item_qty_map[d.item_code])
782 item_qty_map[d.item_code] += flt(d.qty);
783 else
784 item_qty_map[d.item_code] = flt(d.qty);
785 }
786 });
787 });
788
789 if(already_set) {
790 opts.source_name.forEach(function(src) {
791 frappe.model.with_doc(opts.source_doctype, src, function(r) {
792 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
793 $.each(source_doc.items || [], function(i, row) {
794 if(row.qty > flt(item_qty_map[row.item_code])) {
795 already_set = false;
796 return false;
797 }
798 })
799 })
800
801 if(already_set) {
802 frappe.msgprint(__("You have already selected items from {0} {1}",
803 [opts.source_doctype, src]));
804 return;
805 }
806
807 })
Nabin Hait802b4352017-01-09 15:32:20 +0530808 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530809 }
810
811 return frappe.call({
812 // Sometimes we hit the limit for URL length of a GET request
813 // as we send the full target_doc. Hence this is a POST request.
814 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530815 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530816 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530817 "method": opts.method,
818 "source_names": opts.source_name,
819 "target_doc": cur_frm.doc,
Maricadb002702020-02-17 15:58:08 +0530820 "args": opts.args
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530821 },
822 callback: function(r) {
823 if(!r.exc) {
824 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530825 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530826 cur_frm.refresh();
827 }
828 }
829 });
830 }
Sagar Vora78777d62021-03-09 20:35:08 +0530831
832 let query_args = {};
833 if (opts.get_query_filters) {
834 query_args.filters = opts.get_query_filters;
835 }
836
837 if (opts.get_query_method) {
838 query_args.query = opts.get_query_method;
839 }
840
841 if (query_args.filters || query_args.query) {
842 opts.get_query = () => query_args;
843 }
844
845 if (opts.source_doctype) {
846 const d = new frappe.ui.form.MultiSelectDialog({
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530847 doctype: opts.source_doctype,
848 target: opts.target,
849 date_field: opts.date_field || undefined,
850 setters: opts.setters,
851 get_query: opts.get_query,
marination4f395cc2020-09-24 12:43:41 +0530852 add_filters_group: 1,
Saqib7292f542021-09-13 12:13:43 +0530853 allow_child_item_selection: opts.allow_child_item_selection,
gn3060292a619fd2022-06-29 17:15:21 +0800854 child_fieldname: opts.child_fieldname,
Saqib7292f542021-09-13 12:13:43 +0530855 child_columns: opts.child_columns,
Rohit Waghchaure69ffddf2021-10-07 17:33:58 +0530856 size: opts.size,
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530857 action: function(selections, args) {
858 let values = selections;
Saqibe03d9aa2021-09-17 13:03:27 +0530859 if (values.length === 0) {
Nabin Haita11dcb62017-12-04 13:36:50 +0530860 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530861 return;
862 }
863 opts.source_name = values;
Saqibe03d9aa2021-09-17 13:03:27 +0530864 if (opts.allow_child_item_selection) {
865 // args contains filtered child docnames
866 opts.args = args;
867 }
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530868 d.dialog.hide();
869 _map();
870 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530871 });
Sagar Vora78777d62021-03-09 20:35:08 +0530872
873 return d;
874 }
875
876 if (opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530877 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530878 _map();
879 }
880}
881
Rushabh Mehta180e4352016-07-26 17:57:42 +0530882frappe.form.link_formatters['Item'] = function(value, doc) {
Alan157b3882021-04-21 21:00:22 +0530883 if (doc && value && doc.item_name && doc.item_name !== value && doc.item_code === value) {
marination824f48f2020-10-12 20:08:03 +0530884 return value + ': ' + doc.item_name;
885 } else if (!value && doc.doctype && doc.item_name) {
886 // format blank value in child table
887 return doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530888 } else {
marination824f48f2020-10-12 20:08:03 +0530889 // 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 +0530890 return value;
891 }
892}
893
894frappe.form.link_formatters['Employee'] = function(value, doc) {
Rucha Mahabal5ba1bc12021-11-23 09:30:30 +0530895 if (doc && value && doc.employee_name && doc.employee_name !== value && doc.employee === value) {
896 return value + ': ' + doc.employee_name;
897 } else if (!value && doc.doctype && doc.employee_name) {
898 // format blank value in child table
899 return doc.employee;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530900 } else {
Rucha Mahabal5ba1bc12021-11-23 09:30:30 +0530901 // 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 +0530902 return value;
903 }
904}
905
Rucha Mahabal59961f72021-05-20 23:43:19 +0530906frappe.form.link_formatters['Project'] = function(value, doc) {
907 if (doc && value && doc.project_name && doc.project_name !== value && doc.project === value) {
908 return value + ': ' + doc.project_name;
909 } else if (!value && doc.doctype && doc.project_name) {
910 // format blank value in child table
911 return doc.project;
912 } else {
913 // if value is blank in report view or project name and name are the same, return as is
914 return value;
915 }
916};
917
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530918// add description on posting time
919$(document).on('app_ready', function() {
920 if(!frappe.datetime.is_timezone_same()) {
921 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
922 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
923 frappe.ui.form.on(d, "onload", function(frm) {
924 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530925 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530926 });
927 });
928 }
akshay14384c22016-07-28 13:53:17 +0530929});
Alanc6dc9ea2021-05-07 20:30:04 +0530930
Himanshuec25d592021-06-14 19:05:52 +0530931// Show SLA dashboard
932$(document).on('app_ready', function() {
Ankush Menatbd9ef742023-07-16 11:34:42 +0530933 $.each(frappe.boot.service_level_agreement_doctypes, function(_i, d) {
934 frappe.ui.form.on(d, {
935 onload: function(frm) {
936 if (!frm.doc.service_level_agreement)
937 return;
Himanshuec25d592021-06-14 19:05:52 +0530938
Ankush Menatbd9ef742023-07-16 11:34:42 +0530939 frappe.call({
940 method: 'erpnext.support.doctype.service_level_agreement.service_level_agreement.get_service_level_agreement_filters',
941 args: {
942 doctype: frm.doc.doctype,
943 name: frm.doc.service_level_agreement,
944 customer: frm.doc.customer
Himanshuec25d592021-06-14 19:05:52 +0530945 },
Ankush Menatbd9ef742023-07-16 11:34:42 +0530946 callback: function (r) {
947 if (r && r.message) {
948 frm.set_query('priority', function() {
949 return {
950 filters: {
951 'name': ['in', r.message.priority],
Himanshuec25d592021-06-14 19:05:52 +0530952 }
Ankush Menatbd9ef742023-07-16 11:34:42 +0530953 };
Himanshuec25d592021-06-14 19:05:52 +0530954 });
Ankush Menatbd9ef742023-07-16 11:34:42 +0530955 frm.set_query('service_level_agreement', function() {
956 return {
957 filters: {
958 'name': ['in', r.message.service_level_agreements],
959 }
960 };
961 });
Himanshuec25d592021-06-14 19:05:52 +0530962 }
Ankush Menatbd9ef742023-07-16 11:34:42 +0530963 }
Himanshuec25d592021-06-14 19:05:52 +0530964 });
Ankush Menatbd9ef742023-07-16 11:34:42 +0530965 },
966
967 refresh: function(frm) {
968 if (frm.doc.status !== 'Closed' && frm.doc.service_level_agreement
969 && ['First Response Due', 'Resolution Due'].includes(frm.doc.agreement_status)) {
970 frappe.call({
971 'method': 'frappe.client.get',
972 args: {
973 doctype: 'Service Level Agreement',
974 name: frm.doc.service_level_agreement
975 },
976 callback: function(data) {
977 let statuses = data.message.pause_sla_on;
978 const hold_statuses = [];
979 $.each(statuses, (_i, entry) => {
980 hold_statuses.push(entry.status);
981 });
982 if (hold_statuses.includes(frm.doc.status)) {
983 frm.dashboard.clear_headline();
984 let message = {'indicator': 'orange', 'msg': __('SLA is on hold since {0}', [moment(frm.doc.on_hold_since).fromNow(true)])};
985 frm.dashboard.set_headline_alert(
986 '<div class="row">' +
987 '<div class="col-xs-12">' +
988 '<span class="indicator whitespace-nowrap '+ message.indicator +'"><span>'+ message.msg +'</span></span> ' +
989 '</div>' +
990 '</div>'
991 );
992 } else {
993 set_time_to_resolve_and_response(frm, data.message.apply_sla_for_resolution);
994 }
995 }
996 });
997 } else if (frm.doc.service_level_agreement) {
998 frm.dashboard.clear_headline();
999
1000 let agreement_status = (frm.doc.agreement_status == 'Fulfilled') ?
1001 {'indicator': 'green', 'msg': 'Service Level Agreement has been fulfilled'} :
1002 {'indicator': 'red', 'msg': 'Service Level Agreement Failed'};
1003
1004 frm.dashboard.set_headline_alert(
1005 '<div class="row">' +
1006 '<div class="col-xs-12">' +
1007 '<span class="indicator whitespace-nowrap '+ agreement_status.indicator +'"><span class="hidden-xs">'+ agreement_status.msg +'</span></span> ' +
1008 '</div>' +
1009 '</div>'
1010 );
1011 }
1012 },
1013 });
Himanshuec25d592021-06-14 19:05:52 +05301014 });
1015});
1016
1017function set_time_to_resolve_and_response(frm, apply_sla_for_resolution) {
1018 frm.dashboard.clear_headline();
1019
Saqib24114772021-12-15 11:45:34 +05301020 let time_to_respond;
Saqib Ansari476e81a2021-12-06 18:55:30 +05301021 if (!frm.doc.first_responded_on) {
Himanshuec25d592021-06-14 19:05:52 +05301022 time_to_respond = get_time_left(frm.doc.response_by, frm.doc.agreement_status);
Saqib24114772021-12-15 11:45:34 +05301023 } else {
1024 time_to_respond = get_status(frm.doc.response_by, frm.doc.first_responded_on);
Himanshuec25d592021-06-14 19:05:52 +05301025 }
1026
1027 let alert = `
1028 <div class="row">
1029 <div class="col-xs-12 col-sm-6">
1030 <span class="indicator whitespace-nowrap ${time_to_respond.indicator}">
1031 <span>Time to Respond: ${time_to_respond.diff_display}</span>
1032 </span>
1033 </div>`;
1034
1035
1036 if (apply_sla_for_resolution) {
Saqib24114772021-12-15 11:45:34 +05301037 let time_to_resolve;
Saqib Ansari476e81a2021-12-06 18:55:30 +05301038 if (!frm.doc.resolution_date) {
Himanshuec25d592021-06-14 19:05:52 +05301039 time_to_resolve = get_time_left(frm.doc.resolution_by, frm.doc.agreement_status);
Saqib24114772021-12-15 11:45:34 +05301040 } else {
1041 time_to_resolve = get_status(frm.doc.resolution_by, frm.doc.resolution_date);
Himanshuec25d592021-06-14 19:05:52 +05301042 }
1043
1044 alert += `
1045 <div class="col-xs-12 col-sm-6">
1046 <span class="indicator whitespace-nowrap ${time_to_resolve.indicator}">
1047 <span>Time to Resolve: ${time_to_resolve.diff_display}</span>
1048 </span>
1049 </div>`;
1050 }
1051
1052 alert += '</div>';
1053
1054 frm.dashboard.set_headline_alert(alert);
1055}
1056
1057function get_time_left(timestamp, agreement_status) {
1058 const diff = moment(timestamp).diff(moment());
1059 const diff_display = diff >= 44500 ? moment.duration(diff).humanize() : 'Failed';
1060 let indicator = (diff_display == 'Failed' && agreement_status != 'Fulfilled') ? 'red' : 'green';
1061 return {'diff_display': diff_display, 'indicator': indicator};
1062}
1063
Saqib24114772021-12-15 11:45:34 +05301064function get_status(expected, actual) {
1065 const time_left = moment(expected).diff(moment(actual));
Saqib Ansari476e81a2021-12-06 18:55:30 +05301066 if (time_left >= 0) {
Himanshuec25d592021-06-14 19:05:52 +05301067 return {'diff_display': 'Fulfilled', 'indicator': 'green'};
1068 } else {
1069 return {'diff_display': 'Failed', 'indicator': 'red'};
1070 }
1071}
1072
Alanc6dc9ea2021-05-07 20:30:04 +05301073function attach_selector_button(inner_text, append_loction, context, grid_row) {
1074 let $btn_div = $("<div>").css({"margin-bottom": "10px", "margin-top": "10px"})
1075 .appendTo(append_loction);
1076 let $btn = $(`<button class="btn btn-sm btn-default">${inner_text}</button>`)
1077 .appendTo($btn_div);
1078
1079 $btn.on("click", function() {
1080 context.show_serial_batch_selector(grid_row.frm, grid_row.doc, "", "", true);
1081 });
Rohit Waghchaure708eefb2023-07-10 16:54:55 +05301082}