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 | |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 51 | setup_serial_no: function() { |
| 52 | var grid_row = cur_frm.open_grid_row(); |
akshay | 14384c2 | 2016-07-28 13:53:17 +0530 | [diff] [blame] | 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 | |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 56 | var $btn = $('<button class="btn btn-sm btn-default">'+__("Add Serial No")+'</button>') |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 57 | .appendTo($("<div>") |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 58 | .css({"margin-bottom": "10px", "margin-top": "10px"}) |
akshay | 14384c2 | 2016-07-28 13:53:17 +0530 | [diff] [blame] | 59 | .appendTo(grid_row.grid_form.fields_dict.serial_no.$wrapper)); |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 60 | |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 61 | $btn.on("click", function() { |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 62 | var d = new frappe.ui.Dialog({ |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 63 | title: __("Add Serial No"), |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 64 | fields: [ |
| 65 | { |
| 66 | "fieldtype": "Link", |
Rohit Waghchaure | 2087e57 | 2017-02-27 13:43:04 +0530 | [diff] [blame] | 67 | "fieldname": "serial_no", |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 68 | "options": "Serial No", |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 69 | "label": __("Serial No"), |
Neil Trini Lasrado | 3d61bbb | 2015-03-12 12:51:50 +0530 | [diff] [blame] | 70 | "get_query": function () { |
Rushabh Mehta | 45ca8a4 | 2015-04-28 16:02:09 +0530 | [diff] [blame] | 71 | return { |
| 72 | filters: { |
Shreya | 14bd43d | 2018-04-27 16:24:05 +0530 | [diff] [blame] | 73 | item_code:grid_row.doc.item_code, |
Nabin Hait | b7c0c55 | 2015-08-04 12:18:12 +0530 | [diff] [blame] | 74 | warehouse:cur_frm.doc.is_return ? null : grid_row.doc.warehouse |
Neil Trini Lasrado | 3d61bbb | 2015-03-12 12:51:50 +0530 | [diff] [blame] | 75 | } |
| 76 | } |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 77 | } |
| 78 | }, |
| 79 | { |
| 80 | "fieldtype": "Button", |
Kanchan Chauhan | 6981870 | 2017-02-27 12:53:05 +0530 | [diff] [blame] | 81 | "fieldname": "add", |
Pratik Vyas | b52618c | 2014-04-14 16:25:30 +0530 | [diff] [blame] | 82 | "label": __("Add") |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 83 | } |
| 84 | ] |
| 85 | }); |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 86 | |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 87 | d.get_input("add").on("click", function() { |
| 88 | var serial_no = d.get_value("serial_no"); |
| 89 | if(serial_no) { |
| 90 | var val = (grid_row.doc.serial_no || "").split("\n").concat([serial_no]).join("\n"); |
akshay | 14384c2 | 2016-07-28 13:53:17 +0530 | [diff] [blame] | 91 | grid_row.grid_form.fields_dict.serial_no.set_model_value(val.trim()); |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 92 | } |
| 93 | d.hide(); |
| 94 | return false; |
| 95 | }); |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 96 | |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 97 | d.show(); |
| 98 | }); |
Rushabh Mehta | 5495bc5 | 2015-05-19 12:00:34 +0530 | [diff] [blame] | 99 | } |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 100 | }); |
Rushabh Mehta | 7d36875 | 2014-11-24 14:16:47 +0530 | [diff] [blame] | 101 | |
Nabin Hait | b072c74 | 2014-12-16 12:49:13 +0530 | [diff] [blame] | 102 | |
| 103 | $.extend(erpnext.utils, { |
Rushabh Mehta | 559aa3a | 2016-09-19 16:04:58 +0530 | [diff] [blame] | 104 | set_party_dashboard_indicators: function(frm) { |
| 105 | if(frm.doc.__onload && frm.doc.__onload.dashboard_info) { |
deepeshgarg007 | 920dc14 | 2018-11-23 10:17:28 +0530 | [diff] [blame] | 106 | var company_wise_info = frm.doc.__onload.dashboard_info; |
deepeshgarg007 | f31caff | 2018-11-27 15:04:12 +0530 | [diff] [blame] | 107 | if(company_wise_info.length > 1) { |
deepeshgarg007 | a1cffc3 | 2018-11-23 16:51:35 +0530 | [diff] [blame] | 108 | company_wise_info.forEach(function(info) { |
deepeshgarg007 | 12f5cef | 2018-12-25 16:06:19 +0530 | [diff] [blame] | 109 | erpnext.utils.add_indicator_for_multicompany(frm, info); |
deepeshgarg007 | a1cffc3 | 2018-11-23 16:51:35 +0530 | [diff] [blame] | 110 | }); |
deepeshgarg007 | 12f5cef | 2018-12-25 16:06:19 +0530 | [diff] [blame] | 111 | } else if (company_wise_info.length === 1) { |
deepeshgarg007 | 64238ee | 2018-12-25 16:28:39 +0530 | [diff] [blame] | 112 | frm.dashboard.add_indicator(__('Annual Billing: {0}', |
| 113 | [format_currency(company_wise_info[0].billing_this_year, company_wise_info[0].currency)]), 'blue'); |
| 114 | frm.dashboard.add_indicator(__('Total Unpaid: {0}', |
| 115 | [format_currency(company_wise_info[0].total_unpaid, company_wise_info[0].currency)]), |
deepeshgarg007 | 8300f5e | 2019-01-01 20:28:49 +0530 | [diff] [blame] | 116 | company_wise_info[0].total_unpaid ? 'orange' : 'green'); |
deepeshgarg007 | 12f5cef | 2018-12-25 16:06:19 +0530 | [diff] [blame] | 117 | |
deepeshgarg007 | 64238ee | 2018-12-25 16:28:39 +0530 | [diff] [blame] | 118 | if(company_wise_info[0].loyalty_points) { |
| 119 | frm.dashboard.add_indicator(__('Loyalty Points: {0}', |
| 120 | [company_wise_info[0].loyalty_points]), 'blue'); |
| 121 | } |
deepeshgarg007 | f31caff | 2018-11-27 15:04:12 +0530 | [diff] [blame] | 122 | } |
Rushabh Mehta | 559aa3a | 2016-09-19 16:04:58 +0530 | [diff] [blame] | 123 | } |
| 124 | }, |
| 125 | |
deepeshgarg007 | 12f5cef | 2018-12-25 16:06:19 +0530 | [diff] [blame] | 126 | add_indicator_for_multicompany: function(frm, info) { |
| 127 | frm.dashboard.stats_area.removeClass('hidden'); |
| 128 | frm.dashboard.stats_area_row.addClass('flex'); |
| 129 | frm.dashboard.stats_area_row.css('flex-wrap', 'wrap'); |
| 130 | |
| 131 | var color = info.total_unpaid ? 'orange' : 'green'; |
| 132 | |
| 133 | var indicator = $('<div class="flex-column col-xs-6">'+ |
| 134 | '<div style="margin-top:10px"><h6>'+info.company+'</h6></div>'+ |
| 135 | |
| 136 | '<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+ |
| 137 | 'Annual Billing: '+format_currency(info.billing_this_year, info.currency)+'</span></div>'+ |
| 138 | |
| 139 | '<div class="badge-link small" style="margin-bottom:10px">'+ |
| 140 | '<span class="indicator '+color+'">Total Unpaid: ' |
| 141 | +format_currency(info.total_unpaid, info.currency)+'</span></div>'+ |
| 142 | |
| 143 | |
| 144 | '</div>').appendTo(frm.dashboard.stats_area_row); |
| 145 | |
| 146 | if(info.loyalty_points){ |
| 147 | $('<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+ |
| 148 | 'Loyalty Points: '+info.loyalty_points+'</span></div>').appendTo(indicator); |
| 149 | } |
| 150 | |
| 151 | return indicator; |
| 152 | }, |
| 153 | |
Shreya Shah | 149f7ee | 2018-03-27 11:29:25 +0530 | [diff] [blame] | 154 | get_party_name: function(party_type) { |
| 155 | var dict = {'Customer': 'customer_name', 'Supplier': 'supplier_name', 'Employee': 'employee_name', |
| 156 | 'Member': 'member_name'}; |
| 157 | return dict[party_type]; |
| 158 | }, |
| 159 | |
Rushabh Mehta | 49f9747 | 2018-08-30 18:50:48 +0530 | [diff] [blame] | 160 | 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] | 161 | var d = locals[dt][dn]; |
| 162 | if(d[fieldname]){ |
| 163 | var cl = doc[table_fieldname] || []; |
| 164 | for(var i = 0; i < cl.length; i++) { |
| 165 | if(!cl[i][fieldname]) cl[i][fieldname] = d[fieldname]; |
| 166 | } |
| 167 | } |
| 168 | refresh_field(table_fieldname); |
Makarand Bauskar | 157c334 | 2017-05-26 21:32:33 +0530 | [diff] [blame] | 169 | }, |
| 170 | |
| 171 | get_terms: function(tc_name, doc, callback) { |
| 172 | if(tc_name) { |
| 173 | return frappe.call({ |
| 174 | method: 'erpnext.setup.doctype.terms_and_conditions.terms_and_conditions.get_terms_and_conditions', |
| 175 | args: { |
| 176 | template_name: tc_name, |
| 177 | doc: doc |
| 178 | }, |
| 179 | callback: function(r) { |
| 180 | callback(r) |
| 181 | } |
| 182 | }); |
| 183 | } |
tundebabzy | 1a947db | 2017-08-29 13:48:27 +0100 | [diff] [blame] | 184 | }, |
| 185 | |
rohitwaghchaure | 713cfc7 | 2018-09-11 17:40:37 +0530 | [diff] [blame] | 186 | make_bank_account: function(doctype, docname) { |
| 187 | frappe.call({ |
| 188 | method: "erpnext.accounts.doctype.bank_account.bank_account.make_bank_account", |
| 189 | args: { |
| 190 | doctype: doctype, |
| 191 | docname: docname |
| 192 | }, |
| 193 | freeze: true, |
| 194 | callback: function(r) { |
| 195 | var doclist = frappe.model.sync(r.message); |
| 196 | frappe.set_route("Form", doclist[0].doctype, doclist[0].name); |
| 197 | } |
| 198 | }) |
| 199 | }, |
| 200 | |
rohitwaghchaure | 166b78f | 2017-09-07 16:14:22 +0530 | [diff] [blame] | 201 | make_subscription: function(doctype, docname) { |
| 202 | frappe.call({ |
rohitwaghchaure | 79e5b07 | 2018-06-21 12:58:14 +0530 | [diff] [blame] | 203 | method: "frappe.desk.doctype.auto_repeat.auto_repeat.make_auto_repeat", |
rohitwaghchaure | 166b78f | 2017-09-07 16:14:22 +0530 | [diff] [blame] | 204 | args: { |
| 205 | doctype: doctype, |
| 206 | docname: docname |
| 207 | }, |
| 208 | callback: function(r) { |
| 209 | var doclist = frappe.model.sync(r.message); |
| 210 | frappe.set_route("Form", doclist[0].doctype, doclist[0].name); |
| 211 | } |
| 212 | }) |
| 213 | }, |
| 214 | |
Saif | 90cf2dd | 2018-09-30 21:46:31 +0500 | [diff] [blame] | 215 | make_pricing_rule: function(doctype, docname) { |
| 216 | frappe.call({ |
| 217 | method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.make_pricing_rule", |
| 218 | args: { |
| 219 | doctype: doctype, |
| 220 | docname: docname |
| 221 | }, |
| 222 | callback: function(r) { |
| 223 | var doclist = frappe.model.sync(r.message); |
| 224 | frappe.set_route("Form", doclist[0].doctype, doclist[0].name); |
| 225 | } |
| 226 | }) |
| 227 | }, |
| 228 | |
tundebabzy | 1a947db | 2017-08-29 13:48:27 +0100 | [diff] [blame] | 229 | /** |
| 230 | * Checks if the first row of a given child table is empty |
| 231 | * @param child_table - Child table Doctype |
| 232 | * @return {Boolean} |
| 233 | **/ |
| 234 | first_row_is_empty: function(child_table){ |
| 235 | if($.isArray(child_table) && child_table.length > 0) { |
| 236 | return !child_table[0].item_code; |
| 237 | } |
| 238 | return false; |
| 239 | }, |
| 240 | |
| 241 | /** |
| 242 | * Removes the first row of a child table if it is empty |
| 243 | * @param {_Frm} frm - The current form |
| 244 | * @param {String} child_table_name - The child table field name |
| 245 | * @return {Boolean} |
| 246 | **/ |
| 247 | remove_empty_first_row: function(frm, child_table_name){ |
| 248 | const rows = frm['doc'][child_table_name]; |
| 249 | if (this.first_row_is_empty(rows)){ |
| 250 | frm['doc'][child_table_name] = rows.splice(1); |
| 251 | } |
| 252 | return rows; |
| 253 | }, |
Zarrar | 6ffdf94 | 2018-06-14 12:24:16 +0530 | [diff] [blame] | 254 | get_tree_options: function(option) { |
| 255 | // get valid options for tree based on user permission & locals dict |
| 256 | let unscrub_option = frappe.model.unscrub(option); |
| 257 | let user_permission = frappe.defaults.get_user_permissions(); |
| 258 | if(user_permission && user_permission[unscrub_option]) { |
Suraj Shetty | 07e5786 | 2018-12-10 19:10:18 +0530 | [diff] [blame] | 259 | return user_permission[unscrub_option].map(perm => perm.doc); |
Zarrar | 6ffdf94 | 2018-06-14 12:24:16 +0530 | [diff] [blame] | 260 | } else { |
| 261 | return $.map(locals[`:${unscrub_option}`], function(c) { return c.name; }).sort(); |
| 262 | } |
| 263 | }, |
| 264 | get_tree_default: function(option) { |
| 265 | // set default for a field based on user permission |
| 266 | let options = this.get_tree_options(option); |
| 267 | if(options.includes(frappe.defaults.get_default(option))) { |
| 268 | return frappe.defaults.get_default(option); |
| 269 | } else { |
| 270 | return options[0]; |
| 271 | } |
Sanjay Kumar | 1b49f3a | 2018-09-06 13:09:35 +0400 | [diff] [blame] | 272 | }, |
| 273 | copy_parent_value_in_all_row: function(doc, dt, dn, table_fieldname, fieldname, parent_fieldname) { |
| 274 | var d = locals[dt][dn]; |
| 275 | if(d[fieldname]){ |
| 276 | var cl = doc[table_fieldname] || []; |
| 277 | for(var i = 0; i < cl.length; i++) { |
| 278 | cl[i][fieldname] = doc[parent_fieldname]; |
| 279 | } |
| 280 | } |
| 281 | refresh_field(table_fieldname); |
| 282 | }, |
| 283 | |
Rushabh Mehta | 95b995b | 2015-05-28 12:10:55 +0530 | [diff] [blame] | 284 | }); |
| 285 | |
rohitwaghchaure | a3c3dec | 2018-03-28 11:51:44 +0530 | [diff] [blame] | 286 | erpnext.utils.select_alternate_items = function(opts) { |
| 287 | const frm = opts.frm; |
| 288 | const warehouse_field = opts.warehouse_field || 'warehouse'; |
| 289 | const item_field = opts.item_field || 'item_code'; |
| 290 | |
| 291 | this.data = []; |
| 292 | const dialog = new frappe.ui.Dialog({ |
| 293 | title: __("Select Alternate Item"), |
| 294 | fields: [ |
| 295 | {fieldtype:'Section Break', label: __('Items')}, |
| 296 | { |
| 297 | fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true, |
| 298 | in_place_edit: true, data: this.data, |
| 299 | get_data: () => { |
| 300 | return this.data; |
| 301 | }, |
| 302 | fields: [{ |
| 303 | fieldtype:'Data', |
| 304 | fieldname:"docname", |
| 305 | hidden: 1 |
| 306 | }, { |
| 307 | fieldtype:'Link', |
| 308 | fieldname:"item_code", |
| 309 | options: 'Item', |
| 310 | in_list_view: 1, |
| 311 | read_only: 1, |
| 312 | label: __('Item Code') |
| 313 | }, { |
| 314 | fieldtype:'Link', |
| 315 | fieldname:"alternate_item", |
| 316 | options: 'Item', |
| 317 | default: "", |
| 318 | in_list_view: 1, |
| 319 | label: __('Alternate Item'), |
| 320 | onchange: function() { |
| 321 | const item_code = this.get_value(); |
| 322 | const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value(); |
| 323 | if (item_code && warehouse) { |
| 324 | frappe.call({ |
| 325 | method: "erpnext.stock.utils.get_latest_stock_qty", |
| 326 | args: { |
| 327 | item_code: item_code, |
| 328 | warehouse: warehouse |
| 329 | }, |
| 330 | callback: (r) => { |
| 331 | this.grid_row.on_grid_fields_dict |
| 332 | .actual_qty.set_value(r.message || 0); |
| 333 | } |
| 334 | }) |
| 335 | } |
| 336 | }, |
| 337 | get_query: (e) => { |
| 338 | return { |
| 339 | query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items", |
| 340 | filters: { |
| 341 | item_code: e.item_code |
| 342 | } |
| 343 | }; |
| 344 | } |
| 345 | }, { |
| 346 | fieldtype:'Link', |
| 347 | fieldname:"warehouse", |
| 348 | options: 'Warehouse', |
| 349 | default: "", |
| 350 | in_list_view: 1, |
| 351 | label: __('Warehouse'), |
| 352 | onchange: function() { |
| 353 | const warehouse = this.get_value(); |
| 354 | const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value(); |
| 355 | if (item_code && warehouse) { |
| 356 | frappe.call({ |
| 357 | method: "erpnext.stock.utils.get_latest_stock_qty", |
| 358 | args: { |
| 359 | item_code: item_code, |
| 360 | warehouse: warehouse |
| 361 | }, |
| 362 | callback: (r) => { |
| 363 | this.grid_row.on_grid_fields_dict |
| 364 | .actual_qty.set_value(r.message || 0); |
| 365 | } |
| 366 | }) |
| 367 | } |
| 368 | }, |
| 369 | }, { |
| 370 | fieldtype:'Float', |
| 371 | fieldname:"actual_qty", |
| 372 | default: 0, |
| 373 | read_only: 1, |
| 374 | in_list_view: 1, |
| 375 | label: __('Available Qty') |
| 376 | }] |
| 377 | }, |
| 378 | ], |
| 379 | primary_action: function() { |
| 380 | const args = this.get_values()["alternative_items"]; |
| 381 | const alternative_items = args.filter(d => { |
| 382 | if (d.alternate_item && d.item_code != d.alternate_item) { |
| 383 | return true; |
| 384 | } |
| 385 | }); |
| 386 | |
| 387 | alternative_items.forEach(d => { |
| 388 | let row = frappe.get_doc(opts.child_doctype, d.docname); |
Doridel Cahanap | e42192b | 2018-05-16 13:28:39 +0800 | [diff] [blame] | 389 | let qty = null; |
| 390 | if (row.doctype === 'Work Order Item') { |
| 391 | qty = row.required_qty; |
| 392 | } else { |
| 393 | qty = row.qty; |
| 394 | } |
rohitwaghchaure | a3c3dec | 2018-03-28 11:51:44 +0530 | [diff] [blame] | 395 | row[item_field] = d.alternate_item; |
rohitwaghchaure | a3c3dec | 2018-03-28 11:51:44 +0530 | [diff] [blame] | 396 | frm.script_manager.trigger(item_field, row.doctype, row.name) |
| 397 | .then(() => { |
| 398 | frappe.model.set_value(row.doctype, row.name, 'qty', qty); |
| 399 | frappe.model.set_value(row.doctype, row.name, |
| 400 | opts.original_item_field, d.item_code); |
| 401 | }); |
| 402 | }); |
| 403 | |
| 404 | refresh_field(opts.child_docname); |
| 405 | this.hide(); |
| 406 | }, |
| 407 | primary_action_label: __('Update') |
| 408 | }); |
| 409 | |
| 410 | frm.doc[opts.child_docname].forEach(d => { |
| 411 | if (!opts.condition || opts.condition(d)) { |
| 412 | dialog.fields_dict.alternative_items.df.data.push({ |
| 413 | "docname": d.name, |
| 414 | "item_code": d[item_field], |
| 415 | "warehouse": d[warehouse_field], |
| 416 | "actual_qty": d.actual_qty |
| 417 | }); |
| 418 | } |
| 419 | }) |
| 420 | |
| 421 | this.data = dialog.fields_dict.alternative_items.df.data; |
| 422 | dialog.fields_dict.alternative_items.grid.refresh(); |
| 423 | dialog.show(); |
| 424 | } |
| 425 | |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 426 | erpnext.utils.update_child_items = function(opts) { |
| 427 | const frm = opts.frm; |
Stavros Anastasiadis | 3d82b74 | 2018-12-10 13:00:55 +0100 | [diff] [blame] | 428 | const cannot_add_row = (typeof opts.cannot_add_row === 'undefined') ? true : opts.cannot_add_row; |
| 429 | const child_docname = (typeof opts.cannot_add_row === 'undefined') ? "items" : opts.child_docname; |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 430 | this.data = []; |
| 431 | const dialog = new frappe.ui.Dialog({ |
| 432 | title: __("Update Items"), |
| 433 | fields: [ |
| 434 | {fieldtype:'Section Break', label: __('Items')}, |
| 435 | { |
Stavros Anastasiadis | 3d82b74 | 2018-12-10 13:00:55 +0100 | [diff] [blame] | 436 | fieldname: "trans_items", |
| 437 | fieldtype: "Table", |
| 438 | cannot_add_rows: cannot_add_row, |
| 439 | in_place_edit: true, |
| 440 | data: this.data, |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 441 | get_data: () => { |
| 442 | return this.data; |
| 443 | }, |
| 444 | fields: [{ |
| 445 | fieldtype:'Data', |
| 446 | fieldname:"docname", |
| 447 | hidden: 0, |
| 448 | }, { |
| 449 | fieldtype:'Link', |
| 450 | fieldname:"item_code", |
| 451 | options: 'Item', |
| 452 | in_list_view: 1, |
| 453 | read_only: 1, |
| 454 | label: __('Item Code') |
| 455 | }, { |
| 456 | fieldtype:'Float', |
| 457 | fieldname:"qty", |
| 458 | default: 0, |
| 459 | read_only: 0, |
| 460 | in_list_view: 1, |
| 461 | label: __('Qty') |
| 462 | }, { |
| 463 | fieldtype:'Currency', |
| 464 | fieldname:"rate", |
| 465 | default: 0, |
| 466 | read_only: 0, |
| 467 | in_list_view: 1, |
| 468 | label: __('Rate') |
| 469 | }] |
| 470 | }, |
| 471 | ], |
| 472 | primary_action: function() { |
| 473 | const trans_items = this.get_values()["trans_items"]; |
| 474 | frappe.call({ |
| 475 | method: 'erpnext.controllers.accounts_controller.update_child_qty_rate', |
Stavros Anastasiadis | 3d82b74 | 2018-12-10 13:00:55 +0100 | [diff] [blame] | 476 | freeze: true, |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 477 | args: { |
| 478 | 'parent_doctype': frm.doc.doctype, |
| 479 | 'trans_items': trans_items, |
Stavros Anastasiadis | 3d82b74 | 2018-12-10 13:00:55 +0100 | [diff] [blame] | 480 | 'parent_doctype_name': frm.doc.name, |
| 481 | 'child_docname': child_docname |
Doridel Cahanap | 6f06cc2 | 2018-05-17 16:28:58 +0800 | [diff] [blame] | 482 | }, |
| 483 | callback: function() { |
| 484 | frm.reload_doc(); |
| 485 | } |
| 486 | }); |
| 487 | this.hide(); |
| 488 | refresh_field("items"); |
| 489 | }, |
| 490 | primary_action_label: __('Update') |
| 491 | }); |
| 492 | |
| 493 | frm.doc[opts.child_docname].forEach(d => { |
| 494 | dialog.fields_dict.trans_items.df.data.push({ |
| 495 | "docname": d.name, |
| 496 | "item_code": d.item_code, |
| 497 | "qty": d.qty, |
| 498 | "rate": d.rate, |
| 499 | }); |
| 500 | this.data = dialog.fields_dict.trans_items.df.data; |
| 501 | dialog.fields_dict.trans_items.grid.refresh(); |
| 502 | }) |
| 503 | dialog.show(); |
| 504 | } |
| 505 | |
Rushabh Mehta | 176b63b | 2016-07-14 17:43:48 +0530 | [diff] [blame] | 506 | erpnext.utils.map_current_doc = function(opts) { |
| 507 | if(opts.get_query_filters) { |
| 508 | opts.get_query = function() { |
| 509 | return {filters: opts.get_query_filters}; |
| 510 | } |
| 511 | } |
| 512 | var _map = function() { |
Rushabh Mehta | 55ea7b1 | 2016-07-15 16:43:25 +0530 | [diff] [blame] | 513 | if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) { |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 514 | // remove first item row if empty |
Rushabh Mehta | 176b63b | 2016-07-14 17:43:48 +0530 | [diff] [blame] | 515 | if(!cur_frm.doc.items[0].item_code) { |
| 516 | cur_frm.doc.items = cur_frm.doc.items.splice(1); |
| 517 | } |
bhupen | 3c7e70f | 2016-11-30 14:32:11 +0530 | [diff] [blame] | 518 | |
| 519 | // find the doctype of the items table |
| 520 | var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options; |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 521 | |
bhupen | 3c7e70f | 2016-11-30 14:32:11 +0530 | [diff] [blame] | 522 | // find the link fieldname from items table for the given |
| 523 | // source_doctype |
| 524 | var link_fieldname = null; |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 525 | frappe.get_meta(items_doctype).fields.forEach(function(d) { |
bhupen | 3c7e70f | 2016-11-30 14:32:11 +0530 | [diff] [blame] | 526 | if(d.options===opts.source_doctype) link_fieldname = d.fieldname; }); |
| 527 | |
Nabin Hait | 802b435 | 2017-01-09 15:32:20 +0530 | [diff] [blame] | 528 | // 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] | 529 | var already_set = false; |
Nabin Hait | 802b435 | 2017-01-09 15:32:20 +0530 | [diff] [blame] | 530 | var item_qty_map = {}; |
bhupen | 3c7e70f | 2016-11-30 14:32:11 +0530 | [diff] [blame] | 531 | |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 532 | $.each(cur_frm.doc.items, function(i, d) { |
| 533 | opts.source_name.forEach(function(src) { |
| 534 | if(d[link_fieldname]==src) { |
| 535 | already_set = true; |
| 536 | if (item_qty_map[d.item_code]) |
| 537 | item_qty_map[d.item_code] += flt(d.qty); |
| 538 | else |
| 539 | item_qty_map[d.item_code] = flt(d.qty); |
| 540 | } |
| 541 | }); |
| 542 | }); |
| 543 | |
| 544 | if(already_set) { |
| 545 | opts.source_name.forEach(function(src) { |
| 546 | frappe.model.with_doc(opts.source_doctype, src, function(r) { |
| 547 | var source_doc = frappe.model.get_doc(opts.source_doctype, src); |
| 548 | $.each(source_doc.items || [], function(i, row) { |
| 549 | if(row.qty > flt(item_qty_map[row.item_code])) { |
| 550 | already_set = false; |
| 551 | return false; |
| 552 | } |
| 553 | }) |
| 554 | }) |
| 555 | |
| 556 | if(already_set) { |
| 557 | frappe.msgprint(__("You have already selected items from {0} {1}", |
| 558 | [opts.source_doctype, src])); |
| 559 | return; |
| 560 | } |
| 561 | |
| 562 | }) |
Nabin Hait | 802b435 | 2017-01-09 15:32:20 +0530 | [diff] [blame] | 563 | } |
Rushabh Mehta | 176b63b | 2016-07-14 17:43:48 +0530 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | return frappe.call({ |
| 567 | // Sometimes we hit the limit for URL length of a GET request |
| 568 | // as we send the full target_doc. Hence this is a POST request. |
| 569 | type: "POST", |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 570 | method: 'frappe.model.mapper.map_docs', |
Rushabh Mehta | 176b63b | 2016-07-14 17:43:48 +0530 | [diff] [blame] | 571 | args: { |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 572 | "method": opts.method, |
| 573 | "source_names": opts.source_name, |
| 574 | "target_doc": cur_frm.doc, |
Rushabh Mehta | 49f9747 | 2018-08-30 18:50:48 +0530 | [diff] [blame] | 575 | 'args': opts.args |
Rushabh Mehta | 176b63b | 2016-07-14 17:43:48 +0530 | [diff] [blame] | 576 | }, |
| 577 | callback: function(r) { |
| 578 | if(!r.exc) { |
| 579 | var doc = frappe.model.sync(r.message); |
Vishal Dhayagude | f06c281 | 2017-12-26 16:22:40 +0530 | [diff] [blame] | 580 | cur_frm.dirty(); |
Rushabh Mehta | 176b63b | 2016-07-14 17:43:48 +0530 | [diff] [blame] | 581 | cur_frm.refresh(); |
| 582 | } |
| 583 | } |
| 584 | }); |
| 585 | } |
| 586 | if(opts.source_doctype) { |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 587 | var d = new frappe.ui.form.MultiSelectDialog({ |
| 588 | doctype: opts.source_doctype, |
| 589 | target: opts.target, |
| 590 | date_field: opts.date_field || undefined, |
| 591 | setters: opts.setters, |
| 592 | get_query: opts.get_query, |
| 593 | action: function(selections, args) { |
| 594 | let values = selections; |
| 595 | if(values.length === 0){ |
Nabin Hait | a11dcb6 | 2017-12-04 13:36:50 +0530 | [diff] [blame] | 596 | frappe.msgprint(__("Please select {0}", [opts.source_doctype])) |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 597 | return; |
| 598 | } |
| 599 | opts.source_name = values; |
| 600 | opts.setters = args; |
| 601 | d.dialog.hide(); |
| 602 | _map(); |
| 603 | }, |
Rushabh Mehta | 176b63b | 2016-07-14 17:43:48 +0530 | [diff] [blame] | 604 | }); |
Rushabh Mehta | 176b63b | 2016-07-14 17:43:48 +0530 | [diff] [blame] | 605 | } else if(opts.source_name) { |
Prateeksha Singh | edeb4dc | 2017-05-15 11:32:06 +0530 | [diff] [blame] | 606 | opts.source_name = [opts.source_name]; |
Rushabh Mehta | 176b63b | 2016-07-14 17:43:48 +0530 | [diff] [blame] | 607 | _map(); |
| 608 | } |
| 609 | } |
| 610 | |
Rushabh Mehta | 180e435 | 2016-07-26 17:57:42 +0530 | [diff] [blame] | 611 | frappe.form.link_formatters['Item'] = function(value, doc) { |
Nabin Hait | 981f6ea | 2016-07-31 11:44:43 +0530 | [diff] [blame] | 612 | if(doc && doc.item_name && doc.item_name !== value) { |
mbauskar | be81442 | 2016-10-27 09:49:22 +0530 | [diff] [blame] | 613 | return value? value + ': ' + doc.item_name: doc.item_name; |
Rushabh Mehta | 180e435 | 2016-07-26 17:57:42 +0530 | [diff] [blame] | 614 | } else { |
| 615 | return value; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | frappe.form.link_formatters['Employee'] = function(value, doc) { |
Nabin Hait | 981f6ea | 2016-07-31 11:44:43 +0530 | [diff] [blame] | 620 | if(doc && doc.employee_name && doc.employee_name !== value) { |
mbauskar | be81442 | 2016-10-27 09:49:22 +0530 | [diff] [blame] | 621 | return value? value + ': ' + doc.employee_name: doc.employee_name; |
Rushabh Mehta | 180e435 | 2016-07-26 17:57:42 +0530 | [diff] [blame] | 622 | } else { |
| 623 | return value; |
| 624 | } |
| 625 | } |
| 626 | |
Rushabh Mehta | 95b995b | 2015-05-28 12:10:55 +0530 | [diff] [blame] | 627 | // add description on posting time |
| 628 | $(document).on('app_ready', function() { |
| 629 | if(!frappe.datetime.is_timezone_same()) { |
| 630 | $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry", |
| 631 | "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) { |
| 632 | frappe.ui.form.on(d, "onload", function(frm) { |
| 633 | cur_frm.set_df_property("posting_time", "description", |
Faris Ansari | ab74ca7 | 2017-05-30 12:54:42 +0530 | [diff] [blame] | 634 | frappe.sys_defaults.time_zone); |
Rushabh Mehta | 95b995b | 2015-05-28 12:10:55 +0530 | [diff] [blame] | 635 | }); |
| 636 | }); |
| 637 | } |
akshay | 14384c2 | 2016-07-28 13:53:17 +0530 | [diff] [blame] | 638 | }); |