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