blob: 06e39aacc7c8899b7cfc59136548cdb62ac27e73 [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
403 get_fiscal_year: function(date) {
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) {
417 fiscal_year = r.message[0];
418 }
419 }
420 });
421 return fiscal_year;
Rohit Waghchaure1c2fe082023-06-15 12:54:43 +0530422 }
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530423});
424
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530425erpnext.utils.select_alternate_items = function(opts) {
426 const frm = opts.frm;
427 const warehouse_field = opts.warehouse_field || 'warehouse';
428 const item_field = opts.item_field || 'item_code';
429
430 this.data = [];
431 const dialog = new frappe.ui.Dialog({
432 title: __("Select Alternate Item"),
433 fields: [
434 {fieldtype:'Section Break', label: __('Items')},
435 {
436 fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true,
437 in_place_edit: true, data: this.data,
438 get_data: () => {
439 return this.data;
440 },
441 fields: [{
442 fieldtype:'Data',
443 fieldname:"docname",
444 hidden: 1
445 }, {
446 fieldtype:'Link',
447 fieldname:"item_code",
448 options: 'Item',
449 in_list_view: 1,
450 read_only: 1,
451 label: __('Item Code')
452 }, {
453 fieldtype:'Link',
454 fieldname:"alternate_item",
455 options: 'Item',
456 default: "",
457 in_list_view: 1,
458 label: __('Alternate Item'),
459 onchange: function() {
460 const item_code = this.get_value();
461 const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value();
462 if (item_code && warehouse) {
463 frappe.call({
464 method: "erpnext.stock.utils.get_latest_stock_qty",
465 args: {
466 item_code: item_code,
467 warehouse: warehouse
468 },
469 callback: (r) => {
470 this.grid_row.on_grid_fields_dict
471 .actual_qty.set_value(r.message || 0);
472 }
473 })
474 }
475 },
476 get_query: (e) => {
477 return {
478 query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items",
479 filters: {
480 item_code: e.item_code
481 }
482 };
483 }
484 }, {
485 fieldtype:'Link',
486 fieldname:"warehouse",
487 options: 'Warehouse',
488 default: "",
489 in_list_view: 1,
490 label: __('Warehouse'),
491 onchange: function() {
492 const warehouse = this.get_value();
493 const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value();
494 if (item_code && warehouse) {
495 frappe.call({
496 method: "erpnext.stock.utils.get_latest_stock_qty",
497 args: {
498 item_code: item_code,
499 warehouse: warehouse
500 },
501 callback: (r) => {
502 this.grid_row.on_grid_fields_dict
503 .actual_qty.set_value(r.message || 0);
504 }
505 })
506 }
507 },
508 }, {
509 fieldtype:'Float',
510 fieldname:"actual_qty",
511 default: 0,
512 read_only: 1,
513 in_list_view: 1,
514 label: __('Available Qty')
515 }]
516 },
517 ],
518 primary_action: function() {
519 const args = this.get_values()["alternative_items"];
520 const alternative_items = args.filter(d => {
521 if (d.alternate_item && d.item_code != d.alternate_item) {
522 return true;
523 }
524 });
525
526 alternative_items.forEach(d => {
527 let row = frappe.get_doc(opts.child_doctype, d.docname);
Doridel Cahanape42192b2018-05-16 13:28:39 +0800528 let qty = null;
529 if (row.doctype === 'Work Order Item') {
530 qty = row.required_qty;
531 } else {
532 qty = row.qty;
533 }
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530534 row[item_field] = d.alternate_item;
mergify[bot]c1a0f642021-12-17 19:06:19 +0530535 frappe.model.set_value(row.doctype, row.name, 'qty', qty);
536 frappe.model.set_value(row.doctype, row.name, opts.original_item_field, d.item_code);
537 frm.trigger(item_field, row.doctype, row.name);
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530538 });
539
540 refresh_field(opts.child_docname);
541 this.hide();
542 },
543 primary_action_label: __('Update')
544 });
545
546 frm.doc[opts.child_docname].forEach(d => {
547 if (!opts.condition || opts.condition(d)) {
548 dialog.fields_dict.alternative_items.df.data.push({
549 "docname": d.name,
550 "item_code": d[item_field],
551 "warehouse": d[warehouse_field],
552 "actual_qty": d.actual_qty
553 });
554 }
555 })
556
557 this.data = dialog.fields_dict.alternative_items.df.data;
558 dialog.fields_dict.alternative_items.grid.refresh();
559 dialog.show();
560}
561
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800562erpnext.utils.update_child_items = function(opts) {
563 const frm = opts.frm;
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100564 const cannot_add_row = (typeof opts.cannot_add_row === 'undefined') ? true : opts.cannot_add_row;
565 const child_docname = (typeof opts.cannot_add_row === 'undefined') ? "items" : opts.child_docname;
Saqib56fea7d2020-10-09 21:19:25 +0530566 const child_meta = frappe.get_meta(`${frm.doc.doctype} Item`);
567 const get_precision = (fieldname) => child_meta.fields.find(f => f.fieldname == fieldname).precision;
568
Devin Slauenwhitea835c1a2023-01-26 16:53:05 -0500569 this.data = frm.doc[opts.child_docname].map((d) => {
570 return {
571 "docname": d.name,
572 "name": d.name,
573 "item_code": d.item_code,
574 "delivery_date": d.delivery_date,
575 "schedule_date": d.schedule_date,
576 "conversion_factor": d.conversion_factor,
577 "qty": d.qty,
578 "rate": d.rate,
579 "uom": d.uom
580 }
581 });
582
Saqib Ansarif53299e2020-04-15 22:08:12 +0530583 const fields = [{
584 fieldtype:'Data',
585 fieldname:"docname",
586 read_only: 1,
587 hidden: 1,
588 }, {
589 fieldtype:'Link',
590 fieldname:"item_code",
591 options: 'Item',
592 in_list_view: 1,
593 read_only: 0,
594 disabled: 0,
Afshandc7280e2021-08-18 17:44:40 +0530595 label: __('Item Code'),
596 get_query: function() {
597 let filters;
598 if (frm.doc.doctype == 'Sales Order') {
599 filters = {"is_sales_item": 1};
600 } else if (frm.doc.doctype == 'Purchase Order') {
Sagar Sharmad074c932022-03-31 19:57:42 +0530601 if (frm.doc.is_subcontracted) {
s-aga-r6d89b2f2022-06-18 15:46:59 +0530602 if (frm.doc.is_old_subcontracting_flow) {
603 filters = {"is_sub_contracted_item": 1};
604 } else {
605 filters = {"is_stock_item": 0};
606 }
Afshandc7280e2021-08-18 17:44:40 +0530607 } else {
608 filters = {"is_purchase_item": 1};
609 }
610 }
611 return {
612 query: "erpnext.controllers.queries.item_query",
613 filters: filters
614 };
615 }
Saqib Ansarif53299e2020-04-15 22:08:12 +0530616 }, {
Saqib61314242020-09-15 11:14:31 +0530617 fieldtype:'Link',
618 fieldname:'uom',
619 options: 'UOM',
620 read_only: 0,
621 label: __('UOM'),
622 reqd: 1,
623 onchange: function () {
624 frappe.call({
625 method: "erpnext.stock.get_item_details.get_conversion_factor",
626 args: { item_code: this.doc.item_code, uom: this.value },
627 callback: r => {
628 if(!r.exc) {
629 if (this.doc.conversion_factor == r.message.conversion_factor) return;
marination4f395cc2020-09-24 12:43:41 +0530630
Saqib61314242020-09-15 11:14:31 +0530631 const docname = this.doc.docname;
632 dialog.fields_dict.trans_items.df.data.some(doc => {
633 if (doc.docname == docname) {
634 doc.conversion_factor = r.message.conversion_factor;
635 dialog.fields_dict.trans_items.grid.refresh();
636 return true;
637 }
638 })
639 }
640 }
641 });
642 }
643 }, {
Saqib Ansarif53299e2020-04-15 22:08:12 +0530644 fieldtype:'Float',
645 fieldname:"qty",
646 default: 0,
647 read_only: 0,
648 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530649 label: __('Qty'),
650 precision: get_precision("qty")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530651 }, {
652 fieldtype:'Currency',
653 fieldname:"rate",
Afshan84184552021-02-24 16:51:11 +0530654 options: "currency",
Saqib Ansarif53299e2020-04-15 22:08:12 +0530655 default: 0,
656 read_only: 0,
657 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530658 label: __('Rate'),
659 precision: get_precision("rate")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530660 }];
661
662 if (frm.doc.doctype == 'Sales Order' || frm.doc.doctype == 'Purchase Order' ) {
663 fields.splice(2, 0, {
664 fieldtype: 'Date',
665 fieldname: frm.doc.doctype == 'Sales Order' ? "delivery_date" : "schedule_date",
666 in_list_view: 1,
Saqib Ansaric579b082020-05-29 22:21:50 +0530667 label: frm.doc.doctype == 'Sales Order' ? __("Delivery Date") : __("Reqd by date"),
668 reqd: 1
Saqib Ansarif53299e2020-04-15 22:08:12 +0530669 })
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530670 fields.splice(3, 0, {
671 fieldtype: 'Float',
672 fieldname: "conversion_factor",
Saqib56fea7d2020-10-09 21:19:25 +0530673 label: __("Conversion Factor"),
674 precision: get_precision('conversion_factor')
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530675 })
Saqib Ansarif53299e2020-04-15 22:08:12 +0530676 }
677
Deepesh Garg40772542023-07-22 23:07:18 +0530678 let dialog = new frappe.ui.Dialog({
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800679 title: __("Update Items"),
Raffael Meyerd5fe1432023-07-14 08:57:35 +0200680 size: "extra-large",
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800681 fields: [
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800682 {
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100683 fieldname: "trans_items",
684 fieldtype: "Table",
Maricafc96c1a2020-03-06 10:57:59 +0530685 label: "Items",
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100686 cannot_add_rows: cannot_add_row,
Saqibbc919e22020-11-05 17:38:35 +0530687 in_place_edit: false,
Maricafc96c1a2020-03-06 10:57:59 +0530688 reqd: 1,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100689 data: this.data,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800690 get_data: () => {
691 return this.data;
692 },
Saqib Ansarif53299e2020-04-15 22:08:12 +0530693 fields: fields
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800694 },
695 ],
696 primary_action: function() {
Ankush Menat6de7b8e2021-08-24 12:18:40 +0530697 const trans_items = this.get_values()["trans_items"].filter((item) => !!item.item_code);
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800698 frappe.call({
699 method: 'erpnext.controllers.accounts_controller.update_child_qty_rate',
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100700 freeze: true,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800701 args: {
702 'parent_doctype': frm.doc.doctype,
703 'trans_items': trans_items,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100704 'parent_doctype_name': frm.doc.name,
705 'child_docname': child_docname
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800706 },
707 callback: function() {
708 frm.reload_doc();
709 }
710 });
711 this.hide();
712 refresh_field("items");
713 },
714 primary_action_label: __('Update')
Deepesh Garg40772542023-07-22 23:07:18 +0530715 })
716
717 dialog.show();
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800718}
719
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530720erpnext.utils.map_current_doc = function(opts) {
Sagar Vora78777d62021-03-09 20:35:08 +0530721 function _map() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530722 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530723 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530724 if(!cur_frm.doc.items[0].item_code) {
725 cur_frm.doc.items = cur_frm.doc.items.splice(1);
726 }
bhupen3c7e70f2016-11-30 14:32:11 +0530727
728 // find the doctype of the items table
729 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530730
bhupen3c7e70f2016-11-30 14:32:11 +0530731 // find the link fieldname from items table for the given
732 // source_doctype
733 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530734 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530735 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
736
Nabin Hait802b4352017-01-09 15:32:20 +0530737 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530738 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530739 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530740
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530741 $.each(cur_frm.doc.items, function(i, d) {
742 opts.source_name.forEach(function(src) {
743 if(d[link_fieldname]==src) {
744 already_set = true;
745 if (item_qty_map[d.item_code])
746 item_qty_map[d.item_code] += flt(d.qty);
747 else
748 item_qty_map[d.item_code] = flt(d.qty);
749 }
750 });
751 });
752
753 if(already_set) {
754 opts.source_name.forEach(function(src) {
755 frappe.model.with_doc(opts.source_doctype, src, function(r) {
756 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
757 $.each(source_doc.items || [], function(i, row) {
758 if(row.qty > flt(item_qty_map[row.item_code])) {
759 already_set = false;
760 return false;
761 }
762 })
763 })
764
765 if(already_set) {
766 frappe.msgprint(__("You have already selected items from {0} {1}",
767 [opts.source_doctype, src]));
768 return;
769 }
770
771 })
Nabin Hait802b4352017-01-09 15:32:20 +0530772 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530773 }
774
775 return frappe.call({
776 // Sometimes we hit the limit for URL length of a GET request
777 // as we send the full target_doc. Hence this is a POST request.
778 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530779 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530780 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530781 "method": opts.method,
782 "source_names": opts.source_name,
783 "target_doc": cur_frm.doc,
Maricadb002702020-02-17 15:58:08 +0530784 "args": opts.args
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530785 },
786 callback: function(r) {
787 if(!r.exc) {
788 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530789 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530790 cur_frm.refresh();
791 }
792 }
793 });
794 }
Sagar Vora78777d62021-03-09 20:35:08 +0530795
796 let query_args = {};
797 if (opts.get_query_filters) {
798 query_args.filters = opts.get_query_filters;
799 }
800
801 if (opts.get_query_method) {
802 query_args.query = opts.get_query_method;
803 }
804
805 if (query_args.filters || query_args.query) {
806 opts.get_query = () => query_args;
807 }
808
809 if (opts.source_doctype) {
810 const d = new frappe.ui.form.MultiSelectDialog({
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530811 doctype: opts.source_doctype,
812 target: opts.target,
813 date_field: opts.date_field || undefined,
814 setters: opts.setters,
815 get_query: opts.get_query,
marination4f395cc2020-09-24 12:43:41 +0530816 add_filters_group: 1,
Saqib7292f542021-09-13 12:13:43 +0530817 allow_child_item_selection: opts.allow_child_item_selection,
gn3060292a619fd2022-06-29 17:15:21 +0800818 child_fieldname: opts.child_fieldname,
Saqib7292f542021-09-13 12:13:43 +0530819 child_columns: opts.child_columns,
Rohit Waghchaure69ffddf2021-10-07 17:33:58 +0530820 size: opts.size,
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530821 action: function(selections, args) {
822 let values = selections;
Saqibe03d9aa2021-09-17 13:03:27 +0530823 if (values.length === 0) {
Nabin Haita11dcb62017-12-04 13:36:50 +0530824 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530825 return;
826 }
827 opts.source_name = values;
Saqibe03d9aa2021-09-17 13:03:27 +0530828 if (opts.allow_child_item_selection) {
829 // args contains filtered child docnames
830 opts.args = args;
831 }
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530832 d.dialog.hide();
833 _map();
834 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530835 });
Sagar Vora78777d62021-03-09 20:35:08 +0530836
837 return d;
838 }
839
840 if (opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530841 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530842 _map();
843 }
844}
845
Rushabh Mehta180e4352016-07-26 17:57:42 +0530846frappe.form.link_formatters['Item'] = function(value, doc) {
Alan157b3882021-04-21 21:00:22 +0530847 if (doc && value && doc.item_name && doc.item_name !== value && doc.item_code === value) {
marination824f48f2020-10-12 20:08:03 +0530848 return value + ': ' + doc.item_name;
849 } else if (!value && doc.doctype && doc.item_name) {
850 // format blank value in child table
851 return doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530852 } else {
marination824f48f2020-10-12 20:08:03 +0530853 // 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 +0530854 return value;
855 }
856}
857
858frappe.form.link_formatters['Employee'] = function(value, doc) {
Rucha Mahabal5ba1bc12021-11-23 09:30:30 +0530859 if (doc && value && doc.employee_name && doc.employee_name !== value && doc.employee === value) {
860 return value + ': ' + doc.employee_name;
861 } else if (!value && doc.doctype && doc.employee_name) {
862 // format blank value in child table
863 return doc.employee;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530864 } else {
Rucha Mahabal5ba1bc12021-11-23 09:30:30 +0530865 // 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 +0530866 return value;
867 }
868}
869
Rucha Mahabal59961f72021-05-20 23:43:19 +0530870frappe.form.link_formatters['Project'] = function(value, doc) {
871 if (doc && value && doc.project_name && doc.project_name !== value && doc.project === value) {
872 return value + ': ' + doc.project_name;
873 } else if (!value && doc.doctype && doc.project_name) {
874 // format blank value in child table
875 return doc.project;
876 } else {
877 // if value is blank in report view or project name and name are the same, return as is
878 return value;
879 }
880};
881
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530882// add description on posting time
883$(document).on('app_ready', function() {
884 if(!frappe.datetime.is_timezone_same()) {
885 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
886 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
887 frappe.ui.form.on(d, "onload", function(frm) {
888 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530889 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530890 });
891 });
892 }
akshay14384c22016-07-28 13:53:17 +0530893});
Alanc6dc9ea2021-05-07 20:30:04 +0530894
Himanshuec25d592021-06-14 19:05:52 +0530895// Show SLA dashboard
896$(document).on('app_ready', function() {
Ankush Menatbd9ef742023-07-16 11:34:42 +0530897 $.each(frappe.boot.service_level_agreement_doctypes, function(_i, d) {
898 frappe.ui.form.on(d, {
899 onload: function(frm) {
900 if (!frm.doc.service_level_agreement)
901 return;
Himanshuec25d592021-06-14 19:05:52 +0530902
Ankush Menatbd9ef742023-07-16 11:34:42 +0530903 frappe.call({
904 method: 'erpnext.support.doctype.service_level_agreement.service_level_agreement.get_service_level_agreement_filters',
905 args: {
906 doctype: frm.doc.doctype,
907 name: frm.doc.service_level_agreement,
908 customer: frm.doc.customer
Himanshuec25d592021-06-14 19:05:52 +0530909 },
Ankush Menatbd9ef742023-07-16 11:34:42 +0530910 callback: function (r) {
911 if (r && r.message) {
912 frm.set_query('priority', function() {
913 return {
914 filters: {
915 'name': ['in', r.message.priority],
Himanshuec25d592021-06-14 19:05:52 +0530916 }
Ankush Menatbd9ef742023-07-16 11:34:42 +0530917 };
Himanshuec25d592021-06-14 19:05:52 +0530918 });
Ankush Menatbd9ef742023-07-16 11:34:42 +0530919 frm.set_query('service_level_agreement', function() {
920 return {
921 filters: {
922 'name': ['in', r.message.service_level_agreements],
923 }
924 };
925 });
Himanshuec25d592021-06-14 19:05:52 +0530926 }
Ankush Menatbd9ef742023-07-16 11:34:42 +0530927 }
Himanshuec25d592021-06-14 19:05:52 +0530928 });
Ankush Menatbd9ef742023-07-16 11:34:42 +0530929 },
930
931 refresh: function(frm) {
932 if (frm.doc.status !== 'Closed' && frm.doc.service_level_agreement
933 && ['First Response Due', 'Resolution Due'].includes(frm.doc.agreement_status)) {
934 frappe.call({
935 'method': 'frappe.client.get',
936 args: {
937 doctype: 'Service Level Agreement',
938 name: frm.doc.service_level_agreement
939 },
940 callback: function(data) {
941 let statuses = data.message.pause_sla_on;
942 const hold_statuses = [];
943 $.each(statuses, (_i, entry) => {
944 hold_statuses.push(entry.status);
945 });
946 if (hold_statuses.includes(frm.doc.status)) {
947 frm.dashboard.clear_headline();
948 let message = {'indicator': 'orange', 'msg': __('SLA is on hold since {0}', [moment(frm.doc.on_hold_since).fromNow(true)])};
949 frm.dashboard.set_headline_alert(
950 '<div class="row">' +
951 '<div class="col-xs-12">' +
952 '<span class="indicator whitespace-nowrap '+ message.indicator +'"><span>'+ message.msg +'</span></span> ' +
953 '</div>' +
954 '</div>'
955 );
956 } else {
957 set_time_to_resolve_and_response(frm, data.message.apply_sla_for_resolution);
958 }
959 }
960 });
961 } else if (frm.doc.service_level_agreement) {
962 frm.dashboard.clear_headline();
963
964 let agreement_status = (frm.doc.agreement_status == 'Fulfilled') ?
965 {'indicator': 'green', 'msg': 'Service Level Agreement has been fulfilled'} :
966 {'indicator': 'red', 'msg': 'Service Level Agreement Failed'};
967
968 frm.dashboard.set_headline_alert(
969 '<div class="row">' +
970 '<div class="col-xs-12">' +
971 '<span class="indicator whitespace-nowrap '+ agreement_status.indicator +'"><span class="hidden-xs">'+ agreement_status.msg +'</span></span> ' +
972 '</div>' +
973 '</div>'
974 );
975 }
976 },
977 });
Himanshuec25d592021-06-14 19:05:52 +0530978 });
979});
980
981function set_time_to_resolve_and_response(frm, apply_sla_for_resolution) {
982 frm.dashboard.clear_headline();
983
Saqib24114772021-12-15 11:45:34 +0530984 let time_to_respond;
Saqib Ansari476e81a2021-12-06 18:55:30 +0530985 if (!frm.doc.first_responded_on) {
Himanshuec25d592021-06-14 19:05:52 +0530986 time_to_respond = get_time_left(frm.doc.response_by, frm.doc.agreement_status);
Saqib24114772021-12-15 11:45:34 +0530987 } else {
988 time_to_respond = get_status(frm.doc.response_by, frm.doc.first_responded_on);
Himanshuec25d592021-06-14 19:05:52 +0530989 }
990
991 let alert = `
992 <div class="row">
993 <div class="col-xs-12 col-sm-6">
994 <span class="indicator whitespace-nowrap ${time_to_respond.indicator}">
995 <span>Time to Respond: ${time_to_respond.diff_display}</span>
996 </span>
997 </div>`;
998
999
1000 if (apply_sla_for_resolution) {
Saqib24114772021-12-15 11:45:34 +05301001 let time_to_resolve;
Saqib Ansari476e81a2021-12-06 18:55:30 +05301002 if (!frm.doc.resolution_date) {
Himanshuec25d592021-06-14 19:05:52 +05301003 time_to_resolve = get_time_left(frm.doc.resolution_by, frm.doc.agreement_status);
Saqib24114772021-12-15 11:45:34 +05301004 } else {
1005 time_to_resolve = get_status(frm.doc.resolution_by, frm.doc.resolution_date);
Himanshuec25d592021-06-14 19:05:52 +05301006 }
1007
1008 alert += `
1009 <div class="col-xs-12 col-sm-6">
1010 <span class="indicator whitespace-nowrap ${time_to_resolve.indicator}">
1011 <span>Time to Resolve: ${time_to_resolve.diff_display}</span>
1012 </span>
1013 </div>`;
1014 }
1015
1016 alert += '</div>';
1017
1018 frm.dashboard.set_headline_alert(alert);
1019}
1020
1021function get_time_left(timestamp, agreement_status) {
1022 const diff = moment(timestamp).diff(moment());
1023 const diff_display = diff >= 44500 ? moment.duration(diff).humanize() : 'Failed';
1024 let indicator = (diff_display == 'Failed' && agreement_status != 'Fulfilled') ? 'red' : 'green';
1025 return {'diff_display': diff_display, 'indicator': indicator};
1026}
1027
Saqib24114772021-12-15 11:45:34 +05301028function get_status(expected, actual) {
1029 const time_left = moment(expected).diff(moment(actual));
Saqib Ansari476e81a2021-12-06 18:55:30 +05301030 if (time_left >= 0) {
Himanshuec25d592021-06-14 19:05:52 +05301031 return {'diff_display': 'Fulfilled', 'indicator': 'green'};
1032 } else {
1033 return {'diff_display': 'Failed', 'indicator': 'red'};
1034 }
1035}
1036
Alanc6dc9ea2021-05-07 20:30:04 +05301037function attach_selector_button(inner_text, append_loction, context, grid_row) {
1038 let $btn_div = $("<div>").css({"margin-bottom": "10px", "margin-top": "10px"})
1039 .appendTo(append_loction);
1040 let $btn = $(`<button class="btn btn-sm btn-default">${inner_text}</button>`)
1041 .appendTo($btn_div);
1042
1043 $btn.on("click", function() {
1044 context.show_serial_batch_selector(grid_row.frm, grid_row.doc, "", "", true);
1045 });
Rohit Waghchaure708eefb2023-07-10 16:54:55 +05301046}