Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 93 additions & 16 deletions erpnext/public/js/controllers/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -1832,8 +1832,12 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe
this._last_party_account_currency = this.frm.doc.party_account_currency;
this._last_company_currency = company_currency;

this.toggle_base_currency_fields(company_currency);
this.change_form_labels(company_currency);
this.change_grid_labels(company_currency);
if (this.frm.doc.currency == company_currency) {
this.toggle_base_currency_fields(company_currency);
}
this.frm.refresh_fields();
}

Expand All @@ -1845,7 +1849,7 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe
};
}

set_currency_labels_from_options(currency_options, parentfield) {
set_currency_labels_from_options(currency_options, parentfield, company_currency) {
const doctype = parentfield ? this.frm.fields_dict[parentfield].grid.doctype : this.frm.doc.doctype;
const docfields = frappe.meta.get_docfields(doctype);

Expand All @@ -1854,16 +1858,98 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe
.filter((df) => df.fieldtype === "Currency" && df.options === options)
.map((df) => df.fieldname);

if (this.frm.doc.currency == company_currency && currency == company_currency) {
this.reset_currency_labels(
fields.filter((field) => !field.startsWith("base_")),
parentfield
);
return;
}

this.frm.set_currency_labels(fields, currency, parentfield);
});
}

reset_currency_labels(fields, parentfield) {
if (!fields.length) return;

const doctype = parentfield ? this.frm.fields_dict[parentfield].grid.doctype : this.frm.doc.doctype;

fields.forEach((field) => {
const docfield = frappe.meta.docfield_map[doctype][field];
if (docfield) {
const label = __(docfield.label || "", null, docfield.parent)
.replace(/\s*\([^\)]*\)\s*$/, "")
.trim();

if (parentfield) {
this.frm.fields_dict[parentfield].grid.update_docfield_property(field, "label", label);
} else {
this.frm.fields_dict[field].set_label(label);
}
}
});
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

toggle_base_currency_fields(company_currency) {
const show = this.frm.doc.currency != company_currency;
this._base_currency_field_visibility = this._base_currency_field_visibility || {};

const parent_fields = { show: [], hide: [] };
(frappe.meta.get_docfields(this.frm.doctype, this.frm.docname) || []).forEach((df) => {
if (df.fieldname?.startsWith("base_")) {
const field_key = `${this.frm.doctype}.${df.fieldname}`;
const field_list = this.should_show_base_currency_field(field_key, df, show)
? "show"
: "hide";
parent_fields[field_list].push(df.fieldname);
}
});

if (parent_fields.show.length) this.frm.toggle_display(parent_fields.show, true);
if (parent_fields.hide.length) this.frm.toggle_display(parent_fields.hide, false);

Object.values(this.frm.fields_dict).forEach((field) => {
if (!field.grid) return;

const grid_fields = { show: [], hide: [] };
(field.grid.docfields || []).forEach((df) => {
if (df.fieldname?.startsWith("base_")) {
const field_key =
`${this.frm.doctype}.${field.grid.df.fieldname}` +
`.${field.grid.doctype}.${df.fieldname}`;
const field_list = this.should_show_base_currency_field(field_key, df, show)
? "show"
: "hide";
grid_fields[field_list].push(df.fieldname);
}
});

if (grid_fields.show.length) field.grid.set_column_disp(grid_fields.show, true);
if (grid_fields.hide.length) field.grid.set_column_disp(grid_fields.hide, false);
});
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

should_show_base_currency_field(field_key, df, show) {
if (!Object.prototype.hasOwnProperty.call(this._base_currency_field_visibility, field_key)) {
this._base_currency_field_visibility[field_key] = {
originalHidden: cint(df.hidden) === 1,
};
}

return show && this._base_currency_field_visibility[field_key].originalHidden !== true;
}

change_form_labels(company_currency) {
let me = this;
const currency_options = this.get_currency_label_options(company_currency);

this.set_currency_labels_from_options(currency_options);
this.frm.set_currency_labels(["totals_section"], this.frm.doc.currency);
this.set_currency_labels_from_options(currency_options, null, company_currency);
if (this.frm.doc.currency == company_currency) {
this.reset_currency_labels(["totals_section"]);
} else {
this.frm.set_currency_labels(["totals_section"], this.frm.doc.currency);
}
this.frm.set_currency_labels(["base_totals_section"], company_currency);

this.frm.set_df_property(
Expand Down Expand Up @@ -1942,7 +2028,7 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe
var me = this;
const currency_options = this.get_currency_label_options(company_currency);

this.toggle_item_grid_columns(company_currency);
this.toggle_item_grid_columns();

for (const child_table of [
"items",
Expand All @@ -1954,7 +2040,7 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe
"sales_team",
]) {
if (this.frm.fields_dict[child_table]) {
this.set_currency_labels_from_options(currency_options, child_table);
this.set_currency_labels_from_options(currency_options, child_table, company_currency);
}
}

Expand Down Expand Up @@ -2018,17 +2104,9 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe
}
}

toggle_item_grid_columns(company_currency) {
const me = this;
toggle_item_grid_columns() {
// toggle columns
var item_grid = this.frm.fields_dict["items"].grid;
$.each(
["base_rate", "base_price_list_rate", "base_amount", "base_rate_with_margin"],
function (i, fname) {
if (frappe.meta.get_docfield(item_grid.doctype, fname))
item_grid.set_column_disp(fname, me.frm.doc.currency != company_currency);
}
);

var show =
cint(this.frm.doc.discount_amount) ||
Expand All @@ -2041,8 +2119,7 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe
});

$.each(["base_net_rate", "base_net_amount"], function (i, fname) {
if (frappe.meta.get_docfield(item_grid.doctype, fname))
item_grid.set_column_disp(fname, show && me.frm.doc.currency != company_currency);
if (frappe.meta.get_docfield(item_grid.doctype, fname)) item_grid.set_column_disp(fname, show);
});
}

Expand Down
Loading