blob: 9339c5d998fb10003642c339fc08e844537fcb48 [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 },
90
Frappe PR Bot255b99e2021-08-24 20:19:22 +053091 proceed_save_with_reminders_frequency_change: () => {
92 frappe.ui.hide_open_dialog();
Ankush Menatb147b852021-09-01 16:45:57 +053093
Frappe PR Bot255b99e2021-08-24 20:19:22 +053094 frappe.call({
Ankush Menatb147b852021-09-01 16:45:57 +053095 method: 'erpnext.hr.doctype.hr_settings.hr_settings.set_proceed_with_frequency_change',
Frappe PR Bot255b99e2021-08-24 20:19:22 +053096 callback: () => {
97 cur_frm.save();
98 }
99 });
Nabin Hait1ed1c4e2019-11-25 12:33:40 +0530100 }
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530101});
Rushabh Mehta7d368752014-11-24 14:16:47 +0530102
Nabin Haitb072c742014-12-16 12:49:13 +0530103
104$.extend(erpnext.utils, {
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530105 set_party_dashboard_indicators: function(frm) {
106 if(frm.doc.__onload && frm.doc.__onload.dashboard_info) {
deepeshgarg007920dc142018-11-23 10:17:28 +0530107 var company_wise_info = frm.doc.__onload.dashboard_info;
deepeshgarg007f31caff2018-11-27 15:04:12 +0530108 if(company_wise_info.length > 1) {
deepeshgarg007a1cffc32018-11-23 16:51:35 +0530109 company_wise_info.forEach(function(info) {
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530110 erpnext.utils.add_indicator_for_multicompany(frm, info);
deepeshgarg007a1cffc32018-11-23 16:51:35 +0530111 });
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530112 } else if (company_wise_info.length === 1) {
deepeshgarg00764238ee2018-12-25 16:28:39 +0530113 frm.dashboard.add_indicator(__('Annual Billing: {0}',
114 [format_currency(company_wise_info[0].billing_this_year, company_wise_info[0].currency)]), 'blue');
115 frm.dashboard.add_indicator(__('Total Unpaid: {0}',
116 [format_currency(company_wise_info[0].total_unpaid, company_wise_info[0].currency)]),
deepeshgarg0078300f5e2019-01-01 20:28:49 +0530117 company_wise_info[0].total_unpaid ? 'orange' : 'green');
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530118
deepeshgarg00764238ee2018-12-25 16:28:39 +0530119 if(company_wise_info[0].loyalty_points) {
120 frm.dashboard.add_indicator(__('Loyalty Points: {0}',
121 [company_wise_info[0].loyalty_points]), 'blue');
122 }
deepeshgarg007f31caff2018-11-27 15:04:12 +0530123 }
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530124 }
125 },
126
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530127 add_indicator_for_multicompany: function(frm, info) {
128 frm.dashboard.stats_area.removeClass('hidden');
129 frm.dashboard.stats_area_row.addClass('flex');
130 frm.dashboard.stats_area_row.css('flex-wrap', 'wrap');
131
132 var color = info.total_unpaid ? 'orange' : 'green';
133
134 var indicator = $('<div class="flex-column col-xs-6">'+
135 '<div style="margin-top:10px"><h6>'+info.company+'</h6></div>'+
136
137 '<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
138 'Annual Billing: '+format_currency(info.billing_this_year, info.currency)+'</span></div>'+
139
140 '<div class="badge-link small" style="margin-bottom:10px">'+
141 '<span class="indicator '+color+'">Total Unpaid: '
142 +format_currency(info.total_unpaid, info.currency)+'</span></div>'+
143
144
145 '</div>').appendTo(frm.dashboard.stats_area_row);
146
147 if(info.loyalty_points){
148 $('<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
149 'Loyalty Points: '+info.loyalty_points+'</span></div>').appendTo(indicator);
150 }
151
152 return indicator;
153 },
154
Shreya Shah149f7ee2018-03-27 11:29:25 +0530155 get_party_name: function(party_type) {
156 var dict = {'Customer': 'customer_name', 'Supplier': 'supplier_name', 'Employee': 'employee_name',
157 'Member': 'member_name'};
158 return dict[party_type];
159 },
160
Rushabh Mehta49f97472018-08-30 18:50:48 +0530161 copy_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname) {
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +0530162 var d = locals[dt][dn];
163 if(d[fieldname]){
164 var cl = doc[table_fieldname] || [];
165 for(var i = 0; i < cl.length; i++) {
166 if(!cl[i][fieldname]) cl[i][fieldname] = d[fieldname];
167 }
168 }
169 refresh_field(table_fieldname);
Makarand Bauskar157c3342017-05-26 21:32:33 +0530170 },
171
172 get_terms: function(tc_name, doc, callback) {
173 if(tc_name) {
174 return frappe.call({
175 method: 'erpnext.setup.doctype.terms_and_conditions.terms_and_conditions.get_terms_and_conditions',
176 args: {
177 template_name: tc_name,
178 doc: doc
179 },
180 callback: function(r) {
181 callback(r)
182 }
183 });
184 }
tundebabzy1a947db2017-08-29 13:48:27 +0100185 },
186
rohitwaghchaure713cfc72018-09-11 17:40:37 +0530187 make_bank_account: function(doctype, docname) {
188 frappe.call({
189 method: "erpnext.accounts.doctype.bank_account.bank_account.make_bank_account",
190 args: {
191 doctype: doctype,
192 docname: docname
193 },
194 freeze: true,
195 callback: function(r) {
196 var doclist = frappe.model.sync(r.message);
197 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
198 }
199 })
200 },
201
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530202 add_dimensions: function(report_name, index) {
203 let filters = frappe.query_reports[report_name].filters;
204
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530205 frappe.call({
206 method: "erpnext.accounts.doctype.accounting_dimension.accounting_dimension.get_dimensions",
207 callback: function(r) {
208 let accounting_dimensions = r.message[0];
209 accounting_dimensions.forEach((dimension) => {
210 let found = filters.some(el => el.fieldname === dimension['fieldname']);
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530211
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530212 if (!found) {
Deepesh Garg23ab5c52020-12-31 11:29:06 +0530213 filters.splice(index, 0, {
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530214 "fieldname": dimension["fieldname"],
215 "label": __(dimension["label"]),
216 "fieldtype": "Link",
217 "options": dimension["document_type"]
218 });
219 }
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530220 });
221 }
222 });
223 },
224
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530225 make_subscription: function(doctype, docname) {
226 frappe.call({
Rucha Mahabal65a627c2019-07-17 13:50:32 +0530227 method: "frappe.automation.doctype.auto_repeat.auto_repeat.make_auto_repeat",
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530228 args: {
229 doctype: doctype,
230 docname: docname
231 },
232 callback: function(r) {
233 var doclist = frappe.model.sync(r.message);
234 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
235 }
236 })
237 },
238
Saif90cf2dd2018-09-30 21:46:31 +0500239 make_pricing_rule: function(doctype, docname) {
240 frappe.call({
241 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.make_pricing_rule",
242 args: {
243 doctype: doctype,
244 docname: docname
245 },
246 callback: function(r) {
247 var doclist = frappe.model.sync(r.message);
248 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
249 }
250 })
251 },
252
tundebabzy1a947db2017-08-29 13:48:27 +0100253 /**
254 * Checks if the first row of a given child table is empty
255 * @param child_table - Child table Doctype
256 * @return {Boolean}
257 **/
258 first_row_is_empty: function(child_table){
259 if($.isArray(child_table) && child_table.length > 0) {
260 return !child_table[0].item_code;
261 }
262 return false;
263 },
264
265 /**
266 * Removes the first row of a child table if it is empty
267 * @param {_Frm} frm - The current form
268 * @param {String} child_table_name - The child table field name
269 * @return {Boolean}
270 **/
271 remove_empty_first_row: function(frm, child_table_name){
272 const rows = frm['doc'][child_table_name];
273 if (this.first_row_is_empty(rows)){
274 frm['doc'][child_table_name] = rows.splice(1);
275 }
276 return rows;
277 },
Zarrar6ffdf942018-06-14 12:24:16 +0530278 get_tree_options: function(option) {
279 // get valid options for tree based on user permission & locals dict
280 let unscrub_option = frappe.model.unscrub(option);
281 let user_permission = frappe.defaults.get_user_permissions();
deepeshgarg0070d64d622019-02-20 17:55:14 +0530282 let options;
283
Zarrar6ffdf942018-06-14 12:24:16 +0530284 if(user_permission && user_permission[unscrub_option]) {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530285 options = user_permission[unscrub_option].map(perm => perm.doc);
Zarrar6ffdf942018-06-14 12:24:16 +0530286 } else {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530287 options = $.map(locals[`:${unscrub_option}`], function(c) { return c.name; }).sort();
Zarrar6ffdf942018-06-14 12:24:16 +0530288 }
deepeshgarg0070d64d622019-02-20 17:55:14 +0530289
290 // filter unique values, as there may be multiple user permissions for any value
291 return options.filter((value, index, self) => self.indexOf(value) === index);
Zarrar6ffdf942018-06-14 12:24:16 +0530292 },
293 get_tree_default: function(option) {
294 // set default for a field based on user permission
295 let options = this.get_tree_options(option);
296 if(options.includes(frappe.defaults.get_default(option))) {
297 return frappe.defaults.get_default(option);
298 } else {
299 return options[0];
300 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400301 },
Andy Zhubffe9332021-04-12 22:49:26 +1200302 overrides_parent_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname, parent_fieldname) {
Marica24e45162021-04-14 18:53:15 +0530303 if (doc[parent_fieldname]) {
Andy Zhubffe9332021-04-12 22:49:26 +1200304 let cl = doc[table_fieldname] || [];
Marica24e45162021-04-14 18:53:15 +0530305 for (let i = 0; i < cl.length; i++) {
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400306 cl[i][fieldname] = doc[parent_fieldname];
307 }
Andy Zhubffe9332021-04-12 22:49:26 +1200308 frappe.refresh_field(table_fieldname);
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400309 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400310 },
Nabin Hait34c551d2019-07-03 10:34:31 +0530311 create_new_doc: function (doctype, update_fields) {
312 frappe.model.with_doctype(doctype, function() {
313 var new_doc = frappe.model.get_new_doc(doctype);
314 for (let [key, value] of Object.entries(update_fields)) {
315 new_doc[key] = value;
316 }
317 frappe.ui.form.make_quick_entry(doctype, null, null, new_doc);
318 });
319 }
320
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530321});
322
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530323erpnext.utils.select_alternate_items = function(opts) {
324 const frm = opts.frm;
325 const warehouse_field = opts.warehouse_field || 'warehouse';
326 const item_field = opts.item_field || 'item_code';
327
328 this.data = [];
329 const dialog = new frappe.ui.Dialog({
330 title: __("Select Alternate Item"),
331 fields: [
332 {fieldtype:'Section Break', label: __('Items')},
333 {
334 fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true,
335 in_place_edit: true, data: this.data,
336 get_data: () => {
337 return this.data;
338 },
339 fields: [{
340 fieldtype:'Data',
341 fieldname:"docname",
342 hidden: 1
343 }, {
344 fieldtype:'Link',
345 fieldname:"item_code",
346 options: 'Item',
347 in_list_view: 1,
348 read_only: 1,
349 label: __('Item Code')
350 }, {
351 fieldtype:'Link',
352 fieldname:"alternate_item",
353 options: 'Item',
354 default: "",
355 in_list_view: 1,
356 label: __('Alternate Item'),
357 onchange: function() {
358 const item_code = this.get_value();
359 const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value();
360 if (item_code && warehouse) {
361 frappe.call({
362 method: "erpnext.stock.utils.get_latest_stock_qty",
363 args: {
364 item_code: item_code,
365 warehouse: warehouse
366 },
367 callback: (r) => {
368 this.grid_row.on_grid_fields_dict
369 .actual_qty.set_value(r.message || 0);
370 }
371 })
372 }
373 },
374 get_query: (e) => {
375 return {
376 query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items",
377 filters: {
378 item_code: e.item_code
379 }
380 };
381 }
382 }, {
383 fieldtype:'Link',
384 fieldname:"warehouse",
385 options: 'Warehouse',
386 default: "",
387 in_list_view: 1,
388 label: __('Warehouse'),
389 onchange: function() {
390 const warehouse = this.get_value();
391 const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value();
392 if (item_code && warehouse) {
393 frappe.call({
394 method: "erpnext.stock.utils.get_latest_stock_qty",
395 args: {
396 item_code: item_code,
397 warehouse: warehouse
398 },
399 callback: (r) => {
400 this.grid_row.on_grid_fields_dict
401 .actual_qty.set_value(r.message || 0);
402 }
403 })
404 }
405 },
406 }, {
407 fieldtype:'Float',
408 fieldname:"actual_qty",
409 default: 0,
410 read_only: 1,
411 in_list_view: 1,
412 label: __('Available Qty')
413 }]
414 },
415 ],
416 primary_action: function() {
417 const args = this.get_values()["alternative_items"];
418 const alternative_items = args.filter(d => {
419 if (d.alternate_item && d.item_code != d.alternate_item) {
420 return true;
421 }
422 });
423
424 alternative_items.forEach(d => {
425 let row = frappe.get_doc(opts.child_doctype, d.docname);
Doridel Cahanape42192b2018-05-16 13:28:39 +0800426 let qty = null;
427 if (row.doctype === 'Work Order Item') {
428 qty = row.required_qty;
429 } else {
430 qty = row.qty;
431 }
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530432 row[item_field] = d.alternate_item;
mergify[bot]c1a0f642021-12-17 19:06:19 +0530433 frappe.model.set_value(row.doctype, row.name, 'qty', qty);
434 frappe.model.set_value(row.doctype, row.name, opts.original_item_field, d.item_code);
435 frm.trigger(item_field, row.doctype, row.name);
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530436 });
437
438 refresh_field(opts.child_docname);
439 this.hide();
440 },
441 primary_action_label: __('Update')
442 });
443
444 frm.doc[opts.child_docname].forEach(d => {
445 if (!opts.condition || opts.condition(d)) {
446 dialog.fields_dict.alternative_items.df.data.push({
447 "docname": d.name,
448 "item_code": d[item_field],
449 "warehouse": d[warehouse_field],
450 "actual_qty": d.actual_qty
451 });
452 }
453 })
454
455 this.data = dialog.fields_dict.alternative_items.df.data;
456 dialog.fields_dict.alternative_items.grid.refresh();
457 dialog.show();
458}
459
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800460erpnext.utils.update_child_items = function(opts) {
461 const frm = opts.frm;
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100462 const cannot_add_row = (typeof opts.cannot_add_row === 'undefined') ? true : opts.cannot_add_row;
463 const child_docname = (typeof opts.cannot_add_row === 'undefined') ? "items" : opts.child_docname;
Saqib56fea7d2020-10-09 21:19:25 +0530464 const child_meta = frappe.get_meta(`${frm.doc.doctype} Item`);
465 const get_precision = (fieldname) => child_meta.fields.find(f => f.fieldname == fieldname).precision;
466
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800467 this.data = [];
Saqib Ansarif53299e2020-04-15 22:08:12 +0530468 const fields = [{
469 fieldtype:'Data',
470 fieldname:"docname",
471 read_only: 1,
472 hidden: 1,
473 }, {
474 fieldtype:'Link',
475 fieldname:"item_code",
476 options: 'Item',
477 in_list_view: 1,
478 read_only: 0,
479 disabled: 0,
Afshandc7280e2021-08-18 17:44:40 +0530480 label: __('Item Code'),
481 get_query: function() {
482 let filters;
483 if (frm.doc.doctype == 'Sales Order') {
484 filters = {"is_sales_item": 1};
485 } else if (frm.doc.doctype == 'Purchase Order') {
486 if (frm.doc.is_subcontracted == "Yes") {
487 filters = {"is_sub_contracted_item": 1};
488 } else {
489 filters = {"is_purchase_item": 1};
490 }
491 }
492 return {
493 query: "erpnext.controllers.queries.item_query",
494 filters: filters
495 };
496 }
Saqib Ansarif53299e2020-04-15 22:08:12 +0530497 }, {
Saqib61314242020-09-15 11:14:31 +0530498 fieldtype:'Link',
499 fieldname:'uom',
500 options: 'UOM',
501 read_only: 0,
502 label: __('UOM'),
503 reqd: 1,
504 onchange: function () {
505 frappe.call({
506 method: "erpnext.stock.get_item_details.get_conversion_factor",
507 args: { item_code: this.doc.item_code, uom: this.value },
508 callback: r => {
509 if(!r.exc) {
510 if (this.doc.conversion_factor == r.message.conversion_factor) return;
marination4f395cc2020-09-24 12:43:41 +0530511
Saqib61314242020-09-15 11:14:31 +0530512 const docname = this.doc.docname;
513 dialog.fields_dict.trans_items.df.data.some(doc => {
514 if (doc.docname == docname) {
515 doc.conversion_factor = r.message.conversion_factor;
516 dialog.fields_dict.trans_items.grid.refresh();
517 return true;
518 }
519 })
520 }
521 }
522 });
523 }
524 }, {
Saqib Ansarif53299e2020-04-15 22:08:12 +0530525 fieldtype:'Float',
526 fieldname:"qty",
527 default: 0,
528 read_only: 0,
529 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530530 label: __('Qty'),
531 precision: get_precision("qty")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530532 }, {
533 fieldtype:'Currency',
534 fieldname:"rate",
Afshan84184552021-02-24 16:51:11 +0530535 options: "currency",
Saqib Ansarif53299e2020-04-15 22:08:12 +0530536 default: 0,
537 read_only: 0,
538 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530539 label: __('Rate'),
540 precision: get_precision("rate")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530541 }];
542
543 if (frm.doc.doctype == 'Sales Order' || frm.doc.doctype == 'Purchase Order' ) {
544 fields.splice(2, 0, {
545 fieldtype: 'Date',
546 fieldname: frm.doc.doctype == 'Sales Order' ? "delivery_date" : "schedule_date",
547 in_list_view: 1,
Saqib Ansaric579b082020-05-29 22:21:50 +0530548 label: frm.doc.doctype == 'Sales Order' ? __("Delivery Date") : __("Reqd by date"),
549 reqd: 1
Saqib Ansarif53299e2020-04-15 22:08:12 +0530550 })
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530551 fields.splice(3, 0, {
552 fieldtype: 'Float',
553 fieldname: "conversion_factor",
554 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530555 label: __("Conversion Factor"),
556 precision: get_precision('conversion_factor')
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530557 })
Saqib Ansarif53299e2020-04-15 22:08:12 +0530558 }
559
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800560 const dialog = new frappe.ui.Dialog({
561 title: __("Update Items"),
562 fields: [
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800563 {
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100564 fieldname: "trans_items",
565 fieldtype: "Table",
Maricafc96c1a2020-03-06 10:57:59 +0530566 label: "Items",
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100567 cannot_add_rows: cannot_add_row,
Saqibbc919e22020-11-05 17:38:35 +0530568 in_place_edit: false,
Maricafc96c1a2020-03-06 10:57:59 +0530569 reqd: 1,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100570 data: this.data,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800571 get_data: () => {
572 return this.data;
573 },
Saqib Ansarif53299e2020-04-15 22:08:12 +0530574 fields: fields
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800575 },
576 ],
577 primary_action: function() {
Ankush Menat6de7b8e2021-08-24 12:18:40 +0530578 const trans_items = this.get_values()["trans_items"].filter((item) => !!item.item_code);
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800579 frappe.call({
580 method: 'erpnext.controllers.accounts_controller.update_child_qty_rate',
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100581 freeze: true,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800582 args: {
583 'parent_doctype': frm.doc.doctype,
584 'trans_items': trans_items,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100585 'parent_doctype_name': frm.doc.name,
586 'child_docname': child_docname
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800587 },
588 callback: function() {
589 frm.reload_doc();
590 }
591 });
592 this.hide();
593 refresh_field("items");
594 },
595 primary_action_label: __('Update')
596 });
597
598 frm.doc[opts.child_docname].forEach(d => {
599 dialog.fields_dict.trans_items.df.data.push({
600 "docname": d.name,
Prssanna Desai4f72c0b2019-12-23 18:27:12 +0530601 "name": d.name,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800602 "item_code": d.item_code,
Saqib Ansarif53299e2020-04-15 22:08:12 +0530603 "delivery_date": d.delivery_date,
604 "schedule_date": d.schedule_date,
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530605 "conversion_factor": d.conversion_factor,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800606 "qty": d.qty,
607 "rate": d.rate,
Saqib61314242020-09-15 11:14:31 +0530608 "uom": d.uom
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800609 });
610 this.data = dialog.fields_dict.trans_items.df.data;
611 dialog.fields_dict.trans_items.grid.refresh();
612 })
613 dialog.show();
614}
615
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530616erpnext.utils.map_current_doc = function(opts) {
Sagar Vora78777d62021-03-09 20:35:08 +0530617 function _map() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530618 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530619 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530620 if(!cur_frm.doc.items[0].item_code) {
621 cur_frm.doc.items = cur_frm.doc.items.splice(1);
622 }
bhupen3c7e70f2016-11-30 14:32:11 +0530623
624 // find the doctype of the items table
625 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530626
bhupen3c7e70f2016-11-30 14:32:11 +0530627 // find the link fieldname from items table for the given
628 // source_doctype
629 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530630 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530631 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
632
Nabin Hait802b4352017-01-09 15:32:20 +0530633 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530634 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530635 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530636
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530637 $.each(cur_frm.doc.items, function(i, d) {
638 opts.source_name.forEach(function(src) {
639 if(d[link_fieldname]==src) {
640 already_set = true;
641 if (item_qty_map[d.item_code])
642 item_qty_map[d.item_code] += flt(d.qty);
643 else
644 item_qty_map[d.item_code] = flt(d.qty);
645 }
646 });
647 });
648
649 if(already_set) {
650 opts.source_name.forEach(function(src) {
651 frappe.model.with_doc(opts.source_doctype, src, function(r) {
652 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
653 $.each(source_doc.items || [], function(i, row) {
654 if(row.qty > flt(item_qty_map[row.item_code])) {
655 already_set = false;
656 return false;
657 }
658 })
659 })
660
661 if(already_set) {
662 frappe.msgprint(__("You have already selected items from {0} {1}",
663 [opts.source_doctype, src]));
664 return;
665 }
666
667 })
Nabin Hait802b4352017-01-09 15:32:20 +0530668 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530669 }
670
671 return frappe.call({
672 // Sometimes we hit the limit for URL length of a GET request
673 // as we send the full target_doc. Hence this is a POST request.
674 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530675 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530676 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530677 "method": opts.method,
678 "source_names": opts.source_name,
679 "target_doc": cur_frm.doc,
Maricadb002702020-02-17 15:58:08 +0530680 "args": opts.args
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530681 },
682 callback: function(r) {
683 if(!r.exc) {
684 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530685 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530686 cur_frm.refresh();
687 }
688 }
689 });
690 }
Sagar Vora78777d62021-03-09 20:35:08 +0530691
692 let query_args = {};
693 if (opts.get_query_filters) {
694 query_args.filters = opts.get_query_filters;
695 }
696
697 if (opts.get_query_method) {
698 query_args.query = opts.get_query_method;
699 }
700
701 if (query_args.filters || query_args.query) {
702 opts.get_query = () => query_args;
703 }
704
705 if (opts.source_doctype) {
706 const d = new frappe.ui.form.MultiSelectDialog({
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530707 doctype: opts.source_doctype,
708 target: opts.target,
709 date_field: opts.date_field || undefined,
710 setters: opts.setters,
711 get_query: opts.get_query,
marination4f395cc2020-09-24 12:43:41 +0530712 add_filters_group: 1,
Saqib7292f542021-09-13 12:13:43 +0530713 allow_child_item_selection: opts.allow_child_item_selection,
714 child_fieldname: opts.child_fielname,
715 child_columns: opts.child_columns,
Rohit Waghchaure69ffddf2021-10-07 17:33:58 +0530716 size: opts.size,
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530717 action: function(selections, args) {
718 let values = selections;
Saqibe03d9aa2021-09-17 13:03:27 +0530719 if (values.length === 0) {
Nabin Haita11dcb62017-12-04 13:36:50 +0530720 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530721 return;
722 }
723 opts.source_name = values;
Saqibe03d9aa2021-09-17 13:03:27 +0530724 if (opts.allow_child_item_selection) {
725 // args contains filtered child docnames
726 opts.args = args;
727 }
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530728 d.dialog.hide();
729 _map();
730 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530731 });
Sagar Vora78777d62021-03-09 20:35:08 +0530732
733 return d;
734 }
735
736 if (opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530737 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530738 _map();
739 }
740}
741
Rushabh Mehta180e4352016-07-26 17:57:42 +0530742frappe.form.link_formatters['Item'] = function(value, doc) {
Alan157b3882021-04-21 21:00:22 +0530743 if (doc && value && doc.item_name && doc.item_name !== value && doc.item_code === value) {
marination824f48f2020-10-12 20:08:03 +0530744 return value + ': ' + doc.item_name;
745 } else if (!value && doc.doctype && doc.item_name) {
746 // format blank value in child table
747 return doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530748 } else {
marination824f48f2020-10-12 20:08:03 +0530749 // 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 +0530750 return value;
751 }
752}
753
754frappe.form.link_formatters['Employee'] = function(value, doc) {
Rucha Mahabal5ba1bc12021-11-23 09:30:30 +0530755 if (doc && value && doc.employee_name && doc.employee_name !== value && doc.employee === value) {
756 return value + ': ' + doc.employee_name;
757 } else if (!value && doc.doctype && doc.employee_name) {
758 // format blank value in child table
759 return doc.employee;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530760 } else {
Rucha Mahabal5ba1bc12021-11-23 09:30:30 +0530761 // 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 +0530762 return value;
763 }
764}
765
Rucha Mahabal59961f72021-05-20 23:43:19 +0530766frappe.form.link_formatters['Project'] = function(value, doc) {
767 if (doc && value && doc.project_name && doc.project_name !== value && doc.project === value) {
768 return value + ': ' + doc.project_name;
769 } else if (!value && doc.doctype && doc.project_name) {
770 // format blank value in child table
771 return doc.project;
772 } else {
773 // if value is blank in report view or project name and name are the same, return as is
774 return value;
775 }
776};
777
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530778// add description on posting time
779$(document).on('app_ready', function() {
780 if(!frappe.datetime.is_timezone_same()) {
781 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
782 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
783 frappe.ui.form.on(d, "onload", function(frm) {
784 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530785 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530786 });
787 });
788 }
akshay14384c22016-07-28 13:53:17 +0530789});
Alanc6dc9ea2021-05-07 20:30:04 +0530790
Himanshuec25d592021-06-14 19:05:52 +0530791// Show SLA dashboard
792$(document).on('app_ready', function() {
793 frappe.call({
794 method: 'erpnext.support.doctype.service_level_agreement.service_level_agreement.get_sla_doctypes',
795 callback: function(r) {
796 if (!r.message)
797 return;
798
799 $.each(r.message, function(_i, d) {
800 frappe.ui.form.on(d, {
801 onload: function(frm) {
802 if (!frm.doc.service_level_agreement)
803 return;
804
805 frappe.call({
806 method: 'erpnext.support.doctype.service_level_agreement.service_level_agreement.get_service_level_agreement_filters',
807 args: {
808 doctype: frm.doc.doctype,
809 name: frm.doc.service_level_agreement,
810 customer: frm.doc.customer
811 },
812 callback: function (r) {
813 if (r && r.message) {
814 frm.set_query('priority', function() {
815 return {
816 filters: {
817 'name': ['in', r.message.priority],
818 }
819 };
820 });
821 frm.set_query('service_level_agreement', function() {
822 return {
823 filters: {
824 'name': ['in', r.message.service_level_agreements],
825 }
826 };
827 });
828 }
829 }
830 });
831 },
832
833 refresh: function(frm) {
834 if (frm.doc.status !== 'Closed' && frm.doc.service_level_agreement
Saqib Ansari476e81a2021-12-06 18:55:30 +0530835 && ['First Response Due', 'Resolution Due'].includes(frm.doc.agreement_status)) {
Himanshuec25d592021-06-14 19:05:52 +0530836 frappe.call({
837 'method': 'frappe.client.get',
838 args: {
839 doctype: 'Service Level Agreement',
840 name: frm.doc.service_level_agreement
841 },
842 callback: function(data) {
843 let statuses = data.message.pause_sla_on;
844 const hold_statuses = [];
845 $.each(statuses, (_i, entry) => {
846 hold_statuses.push(entry.status);
847 });
848 if (hold_statuses.includes(frm.doc.status)) {
849 frm.dashboard.clear_headline();
850 let message = {'indicator': 'orange', 'msg': __('SLA is on hold since {0}', [moment(frm.doc.on_hold_since).fromNow(true)])};
851 frm.dashboard.set_headline_alert(
852 '<div class="row">' +
853 '<div class="col-xs-12">' +
854 '<span class="indicator whitespace-nowrap '+ message.indicator +'"><span>'+ message.msg +'</span></span> ' +
855 '</div>' +
856 '</div>'
857 );
858 } else {
859 set_time_to_resolve_and_response(frm, data.message.apply_sla_for_resolution);
860 }
861 }
862 });
863 } else if (frm.doc.service_level_agreement) {
864 frm.dashboard.clear_headline();
865
866 let agreement_status = (frm.doc.agreement_status == 'Fulfilled') ?
867 {'indicator': 'green', 'msg': 'Service Level Agreement has been fulfilled'} :
868 {'indicator': 'red', 'msg': 'Service Level Agreement Failed'};
869
870 frm.dashboard.set_headline_alert(
871 '<div class="row">' +
872 '<div class="col-xs-12">' +
873 '<span class="indicator whitespace-nowrap '+ agreement_status.indicator +'"><span class="hidden-xs">'+ agreement_status.msg +'</span></span> ' +
874 '</div>' +
875 '</div>'
876 );
877 }
878 },
879 });
880 });
881 }
882 });
883});
884
885function set_time_to_resolve_and_response(frm, apply_sla_for_resolution) {
886 frm.dashboard.clear_headline();
887
Saqib24114772021-12-15 11:45:34 +0530888 let time_to_respond;
Saqib Ansari476e81a2021-12-06 18:55:30 +0530889 if (!frm.doc.first_responded_on) {
Himanshuec25d592021-06-14 19:05:52 +0530890 time_to_respond = get_time_left(frm.doc.response_by, frm.doc.agreement_status);
Saqib24114772021-12-15 11:45:34 +0530891 } else {
892 time_to_respond = get_status(frm.doc.response_by, frm.doc.first_responded_on);
Himanshuec25d592021-06-14 19:05:52 +0530893 }
894
895 let alert = `
896 <div class="row">
897 <div class="col-xs-12 col-sm-6">
898 <span class="indicator whitespace-nowrap ${time_to_respond.indicator}">
899 <span>Time to Respond: ${time_to_respond.diff_display}</span>
900 </span>
901 </div>`;
902
903
904 if (apply_sla_for_resolution) {
Saqib24114772021-12-15 11:45:34 +0530905 let time_to_resolve;
Saqib Ansari476e81a2021-12-06 18:55:30 +0530906 if (!frm.doc.resolution_date) {
Himanshuec25d592021-06-14 19:05:52 +0530907 time_to_resolve = get_time_left(frm.doc.resolution_by, frm.doc.agreement_status);
Saqib24114772021-12-15 11:45:34 +0530908 } else {
909 time_to_resolve = get_status(frm.doc.resolution_by, frm.doc.resolution_date);
Himanshuec25d592021-06-14 19:05:52 +0530910 }
911
912 alert += `
913 <div class="col-xs-12 col-sm-6">
914 <span class="indicator whitespace-nowrap ${time_to_resolve.indicator}">
915 <span>Time to Resolve: ${time_to_resolve.diff_display}</span>
916 </span>
917 </div>`;
918 }
919
920 alert += '</div>';
921
922 frm.dashboard.set_headline_alert(alert);
923}
924
925function get_time_left(timestamp, agreement_status) {
926 const diff = moment(timestamp).diff(moment());
927 const diff_display = diff >= 44500 ? moment.duration(diff).humanize() : 'Failed';
928 let indicator = (diff_display == 'Failed' && agreement_status != 'Fulfilled') ? 'red' : 'green';
929 return {'diff_display': diff_display, 'indicator': indicator};
930}
931
Saqib24114772021-12-15 11:45:34 +0530932function get_status(expected, actual) {
933 const time_left = moment(expected).diff(moment(actual));
Saqib Ansari476e81a2021-12-06 18:55:30 +0530934 if (time_left >= 0) {
Himanshuec25d592021-06-14 19:05:52 +0530935 return {'diff_display': 'Fulfilled', 'indicator': 'green'};
936 } else {
937 return {'diff_display': 'Failed', 'indicator': 'red'};
938 }
939}
940
Alanc6dc9ea2021-05-07 20:30:04 +0530941function attach_selector_button(inner_text, append_loction, context, grid_row) {
942 let $btn_div = $("<div>").css({"margin-bottom": "10px", "margin-top": "10px"})
943 .appendTo(append_loction);
944 let $btn = $(`<button class="btn btn-sm btn-default">${inner_text}</button>`)
945 .appendTo($btn_div);
946
947 $btn.on("click", function() {
948 context.show_serial_batch_selector(grid_row.frm, grid_row.doc, "", "", true);
949 });
950}