blob: 51139b31af51ae5a21d198d006f7711d26865d91 [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302// License: GNU General Public License v3. See license.txt
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05303frappe.provide("erpnext");
Nabin Haitb072c742014-12-16 12:49:13 +05304frappe.provide("erpnext.utils");
Anand Doshie65e6212012-11-19 15:43:18 +05305
Anand Doshibdee6e02013-07-09 13:03:39 +05306$.extend(erpnext, {
7 get_currency: function(company) {
8 if(!company && cur_frm)
9 company = cur_frm.doc.company;
10 if(company)
Rushabh Mehta66d52b52014-03-27 14:17:33 +053011 return frappe.get_doc(":Company", company).default_currency || frappe.boot.sysdefaults.currency;
Anand Doshibdee6e02013-07-09 13:03:39 +053012 else
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053013 return frappe.boot.sysdefaults.currency;
Anand Doshibdee6e02013-07-09 13:03:39 +053014 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053015
tundebabzyc8978252018-02-12 10:34:50 +010016 get_presentation_currency_list: () => {
17 const docs = frappe.boot.docs;
Zarrare25dcd22018-06-21 10:53:12 +053018 let currency_list = docs.filter(d => d.doctype === ":Currency").map(d => d.name);
19 currency_list.unshift("");
tundebabzyc8978252018-02-12 10:34:50 +010020 return currency_list;
21 },
22
Anand Doshi8bde7f92014-04-24 18:11:49 +053023 toggle_naming_series: function() {
Rushabh Mehtac38fc712014-04-16 17:21:25 +053024 if(cur_frm.fields_dict.naming_series) {
Anand Doshibdee6e02013-07-09 13:03:39 +053025 cur_frm.toggle_display("naming_series", cur_frm.doc.__islocal?true:false);
26 }
27 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053028
Anand Doshibdee6e02013-07-09 13:03:39 +053029 hide_company: function() {
30 if(cur_frm.fields_dict.company) {
Rushabh Mehta89418412013-10-07 18:22:29 +053031 var companies = Object.keys(locals[":Company"] || {});
Anand Doshibdee6e02013-07-09 13:03:39 +053032 if(companies.length === 1) {
33 if(!cur_frm.doc.company) cur_frm.set_value("company", companies[0]);
34 cur_frm.toggle_display("company", false);
Rushabh Mehta45ca8a42015-04-28 16:02:09 +053035 } else if(erpnext.last_selected_company) {
36 if(!cur_frm.doc.company) cur_frm.set_value("company", erpnext.last_selected_company);
Anand Doshibdee6e02013-07-09 13:03:39 +053037 }
38 }
Anand Doshi1ffc5b52013-07-15 18:09:43 +053039 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053040
Rohit Waghchauree9ff1912017-06-19 12:54:59 +053041 is_perpetual_inventory_enabled: function(company) {
42 if(company) {
43 return frappe.get_doc(":Company", company).enable_perpetual_inventory
44 }
45 },
46
Makarand Bauskare6712c12017-10-25 12:17:40 +053047 stale_rate_allowed: () => {
rohitwaghchaure7677ff02017-11-02 18:12:14 +053048 return cint(frappe.boot.sysdefaults.allow_stale);
Makarand Bauskare6712c12017-10-25 12:17:40 +053049 },
50
Alanc6dc9ea2021-05-07 20:30:04 +053051 setup_serial_or_batch_no: function() {
52 let grid_row = cur_frm.open_grid_row();
53 if (!grid_row || !grid_row.grid_form.fields_dict.serial_no ||
54 grid_row.grid_form.fields_dict.serial_no.get_status() !== "Write") return;
Rushabh Mehtac38fc712014-04-16 17:21:25 +053055
Alanc6dc9ea2021-05-07 20:30:04 +053056 frappe.model.get_value('Item', {'name': grid_row.doc.item_code},
57 ['has_serial_no', 'has_batch_no'], ({has_serial_no, has_batch_no}) => {
58 Object.assign(grid_row.doc, {has_serial_no, has_batch_no});
Rushabh Mehtac38fc712014-04-16 17:21:25 +053059
Alanc6dc9ea2021-05-07 20:30:04 +053060 if (has_serial_no) {
61 attach_selector_button(__("Add Serial No"),
62 grid_row.grid_form.fields_dict.serial_no.$wrapper, this, grid_row);
63 } else if (has_batch_no) {
64 attach_selector_button(__("Pick Batch No"),
65 grid_row.grid_form.fields_dict.batch_no.$wrapper, this, grid_row);
Marica29a2e162019-11-12 14:43:41 +053066 }
Alanc6dc9ea2021-05-07 20:30:04 +053067 }
68 );
deepeshgarg007e64c3572019-05-14 08:50:45 +053069 },
Nabin Hait1ed1c4e2019-11-25 12:33:40 +053070
71 route_to_adjustment_jv: (args) => {
72 frappe.model.with_doctype('Journal Entry', () => {
73 // route to adjustment Journal Entry to handle Account Balance and Stock Value mismatch
74 let journal_entry = frappe.model.get_new_doc('Journal Entry');
75
76 args.accounts.forEach((je_account) => {
77 let child_row = frappe.model.add_child(journal_entry, "accounts");
78 child_row.account = je_account.account;
79 child_row.debit_in_account_currency = je_account.debit_in_account_currency;
80 child_row.credit_in_account_currency = je_account.credit_in_account_currency;
81 child_row.party_type = "" ;
82 });
83 frappe.set_route('Form','Journal Entry', journal_entry.name);
84 });
Frappe PR Bot255b99e2021-08-24 20:19:22 +053085 },
86
Ankush Menatd37541d2021-12-10 12:04:10 +053087 route_to_pending_reposts: (args) => {
88 frappe.set_route('List', 'Repost Item Valuation', args);
89 },
Rushabh Mehtac38fc712014-04-16 17:21:25 +053090});
Rushabh Mehta7d368752014-11-24 14:16:47 +053091
Nabin Haitb072c742014-12-16 12:49:13 +053092
93$.extend(erpnext.utils, {
Rushabh Mehta559aa3a2016-09-19 16:04:58 +053094 set_party_dashboard_indicators: function(frm) {
95 if(frm.doc.__onload && frm.doc.__onload.dashboard_info) {
deepeshgarg007920dc142018-11-23 10:17:28 +053096 var company_wise_info = frm.doc.__onload.dashboard_info;
deepeshgarg007f31caff2018-11-27 15:04:12 +053097 if(company_wise_info.length > 1) {
deepeshgarg007a1cffc32018-11-23 16:51:35 +053098 company_wise_info.forEach(function(info) {
deepeshgarg00712f5cef2018-12-25 16:06:19 +053099 erpnext.utils.add_indicator_for_multicompany(frm, info);
deepeshgarg007a1cffc32018-11-23 16:51:35 +0530100 });
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530101 } else if (company_wise_info.length === 1) {
deepeshgarg00764238ee2018-12-25 16:28:39 +0530102 frm.dashboard.add_indicator(__('Annual Billing: {0}',
103 [format_currency(company_wise_info[0].billing_this_year, company_wise_info[0].currency)]), 'blue');
104 frm.dashboard.add_indicator(__('Total Unpaid: {0}',
105 [format_currency(company_wise_info[0].total_unpaid, company_wise_info[0].currency)]),
deepeshgarg0078300f5e2019-01-01 20:28:49 +0530106 company_wise_info[0].total_unpaid ? 'orange' : 'green');
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530107
deepeshgarg00764238ee2018-12-25 16:28:39 +0530108 if(company_wise_info[0].loyalty_points) {
109 frm.dashboard.add_indicator(__('Loyalty Points: {0}',
110 [company_wise_info[0].loyalty_points]), 'blue');
111 }
deepeshgarg007f31caff2018-11-27 15:04:12 +0530112 }
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530113 }
114 },
115
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530116 add_indicator_for_multicompany: function(frm, info) {
Deepak867f2c62022-05-17 11:52:52 +0530117 frm.dashboard.stats_area.show();
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530118 frm.dashboard.stats_area_row.addClass('flex');
119 frm.dashboard.stats_area_row.css('flex-wrap', 'wrap');
120
121 var color = info.total_unpaid ? 'orange' : 'green';
122
123 var indicator = $('<div class="flex-column col-xs-6">'+
124 '<div style="margin-top:10px"><h6>'+info.company+'</h6></div>'+
125
126 '<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
127 'Annual Billing: '+format_currency(info.billing_this_year, info.currency)+'</span></div>'+
128
129 '<div class="badge-link small" style="margin-bottom:10px">'+
130 '<span class="indicator '+color+'">Total Unpaid: '
131 +format_currency(info.total_unpaid, info.currency)+'</span></div>'+
132
133
134 '</div>').appendTo(frm.dashboard.stats_area_row);
135
136 if(info.loyalty_points){
137 $('<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
138 'Loyalty Points: '+info.loyalty_points+'</span></div>').appendTo(indicator);
139 }
140
141 return indicator;
142 },
143
Shreya Shah149f7ee2018-03-27 11:29:25 +0530144 get_party_name: function(party_type) {
145 var dict = {'Customer': 'customer_name', 'Supplier': 'supplier_name', 'Employee': 'employee_name',
146 'Member': 'member_name'};
147 return dict[party_type];
148 },
149
Rushabh Mehta49f97472018-08-30 18:50:48 +0530150 copy_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname) {
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +0530151 var d = locals[dt][dn];
152 if(d[fieldname]){
153 var cl = doc[table_fieldname] || [];
154 for(var i = 0; i < cl.length; i++) {
155 if(!cl[i][fieldname]) cl[i][fieldname] = d[fieldname];
156 }
157 }
158 refresh_field(table_fieldname);
Makarand Bauskar157c3342017-05-26 21:32:33 +0530159 },
160
161 get_terms: function(tc_name, doc, callback) {
162 if(tc_name) {
163 return frappe.call({
164 method: 'erpnext.setup.doctype.terms_and_conditions.terms_and_conditions.get_terms_and_conditions',
165 args: {
166 template_name: tc_name,
167 doc: doc
168 },
169 callback: function(r) {
170 callback(r)
171 }
172 });
173 }
tundebabzy1a947db2017-08-29 13:48:27 +0100174 },
175
rohitwaghchaure713cfc72018-09-11 17:40:37 +0530176 make_bank_account: function(doctype, docname) {
177 frappe.call({
178 method: "erpnext.accounts.doctype.bank_account.bank_account.make_bank_account",
179 args: {
180 doctype: doctype,
181 docname: docname
182 },
183 freeze: true,
184 callback: function(r) {
185 var doclist = frappe.model.sync(r.message);
186 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
187 }
188 })
189 },
190
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530191 add_dimensions: function(report_name, index) {
192 let filters = frappe.query_reports[report_name].filters;
193
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530194 frappe.call({
195 method: "erpnext.accounts.doctype.accounting_dimension.accounting_dimension.get_dimensions",
196 callback: function(r) {
197 let accounting_dimensions = r.message[0];
198 accounting_dimensions.forEach((dimension) => {
199 let found = filters.some(el => el.fieldname === dimension['fieldname']);
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530200
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530201 if (!found) {
Deepesh Garg23ab5c52020-12-31 11:29:06 +0530202 filters.splice(index, 0, {
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530203 "fieldname": dimension["fieldname"],
204 "label": __(dimension["label"]),
Rohit Waghchaure69be22b2022-05-14 17:19:34 +0530205 "fieldtype": "MultiSelectList",
206 get_data: function(txt) {
207 return frappe.db.get_link_options(dimension["document_type"], txt);
208 },
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530209 });
210 }
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530211 });
212 }
213 });
214 },
215
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530216 make_subscription: function(doctype, docname) {
217 frappe.call({
Rucha Mahabal65a627c2019-07-17 13:50:32 +0530218 method: "frappe.automation.doctype.auto_repeat.auto_repeat.make_auto_repeat",
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530219 args: {
220 doctype: doctype,
221 docname: docname
222 },
223 callback: function(r) {
224 var doclist = frappe.model.sync(r.message);
225 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
226 }
227 })
228 },
229
Saif90cf2dd2018-09-30 21:46:31 +0500230 make_pricing_rule: function(doctype, docname) {
231 frappe.call({
232 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.make_pricing_rule",
233 args: {
234 doctype: doctype,
235 docname: docname
236 },
237 callback: function(r) {
238 var doclist = frappe.model.sync(r.message);
239 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
240 }
241 })
242 },
243
tundebabzy1a947db2017-08-29 13:48:27 +0100244 /**
245 * Checks if the first row of a given child table is empty
246 * @param child_table - Child table Doctype
247 * @return {Boolean}
248 **/
249 first_row_is_empty: function(child_table){
250 if($.isArray(child_table) && child_table.length > 0) {
251 return !child_table[0].item_code;
252 }
253 return false;
254 },
255
256 /**
257 * Removes the first row of a child table if it is empty
258 * @param {_Frm} frm - The current form
259 * @param {String} child_table_name - The child table field name
260 * @return {Boolean}
261 **/
262 remove_empty_first_row: function(frm, child_table_name){
263 const rows = frm['doc'][child_table_name];
264 if (this.first_row_is_empty(rows)){
265 frm['doc'][child_table_name] = rows.splice(1);
266 }
267 return rows;
268 },
Zarrar6ffdf942018-06-14 12:24:16 +0530269 get_tree_options: function(option) {
270 // get valid options for tree based on user permission & locals dict
271 let unscrub_option = frappe.model.unscrub(option);
272 let user_permission = frappe.defaults.get_user_permissions();
deepeshgarg0070d64d622019-02-20 17:55:14 +0530273 let options;
274
Zarrar6ffdf942018-06-14 12:24:16 +0530275 if(user_permission && user_permission[unscrub_option]) {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530276 options = user_permission[unscrub_option].map(perm => perm.doc);
Zarrar6ffdf942018-06-14 12:24:16 +0530277 } else {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530278 options = $.map(locals[`:${unscrub_option}`], function(c) { return c.name; }).sort();
Zarrar6ffdf942018-06-14 12:24:16 +0530279 }
deepeshgarg0070d64d622019-02-20 17:55:14 +0530280
281 // filter unique values, as there may be multiple user permissions for any value
282 return options.filter((value, index, self) => self.indexOf(value) === index);
Zarrar6ffdf942018-06-14 12:24:16 +0530283 },
284 get_tree_default: function(option) {
285 // set default for a field based on user permission
286 let options = this.get_tree_options(option);
287 if(options.includes(frappe.defaults.get_default(option))) {
288 return frappe.defaults.get_default(option);
289 } else {
290 return options[0];
291 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400292 },
Andy Zhubffe9332021-04-12 22:49:26 +1200293 overrides_parent_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname, parent_fieldname) {
Marica24e45162021-04-14 18:53:15 +0530294 if (doc[parent_fieldname]) {
Andy Zhubffe9332021-04-12 22:49:26 +1200295 let cl = doc[table_fieldname] || [];
Marica24e45162021-04-14 18:53:15 +0530296 for (let i = 0; i < cl.length; i++) {
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400297 cl[i][fieldname] = doc[parent_fieldname];
298 }
Andy Zhubffe9332021-04-12 22:49:26 +1200299 frappe.refresh_field(table_fieldname);
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400300 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400301 },
Nabin Hait34c551d2019-07-03 10:34:31 +0530302 create_new_doc: function (doctype, update_fields) {
303 frappe.model.with_doctype(doctype, function() {
304 var new_doc = frappe.model.get_new_doc(doctype);
305 for (let [key, value] of Object.entries(update_fields)) {
306 new_doc[key] = value;
307 }
308 frappe.ui.form.make_quick_entry(doctype, null, null, new_doc);
309 });
310 }
311
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530312});
313
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530314erpnext.utils.select_alternate_items = function(opts) {
315 const frm = opts.frm;
316 const warehouse_field = opts.warehouse_field || 'warehouse';
317 const item_field = opts.item_field || 'item_code';
318
319 this.data = [];
320 const dialog = new frappe.ui.Dialog({
321 title: __("Select Alternate Item"),
322 fields: [
323 {fieldtype:'Section Break', label: __('Items')},
324 {
325 fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true,
326 in_place_edit: true, data: this.data,
327 get_data: () => {
328 return this.data;
329 },
330 fields: [{
331 fieldtype:'Data',
332 fieldname:"docname",
333 hidden: 1
334 }, {
335 fieldtype:'Link',
336 fieldname:"item_code",
337 options: 'Item',
338 in_list_view: 1,
339 read_only: 1,
340 label: __('Item Code')
341 }, {
342 fieldtype:'Link',
343 fieldname:"alternate_item",
344 options: 'Item',
345 default: "",
346 in_list_view: 1,
347 label: __('Alternate Item'),
348 onchange: function() {
349 const item_code = this.get_value();
350 const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value();
351 if (item_code && warehouse) {
352 frappe.call({
353 method: "erpnext.stock.utils.get_latest_stock_qty",
354 args: {
355 item_code: item_code,
356 warehouse: warehouse
357 },
358 callback: (r) => {
359 this.grid_row.on_grid_fields_dict
360 .actual_qty.set_value(r.message || 0);
361 }
362 })
363 }
364 },
365 get_query: (e) => {
366 return {
367 query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items",
368 filters: {
369 item_code: e.item_code
370 }
371 };
372 }
373 }, {
374 fieldtype:'Link',
375 fieldname:"warehouse",
376 options: 'Warehouse',
377 default: "",
378 in_list_view: 1,
379 label: __('Warehouse'),
380 onchange: function() {
381 const warehouse = this.get_value();
382 const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value();
383 if (item_code && warehouse) {
384 frappe.call({
385 method: "erpnext.stock.utils.get_latest_stock_qty",
386 args: {
387 item_code: item_code,
388 warehouse: warehouse
389 },
390 callback: (r) => {
391 this.grid_row.on_grid_fields_dict
392 .actual_qty.set_value(r.message || 0);
393 }
394 })
395 }
396 },
397 }, {
398 fieldtype:'Float',
399 fieldname:"actual_qty",
400 default: 0,
401 read_only: 1,
402 in_list_view: 1,
403 label: __('Available Qty')
404 }]
405 },
406 ],
407 primary_action: function() {
408 const args = this.get_values()["alternative_items"];
409 const alternative_items = args.filter(d => {
410 if (d.alternate_item && d.item_code != d.alternate_item) {
411 return true;
412 }
413 });
414
415 alternative_items.forEach(d => {
416 let row = frappe.get_doc(opts.child_doctype, d.docname);
Doridel Cahanape42192b2018-05-16 13:28:39 +0800417 let qty = null;
418 if (row.doctype === 'Work Order Item') {
419 qty = row.required_qty;
420 } else {
421 qty = row.qty;
422 }
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530423 row[item_field] = d.alternate_item;
mergify[bot]c1a0f642021-12-17 19:06:19 +0530424 frappe.model.set_value(row.doctype, row.name, 'qty', qty);
425 frappe.model.set_value(row.doctype, row.name, opts.original_item_field, d.item_code);
426 frm.trigger(item_field, row.doctype, row.name);
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530427 });
428
429 refresh_field(opts.child_docname);
430 this.hide();
431 },
432 primary_action_label: __('Update')
433 });
434
435 frm.doc[opts.child_docname].forEach(d => {
436 if (!opts.condition || opts.condition(d)) {
437 dialog.fields_dict.alternative_items.df.data.push({
438 "docname": d.name,
439 "item_code": d[item_field],
440 "warehouse": d[warehouse_field],
441 "actual_qty": d.actual_qty
442 });
443 }
444 })
445
446 this.data = dialog.fields_dict.alternative_items.df.data;
447 dialog.fields_dict.alternative_items.grid.refresh();
448 dialog.show();
449}
450
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800451erpnext.utils.update_child_items = function(opts) {
452 const frm = opts.frm;
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100453 const cannot_add_row = (typeof opts.cannot_add_row === 'undefined') ? true : opts.cannot_add_row;
454 const child_docname = (typeof opts.cannot_add_row === 'undefined') ? "items" : opts.child_docname;
Saqib56fea7d2020-10-09 21:19:25 +0530455 const child_meta = frappe.get_meta(`${frm.doc.doctype} Item`);
456 const get_precision = (fieldname) => child_meta.fields.find(f => f.fieldname == fieldname).precision;
457
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800458 this.data = [];
Saqib Ansarif53299e2020-04-15 22:08:12 +0530459 const fields = [{
460 fieldtype:'Data',
461 fieldname:"docname",
462 read_only: 1,
463 hidden: 1,
464 }, {
465 fieldtype:'Link',
466 fieldname:"item_code",
467 options: 'Item',
468 in_list_view: 1,
469 read_only: 0,
470 disabled: 0,
Afshandc7280e2021-08-18 17:44:40 +0530471 label: __('Item Code'),
472 get_query: function() {
473 let filters;
474 if (frm.doc.doctype == 'Sales Order') {
475 filters = {"is_sales_item": 1};
476 } else if (frm.doc.doctype == 'Purchase Order') {
Sagar Sharmad074c932022-03-31 19:57:42 +0530477 if (frm.doc.is_subcontracted) {
s-aga-r6d89b2f2022-06-18 15:46:59 +0530478 if (frm.doc.is_old_subcontracting_flow) {
479 filters = {"is_sub_contracted_item": 1};
480 } else {
481 filters = {"is_stock_item": 0};
482 }
Afshandc7280e2021-08-18 17:44:40 +0530483 } else {
484 filters = {"is_purchase_item": 1};
485 }
486 }
487 return {
488 query: "erpnext.controllers.queries.item_query",
489 filters: filters
490 };
491 }
Saqib Ansarif53299e2020-04-15 22:08:12 +0530492 }, {
Saqib61314242020-09-15 11:14:31 +0530493 fieldtype:'Link',
494 fieldname:'uom',
495 options: 'UOM',
496 read_only: 0,
497 label: __('UOM'),
498 reqd: 1,
499 onchange: function () {
500 frappe.call({
501 method: "erpnext.stock.get_item_details.get_conversion_factor",
502 args: { item_code: this.doc.item_code, uom: this.value },
503 callback: r => {
504 if(!r.exc) {
505 if (this.doc.conversion_factor == r.message.conversion_factor) return;
marination4f395cc2020-09-24 12:43:41 +0530506
Saqib61314242020-09-15 11:14:31 +0530507 const docname = this.doc.docname;
508 dialog.fields_dict.trans_items.df.data.some(doc => {
509 if (doc.docname == docname) {
510 doc.conversion_factor = r.message.conversion_factor;
511 dialog.fields_dict.trans_items.grid.refresh();
512 return true;
513 }
514 })
515 }
516 }
517 });
518 }
519 }, {
Saqib Ansarif53299e2020-04-15 22:08:12 +0530520 fieldtype:'Float',
521 fieldname:"qty",
522 default: 0,
523 read_only: 0,
524 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530525 label: __('Qty'),
526 precision: get_precision("qty")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530527 }, {
528 fieldtype:'Currency',
529 fieldname:"rate",
Afshan84184552021-02-24 16:51:11 +0530530 options: "currency",
Saqib Ansarif53299e2020-04-15 22:08:12 +0530531 default: 0,
532 read_only: 0,
533 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530534 label: __('Rate'),
535 precision: get_precision("rate")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530536 }];
537
538 if (frm.doc.doctype == 'Sales Order' || frm.doc.doctype == 'Purchase Order' ) {
539 fields.splice(2, 0, {
540 fieldtype: 'Date',
541 fieldname: frm.doc.doctype == 'Sales Order' ? "delivery_date" : "schedule_date",
542 in_list_view: 1,
Saqib Ansaric579b082020-05-29 22:21:50 +0530543 label: frm.doc.doctype == 'Sales Order' ? __("Delivery Date") : __("Reqd by date"),
544 reqd: 1
Saqib Ansarif53299e2020-04-15 22:08:12 +0530545 })
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530546 fields.splice(3, 0, {
547 fieldtype: 'Float',
548 fieldname: "conversion_factor",
549 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530550 label: __("Conversion Factor"),
551 precision: get_precision('conversion_factor')
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530552 })
Saqib Ansarif53299e2020-04-15 22:08:12 +0530553 }
554
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800555 const dialog = new frappe.ui.Dialog({
556 title: __("Update Items"),
557 fields: [
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800558 {
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100559 fieldname: "trans_items",
560 fieldtype: "Table",
Maricafc96c1a2020-03-06 10:57:59 +0530561 label: "Items",
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100562 cannot_add_rows: cannot_add_row,
Saqibbc919e22020-11-05 17:38:35 +0530563 in_place_edit: false,
Maricafc96c1a2020-03-06 10:57:59 +0530564 reqd: 1,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100565 data: this.data,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800566 get_data: () => {
567 return this.data;
568 },
Saqib Ansarif53299e2020-04-15 22:08:12 +0530569 fields: fields
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800570 },
571 ],
572 primary_action: function() {
Ankush Menat6de7b8e2021-08-24 12:18:40 +0530573 const trans_items = this.get_values()["trans_items"].filter((item) => !!item.item_code);
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800574 frappe.call({
575 method: 'erpnext.controllers.accounts_controller.update_child_qty_rate',
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100576 freeze: true,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800577 args: {
578 'parent_doctype': frm.doc.doctype,
579 'trans_items': trans_items,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100580 'parent_doctype_name': frm.doc.name,
581 'child_docname': child_docname
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800582 },
583 callback: function() {
584 frm.reload_doc();
585 }
586 });
587 this.hide();
588 refresh_field("items");
589 },
590 primary_action_label: __('Update')
591 });
592
593 frm.doc[opts.child_docname].forEach(d => {
594 dialog.fields_dict.trans_items.df.data.push({
595 "docname": d.name,
Prssanna Desai4f72c0b2019-12-23 18:27:12 +0530596 "name": d.name,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800597 "item_code": d.item_code,
Saqib Ansarif53299e2020-04-15 22:08:12 +0530598 "delivery_date": d.delivery_date,
599 "schedule_date": d.schedule_date,
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530600 "conversion_factor": d.conversion_factor,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800601 "qty": d.qty,
602 "rate": d.rate,
Saqib61314242020-09-15 11:14:31 +0530603 "uom": d.uom
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800604 });
605 this.data = dialog.fields_dict.trans_items.df.data;
606 dialog.fields_dict.trans_items.grid.refresh();
607 })
608 dialog.show();
609}
610
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530611erpnext.utils.map_current_doc = function(opts) {
Sagar Vora78777d62021-03-09 20:35:08 +0530612 function _map() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530613 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530614 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530615 if(!cur_frm.doc.items[0].item_code) {
616 cur_frm.doc.items = cur_frm.doc.items.splice(1);
617 }
bhupen3c7e70f2016-11-30 14:32:11 +0530618
619 // find the doctype of the items table
620 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530621
bhupen3c7e70f2016-11-30 14:32:11 +0530622 // find the link fieldname from items table for the given
623 // source_doctype
624 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530625 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530626 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
627
Nabin Hait802b4352017-01-09 15:32:20 +0530628 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530629 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530630 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530631
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530632 $.each(cur_frm.doc.items, function(i, d) {
633 opts.source_name.forEach(function(src) {
634 if(d[link_fieldname]==src) {
635 already_set = true;
636 if (item_qty_map[d.item_code])
637 item_qty_map[d.item_code] += flt(d.qty);
638 else
639 item_qty_map[d.item_code] = flt(d.qty);
640 }
641 });
642 });
643
644 if(already_set) {
645 opts.source_name.forEach(function(src) {
646 frappe.model.with_doc(opts.source_doctype, src, function(r) {
647 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
648 $.each(source_doc.items || [], function(i, row) {
649 if(row.qty > flt(item_qty_map[row.item_code])) {
650 already_set = false;
651 return false;
652 }
653 })
654 })
655
656 if(already_set) {
657 frappe.msgprint(__("You have already selected items from {0} {1}",
658 [opts.source_doctype, src]));
659 return;
660 }
661
662 })
Nabin Hait802b4352017-01-09 15:32:20 +0530663 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530664 }
665
666 return frappe.call({
667 // Sometimes we hit the limit for URL length of a GET request
668 // as we send the full target_doc. Hence this is a POST request.
669 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530670 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530671 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530672 "method": opts.method,
673 "source_names": opts.source_name,
674 "target_doc": cur_frm.doc,
Maricadb002702020-02-17 15:58:08 +0530675 "args": opts.args
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530676 },
677 callback: function(r) {
678 if(!r.exc) {
679 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530680 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530681 cur_frm.refresh();
682 }
683 }
684 });
685 }
Sagar Vora78777d62021-03-09 20:35:08 +0530686
687 let query_args = {};
688 if (opts.get_query_filters) {
689 query_args.filters = opts.get_query_filters;
690 }
691
692 if (opts.get_query_method) {
693 query_args.query = opts.get_query_method;
694 }
695
696 if (query_args.filters || query_args.query) {
697 opts.get_query = () => query_args;
698 }
699
700 if (opts.source_doctype) {
701 const d = new frappe.ui.form.MultiSelectDialog({
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530702 doctype: opts.source_doctype,
703 target: opts.target,
704 date_field: opts.date_field || undefined,
705 setters: opts.setters,
706 get_query: opts.get_query,
marination4f395cc2020-09-24 12:43:41 +0530707 add_filters_group: 1,
Saqib7292f542021-09-13 12:13:43 +0530708 allow_child_item_selection: opts.allow_child_item_selection,
gn3060292a619fd2022-06-29 17:15:21 +0800709 child_fieldname: opts.child_fieldname,
Saqib7292f542021-09-13 12:13:43 +0530710 child_columns: opts.child_columns,
Rohit Waghchaure69ffddf2021-10-07 17:33:58 +0530711 size: opts.size,
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530712 action: function(selections, args) {
713 let values = selections;
Saqibe03d9aa2021-09-17 13:03:27 +0530714 if (values.length === 0) {
Nabin Haita11dcb62017-12-04 13:36:50 +0530715 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530716 return;
717 }
718 opts.source_name = values;
Saqibe03d9aa2021-09-17 13:03:27 +0530719 if (opts.allow_child_item_selection) {
720 // args contains filtered child docnames
721 opts.args = args;
722 }
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530723 d.dialog.hide();
724 _map();
725 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530726 });
Sagar Vora78777d62021-03-09 20:35:08 +0530727
728 return d;
729 }
730
731 if (opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530732 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530733 _map();
734 }
735}
736
Rushabh Mehta180e4352016-07-26 17:57:42 +0530737frappe.form.link_formatters['Item'] = function(value, doc) {
Alan157b3882021-04-21 21:00:22 +0530738 if (doc && value && doc.item_name && doc.item_name !== value && doc.item_code === value) {
marination824f48f2020-10-12 20:08:03 +0530739 return value + ': ' + doc.item_name;
740 } else if (!value && doc.doctype && doc.item_name) {
741 // format blank value in child table
742 return doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530743 } else {
marination824f48f2020-10-12 20:08:03 +0530744 // if value is blank in report view or item code and name are the same, return as is
Rushabh Mehta180e4352016-07-26 17:57:42 +0530745 return value;
746 }
747}
748
749frappe.form.link_formatters['Employee'] = function(value, doc) {
Rucha Mahabal5ba1bc12021-11-23 09:30:30 +0530750 if (doc && value && doc.employee_name && doc.employee_name !== value && doc.employee === value) {
751 return value + ': ' + doc.employee_name;
752 } else if (!value && doc.doctype && doc.employee_name) {
753 // format blank value in child table
754 return doc.employee;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530755 } else {
Rucha Mahabal5ba1bc12021-11-23 09:30:30 +0530756 // if value is blank in report view or project name and name are the same, return as is
Rushabh Mehta180e4352016-07-26 17:57:42 +0530757 return value;
758 }
759}
760
Rucha Mahabal59961f72021-05-20 23:43:19 +0530761frappe.form.link_formatters['Project'] = function(value, doc) {
762 if (doc && value && doc.project_name && doc.project_name !== value && doc.project === value) {
763 return value + ': ' + doc.project_name;
764 } else if (!value && doc.doctype && doc.project_name) {
765 // format blank value in child table
766 return doc.project;
767 } else {
768 // if value is blank in report view or project name and name are the same, return as is
769 return value;
770 }
771};
772
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530773// add description on posting time
774$(document).on('app_ready', function() {
775 if(!frappe.datetime.is_timezone_same()) {
776 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
777 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
778 frappe.ui.form.on(d, "onload", function(frm) {
779 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530780 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530781 });
782 });
783 }
akshay14384c22016-07-28 13:53:17 +0530784});
Alanc6dc9ea2021-05-07 20:30:04 +0530785
Himanshuec25d592021-06-14 19:05:52 +0530786// Show SLA dashboard
787$(document).on('app_ready', function() {
788 frappe.call({
789 method: 'erpnext.support.doctype.service_level_agreement.service_level_agreement.get_sla_doctypes',
790 callback: function(r) {
791 if (!r.message)
792 return;
793
794 $.each(r.message, function(_i, d) {
795 frappe.ui.form.on(d, {
796 onload: function(frm) {
797 if (!frm.doc.service_level_agreement)
798 return;
799
800 frappe.call({
801 method: 'erpnext.support.doctype.service_level_agreement.service_level_agreement.get_service_level_agreement_filters',
802 args: {
803 doctype: frm.doc.doctype,
804 name: frm.doc.service_level_agreement,
805 customer: frm.doc.customer
806 },
807 callback: function (r) {
808 if (r && r.message) {
809 frm.set_query('priority', function() {
810 return {
811 filters: {
812 'name': ['in', r.message.priority],
813 }
814 };
815 });
816 frm.set_query('service_level_agreement', function() {
817 return {
818 filters: {
819 'name': ['in', r.message.service_level_agreements],
820 }
821 };
822 });
823 }
824 }
825 });
826 },
827
828 refresh: function(frm) {
829 if (frm.doc.status !== 'Closed' && frm.doc.service_level_agreement
Saqib Ansari476e81a2021-12-06 18:55:30 +0530830 && ['First Response Due', 'Resolution Due'].includes(frm.doc.agreement_status)) {
Himanshuec25d592021-06-14 19:05:52 +0530831 frappe.call({
832 'method': 'frappe.client.get',
833 args: {
834 doctype: 'Service Level Agreement',
835 name: frm.doc.service_level_agreement
836 },
837 callback: function(data) {
838 let statuses = data.message.pause_sla_on;
839 const hold_statuses = [];
840 $.each(statuses, (_i, entry) => {
841 hold_statuses.push(entry.status);
842 });
843 if (hold_statuses.includes(frm.doc.status)) {
844 frm.dashboard.clear_headline();
845 let message = {'indicator': 'orange', 'msg': __('SLA is on hold since {0}', [moment(frm.doc.on_hold_since).fromNow(true)])};
846 frm.dashboard.set_headline_alert(
847 '<div class="row">' +
848 '<div class="col-xs-12">' +
849 '<span class="indicator whitespace-nowrap '+ message.indicator +'"><span>'+ message.msg +'</span></span> ' +
850 '</div>' +
851 '</div>'
852 );
853 } else {
854 set_time_to_resolve_and_response(frm, data.message.apply_sla_for_resolution);
855 }
856 }
857 });
858 } else if (frm.doc.service_level_agreement) {
859 frm.dashboard.clear_headline();
860
861 let agreement_status = (frm.doc.agreement_status == 'Fulfilled') ?
862 {'indicator': 'green', 'msg': 'Service Level Agreement has been fulfilled'} :
863 {'indicator': 'red', 'msg': 'Service Level Agreement Failed'};
864
865 frm.dashboard.set_headline_alert(
866 '<div class="row">' +
867 '<div class="col-xs-12">' +
868 '<span class="indicator whitespace-nowrap '+ agreement_status.indicator +'"><span class="hidden-xs">'+ agreement_status.msg +'</span></span> ' +
869 '</div>' +
870 '</div>'
871 );
872 }
873 },
874 });
875 });
876 }
877 });
878});
879
880function set_time_to_resolve_and_response(frm, apply_sla_for_resolution) {
881 frm.dashboard.clear_headline();
882
Saqib24114772021-12-15 11:45:34 +0530883 let time_to_respond;
Saqib Ansari476e81a2021-12-06 18:55:30 +0530884 if (!frm.doc.first_responded_on) {
Himanshuec25d592021-06-14 19:05:52 +0530885 time_to_respond = get_time_left(frm.doc.response_by, frm.doc.agreement_status);
Saqib24114772021-12-15 11:45:34 +0530886 } else {
887 time_to_respond = get_status(frm.doc.response_by, frm.doc.first_responded_on);
Himanshuec25d592021-06-14 19:05:52 +0530888 }
889
890 let alert = `
891 <div class="row">
892 <div class="col-xs-12 col-sm-6">
893 <span class="indicator whitespace-nowrap ${time_to_respond.indicator}">
894 <span>Time to Respond: ${time_to_respond.diff_display}</span>
895 </span>
896 </div>`;
897
898
899 if (apply_sla_for_resolution) {
Saqib24114772021-12-15 11:45:34 +0530900 let time_to_resolve;
Saqib Ansari476e81a2021-12-06 18:55:30 +0530901 if (!frm.doc.resolution_date) {
Himanshuec25d592021-06-14 19:05:52 +0530902 time_to_resolve = get_time_left(frm.doc.resolution_by, frm.doc.agreement_status);
Saqib24114772021-12-15 11:45:34 +0530903 } else {
904 time_to_resolve = get_status(frm.doc.resolution_by, frm.doc.resolution_date);
Himanshuec25d592021-06-14 19:05:52 +0530905 }
906
907 alert += `
908 <div class="col-xs-12 col-sm-6">
909 <span class="indicator whitespace-nowrap ${time_to_resolve.indicator}">
910 <span>Time to Resolve: ${time_to_resolve.diff_display}</span>
911 </span>
912 </div>`;
913 }
914
915 alert += '</div>';
916
917 frm.dashboard.set_headline_alert(alert);
918}
919
920function get_time_left(timestamp, agreement_status) {
921 const diff = moment(timestamp).diff(moment());
922 const diff_display = diff >= 44500 ? moment.duration(diff).humanize() : 'Failed';
923 let indicator = (diff_display == 'Failed' && agreement_status != 'Fulfilled') ? 'red' : 'green';
924 return {'diff_display': diff_display, 'indicator': indicator};
925}
926
Saqib24114772021-12-15 11:45:34 +0530927function get_status(expected, actual) {
928 const time_left = moment(expected).diff(moment(actual));
Saqib Ansari476e81a2021-12-06 18:55:30 +0530929 if (time_left >= 0) {
Himanshuec25d592021-06-14 19:05:52 +0530930 return {'diff_display': 'Fulfilled', 'indicator': 'green'};
931 } else {
932 return {'diff_display': 'Failed', 'indicator': 'red'};
933 }
934}
935
Alanc6dc9ea2021-05-07 20:30:04 +0530936function attach_selector_button(inner_text, append_loction, context, grid_row) {
937 let $btn_div = $("<div>").css({"margin-bottom": "10px", "margin-top": "10px"})
938 .appendTo(append_loction);
939 let $btn = $(`<button class="btn btn-sm btn-default">${inner_text}</button>`)
940 .appendTo($btn_div);
941
942 $btn.on("click", function() {
943 context.show_serial_batch_selector(grid_row.frm, grid_row.doc, "", "", true);
944 });
945}