blob: 7f39b990bf0938e8219ee5c4acd7dde8e793ad31 [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
87 proceed_save_with_reminders_frequency_change: () => {
88 frappe.ui.hide_open_dialog();
Ankush Menatb147b852021-09-01 16:45:57 +053089
Frappe PR Bot255b99e2021-08-24 20:19:22 +053090 frappe.call({
Ankush Menatb147b852021-09-01 16:45:57 +053091 method: 'erpnext.hr.doctype.hr_settings.hr_settings.set_proceed_with_frequency_change',
Frappe PR Bot255b99e2021-08-24 20:19:22 +053092 callback: () => {
93 cur_frm.save();
94 }
95 });
Nabin Hait1ed1c4e2019-11-25 12:33:40 +053096 }
Rushabh Mehtac38fc712014-04-16 17:21:25 +053097});
Rushabh Mehta7d368752014-11-24 14:16:47 +053098
Nabin Haitb072c742014-12-16 12:49:13 +053099
100$.extend(erpnext.utils, {
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530101 set_party_dashboard_indicators: function(frm) {
102 if(frm.doc.__onload && frm.doc.__onload.dashboard_info) {
deepeshgarg007920dc142018-11-23 10:17:28 +0530103 var company_wise_info = frm.doc.__onload.dashboard_info;
deepeshgarg007f31caff2018-11-27 15:04:12 +0530104 if(company_wise_info.length > 1) {
deepeshgarg007a1cffc32018-11-23 16:51:35 +0530105 company_wise_info.forEach(function(info) {
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530106 erpnext.utils.add_indicator_for_multicompany(frm, info);
deepeshgarg007a1cffc32018-11-23 16:51:35 +0530107 });
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530108 } else if (company_wise_info.length === 1) {
deepeshgarg00764238ee2018-12-25 16:28:39 +0530109 frm.dashboard.add_indicator(__('Annual Billing: {0}',
110 [format_currency(company_wise_info[0].billing_this_year, company_wise_info[0].currency)]), 'blue');
111 frm.dashboard.add_indicator(__('Total Unpaid: {0}',
112 [format_currency(company_wise_info[0].total_unpaid, company_wise_info[0].currency)]),
deepeshgarg0078300f5e2019-01-01 20:28:49 +0530113 company_wise_info[0].total_unpaid ? 'orange' : 'green');
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530114
deepeshgarg00764238ee2018-12-25 16:28:39 +0530115 if(company_wise_info[0].loyalty_points) {
116 frm.dashboard.add_indicator(__('Loyalty Points: {0}',
117 [company_wise_info[0].loyalty_points]), 'blue');
118 }
deepeshgarg007f31caff2018-11-27 15:04:12 +0530119 }
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530120 }
121 },
122
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530123 add_indicator_for_multicompany: function(frm, info) {
124 frm.dashboard.stats_area.removeClass('hidden');
125 frm.dashboard.stats_area_row.addClass('flex');
126 frm.dashboard.stats_area_row.css('flex-wrap', 'wrap');
127
128 var color = info.total_unpaid ? 'orange' : 'green';
129
130 var indicator = $('<div class="flex-column col-xs-6">'+
131 '<div style="margin-top:10px"><h6>'+info.company+'</h6></div>'+
132
133 '<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
134 'Annual Billing: '+format_currency(info.billing_this_year, info.currency)+'</span></div>'+
135
136 '<div class="badge-link small" style="margin-bottom:10px">'+
137 '<span class="indicator '+color+'">Total Unpaid: '
138 +format_currency(info.total_unpaid, info.currency)+'</span></div>'+
139
140
141 '</div>').appendTo(frm.dashboard.stats_area_row);
142
143 if(info.loyalty_points){
144 $('<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
145 'Loyalty Points: '+info.loyalty_points+'</span></div>').appendTo(indicator);
146 }
147
148 return indicator;
149 },
150
Shreya Shah149f7ee2018-03-27 11:29:25 +0530151 get_party_name: function(party_type) {
152 var dict = {'Customer': 'customer_name', 'Supplier': 'supplier_name', 'Employee': 'employee_name',
153 'Member': 'member_name'};
154 return dict[party_type];
155 },
156
Rushabh Mehta49f97472018-08-30 18:50:48 +0530157 copy_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname) {
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +0530158 var d = locals[dt][dn];
159 if(d[fieldname]){
160 var cl = doc[table_fieldname] || [];
161 for(var i = 0; i < cl.length; i++) {
162 if(!cl[i][fieldname]) cl[i][fieldname] = d[fieldname];
163 }
164 }
165 refresh_field(table_fieldname);
Makarand Bauskar157c3342017-05-26 21:32:33 +0530166 },
167
168 get_terms: function(tc_name, doc, callback) {
169 if(tc_name) {
170 return frappe.call({
171 method: 'erpnext.setup.doctype.terms_and_conditions.terms_and_conditions.get_terms_and_conditions',
172 args: {
173 template_name: tc_name,
174 doc: doc
175 },
176 callback: function(r) {
177 callback(r)
178 }
179 });
180 }
tundebabzy1a947db2017-08-29 13:48:27 +0100181 },
182
rohitwaghchaure713cfc72018-09-11 17:40:37 +0530183 make_bank_account: function(doctype, docname) {
184 frappe.call({
185 method: "erpnext.accounts.doctype.bank_account.bank_account.make_bank_account",
186 args: {
187 doctype: doctype,
188 docname: docname
189 },
190 freeze: true,
191 callback: function(r) {
192 var doclist = frappe.model.sync(r.message);
193 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
194 }
195 })
196 },
197
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530198 add_dimensions: function(report_name, index) {
199 let filters = frappe.query_reports[report_name].filters;
200
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530201 frappe.call({
202 method: "erpnext.accounts.doctype.accounting_dimension.accounting_dimension.get_dimensions",
203 callback: function(r) {
204 let accounting_dimensions = r.message[0];
205 accounting_dimensions.forEach((dimension) => {
206 let found = filters.some(el => el.fieldname === dimension['fieldname']);
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530207
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530208 if (!found) {
Deepesh Garg23ab5c52020-12-31 11:29:06 +0530209 filters.splice(index, 0, {
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530210 "fieldname": dimension["fieldname"],
211 "label": __(dimension["label"]),
212 "fieldtype": "Link",
213 "options": dimension["document_type"]
214 });
215 }
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530216 });
217 }
218 });
219 },
220
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530221 make_subscription: function(doctype, docname) {
222 frappe.call({
Rucha Mahabal65a627c2019-07-17 13:50:32 +0530223 method: "frappe.automation.doctype.auto_repeat.auto_repeat.make_auto_repeat",
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530224 args: {
225 doctype: doctype,
226 docname: docname
227 },
228 callback: function(r) {
229 var doclist = frappe.model.sync(r.message);
230 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
231 }
232 })
233 },
234
Saif90cf2dd2018-09-30 21:46:31 +0500235 make_pricing_rule: function(doctype, docname) {
236 frappe.call({
237 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.make_pricing_rule",
238 args: {
239 doctype: doctype,
240 docname: docname
241 },
242 callback: function(r) {
243 var doclist = frappe.model.sync(r.message);
244 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
245 }
246 })
247 },
248
tundebabzy1a947db2017-08-29 13:48:27 +0100249 /**
250 * Checks if the first row of a given child table is empty
251 * @param child_table - Child table Doctype
252 * @return {Boolean}
253 **/
254 first_row_is_empty: function(child_table){
255 if($.isArray(child_table) && child_table.length > 0) {
256 return !child_table[0].item_code;
257 }
258 return false;
259 },
260
261 /**
262 * Removes the first row of a child table if it is empty
263 * @param {_Frm} frm - The current form
264 * @param {String} child_table_name - The child table field name
265 * @return {Boolean}
266 **/
267 remove_empty_first_row: function(frm, child_table_name){
268 const rows = frm['doc'][child_table_name];
269 if (this.first_row_is_empty(rows)){
270 frm['doc'][child_table_name] = rows.splice(1);
271 }
272 return rows;
273 },
Zarrar6ffdf942018-06-14 12:24:16 +0530274 get_tree_options: function(option) {
275 // get valid options for tree based on user permission & locals dict
276 let unscrub_option = frappe.model.unscrub(option);
277 let user_permission = frappe.defaults.get_user_permissions();
deepeshgarg0070d64d622019-02-20 17:55:14 +0530278 let options;
279
Zarrar6ffdf942018-06-14 12:24:16 +0530280 if(user_permission && user_permission[unscrub_option]) {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530281 options = user_permission[unscrub_option].map(perm => perm.doc);
Zarrar6ffdf942018-06-14 12:24:16 +0530282 } else {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530283 options = $.map(locals[`:${unscrub_option}`], function(c) { return c.name; }).sort();
Zarrar6ffdf942018-06-14 12:24:16 +0530284 }
deepeshgarg0070d64d622019-02-20 17:55:14 +0530285
286 // filter unique values, as there may be multiple user permissions for any value
287 return options.filter((value, index, self) => self.indexOf(value) === index);
Zarrar6ffdf942018-06-14 12:24:16 +0530288 },
289 get_tree_default: function(option) {
290 // set default for a field based on user permission
291 let options = this.get_tree_options(option);
292 if(options.includes(frappe.defaults.get_default(option))) {
293 return frappe.defaults.get_default(option);
294 } else {
295 return options[0];
296 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400297 },
Andy Zhubffe9332021-04-12 22:49:26 +1200298 overrides_parent_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname, parent_fieldname) {
Marica24e45162021-04-14 18:53:15 +0530299 if (doc[parent_fieldname]) {
Andy Zhubffe9332021-04-12 22:49:26 +1200300 let cl = doc[table_fieldname] || [];
Marica24e45162021-04-14 18:53:15 +0530301 for (let i = 0; i < cl.length; i++) {
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400302 cl[i][fieldname] = doc[parent_fieldname];
303 }
Andy Zhubffe9332021-04-12 22:49:26 +1200304 frappe.refresh_field(table_fieldname);
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400305 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400306 },
Nabin Hait34c551d2019-07-03 10:34:31 +0530307 create_new_doc: function (doctype, update_fields) {
308 frappe.model.with_doctype(doctype, function() {
309 var new_doc = frappe.model.get_new_doc(doctype);
310 for (let [key, value] of Object.entries(update_fields)) {
311 new_doc[key] = value;
312 }
313 frappe.ui.form.make_quick_entry(doctype, null, null, new_doc);
314 });
315 }
316
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530317});
318
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530319erpnext.utils.select_alternate_items = function(opts) {
320 const frm = opts.frm;
321 const warehouse_field = opts.warehouse_field || 'warehouse';
322 const item_field = opts.item_field || 'item_code';
323
324 this.data = [];
325 const dialog = new frappe.ui.Dialog({
326 title: __("Select Alternate Item"),
327 fields: [
328 {fieldtype:'Section Break', label: __('Items')},
329 {
330 fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true,
331 in_place_edit: true, data: this.data,
332 get_data: () => {
333 return this.data;
334 },
335 fields: [{
336 fieldtype:'Data',
337 fieldname:"docname",
338 hidden: 1
339 }, {
340 fieldtype:'Link',
341 fieldname:"item_code",
342 options: 'Item',
343 in_list_view: 1,
344 read_only: 1,
345 label: __('Item Code')
346 }, {
347 fieldtype:'Link',
348 fieldname:"alternate_item",
349 options: 'Item',
350 default: "",
351 in_list_view: 1,
352 label: __('Alternate Item'),
353 onchange: function() {
354 const item_code = this.get_value();
355 const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value();
356 if (item_code && warehouse) {
357 frappe.call({
358 method: "erpnext.stock.utils.get_latest_stock_qty",
359 args: {
360 item_code: item_code,
361 warehouse: warehouse
362 },
363 callback: (r) => {
364 this.grid_row.on_grid_fields_dict
365 .actual_qty.set_value(r.message || 0);
366 }
367 })
368 }
369 },
370 get_query: (e) => {
371 return {
372 query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items",
373 filters: {
374 item_code: e.item_code
375 }
376 };
377 }
378 }, {
379 fieldtype:'Link',
380 fieldname:"warehouse",
381 options: 'Warehouse',
382 default: "",
383 in_list_view: 1,
384 label: __('Warehouse'),
385 onchange: function() {
386 const warehouse = this.get_value();
387 const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value();
388 if (item_code && warehouse) {
389 frappe.call({
390 method: "erpnext.stock.utils.get_latest_stock_qty",
391 args: {
392 item_code: item_code,
393 warehouse: warehouse
394 },
395 callback: (r) => {
396 this.grid_row.on_grid_fields_dict
397 .actual_qty.set_value(r.message || 0);
398 }
399 })
400 }
401 },
402 }, {
403 fieldtype:'Float',
404 fieldname:"actual_qty",
405 default: 0,
406 read_only: 1,
407 in_list_view: 1,
408 label: __('Available Qty')
409 }]
410 },
411 ],
412 primary_action: function() {
413 const args = this.get_values()["alternative_items"];
414 const alternative_items = args.filter(d => {
415 if (d.alternate_item && d.item_code != d.alternate_item) {
416 return true;
417 }
418 });
419
420 alternative_items.forEach(d => {
421 let row = frappe.get_doc(opts.child_doctype, d.docname);
Doridel Cahanape42192b2018-05-16 13:28:39 +0800422 let qty = null;
423 if (row.doctype === 'Work Order Item') {
424 qty = row.required_qty;
425 } else {
426 qty = row.qty;
427 }
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530428 row[item_field] = d.alternate_item;
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530429 frm.script_manager.trigger(item_field, row.doctype, row.name)
430 .then(() => {
431 frappe.model.set_value(row.doctype, row.name, 'qty', qty);
432 frappe.model.set_value(row.doctype, row.name,
433 opts.original_item_field, d.item_code);
434 });
435 });
436
437 refresh_field(opts.child_docname);
438 this.hide();
439 },
440 primary_action_label: __('Update')
441 });
442
443 frm.doc[opts.child_docname].forEach(d => {
444 if (!opts.condition || opts.condition(d)) {
445 dialog.fields_dict.alternative_items.df.data.push({
446 "docname": d.name,
447 "item_code": d[item_field],
448 "warehouse": d[warehouse_field],
449 "actual_qty": d.actual_qty
450 });
451 }
452 })
453
454 this.data = dialog.fields_dict.alternative_items.df.data;
455 dialog.fields_dict.alternative_items.grid.refresh();
456 dialog.show();
457}
458
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800459erpnext.utils.update_child_items = function(opts) {
460 const frm = opts.frm;
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100461 const cannot_add_row = (typeof opts.cannot_add_row === 'undefined') ? true : opts.cannot_add_row;
462 const child_docname = (typeof opts.cannot_add_row === 'undefined') ? "items" : opts.child_docname;
Saqib56fea7d2020-10-09 21:19:25 +0530463 const child_meta = frappe.get_meta(`${frm.doc.doctype} Item`);
464 const get_precision = (fieldname) => child_meta.fields.find(f => f.fieldname == fieldname).precision;
465
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800466 this.data = [];
Saqib Ansarif53299e2020-04-15 22:08:12 +0530467 const fields = [{
468 fieldtype:'Data',
469 fieldname:"docname",
470 read_only: 1,
471 hidden: 1,
472 }, {
473 fieldtype:'Link',
474 fieldname:"item_code",
475 options: 'Item',
476 in_list_view: 1,
477 read_only: 0,
478 disabled: 0,
Afshandc7280e2021-08-18 17:44:40 +0530479 label: __('Item Code'),
480 get_query: function() {
481 let filters;
482 if (frm.doc.doctype == 'Sales Order') {
483 filters = {"is_sales_item": 1};
484 } else if (frm.doc.doctype == 'Purchase Order') {
485 if (frm.doc.is_subcontracted == "Yes") {
486 filters = {"is_sub_contracted_item": 1};
487 } else {
488 filters = {"is_purchase_item": 1};
489 }
490 }
491 return {
492 query: "erpnext.controllers.queries.item_query",
493 filters: filters
494 };
495 }
Saqib Ansarif53299e2020-04-15 22:08:12 +0530496 }, {
Saqib61314242020-09-15 11:14:31 +0530497 fieldtype:'Link',
498 fieldname:'uom',
499 options: 'UOM',
500 read_only: 0,
501 label: __('UOM'),
502 reqd: 1,
503 onchange: function () {
504 frappe.call({
505 method: "erpnext.stock.get_item_details.get_conversion_factor",
506 args: { item_code: this.doc.item_code, uom: this.value },
507 callback: r => {
508 if(!r.exc) {
509 if (this.doc.conversion_factor == r.message.conversion_factor) return;
marination4f395cc2020-09-24 12:43:41 +0530510
Saqib61314242020-09-15 11:14:31 +0530511 const docname = this.doc.docname;
512 dialog.fields_dict.trans_items.df.data.some(doc => {
513 if (doc.docname == docname) {
514 doc.conversion_factor = r.message.conversion_factor;
515 dialog.fields_dict.trans_items.grid.refresh();
516 return true;
517 }
518 })
519 }
520 }
521 });
522 }
523 }, {
Saqib Ansarif53299e2020-04-15 22:08:12 +0530524 fieldtype:'Float',
525 fieldname:"qty",
526 default: 0,
527 read_only: 0,
528 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530529 label: __('Qty'),
530 precision: get_precision("qty")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530531 }, {
532 fieldtype:'Currency',
533 fieldname:"rate",
Afshan84184552021-02-24 16:51:11 +0530534 options: "currency",
Saqib Ansarif53299e2020-04-15 22:08:12 +0530535 default: 0,
536 read_only: 0,
537 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530538 label: __('Rate'),
539 precision: get_precision("rate")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530540 }];
541
542 if (frm.doc.doctype == 'Sales Order' || frm.doc.doctype == 'Purchase Order' ) {
543 fields.splice(2, 0, {
544 fieldtype: 'Date',
545 fieldname: frm.doc.doctype == 'Sales Order' ? "delivery_date" : "schedule_date",
546 in_list_view: 1,
Saqib Ansaric579b082020-05-29 22:21:50 +0530547 label: frm.doc.doctype == 'Sales Order' ? __("Delivery Date") : __("Reqd by date"),
548 reqd: 1
Saqib Ansarif53299e2020-04-15 22:08:12 +0530549 })
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530550 fields.splice(3, 0, {
551 fieldtype: 'Float',
552 fieldname: "conversion_factor",
553 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530554 label: __("Conversion Factor"),
555 precision: get_precision('conversion_factor')
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530556 })
Saqib Ansarif53299e2020-04-15 22:08:12 +0530557 }
558
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800559 const dialog = new frappe.ui.Dialog({
560 title: __("Update Items"),
561 fields: [
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800562 {
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100563 fieldname: "trans_items",
564 fieldtype: "Table",
Maricafc96c1a2020-03-06 10:57:59 +0530565 label: "Items",
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100566 cannot_add_rows: cannot_add_row,
Saqibbc919e22020-11-05 17:38:35 +0530567 in_place_edit: false,
Maricafc96c1a2020-03-06 10:57:59 +0530568 reqd: 1,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100569 data: this.data,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800570 get_data: () => {
571 return this.data;
572 },
Saqib Ansarif53299e2020-04-15 22:08:12 +0530573 fields: fields
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800574 },
575 ],
576 primary_action: function() {
Ankush Menat6de7b8e2021-08-24 12:18:40 +0530577 const trans_items = this.get_values()["trans_items"].filter((item) => !!item.item_code);
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800578 frappe.call({
579 method: 'erpnext.controllers.accounts_controller.update_child_qty_rate',
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100580 freeze: true,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800581 args: {
582 'parent_doctype': frm.doc.doctype,
583 'trans_items': trans_items,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100584 'parent_doctype_name': frm.doc.name,
585 'child_docname': child_docname
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800586 },
587 callback: function() {
588 frm.reload_doc();
589 }
590 });
591 this.hide();
592 refresh_field("items");
593 },
594 primary_action_label: __('Update')
595 });
596
597 frm.doc[opts.child_docname].forEach(d => {
598 dialog.fields_dict.trans_items.df.data.push({
599 "docname": d.name,
Prssanna Desai4f72c0b2019-12-23 18:27:12 +0530600 "name": d.name,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800601 "item_code": d.item_code,
Saqib Ansarif53299e2020-04-15 22:08:12 +0530602 "delivery_date": d.delivery_date,
603 "schedule_date": d.schedule_date,
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530604 "conversion_factor": d.conversion_factor,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800605 "qty": d.qty,
606 "rate": d.rate,
Saqib61314242020-09-15 11:14:31 +0530607 "uom": d.uom
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800608 });
609 this.data = dialog.fields_dict.trans_items.df.data;
610 dialog.fields_dict.trans_items.grid.refresh();
611 })
612 dialog.show();
613}
614
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530615erpnext.utils.map_current_doc = function(opts) {
Sagar Vora78777d62021-03-09 20:35:08 +0530616 function _map() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530617 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530618 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530619 if(!cur_frm.doc.items[0].item_code) {
620 cur_frm.doc.items = cur_frm.doc.items.splice(1);
621 }
bhupen3c7e70f2016-11-30 14:32:11 +0530622
623 // find the doctype of the items table
624 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530625
bhupen3c7e70f2016-11-30 14:32:11 +0530626 // find the link fieldname from items table for the given
627 // source_doctype
628 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530629 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530630 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
631
Nabin Hait802b4352017-01-09 15:32:20 +0530632 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530633 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530634 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530635
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530636 $.each(cur_frm.doc.items, function(i, d) {
637 opts.source_name.forEach(function(src) {
638 if(d[link_fieldname]==src) {
639 already_set = true;
640 if (item_qty_map[d.item_code])
641 item_qty_map[d.item_code] += flt(d.qty);
642 else
643 item_qty_map[d.item_code] = flt(d.qty);
644 }
645 });
646 });
647
648 if(already_set) {
649 opts.source_name.forEach(function(src) {
650 frappe.model.with_doc(opts.source_doctype, src, function(r) {
651 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
652 $.each(source_doc.items || [], function(i, row) {
653 if(row.qty > flt(item_qty_map[row.item_code])) {
654 already_set = false;
655 return false;
656 }
657 })
658 })
659
660 if(already_set) {
661 frappe.msgprint(__("You have already selected items from {0} {1}",
662 [opts.source_doctype, src]));
663 return;
664 }
665
666 })
Nabin Hait802b4352017-01-09 15:32:20 +0530667 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530668 }
669
670 return frappe.call({
671 // Sometimes we hit the limit for URL length of a GET request
672 // as we send the full target_doc. Hence this is a POST request.
673 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530674 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530675 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530676 "method": opts.method,
677 "source_names": opts.source_name,
678 "target_doc": cur_frm.doc,
Maricadb002702020-02-17 15:58:08 +0530679 "args": opts.args
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530680 },
681 callback: function(r) {
682 if(!r.exc) {
683 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530684 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530685 cur_frm.refresh();
686 }
687 }
688 });
689 }
Sagar Vora78777d62021-03-09 20:35:08 +0530690
691 let query_args = {};
692 if (opts.get_query_filters) {
693 query_args.filters = opts.get_query_filters;
694 }
695
696 if (opts.get_query_method) {
697 query_args.query = opts.get_query_method;
698 }
699
700 if (query_args.filters || query_args.query) {
701 opts.get_query = () => query_args;
702 }
703
704 if (opts.source_doctype) {
705 const d = new frappe.ui.form.MultiSelectDialog({
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530706 doctype: opts.source_doctype,
707 target: opts.target,
708 date_field: opts.date_field || undefined,
709 setters: opts.setters,
710 get_query: opts.get_query,
marination4f395cc2020-09-24 12:43:41 +0530711 add_filters_group: 1,
Saqib7292f542021-09-13 12:13:43 +0530712 allow_child_item_selection: opts.allow_child_item_selection,
713 child_fieldname: opts.child_fielname,
714 child_columns: opts.child_columns,
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530715 action: function(selections, args) {
716 let values = selections;
Saqibe03d9aa2021-09-17 13:03:27 +0530717 if (values.length === 0) {
Nabin Haita11dcb62017-12-04 13:36:50 +0530718 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530719 return;
720 }
721 opts.source_name = values;
Saqibe03d9aa2021-09-17 13:03:27 +0530722 if (opts.allow_child_item_selection) {
723 // args contains filtered child docnames
724 opts.args = args;
725 }
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530726 d.dialog.hide();
727 _map();
728 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530729 });
Sagar Vora78777d62021-03-09 20:35:08 +0530730
731 return d;
732 }
733
734 if (opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530735 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530736 _map();
737 }
738}
739
Rushabh Mehta180e4352016-07-26 17:57:42 +0530740frappe.form.link_formatters['Item'] = function(value, doc) {
Alan157b3882021-04-21 21:00:22 +0530741 if (doc && value && doc.item_name && doc.item_name !== value && doc.item_code === value) {
marination824f48f2020-10-12 20:08:03 +0530742 return value + ': ' + doc.item_name;
743 } else if (!value && doc.doctype && doc.item_name) {
744 // format blank value in child table
745 return doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530746 } else {
marination824f48f2020-10-12 20:08:03 +0530747 // 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 +0530748 return value;
749 }
750}
751
752frappe.form.link_formatters['Employee'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530753 if(doc && doc.employee_name && doc.employee_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530754 return value? value + ': ' + doc.employee_name: doc.employee_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530755 } else {
756 return value;
757 }
758}
759
Rucha Mahabal59961f72021-05-20 23:43:19 +0530760frappe.form.link_formatters['Project'] = function(value, doc) {
761 if (doc && value && doc.project_name && doc.project_name !== value && doc.project === value) {
762 return value + ': ' + doc.project_name;
763 } else if (!value && doc.doctype && doc.project_name) {
764 // format blank value in child table
765 return doc.project;
766 } else {
767 // if value is blank in report view or project name and name are the same, return as is
768 return value;
769 }
770};
771
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530772// add description on posting time
773$(document).on('app_ready', function() {
774 if(!frappe.datetime.is_timezone_same()) {
775 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
776 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
777 frappe.ui.form.on(d, "onload", function(frm) {
778 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530779 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530780 });
781 });
782 }
akshay14384c22016-07-28 13:53:17 +0530783});
Alanc6dc9ea2021-05-07 20:30:04 +0530784
Himanshuec25d592021-06-14 19:05:52 +0530785// Show SLA dashboard
786$(document).on('app_ready', function() {
787 frappe.call({
788 method: 'erpnext.support.doctype.service_level_agreement.service_level_agreement.get_sla_doctypes',
789 callback: function(r) {
790 if (!r.message)
791 return;
792
793 $.each(r.message, function(_i, d) {
794 frappe.ui.form.on(d, {
795 onload: function(frm) {
796 if (!frm.doc.service_level_agreement)
797 return;
798
799 frappe.call({
800 method: 'erpnext.support.doctype.service_level_agreement.service_level_agreement.get_service_level_agreement_filters',
801 args: {
802 doctype: frm.doc.doctype,
803 name: frm.doc.service_level_agreement,
804 customer: frm.doc.customer
805 },
806 callback: function (r) {
807 if (r && r.message) {
808 frm.set_query('priority', function() {
809 return {
810 filters: {
811 'name': ['in', r.message.priority],
812 }
813 };
814 });
815 frm.set_query('service_level_agreement', function() {
816 return {
817 filters: {
818 'name': ['in', r.message.service_level_agreements],
819 }
820 };
821 });
822 }
823 }
824 });
825 },
826
827 refresh: function(frm) {
828 if (frm.doc.status !== 'Closed' && frm.doc.service_level_agreement
829 && frm.doc.agreement_status === 'Ongoing') {
830 frappe.call({
831 'method': 'frappe.client.get',
832 args: {
833 doctype: 'Service Level Agreement',
834 name: frm.doc.service_level_agreement
835 },
836 callback: function(data) {
837 let statuses = data.message.pause_sla_on;
838 const hold_statuses = [];
839 $.each(statuses, (_i, entry) => {
840 hold_statuses.push(entry.status);
841 });
842 if (hold_statuses.includes(frm.doc.status)) {
843 frm.dashboard.clear_headline();
844 let message = {'indicator': 'orange', 'msg': __('SLA is on hold since {0}', [moment(frm.doc.on_hold_since).fromNow(true)])};
845 frm.dashboard.set_headline_alert(
846 '<div class="row">' +
847 '<div class="col-xs-12">' +
848 '<span class="indicator whitespace-nowrap '+ message.indicator +'"><span>'+ message.msg +'</span></span> ' +
849 '</div>' +
850 '</div>'
851 );
852 } else {
853 set_time_to_resolve_and_response(frm, data.message.apply_sla_for_resolution);
854 }
855 }
856 });
857 } else if (frm.doc.service_level_agreement) {
858 frm.dashboard.clear_headline();
859
860 let agreement_status = (frm.doc.agreement_status == 'Fulfilled') ?
861 {'indicator': 'green', 'msg': 'Service Level Agreement has been fulfilled'} :
862 {'indicator': 'red', 'msg': 'Service Level Agreement Failed'};
863
864 frm.dashboard.set_headline_alert(
865 '<div class="row">' +
866 '<div class="col-xs-12">' +
867 '<span class="indicator whitespace-nowrap '+ agreement_status.indicator +'"><span class="hidden-xs">'+ agreement_status.msg +'</span></span> ' +
868 '</div>' +
869 '</div>'
870 );
871 }
872 },
873 });
874 });
875 }
876 });
877});
878
879function set_time_to_resolve_and_response(frm, apply_sla_for_resolution) {
880 frm.dashboard.clear_headline();
881
882 let time_to_respond = get_status(frm.doc.response_by_variance);
883 if (!frm.doc.first_responded_on && frm.doc.agreement_status === 'Ongoing') {
884 time_to_respond = get_time_left(frm.doc.response_by, frm.doc.agreement_status);
885 }
886
887 let alert = `
888 <div class="row">
889 <div class="col-xs-12 col-sm-6">
890 <span class="indicator whitespace-nowrap ${time_to_respond.indicator}">
891 <span>Time to Respond: ${time_to_respond.diff_display}</span>
892 </span>
893 </div>`;
894
895
896 if (apply_sla_for_resolution) {
897 let time_to_resolve = get_status(frm.doc.resolution_by_variance);
898 if (!frm.doc.resolution_date && frm.doc.agreement_status === 'Ongoing') {
899 time_to_resolve = get_time_left(frm.doc.resolution_by, frm.doc.agreement_status);
900 }
901
902 alert += `
903 <div class="col-xs-12 col-sm-6">
904 <span class="indicator whitespace-nowrap ${time_to_resolve.indicator}">
905 <span>Time to Resolve: ${time_to_resolve.diff_display}</span>
906 </span>
907 </div>`;
908 }
909
910 alert += '</div>';
911
912 frm.dashboard.set_headline_alert(alert);
913}
914
915function get_time_left(timestamp, agreement_status) {
916 const diff = moment(timestamp).diff(moment());
917 const diff_display = diff >= 44500 ? moment.duration(diff).humanize() : 'Failed';
918 let indicator = (diff_display == 'Failed' && agreement_status != 'Fulfilled') ? 'red' : 'green';
919 return {'diff_display': diff_display, 'indicator': indicator};
920}
921
922function get_status(variance) {
923 if (variance > 0) {
924 return {'diff_display': 'Fulfilled', 'indicator': 'green'};
925 } else {
926 return {'diff_display': 'Failed', 'indicator': 'red'};
927 }
928}
929
Alanc6dc9ea2021-05-07 20:30:04 +0530930function attach_selector_button(inner_text, append_loction, context, grid_row) {
931 let $btn_div = $("<div>").css({"margin-bottom": "10px", "margin-top": "10px"})
932 .appendTo(append_loction);
933 let $btn = $(`<button class="btn btn-sm btn-default">${inner_text}</button>`)
934 .appendTo($btn_div);
935
936 $btn.on("click", function() {
937 context.show_serial_batch_selector(grid_row.frm, grid_row.doc, "", "", true);
938 });
939}