blob: 9116fd1f036e02882c6190eb8ce5e66f32827c0c [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,
582 "uom": d.uom
583 }
584 });
585
Saqib Ansarif53299e2020-04-15 22:08:12 +0530586 const fields = [{
587 fieldtype:'Data',
588 fieldname:"docname",
589 read_only: 1,
590 hidden: 1,
591 }, {
592 fieldtype:'Link',
593 fieldname:"item_code",
594 options: 'Item',
595 in_list_view: 1,
596 read_only: 0,
597 disabled: 0,
Afshandc7280e2021-08-18 17:44:40 +0530598 label: __('Item Code'),
599 get_query: function() {
600 let filters;
601 if (frm.doc.doctype == 'Sales Order') {
602 filters = {"is_sales_item": 1};
603 } else if (frm.doc.doctype == 'Purchase Order') {
Sagar Sharmad074c932022-03-31 19:57:42 +0530604 if (frm.doc.is_subcontracted) {
s-aga-r6d89b2f2022-06-18 15:46:59 +0530605 if (frm.doc.is_old_subcontracting_flow) {
606 filters = {"is_sub_contracted_item": 1};
607 } else {
608 filters = {"is_stock_item": 0};
609 }
Afshandc7280e2021-08-18 17:44:40 +0530610 } else {
611 filters = {"is_purchase_item": 1};
612 }
613 }
614 return {
615 query: "erpnext.controllers.queries.item_query",
616 filters: filters
617 };
618 }
Saqib Ansarif53299e2020-04-15 22:08:12 +0530619 }, {
Saqib61314242020-09-15 11:14:31 +0530620 fieldtype:'Link',
621 fieldname:'uom',
622 options: 'UOM',
623 read_only: 0,
624 label: __('UOM'),
625 reqd: 1,
626 onchange: function () {
627 frappe.call({
628 method: "erpnext.stock.get_item_details.get_conversion_factor",
629 args: { item_code: this.doc.item_code, uom: this.value },
630 callback: r => {
631 if(!r.exc) {
632 if (this.doc.conversion_factor == r.message.conversion_factor) return;
marination4f395cc2020-09-24 12:43:41 +0530633
Saqib61314242020-09-15 11:14:31 +0530634 const docname = this.doc.docname;
635 dialog.fields_dict.trans_items.df.data.some(doc => {
636 if (doc.docname == docname) {
637 doc.conversion_factor = r.message.conversion_factor;
638 dialog.fields_dict.trans_items.grid.refresh();
639 return true;
640 }
641 })
642 }
643 }
644 });
645 }
646 }, {
Saqib Ansarif53299e2020-04-15 22:08:12 +0530647 fieldtype:'Float',
648 fieldname:"qty",
649 default: 0,
650 read_only: 0,
651 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530652 label: __('Qty'),
653 precision: get_precision("qty")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530654 }, {
655 fieldtype:'Currency',
656 fieldname:"rate",
Afshan84184552021-02-24 16:51:11 +0530657 options: "currency",
Saqib Ansarif53299e2020-04-15 22:08:12 +0530658 default: 0,
659 read_only: 0,
660 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530661 label: __('Rate'),
662 precision: get_precision("rate")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530663 }];
664
665 if (frm.doc.doctype == 'Sales Order' || frm.doc.doctype == 'Purchase Order' ) {
666 fields.splice(2, 0, {
667 fieldtype: 'Date',
668 fieldname: frm.doc.doctype == 'Sales Order' ? "delivery_date" : "schedule_date",
669 in_list_view: 1,
Saqib Ansaric579b082020-05-29 22:21:50 +0530670 label: frm.doc.doctype == 'Sales Order' ? __("Delivery Date") : __("Reqd by date"),
671 reqd: 1
Saqib Ansarif53299e2020-04-15 22:08:12 +0530672 })
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530673 fields.splice(3, 0, {
674 fieldtype: 'Float',
675 fieldname: "conversion_factor",
Saqib56fea7d2020-10-09 21:19:25 +0530676 label: __("Conversion Factor"),
677 precision: get_precision('conversion_factor')
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530678 })
Saqib Ansarif53299e2020-04-15 22:08:12 +0530679 }
680
s-aga-rfaf9f132023-08-21 15:51:57 +0530681 if (frm.doc.doctype == 'Purchase Order' && frm.doc.is_subcontracted && !frm.doc.is_old_subcontracting_flow) {
682 fields.push({
683 fieldtype:'Link',
684 fieldname:'fg_item',
685 options: 'Item',
686 reqd: 1,
687 in_list_view: 0,
688 read_only: 0,
689 disabled: 0,
690 label: __('Finished Good Item'),
691 get_query: () => {
692 return {
693 filters: {
694 'is_stock_item': 1,
695 'is_sub_contracted_item': 1,
696 'default_bom': ['!=', '']
697 }
698 }
699 },
700 }, {
701 fieldtype:'Float',
702 fieldname:'fg_item_qty',
703 reqd: 1,
704 default: 0,
705 read_only: 0,
706 in_list_view: 0,
707 label: __('Finished Good Item Qty'),
708 precision: get_precision('fg_item_qty')
709 })
710 }
711
Deepesh Garg40772542023-07-22 23:07:18 +0530712 let dialog = new frappe.ui.Dialog({
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800713 title: __("Update Items"),
Raffael Meyerd5fe1432023-07-14 08:57:35 +0200714 size: "extra-large",
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800715 fields: [
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800716 {
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100717 fieldname: "trans_items",
718 fieldtype: "Table",
Maricafc96c1a2020-03-06 10:57:59 +0530719 label: "Items",
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100720 cannot_add_rows: cannot_add_row,
Saqibbc919e22020-11-05 17:38:35 +0530721 in_place_edit: false,
Maricafc96c1a2020-03-06 10:57:59 +0530722 reqd: 1,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100723 data: this.data,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800724 get_data: () => {
725 return this.data;
726 },
Saqib Ansarif53299e2020-04-15 22:08:12 +0530727 fields: fields
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800728 },
729 ],
730 primary_action: function() {
Ankush Menat6de7b8e2021-08-24 12:18:40 +0530731 const trans_items = this.get_values()["trans_items"].filter((item) => !!item.item_code);
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800732 frappe.call({
733 method: 'erpnext.controllers.accounts_controller.update_child_qty_rate',
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100734 freeze: true,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800735 args: {
736 'parent_doctype': frm.doc.doctype,
737 'trans_items': trans_items,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100738 'parent_doctype_name': frm.doc.name,
739 'child_docname': child_docname
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800740 },
741 callback: function() {
742 frm.reload_doc();
743 }
744 });
745 this.hide();
746 refresh_field("items");
747 },
748 primary_action_label: __('Update')
Deepesh Garg40772542023-07-22 23:07:18 +0530749 })
750
751 dialog.show();
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800752}
753
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530754erpnext.utils.map_current_doc = function(opts) {
Sagar Vora78777d62021-03-09 20:35:08 +0530755 function _map() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530756 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530757 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530758 if(!cur_frm.doc.items[0].item_code) {
759 cur_frm.doc.items = cur_frm.doc.items.splice(1);
760 }
bhupen3c7e70f2016-11-30 14:32:11 +0530761
762 // find the doctype of the items table
763 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530764
bhupen3c7e70f2016-11-30 14:32:11 +0530765 // find the link fieldname from items table for the given
766 // source_doctype
767 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530768 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530769 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
770
Nabin Hait802b4352017-01-09 15:32:20 +0530771 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530772 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530773 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530774
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530775 $.each(cur_frm.doc.items, function(i, d) {
776 opts.source_name.forEach(function(src) {
777 if(d[link_fieldname]==src) {
778 already_set = true;
779 if (item_qty_map[d.item_code])
780 item_qty_map[d.item_code] += flt(d.qty);
781 else
782 item_qty_map[d.item_code] = flt(d.qty);
783 }
784 });
785 });
786
787 if(already_set) {
788 opts.source_name.forEach(function(src) {
789 frappe.model.with_doc(opts.source_doctype, src, function(r) {
790 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
791 $.each(source_doc.items || [], function(i, row) {
792 if(row.qty > flt(item_qty_map[row.item_code])) {
793 already_set = false;
794 return false;
795 }
796 })
797 })
798
799 if(already_set) {
800 frappe.msgprint(__("You have already selected items from {0} {1}",
801 [opts.source_doctype, src]));
802 return;
803 }
804
805 })
Nabin Hait802b4352017-01-09 15:32:20 +0530806 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530807 }
808
809 return frappe.call({
810 // Sometimes we hit the limit for URL length of a GET request
811 // as we send the full target_doc. Hence this is a POST request.
812 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530813 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530814 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530815 "method": opts.method,
816 "source_names": opts.source_name,
817 "target_doc": cur_frm.doc,
Maricadb002702020-02-17 15:58:08 +0530818 "args": opts.args
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530819 },
820 callback: function(r) {
821 if(!r.exc) {
822 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530823 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530824 cur_frm.refresh();
825 }
826 }
827 });
828 }
Sagar Vora78777d62021-03-09 20:35:08 +0530829
830 let query_args = {};
831 if (opts.get_query_filters) {
832 query_args.filters = opts.get_query_filters;
833 }
834
835 if (opts.get_query_method) {
836 query_args.query = opts.get_query_method;
837 }
838
839 if (query_args.filters || query_args.query) {
840 opts.get_query = () => query_args;
841 }
842
843 if (opts.source_doctype) {
844 const d = new frappe.ui.form.MultiSelectDialog({
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530845 doctype: opts.source_doctype,
846 target: opts.target,
847 date_field: opts.date_field || undefined,
848 setters: opts.setters,
849 get_query: opts.get_query,
marination4f395cc2020-09-24 12:43:41 +0530850 add_filters_group: 1,
Saqib7292f542021-09-13 12:13:43 +0530851 allow_child_item_selection: opts.allow_child_item_selection,
gn3060292a619fd2022-06-29 17:15:21 +0800852 child_fieldname: opts.child_fieldname,
Saqib7292f542021-09-13 12:13:43 +0530853 child_columns: opts.child_columns,
Rohit Waghchaure69ffddf2021-10-07 17:33:58 +0530854 size: opts.size,
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530855 action: function(selections, args) {
856 let values = selections;
Saqibe03d9aa2021-09-17 13:03:27 +0530857 if (values.length === 0) {
Nabin Haita11dcb62017-12-04 13:36:50 +0530858 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530859 return;
860 }
861 opts.source_name = values;
Saqibe03d9aa2021-09-17 13:03:27 +0530862 if (opts.allow_child_item_selection) {
863 // args contains filtered child docnames
864 opts.args = args;
865 }
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530866 d.dialog.hide();
867 _map();
868 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530869 });
Sagar Vora78777d62021-03-09 20:35:08 +0530870
871 return d;
872 }
873
874 if (opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530875 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530876 _map();
877 }
878}
879
Rushabh Mehta180e4352016-07-26 17:57:42 +0530880frappe.form.link_formatters['Item'] = function(value, doc) {
Alan157b3882021-04-21 21:00:22 +0530881 if (doc && value && doc.item_name && doc.item_name !== value && doc.item_code === value) {
marination824f48f2020-10-12 20:08:03 +0530882 return value + ': ' + doc.item_name;
883 } else if (!value && doc.doctype && doc.item_name) {
884 // format blank value in child table
885 return doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530886 } else {
marination824f48f2020-10-12 20:08:03 +0530887 // 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 +0530888 return value;
889 }
890}
891
892frappe.form.link_formatters['Employee'] = function(value, doc) {
Rucha Mahabal5ba1bc12021-11-23 09:30:30 +0530893 if (doc && value && doc.employee_name && doc.employee_name !== value && doc.employee === value) {
894 return value + ': ' + doc.employee_name;
895 } else if (!value && doc.doctype && doc.employee_name) {
896 // format blank value in child table
897 return doc.employee;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530898 } else {
Rucha Mahabal5ba1bc12021-11-23 09:30:30 +0530899 // 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 +0530900 return value;
901 }
902}
903
Rucha Mahabal59961f72021-05-20 23:43:19 +0530904frappe.form.link_formatters['Project'] = function(value, doc) {
905 if (doc && value && doc.project_name && doc.project_name !== value && doc.project === value) {
906 return value + ': ' + doc.project_name;
907 } else if (!value && doc.doctype && doc.project_name) {
908 // format blank value in child table
909 return doc.project;
910 } else {
911 // if value is blank in report view or project name and name are the same, return as is
912 return value;
913 }
914};
915
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530916// add description on posting time
917$(document).on('app_ready', function() {
918 if(!frappe.datetime.is_timezone_same()) {
919 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
920 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
921 frappe.ui.form.on(d, "onload", function(frm) {
922 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530923 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530924 });
925 });
926 }
akshay14384c22016-07-28 13:53:17 +0530927});
Alanc6dc9ea2021-05-07 20:30:04 +0530928
Himanshuec25d592021-06-14 19:05:52 +0530929// Show SLA dashboard
930$(document).on('app_ready', function() {
Ankush Menatbd9ef742023-07-16 11:34:42 +0530931 $.each(frappe.boot.service_level_agreement_doctypes, function(_i, d) {
932 frappe.ui.form.on(d, {
933 onload: function(frm) {
934 if (!frm.doc.service_level_agreement)
935 return;
Himanshuec25d592021-06-14 19:05:52 +0530936
Ankush Menatbd9ef742023-07-16 11:34:42 +0530937 frappe.call({
938 method: 'erpnext.support.doctype.service_level_agreement.service_level_agreement.get_service_level_agreement_filters',
939 args: {
940 doctype: frm.doc.doctype,
941 name: frm.doc.service_level_agreement,
942 customer: frm.doc.customer
Himanshuec25d592021-06-14 19:05:52 +0530943 },
Ankush Menatbd9ef742023-07-16 11:34:42 +0530944 callback: function (r) {
945 if (r && r.message) {
946 frm.set_query('priority', function() {
947 return {
948 filters: {
949 'name': ['in', r.message.priority],
Himanshuec25d592021-06-14 19:05:52 +0530950 }
Ankush Menatbd9ef742023-07-16 11:34:42 +0530951 };
Himanshuec25d592021-06-14 19:05:52 +0530952 });
Ankush Menatbd9ef742023-07-16 11:34:42 +0530953 frm.set_query('service_level_agreement', function() {
954 return {
955 filters: {
956 'name': ['in', r.message.service_level_agreements],
957 }
958 };
959 });
Himanshuec25d592021-06-14 19:05:52 +0530960 }
Ankush Menatbd9ef742023-07-16 11:34:42 +0530961 }
Himanshuec25d592021-06-14 19:05:52 +0530962 });
Ankush Menatbd9ef742023-07-16 11:34:42 +0530963 },
964
965 refresh: function(frm) {
966 if (frm.doc.status !== 'Closed' && frm.doc.service_level_agreement
967 && ['First Response Due', 'Resolution Due'].includes(frm.doc.agreement_status)) {
968 frappe.call({
969 'method': 'frappe.client.get',
970 args: {
971 doctype: 'Service Level Agreement',
972 name: frm.doc.service_level_agreement
973 },
974 callback: function(data) {
975 let statuses = data.message.pause_sla_on;
976 const hold_statuses = [];
977 $.each(statuses, (_i, entry) => {
978 hold_statuses.push(entry.status);
979 });
980 if (hold_statuses.includes(frm.doc.status)) {
981 frm.dashboard.clear_headline();
982 let message = {'indicator': 'orange', 'msg': __('SLA is on hold since {0}', [moment(frm.doc.on_hold_since).fromNow(true)])};
983 frm.dashboard.set_headline_alert(
984 '<div class="row">' +
985 '<div class="col-xs-12">' +
986 '<span class="indicator whitespace-nowrap '+ message.indicator +'"><span>'+ message.msg +'</span></span> ' +
987 '</div>' +
988 '</div>'
989 );
990 } else {
991 set_time_to_resolve_and_response(frm, data.message.apply_sla_for_resolution);
992 }
993 }
994 });
995 } else if (frm.doc.service_level_agreement) {
996 frm.dashboard.clear_headline();
997
998 let agreement_status = (frm.doc.agreement_status == 'Fulfilled') ?
999 {'indicator': 'green', 'msg': 'Service Level Agreement has been fulfilled'} :
1000 {'indicator': 'red', 'msg': 'Service Level Agreement Failed'};
1001
1002 frm.dashboard.set_headline_alert(
1003 '<div class="row">' +
1004 '<div class="col-xs-12">' +
1005 '<span class="indicator whitespace-nowrap '+ agreement_status.indicator +'"><span class="hidden-xs">'+ agreement_status.msg +'</span></span> ' +
1006 '</div>' +
1007 '</div>'
1008 );
1009 }
1010 },
1011 });
Himanshuec25d592021-06-14 19:05:52 +05301012 });
1013});
1014
1015function set_time_to_resolve_and_response(frm, apply_sla_for_resolution) {
1016 frm.dashboard.clear_headline();
1017
Saqib24114772021-12-15 11:45:34 +05301018 let time_to_respond;
Saqib Ansari476e81a2021-12-06 18:55:30 +05301019 if (!frm.doc.first_responded_on) {
Himanshuec25d592021-06-14 19:05:52 +05301020 time_to_respond = get_time_left(frm.doc.response_by, frm.doc.agreement_status);
Saqib24114772021-12-15 11:45:34 +05301021 } else {
1022 time_to_respond = get_status(frm.doc.response_by, frm.doc.first_responded_on);
Himanshuec25d592021-06-14 19:05:52 +05301023 }
1024
1025 let alert = `
1026 <div class="row">
1027 <div class="col-xs-12 col-sm-6">
1028 <span class="indicator whitespace-nowrap ${time_to_respond.indicator}">
1029 <span>Time to Respond: ${time_to_respond.diff_display}</span>
1030 </span>
1031 </div>`;
1032
1033
1034 if (apply_sla_for_resolution) {
Saqib24114772021-12-15 11:45:34 +05301035 let time_to_resolve;
Saqib Ansari476e81a2021-12-06 18:55:30 +05301036 if (!frm.doc.resolution_date) {
Himanshuec25d592021-06-14 19:05:52 +05301037 time_to_resolve = get_time_left(frm.doc.resolution_by, frm.doc.agreement_status);
Saqib24114772021-12-15 11:45:34 +05301038 } else {
1039 time_to_resolve = get_status(frm.doc.resolution_by, frm.doc.resolution_date);
Himanshuec25d592021-06-14 19:05:52 +05301040 }
1041
1042 alert += `
1043 <div class="col-xs-12 col-sm-6">
1044 <span class="indicator whitespace-nowrap ${time_to_resolve.indicator}">
1045 <span>Time to Resolve: ${time_to_resolve.diff_display}</span>
1046 </span>
1047 </div>`;
1048 }
1049
1050 alert += '</div>';
1051
1052 frm.dashboard.set_headline_alert(alert);
1053}
1054
1055function get_time_left(timestamp, agreement_status) {
1056 const diff = moment(timestamp).diff(moment());
1057 const diff_display = diff >= 44500 ? moment.duration(diff).humanize() : 'Failed';
1058 let indicator = (diff_display == 'Failed' && agreement_status != 'Fulfilled') ? 'red' : 'green';
1059 return {'diff_display': diff_display, 'indicator': indicator};
1060}
1061
Saqib24114772021-12-15 11:45:34 +05301062function get_status(expected, actual) {
1063 const time_left = moment(expected).diff(moment(actual));
Saqib Ansari476e81a2021-12-06 18:55:30 +05301064 if (time_left >= 0) {
Himanshuec25d592021-06-14 19:05:52 +05301065 return {'diff_display': 'Fulfilled', 'indicator': 'green'};
1066 } else {
1067 return {'diff_display': 'Failed', 'indicator': 'red'};
1068 }
1069}
1070
Alanc6dc9ea2021-05-07 20:30:04 +05301071function attach_selector_button(inner_text, append_loction, context, grid_row) {
1072 let $btn_div = $("<div>").css({"margin-bottom": "10px", "margin-top": "10px"})
1073 .appendTo(append_loction);
1074 let $btn = $(`<button class="btn btn-sm btn-default">${inner_text}</button>`)
1075 .appendTo($btn_div);
1076
1077 $btn.on("click", function() {
1078 context.show_serial_batch_selector(grid_row.frm, grid_row.doc, "", "", true);
1079 });
Rohit Waghchaure708eefb2023-07-10 16:54:55 +05301080}