Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | // Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | // License: GNU General Public License v3. See license.txt |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 3 | frappe.provide("erpnext"); |
Nabin Hait | b072c74 | 2014-12-16 12:49:13 +0530 | [diff] [blame] | 4 | frappe.provide("erpnext.utils"); |
Anand Doshi | e65e621 | 2012-11-19 15:43:18 +0530 | [diff] [blame] | 5 | |
Anand Doshi | bdee6e0 | 2013-07-09 13:03:39 +0530 | [diff] [blame] | 6 | $.extend(erpnext, { |
| 7 | get_currency: function(company) { |
| 8 | if(!company && cur_frm) |
| 9 | company = cur_frm.doc.company; |
| 10 | if(company) |
ruthra kumar | fa2d33c | 2023-12-12 15:43:39 +0530 | [diff] [blame] | 11 | return frappe.get_doc(":Company", company)?.default_currency || frappe.boot.sysdefaults.currency; |
Anand Doshi | bdee6e0 | 2013-07-09 13:03:39 +0530 | [diff] [blame] | 12 | else |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 13 | return frappe.boot.sysdefaults.currency; |
Anand Doshi | bdee6e0 | 2013-07-09 13:03:39 +0530 | [diff] [blame] | 14 | }, |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 15 | |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 16 | get_presentation_currency_list: () => { |
| 17 | const docs = frappe.boot.docs; |
Zarrar | e25dcd2 | 2018-06-21 10:53:12 +0530 | [diff] [blame] | 18 | let currency_list = docs.filter(d => d.doctype === ":Currency").map(d => d.name); |
| 19 | currency_list.unshift(""); |
tundebabzy | c897825 | 2018-02-12 10:34:50 +0100 | [diff] [blame] | 20 | return currency_list; |
| 21 | }, |
| 22 | |
Anand Doshi | 8bde7f9 | 2014-04-24 18:11:49 +0530 | [diff] [blame] | 23 | toggle_naming_series: function() { |
ruthra kumar | 60b26ad | 2024-01-15 20:22:30 +0530 | [diff] [blame] | 24 | if(cur_frm && cur_frm.fields_dict.naming_series) { |
Anand Doshi | bdee6e0 | 2013-07-09 13:03:39 +0530 | [diff] [blame] | 25 | cur_frm.toggle_display("naming_series", cur_frm.doc.__islocal?true:false); |
| 26 | } |
| 27 | }, |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 28 | |
Anand Doshi | bdee6e0 | 2013-07-09 13:03:39 +0530 | [diff] [blame] | 29 | hide_company: function() { |
| 30 | if(cur_frm.fields_dict.company) { |
Rushabh Mehta | 8941841 | 2013-10-07 18:22:29 +0530 | [diff] [blame] | 31 | var companies = Object.keys(locals[":Company"] || {}); |
Anand Doshi | bdee6e0 | 2013-07-09 13:03:39 +0530 | [diff] [blame] | 32 | 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 Mehta | 45ca8a4 | 2015-04-28 16:02:09 +0530 | [diff] [blame] | 35 | } else if(erpnext.last_selected_company) { |
| 36 | if(!cur_frm.doc.company) cur_frm.set_value("company", erpnext.last_selected_company); |
Anand Doshi | bdee6e0 | 2013-07-09 13:03:39 +0530 | [diff] [blame] | 37 | } |
| 38 | } |
Anand Doshi | 1ffc5b5 | 2013-07-15 18:09:43 +0530 | [diff] [blame] | 39 | }, |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 40 | |
Rohit Waghchaure | e9ff191 | 2017-06-19 12:54:59 +0530 | [diff] [blame] | 41 | is_perpetual_inventory_enabled: function(company) { |
| 42 | if(company) { |
| 43 | return frappe.get_doc(":Company", company).enable_perpetual_inventory |
| 44 | } |
| 45 | }, |
| 46 | |
Makarand Bauskar | e6712c1 | 2017-10-25 12:17:40 +0530 | [diff] [blame] | 47 | stale_rate_allowed: () => { |
rohitwaghchaure | 7677ff0 | 2017-11-02 18:12:14 +0530 | [diff] [blame] | 48 | return cint(frappe.boot.sysdefaults.allow_stale); |
Makarand Bauskar | e6712c1 | 2017-10-25 12:17:40 +0530 | [diff] [blame] | 49 | }, |
| 50 | |
Alan | c6dc9ea | 2021-05-07 20:30:04 +0530 | [diff] [blame] | 51 | 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 Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 55 | |
Alan | c6dc9ea | 2021-05-07 20:30:04 +0530 | [diff] [blame] | 56 | 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 Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 59 | |
Alan | c6dc9ea | 2021-05-07 20:30:04 +0530 | [diff] [blame] | 60 | 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); |
Marica | 29a2e16 | 2019-11-12 14:43:41 +0530 | [diff] [blame] | 66 | } |
Alan | c6dc9ea | 2021-05-07 20:30:04 +0530 | [diff] [blame] | 67 | } |
| 68 | ); |
deepeshgarg007 | e64c357 | 2019-05-14 08:50:45 +0530 | [diff] [blame] | 69 | }, |
Nabin Hait | 1ed1c4e | 2019-11-25 12:33:40 +0530 | [diff] [blame] | 70 | |
| 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 Bot | 255b99e | 2021-08-24 20:19:22 +0530 | [diff] [blame] | 85 | }, |
| 86 | |
Ankush Menat | d37541d | 2021-12-10 12:04:10 +0530 | [diff] [blame] | 87 | route_to_pending_reposts: (args) => { |
| 88 | frappe.set_route('List', 'Repost Item Valuation', args); |
| 89 | }, |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 90 | }); |
Rushabh Mehta | 7d36875 | 2014-11-24 14:16:47 +0530 | [diff] [blame] | 91 | |
Nabin Hait | b072c74 | 2014-12-16 12:49:13 +0530 | [diff] [blame] | 92 | |
| 93 | $.extend(erpnext.utils, { |
Rushabh Mehta | 559aa3a | 2016-09-19 16:04:58 +0530 | [diff] [blame] | 94 | set_party_dashboard_indicators: function(frm) { |
| 95 | if(frm.doc.__onload && frm.doc.__onload.dashboard_info) { |
deepeshgarg007 | 920dc14 | 2018-11-23 10:17:28 +0530 | [diff] [blame] | 96 | var company_wise_info = frm.doc.__onload.dashboard_info; |
deepeshgarg007 | f31caff | 2018-11-27 15:04:12 +0530 | [diff] [blame] | 97 | if(company_wise_info.length > 1) { |
deepeshgarg007 | a1cffc3 | 2018-11-23 16:51:35 +0530 | [diff] [blame] | 98 | company_wise_info.forEach(function(info) { |
deepeshgarg007 | 12f5cef | 2018-12-25 16:06:19 +0530 | [diff] [blame] | 99 | erpnext.utils.add_indicator_for_multicompany(frm, info); |
deepeshgarg007 | a1cffc3 | 2018-11-23 16:51:35 +0530 | [diff] [blame] | 100 | }); |
deepeshgarg007 | 12f5cef | 2018-12-25 16:06:19 +0530 | [diff] [blame] | 101 | } else if (company_wise_info.length === 1) { |
deepeshgarg007 | 64238ee | 2018-12-25 16:28:39 +0530 | [diff] [blame] | 102 | 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)]), |
deepeshgarg007 | 8300f5e | 2019-01-01 20:28:49 +0530 | [diff] [blame] | 106 | company_wise_info[0].total_unpaid ? 'orange' : 'green'); |
deepeshgarg007 | 12f5cef | 2018-12-25 16:06:19 +0530 | [diff] [blame] | 107 | |
deepeshgarg007 | 64238ee | 2018-12-25 16:28:39 +0530 | [diff] [blame] | 108 | if(company_wise_info[0].loyalty_points) { |
| 109 | frm.dashboard.add_indicator(__('Loyalty Points: {0}', |
| 110 | [company_wise_info[0].loyalty_points]), 'blue'); |
| 111 | } |
deepeshgarg007 | f31caff | 2018-11-27 15:04:12 +0530 | [diff] [blame] | 112 | } |
Rushabh Mehta | 559aa3a | 2016-09-19 16:04:58 +0530 | [diff] [blame] | 113 | } |
| 114 | }, |
| 115 | |
Rohit Waghchaure | 708eefb | 2023-07-10 16:54:55 +0530 | [diff] [blame] | 116 | view_serial_batch_nos: function(frm) { |
rohitwaghchaure | 3c15fea | 2023-08-24 17:24:44 +0530 | [diff] [blame] | 117 | if (!frm.doc?.items) { |
| 118 | return; |
| 119 | } |
| 120 | |
Rohit Waghchaure | 708eefb | 2023-07-10 16:54:55 +0530 | [diff] [blame] | 121 | let bundle_ids = frm.doc.items.filter(d => d.serial_and_batch_bundle); |
| 122 | |
| 123 | if (bundle_ids?.length) { |
| 124 | frm.add_custom_button(__('Serial / Batch Nos'), () => { |
| 125 | frappe.route_options = { |
| 126 | "voucher_no": frm.doc.name, |
| 127 | "voucher_type": frm.doc.doctype, |
| 128 | "from_date": frm.doc.posting_date || frm.doc.transaction_date, |
| 129 | "to_date": frm.doc.posting_date || frm.doc.transaction_date, |
| 130 | "company": frm.doc.company, |
| 131 | }; |
| 132 | frappe.set_route("query-report", "Serial and Batch Summary"); |
| 133 | }, __('View')); |
| 134 | } |
| 135 | }, |
| 136 | |
deepeshgarg007 | 12f5cef | 2018-12-25 16:06:19 +0530 | [diff] [blame] | 137 | add_indicator_for_multicompany: function(frm, info) { |
Deepak | 867f2c6 | 2022-05-17 11:52:52 +0530 | [diff] [blame] | 138 | frm.dashboard.stats_area.show(); |
deepeshgarg007 | 12f5cef | 2018-12-25 16:06:19 +0530 | [diff] [blame] | 139 | frm.dashboard.stats_area_row.addClass('flex'); |
| 140 | frm.dashboard.stats_area_row.css('flex-wrap', 'wrap'); |
| 141 | |
| 142 | var color = info.total_unpaid ? 'orange' : 'green'; |
| 143 | |
| 144 | var indicator = $('<div class="flex-column col-xs-6">'+ |
| 145 | '<div style="margin-top:10px"><h6>'+info.company+'</h6></div>'+ |
| 146 | |
| 147 | '<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+ |
| 148 | 'Annual Billing: '+format_currency(info.billing_this_year, info.currency)+'</span></div>'+ |
| 149 | |
| 150 | '<div class="badge-link small" style="margin-bottom:10px">'+ |
| 151 | '<span class="indicator '+color+'">Total Unpaid: ' |
| 152 | +format_currency(info.total_unpaid, info.currency)+'</span></div>'+ |
| 153 | |
| 154 | |
| 155 | '</div>').appendTo(frm.dashboard.stats_area_row); |
| 156 | |
| 157 | if(info.loyalty_points){ |
| 158 | $('<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+ |
| 159 | 'Loyalty Points: '+info.loyalty_points+'</span></div>').appendTo(indicator); |
| 160 | } |
| 161 | |
| 162 | return indicator; |
| 163 | }, |
| 164 | |
Shreya Shah | 149f7ee | 2018-03-27 11:29:25 +0530 | [diff] [blame] | 165 | get_party_name: function(party_type) { |
| 166 | var dict = {'Customer': 'customer_name', 'Supplier': 'supplier_name', 'Employee': 'employee_name', |
| 167 | 'Member': 'member_name'}; |
| 168 | return dict[party_type]; |
| 169 | }, |
| 170 | |
Rushabh Mehta | 49f9747 | 2018-08-30 18:50:48 +0530 | [diff] [blame] | 171 | copy_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname) { |
Neil Trini Lasrado | fe2ffae | 2015-07-08 18:16:51 +0530 | [diff] [blame] | 172 | var d = locals[dt][dn]; |
| 173 | if(d[fieldname]){ |
| 174 | var cl = doc[table_fieldname] || []; |
| 175 | for(var i = 0; i < cl.length; i++) { |
| 176 | if(!cl[i][fieldname]) cl[i][fieldname] = d[fieldname]; |
| 177 | } |
| 178 | } |
| 179 | refresh_field(table_fieldname); |
Makarand Bauskar | 157c334 | 2017-05-26 21:32:33 +0530 | [diff] [blame] | 180 | }, |
| 181 | |
| 182 | get_terms: function(tc_name, doc, callback) { |
| 183 | if(tc_name) { |
| 184 | return frappe.call({ |
| 185 | method: 'erpnext.setup.doctype.terms_and_conditions.terms_and_conditions.get_terms_and_conditions', |
| 186 | args: { |
| 187 | template_name: tc_name, |
| 188 | doc: doc |
| 189 | }, |
| 190 | callback: function(r) { |
| 191 | callback(r) |
| 192 | } |
| 193 | }); |
| 194 | } |
tundebabzy | 1a947db | 2017-08-29 13:48:27 +0100 | [diff] [blame] | 195 | }, |
| 196 | |
rohitwaghchaure | 713cfc7 | 2018-09-11 17:40:37 +0530 | [diff] [blame] | 197 | make_bank_account: function(doctype, docname) { |
| 198 | frappe.call({ |
| 199 | method: "erpnext.accounts.doctype.bank_account.bank_account.make_bank_account", |
| 200 | args: { |
| 201 | doctype: doctype, |
| 202 | docname: docname |
| 203 | }, |
| 204 | freeze: true, |
| 205 | callback: function(r) { |
| 206 | var doclist = frappe.model.sync(r.message); |
| 207 | frappe.set_route("Form", doclist[0].doctype, doclist[0].name); |
| 208 | } |
| 209 | }) |
| 210 | }, |
| 211 | |
Deepesh Garg | 11ea0b1 | 2020-05-26 19:23:45 +0530 | [diff] [blame] | 212 | add_dimensions: function(report_name, index) { |
| 213 | let filters = frappe.query_reports[report_name].filters; |
| 214 | |
Deepesh Garg | 7b2d518 | 2020-12-04 11:28:26 +0530 | [diff] [blame] | 215 | frappe.call({ |
| 216 | method: "erpnext.accounts.doctype.accounting_dimension.accounting_dimension.get_dimensions", |
| 217 | callback: function(r) { |
| 218 | let accounting_dimensions = r.message[0]; |
| 219 | accounting_dimensions.forEach((dimension) => { |
| 220 | let found = filters.some(el => el.fieldname === dimension['fieldname']); |
Deepesh Garg | 11ea0b1 | 2020-05-26 19:23:45 +0530 | [diff] [blame] | 221 | |
Deepesh Garg | 7b2d518 | 2020-12-04 11:28:26 +0530 | [diff] [blame] | 222 | if (!found) { |
Deepesh Garg | 23ab5c5 | 2020-12-31 11:29:06 +0530 | [diff] [blame] | 223 | filters.splice(index, 0, { |
Deepesh Garg | 7b2d518 | 2020-12-04 11:28:26 +0530 | [diff] [blame] | 224 | "fieldname": dimension["fieldname"], |
| 225 | "label": __(dimension["label"]), |
Rohit Waghchaure | 69be22b | 2022-05-14 17:19:34 +0530 | [diff] [blame] | 226 | "fieldtype": "MultiSelectList", |
| 227 | get_data: function(txt) { |
| 228 | return frappe.db.get_link_options(dimension["document_type"], txt); |
| 229 | }, |
Deepesh Garg | 7b2d518 | 2020-12-04 11:28:26 +0530 | [diff] [blame] | 230 | }); |
| 231 | } |
Deepesh Garg | 11ea0b1 | 2020-05-26 19:23:45 +0530 | [diff] [blame] | 232 | }); |
| 233 | } |
| 234 | }); |
| 235 | }, |
| 236 | |
Rohit Waghchaure | dbec5cf | 2022-06-22 12:24:08 +0530 | [diff] [blame] | 237 | add_inventory_dimensions: function(report_name, index) { |
| 238 | let filters = frappe.query_reports[report_name].filters; |
| 239 | |
| 240 | frappe.call({ |
| 241 | method: "erpnext.stock.doctype.inventory_dimension.inventory_dimension.get_inventory_dimensions", |
| 242 | callback: function(r) { |
| 243 | if (r.message && r.message.length) { |
| 244 | r.message.forEach((dimension) => { |
Rohit Waghchaure | 0e388ba | 2023-02-20 12:20:03 +0530 | [diff] [blame] | 245 | let existing_filter = filters.filter(el => el.fieldname === dimension['fieldname']); |
Rohit Waghchaure | dbec5cf | 2022-06-22 12:24:08 +0530 | [diff] [blame] | 246 | |
Rohit Waghchaure | 0e388ba | 2023-02-20 12:20:03 +0530 | [diff] [blame] | 247 | if (!existing_filter.length) { |
Rohit Waghchaure | dbec5cf | 2022-06-22 12:24:08 +0530 | [diff] [blame] | 248 | filters.splice(index, 0, { |
| 249 | "fieldname": dimension["fieldname"], |
Rohit Waghchaure | ef7def8 | 2022-09-07 14:40:18 +0530 | [diff] [blame] | 250 | "label": __(dimension["doctype"]), |
Rohit Waghchaure | dbec5cf | 2022-06-22 12:24:08 +0530 | [diff] [blame] | 251 | "fieldtype": "MultiSelectList", |
| 252 | get_data: function(txt) { |
| 253 | return frappe.db.get_link_options(dimension["doctype"], txt); |
| 254 | }, |
| 255 | }); |
Rohit Waghchaure | 0e388ba | 2023-02-20 12:20:03 +0530 | [diff] [blame] | 256 | } else { |
| 257 | existing_filter[0]['fieldtype'] = "MultiSelectList"; |
| 258 | existing_filter[0]['get_data'] = function(txt) { |
| 259 | return frappe.db.get_link_options(dimension["doctype"], txt); |
| 260 | } |
Rohit Waghchaure | dbec5cf | 2022-06-22 12:24:08 +0530 | [diff] [blame] | 261 | } |
| 262 | }); |
| 263 | } |
| 264 | } |
| 265 | }); |
| 266 | }, |
| 267 | |
rohitwaghchaure | 166b78f | 2017-09-07 16:14:22 +0530 | [diff] [blame] | 268 | make_subscription: function(doctype, docname) { |
| 269 | frappe.call({ |
Rucha Mahabal | 65a627c | 2019-07-17 13:50:32 +0530 | [diff] [blame] | 270 | method: "frappe.automation.doctype.auto_repeat.auto_repeat.make_auto_repeat", |
rohitwaghchaure | 166b78f | 2017-09-07 16:14:22 +0530 | [diff] [blame] | 271 | args: { |
| 272 | doctype: doctype, |
| 273 | docname: docname |
| 274 | }, |
| 275 | callback: function(r) { |
| 276 | var doclist = frappe.model.sync(r.message); |
| 277 | frappe.set_route("Form", doclist[0].doctype, doclist[0].name); |
| 278 | } |
| 279 | }) |
| 280 | }, |
| 281 | |
Saif | 90cf2dd | 2018-09-30 21:46:31 +0500 | [diff] [blame] | 282 | make_pricing_rule: function(doctype, docname) { |
| 283 | frappe.call({ |
| 284 | method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.make_pricing_rule", |
| 285 | args: { |
| 286 | doctype: doctype, |
| 287 | docname: docname |
| 288 | }, |
| 289 | callback: function(r) { |
| 290 | var doclist = frappe.model.sync(r.message); |
| 291 | frappe.set_route("Form", doclist[0].doctype, doclist[0].name); |
| 292 | } |
| 293 | }) |
| 294 | }, |
| 295 | |
tundebabzy | 1a947db | 2017-08-29 13:48:27 +0100 | [diff] [blame] | 296 | /** |
| 297 | * Checks if the first row of a given child table is empty |
| 298 | * @param child_table - Child table Doctype |
| 299 | * @return {Boolean} |
| 300 | **/ |
| 301 | first_row_is_empty: function(child_table){ |
| 302 | if($.isArray(child_table) && child_table.length > 0) { |
| 303 | return !child_table[0].item_code; |
| 304 | } |
| 305 | return false; |
| 306 | }, |
| 307 | |
| 308 | /** |
| 309 | * Removes the first row of a child table if it is empty |
| 310 | * @param {_Frm} frm - The current form |
| 311 | * @param {String} child_table_name - The child table field name |
| 312 | * @return {Boolean} |
| 313 | **/ |
| 314 | remove_empty_first_row: function(frm, child_table_name){ |
| 315 | const rows = frm['doc'][child_table_name]; |
| 316 | if (this.first_row_is_empty(rows)){ |
| 317 | frm['doc'][child_table_name] = rows.splice(1); |
| 318 | } |
| 319 | return rows; |
| 320 | }, |
Zarrar | 6ffdf94 | 2018-06-14 12:24:16 +0530 | [diff] [blame] | 321 | get_tree_options: function(option) { |
| 322 | // get valid options for tree based on user permission & locals dict |
| 323 | let unscrub_option = frappe.model.unscrub(option); |
| 324 | let user_permission = frappe.defaults.get_user_permissions(); |
deepeshgarg007 | 0d64d62 | 2019-02-20 17:55:14 +0530 | [diff] [blame] | 325 | let options; |
| 326 | |
Zarrar | 6ffdf94 | 2018-06-14 12:24:16 +0530 | [diff] [blame] | 327 | if(user_permission && user_permission[unscrub_option]) { |
deepeshgarg007 | 0d64d62 | 2019-02-20 17:55:14 +0530 | [diff] [blame] | 328 | options = user_permission[unscrub_option].map(perm => perm.doc); |
Zarrar | 6ffdf94 | 2018-06-14 12:24:16 +0530 | [diff] [blame] | 329 | } else { |
deepeshgarg007 | 0d64d62 | 2019-02-20 17:55:14 +0530 | [diff] [blame] | 330 | options = $.map(locals[`:${unscrub_option}`], function(c) { return c.name; }).sort(); |
Zarrar | 6ffdf94 | 2018-06-14 12:24:16 +0530 | [diff] [blame] | 331 | } |
deepeshgarg007 | 0d64d62 | 2019-02-20 17:55:14 +0530 | [diff] [blame] | 332 | |
| 333 | // filter unique values, as there may be multiple user permissions for any value |
| 334 | return options.filter((value, index, self) => self.indexOf(value) === index); |
Zarrar | 6ffdf94 | 2018-06-14 12:24:16 +0530 | [diff] [blame] | 335 | }, |
| 336 | get_tree_default: function(option) { |
| 337 | // set default for a field based on user permission |
| 338 | let options = this.get_tree_options(option); |
| 339 | if(options.includes(frappe.defaults.get_default(option))) { |
| 340 | return frappe.defaults.get_default(option); |
| 341 | } else { |
| 342 | return options[0]; |
| 343 | } |
Sanjay Kumar | 1b49f3a | 2018-09-06 13:09:35 +0400 | [diff] [blame] | 344 | }, |
Andy Zhu | bffe933 | 2021-04-12 22:49:26 +1200 | [diff] [blame] | 345 | overrides_parent_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname, parent_fieldname) { |
Marica | 24e4516 | 2021-04-14 18:53:15 +0530 | [diff] [blame] | 346 | if (doc[parent_fieldname]) { |
Andy Zhu | bffe933 | 2021-04-12 22:49:26 +1200 | [diff] [blame] | 347 | let cl = doc[table_fieldname] || []; |
Marica | 24e4516 | 2021-04-14 18:53:15 +0530 | [diff] [blame] | 348 | for (let i = 0; i < cl.length; i++) { |
Sanjay Kumar | 1b49f3a | 2018-09-06 13:09:35 +0400 | [diff] [blame] | 349 | cl[i][fieldname] = doc[parent_fieldname]; |
| 350 | } |
Andy Zhu | bffe933 | 2021-04-12 22:49:26 +1200 | [diff] [blame] | 351 | frappe.refresh_field(table_fieldname); |
Sanjay Kumar | 1b49f3a | 2018-09-06 13:09:35 +0400 | [diff] [blame] | 352 | } |
Sanjay Kumar | 1b49f3a | 2018-09-06 13:09:35 +0400 | [diff] [blame] | 353 | }, |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 354 | create_new_doc: function (doctype, update_fields) { |
| 355 | frappe.model.with_doctype(doctype, function() { |
| 356 | var new_doc = frappe.model.get_new_doc(doctype); |
| 357 | for (let [key, value] of Object.entries(update_fields)) { |
| 358 | new_doc[key] = value; |
| 359 | } |
| 360 | frappe.ui.form.make_quick_entry(doctype, null, null, new_doc); |
| 361 | }); |
Ankush Menat | 0b86b1b | 2022-12-08 16:40:13 +0530 | [diff] [blame] | 362 | }, |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 363 | |
Ankush Menat | 0b86b1b | 2022-12-08 16:40:13 +0530 | [diff] [blame] | 364 | // check if payments app is installed on site, if not warn user. |
| 365 | check_payments_app: () => { |
| 366 | if (frappe.boot.versions && !frappe.boot.versions.payments) { |
| 367 | const marketplace_link = '<a href="https://frappecloud.com/marketplace/apps/payments">Marketplace</a>' |
| 368 | const github_link = '<a href="https://github.com/frappe/payments/">GitHub</a>' |
| 369 | const msg = __("payments app is not installed. Please install it from {0} or {1}", [marketplace_link, github_link]) |
| 370 | frappe.msgprint(msg); |
| 371 | } |
| 372 | |
| 373 | }, |
Rohit Waghchaure | 1c2fe08 | 2023-06-15 12:54:43 +0530 | [diff] [blame] | 374 | |
| 375 | pick_serial_and_batch_bundle(frm, cdt, cdn, type_of_transaction, warehouse_field) { |
| 376 | let item_row = frappe.get_doc(cdt, cdn); |
| 377 | item_row.type_of_transaction = type_of_transaction; |
| 378 | |
| 379 | frappe.db.get_value("Item", item_row.item_code, ["has_batch_no", "has_serial_no"]) |
| 380 | .then((r) => { |
| 381 | item_row.has_batch_no = r.message.has_batch_no; |
| 382 | item_row.has_serial_no = r.message.has_serial_no; |
| 383 | |
| 384 | frappe.require("assets/erpnext/js/utils/serial_no_batch_selector.js", function() { |
| 385 | new erpnext.SerialBatchPackageSelector(frm, item_row, (r) => { |
| 386 | if (r) { |
| 387 | let update_values = { |
| 388 | "serial_and_batch_bundle": r.name, |
| 389 | "qty": Math.abs(r.total_qty) |
| 390 | } |
| 391 | |
| 392 | if (!warehouse_field) { |
| 393 | warehouse_field = "warehouse"; |
| 394 | } |
| 395 | |
| 396 | if (r.warehouse) { |
| 397 | update_values[warehouse_field] = r.warehouse; |
| 398 | } |
| 399 | |
| 400 | frappe.model.set_value(item_row.doctype, item_row.name, update_values); |
| 401 | } |
| 402 | }); |
| 403 | }); |
| 404 | }); |
Deepesh Garg | 6270607 | 2023-07-16 12:58:42 +0530 | [diff] [blame] | 405 | }, |
| 406 | |
ruthra kumar | c31ee8e | 2023-11-14 06:44:49 +0530 | [diff] [blame] | 407 | get_fiscal_year: function(date, with_dates=false, boolean=false) { |
Deepesh Garg | 3759a41 | 2023-07-19 13:17:12 +0530 | [diff] [blame] | 408 | if(!date) { |
| 409 | date = frappe.datetime.get_today(); |
| 410 | } |
| 411 | |
Deepesh Garg | 6270607 | 2023-07-16 12:58:42 +0530 | [diff] [blame] | 412 | let fiscal_year = ''; |
| 413 | frappe.call({ |
| 414 | method: "erpnext.accounts.utils.get_fiscal_year", |
| 415 | args: { |
ruthra kumar | c31ee8e | 2023-11-14 06:44:49 +0530 | [diff] [blame] | 416 | date: date, |
| 417 | boolean: boolean |
Deepesh Garg | 6270607 | 2023-07-16 12:58:42 +0530 | [diff] [blame] | 418 | }, |
| 419 | async: false, |
| 420 | callback: function(r) { |
| 421 | if (r.message) { |
Deepesh Garg | 4496a67 | 2023-07-24 11:58:16 +0530 | [diff] [blame] | 422 | if (with_dates) |
| 423 | fiscal_year = r.message; |
| 424 | else |
| 425 | fiscal_year = r.message[0]; |
Deepesh Garg | 6270607 | 2023-07-16 12:58:42 +0530 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | }); |
| 429 | return fiscal_year; |
Rohit Waghchaure | 1c2fe08 | 2023-06-15 12:54:43 +0530 | [diff] [blame] | 430 | } |
Rushabh Mehta | 95b995b | 2015-05-28 12:10:55 +0530 | [diff] [blame] | 431 | }); |
| 432 | |
rohitwaghchaure | a3c3dec | 2018-03-28 11:51:44 +0530 | [diff] [blame] | 433 | erpnext.utils.select_alternate_items = function(opts) { |
| 434 | const frm = opts.frm; |
| 435 | const warehouse_field = opts.warehouse_field || 'warehouse'; |
| 436 | const item_field = opts.item_field || 'item_code'; |
| 437 | |
| 438 | this.data = []; |
| 439 | const dialog = new frappe.ui.Dialog({ |
| 440 | title: __("Select Alternate Item"), |
| 441 | fields: [ |
| 442 | {fieldtype:'Section Break', label: __('Items')}, |
| 443 | { |
| 444 | fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true, |
| 445 | in_place_edit: true, data: this.data, |
| 446 | get_data: () => { |
| 447 | return this.data; |
| 448 | }, |
| 449 | fields: [{ |
| 450 | fieldtype:'Data', |
| 451 | fieldname:"docname", |
| 452 | hidden: 1 |
| 453 | }, { |
| 454 | fieldtype:'Link', |
| 455 | fieldname:"item_code", |
| 456 | options: 'Item', |
| 457 | in_list_view: 1, |
| 458 | read_only: 1, |
| 459 | label: __('Item Code') |
| 460 | }, { |
| 461 | fieldtype:'Link', |
| 462 | fieldname:"alternate_item", |
| 463 | options: 'Item', |
| 464 | default: "", |
| 465 | in_list_view: 1, |
| 466 | label: __('Alternate Item'), |
| 467 | onchange: function() { |
| 468 | const item_code = this.get_value(); |
| 469 | const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value(); |
| 470 | if (item_code && warehouse) { |
| 471 | frappe.call({ |
| 472 | method: "erpnext.stock.utils.get_latest_stock_qty", |
| 473 | args: { |
| 474 | item_code: item_code, |
| 475 | warehouse: warehouse |
| 476 | }, |
| 477 | callback: (r) => { |
| 478 | this.grid_row.on_grid_fields_dict |
| 479 | .actual_qty.set_value(r.message || 0); |
| 480 | } |
| 481 | }) |
| 482 | } |
| 483 | }, |
| 484 | get_query: (e) => { |
| 485 | return { |
| 486 | query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items", |
| 487 | filters: { |
| 488 | item_code: e.item_code |
| 489 | } |
| 490 | }; |
| 491 | } |
| 492 | }, { |
| 493 | fieldtype:'Link', |
| 494 | fieldname:"warehouse", |
| 495 | options: 'Warehouse', |
| 496 | default: "", |
| 497 | in_list_view: 1, |
| 498 | label: __('Warehouse'), |
| 499 | onchange: function() { |
| 500 | const warehouse = this.get_value(); |
| 501 | const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value(); |
| 502 | if (item_code && warehouse) { |
| 503 | frappe.call({ |
| 504 | method: "erpnext.stock.utils.get_latest_stock_qty", |
| 505 | args: { |
| 506 | item_code: item_code, |
| 507 | warehouse: warehouse |
| 508 | }, |
| 509 | callback: (r) => { |
| 510 | this.grid_row.on_grid_fields_dict |
| 511 | .actual_qty.set_value(r.message || 0); |
| 512 | } |
| 513 | }) |
| 514 | } |
| 515 | }, |
| 516 | }, { |
| 517 | fieldtype:'Float', |
| 518 | fieldname:"actual_qty", |
| 519 | default: 0, |
| 520 | read_only: 1, |
| 521 | in_list_view: 1, |
| 522 | label: __('Available Qty') |
| 523 | }] |
| 524 | }, |
| 525 | ], |
| 526 | primary_action: function() { |
| 527 | const args = this.get_values()["alternative_items"]; |
| 528 | const alternative_items = args.filter(d => { |
| 529 | if (d.alternate_item && d.item_code != d.alternate_item) { |
| 530 | return true; |
| 531 | } |
| 532 | }); |
| 533 | |
| 534 | alternative_items.forEach(d => { |
| 535 | let row = frappe.get_doc(opts.child_doctype, d.docname); |
Doridel Cahanap | e42192b | 2018-05-16 13:28:39 +0800 | [diff] [blame] | 536 | let qty = null; |
| 537 | if (row.doctype === 'Work Order Item') { |
| 538 | qty = row.required_qty; |
| 539 | } else { |
| 540 | qty = row.qty; |
| 541 | } |
rohitwaghchaure | a3c3dec | 2018-03-28 11:51:44 +0530 | [diff] [blame] | 542 | row[item_field] = d.alternate_item; |
mergify[bot] | c1a0f64 | 2021-12-17 19:06:19 +0530 | [diff] [blame] | 543 | frappe.model.set_value(row.doctype, row.name, 'qty', qty); |
| 544 | frappe.model.set_value(row.doctype, row.name, opts.original_item_field, d.item_code); |
| 545 | frm.trigger(item_field, row.doctype, row.name); |
rohitwaghchaure | a3c3dec | 2018-03-28 11:51:44 +0530 | [diff] [blame] | 546 | }); |
| 547 | |
| 548 | refresh_field(opts.child_docname); |
| 549 | this.hide(); |
| 550 | }, |
| 551 | primary_action_label: __('Update') |
| 552 | }); |
| 553 | |
| 554 | frm.doc[opts.child_docname].forEach(d => { |
| 555 | if (!opts.condition || opts.condition(d)) { |
| 556 | dialog.fields_dict.alternative_items.df.data.push({ |
| 557 | "docname": d.name, |
| 558 | "item_code": d[item_field], |
| 559 | "warehouse": d[warehouse_field], |
| 560 | "actual_qty": d.actual_qty |
| 561 | }); |
| 562 | } |
| 563 | }) |
| 564 | |
| 565 | this.data = dialog.fields_dict.alternative_items.df.data; |
| 566 | dialog.fields_dict.alternative_items.grid.refresh(); |
| 567 | dialog.show(); |
| 568 | } |
| 569 | |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 570 | erpnext.utils.update_child_items = function(opts) { |
| 571 | const frm = opts.frm; |
Stavros Anastasiadis | 3d82b74 | 2018-12-10 13:00:55 +0100 | [diff] [blame] | 572 | const cannot_add_row = (typeof opts.cannot_add_row === 'undefined') ? true : opts.cannot_add_row; |
| 573 | const child_docname = (typeof opts.cannot_add_row === 'undefined') ? "items" : opts.child_docname; |
Saqib | 56fea7d | 2020-10-09 21:19:25 +0530 | [diff] [blame] | 574 | const child_meta = frappe.get_meta(`${frm.doc.doctype} Item`); |
s-aga-r | 2d8363a | 2023-09-02 11:02:24 +0530 | [diff] [blame] | 575 | const has_reserved_stock = opts.has_reserved_stock ? true : false; |
Saqib | 56fea7d | 2020-10-09 21:19:25 +0530 | [diff] [blame] | 576 | const get_precision = (fieldname) => child_meta.fields.find(f => f.fieldname == fieldname).precision; |
| 577 | |
Devin Slauenwhite | a835c1a | 2023-01-26 16:53:05 -0500 | [diff] [blame] | 578 | this.data = frm.doc[opts.child_docname].map((d) => { |
| 579 | return { |
| 580 | "docname": d.name, |
| 581 | "name": d.name, |
| 582 | "item_code": d.item_code, |
| 583 | "delivery_date": d.delivery_date, |
| 584 | "schedule_date": d.schedule_date, |
| 585 | "conversion_factor": d.conversion_factor, |
| 586 | "qty": d.qty, |
| 587 | "rate": d.rate, |
s-aga-r | 9588bb7 | 2023-08-21 22:23:57 +0530 | [diff] [blame] | 588 | "uom": d.uom, |
| 589 | "fg_item": d.fg_item, |
| 590 | "fg_item_qty": d.fg_item_qty, |
Devin Slauenwhite | a835c1a | 2023-01-26 16:53:05 -0500 | [diff] [blame] | 591 | } |
| 592 | }); |
| 593 | |
Saqib Ansari | f53299e | 2020-04-15 22:08:12 +0530 | [diff] [blame] | 594 | const fields = [{ |
| 595 | fieldtype:'Data', |
| 596 | fieldname:"docname", |
| 597 | read_only: 1, |
| 598 | hidden: 1, |
| 599 | }, { |
| 600 | fieldtype:'Link', |
| 601 | fieldname:"item_code", |
| 602 | options: 'Item', |
| 603 | in_list_view: 1, |
| 604 | read_only: 0, |
| 605 | disabled: 0, |
Afshan | dc7280e | 2021-08-18 17:44:40 +0530 | [diff] [blame] | 606 | label: __('Item Code'), |
| 607 | get_query: function() { |
| 608 | let filters; |
| 609 | if (frm.doc.doctype == 'Sales Order') { |
| 610 | filters = {"is_sales_item": 1}; |
| 611 | } else if (frm.doc.doctype == 'Purchase Order') { |
Sagar Sharma | d074c93 | 2022-03-31 19:57:42 +0530 | [diff] [blame] | 612 | if (frm.doc.is_subcontracted) { |
s-aga-r | 6d89b2f | 2022-06-18 15:46:59 +0530 | [diff] [blame] | 613 | if (frm.doc.is_old_subcontracting_flow) { |
| 614 | filters = {"is_sub_contracted_item": 1}; |
| 615 | } else { |
| 616 | filters = {"is_stock_item": 0}; |
| 617 | } |
Afshan | dc7280e | 2021-08-18 17:44:40 +0530 | [diff] [blame] | 618 | } else { |
| 619 | filters = {"is_purchase_item": 1}; |
| 620 | } |
| 621 | } |
| 622 | return { |
| 623 | query: "erpnext.controllers.queries.item_query", |
| 624 | filters: filters |
| 625 | }; |
| 626 | } |
Saqib Ansari | f53299e | 2020-04-15 22:08:12 +0530 | [diff] [blame] | 627 | }, { |
Saqib | 6131424 | 2020-09-15 11:14:31 +0530 | [diff] [blame] | 628 | fieldtype:'Link', |
| 629 | fieldname:'uom', |
| 630 | options: 'UOM', |
| 631 | read_only: 0, |
| 632 | label: __('UOM'), |
| 633 | reqd: 1, |
| 634 | onchange: function () { |
| 635 | frappe.call({ |
| 636 | method: "erpnext.stock.get_item_details.get_conversion_factor", |
| 637 | args: { item_code: this.doc.item_code, uom: this.value }, |
| 638 | callback: r => { |
| 639 | if(!r.exc) { |
| 640 | if (this.doc.conversion_factor == r.message.conversion_factor) return; |
marination | 4f395cc | 2020-09-24 12:43:41 +0530 | [diff] [blame] | 641 | |
Saqib | 6131424 | 2020-09-15 11:14:31 +0530 | [diff] [blame] | 642 | const docname = this.doc.docname; |
| 643 | dialog.fields_dict.trans_items.df.data.some(doc => { |
| 644 | if (doc.docname == docname) { |
| 645 | doc.conversion_factor = r.message.conversion_factor; |
| 646 | dialog.fields_dict.trans_items.grid.refresh(); |
| 647 | return true; |
| 648 | } |
| 649 | }) |
| 650 | } |
| 651 | } |
| 652 | }); |
| 653 | } |
| 654 | }, { |
Saqib Ansari | f53299e | 2020-04-15 22:08:12 +0530 | [diff] [blame] | 655 | fieldtype:'Float', |
| 656 | fieldname:"qty", |
| 657 | default: 0, |
| 658 | read_only: 0, |
| 659 | in_list_view: 1, |
Saqib | 56fea7d | 2020-10-09 21:19:25 +0530 | [diff] [blame] | 660 | label: __('Qty'), |
| 661 | precision: get_precision("qty") |
Saqib Ansari | f53299e | 2020-04-15 22:08:12 +0530 | [diff] [blame] | 662 | }, { |
| 663 | fieldtype:'Currency', |
| 664 | fieldname:"rate", |
Afshan | 8418455 | 2021-02-24 16:51:11 +0530 | [diff] [blame] | 665 | options: "currency", |
Saqib Ansari | f53299e | 2020-04-15 22:08:12 +0530 | [diff] [blame] | 666 | default: 0, |
| 667 | read_only: 0, |
| 668 | in_list_view: 1, |
Saqib | 56fea7d | 2020-10-09 21:19:25 +0530 | [diff] [blame] | 669 | label: __('Rate'), |
| 670 | precision: get_precision("rate") |
Saqib Ansari | f53299e | 2020-04-15 22:08:12 +0530 | [diff] [blame] | 671 | }]; |
| 672 | |
| 673 | if (frm.doc.doctype == 'Sales Order' || frm.doc.doctype == 'Purchase Order' ) { |
| 674 | fields.splice(2, 0, { |
| 675 | fieldtype: 'Date', |
| 676 | fieldname: frm.doc.doctype == 'Sales Order' ? "delivery_date" : "schedule_date", |
| 677 | in_list_view: 1, |
Saqib Ansari | c579b08 | 2020-05-29 22:21:50 +0530 | [diff] [blame] | 678 | label: frm.doc.doctype == 'Sales Order' ? __("Delivery Date") : __("Reqd by date"), |
Devin Slauenwhite | c86dece | 2023-12-08 19:57:34 +0000 | [diff] [blame] | 679 | default: frm.doc.doctype == 'Sales Order' ? frm.doc.delivery_date : frm.doc.schedule_date, |
Saqib Ansari | c579b08 | 2020-05-29 22:21:50 +0530 | [diff] [blame] | 680 | reqd: 1 |
Saqib Ansari | f53299e | 2020-04-15 22:08:12 +0530 | [diff] [blame] | 681 | }) |
Saqib Ansari | 2c3c8aa | 2020-05-29 21:55:38 +0530 | [diff] [blame] | 682 | fields.splice(3, 0, { |
| 683 | fieldtype: 'Float', |
| 684 | fieldname: "conversion_factor", |
Saqib | 56fea7d | 2020-10-09 21:19:25 +0530 | [diff] [blame] | 685 | label: __("Conversion Factor"), |
| 686 | precision: get_precision('conversion_factor') |
Saqib Ansari | 2c3c8aa | 2020-05-29 21:55:38 +0530 | [diff] [blame] | 687 | }) |
Saqib Ansari | f53299e | 2020-04-15 22:08:12 +0530 | [diff] [blame] | 688 | } |
| 689 | |
s-aga-r | faf9f13 | 2023-08-21 15:51:57 +0530 | [diff] [blame] | 690 | if (frm.doc.doctype == 'Purchase Order' && frm.doc.is_subcontracted && !frm.doc.is_old_subcontracting_flow) { |
| 691 | fields.push({ |
| 692 | fieldtype:'Link', |
| 693 | fieldname:'fg_item', |
| 694 | options: 'Item', |
| 695 | reqd: 1, |
| 696 | in_list_view: 0, |
| 697 | read_only: 0, |
| 698 | disabled: 0, |
| 699 | label: __('Finished Good Item'), |
| 700 | get_query: () => { |
| 701 | return { |
| 702 | filters: { |
| 703 | 'is_stock_item': 1, |
| 704 | 'is_sub_contracted_item': 1, |
| 705 | 'default_bom': ['!=', ''] |
| 706 | } |
| 707 | } |
| 708 | }, |
| 709 | }, { |
| 710 | fieldtype:'Float', |
| 711 | fieldname:'fg_item_qty', |
| 712 | reqd: 1, |
| 713 | default: 0, |
| 714 | read_only: 0, |
| 715 | in_list_view: 0, |
| 716 | label: __('Finished Good Item Qty'), |
| 717 | precision: get_precision('fg_item_qty') |
| 718 | }) |
| 719 | } |
| 720 | |
Deepesh Garg | 4077254 | 2023-07-22 23:07:18 +0530 | [diff] [blame] | 721 | let dialog = new frappe.ui.Dialog({ |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 722 | title: __("Update Items"), |
Raffael Meyer | d5fe143 | 2023-07-14 08:57:35 +0200 | [diff] [blame] | 723 | size: "extra-large", |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 724 | fields: [ |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 725 | { |
Stavros Anastasiadis | 3d82b74 | 2018-12-10 13:00:55 +0100 | [diff] [blame] | 726 | fieldname: "trans_items", |
| 727 | fieldtype: "Table", |
Marica | fc96c1a | 2020-03-06 10:57:59 +0530 | [diff] [blame] | 728 | label: "Items", |
Stavros Anastasiadis | 3d82b74 | 2018-12-10 13:00:55 +0100 | [diff] [blame] | 729 | cannot_add_rows: cannot_add_row, |
Saqib | bc919e2 | 2020-11-05 17:38:35 +0530 | [diff] [blame] | 730 | in_place_edit: false, |
Marica | fc96c1a | 2020-03-06 10:57:59 +0530 | [diff] [blame] | 731 | reqd: 1, |
Stavros Anastasiadis | 3d82b74 | 2018-12-10 13:00:55 +0100 | [diff] [blame] | 732 | data: this.data, |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 733 | get_data: () => { |
| 734 | return this.data; |
| 735 | }, |
Saqib Ansari | f53299e | 2020-04-15 22:08:12 +0530 | [diff] [blame] | 736 | fields: fields |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 737 | }, |
| 738 | ], |
| 739 | primary_action: function() { |
s-aga-r | 2d8363a | 2023-09-02 11:02:24 +0530 | [diff] [blame] | 740 | if (frm.doctype == "Sales Order" && has_reserved_stock) { |
| 741 | this.hide(); |
| 742 | frappe.confirm( |
| 743 | __('The reserved stock will be released when you update items. Are you certain you wish to proceed?'), |
| 744 | () => this.update_items(), |
| 745 | ) |
| 746 | } else { |
| 747 | this.update_items(); |
| 748 | } |
| 749 | }, |
| 750 | update_items: function() { |
Ankush Menat | 6de7b8e | 2021-08-24 12:18:40 +0530 | [diff] [blame] | 751 | const trans_items = this.get_values()["trans_items"].filter((item) => !!item.item_code); |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 752 | frappe.call({ |
| 753 | method: 'erpnext.controllers.accounts_controller.update_child_qty_rate', |
Stavros Anastasiadis | 3d82b74 | 2018-12-10 13:00:55 +0100 | [diff] [blame] | 754 | freeze: true, |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 755 | args: { |
| 756 | 'parent_doctype': frm.doc.doctype, |
| 757 | 'trans_items': trans_items, |
Stavros Anastasiadis | 3d82b74 | 2018-12-10 13:00:55 +0100 | [diff] [blame] | 758 | 'parent_doctype_name': frm.doc.name, |
| 759 | 'child_docname': child_docname |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 760 | }, |
| 761 | callback: function() { |
| 762 | frm.reload_doc(); |
| 763 | } |
| 764 | }); |
| 765 | this.hide(); |
| 766 | refresh_field("items"); |
| 767 | }, |
| 768 | primary_action_label: __('Update') |
Deepesh Garg | 4077254 | 2023-07-22 23:07:18 +0530 | [diff] [blame] | 769 | }) |
| 770 | |
| 771 | dialog.show(); |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 772 | } |
| 773 | |
ruthra kumar | 5981c7e | 2023-08-29 13:19:26 +0530 | [diff] [blame] | 774 | |
ruthra kumar | 5981c7e | 2023-08-29 13:19:26 +0530 | [diff] [blame] | 775 | |
ruthra kumar | 5981c7e | 2023-08-29 13:19:26 +0530 | [diff] [blame] | 776 | |
Rushabh Mehta | 176b63b | 2016-07-14 17:43:48 +0530 | [diff] [blame] | 777 | erpnext.utils.map_current_doc = function(opts) { |
Sagar Vora | 78777d6 | 2021-03-09 20:35:08 +0530 | [diff] [blame] | 778 | function _map() { |
Rushabh Mehta | 55ea7b1 | 2016-07-15 16:43:25 +0530 | [diff] [blame] | 779 | if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) { |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 780 | // remove first item row if empty |
Rushabh Mehta | 176b63b | 2016-07-14 17:43:48 +0530 | [diff] [blame] | 781 | if(!cur_frm.doc.items[0].item_code) { |
| 782 | cur_frm.doc.items = cur_frm.doc.items.splice(1); |
| 783 | } |
bhupen | 3c7e70f | 2016-11-30 14:32:11 +0530 | [diff] [blame] | 784 | |
| 785 | // find the doctype of the items table |
| 786 | var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options; |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 787 | |
bhupen | 3c7e70f | 2016-11-30 14:32:11 +0530 | [diff] [blame] | 788 | // find the link fieldname from items table for the given |
| 789 | // source_doctype |
| 790 | var link_fieldname = null; |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 791 | frappe.get_meta(items_doctype).fields.forEach(function(d) { |
bhupen | 3c7e70f | 2016-11-30 14:32:11 +0530 | [diff] [blame] | 792 | if(d.options===opts.source_doctype) link_fieldname = d.fieldname; }); |
| 793 | |
Nabin Hait | 802b435 | 2017-01-09 15:32:20 +0530 | [diff] [blame] | 794 | // search in existing items if the source_name is already set and full qty fetched |
bhupen | 3c7e70f | 2016-11-30 14:32:11 +0530 | [diff] [blame] | 795 | var already_set = false; |
Nabin Hait | 802b435 | 2017-01-09 15:32:20 +0530 | [diff] [blame] | 796 | var item_qty_map = {}; |
bhupen | 3c7e70f | 2016-11-30 14:32:11 +0530 | [diff] [blame] | 797 | |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 798 | $.each(cur_frm.doc.items, function(i, d) { |
| 799 | opts.source_name.forEach(function(src) { |
| 800 | if(d[link_fieldname]==src) { |
| 801 | already_set = true; |
| 802 | if (item_qty_map[d.item_code]) |
| 803 | item_qty_map[d.item_code] += flt(d.qty); |
| 804 | else |
| 805 | item_qty_map[d.item_code] = flt(d.qty); |
| 806 | } |
| 807 | }); |
| 808 | }); |
| 809 | |
| 810 | if(already_set) { |
| 811 | opts.source_name.forEach(function(src) { |
| 812 | frappe.model.with_doc(opts.source_doctype, src, function(r) { |
| 813 | var source_doc = frappe.model.get_doc(opts.source_doctype, src); |
| 814 | $.each(source_doc.items || [], function(i, row) { |
| 815 | if(row.qty > flt(item_qty_map[row.item_code])) { |
| 816 | already_set = false; |
| 817 | return false; |
| 818 | } |
| 819 | }) |
| 820 | }) |
| 821 | |
| 822 | if(already_set) { |
| 823 | frappe.msgprint(__("You have already selected items from {0} {1}", |
| 824 | [opts.source_doctype, src])); |
| 825 | return; |
| 826 | } |
| 827 | |
| 828 | }) |
Nabin Hait | 802b435 | 2017-01-09 15:32:20 +0530 | [diff] [blame] | 829 | } |
Rushabh Mehta | 176b63b | 2016-07-14 17:43:48 +0530 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | return frappe.call({ |
| 833 | // Sometimes we hit the limit for URL length of a GET request |
| 834 | // as we send the full target_doc. Hence this is a POST request. |
| 835 | type: "POST", |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 836 | method: 'frappe.model.mapper.map_docs', |
Rushabh Mehta | 176b63b | 2016-07-14 17:43:48 +0530 | [diff] [blame] | 837 | args: { |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 838 | "method": opts.method, |
| 839 | "source_names": opts.source_name, |
| 840 | "target_doc": cur_frm.doc, |
Marica | db00270 | 2020-02-17 15:58:08 +0530 | [diff] [blame] | 841 | "args": opts.args |
Rushabh Mehta | 176b63b | 2016-07-14 17:43:48 +0530 | [diff] [blame] | 842 | }, |
s-aga-r | 2d8363a | 2023-09-02 11:02:24 +0530 | [diff] [blame] | 843 | freeze: true, |
| 844 | freeze_message: __("Mapping {0} ...", [opts.source_doctype]), |
Rushabh Mehta | 176b63b | 2016-07-14 17:43:48 +0530 | [diff] [blame] | 845 | callback: function(r) { |
| 846 | if(!r.exc) { |
Deepesh Garg | 9b1c222 | 2023-12-27 22:37:01 +0530 | [diff] [blame] | 847 | frappe.model.sync(r.message); |
Vishal Dhayagude | f06c281 | 2017-12-26 16:22:40 +0530 | [diff] [blame] | 848 | cur_frm.dirty(); |
Rushabh Mehta | 176b63b | 2016-07-14 17:43:48 +0530 | [diff] [blame] | 849 | cur_frm.refresh(); |
| 850 | } |
| 851 | } |
| 852 | }); |
| 853 | } |
Sagar Vora | 78777d6 | 2021-03-09 20:35:08 +0530 | [diff] [blame] | 854 | |
| 855 | let query_args = {}; |
| 856 | if (opts.get_query_filters) { |
| 857 | query_args.filters = opts.get_query_filters; |
| 858 | } |
| 859 | |
| 860 | if (opts.get_query_method) { |
| 861 | query_args.query = opts.get_query_method; |
| 862 | } |
| 863 | |
| 864 | if (query_args.filters || query_args.query) { |
| 865 | opts.get_query = () => query_args; |
| 866 | } |
| 867 | |
| 868 | if (opts.source_doctype) { |
ruthra kumar | 7da9ffa | 2024-01-03 17:59:15 +0530 | [diff] [blame] | 869 | let data_fields = []; |
| 870 | if(opts.source_doctype == "Purchase Receipt") { |
| 871 | data_fields.push({ |
| 872 | fieldname: 'merge_taxes', |
| 873 | fieldtype: 'Check', |
| 874 | label: __('Merge taxes from multiple documents'), |
| 875 | }); |
| 876 | } |
Sagar Vora | 78777d6 | 2021-03-09 20:35:08 +0530 | [diff] [blame] | 877 | const d = new frappe.ui.form.MultiSelectDialog({ |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 878 | doctype: opts.source_doctype, |
| 879 | target: opts.target, |
| 880 | date_field: opts.date_field || undefined, |
| 881 | setters: opts.setters, |
ruthra kumar | 7da9ffa | 2024-01-03 17:59:15 +0530 | [diff] [blame] | 882 | data_fields: data_fields, |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 883 | get_query: opts.get_query, |
marination | 4f395cc | 2020-09-24 12:43:41 +0530 | [diff] [blame] | 884 | add_filters_group: 1, |
Saqib | 7292f54 | 2021-09-13 12:13:43 +0530 | [diff] [blame] | 885 | allow_child_item_selection: opts.allow_child_item_selection, |
gn306029 | 2a619fd | 2022-06-29 17:15:21 +0800 | [diff] [blame] | 886 | child_fieldname: opts.child_fieldname, |
Saqib | 7292f54 | 2021-09-13 12:13:43 +0530 | [diff] [blame] | 887 | child_columns: opts.child_columns, |
Rohit Waghchaure | 69ffddf | 2021-10-07 17:33:58 +0530 | [diff] [blame] | 888 | size: opts.size, |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 889 | action: function(selections, args) { |
| 890 | let values = selections; |
Saqib | e03d9aa | 2021-09-17 13:03:27 +0530 | [diff] [blame] | 891 | if (values.length === 0) { |
Nabin Hait | a11dcb6 | 2017-12-04 13:36:50 +0530 | [diff] [blame] | 892 | frappe.msgprint(__("Please select {0}", [opts.source_doctype])) |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 893 | return; |
| 894 | } |
| 895 | opts.source_name = values; |
ruthra kumar | 7da9ffa | 2024-01-03 17:59:15 +0530 | [diff] [blame] | 896 | if (opts.allow_child_item_selection || opts.source_doctype == "Purchase Receipt") { |
| 897 | // args contains filtered child docnames |
| 898 | opts.args = args; |
| 899 | } |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 900 | d.dialog.hide(); |
| 901 | _map(); |
| 902 | }, |
Rushabh Mehta | 176b63b | 2016-07-14 17:43:48 +0530 | [diff] [blame] | 903 | }); |
Sagar Vora | 78777d6 | 2021-03-09 20:35:08 +0530 | [diff] [blame] | 904 | |
| 905 | return d; |
| 906 | } |
| 907 | |
| 908 | if (opts.source_name) { |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 909 | opts.source_name = [opts.source_name]; |
Rushabh Mehta | 176b63b | 2016-07-14 17:43:48 +0530 | [diff] [blame] | 910 | _map(); |
| 911 | } |
| 912 | } |
| 913 | |
Rushabh Mehta | 180e435 | 2016-07-26 17:57:42 +0530 | [diff] [blame] | 914 | frappe.form.link_formatters['Item'] = function(value, doc) { |
Alan | 157b388 | 2021-04-21 21:00:22 +0530 | [diff] [blame] | 915 | if (doc && value && doc.item_name && doc.item_name !== value && doc.item_code === value) { |
marination | 824f48f | 2020-10-12 20:08:03 +0530 | [diff] [blame] | 916 | return value + ': ' + doc.item_name; |
| 917 | } else if (!value && doc.doctype && doc.item_name) { |
| 918 | // format blank value in child table |
| 919 | return doc.item_name; |
Rushabh Mehta | 180e435 | 2016-07-26 17:57:42 +0530 | [diff] [blame] | 920 | } else { |
marination | 824f48f | 2020-10-12 20:08:03 +0530 | [diff] [blame] | 921 | // if value is blank in report view or item code and name are the same, return as is |
Rushabh Mehta | 180e435 | 2016-07-26 17:57:42 +0530 | [diff] [blame] | 922 | return value; |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | frappe.form.link_formatters['Employee'] = function(value, doc) { |
Rucha Mahabal | 5ba1bc1 | 2021-11-23 09:30:30 +0530 | [diff] [blame] | 927 | if (doc && value && doc.employee_name && doc.employee_name !== value && doc.employee === value) { |
| 928 | return value + ': ' + doc.employee_name; |
| 929 | } else if (!value && doc.doctype && doc.employee_name) { |
| 930 | // format blank value in child table |
| 931 | return doc.employee; |
Rushabh Mehta | 180e435 | 2016-07-26 17:57:42 +0530 | [diff] [blame] | 932 | } else { |
Rucha Mahabal | 5ba1bc1 | 2021-11-23 09:30:30 +0530 | [diff] [blame] | 933 | // if value is blank in report view or project name and name are the same, return as is |
Rushabh Mehta | 180e435 | 2016-07-26 17:57:42 +0530 | [diff] [blame] | 934 | return value; |
| 935 | } |
| 936 | } |
| 937 | |
Rucha Mahabal | 59961f7 | 2021-05-20 23:43:19 +0530 | [diff] [blame] | 938 | frappe.form.link_formatters['Project'] = function(value, doc) { |
| 939 | if (doc && value && doc.project_name && doc.project_name !== value && doc.project === value) { |
| 940 | return value + ': ' + doc.project_name; |
| 941 | } else if (!value && doc.doctype && doc.project_name) { |
| 942 | // format blank value in child table |
| 943 | return doc.project; |
| 944 | } else { |
| 945 | // if value is blank in report view or project name and name are the same, return as is |
| 946 | return value; |
| 947 | } |
| 948 | }; |
| 949 | |
Rushabh Mehta | 95b995b | 2015-05-28 12:10:55 +0530 | [diff] [blame] | 950 | // add description on posting time |
| 951 | $(document).on('app_ready', function() { |
| 952 | if(!frappe.datetime.is_timezone_same()) { |
| 953 | $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry", |
| 954 | "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) { |
| 955 | frappe.ui.form.on(d, "onload", function(frm) { |
| 956 | cur_frm.set_df_property("posting_time", "description", |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 957 | frappe.sys_defaults.time_zone); |
Rushabh Mehta | 95b995b | 2015-05-28 12:10:55 +0530 | [diff] [blame] | 958 | }); |
| 959 | }); |
| 960 | } |
akshay | 14384c2 | 2016-07-28 13:53:17 +0530 | [diff] [blame] | 961 | }); |
Alan | c6dc9ea | 2021-05-07 20:30:04 +0530 | [diff] [blame] | 962 | |
Himanshu | ec25d59 | 2021-06-14 19:05:52 +0530 | [diff] [blame] | 963 | // Show SLA dashboard |
| 964 | $(document).on('app_ready', function() { |
Ankush Menat | bd9ef74 | 2023-07-16 11:34:42 +0530 | [diff] [blame] | 965 | $.each(frappe.boot.service_level_agreement_doctypes, function(_i, d) { |
| 966 | frappe.ui.form.on(d, { |
| 967 | onload: function(frm) { |
| 968 | if (!frm.doc.service_level_agreement) |
| 969 | return; |
Himanshu | ec25d59 | 2021-06-14 19:05:52 +0530 | [diff] [blame] | 970 | |
Ankush Menat | bd9ef74 | 2023-07-16 11:34:42 +0530 | [diff] [blame] | 971 | frappe.call({ |
| 972 | method: 'erpnext.support.doctype.service_level_agreement.service_level_agreement.get_service_level_agreement_filters', |
| 973 | args: { |
| 974 | doctype: frm.doc.doctype, |
| 975 | name: frm.doc.service_level_agreement, |
| 976 | customer: frm.doc.customer |
Himanshu | ec25d59 | 2021-06-14 19:05:52 +0530 | [diff] [blame] | 977 | }, |
Ankush Menat | bd9ef74 | 2023-07-16 11:34:42 +0530 | [diff] [blame] | 978 | callback: function (r) { |
| 979 | if (r && r.message) { |
| 980 | frm.set_query('priority', function() { |
| 981 | return { |
| 982 | filters: { |
| 983 | 'name': ['in', r.message.priority], |
Himanshu | ec25d59 | 2021-06-14 19:05:52 +0530 | [diff] [blame] | 984 | } |
Ankush Menat | bd9ef74 | 2023-07-16 11:34:42 +0530 | [diff] [blame] | 985 | }; |
Himanshu | ec25d59 | 2021-06-14 19:05:52 +0530 | [diff] [blame] | 986 | }); |
Ankush Menat | bd9ef74 | 2023-07-16 11:34:42 +0530 | [diff] [blame] | 987 | frm.set_query('service_level_agreement', function() { |
| 988 | return { |
| 989 | filters: { |
| 990 | 'name': ['in', r.message.service_level_agreements], |
| 991 | } |
| 992 | }; |
| 993 | }); |
Himanshu | ec25d59 | 2021-06-14 19:05:52 +0530 | [diff] [blame] | 994 | } |
Ankush Menat | bd9ef74 | 2023-07-16 11:34:42 +0530 | [diff] [blame] | 995 | } |
Himanshu | ec25d59 | 2021-06-14 19:05:52 +0530 | [diff] [blame] | 996 | }); |
Ankush Menat | bd9ef74 | 2023-07-16 11:34:42 +0530 | [diff] [blame] | 997 | }, |
| 998 | |
| 999 | refresh: function(frm) { |
| 1000 | if (frm.doc.status !== 'Closed' && frm.doc.service_level_agreement |
| 1001 | && ['First Response Due', 'Resolution Due'].includes(frm.doc.agreement_status)) { |
| 1002 | frappe.call({ |
| 1003 | 'method': 'frappe.client.get', |
| 1004 | args: { |
| 1005 | doctype: 'Service Level Agreement', |
| 1006 | name: frm.doc.service_level_agreement |
| 1007 | }, |
| 1008 | callback: function(data) { |
| 1009 | let statuses = data.message.pause_sla_on; |
| 1010 | const hold_statuses = []; |
| 1011 | $.each(statuses, (_i, entry) => { |
| 1012 | hold_statuses.push(entry.status); |
| 1013 | }); |
| 1014 | if (hold_statuses.includes(frm.doc.status)) { |
| 1015 | frm.dashboard.clear_headline(); |
| 1016 | let message = {'indicator': 'orange', 'msg': __('SLA is on hold since {0}', [moment(frm.doc.on_hold_since).fromNow(true)])}; |
| 1017 | frm.dashboard.set_headline_alert( |
| 1018 | '<div class="row">' + |
| 1019 | '<div class="col-xs-12">' + |
| 1020 | '<span class="indicator whitespace-nowrap '+ message.indicator +'"><span>'+ message.msg +'</span></span> ' + |
| 1021 | '</div>' + |
| 1022 | '</div>' |
| 1023 | ); |
| 1024 | } else { |
| 1025 | set_time_to_resolve_and_response(frm, data.message.apply_sla_for_resolution); |
| 1026 | } |
| 1027 | } |
| 1028 | }); |
| 1029 | } else if (frm.doc.service_level_agreement) { |
| 1030 | frm.dashboard.clear_headline(); |
| 1031 | |
| 1032 | let agreement_status = (frm.doc.agreement_status == 'Fulfilled') ? |
| 1033 | {'indicator': 'green', 'msg': 'Service Level Agreement has been fulfilled'} : |
| 1034 | {'indicator': 'red', 'msg': 'Service Level Agreement Failed'}; |
| 1035 | |
| 1036 | frm.dashboard.set_headline_alert( |
| 1037 | '<div class="row">' + |
| 1038 | '<div class="col-xs-12">' + |
| 1039 | '<span class="indicator whitespace-nowrap '+ agreement_status.indicator +'"><span class="hidden-xs">'+ agreement_status.msg +'</span></span> ' + |
| 1040 | '</div>' + |
| 1041 | '</div>' |
| 1042 | ); |
| 1043 | } |
| 1044 | }, |
| 1045 | }); |
Himanshu | ec25d59 | 2021-06-14 19:05:52 +0530 | [diff] [blame] | 1046 | }); |
| 1047 | }); |
| 1048 | |
| 1049 | function set_time_to_resolve_and_response(frm, apply_sla_for_resolution) { |
| 1050 | frm.dashboard.clear_headline(); |
| 1051 | |
Saqib | 2411477 | 2021-12-15 11:45:34 +0530 | [diff] [blame] | 1052 | let time_to_respond; |
Saqib Ansari | 476e81a | 2021-12-06 18:55:30 +0530 | [diff] [blame] | 1053 | if (!frm.doc.first_responded_on) { |
Himanshu | ec25d59 | 2021-06-14 19:05:52 +0530 | [diff] [blame] | 1054 | time_to_respond = get_time_left(frm.doc.response_by, frm.doc.agreement_status); |
Saqib | 2411477 | 2021-12-15 11:45:34 +0530 | [diff] [blame] | 1055 | } else { |
| 1056 | time_to_respond = get_status(frm.doc.response_by, frm.doc.first_responded_on); |
Himanshu | ec25d59 | 2021-06-14 19:05:52 +0530 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | let alert = ` |
| 1060 | <div class="row"> |
| 1061 | <div class="col-xs-12 col-sm-6"> |
| 1062 | <span class="indicator whitespace-nowrap ${time_to_respond.indicator}"> |
| 1063 | <span>Time to Respond: ${time_to_respond.diff_display}</span> |
| 1064 | </span> |
| 1065 | </div>`; |
| 1066 | |
| 1067 | |
| 1068 | if (apply_sla_for_resolution) { |
Saqib | 2411477 | 2021-12-15 11:45:34 +0530 | [diff] [blame] | 1069 | let time_to_resolve; |
Saqib Ansari | 476e81a | 2021-12-06 18:55:30 +0530 | [diff] [blame] | 1070 | if (!frm.doc.resolution_date) { |
Himanshu | ec25d59 | 2021-06-14 19:05:52 +0530 | [diff] [blame] | 1071 | time_to_resolve = get_time_left(frm.doc.resolution_by, frm.doc.agreement_status); |
Saqib | 2411477 | 2021-12-15 11:45:34 +0530 | [diff] [blame] | 1072 | } else { |
| 1073 | time_to_resolve = get_status(frm.doc.resolution_by, frm.doc.resolution_date); |
Himanshu | ec25d59 | 2021-06-14 19:05:52 +0530 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | alert += ` |
| 1077 | <div class="col-xs-12 col-sm-6"> |
| 1078 | <span class="indicator whitespace-nowrap ${time_to_resolve.indicator}"> |
| 1079 | <span>Time to Resolve: ${time_to_resolve.diff_display}</span> |
| 1080 | </span> |
| 1081 | </div>`; |
| 1082 | } |
| 1083 | |
| 1084 | alert += '</div>'; |
| 1085 | |
| 1086 | frm.dashboard.set_headline_alert(alert); |
| 1087 | } |
| 1088 | |
| 1089 | function get_time_left(timestamp, agreement_status) { |
Ankush Menat | eaf86a6 | 2023-12-14 15:53:57 +0530 | [diff] [blame] | 1090 | const diff = moment(timestamp).diff(frappe.datetime.system_datetime(true)); |
Himanshu | ec25d59 | 2021-06-14 19:05:52 +0530 | [diff] [blame] | 1091 | const diff_display = diff >= 44500 ? moment.duration(diff).humanize() : 'Failed'; |
| 1092 | let indicator = (diff_display == 'Failed' && agreement_status != 'Fulfilled') ? 'red' : 'green'; |
| 1093 | return {'diff_display': diff_display, 'indicator': indicator}; |
| 1094 | } |
| 1095 | |
Saqib | 2411477 | 2021-12-15 11:45:34 +0530 | [diff] [blame] | 1096 | function get_status(expected, actual) { |
| 1097 | const time_left = moment(expected).diff(moment(actual)); |
Saqib Ansari | 476e81a | 2021-12-06 18:55:30 +0530 | [diff] [blame] | 1098 | if (time_left >= 0) { |
Himanshu | ec25d59 | 2021-06-14 19:05:52 +0530 | [diff] [blame] | 1099 | return {'diff_display': 'Fulfilled', 'indicator': 'green'}; |
| 1100 | } else { |
| 1101 | return {'diff_display': 'Failed', 'indicator': 'red'}; |
| 1102 | } |
| 1103 | } |
| 1104 | |
Alan | c6dc9ea | 2021-05-07 20:30:04 +0530 | [diff] [blame] | 1105 | function attach_selector_button(inner_text, append_loction, context, grid_row) { |
| 1106 | let $btn_div = $("<div>").css({"margin-bottom": "10px", "margin-top": "10px"}) |
| 1107 | .appendTo(append_loction); |
| 1108 | let $btn = $(`<button class="btn btn-sm btn-default">${inner_text}</button>`) |
| 1109 | .appendTo($btn_div); |
| 1110 | |
| 1111 | $btn.on("click", function() { |
| 1112 | context.show_serial_batch_selector(grid_row.frm, grid_row.doc, "", "", true); |
| 1113 | }); |
ruthra kumar | 5981c7e | 2023-08-29 13:19:26 +0530 | [diff] [blame] | 1114 | } |