blob: 93a273187a80544a4c498d9bbb0fe105a66e4fe3 [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;
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530433 frm.script_manager.trigger(item_field, row.doctype, row.name)
434 .then(() => {
435 frappe.model.set_value(row.doctype, row.name, 'qty', qty);
436 frappe.model.set_value(row.doctype, row.name,
437 opts.original_item_field, d.item_code);
438 });
439 });
440
441 refresh_field(opts.child_docname);
442 this.hide();
443 },
444 primary_action_label: __('Update')
445 });
446
447 frm.doc[opts.child_docname].forEach(d => {
448 if (!opts.condition || opts.condition(d)) {
449 dialog.fields_dict.alternative_items.df.data.push({
450 "docname": d.name,
451 "item_code": d[item_field],
452 "warehouse": d[warehouse_field],
453 "actual_qty": d.actual_qty
454 });
455 }
456 })
457
458 this.data = dialog.fields_dict.alternative_items.df.data;
459 dialog.fields_dict.alternative_items.grid.refresh();
460 dialog.show();
461}
462
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800463erpnext.utils.update_child_items = function(opts) {
464 const frm = opts.frm;
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100465 const cannot_add_row = (typeof opts.cannot_add_row === 'undefined') ? true : opts.cannot_add_row;
466 const child_docname = (typeof opts.cannot_add_row === 'undefined') ? "items" : opts.child_docname;
Saqib56fea7d2020-10-09 21:19:25 +0530467 const child_meta = frappe.get_meta(`${frm.doc.doctype} Item`);
468 const get_precision = (fieldname) => child_meta.fields.find(f => f.fieldname == fieldname).precision;
469
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800470 this.data = [];
Saqib Ansarif53299e2020-04-15 22:08:12 +0530471 const fields = [{
472 fieldtype:'Data',
473 fieldname:"docname",
474 read_only: 1,
475 hidden: 1,
476 }, {
477 fieldtype:'Link',
478 fieldname:"item_code",
479 options: 'Item',
480 in_list_view: 1,
481 read_only: 0,
482 disabled: 0,
Afshandc7280e2021-08-18 17:44:40 +0530483 label: __('Item Code'),
484 get_query: function() {
485 let filters;
486 if (frm.doc.doctype == 'Sales Order') {
487 filters = {"is_sales_item": 1};
488 } else if (frm.doc.doctype == 'Purchase Order') {
489 if (frm.doc.is_subcontracted == "Yes") {
490 filters = {"is_sub_contracted_item": 1};
491 } else {
492 filters = {"is_purchase_item": 1};
493 }
494 }
495 return {
496 query: "erpnext.controllers.queries.item_query",
497 filters: filters
498 };
499 }
Saqib Ansarif53299e2020-04-15 22:08:12 +0530500 }, {
Saqib61314242020-09-15 11:14:31 +0530501 fieldtype:'Link',
502 fieldname:'uom',
503 options: 'UOM',
504 read_only: 0,
505 label: __('UOM'),
506 reqd: 1,
507 onchange: function () {
508 frappe.call({
509 method: "erpnext.stock.get_item_details.get_conversion_factor",
510 args: { item_code: this.doc.item_code, uom: this.value },
511 callback: r => {
512 if(!r.exc) {
513 if (this.doc.conversion_factor == r.message.conversion_factor) return;
marination4f395cc2020-09-24 12:43:41 +0530514
Saqib61314242020-09-15 11:14:31 +0530515 const docname = this.doc.docname;
516 dialog.fields_dict.trans_items.df.data.some(doc => {
517 if (doc.docname == docname) {
518 doc.conversion_factor = r.message.conversion_factor;
519 dialog.fields_dict.trans_items.grid.refresh();
520 return true;
521 }
522 })
523 }
524 }
525 });
526 }
527 }, {
Saqib Ansarif53299e2020-04-15 22:08:12 +0530528 fieldtype:'Float',
529 fieldname:"qty",
530 default: 0,
531 read_only: 0,
532 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530533 label: __('Qty'),
534 precision: get_precision("qty")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530535 }, {
536 fieldtype:'Currency',
537 fieldname:"rate",
Afshan84184552021-02-24 16:51:11 +0530538 options: "currency",
Saqib Ansarif53299e2020-04-15 22:08:12 +0530539 default: 0,
540 read_only: 0,
541 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530542 label: __('Rate'),
543 precision: get_precision("rate")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530544 }];
545
546 if (frm.doc.doctype == 'Sales Order' || frm.doc.doctype == 'Purchase Order' ) {
547 fields.splice(2, 0, {
548 fieldtype: 'Date',
549 fieldname: frm.doc.doctype == 'Sales Order' ? "delivery_date" : "schedule_date",
550 in_list_view: 1,
Saqib Ansaric579b082020-05-29 22:21:50 +0530551 label: frm.doc.doctype == 'Sales Order' ? __("Delivery Date") : __("Reqd by date"),
552 reqd: 1
Saqib Ansarif53299e2020-04-15 22:08:12 +0530553 })
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530554 fields.splice(3, 0, {
555 fieldtype: 'Float',
556 fieldname: "conversion_factor",
557 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530558 label: __("Conversion Factor"),
559 precision: get_precision('conversion_factor')
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530560 })
Saqib Ansarif53299e2020-04-15 22:08:12 +0530561 }
562
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800563 const dialog = new frappe.ui.Dialog({
564 title: __("Update Items"),
565 fields: [
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800566 {
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100567 fieldname: "trans_items",
568 fieldtype: "Table",
Maricafc96c1a2020-03-06 10:57:59 +0530569 label: "Items",
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100570 cannot_add_rows: cannot_add_row,
Saqibbc919e22020-11-05 17:38:35 +0530571 in_place_edit: false,
Maricafc96c1a2020-03-06 10:57:59 +0530572 reqd: 1,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100573 data: this.data,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800574 get_data: () => {
575 return this.data;
576 },
Saqib Ansarif53299e2020-04-15 22:08:12 +0530577 fields: fields
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800578 },
579 ],
580 primary_action: function() {
Ankush Menat6de7b8e2021-08-24 12:18:40 +0530581 const trans_items = this.get_values()["trans_items"].filter((item) => !!item.item_code);
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800582 frappe.call({
583 method: 'erpnext.controllers.accounts_controller.update_child_qty_rate',
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100584 freeze: true,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800585 args: {
586 'parent_doctype': frm.doc.doctype,
587 'trans_items': trans_items,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100588 'parent_doctype_name': frm.doc.name,
589 'child_docname': child_docname
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800590 },
591 callback: function() {
592 frm.reload_doc();
593 }
594 });
595 this.hide();
596 refresh_field("items");
597 },
598 primary_action_label: __('Update')
599 });
600
601 frm.doc[opts.child_docname].forEach(d => {
602 dialog.fields_dict.trans_items.df.data.push({
603 "docname": d.name,
Prssanna Desai4f72c0b2019-12-23 18:27:12 +0530604 "name": d.name,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800605 "item_code": d.item_code,
Saqib Ansarif53299e2020-04-15 22:08:12 +0530606 "delivery_date": d.delivery_date,
607 "schedule_date": d.schedule_date,
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530608 "conversion_factor": d.conversion_factor,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800609 "qty": d.qty,
610 "rate": d.rate,
Saqib61314242020-09-15 11:14:31 +0530611 "uom": d.uom
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800612 });
613 this.data = dialog.fields_dict.trans_items.df.data;
614 dialog.fields_dict.trans_items.grid.refresh();
615 })
616 dialog.show();
617}
618
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530619erpnext.utils.map_current_doc = function(opts) {
Sagar Vora78777d62021-03-09 20:35:08 +0530620 function _map() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530621 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530622 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530623 if(!cur_frm.doc.items[0].item_code) {
624 cur_frm.doc.items = cur_frm.doc.items.splice(1);
625 }
bhupen3c7e70f2016-11-30 14:32:11 +0530626
627 // find the doctype of the items table
628 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530629
bhupen3c7e70f2016-11-30 14:32:11 +0530630 // find the link fieldname from items table for the given
631 // source_doctype
632 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530633 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530634 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
635
Nabin Hait802b4352017-01-09 15:32:20 +0530636 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530637 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530638 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530639
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530640 $.each(cur_frm.doc.items, function(i, d) {
641 opts.source_name.forEach(function(src) {
642 if(d[link_fieldname]==src) {
643 already_set = true;
644 if (item_qty_map[d.item_code])
645 item_qty_map[d.item_code] += flt(d.qty);
646 else
647 item_qty_map[d.item_code] = flt(d.qty);
648 }
649 });
650 });
651
652 if(already_set) {
653 opts.source_name.forEach(function(src) {
654 frappe.model.with_doc(opts.source_doctype, src, function(r) {
655 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
656 $.each(source_doc.items || [], function(i, row) {
657 if(row.qty > flt(item_qty_map[row.item_code])) {
658 already_set = false;
659 return false;
660 }
661 })
662 })
663
664 if(already_set) {
665 frappe.msgprint(__("You have already selected items from {0} {1}",
666 [opts.source_doctype, src]));
667 return;
668 }
669
670 })
Nabin Hait802b4352017-01-09 15:32:20 +0530671 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530672 }
673
674 return frappe.call({
675 // Sometimes we hit the limit for URL length of a GET request
676 // as we send the full target_doc. Hence this is a POST request.
677 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530678 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530679 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530680 "method": opts.method,
681 "source_names": opts.source_name,
682 "target_doc": cur_frm.doc,
Maricadb002702020-02-17 15:58:08 +0530683 "args": opts.args
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530684 },
685 callback: function(r) {
686 if(!r.exc) {
687 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530688 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530689 cur_frm.refresh();
690 }
691 }
692 });
693 }
Sagar Vora78777d62021-03-09 20:35:08 +0530694
695 let query_args = {};
696 if (opts.get_query_filters) {
697 query_args.filters = opts.get_query_filters;
698 }
699
700 if (opts.get_query_method) {
701 query_args.query = opts.get_query_method;
702 }
703
704 if (query_args.filters || query_args.query) {
705 opts.get_query = () => query_args;
706 }
707
708 if (opts.source_doctype) {
709 const d = new frappe.ui.form.MultiSelectDialog({
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530710 doctype: opts.source_doctype,
711 target: opts.target,
712 date_field: opts.date_field || undefined,
713 setters: opts.setters,
714 get_query: opts.get_query,
marination4f395cc2020-09-24 12:43:41 +0530715 add_filters_group: 1,
Saqib7292f542021-09-13 12:13:43 +0530716 allow_child_item_selection: opts.allow_child_item_selection,
717 child_fieldname: opts.child_fielname,
718 child_columns: opts.child_columns,
Rohit Waghchaure69ffddf2021-10-07 17:33:58 +0530719 size: opts.size,
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530720 action: function(selections, args) {
721 let values = selections;
Saqibe03d9aa2021-09-17 13:03:27 +0530722 if (values.length === 0) {
Nabin Haita11dcb62017-12-04 13:36:50 +0530723 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530724 return;
725 }
726 opts.source_name = values;
Saqibe03d9aa2021-09-17 13:03:27 +0530727 if (opts.allow_child_item_selection) {
728 // args contains filtered child docnames
729 opts.args = args;
730 }
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530731 d.dialog.hide();
732 _map();
733 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530734 });
Sagar Vora78777d62021-03-09 20:35:08 +0530735
736 return d;
737 }
738
739 if (opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530740 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530741 _map();
742 }
743}
744
Rushabh Mehta180e4352016-07-26 17:57:42 +0530745frappe.form.link_formatters['Item'] = function(value, doc) {
Alan157b3882021-04-21 21:00:22 +0530746 if (doc && value && doc.item_name && doc.item_name !== value && doc.item_code === value) {
marination824f48f2020-10-12 20:08:03 +0530747 return value + ': ' + doc.item_name;
748 } else if (!value && doc.doctype && doc.item_name) {
749 // format blank value in child table
750 return doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530751 } else {
marination824f48f2020-10-12 20:08:03 +0530752 // 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 +0530753 return value;
754 }
755}
756
757frappe.form.link_formatters['Employee'] = function(value, doc) {
Rucha Mahabal5ba1bc12021-11-23 09:30:30 +0530758 if (doc && value && doc.employee_name && doc.employee_name !== value && doc.employee === value) {
759 return value + ': ' + doc.employee_name;
760 } else if (!value && doc.doctype && doc.employee_name) {
761 // format blank value in child table
762 return doc.employee;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530763 } else {
Rucha Mahabal5ba1bc12021-11-23 09:30:30 +0530764 // 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 +0530765 return value;
766 }
767}
768
Rucha Mahabal59961f72021-05-20 23:43:19 +0530769frappe.form.link_formatters['Project'] = function(value, doc) {
770 if (doc && value && doc.project_name && doc.project_name !== value && doc.project === value) {
771 return value + ': ' + doc.project_name;
772 } else if (!value && doc.doctype && doc.project_name) {
773 // format blank value in child table
774 return doc.project;
775 } else {
776 // if value is blank in report view or project name and name are the same, return as is
777 return value;
778 }
779};
780
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530781// add description on posting time
782$(document).on('app_ready', function() {
783 if(!frappe.datetime.is_timezone_same()) {
784 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
785 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
786 frappe.ui.form.on(d, "onload", function(frm) {
787 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530788 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530789 });
790 });
791 }
akshay14384c22016-07-28 13:53:17 +0530792});
Alanc6dc9ea2021-05-07 20:30:04 +0530793
Himanshuec25d592021-06-14 19:05:52 +0530794// Show SLA dashboard
795$(document).on('app_ready', function() {
796 frappe.call({
797 method: 'erpnext.support.doctype.service_level_agreement.service_level_agreement.get_sla_doctypes',
798 callback: function(r) {
799 if (!r.message)
800 return;
801
802 $.each(r.message, function(_i, d) {
803 frappe.ui.form.on(d, {
804 onload: function(frm) {
805 if (!frm.doc.service_level_agreement)
806 return;
807
808 frappe.call({
809 method: 'erpnext.support.doctype.service_level_agreement.service_level_agreement.get_service_level_agreement_filters',
810 args: {
811 doctype: frm.doc.doctype,
812 name: frm.doc.service_level_agreement,
813 customer: frm.doc.customer
814 },
815 callback: function (r) {
816 if (r && r.message) {
817 frm.set_query('priority', function() {
818 return {
819 filters: {
820 'name': ['in', r.message.priority],
821 }
822 };
823 });
824 frm.set_query('service_level_agreement', function() {
825 return {
826 filters: {
827 'name': ['in', r.message.service_level_agreements],
828 }
829 };
830 });
831 }
832 }
833 });
834 },
835
836 refresh: function(frm) {
837 if (frm.doc.status !== 'Closed' && frm.doc.service_level_agreement
Saqib Ansari476e81a2021-12-06 18:55:30 +0530838 && ['First Response Due', 'Resolution Due'].includes(frm.doc.agreement_status)) {
Himanshuec25d592021-06-14 19:05:52 +0530839 frappe.call({
840 'method': 'frappe.client.get',
841 args: {
842 doctype: 'Service Level Agreement',
843 name: frm.doc.service_level_agreement
844 },
845 callback: function(data) {
846 let statuses = data.message.pause_sla_on;
847 const hold_statuses = [];
848 $.each(statuses, (_i, entry) => {
849 hold_statuses.push(entry.status);
850 });
851 if (hold_statuses.includes(frm.doc.status)) {
852 frm.dashboard.clear_headline();
853 let message = {'indicator': 'orange', 'msg': __('SLA is on hold since {0}', [moment(frm.doc.on_hold_since).fromNow(true)])};
854 frm.dashboard.set_headline_alert(
855 '<div class="row">' +
856 '<div class="col-xs-12">' +
857 '<span class="indicator whitespace-nowrap '+ message.indicator +'"><span>'+ message.msg +'</span></span> ' +
858 '</div>' +
859 '</div>'
860 );
861 } else {
862 set_time_to_resolve_and_response(frm, data.message.apply_sla_for_resolution);
863 }
864 }
865 });
866 } else if (frm.doc.service_level_agreement) {
867 frm.dashboard.clear_headline();
868
869 let agreement_status = (frm.doc.agreement_status == 'Fulfilled') ?
870 {'indicator': 'green', 'msg': 'Service Level Agreement has been fulfilled'} :
871 {'indicator': 'red', 'msg': 'Service Level Agreement Failed'};
872
873 frm.dashboard.set_headline_alert(
874 '<div class="row">' +
875 '<div class="col-xs-12">' +
876 '<span class="indicator whitespace-nowrap '+ agreement_status.indicator +'"><span class="hidden-xs">'+ agreement_status.msg +'</span></span> ' +
877 '</div>' +
878 '</div>'
879 );
880 }
881 },
882 });
883 });
884 }
885 });
886});
887
888function set_time_to_resolve_and_response(frm, apply_sla_for_resolution) {
889 frm.dashboard.clear_headline();
890
Saqib24114772021-12-15 11:45:34 +0530891 let time_to_respond;
Saqib Ansari476e81a2021-12-06 18:55:30 +0530892 if (!frm.doc.first_responded_on) {
Himanshuec25d592021-06-14 19:05:52 +0530893 time_to_respond = get_time_left(frm.doc.response_by, frm.doc.agreement_status);
Saqib24114772021-12-15 11:45:34 +0530894 } else {
895 time_to_respond = get_status(frm.doc.response_by, frm.doc.first_responded_on);
Himanshuec25d592021-06-14 19:05:52 +0530896 }
897
898 let alert = `
899 <div class="row">
900 <div class="col-xs-12 col-sm-6">
901 <span class="indicator whitespace-nowrap ${time_to_respond.indicator}">
902 <span>Time to Respond: ${time_to_respond.diff_display}</span>
903 </span>
904 </div>`;
905
906
907 if (apply_sla_for_resolution) {
Saqib24114772021-12-15 11:45:34 +0530908 let time_to_resolve;
Saqib Ansari476e81a2021-12-06 18:55:30 +0530909 if (!frm.doc.resolution_date) {
Himanshuec25d592021-06-14 19:05:52 +0530910 time_to_resolve = get_time_left(frm.doc.resolution_by, frm.doc.agreement_status);
Saqib24114772021-12-15 11:45:34 +0530911 } else {
912 time_to_resolve = get_status(frm.doc.resolution_by, frm.doc.resolution_date);
Himanshuec25d592021-06-14 19:05:52 +0530913 }
914
915 alert += `
916 <div class="col-xs-12 col-sm-6">
917 <span class="indicator whitespace-nowrap ${time_to_resolve.indicator}">
918 <span>Time to Resolve: ${time_to_resolve.diff_display}</span>
919 </span>
920 </div>`;
921 }
922
923 alert += '</div>';
924
925 frm.dashboard.set_headline_alert(alert);
926}
927
928function get_time_left(timestamp, agreement_status) {
929 const diff = moment(timestamp).diff(moment());
930 const diff_display = diff >= 44500 ? moment.duration(diff).humanize() : 'Failed';
931 let indicator = (diff_display == 'Failed' && agreement_status != 'Fulfilled') ? 'red' : 'green';
932 return {'diff_display': diff_display, 'indicator': indicator};
933}
934
Saqib24114772021-12-15 11:45:34 +0530935function get_status(expected, actual) {
936 const time_left = moment(expected).diff(moment(actual));
Saqib Ansari476e81a2021-12-06 18:55:30 +0530937 if (time_left >= 0) {
Himanshuec25d592021-06-14 19:05:52 +0530938 return {'diff_display': 'Fulfilled', 'indicator': 'green'};
939 } else {
940 return {'diff_display': 'Failed', 'indicator': 'red'};
941 }
942}
943
Alanc6dc9ea2021-05-07 20:30:04 +0530944function attach_selector_button(inner_text, append_loction, context, grid_row) {
945 let $btn_div = $("<div>").css({"margin-bottom": "10px", "margin-top": "10px"})
946 .appendTo(append_loction);
947 let $btn = $(`<button class="btn btn-sm btn-default">${inner_text}</button>`)
948 .appendTo($btn_div);
949
950 $btn.on("click", function() {
951 context.show_serial_batch_selector(grid_row.frm, grid_row.doc, "", "", true);
952 });
953}