blob: 8d6097d0a2021cce4dfc4ff393a83887eef6d366 [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
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530116 add_indicator_for_multicompany: function(frm, info) {
Deepak867f2c62022-05-17 11:52:52 +0530117 frm.dashboard.stats_area.show();
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530118 frm.dashboard.stats_area_row.addClass('flex');
119 frm.dashboard.stats_area_row.css('flex-wrap', 'wrap');
120
121 var color = info.total_unpaid ? 'orange' : 'green';
122
123 var indicator = $('<div class="flex-column col-xs-6">'+
124 '<div style="margin-top:10px"><h6>'+info.company+'</h6></div>'+
125
126 '<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
127 'Annual Billing: '+format_currency(info.billing_this_year, info.currency)+'</span></div>'+
128
129 '<div class="badge-link small" style="margin-bottom:10px">'+
130 '<span class="indicator '+color+'">Total Unpaid: '
131 +format_currency(info.total_unpaid, info.currency)+'</span></div>'+
132
133
134 '</div>').appendTo(frm.dashboard.stats_area_row);
135
136 if(info.loyalty_points){
137 $('<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
138 'Loyalty Points: '+info.loyalty_points+'</span></div>').appendTo(indicator);
139 }
140
141 return indicator;
142 },
143
Shreya Shah149f7ee2018-03-27 11:29:25 +0530144 get_party_name: function(party_type) {
145 var dict = {'Customer': 'customer_name', 'Supplier': 'supplier_name', 'Employee': 'employee_name',
146 'Member': 'member_name'};
147 return dict[party_type];
148 },
149
Rushabh Mehta49f97472018-08-30 18:50:48 +0530150 copy_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname) {
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +0530151 var d = locals[dt][dn];
152 if(d[fieldname]){
153 var cl = doc[table_fieldname] || [];
154 for(var i = 0; i < cl.length; i++) {
155 if(!cl[i][fieldname]) cl[i][fieldname] = d[fieldname];
156 }
157 }
158 refresh_field(table_fieldname);
Makarand Bauskar157c3342017-05-26 21:32:33 +0530159 },
160
161 get_terms: function(tc_name, doc, callback) {
162 if(tc_name) {
163 return frappe.call({
164 method: 'erpnext.setup.doctype.terms_and_conditions.terms_and_conditions.get_terms_and_conditions',
165 args: {
166 template_name: tc_name,
167 doc: doc
168 },
169 callback: function(r) {
170 callback(r)
171 }
172 });
173 }
tundebabzy1a947db2017-08-29 13:48:27 +0100174 },
175
rohitwaghchaure713cfc72018-09-11 17:40:37 +0530176 make_bank_account: function(doctype, docname) {
177 frappe.call({
178 method: "erpnext.accounts.doctype.bank_account.bank_account.make_bank_account",
179 args: {
180 doctype: doctype,
181 docname: docname
182 },
183 freeze: true,
184 callback: function(r) {
185 var doclist = frappe.model.sync(r.message);
186 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
187 }
188 })
189 },
190
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530191 add_dimensions: function(report_name, index) {
192 let filters = frappe.query_reports[report_name].filters;
193
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530194 frappe.call({
195 method: "erpnext.accounts.doctype.accounting_dimension.accounting_dimension.get_dimensions",
196 callback: function(r) {
197 let accounting_dimensions = r.message[0];
198 accounting_dimensions.forEach((dimension) => {
199 let found = filters.some(el => el.fieldname === dimension['fieldname']);
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530200
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530201 if (!found) {
Deepesh Garg23ab5c52020-12-31 11:29:06 +0530202 filters.splice(index, 0, {
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530203 "fieldname": dimension["fieldname"],
204 "label": __(dimension["label"]),
Rohit Waghchaure69be22b2022-05-14 17:19:34 +0530205 "fieldtype": "MultiSelectList",
206 get_data: function(txt) {
207 return frappe.db.get_link_options(dimension["document_type"], txt);
208 },
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530209 });
210 }
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530211 });
212 }
213 });
214 },
215
Rohit Waghchauredbec5cf2022-06-22 12:24:08 +0530216 add_inventory_dimensions: function(report_name, index) {
217 let filters = frappe.query_reports[report_name].filters;
218
219 frappe.call({
220 method: "erpnext.stock.doctype.inventory_dimension.inventory_dimension.get_inventory_dimensions",
221 callback: function(r) {
222 if (r.message && r.message.length) {
223 r.message.forEach((dimension) => {
Rohit Waghchaure0e388ba2023-02-20 12:20:03 +0530224 let existing_filter = filters.filter(el => el.fieldname === dimension['fieldname']);
Rohit Waghchauredbec5cf2022-06-22 12:24:08 +0530225
Rohit Waghchaure0e388ba2023-02-20 12:20:03 +0530226 if (!existing_filter.length) {
Rohit Waghchauredbec5cf2022-06-22 12:24:08 +0530227 filters.splice(index, 0, {
228 "fieldname": dimension["fieldname"],
Rohit Waghchaureef7def82022-09-07 14:40:18 +0530229 "label": __(dimension["doctype"]),
Rohit Waghchauredbec5cf2022-06-22 12:24:08 +0530230 "fieldtype": "MultiSelectList",
231 get_data: function(txt) {
232 return frappe.db.get_link_options(dimension["doctype"], txt);
233 },
234 });
Rohit Waghchaure0e388ba2023-02-20 12:20:03 +0530235 } else {
236 existing_filter[0]['fieldtype'] = "MultiSelectList";
237 existing_filter[0]['get_data'] = function(txt) {
238 return frappe.db.get_link_options(dimension["doctype"], txt);
239 }
Rohit Waghchauredbec5cf2022-06-22 12:24:08 +0530240 }
241 });
242 }
243 }
244 });
245 },
246
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530247 make_subscription: function(doctype, docname) {
248 frappe.call({
Rucha Mahabal65a627c2019-07-17 13:50:32 +0530249 method: "frappe.automation.doctype.auto_repeat.auto_repeat.make_auto_repeat",
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530250 args: {
251 doctype: doctype,
252 docname: docname
253 },
254 callback: function(r) {
255 var doclist = frappe.model.sync(r.message);
256 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
257 }
258 })
259 },
260
Saif90cf2dd2018-09-30 21:46:31 +0500261 make_pricing_rule: function(doctype, docname) {
262 frappe.call({
263 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.make_pricing_rule",
264 args: {
265 doctype: doctype,
266 docname: docname
267 },
268 callback: function(r) {
269 var doclist = frappe.model.sync(r.message);
270 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
271 }
272 })
273 },
274
tundebabzy1a947db2017-08-29 13:48:27 +0100275 /**
276 * Checks if the first row of a given child table is empty
277 * @param child_table - Child table Doctype
278 * @return {Boolean}
279 **/
280 first_row_is_empty: function(child_table){
281 if($.isArray(child_table) && child_table.length > 0) {
282 return !child_table[0].item_code;
283 }
284 return false;
285 },
286
287 /**
288 * Removes the first row of a child table if it is empty
289 * @param {_Frm} frm - The current form
290 * @param {String} child_table_name - The child table field name
291 * @return {Boolean}
292 **/
293 remove_empty_first_row: function(frm, child_table_name){
294 const rows = frm['doc'][child_table_name];
295 if (this.first_row_is_empty(rows)){
296 frm['doc'][child_table_name] = rows.splice(1);
297 }
298 return rows;
299 },
Zarrar6ffdf942018-06-14 12:24:16 +0530300 get_tree_options: function(option) {
301 // get valid options for tree based on user permission & locals dict
302 let unscrub_option = frappe.model.unscrub(option);
303 let user_permission = frappe.defaults.get_user_permissions();
deepeshgarg0070d64d622019-02-20 17:55:14 +0530304 let options;
305
Zarrar6ffdf942018-06-14 12:24:16 +0530306 if(user_permission && user_permission[unscrub_option]) {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530307 options = user_permission[unscrub_option].map(perm => perm.doc);
Zarrar6ffdf942018-06-14 12:24:16 +0530308 } else {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530309 options = $.map(locals[`:${unscrub_option}`], function(c) { return c.name; }).sort();
Zarrar6ffdf942018-06-14 12:24:16 +0530310 }
deepeshgarg0070d64d622019-02-20 17:55:14 +0530311
312 // filter unique values, as there may be multiple user permissions for any value
313 return options.filter((value, index, self) => self.indexOf(value) === index);
Zarrar6ffdf942018-06-14 12:24:16 +0530314 },
315 get_tree_default: function(option) {
316 // set default for a field based on user permission
317 let options = this.get_tree_options(option);
318 if(options.includes(frappe.defaults.get_default(option))) {
319 return frappe.defaults.get_default(option);
320 } else {
321 return options[0];
322 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400323 },
Andy Zhubffe9332021-04-12 22:49:26 +1200324 overrides_parent_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname, parent_fieldname) {
Marica24e45162021-04-14 18:53:15 +0530325 if (doc[parent_fieldname]) {
Andy Zhubffe9332021-04-12 22:49:26 +1200326 let cl = doc[table_fieldname] || [];
Marica24e45162021-04-14 18:53:15 +0530327 for (let i = 0; i < cl.length; i++) {
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400328 cl[i][fieldname] = doc[parent_fieldname];
329 }
Andy Zhubffe9332021-04-12 22:49:26 +1200330 frappe.refresh_field(table_fieldname);
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400331 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400332 },
Nabin Hait34c551d2019-07-03 10:34:31 +0530333 create_new_doc: function (doctype, update_fields) {
334 frappe.model.with_doctype(doctype, function() {
335 var new_doc = frappe.model.get_new_doc(doctype);
336 for (let [key, value] of Object.entries(update_fields)) {
337 new_doc[key] = value;
338 }
339 frappe.ui.form.make_quick_entry(doctype, null, null, new_doc);
340 });
Ankush Menat0b86b1b2022-12-08 16:40:13 +0530341 },
Nabin Hait34c551d2019-07-03 10:34:31 +0530342
Ankush Menat0b86b1b2022-12-08 16:40:13 +0530343 // check if payments app is installed on site, if not warn user.
344 check_payments_app: () => {
345 if (frappe.boot.versions && !frappe.boot.versions.payments) {
346 const marketplace_link = '<a href="https://frappecloud.com/marketplace/apps/payments">Marketplace</a>'
347 const github_link = '<a href="https://github.com/frappe/payments/">GitHub</a>'
348 const msg = __("payments app is not installed. Please install it from {0} or {1}", [marketplace_link, github_link])
349 frappe.msgprint(msg);
350 }
351
352 },
Rohit Waghchaure1c2fe082023-06-15 12:54:43 +0530353
354 pick_serial_and_batch_bundle(frm, cdt, cdn, type_of_transaction, warehouse_field) {
355 let item_row = frappe.get_doc(cdt, cdn);
356 item_row.type_of_transaction = type_of_transaction;
357
358 frappe.db.get_value("Item", item_row.item_code, ["has_batch_no", "has_serial_no"])
359 .then((r) => {
360 item_row.has_batch_no = r.message.has_batch_no;
361 item_row.has_serial_no = r.message.has_serial_no;
362
363 frappe.require("assets/erpnext/js/utils/serial_no_batch_selector.js", function() {
364 new erpnext.SerialBatchPackageSelector(frm, item_row, (r) => {
365 if (r) {
366 let update_values = {
367 "serial_and_batch_bundle": r.name,
368 "qty": Math.abs(r.total_qty)
369 }
370
371 if (!warehouse_field) {
372 warehouse_field = "warehouse";
373 }
374
375 if (r.warehouse) {
376 update_values[warehouse_field] = r.warehouse;
377 }
378
379 frappe.model.set_value(item_row.doctype, item_row.name, update_values);
380 }
381 });
382 });
383 });
Deepesh Garg62706072023-07-16 12:58:42 +0530384 },
385
386 get_fiscal_year: function(date) {
387 let fiscal_year = '';
388 frappe.call({
389 method: "erpnext.accounts.utils.get_fiscal_year",
390 args: {
391 date: date
392 },
393 async: false,
394 callback: function(r) {
395 if (r.message) {
396 fiscal_year = r.message[0];
397 }
398 }
399 });
400 return fiscal_year;
Rohit Waghchaure1c2fe082023-06-15 12:54:43 +0530401 }
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530402});
403
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530404erpnext.utils.select_alternate_items = function(opts) {
405 const frm = opts.frm;
406 const warehouse_field = opts.warehouse_field || 'warehouse';
407 const item_field = opts.item_field || 'item_code';
408
409 this.data = [];
410 const dialog = new frappe.ui.Dialog({
411 title: __("Select Alternate Item"),
412 fields: [
413 {fieldtype:'Section Break', label: __('Items')},
414 {
415 fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true,
416 in_place_edit: true, data: this.data,
417 get_data: () => {
418 return this.data;
419 },
420 fields: [{
421 fieldtype:'Data',
422 fieldname:"docname",
423 hidden: 1
424 }, {
425 fieldtype:'Link',
426 fieldname:"item_code",
427 options: 'Item',
428 in_list_view: 1,
429 read_only: 1,
430 label: __('Item Code')
431 }, {
432 fieldtype:'Link',
433 fieldname:"alternate_item",
434 options: 'Item',
435 default: "",
436 in_list_view: 1,
437 label: __('Alternate Item'),
438 onchange: function() {
439 const item_code = this.get_value();
440 const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value();
441 if (item_code && warehouse) {
442 frappe.call({
443 method: "erpnext.stock.utils.get_latest_stock_qty",
444 args: {
445 item_code: item_code,
446 warehouse: warehouse
447 },
448 callback: (r) => {
449 this.grid_row.on_grid_fields_dict
450 .actual_qty.set_value(r.message || 0);
451 }
452 })
453 }
454 },
455 get_query: (e) => {
456 return {
457 query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items",
458 filters: {
459 item_code: e.item_code
460 }
461 };
462 }
463 }, {
464 fieldtype:'Link',
465 fieldname:"warehouse",
466 options: 'Warehouse',
467 default: "",
468 in_list_view: 1,
469 label: __('Warehouse'),
470 onchange: function() {
471 const warehouse = this.get_value();
472 const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value();
473 if (item_code && warehouse) {
474 frappe.call({
475 method: "erpnext.stock.utils.get_latest_stock_qty",
476 args: {
477 item_code: item_code,
478 warehouse: warehouse
479 },
480 callback: (r) => {
481 this.grid_row.on_grid_fields_dict
482 .actual_qty.set_value(r.message || 0);
483 }
484 })
485 }
486 },
487 }, {
488 fieldtype:'Float',
489 fieldname:"actual_qty",
490 default: 0,
491 read_only: 1,
492 in_list_view: 1,
493 label: __('Available Qty')
494 }]
495 },
496 ],
497 primary_action: function() {
498 const args = this.get_values()["alternative_items"];
499 const alternative_items = args.filter(d => {
500 if (d.alternate_item && d.item_code != d.alternate_item) {
501 return true;
502 }
503 });
504
505 alternative_items.forEach(d => {
506 let row = frappe.get_doc(opts.child_doctype, d.docname);
Doridel Cahanape42192b2018-05-16 13:28:39 +0800507 let qty = null;
508 if (row.doctype === 'Work Order Item') {
509 qty = row.required_qty;
510 } else {
511 qty = row.qty;
512 }
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530513 row[item_field] = d.alternate_item;
mergify[bot]c1a0f642021-12-17 19:06:19 +0530514 frappe.model.set_value(row.doctype, row.name, 'qty', qty);
515 frappe.model.set_value(row.doctype, row.name, opts.original_item_field, d.item_code);
516 frm.trigger(item_field, row.doctype, row.name);
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530517 });
518
519 refresh_field(opts.child_docname);
520 this.hide();
521 },
522 primary_action_label: __('Update')
523 });
524
525 frm.doc[opts.child_docname].forEach(d => {
526 if (!opts.condition || opts.condition(d)) {
527 dialog.fields_dict.alternative_items.df.data.push({
528 "docname": d.name,
529 "item_code": d[item_field],
530 "warehouse": d[warehouse_field],
531 "actual_qty": d.actual_qty
532 });
533 }
534 })
535
536 this.data = dialog.fields_dict.alternative_items.df.data;
537 dialog.fields_dict.alternative_items.grid.refresh();
538 dialog.show();
539}
540
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800541erpnext.utils.update_child_items = function(opts) {
542 const frm = opts.frm;
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100543 const cannot_add_row = (typeof opts.cannot_add_row === 'undefined') ? true : opts.cannot_add_row;
544 const child_docname = (typeof opts.cannot_add_row === 'undefined') ? "items" : opts.child_docname;
Saqib56fea7d2020-10-09 21:19:25 +0530545 const child_meta = frappe.get_meta(`${frm.doc.doctype} Item`);
546 const get_precision = (fieldname) => child_meta.fields.find(f => f.fieldname == fieldname).precision;
547
Devin Slauenwhitea835c1a2023-01-26 16:53:05 -0500548 this.data = frm.doc[opts.child_docname].map((d) => {
549 return {
550 "docname": d.name,
551 "name": d.name,
552 "item_code": d.item_code,
553 "delivery_date": d.delivery_date,
554 "schedule_date": d.schedule_date,
555 "conversion_factor": d.conversion_factor,
556 "qty": d.qty,
557 "rate": d.rate,
558 "uom": d.uom
559 }
560 });
561
Saqib Ansarif53299e2020-04-15 22:08:12 +0530562 const fields = [{
563 fieldtype:'Data',
564 fieldname:"docname",
565 read_only: 1,
566 hidden: 1,
567 }, {
568 fieldtype:'Link',
569 fieldname:"item_code",
570 options: 'Item',
571 in_list_view: 1,
572 read_only: 0,
573 disabled: 0,
Afshandc7280e2021-08-18 17:44:40 +0530574 label: __('Item Code'),
575 get_query: function() {
576 let filters;
577 if (frm.doc.doctype == 'Sales Order') {
578 filters = {"is_sales_item": 1};
579 } else if (frm.doc.doctype == 'Purchase Order') {
Sagar Sharmad074c932022-03-31 19:57:42 +0530580 if (frm.doc.is_subcontracted) {
s-aga-r6d89b2f2022-06-18 15:46:59 +0530581 if (frm.doc.is_old_subcontracting_flow) {
582 filters = {"is_sub_contracted_item": 1};
583 } else {
584 filters = {"is_stock_item": 0};
585 }
Afshandc7280e2021-08-18 17:44:40 +0530586 } else {
587 filters = {"is_purchase_item": 1};
588 }
589 }
590 return {
591 query: "erpnext.controllers.queries.item_query",
592 filters: filters
593 };
594 }
Saqib Ansarif53299e2020-04-15 22:08:12 +0530595 }, {
Saqib61314242020-09-15 11:14:31 +0530596 fieldtype:'Link',
597 fieldname:'uom',
598 options: 'UOM',
599 read_only: 0,
600 label: __('UOM'),
601 reqd: 1,
602 onchange: function () {
603 frappe.call({
604 method: "erpnext.stock.get_item_details.get_conversion_factor",
605 args: { item_code: this.doc.item_code, uom: this.value },
606 callback: r => {
607 if(!r.exc) {
608 if (this.doc.conversion_factor == r.message.conversion_factor) return;
marination4f395cc2020-09-24 12:43:41 +0530609
Saqib61314242020-09-15 11:14:31 +0530610 const docname = this.doc.docname;
611 dialog.fields_dict.trans_items.df.data.some(doc => {
612 if (doc.docname == docname) {
613 doc.conversion_factor = r.message.conversion_factor;
614 dialog.fields_dict.trans_items.grid.refresh();
615 return true;
616 }
617 })
618 }
619 }
620 });
621 }
622 }, {
Saqib Ansarif53299e2020-04-15 22:08:12 +0530623 fieldtype:'Float',
624 fieldname:"qty",
625 default: 0,
626 read_only: 0,
627 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530628 label: __('Qty'),
629 precision: get_precision("qty")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530630 }, {
631 fieldtype:'Currency',
632 fieldname:"rate",
Afshan84184552021-02-24 16:51:11 +0530633 options: "currency",
Saqib Ansarif53299e2020-04-15 22:08:12 +0530634 default: 0,
635 read_only: 0,
636 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530637 label: __('Rate'),
638 precision: get_precision("rate")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530639 }];
640
641 if (frm.doc.doctype == 'Sales Order' || frm.doc.doctype == 'Purchase Order' ) {
642 fields.splice(2, 0, {
643 fieldtype: 'Date',
644 fieldname: frm.doc.doctype == 'Sales Order' ? "delivery_date" : "schedule_date",
645 in_list_view: 1,
Saqib Ansaric579b082020-05-29 22:21:50 +0530646 label: frm.doc.doctype == 'Sales Order' ? __("Delivery Date") : __("Reqd by date"),
647 reqd: 1
Saqib Ansarif53299e2020-04-15 22:08:12 +0530648 })
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530649 fields.splice(3, 0, {
650 fieldtype: 'Float',
651 fieldname: "conversion_factor",
Saqib56fea7d2020-10-09 21:19:25 +0530652 label: __("Conversion Factor"),
653 precision: get_precision('conversion_factor')
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530654 })
Saqib Ansarif53299e2020-04-15 22:08:12 +0530655 }
656
Devin Slauenwhitea835c1a2023-01-26 16:53:05 -0500657 new frappe.ui.Dialog({
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800658 title: __("Update Items"),
Raffael Meyerd5fe1432023-07-14 08:57:35 +0200659 size: "extra-large",
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800660 fields: [
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800661 {
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100662 fieldname: "trans_items",
663 fieldtype: "Table",
Maricafc96c1a2020-03-06 10:57:59 +0530664 label: "Items",
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100665 cannot_add_rows: cannot_add_row,
Saqibbc919e22020-11-05 17:38:35 +0530666 in_place_edit: false,
Maricafc96c1a2020-03-06 10:57:59 +0530667 reqd: 1,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100668 data: this.data,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800669 get_data: () => {
670 return this.data;
671 },
Saqib Ansarif53299e2020-04-15 22:08:12 +0530672 fields: fields
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800673 },
674 ],
675 primary_action: function() {
Ankush Menat6de7b8e2021-08-24 12:18:40 +0530676 const trans_items = this.get_values()["trans_items"].filter((item) => !!item.item_code);
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800677 frappe.call({
678 method: 'erpnext.controllers.accounts_controller.update_child_qty_rate',
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100679 freeze: true,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800680 args: {
681 'parent_doctype': frm.doc.doctype,
682 'trans_items': trans_items,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100683 'parent_doctype_name': frm.doc.name,
684 'child_docname': child_docname
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800685 },
686 callback: function() {
687 frm.reload_doc();
688 }
689 });
690 this.hide();
691 refresh_field("items");
692 },
693 primary_action_label: __('Update')
Devin Slauenwhitea835c1a2023-01-26 16:53:05 -0500694 }).show();
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800695}
696
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530697erpnext.utils.map_current_doc = function(opts) {
Sagar Vora78777d62021-03-09 20:35:08 +0530698 function _map() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530699 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530700 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530701 if(!cur_frm.doc.items[0].item_code) {
702 cur_frm.doc.items = cur_frm.doc.items.splice(1);
703 }
bhupen3c7e70f2016-11-30 14:32:11 +0530704
705 // find the doctype of the items table
706 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530707
bhupen3c7e70f2016-11-30 14:32:11 +0530708 // find the link fieldname from items table for the given
709 // source_doctype
710 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530711 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530712 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
713
Nabin Hait802b4352017-01-09 15:32:20 +0530714 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530715 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530716 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530717
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530718 $.each(cur_frm.doc.items, function(i, d) {
719 opts.source_name.forEach(function(src) {
720 if(d[link_fieldname]==src) {
721 already_set = true;
722 if (item_qty_map[d.item_code])
723 item_qty_map[d.item_code] += flt(d.qty);
724 else
725 item_qty_map[d.item_code] = flt(d.qty);
726 }
727 });
728 });
729
730 if(already_set) {
731 opts.source_name.forEach(function(src) {
732 frappe.model.with_doc(opts.source_doctype, src, function(r) {
733 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
734 $.each(source_doc.items || [], function(i, row) {
735 if(row.qty > flt(item_qty_map[row.item_code])) {
736 already_set = false;
737 return false;
738 }
739 })
740 })
741
742 if(already_set) {
743 frappe.msgprint(__("You have already selected items from {0} {1}",
744 [opts.source_doctype, src]));
745 return;
746 }
747
748 })
Nabin Hait802b4352017-01-09 15:32:20 +0530749 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530750 }
751
752 return frappe.call({
753 // Sometimes we hit the limit for URL length of a GET request
754 // as we send the full target_doc. Hence this is a POST request.
755 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530756 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530757 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530758 "method": opts.method,
759 "source_names": opts.source_name,
760 "target_doc": cur_frm.doc,
Maricadb002702020-02-17 15:58:08 +0530761 "args": opts.args
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530762 },
763 callback: function(r) {
764 if(!r.exc) {
765 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530766 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530767 cur_frm.refresh();
768 }
769 }
770 });
771 }
Sagar Vora78777d62021-03-09 20:35:08 +0530772
773 let query_args = {};
774 if (opts.get_query_filters) {
775 query_args.filters = opts.get_query_filters;
776 }
777
778 if (opts.get_query_method) {
779 query_args.query = opts.get_query_method;
780 }
781
782 if (query_args.filters || query_args.query) {
783 opts.get_query = () => query_args;
784 }
785
786 if (opts.source_doctype) {
787 const d = new frappe.ui.form.MultiSelectDialog({
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530788 doctype: opts.source_doctype,
789 target: opts.target,
790 date_field: opts.date_field || undefined,
791 setters: opts.setters,
792 get_query: opts.get_query,
marination4f395cc2020-09-24 12:43:41 +0530793 add_filters_group: 1,
Saqib7292f542021-09-13 12:13:43 +0530794 allow_child_item_selection: opts.allow_child_item_selection,
gn3060292a619fd2022-06-29 17:15:21 +0800795 child_fieldname: opts.child_fieldname,
Saqib7292f542021-09-13 12:13:43 +0530796 child_columns: opts.child_columns,
Rohit Waghchaure69ffddf2021-10-07 17:33:58 +0530797 size: opts.size,
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530798 action: function(selections, args) {
799 let values = selections;
Saqibe03d9aa2021-09-17 13:03:27 +0530800 if (values.length === 0) {
Nabin Haita11dcb62017-12-04 13:36:50 +0530801 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530802 return;
803 }
804 opts.source_name = values;
Saqibe03d9aa2021-09-17 13:03:27 +0530805 if (opts.allow_child_item_selection) {
806 // args contains filtered child docnames
807 opts.args = args;
808 }
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530809 d.dialog.hide();
810 _map();
811 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530812 });
Sagar Vora78777d62021-03-09 20:35:08 +0530813
814 return d;
815 }
816
817 if (opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530818 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530819 _map();
820 }
821}
822
Rushabh Mehta180e4352016-07-26 17:57:42 +0530823frappe.form.link_formatters['Item'] = function(value, doc) {
Alan157b3882021-04-21 21:00:22 +0530824 if (doc && value && doc.item_name && doc.item_name !== value && doc.item_code === value) {
marination824f48f2020-10-12 20:08:03 +0530825 return value + ': ' + doc.item_name;
826 } else if (!value && doc.doctype && doc.item_name) {
827 // format blank value in child table
828 return doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530829 } else {
marination824f48f2020-10-12 20:08:03 +0530830 // 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 +0530831 return value;
832 }
833}
834
835frappe.form.link_formatters['Employee'] = function(value, doc) {
Rucha Mahabal5ba1bc12021-11-23 09:30:30 +0530836 if (doc && value && doc.employee_name && doc.employee_name !== value && doc.employee === value) {
837 return value + ': ' + doc.employee_name;
838 } else if (!value && doc.doctype && doc.employee_name) {
839 // format blank value in child table
840 return doc.employee;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530841 } else {
Rucha Mahabal5ba1bc12021-11-23 09:30:30 +0530842 // 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 +0530843 return value;
844 }
845}
846
Rucha Mahabal59961f72021-05-20 23:43:19 +0530847frappe.form.link_formatters['Project'] = function(value, doc) {
848 if (doc && value && doc.project_name && doc.project_name !== value && doc.project === value) {
849 return value + ': ' + doc.project_name;
850 } else if (!value && doc.doctype && doc.project_name) {
851 // format blank value in child table
852 return doc.project;
853 } else {
854 // if value is blank in report view or project name and name are the same, return as is
855 return value;
856 }
857};
858
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530859// add description on posting time
860$(document).on('app_ready', function() {
861 if(!frappe.datetime.is_timezone_same()) {
862 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
863 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
864 frappe.ui.form.on(d, "onload", function(frm) {
865 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530866 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530867 });
868 });
869 }
akshay14384c22016-07-28 13:53:17 +0530870});
Alanc6dc9ea2021-05-07 20:30:04 +0530871
Himanshuec25d592021-06-14 19:05:52 +0530872// Show SLA dashboard
873$(document).on('app_ready', function() {
Ankush Menatbd9ef742023-07-16 11:34:42 +0530874 $.each(frappe.boot.service_level_agreement_doctypes, function(_i, d) {
875 frappe.ui.form.on(d, {
876 onload: function(frm) {
877 if (!frm.doc.service_level_agreement)
878 return;
Himanshuec25d592021-06-14 19:05:52 +0530879
Ankush Menatbd9ef742023-07-16 11:34:42 +0530880 frappe.call({
881 method: 'erpnext.support.doctype.service_level_agreement.service_level_agreement.get_service_level_agreement_filters',
882 args: {
883 doctype: frm.doc.doctype,
884 name: frm.doc.service_level_agreement,
885 customer: frm.doc.customer
Himanshuec25d592021-06-14 19:05:52 +0530886 },
Ankush Menatbd9ef742023-07-16 11:34:42 +0530887 callback: function (r) {
888 if (r && r.message) {
889 frm.set_query('priority', function() {
890 return {
891 filters: {
892 'name': ['in', r.message.priority],
Himanshuec25d592021-06-14 19:05:52 +0530893 }
Ankush Menatbd9ef742023-07-16 11:34:42 +0530894 };
Himanshuec25d592021-06-14 19:05:52 +0530895 });
Ankush Menatbd9ef742023-07-16 11:34:42 +0530896 frm.set_query('service_level_agreement', function() {
897 return {
898 filters: {
899 'name': ['in', r.message.service_level_agreements],
900 }
901 };
902 });
Himanshuec25d592021-06-14 19:05:52 +0530903 }
Ankush Menatbd9ef742023-07-16 11:34:42 +0530904 }
Himanshuec25d592021-06-14 19:05:52 +0530905 });
Ankush Menatbd9ef742023-07-16 11:34:42 +0530906 },
907
908 refresh: function(frm) {
909 if (frm.doc.status !== 'Closed' && frm.doc.service_level_agreement
910 && ['First Response Due', 'Resolution Due'].includes(frm.doc.agreement_status)) {
911 frappe.call({
912 'method': 'frappe.client.get',
913 args: {
914 doctype: 'Service Level Agreement',
915 name: frm.doc.service_level_agreement
916 },
917 callback: function(data) {
918 let statuses = data.message.pause_sla_on;
919 const hold_statuses = [];
920 $.each(statuses, (_i, entry) => {
921 hold_statuses.push(entry.status);
922 });
923 if (hold_statuses.includes(frm.doc.status)) {
924 frm.dashboard.clear_headline();
925 let message = {'indicator': 'orange', 'msg': __('SLA is on hold since {0}', [moment(frm.doc.on_hold_since).fromNow(true)])};
926 frm.dashboard.set_headline_alert(
927 '<div class="row">' +
928 '<div class="col-xs-12">' +
929 '<span class="indicator whitespace-nowrap '+ message.indicator +'"><span>'+ message.msg +'</span></span> ' +
930 '</div>' +
931 '</div>'
932 );
933 } else {
934 set_time_to_resolve_and_response(frm, data.message.apply_sla_for_resolution);
935 }
936 }
937 });
938 } else if (frm.doc.service_level_agreement) {
939 frm.dashboard.clear_headline();
940
941 let agreement_status = (frm.doc.agreement_status == 'Fulfilled') ?
942 {'indicator': 'green', 'msg': 'Service Level Agreement has been fulfilled'} :
943 {'indicator': 'red', 'msg': 'Service Level Agreement Failed'};
944
945 frm.dashboard.set_headline_alert(
946 '<div class="row">' +
947 '<div class="col-xs-12">' +
948 '<span class="indicator whitespace-nowrap '+ agreement_status.indicator +'"><span class="hidden-xs">'+ agreement_status.msg +'</span></span> ' +
949 '</div>' +
950 '</div>'
951 );
952 }
953 },
954 });
Himanshuec25d592021-06-14 19:05:52 +0530955 });
956});
957
958function set_time_to_resolve_and_response(frm, apply_sla_for_resolution) {
959 frm.dashboard.clear_headline();
960
Saqib24114772021-12-15 11:45:34 +0530961 let time_to_respond;
Saqib Ansari476e81a2021-12-06 18:55:30 +0530962 if (!frm.doc.first_responded_on) {
Himanshuec25d592021-06-14 19:05:52 +0530963 time_to_respond = get_time_left(frm.doc.response_by, frm.doc.agreement_status);
Saqib24114772021-12-15 11:45:34 +0530964 } else {
965 time_to_respond = get_status(frm.doc.response_by, frm.doc.first_responded_on);
Himanshuec25d592021-06-14 19:05:52 +0530966 }
967
968 let alert = `
969 <div class="row">
970 <div class="col-xs-12 col-sm-6">
971 <span class="indicator whitespace-nowrap ${time_to_respond.indicator}">
972 <span>Time to Respond: ${time_to_respond.diff_display}</span>
973 </span>
974 </div>`;
975
976
977 if (apply_sla_for_resolution) {
Saqib24114772021-12-15 11:45:34 +0530978 let time_to_resolve;
Saqib Ansari476e81a2021-12-06 18:55:30 +0530979 if (!frm.doc.resolution_date) {
Himanshuec25d592021-06-14 19:05:52 +0530980 time_to_resolve = get_time_left(frm.doc.resolution_by, frm.doc.agreement_status);
Saqib24114772021-12-15 11:45:34 +0530981 } else {
982 time_to_resolve = get_status(frm.doc.resolution_by, frm.doc.resolution_date);
Himanshuec25d592021-06-14 19:05:52 +0530983 }
984
985 alert += `
986 <div class="col-xs-12 col-sm-6">
987 <span class="indicator whitespace-nowrap ${time_to_resolve.indicator}">
988 <span>Time to Resolve: ${time_to_resolve.diff_display}</span>
989 </span>
990 </div>`;
991 }
992
993 alert += '</div>';
994
995 frm.dashboard.set_headline_alert(alert);
996}
997
998function get_time_left(timestamp, agreement_status) {
999 const diff = moment(timestamp).diff(moment());
1000 const diff_display = diff >= 44500 ? moment.duration(diff).humanize() : 'Failed';
1001 let indicator = (diff_display == 'Failed' && agreement_status != 'Fulfilled') ? 'red' : 'green';
1002 return {'diff_display': diff_display, 'indicator': indicator};
1003}
1004
Saqib24114772021-12-15 11:45:34 +05301005function get_status(expected, actual) {
1006 const time_left = moment(expected).diff(moment(actual));
Saqib Ansari476e81a2021-12-06 18:55:30 +05301007 if (time_left >= 0) {
Himanshuec25d592021-06-14 19:05:52 +05301008 return {'diff_display': 'Fulfilled', 'indicator': 'green'};
1009 } else {
1010 return {'diff_display': 'Failed', 'indicator': 'red'};
1011 }
1012}
1013
Alanc6dc9ea2021-05-07 20:30:04 +05301014function attach_selector_button(inner_text, append_loction, context, grid_row) {
1015 let $btn_div = $("<div>").css({"margin-bottom": "10px", "margin-top": "10px"})
1016 .appendTo(append_loction);
1017 let $btn = $(`<button class="btn btn-sm btn-default">${inner_text}</button>`)
1018 .appendTo($btn_div);
1019
1020 $btn.on("click", function() {
1021 context.show_serial_batch_selector(grid_row.frm, grid_row.doc, "", "", true);
1022 });
1023}