blob: 497f8d2674e72de5deffe925edac311d77f9edff [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) {
404 let fiscal_year = '';
405 frappe.call({
406 method: "erpnext.accounts.utils.get_fiscal_year",
407 args: {
408 date: date
409 },
410 async: false,
411 callback: function(r) {
412 if (r.message) {
413 fiscal_year = r.message[0];
414 }
415 }
416 });
417 return fiscal_year;
Rohit Waghchaure1c2fe082023-06-15 12:54:43 +0530418 }
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530419});
420
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530421erpnext.utils.select_alternate_items = function(opts) {
422 const frm = opts.frm;
423 const warehouse_field = opts.warehouse_field || 'warehouse';
424 const item_field = opts.item_field || 'item_code';
425
426 this.data = [];
427 const dialog = new frappe.ui.Dialog({
428 title: __("Select Alternate Item"),
429 fields: [
430 {fieldtype:'Section Break', label: __('Items')},
431 {
432 fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true,
433 in_place_edit: true, data: this.data,
434 get_data: () => {
435 return this.data;
436 },
437 fields: [{
438 fieldtype:'Data',
439 fieldname:"docname",
440 hidden: 1
441 }, {
442 fieldtype:'Link',
443 fieldname:"item_code",
444 options: 'Item',
445 in_list_view: 1,
446 read_only: 1,
447 label: __('Item Code')
448 }, {
449 fieldtype:'Link',
450 fieldname:"alternate_item",
451 options: 'Item',
452 default: "",
453 in_list_view: 1,
454 label: __('Alternate Item'),
455 onchange: function() {
456 const item_code = this.get_value();
457 const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value();
458 if (item_code && warehouse) {
459 frappe.call({
460 method: "erpnext.stock.utils.get_latest_stock_qty",
461 args: {
462 item_code: item_code,
463 warehouse: warehouse
464 },
465 callback: (r) => {
466 this.grid_row.on_grid_fields_dict
467 .actual_qty.set_value(r.message || 0);
468 }
469 })
470 }
471 },
472 get_query: (e) => {
473 return {
474 query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items",
475 filters: {
476 item_code: e.item_code
477 }
478 };
479 }
480 }, {
481 fieldtype:'Link',
482 fieldname:"warehouse",
483 options: 'Warehouse',
484 default: "",
485 in_list_view: 1,
486 label: __('Warehouse'),
487 onchange: function() {
488 const warehouse = this.get_value();
489 const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value();
490 if (item_code && warehouse) {
491 frappe.call({
492 method: "erpnext.stock.utils.get_latest_stock_qty",
493 args: {
494 item_code: item_code,
495 warehouse: warehouse
496 },
497 callback: (r) => {
498 this.grid_row.on_grid_fields_dict
499 .actual_qty.set_value(r.message || 0);
500 }
501 })
502 }
503 },
504 }, {
505 fieldtype:'Float',
506 fieldname:"actual_qty",
507 default: 0,
508 read_only: 1,
509 in_list_view: 1,
510 label: __('Available Qty')
511 }]
512 },
513 ],
514 primary_action: function() {
515 const args = this.get_values()["alternative_items"];
516 const alternative_items = args.filter(d => {
517 if (d.alternate_item && d.item_code != d.alternate_item) {
518 return true;
519 }
520 });
521
522 alternative_items.forEach(d => {
523 let row = frappe.get_doc(opts.child_doctype, d.docname);
Doridel Cahanape42192b2018-05-16 13:28:39 +0800524 let qty = null;
525 if (row.doctype === 'Work Order Item') {
526 qty = row.required_qty;
527 } else {
528 qty = row.qty;
529 }
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530530 row[item_field] = d.alternate_item;
mergify[bot]c1a0f642021-12-17 19:06:19 +0530531 frappe.model.set_value(row.doctype, row.name, 'qty', qty);
532 frappe.model.set_value(row.doctype, row.name, opts.original_item_field, d.item_code);
533 frm.trigger(item_field, row.doctype, row.name);
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530534 });
535
536 refresh_field(opts.child_docname);
537 this.hide();
538 },
539 primary_action_label: __('Update')
540 });
541
542 frm.doc[opts.child_docname].forEach(d => {
543 if (!opts.condition || opts.condition(d)) {
544 dialog.fields_dict.alternative_items.df.data.push({
545 "docname": d.name,
546 "item_code": d[item_field],
547 "warehouse": d[warehouse_field],
548 "actual_qty": d.actual_qty
549 });
550 }
551 })
552
553 this.data = dialog.fields_dict.alternative_items.df.data;
554 dialog.fields_dict.alternative_items.grid.refresh();
555 dialog.show();
556}
557
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800558erpnext.utils.update_child_items = function(opts) {
559 const frm = opts.frm;
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100560 const cannot_add_row = (typeof opts.cannot_add_row === 'undefined') ? true : opts.cannot_add_row;
561 const child_docname = (typeof opts.cannot_add_row === 'undefined') ? "items" : opts.child_docname;
Saqib56fea7d2020-10-09 21:19:25 +0530562 const child_meta = frappe.get_meta(`${frm.doc.doctype} Item`);
563 const get_precision = (fieldname) => child_meta.fields.find(f => f.fieldname == fieldname).precision;
564
Devin Slauenwhitea835c1a2023-01-26 16:53:05 -0500565 this.data = frm.doc[opts.child_docname].map((d) => {
566 return {
567 "docname": d.name,
568 "name": d.name,
569 "item_code": d.item_code,
570 "delivery_date": d.delivery_date,
571 "schedule_date": d.schedule_date,
572 "conversion_factor": d.conversion_factor,
573 "qty": d.qty,
574 "rate": d.rate,
575 "uom": d.uom
576 }
577 });
578
Saqib Ansarif53299e2020-04-15 22:08:12 +0530579 const fields = [{
580 fieldtype:'Data',
581 fieldname:"docname",
582 read_only: 1,
583 hidden: 1,
584 }, {
585 fieldtype:'Link',
586 fieldname:"item_code",
587 options: 'Item',
588 in_list_view: 1,
589 read_only: 0,
590 disabled: 0,
Afshandc7280e2021-08-18 17:44:40 +0530591 label: __('Item Code'),
592 get_query: function() {
593 let filters;
594 if (frm.doc.doctype == 'Sales Order') {
595 filters = {"is_sales_item": 1};
596 } else if (frm.doc.doctype == 'Purchase Order') {
Sagar Sharmad074c932022-03-31 19:57:42 +0530597 if (frm.doc.is_subcontracted) {
s-aga-r6d89b2f2022-06-18 15:46:59 +0530598 if (frm.doc.is_old_subcontracting_flow) {
599 filters = {"is_sub_contracted_item": 1};
600 } else {
601 filters = {"is_stock_item": 0};
602 }
Afshandc7280e2021-08-18 17:44:40 +0530603 } else {
604 filters = {"is_purchase_item": 1};
605 }
606 }
607 return {
608 query: "erpnext.controllers.queries.item_query",
609 filters: filters
610 };
611 }
Saqib Ansarif53299e2020-04-15 22:08:12 +0530612 }, {
Saqib61314242020-09-15 11:14:31 +0530613 fieldtype:'Link',
614 fieldname:'uom',
615 options: 'UOM',
616 read_only: 0,
617 label: __('UOM'),
618 reqd: 1,
619 onchange: function () {
620 frappe.call({
621 method: "erpnext.stock.get_item_details.get_conversion_factor",
622 args: { item_code: this.doc.item_code, uom: this.value },
623 callback: r => {
624 if(!r.exc) {
625 if (this.doc.conversion_factor == r.message.conversion_factor) return;
marination4f395cc2020-09-24 12:43:41 +0530626
Saqib61314242020-09-15 11:14:31 +0530627 const docname = this.doc.docname;
628 dialog.fields_dict.trans_items.df.data.some(doc => {
629 if (doc.docname == docname) {
630 doc.conversion_factor = r.message.conversion_factor;
631 dialog.fields_dict.trans_items.grid.refresh();
632 return true;
633 }
634 })
635 }
636 }
637 });
638 }
639 }, {
Saqib Ansarif53299e2020-04-15 22:08:12 +0530640 fieldtype:'Float',
641 fieldname:"qty",
642 default: 0,
643 read_only: 0,
644 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530645 label: __('Qty'),
646 precision: get_precision("qty")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530647 }, {
648 fieldtype:'Currency',
649 fieldname:"rate",
Afshan84184552021-02-24 16:51:11 +0530650 options: "currency",
Saqib Ansarif53299e2020-04-15 22:08:12 +0530651 default: 0,
652 read_only: 0,
653 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530654 label: __('Rate'),
655 precision: get_precision("rate")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530656 }];
657
658 if (frm.doc.doctype == 'Sales Order' || frm.doc.doctype == 'Purchase Order' ) {
659 fields.splice(2, 0, {
660 fieldtype: 'Date',
661 fieldname: frm.doc.doctype == 'Sales Order' ? "delivery_date" : "schedule_date",
662 in_list_view: 1,
Saqib Ansaric579b082020-05-29 22:21:50 +0530663 label: frm.doc.doctype == 'Sales Order' ? __("Delivery Date") : __("Reqd by date"),
664 reqd: 1
Saqib Ansarif53299e2020-04-15 22:08:12 +0530665 })
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530666 fields.splice(3, 0, {
667 fieldtype: 'Float',
668 fieldname: "conversion_factor",
Saqib56fea7d2020-10-09 21:19:25 +0530669 label: __("Conversion Factor"),
670 precision: get_precision('conversion_factor')
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530671 })
Saqib Ansarif53299e2020-04-15 22:08:12 +0530672 }
673
Devin Slauenwhitea835c1a2023-01-26 16:53:05 -0500674 new frappe.ui.Dialog({
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800675 title: __("Update Items"),
Raffael Meyerd5fe1432023-07-14 08:57:35 +0200676 size: "extra-large",
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800677 fields: [
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800678 {
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100679 fieldname: "trans_items",
680 fieldtype: "Table",
Maricafc96c1a2020-03-06 10:57:59 +0530681 label: "Items",
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100682 cannot_add_rows: cannot_add_row,
Saqibbc919e22020-11-05 17:38:35 +0530683 in_place_edit: false,
Maricafc96c1a2020-03-06 10:57:59 +0530684 reqd: 1,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100685 data: this.data,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800686 get_data: () => {
687 return this.data;
688 },
Saqib Ansarif53299e2020-04-15 22:08:12 +0530689 fields: fields
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800690 },
691 ],
692 primary_action: function() {
Ankush Menat6de7b8e2021-08-24 12:18:40 +0530693 const trans_items = this.get_values()["trans_items"].filter((item) => !!item.item_code);
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800694 frappe.call({
695 method: 'erpnext.controllers.accounts_controller.update_child_qty_rate',
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100696 freeze: true,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800697 args: {
698 'parent_doctype': frm.doc.doctype,
699 'trans_items': trans_items,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100700 'parent_doctype_name': frm.doc.name,
701 'child_docname': child_docname
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800702 },
703 callback: function() {
704 frm.reload_doc();
705 }
706 });
707 this.hide();
708 refresh_field("items");
709 },
710 primary_action_label: __('Update')
Devin Slauenwhitea835c1a2023-01-26 16:53:05 -0500711 }).show();
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800712}
713
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530714erpnext.utils.map_current_doc = function(opts) {
Sagar Vora78777d62021-03-09 20:35:08 +0530715 function _map() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530716 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530717 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530718 if(!cur_frm.doc.items[0].item_code) {
719 cur_frm.doc.items = cur_frm.doc.items.splice(1);
720 }
bhupen3c7e70f2016-11-30 14:32:11 +0530721
722 // find the doctype of the items table
723 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530724
bhupen3c7e70f2016-11-30 14:32:11 +0530725 // find the link fieldname from items table for the given
726 // source_doctype
727 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530728 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530729 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
730
Nabin Hait802b4352017-01-09 15:32:20 +0530731 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530732 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530733 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530734
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530735 $.each(cur_frm.doc.items, function(i, d) {
736 opts.source_name.forEach(function(src) {
737 if(d[link_fieldname]==src) {
738 already_set = true;
739 if (item_qty_map[d.item_code])
740 item_qty_map[d.item_code] += flt(d.qty);
741 else
742 item_qty_map[d.item_code] = flt(d.qty);
743 }
744 });
745 });
746
747 if(already_set) {
748 opts.source_name.forEach(function(src) {
749 frappe.model.with_doc(opts.source_doctype, src, function(r) {
750 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
751 $.each(source_doc.items || [], function(i, row) {
752 if(row.qty > flt(item_qty_map[row.item_code])) {
753 already_set = false;
754 return false;
755 }
756 })
757 })
758
759 if(already_set) {
760 frappe.msgprint(__("You have already selected items from {0} {1}",
761 [opts.source_doctype, src]));
762 return;
763 }
764
765 })
Nabin Hait802b4352017-01-09 15:32:20 +0530766 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530767 }
768
769 return frappe.call({
770 // Sometimes we hit the limit for URL length of a GET request
771 // as we send the full target_doc. Hence this is a POST request.
772 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530773 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530774 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530775 "method": opts.method,
776 "source_names": opts.source_name,
777 "target_doc": cur_frm.doc,
Maricadb002702020-02-17 15:58:08 +0530778 "args": opts.args
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530779 },
780 callback: function(r) {
781 if(!r.exc) {
782 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530783 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530784 cur_frm.refresh();
785 }
786 }
787 });
788 }
Sagar Vora78777d62021-03-09 20:35:08 +0530789
790 let query_args = {};
791 if (opts.get_query_filters) {
792 query_args.filters = opts.get_query_filters;
793 }
794
795 if (opts.get_query_method) {
796 query_args.query = opts.get_query_method;
797 }
798
799 if (query_args.filters || query_args.query) {
800 opts.get_query = () => query_args;
801 }
802
803 if (opts.source_doctype) {
804 const d = new frappe.ui.form.MultiSelectDialog({
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530805 doctype: opts.source_doctype,
806 target: opts.target,
807 date_field: opts.date_field || undefined,
808 setters: opts.setters,
809 get_query: opts.get_query,
marination4f395cc2020-09-24 12:43:41 +0530810 add_filters_group: 1,
Saqib7292f542021-09-13 12:13:43 +0530811 allow_child_item_selection: opts.allow_child_item_selection,
gn3060292a619fd2022-06-29 17:15:21 +0800812 child_fieldname: opts.child_fieldname,
Saqib7292f542021-09-13 12:13:43 +0530813 child_columns: opts.child_columns,
Rohit Waghchaure69ffddf2021-10-07 17:33:58 +0530814 size: opts.size,
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530815 action: function(selections, args) {
816 let values = selections;
Saqibe03d9aa2021-09-17 13:03:27 +0530817 if (values.length === 0) {
Nabin Haita11dcb62017-12-04 13:36:50 +0530818 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530819 return;
820 }
821 opts.source_name = values;
Saqibe03d9aa2021-09-17 13:03:27 +0530822 if (opts.allow_child_item_selection) {
823 // args contains filtered child docnames
824 opts.args = args;
825 }
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530826 d.dialog.hide();
827 _map();
828 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530829 });
Sagar Vora78777d62021-03-09 20:35:08 +0530830
831 return d;
832 }
833
834 if (opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530835 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530836 _map();
837 }
838}
839
Rushabh Mehta180e4352016-07-26 17:57:42 +0530840frappe.form.link_formatters['Item'] = function(value, doc) {
Alan157b3882021-04-21 21:00:22 +0530841 if (doc && value && doc.item_name && doc.item_name !== value && doc.item_code === value) {
marination824f48f2020-10-12 20:08:03 +0530842 return value + ': ' + doc.item_name;
843 } else if (!value && doc.doctype && doc.item_name) {
844 // format blank value in child table
845 return doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530846 } else {
marination824f48f2020-10-12 20:08:03 +0530847 // 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 +0530848 return value;
849 }
850}
851
852frappe.form.link_formatters['Employee'] = function(value, doc) {
Rucha Mahabal5ba1bc12021-11-23 09:30:30 +0530853 if (doc && value && doc.employee_name && doc.employee_name !== value && doc.employee === value) {
854 return value + ': ' + doc.employee_name;
855 } else if (!value && doc.doctype && doc.employee_name) {
856 // format blank value in child table
857 return doc.employee;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530858 } else {
Rucha Mahabal5ba1bc12021-11-23 09:30:30 +0530859 // 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 +0530860 return value;
861 }
862}
863
Rucha Mahabal59961f72021-05-20 23:43:19 +0530864frappe.form.link_formatters['Project'] = function(value, doc) {
865 if (doc && value && doc.project_name && doc.project_name !== value && doc.project === value) {
866 return value + ': ' + doc.project_name;
867 } else if (!value && doc.doctype && doc.project_name) {
868 // format blank value in child table
869 return doc.project;
870 } else {
871 // if value is blank in report view or project name and name are the same, return as is
872 return value;
873 }
874};
875
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530876// add description on posting time
877$(document).on('app_ready', function() {
878 if(!frappe.datetime.is_timezone_same()) {
879 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
880 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
881 frappe.ui.form.on(d, "onload", function(frm) {
882 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530883 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530884 });
885 });
886 }
akshay14384c22016-07-28 13:53:17 +0530887});
Alanc6dc9ea2021-05-07 20:30:04 +0530888
Himanshuec25d592021-06-14 19:05:52 +0530889// Show SLA dashboard
890$(document).on('app_ready', function() {
Ankush Menatbd9ef742023-07-16 11:34:42 +0530891 $.each(frappe.boot.service_level_agreement_doctypes, function(_i, d) {
892 frappe.ui.form.on(d, {
893 onload: function(frm) {
894 if (!frm.doc.service_level_agreement)
895 return;
Himanshuec25d592021-06-14 19:05:52 +0530896
Ankush Menatbd9ef742023-07-16 11:34:42 +0530897 frappe.call({
898 method: 'erpnext.support.doctype.service_level_agreement.service_level_agreement.get_service_level_agreement_filters',
899 args: {
900 doctype: frm.doc.doctype,
901 name: frm.doc.service_level_agreement,
902 customer: frm.doc.customer
Himanshuec25d592021-06-14 19:05:52 +0530903 },
Ankush Menatbd9ef742023-07-16 11:34:42 +0530904 callback: function (r) {
905 if (r && r.message) {
906 frm.set_query('priority', function() {
907 return {
908 filters: {
909 'name': ['in', r.message.priority],
Himanshuec25d592021-06-14 19:05:52 +0530910 }
Ankush Menatbd9ef742023-07-16 11:34:42 +0530911 };
Himanshuec25d592021-06-14 19:05:52 +0530912 });
Ankush Menatbd9ef742023-07-16 11:34:42 +0530913 frm.set_query('service_level_agreement', function() {
914 return {
915 filters: {
916 'name': ['in', r.message.service_level_agreements],
917 }
918 };
919 });
Himanshuec25d592021-06-14 19:05:52 +0530920 }
Ankush Menatbd9ef742023-07-16 11:34:42 +0530921 }
Himanshuec25d592021-06-14 19:05:52 +0530922 });
Ankush Menatbd9ef742023-07-16 11:34:42 +0530923 },
924
925 refresh: function(frm) {
926 if (frm.doc.status !== 'Closed' && frm.doc.service_level_agreement
927 && ['First Response Due', 'Resolution Due'].includes(frm.doc.agreement_status)) {
928 frappe.call({
929 'method': 'frappe.client.get',
930 args: {
931 doctype: 'Service Level Agreement',
932 name: frm.doc.service_level_agreement
933 },
934 callback: function(data) {
935 let statuses = data.message.pause_sla_on;
936 const hold_statuses = [];
937 $.each(statuses, (_i, entry) => {
938 hold_statuses.push(entry.status);
939 });
940 if (hold_statuses.includes(frm.doc.status)) {
941 frm.dashboard.clear_headline();
942 let message = {'indicator': 'orange', 'msg': __('SLA is on hold since {0}', [moment(frm.doc.on_hold_since).fromNow(true)])};
943 frm.dashboard.set_headline_alert(
944 '<div class="row">' +
945 '<div class="col-xs-12">' +
946 '<span class="indicator whitespace-nowrap '+ message.indicator +'"><span>'+ message.msg +'</span></span> ' +
947 '</div>' +
948 '</div>'
949 );
950 } else {
951 set_time_to_resolve_and_response(frm, data.message.apply_sla_for_resolution);
952 }
953 }
954 });
955 } else if (frm.doc.service_level_agreement) {
956 frm.dashboard.clear_headline();
957
958 let agreement_status = (frm.doc.agreement_status == 'Fulfilled') ?
959 {'indicator': 'green', 'msg': 'Service Level Agreement has been fulfilled'} :
960 {'indicator': 'red', 'msg': 'Service Level Agreement Failed'};
961
962 frm.dashboard.set_headline_alert(
963 '<div class="row">' +
964 '<div class="col-xs-12">' +
965 '<span class="indicator whitespace-nowrap '+ agreement_status.indicator +'"><span class="hidden-xs">'+ agreement_status.msg +'</span></span> ' +
966 '</div>' +
967 '</div>'
968 );
969 }
970 },
971 });
Himanshuec25d592021-06-14 19:05:52 +0530972 });
973});
974
975function set_time_to_resolve_and_response(frm, apply_sla_for_resolution) {
976 frm.dashboard.clear_headline();
977
Saqib24114772021-12-15 11:45:34 +0530978 let time_to_respond;
Saqib Ansari476e81a2021-12-06 18:55:30 +0530979 if (!frm.doc.first_responded_on) {
Himanshuec25d592021-06-14 19:05:52 +0530980 time_to_respond = get_time_left(frm.doc.response_by, frm.doc.agreement_status);
Saqib24114772021-12-15 11:45:34 +0530981 } else {
982 time_to_respond = get_status(frm.doc.response_by, frm.doc.first_responded_on);
Himanshuec25d592021-06-14 19:05:52 +0530983 }
984
985 let alert = `
986 <div class="row">
987 <div class="col-xs-12 col-sm-6">
988 <span class="indicator whitespace-nowrap ${time_to_respond.indicator}">
989 <span>Time to Respond: ${time_to_respond.diff_display}</span>
990 </span>
991 </div>`;
992
993
994 if (apply_sla_for_resolution) {
Saqib24114772021-12-15 11:45:34 +0530995 let time_to_resolve;
Saqib Ansari476e81a2021-12-06 18:55:30 +0530996 if (!frm.doc.resolution_date) {
Himanshuec25d592021-06-14 19:05:52 +0530997 time_to_resolve = get_time_left(frm.doc.resolution_by, frm.doc.agreement_status);
Saqib24114772021-12-15 11:45:34 +0530998 } else {
999 time_to_resolve = get_status(frm.doc.resolution_by, frm.doc.resolution_date);
Himanshuec25d592021-06-14 19:05:52 +05301000 }
1001
1002 alert += `
1003 <div class="col-xs-12 col-sm-6">
1004 <span class="indicator whitespace-nowrap ${time_to_resolve.indicator}">
1005 <span>Time to Resolve: ${time_to_resolve.diff_display}</span>
1006 </span>
1007 </div>`;
1008 }
1009
1010 alert += '</div>';
1011
1012 frm.dashboard.set_headline_alert(alert);
1013}
1014
1015function get_time_left(timestamp, agreement_status) {
1016 const diff = moment(timestamp).diff(moment());
1017 const diff_display = diff >= 44500 ? moment.duration(diff).humanize() : 'Failed';
1018 let indicator = (diff_display == 'Failed' && agreement_status != 'Fulfilled') ? 'red' : 'green';
1019 return {'diff_display': diff_display, 'indicator': indicator};
1020}
1021
Saqib24114772021-12-15 11:45:34 +05301022function get_status(expected, actual) {
1023 const time_left = moment(expected).diff(moment(actual));
Saqib Ansari476e81a2021-12-06 18:55:30 +05301024 if (time_left >= 0) {
Himanshuec25d592021-06-14 19:05:52 +05301025 return {'diff_display': 'Fulfilled', 'indicator': 'green'};
1026 } else {
1027 return {'diff_display': 'Failed', 'indicator': 'red'};
1028 }
1029}
1030
Alanc6dc9ea2021-05-07 20:30:04 +05301031function attach_selector_button(inner_text, append_loction, context, grid_row) {
1032 let $btn_div = $("<div>").css({"margin-bottom": "10px", "margin-top": "10px"})
1033 .appendTo(append_loction);
1034 let $btn = $(`<button class="btn btn-sm btn-default">${inner_text}</button>`)
1035 .appendTo($btn_div);
1036
1037 $btn.on("click", function() {
1038 context.show_serial_batch_selector(grid_row.frm, grid_row.doc, "", "", true);
1039 });
Rohit Waghchaure708eefb2023-07-10 16:54:55 +05301040}