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