8000 fix: currency precision formatter by ankush · Pull Request #21293 · frappe/frappe · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: currency precision formatter #21293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
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
8000
74 changes: 74 additions & 0 deletions cypress/integration/control_currency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
context("Control Currency", () => {
const fieldname = "currency_field";

before(() => {
cy.login();
cy.visit("/app/website");
});

function get_dialog_with_currency(df_options = {}) {
return cy.dialog({
title: "Currency Check",
fields: [
{
fieldname: fieldname,
fieldtype: "Currency",
Label: "Currency",
...df_options,
},
],
});
}

it("check value changes", () => {
const TEST_CASES = [
{
input: "10.101",
df_options: { precision: 1 },
blur_expected: "10.1",
},
{
input: "10.101",
df_options: { precision: "3" },
blur_expected: "10.101",
},
{
input: "10.101",
df_options: { precision: "" }, // default assumed to be 2;
blur_expected: "10.10",
},
{
input: "10.101",
df_options: { precision: "0" },
blur_expected: "10",
},
{
input: "10.101",
df_options: { precision: 0 },
blur_expected: "10",
},
{
input: "10.101",
df_options: { precision: "" },
blur_expected: "10.1",
default_precision: 1,
},
];

TEST_CASES.forEach((test_case) => {
cy.window()
.its("frappe")
.then((frappe) => {
frappe.boot.sysdefaults.currency = test_case.currency;
frappe.boot.sysdefaults.currency_precision = test_case.default_precision ?? 2;
});

get_dialog_with_currency(test_case.df_options).as("dialog");
cy.get_field(fieldname, "Currency").clear();
cy.wait(300);
cy.fill_field(fieldname, test_case.input, "Currency").blur();
cy.get_field(fieldname, "Currency").should("have.value", test_case.blur_expected);
cy.hide_dialog();
});
});
});
2 changes: 1 addition & 1 deletion frappe/public/js/frappe/form/controls/currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ frappe.ui.form.ControlCurrency = class ControlCurrency extends frappe.ui.form.Co
get_precision() {
// always round based on field precision or currency's precision
// this method is also called in this.parse()
if (!this.df.precision) {
if (typeof this.df.precision != "number" && !this.df.precision) {
if (frappe.boot.sysdefaults.currency_precision) {
this.df.precision = frappe.boot.sysdefaults.currency_precision;
} else {
Expand Down
12 changes: 9 additions & 3 deletions frappe/public/js/frappe/form/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,15 @@ frappe.form.formatters = {
},
Currency: function (value, docfield, options, doc) {
var currency = frappe.meta.get_field_currency(docfield, doc);
var precision = cint(
docfield.precision ?? frappe.boot.sysdefaults.currency_precision ?? 2
);

let precision;
if (typeof docfield.precision == "number") {
precision = docfield.precision;
} else {
precision = cint(
docfield.precision || frappe.boot.sysdefaults.currency_precision || 2
);
}

// If you change anything below, it's going to hurt a company in UAE, a bit.
if (precision > 2) {
Expand Down
0