blob: f456e5e500ce2199cdcc64b9636ba15d167752f4 [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
Deepesh Garg40772542023-07-22 23:07:18 +0530681 let dialog = new frappe.ui.Dialog({
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800682 title: __("Update Items"),
Raffael Meyerd5fe1432023-07-14 08:57:35 +0200683 size: "extra-large",
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800684 fields: [
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800685 {
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100686 fieldname: "trans_items",
687 fieldtype: "Table",
Maricafc96c1a2020-03-06 10:57:59 +0530688 label: "Items",
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100689 cannot_add_rows: cannot_add_row,
Saqibbc919e22020-11-05 17:38:35 +0530690 in_place_edit: false,
Maricafc96c1a2020-03-06 10:57:59 +0530691 reqd: 1,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100692 data: this.data,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800693 get_data: () => {
694 return this.data;
695 },
Saqib Ansarif53299e2020-04-15 22:08:12 +0530696 fields: fields
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800697 },
698 ],
699 primary_action: function() {
Ankush Menat6de7b8e2021-08-24 12:18:40 +0530700 const trans_items = this.get_values()["trans_items"].filter((item) => !!item.item_code);
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800701 frappe.call({
702 method: 'erpnext.controllers.accounts_controller.update_child_qty_rate',
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100703 freeze: true,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800704 args: {
705 'parent_doctype': frm.doc.doctype,
706 'trans_items': trans_items,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100707 'parent_doctype_name': frm.doc.name,
708 'child_docname': child_docname
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800709 },
710 callback: function() {
711 frm.reload_doc();
712 }
713 });
714 this.hide();
715 refresh_field("items");
716 },
717 primary_action_label: __('Update')
Deepesh Garg40772542023-07-22 23:07:18 +0530718 })
719
720 dialog.show();
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800721}
722
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530723erpnext.utils.map_current_doc = function(opts) {
Sagar Vora78777d62021-03-09 20:35:08 +0530724 function _map() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530725 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530726 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530727 if(!cur_frm.doc.items[0].item_code) {
728 cur_frm.doc.items = cur_frm.doc.items.splice(1);
729 }
bhupen3c7e70f2016-11-30 14:32:11 +0530730
731 // find the doctype of the items table
732 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530733
bhupen3c7e70f2016-11-30 14:32:11 +0530734 // find the link fieldname from items table for the given
735 // source_doctype
736 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530737 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530738 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
739
Nabin Hait802b4352017-01-09 15:32:20 +0530740 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530741 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530742 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530743
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530744 $.each(cur_frm.doc.items, function(i, d) {
745 opts.source_name.forEach(function(src) {
746 if(d[link_fieldname]==src) {
747 already_set = true;
748 if (item_qty_map[d.item_code])
749 item_qty_map[d.item_code] += flt(d.qty);
750 else
751 item_qty_map[d.item_code] = flt(d.qty);
752 }
753 });
754 });
755
756 if(already_set) {
757 opts.source_name.forEach(function(src) {
758 frappe.model.with_doc(opts.source_doctype, src, function(r) {
759 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
760 $.each(source_doc.items || [], function(i, row) {
761 if(row.qty > flt(item_qty_map[row.item_code])) {
762 already_set = false;
763 return false;
764 }
765 })
766 })
767
768 if(already_set) {
769 frappe.msgprint(__("You have already selected items from {0} {1}",
770 [opts.source_doctype, src]));
771 return;
772 }
773
774 })
Nabin Hait802b4352017-01-09 15:32:20 +0530775 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530776 }
777
778 return frappe.call({
779 // Sometimes we hit the limit for URL length of a GET request
780 // as we send the full target_doc. Hence this is a POST request.
781 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530782 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530783 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530784 "method": opts.method,
785 "source_names": opts.source_name,
786 "target_doc": cur_frm.doc,
Maricadb002702020-02-17 15:58:08 +0530787 "args": opts.args
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530788 },
789 callback: function(r) {
790 if(!r.exc) {
791 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530792 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530793 cur_frm.refresh();
794 }
795 }
796 });
797 }
Sagar Vora78777d62021-03-09 20:35:08 +0530798
799 let query_args = {};
800 if (opts.get_query_filters) {
801 query_args.filters = opts.get_query_filters;
802 }
803
804 if (opts.get_query_method) {
805 query_args.query = opts.get_query_method;
806 }
807
808 if (query_args.filters || query_args.query) {
809 opts.get_query = () => query_args;
810 }
811
812 if (opts.source_doctype) {
813 const d = new frappe.ui.form.MultiSelectDialog({
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530814 doctype: opts.source_doctype,
815 target: opts.target,
816 date_field: opts.date_field || undefined,
817 setters: opts.setters,
818 get_query: opts.get_query,
marination4f395cc2020-09-24 12:43:41 +0530819 add_filters_group: 1,
Saqib7292f542021-09-13 12:13:43 +0530820 allow_child_item_selection: opts.allow_child_item_selection,
gn3060292a619fd2022-06-29 17:15:21 +0800821 child_fieldname: opts.child_fieldname,
Saqib7292f542021-09-13 12:13:43 +0530822 child_columns: opts.child_columns,
Rohit Waghchaure69ffddf2021-10-07 17:33:58 +0530823 size: opts.size,
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530824 action: function(selections, args) {
825 let values = selections;
Saqibe03d9aa2021-09-17 13:03:27 +0530826 if (values.length === 0) {
Nabin Haita11dcb62017-12-04 13:36:50 +0530827 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530828 return;
829 }
830 opts.source_name = values;
Saqibe03d9aa2021-09-17 13:03:27 +0530831 if (opts.allow_child_item_selection) {
832 // args contains filtered child docnames
833 opts.args = args;
834 }
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530835 d.dialog.hide();
836 _map();
837 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530838 });
Sagar Vora78777d62021-03-09 20:35:08 +0530839
840 return d;
841 }
842
843 if (opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530844 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530845 _map();
846 }
847}
848
Rushabh Mehta180e4352016-07-26 17:57:42 +0530849frappe.form.link_formatters['Item'] = function(value, doc) {
Alan157b3882021-04-21 21:00:22 +0530850 if (doc && value && doc.item_name && doc.item_name !== value && doc.item_code === value) {
marination824f48f2020-10-12 20:08:03 +0530851 return value + ': ' + doc.item_name;
852 } else if (!value && doc.doctype && doc.item_name) {
853 // format blank value in child table
854 return doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530855 } else {
marination824f48f2020-10-12 20:08:03 +0530856 // 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 +0530857 return value;
858 }
859}
860
861frappe.form.link_formatters['Employee'] = function(value, doc) {
Rucha Mahabal5ba1bc12021-11-23 09:30:30 +0530862 if (doc && value && doc.employee_name && doc.employee_name !== value && doc.employee === value) {
863 return value + ': ' + doc.employee_name;
864 } else if (!value && doc.doctype && doc.employee_name) {
865 // format blank value in child table
866 return doc.employee;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530867 } else {
Rucha Mahabal5ba1bc12021-11-23 09:30:30 +0530868 // 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 +0530869 return value;
870 }
871}
872
Rucha Mahabal59961f72021-05-20 23:43:19 +0530873frappe.form.link_formatters['Project'] = function(value, doc) {
874 if (doc && value && doc.project_name && doc.project_name !== value && doc.project === value) {
875 return value + ': ' + doc.project_name;
876 } else if (!value && doc.doctype && doc.project_name) {
877 // format blank value in child table
878 return doc.project;
879 } else {
880 // if value is blank in report view or project name and name are the same, return as is
881 return value;
882 }
883};
884
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530885// add description on posting time
886$(document).on('app_ready', function() {
887 if(!frappe.datetime.is_timezone_same()) {
888 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
889 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
890 frappe.ui.form.on(d, "onload", function(frm) {
891 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530892 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530893 });
894 });
895 }
akshay14384c22016-07-28 13:53:17 +0530896});
Alanc6dc9ea2021-05-07 20:30:04 +0530897
Himanshuec25d592021-06-14 19:05:52 +0530898// Show SLA dashboard
899$(document).on('app_ready', function() {
Ankush Menatbd9ef742023-07-16 11:34:42 +0530900 $.each(frappe.boot.service_level_agreement_doctypes, function(_i, d) {
901 frappe.ui.form.on(d, {
902 onload: function(frm) {
903 if (!frm.doc.service_level_agreement)
904 return;
Himanshuec25d592021-06-14 19:05:52 +0530905
Ankush Menatbd9ef742023-07-16 11:34:42 +0530906 frappe.call({
907 method: 'erpnext.support.doctype.service_level_agreement.service_level_agreement.get_service_level_agreement_filters',
908 args: {
909 doctype: frm.doc.doctype,
910 name: frm.doc.service_level_agreement,
911 customer: frm.doc.customer
Himanshuec25d592021-06-14 19:05:52 +0530912 },
Ankush Menatbd9ef742023-07-16 11:34:42 +0530913 callback: function (r) {
914 if (r && r.message) {
915 frm.set_query('priority', function() {
916 return {
917 filters: {
918 'name': ['in', r.message.priority],
Himanshuec25d592021-06-14 19:05:52 +0530919 }
Ankush Menatbd9ef742023-07-16 11:34:42 +0530920 };
Himanshuec25d592021-06-14 19:05:52 +0530921 });
Ankush Menatbd9ef742023-07-16 11:34:42 +0530922 frm.set_query('service_level_agreement', function() {
923 return {
924 filters: {
925 'name': ['in', r.message.service_level_agreements],
926 }
927 };
928 });
Himanshuec25d592021-06-14 19:05:52 +0530929 }
Ankush Menatbd9ef742023-07-16 11:34:42 +0530930 }
Himanshuec25d592021-06-14 19:05:52 +0530931 });
Ankush Menatbd9ef742023-07-16 11:34:42 +0530932 },
933
934 refresh: function(frm) {
935 if (frm.doc.status !== 'Closed' && frm.doc.service_level_agreement
936 && ['First Response Due', 'Resolution Due'].includes(frm.doc.agreement_status)) {
937 frappe.call({
938 'method': 'frappe.client.get',
939 args: {
940 doctype: 'Service Level Agreement',
941 name: frm.doc.service_level_agreement
942 },
943 callback: function(data) {
944 let statuses = data.message.pause_sla_on;
945 const hold_statuses = [];
946 $.each(statuses, (_i, entry) => {
947 hold_statuses.push(entry.status);
948 });
949 if (hold_statuses.includes(frm.doc.status)) {
950 frm.dashboard.clear_headline();
951 let message = {'indicator': 'orange', 'msg': __('SLA is on hold since {0}', [moment(frm.doc.on_hold_since).fromNow(true)])};
952 frm.dashboard.set_headline_alert(
953 '<div class="row">' +
954 '<div class="col-xs-12">' +
955 '<span class="indicator whitespace-nowrap '+ message.indicator +'"><span>'+ message.msg +'</span></span> ' +
956 '</div>' +
957 '</div>'
958 );
959 } else {
960 set_time_to_resolve_and_response(frm, data.message.apply_sla_for_resolution);
961 }
962 }
963 });
964 } else if (frm.doc.service_level_agreement) {
965 frm.dashboard.clear_headline();
966
967 let agreement_status = (frm.doc.agreement_status == 'Fulfilled') ?
968 {'indicator': 'green', 'msg': 'Service Level Agreement has been fulfilled'} :
969 {'indicator': 'red', 'msg': 'Service Level Agreement Failed'};
970
971 frm.dashboard.set_headline_alert(
972 '<div class="row">' +
973 '<div class="col-xs-12">' +
974 '<span class="indicator whitespace-nowrap '+ agreement_status.indicator +'"><span class="hidden-xs">'+ agreement_status.msg +'</span></span> ' +
975 '</div>' +
976 '</div>'
977 );
978 }
979 },
980 });
Himanshuec25d592021-06-14 19:05:52 +0530981 });
982});
983
984function set_time_to_resolve_and_response(frm, apply_sla_for_resolution) {
985 frm.dashboard.clear_headline();
986
Saqib24114772021-12-15 11:45:34 +0530987 let time_to_respond;
Saqib Ansari476e81a2021-12-06 18:55:30 +0530988 if (!frm.doc.first_responded_on) {
Himanshuec25d592021-06-14 19:05:52 +0530989 time_to_respond = get_time_left(frm.doc.response_by, frm.doc.agreement_status);
Saqib24114772021-12-15 11:45:34 +0530990 } else {
991 time_to_respond = get_status(frm.doc.response_by, frm.doc.first_responded_on);
Himanshuec25d592021-06-14 19:05:52 +0530992 }
993
994 let alert = `
995 <div class="row">
996 <div class="col-xs-12 col-sm-6">
997 <span class="indicator whitespace-nowrap ${time_to_respond.indicator}">
998 <span>Time to Respond: ${time_to_respond.diff_display}</span>
999 </span>
1000 </div>`;
1001
1002
1003 if (apply_sla_for_resolution) {
Saqib24114772021-12-15 11:45:34 +05301004 let time_to_resolve;
Saqib Ansari476e81a2021-12-06 18:55:30 +05301005 if (!frm.doc.resolution_date) {
Himanshuec25d592021-06-14 19:05:52 +05301006 time_to_resolve = get_time_left(frm.doc.resolution_by, frm.doc.agreement_status);
Saqib24114772021-12-15 11:45:34 +05301007 } else {
1008 time_to_resolve = get_status(frm.doc.resolution_by, frm.doc.resolution_date);
Himanshuec25d592021-06-14 19:05:52 +05301009 }
1010
1011 alert += `
1012 <div class="col-xs-12 col-sm-6">
1013 <span class="indicator whitespace-nowrap ${time_to_resolve.indicator}">
1014 <span>Time to Resolve: ${time_to_resolve.diff_display}</span>
1015 </span>
1016 </div>`;
1017 }
1018
1019 alert += '</div>';
1020
1021 frm.dashboard.set_headline_alert(alert);
1022}
1023
1024function get_time_left(timestamp, agreement_status) {
1025 const diff = moment(timestamp).diff(moment());
1026 const diff_display = diff >= 44500 ? moment.duration(diff).humanize() : 'Failed';
1027 let indicator = (diff_display == 'Failed' && agreement_status != 'Fulfilled') ? 'red' : 'green';
1028 return {'diff_display': diff_display, 'indicator': indicator};
1029}
1030
Saqib24114772021-12-15 11:45:34 +05301031function get_status(expected, actual) {
1032 const time_left = moment(expected).diff(moment(actual));
Saqib Ansari476e81a2021-12-06 18:55:30 +05301033 if (time_left >= 0) {
Himanshuec25d592021-06-14 19:05:52 +05301034 return {'diff_display': 'Fulfilled', 'indicator': 'green'};
1035 } else {
1036 return {'diff_display': 'Failed', 'indicator': 'red'};
1037 }
1038}
1039
Alanc6dc9ea2021-05-07 20:30:04 +05301040function attach_selector_button(inner_text, append_loction, context, grid_row) {
1041 let $btn_div = $("<div>").css({"margin-bottom": "10px", "margin-top": "10px"})
1042 .appendTo(append_loction);
1043 let $btn = $(`<button class="btn btn-sm btn-default">${inner_text}</button>`)
1044 .appendTo($btn_div);
1045
1046 $btn.on("click", function() {
1047 context.show_serial_batch_selector(grid_row.frm, grid_row.doc, "", "", true);
1048 });
Rohit Waghchaure708eefb2023-07-10 16:54:55 +05301049}