blob: f1b9235fe3a1ecccae81c8782d64182716410f45 [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 });
85 }
Rushabh Mehtac38fc712014-04-16 17:21:25 +053086});
Rushabh Mehta7d368752014-11-24 14:16:47 +053087
Nabin Haitb072c742014-12-16 12:49:13 +053088
89$.extend(erpnext.utils, {
Rushabh Mehta559aa3a2016-09-19 16:04:58 +053090 set_party_dashboard_indicators: function(frm) {
91 if(frm.doc.__onload && frm.doc.__onload.dashboard_info) {
deepeshgarg007920dc142018-11-23 10:17:28 +053092 var company_wise_info = frm.doc.__onload.dashboard_info;
deepeshgarg007f31caff2018-11-27 15:04:12 +053093 if(company_wise_info.length > 1) {
deepeshgarg007a1cffc32018-11-23 16:51:35 +053094 company_wise_info.forEach(function(info) {
deepeshgarg00712f5cef2018-12-25 16:06:19 +053095 erpnext.utils.add_indicator_for_multicompany(frm, info);
deepeshgarg007a1cffc32018-11-23 16:51:35 +053096 });
deepeshgarg00712f5cef2018-12-25 16:06:19 +053097 } else if (company_wise_info.length === 1) {
deepeshgarg00764238ee2018-12-25 16:28:39 +053098 frm.dashboard.add_indicator(__('Annual Billing: {0}',
99 [format_currency(company_wise_info[0].billing_this_year, company_wise_info[0].currency)]), 'blue');
100 frm.dashboard.add_indicator(__('Total Unpaid: {0}',
101 [format_currency(company_wise_info[0].total_unpaid, company_wise_info[0].currency)]),
deepeshgarg0078300f5e2019-01-01 20:28:49 +0530102 company_wise_info[0].total_unpaid ? 'orange' : 'green');
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530103
deepeshgarg00764238ee2018-12-25 16:28:39 +0530104 if(company_wise_info[0].loyalty_points) {
105 frm.dashboard.add_indicator(__('Loyalty Points: {0}',
106 [company_wise_info[0].loyalty_points]), 'blue');
107 }
deepeshgarg007f31caff2018-11-27 15:04:12 +0530108 }
Rushabh Mehta559aa3a2016-09-19 16:04:58 +0530109 }
110 },
111
deepeshgarg00712f5cef2018-12-25 16:06:19 +0530112 add_indicator_for_multicompany: function(frm, info) {
113 frm.dashboard.stats_area.removeClass('hidden');
114 frm.dashboard.stats_area_row.addClass('flex');
115 frm.dashboard.stats_area_row.css('flex-wrap', 'wrap');
116
117 var color = info.total_unpaid ? 'orange' : 'green';
118
119 var indicator = $('<div class="flex-column col-xs-6">'+
120 '<div style="margin-top:10px"><h6>'+info.company+'</h6></div>'+
121
122 '<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
123 'Annual Billing: '+format_currency(info.billing_this_year, info.currency)+'</span></div>'+
124
125 '<div class="badge-link small" style="margin-bottom:10px">'+
126 '<span class="indicator '+color+'">Total Unpaid: '
127 +format_currency(info.total_unpaid, info.currency)+'</span></div>'+
128
129
130 '</div>').appendTo(frm.dashboard.stats_area_row);
131
132 if(info.loyalty_points){
133 $('<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
134 'Loyalty Points: '+info.loyalty_points+'</span></div>').appendTo(indicator);
135 }
136
137 return indicator;
138 },
139
Shreya Shah149f7ee2018-03-27 11:29:25 +0530140 get_party_name: function(party_type) {
141 var dict = {'Customer': 'customer_name', 'Supplier': 'supplier_name', 'Employee': 'employee_name',
142 'Member': 'member_name'};
143 return dict[party_type];
144 },
145
Rushabh Mehta49f97472018-08-30 18:50:48 +0530146 copy_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname) {
Neil Trini Lasradofe2ffae2015-07-08 18:16:51 +0530147 var d = locals[dt][dn];
148 if(d[fieldname]){
149 var cl = doc[table_fieldname] || [];
150 for(var i = 0; i < cl.length; i++) {
151 if(!cl[i][fieldname]) cl[i][fieldname] = d[fieldname];
152 }
153 }
154 refresh_field(table_fieldname);
Makarand Bauskar157c3342017-05-26 21:32:33 +0530155 },
156
157 get_terms: function(tc_name, doc, callback) {
158 if(tc_name) {
159 return frappe.call({
160 method: 'erpnext.setup.doctype.terms_and_conditions.terms_and_conditions.get_terms_and_conditions',
161 args: {
162 template_name: tc_name,
163 doc: doc
164 },
165 callback: function(r) {
166 callback(r)
167 }
168 });
169 }
tundebabzy1a947db2017-08-29 13:48:27 +0100170 },
171
rohitwaghchaure713cfc72018-09-11 17:40:37 +0530172 make_bank_account: function(doctype, docname) {
173 frappe.call({
174 method: "erpnext.accounts.doctype.bank_account.bank_account.make_bank_account",
175 args: {
176 doctype: doctype,
177 docname: docname
178 },
179 freeze: true,
180 callback: function(r) {
181 var doclist = frappe.model.sync(r.message);
182 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
183 }
184 })
185 },
186
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530187 add_dimensions: function(report_name, index) {
188 let filters = frappe.query_reports[report_name].filters;
189
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530190 frappe.call({
191 method: "erpnext.accounts.doctype.accounting_dimension.accounting_dimension.get_dimensions",
192 callback: function(r) {
193 let accounting_dimensions = r.message[0];
194 accounting_dimensions.forEach((dimension) => {
195 let found = filters.some(el => el.fieldname === dimension['fieldname']);
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530196
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530197 if (!found) {
Deepesh Garg23ab5c52020-12-31 11:29:06 +0530198 filters.splice(index, 0, {
Deepesh Garg7b2d5182020-12-04 11:28:26 +0530199 "fieldname": dimension["fieldname"],
200 "label": __(dimension["label"]),
201 "fieldtype": "Link",
202 "options": dimension["document_type"]
203 });
204 }
Deepesh Garg11ea0b12020-05-26 19:23:45 +0530205 });
206 }
207 });
208 },
209
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530210 make_subscription: function(doctype, docname) {
211 frappe.call({
Rucha Mahabal65a627c2019-07-17 13:50:32 +0530212 method: "frappe.automation.doctype.auto_repeat.auto_repeat.make_auto_repeat",
rohitwaghchaure166b78f2017-09-07 16:14:22 +0530213 args: {
214 doctype: doctype,
215 docname: docname
216 },
217 callback: function(r) {
218 var doclist = frappe.model.sync(r.message);
219 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
220 }
221 })
222 },
223
Saif90cf2dd2018-09-30 21:46:31 +0500224 make_pricing_rule: function(doctype, docname) {
225 frappe.call({
226 method: "erpnext.accounts.doctype.pricing_rule.pricing_rule.make_pricing_rule",
227 args: {
228 doctype: doctype,
229 docname: docname
230 },
231 callback: function(r) {
232 var doclist = frappe.model.sync(r.message);
233 frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
234 }
235 })
236 },
237
tundebabzy1a947db2017-08-29 13:48:27 +0100238 /**
239 * Checks if the first row of a given child table is empty
240 * @param child_table - Child table Doctype
241 * @return {Boolean}
242 **/
243 first_row_is_empty: function(child_table){
244 if($.isArray(child_table) && child_table.length > 0) {
245 return !child_table[0].item_code;
246 }
247 return false;
248 },
249
250 /**
251 * Removes the first row of a child table if it is empty
252 * @param {_Frm} frm - The current form
253 * @param {String} child_table_name - The child table field name
254 * @return {Boolean}
255 **/
256 remove_empty_first_row: function(frm, child_table_name){
257 const rows = frm['doc'][child_table_name];
258 if (this.first_row_is_empty(rows)){
259 frm['doc'][child_table_name] = rows.splice(1);
260 }
261 return rows;
262 },
Zarrar6ffdf942018-06-14 12:24:16 +0530263 get_tree_options: function(option) {
264 // get valid options for tree based on user permission & locals dict
265 let unscrub_option = frappe.model.unscrub(option);
266 let user_permission = frappe.defaults.get_user_permissions();
deepeshgarg0070d64d622019-02-20 17:55:14 +0530267 let options;
268
Zarrar6ffdf942018-06-14 12:24:16 +0530269 if(user_permission && user_permission[unscrub_option]) {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530270 options = user_permission[unscrub_option].map(perm => perm.doc);
Zarrar6ffdf942018-06-14 12:24:16 +0530271 } else {
deepeshgarg0070d64d622019-02-20 17:55:14 +0530272 options = $.map(locals[`:${unscrub_option}`], function(c) { return c.name; }).sort();
Zarrar6ffdf942018-06-14 12:24:16 +0530273 }
deepeshgarg0070d64d622019-02-20 17:55:14 +0530274
275 // filter unique values, as there may be multiple user permissions for any value
276 return options.filter((value, index, self) => self.indexOf(value) === index);
Zarrar6ffdf942018-06-14 12:24:16 +0530277 },
278 get_tree_default: function(option) {
279 // set default for a field based on user permission
280 let options = this.get_tree_options(option);
281 if(options.includes(frappe.defaults.get_default(option))) {
282 return frappe.defaults.get_default(option);
283 } else {
284 return options[0];
285 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400286 },
Andy Zhubffe9332021-04-12 22:49:26 +1200287 overrides_parent_value_in_all_rows: function(doc, dt, dn, table_fieldname, fieldname, parent_fieldname) {
Marica24e45162021-04-14 18:53:15 +0530288 if (doc[parent_fieldname]) {
Andy Zhubffe9332021-04-12 22:49:26 +1200289 let cl = doc[table_fieldname] || [];
Marica24e45162021-04-14 18:53:15 +0530290 for (let i = 0; i < cl.length; i++) {
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400291 cl[i][fieldname] = doc[parent_fieldname];
292 }
Andy Zhubffe9332021-04-12 22:49:26 +1200293 frappe.refresh_field(table_fieldname);
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400294 }
Sanjay Kumar1b49f3a2018-09-06 13:09:35 +0400295 },
Nabin Hait34c551d2019-07-03 10:34:31 +0530296 create_new_doc: function (doctype, update_fields) {
297 frappe.model.with_doctype(doctype, function() {
298 var new_doc = frappe.model.get_new_doc(doctype);
299 for (let [key, value] of Object.entries(update_fields)) {
300 new_doc[key] = value;
301 }
302 frappe.ui.form.make_quick_entry(doctype, null, null, new_doc);
303 });
304 }
305
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530306});
307
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530308erpnext.utils.select_alternate_items = function(opts) {
309 const frm = opts.frm;
310 const warehouse_field = opts.warehouse_field || 'warehouse';
311 const item_field = opts.item_field || 'item_code';
312
313 this.data = [];
314 const dialog = new frappe.ui.Dialog({
315 title: __("Select Alternate Item"),
316 fields: [
317 {fieldtype:'Section Break', label: __('Items')},
318 {
319 fieldname: "alternative_items", fieldtype: "Table", cannot_add_rows: true,
320 in_place_edit: true, data: this.data,
321 get_data: () => {
322 return this.data;
323 },
324 fields: [{
325 fieldtype:'Data',
326 fieldname:"docname",
327 hidden: 1
328 }, {
329 fieldtype:'Link',
330 fieldname:"item_code",
331 options: 'Item',
332 in_list_view: 1,
333 read_only: 1,
334 label: __('Item Code')
335 }, {
336 fieldtype:'Link',
337 fieldname:"alternate_item",
338 options: 'Item',
339 default: "",
340 in_list_view: 1,
341 label: __('Alternate Item'),
342 onchange: function() {
343 const item_code = this.get_value();
344 const warehouse = this.grid_row.on_grid_fields_dict.warehouse.get_value();
345 if (item_code && warehouse) {
346 frappe.call({
347 method: "erpnext.stock.utils.get_latest_stock_qty",
348 args: {
349 item_code: item_code,
350 warehouse: warehouse
351 },
352 callback: (r) => {
353 this.grid_row.on_grid_fields_dict
354 .actual_qty.set_value(r.message || 0);
355 }
356 })
357 }
358 },
359 get_query: (e) => {
360 return {
361 query: "erpnext.stock.doctype.item_alternative.item_alternative.get_alternative_items",
362 filters: {
363 item_code: e.item_code
364 }
365 };
366 }
367 }, {
368 fieldtype:'Link',
369 fieldname:"warehouse",
370 options: 'Warehouse',
371 default: "",
372 in_list_view: 1,
373 label: __('Warehouse'),
374 onchange: function() {
375 const warehouse = this.get_value();
376 const item_code = this.grid_row.on_grid_fields_dict.item_code.get_value();
377 if (item_code && warehouse) {
378 frappe.call({
379 method: "erpnext.stock.utils.get_latest_stock_qty",
380 args: {
381 item_code: item_code,
382 warehouse: warehouse
383 },
384 callback: (r) => {
385 this.grid_row.on_grid_fields_dict
386 .actual_qty.set_value(r.message || 0);
387 }
388 })
389 }
390 },
391 }, {
392 fieldtype:'Float',
393 fieldname:"actual_qty",
394 default: 0,
395 read_only: 1,
396 in_list_view: 1,
397 label: __('Available Qty')
398 }]
399 },
400 ],
401 primary_action: function() {
402 const args = this.get_values()["alternative_items"];
403 const alternative_items = args.filter(d => {
404 if (d.alternate_item && d.item_code != d.alternate_item) {
405 return true;
406 }
407 });
408
409 alternative_items.forEach(d => {
410 let row = frappe.get_doc(opts.child_doctype, d.docname);
Doridel Cahanape42192b2018-05-16 13:28:39 +0800411 let qty = null;
412 if (row.doctype === 'Work Order Item') {
413 qty = row.required_qty;
414 } else {
415 qty = row.qty;
416 }
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530417 row[item_field] = d.alternate_item;
rohitwaghchaurea3c3dec2018-03-28 11:51:44 +0530418 frm.script_manager.trigger(item_field, row.doctype, row.name)
419 .then(() => {
420 frappe.model.set_value(row.doctype, row.name, 'qty', qty);
421 frappe.model.set_value(row.doctype, row.name,
422 opts.original_item_field, d.item_code);
423 });
424 });
425
426 refresh_field(opts.child_docname);
427 this.hide();
428 },
429 primary_action_label: __('Update')
430 });
431
432 frm.doc[opts.child_docname].forEach(d => {
433 if (!opts.condition || opts.condition(d)) {
434 dialog.fields_dict.alternative_items.df.data.push({
435 "docname": d.name,
436 "item_code": d[item_field],
437 "warehouse": d[warehouse_field],
438 "actual_qty": d.actual_qty
439 });
440 }
441 })
442
443 this.data = dialog.fields_dict.alternative_items.df.data;
444 dialog.fields_dict.alternative_items.grid.refresh();
445 dialog.show();
446}
447
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800448erpnext.utils.update_child_items = function(opts) {
449 const frm = opts.frm;
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100450 const cannot_add_row = (typeof opts.cannot_add_row === 'undefined') ? true : opts.cannot_add_row;
451 const child_docname = (typeof opts.cannot_add_row === 'undefined') ? "items" : opts.child_docname;
Saqib56fea7d2020-10-09 21:19:25 +0530452 const child_meta = frappe.get_meta(`${frm.doc.doctype} Item`);
453 const get_precision = (fieldname) => child_meta.fields.find(f => f.fieldname == fieldname).precision;
454
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800455 this.data = [];
Saqib Ansarif53299e2020-04-15 22:08:12 +0530456 const fields = [{
457 fieldtype:'Data',
458 fieldname:"docname",
459 read_only: 1,
460 hidden: 1,
461 }, {
462 fieldtype:'Link',
463 fieldname:"item_code",
464 options: 'Item',
465 in_list_view: 1,
466 read_only: 0,
467 disabled: 0,
Afshandc7280e2021-08-18 17:44:40 +0530468 label: __('Item Code'),
469 get_query: function() {
470 let filters;
471 if (frm.doc.doctype == 'Sales Order') {
472 filters = {"is_sales_item": 1};
473 } else if (frm.doc.doctype == 'Purchase Order') {
474 if (frm.doc.is_subcontracted == "Yes") {
475 filters = {"is_sub_contracted_item": 1};
476 } else {
477 filters = {"is_purchase_item": 1};
478 }
479 }
480 return {
481 query: "erpnext.controllers.queries.item_query",
482 filters: filters
483 };
484 }
Saqib Ansarif53299e2020-04-15 22:08:12 +0530485 }, {
Saqib61314242020-09-15 11:14:31 +0530486 fieldtype:'Link',
487 fieldname:'uom',
488 options: 'UOM',
489 read_only: 0,
490 label: __('UOM'),
491 reqd: 1,
492 onchange: function () {
493 frappe.call({
494 method: "erpnext.stock.get_item_details.get_conversion_factor",
495 args: { item_code: this.doc.item_code, uom: this.value },
496 callback: r => {
497 if(!r.exc) {
498 if (this.doc.conversion_factor == r.message.conversion_factor) return;
marination4f395cc2020-09-24 12:43:41 +0530499
Saqib61314242020-09-15 11:14:31 +0530500 const docname = this.doc.docname;
501 dialog.fields_dict.trans_items.df.data.some(doc => {
502 if (doc.docname == docname) {
503 doc.conversion_factor = r.message.conversion_factor;
504 dialog.fields_dict.trans_items.grid.refresh();
505 return true;
506 }
507 })
508 }
509 }
510 });
511 }
512 }, {
Saqib Ansarif53299e2020-04-15 22:08:12 +0530513 fieldtype:'Float',
514 fieldname:"qty",
515 default: 0,
516 read_only: 0,
517 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530518 label: __('Qty'),
519 precision: get_precision("qty")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530520 }, {
521 fieldtype:'Currency',
522 fieldname:"rate",
Afshan84184552021-02-24 16:51:11 +0530523 options: "currency",
Saqib Ansarif53299e2020-04-15 22:08:12 +0530524 default: 0,
525 read_only: 0,
526 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530527 label: __('Rate'),
528 precision: get_precision("rate")
Saqib Ansarif53299e2020-04-15 22:08:12 +0530529 }];
530
531 if (frm.doc.doctype == 'Sales Order' || frm.doc.doctype == 'Purchase Order' ) {
532 fields.splice(2, 0, {
533 fieldtype: 'Date',
534 fieldname: frm.doc.doctype == 'Sales Order' ? "delivery_date" : "schedule_date",
535 in_list_view: 1,
Saqib Ansaric579b082020-05-29 22:21:50 +0530536 label: frm.doc.doctype == 'Sales Order' ? __("Delivery Date") : __("Reqd by date"),
537 reqd: 1
Saqib Ansarif53299e2020-04-15 22:08:12 +0530538 })
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530539 fields.splice(3, 0, {
540 fieldtype: 'Float',
541 fieldname: "conversion_factor",
542 in_list_view: 1,
Saqib56fea7d2020-10-09 21:19:25 +0530543 label: __("Conversion Factor"),
544 precision: get_precision('conversion_factor')
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530545 })
Saqib Ansarif53299e2020-04-15 22:08:12 +0530546 }
547
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800548 const dialog = new frappe.ui.Dialog({
549 title: __("Update Items"),
550 fields: [
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800551 {
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100552 fieldname: "trans_items",
553 fieldtype: "Table",
Maricafc96c1a2020-03-06 10:57:59 +0530554 label: "Items",
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100555 cannot_add_rows: cannot_add_row,
Saqibbc919e22020-11-05 17:38:35 +0530556 in_place_edit: false,
Maricafc96c1a2020-03-06 10:57:59 +0530557 reqd: 1,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100558 data: this.data,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800559 get_data: () => {
560 return this.data;
561 },
Saqib Ansarif53299e2020-04-15 22:08:12 +0530562 fields: fields
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800563 },
564 ],
565 primary_action: function() {
566 const trans_items = this.get_values()["trans_items"];
567 frappe.call({
568 method: 'erpnext.controllers.accounts_controller.update_child_qty_rate',
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100569 freeze: true,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800570 args: {
571 'parent_doctype': frm.doc.doctype,
572 'trans_items': trans_items,
Stavros Anastasiadis3d82b742018-12-10 13:00:55 +0100573 'parent_doctype_name': frm.doc.name,
574 'child_docname': child_docname
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800575 },
576 callback: function() {
577 frm.reload_doc();
578 }
579 });
580 this.hide();
581 refresh_field("items");
582 },
583 primary_action_label: __('Update')
584 });
585
586 frm.doc[opts.child_docname].forEach(d => {
587 dialog.fields_dict.trans_items.df.data.push({
588 "docname": d.name,
Prssanna Desai4f72c0b2019-12-23 18:27:12 +0530589 "name": d.name,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800590 "item_code": d.item_code,
Saqib Ansarif53299e2020-04-15 22:08:12 +0530591 "delivery_date": d.delivery_date,
592 "schedule_date": d.schedule_date,
Saqib Ansari2c3c8aa2020-05-29 21:55:38 +0530593 "conversion_factor": d.conversion_factor,
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800594 "qty": d.qty,
595 "rate": d.rate,
Saqib61314242020-09-15 11:14:31 +0530596 "uom": d.uom
Doridel Cahanap6f06cc22018-05-17 16:28:58 +0800597 });
598 this.data = dialog.fields_dict.trans_items.df.data;
599 dialog.fields_dict.trans_items.grid.refresh();
600 })
601 dialog.show();
602}
603
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530604erpnext.utils.map_current_doc = function(opts) {
Sagar Vora78777d62021-03-09 20:35:08 +0530605 function _map() {
Rushabh Mehta55ea7b12016-07-15 16:43:25 +0530606 if($.isArray(cur_frm.doc.items) && cur_frm.doc.items.length > 0) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530607 // remove first item row if empty
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530608 if(!cur_frm.doc.items[0].item_code) {
609 cur_frm.doc.items = cur_frm.doc.items.splice(1);
610 }
bhupen3c7e70f2016-11-30 14:32:11 +0530611
612 // find the doctype of the items table
613 var items_doctype = frappe.meta.get_docfield(cur_frm.doctype, 'items').options;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530614
bhupen3c7e70f2016-11-30 14:32:11 +0530615 // find the link fieldname from items table for the given
616 // source_doctype
617 var link_fieldname = null;
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530618 frappe.get_meta(items_doctype).fields.forEach(function(d) {
bhupen3c7e70f2016-11-30 14:32:11 +0530619 if(d.options===opts.source_doctype) link_fieldname = d.fieldname; });
620
Nabin Hait802b4352017-01-09 15:32:20 +0530621 // search in existing items if the source_name is already set and full qty fetched
bhupen3c7e70f2016-11-30 14:32:11 +0530622 var already_set = false;
Nabin Hait802b4352017-01-09 15:32:20 +0530623 var item_qty_map = {};
bhupen3c7e70f2016-11-30 14:32:11 +0530624
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530625 $.each(cur_frm.doc.items, function(i, d) {
626 opts.source_name.forEach(function(src) {
627 if(d[link_fieldname]==src) {
628 already_set = true;
629 if (item_qty_map[d.item_code])
630 item_qty_map[d.item_code] += flt(d.qty);
631 else
632 item_qty_map[d.item_code] = flt(d.qty);
633 }
634 });
635 });
636
637 if(already_set) {
638 opts.source_name.forEach(function(src) {
639 frappe.model.with_doc(opts.source_doctype, src, function(r) {
640 var source_doc = frappe.model.get_doc(opts.source_doctype, src);
641 $.each(source_doc.items || [], function(i, row) {
642 if(row.qty > flt(item_qty_map[row.item_code])) {
643 already_set = false;
644 return false;
645 }
646 })
647 })
648
649 if(already_set) {
650 frappe.msgprint(__("You have already selected items from {0} {1}",
651 [opts.source_doctype, src]));
652 return;
653 }
654
655 })
Nabin Hait802b4352017-01-09 15:32:20 +0530656 }
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530657 }
658
659 return frappe.call({
660 // Sometimes we hit the limit for URL length of a GET request
661 // as we send the full target_doc. Hence this is a POST request.
662 type: "POST",
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530663 method: 'frappe.model.mapper.map_docs',
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530664 args: {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530665 "method": opts.method,
666 "source_names": opts.source_name,
667 "target_doc": cur_frm.doc,
Maricadb002702020-02-17 15:58:08 +0530668 "args": opts.args
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530669 },
670 callback: function(r) {
671 if(!r.exc) {
672 var doc = frappe.model.sync(r.message);
Vishal Dhayagudef06c2812017-12-26 16:22:40 +0530673 cur_frm.dirty();
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530674 cur_frm.refresh();
675 }
676 }
677 });
678 }
Sagar Vora78777d62021-03-09 20:35:08 +0530679
680 let query_args = {};
681 if (opts.get_query_filters) {
682 query_args.filters = opts.get_query_filters;
683 }
684
685 if (opts.get_query_method) {
686 query_args.query = opts.get_query_method;
687 }
688
689 if (query_args.filters || query_args.query) {
690 opts.get_query = () => query_args;
691 }
692
693 if (opts.source_doctype) {
694 const d = new frappe.ui.form.MultiSelectDialog({
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530695 doctype: opts.source_doctype,
696 target: opts.target,
697 date_field: opts.date_field || undefined,
698 setters: opts.setters,
699 get_query: opts.get_query,
marination4f395cc2020-09-24 12:43:41 +0530700 add_filters_group: 1,
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530701 action: function(selections, args) {
702 let values = selections;
703 if(values.length === 0){
Nabin Haita11dcb62017-12-04 13:36:50 +0530704 frappe.msgprint(__("Please select {0}", [opts.source_doctype]))
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530705 return;
706 }
707 opts.source_name = values;
708 opts.setters = args;
709 d.dialog.hide();
710 _map();
711 },
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530712 });
Sagar Vora78777d62021-03-09 20:35:08 +0530713
714 return d;
715 }
716
717 if (opts.source_name) {
Prateeksha Singhedeb4dc2017-05-15 11:32:06 +0530718 opts.source_name = [opts.source_name];
Rushabh Mehta176b63b2016-07-14 17:43:48 +0530719 _map();
720 }
721}
722
Rushabh Mehta180e4352016-07-26 17:57:42 +0530723frappe.form.link_formatters['Item'] = function(value, doc) {
Alan157b3882021-04-21 21:00:22 +0530724 if (doc && value && doc.item_name && doc.item_name !== value && doc.item_code === value) {
marination824f48f2020-10-12 20:08:03 +0530725 return value + ': ' + doc.item_name;
726 } else if (!value && doc.doctype && doc.item_name) {
727 // format blank value in child table
728 return doc.item_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530729 } else {
marination824f48f2020-10-12 20:08:03 +0530730 // 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 +0530731 return value;
732 }
733}
734
735frappe.form.link_formatters['Employee'] = function(value, doc) {
Nabin Hait981f6ea2016-07-31 11:44:43 +0530736 if(doc && doc.employee_name && doc.employee_name !== value) {
mbauskarbe814422016-10-27 09:49:22 +0530737 return value? value + ': ' + doc.employee_name: doc.employee_name;
Rushabh Mehta180e4352016-07-26 17:57:42 +0530738 } else {
739 return value;
740 }
741}
742
Rucha Mahabal59961f72021-05-20 23:43:19 +0530743frappe.form.link_formatters['Project'] = function(value, doc) {
744 if (doc && value && doc.project_name && doc.project_name !== value && doc.project === value) {
745 return value + ': ' + doc.project_name;
746 } else if (!value && doc.doctype && doc.project_name) {
747 // format blank value in child table
748 return doc.project;
749 } else {
750 // if value is blank in report view or project name and name are the same, return as is
751 return value;
752 }
753};
754
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530755// add description on posting time
756$(document).on('app_ready', function() {
757 if(!frappe.datetime.is_timezone_same()) {
758 $.each(["Stock Reconciliation", "Stock Entry", "Stock Ledger Entry",
759 "Delivery Note", "Purchase Receipt", "Sales Invoice"], function(i, d) {
760 frappe.ui.form.on(d, "onload", function(frm) {
761 cur_frm.set_df_property("posting_time", "description",
Faris Ansariab74ca72017-05-30 12:54:42 +0530762 frappe.sys_defaults.time_zone);
Rushabh Mehta95b995b2015-05-28 12:10:55 +0530763 });
764 });
765 }
akshay14384c22016-07-28 13:53:17 +0530766});
Alanc6dc9ea2021-05-07 20:30:04 +0530767
Himanshuec25d592021-06-14 19:05:52 +0530768// Show SLA dashboard
769$(document).on('app_ready', function() {
770 frappe.call({
771 method: 'erpnext.support.doctype.service_level_agreement.service_level_agreement.get_sla_doctypes',
772 callback: function(r) {
773 if (!r.message)
774 return;
775
776 $.each(r.message, function(_i, d) {
777 frappe.ui.form.on(d, {
778 onload: function(frm) {
779 if (!frm.doc.service_level_agreement)
780 return;
781
782 frappe.call({
783 method: 'erpnext.support.doctype.service_level_agreement.service_level_agreement.get_service_level_agreement_filters',
784 args: {
785 doctype: frm.doc.doctype,
786 name: frm.doc.service_level_agreement,
787 customer: frm.doc.customer
788 },
789 callback: function (r) {
790 if (r && r.message) {
791 frm.set_query('priority', function() {
792 return {
793 filters: {
794 'name': ['in', r.message.priority],
795 }
796 };
797 });
798 frm.set_query('service_level_agreement', function() {
799 return {
800 filters: {
801 'name': ['in', r.message.service_level_agreements],
802 }
803 };
804 });
805 }
806 }
807 });
808 },
809
810 refresh: function(frm) {
811 if (frm.doc.status !== 'Closed' && frm.doc.service_level_agreement
812 && frm.doc.agreement_status === 'Ongoing') {
813 frappe.call({
814 'method': 'frappe.client.get',
815 args: {
816 doctype: 'Service Level Agreement',
817 name: frm.doc.service_level_agreement
818 },
819 callback: function(data) {
820 let statuses = data.message.pause_sla_on;
821 const hold_statuses = [];
822 $.each(statuses, (_i, entry) => {
823 hold_statuses.push(entry.status);
824 });
825 if (hold_statuses.includes(frm.doc.status)) {
826 frm.dashboard.clear_headline();
827 let message = {'indicator': 'orange', 'msg': __('SLA is on hold since {0}', [moment(frm.doc.on_hold_since).fromNow(true)])};
828 frm.dashboard.set_headline_alert(
829 '<div class="row">' +
830 '<div class="col-xs-12">' +
831 '<span class="indicator whitespace-nowrap '+ message.indicator +'"><span>'+ message.msg +'</span></span> ' +
832 '</div>' +
833 '</div>'
834 );
835 } else {
836 set_time_to_resolve_and_response(frm, data.message.apply_sla_for_resolution);
837 }
838 }
839 });
840 } else if (frm.doc.service_level_agreement) {
841 frm.dashboard.clear_headline();
842
843 let agreement_status = (frm.doc.agreement_status == 'Fulfilled') ?
844 {'indicator': 'green', 'msg': 'Service Level Agreement has been fulfilled'} :
845 {'indicator': 'red', 'msg': 'Service Level Agreement Failed'};
846
847 frm.dashboard.set_headline_alert(
848 '<div class="row">' +
849 '<div class="col-xs-12">' +
850 '<span class="indicator whitespace-nowrap '+ agreement_status.indicator +'"><span class="hidden-xs">'+ agreement_status.msg +'</span></span> ' +
851 '</div>' +
852 '</div>'
853 );
854 }
855 },
856 });
857 });
858 }
859 });
860});
861
862function set_time_to_resolve_and_response(frm, apply_sla_for_resolution) {
863 frm.dashboard.clear_headline();
864
865 let time_to_respond = get_status(frm.doc.response_by_variance);
866 if (!frm.doc.first_responded_on && frm.doc.agreement_status === 'Ongoing') {
867 time_to_respond = get_time_left(frm.doc.response_by, frm.doc.agreement_status);
868 }
869
870 let alert = `
871 <div class="row">
872 <div class="col-xs-12 col-sm-6">
873 <span class="indicator whitespace-nowrap ${time_to_respond.indicator}">
874 <span>Time to Respond: ${time_to_respond.diff_display}</span>
875 </span>
876 </div>`;
877
878
879 if (apply_sla_for_resolution) {
880 let time_to_resolve = get_status(frm.doc.resolution_by_variance);
881 if (!frm.doc.resolution_date && frm.doc.agreement_status === 'Ongoing') {
882 time_to_resolve = get_time_left(frm.doc.resolution_by, frm.doc.agreement_status);
883 }
884
885 alert += `
886 <div class="col-xs-12 col-sm-6">
887 <span class="indicator whitespace-nowrap ${time_to_resolve.indicator}">
888 <span>Time to Resolve: ${time_to_resolve.diff_display}</span>
889 </span>
890 </div>`;
891 }
892
893 alert += '</div>';
894
895 frm.dashboard.set_headline_alert(alert);
896}
897
898function get_time_left(timestamp, agreement_status) {
899 const diff = moment(timestamp).diff(moment());
900 const diff_display = diff >= 44500 ? moment.duration(diff).humanize() : 'Failed';
901 let indicator = (diff_display == 'Failed' && agreement_status != 'Fulfilled') ? 'red' : 'green';
902 return {'diff_display': diff_display, 'indicator': indicator};
903}
904
905function get_status(variance) {
906 if (variance > 0) {
907 return {'diff_display': 'Fulfilled', 'indicator': 'green'};
908 } else {
909 return {'diff_display': 'Failed', 'indicator': 'red'};
910 }
911}
912
Alanc6dc9ea2021-05-07 20:30:04 +0530913function attach_selector_button(inner_text, append_loction, context, grid_row) {
914 let $btn_div = $("<div>").css({"margin-bottom": "10px", "margin-top": "10px"})
915 .appendTo(append_loction);
916 let $btn = $(`<button class="btn btn-sm btn-default">${inner_text}</button>`)
917 .appendTo($btn_div);
918
919 $btn.on("click", function() {
920 context.show_serial_batch_selector(grid_row.frm, grid_row.doc, "", "", true);
921 });
922}