diff --git a/cypress/integration/control_currency.js b/cypress/integration/control_currency.js new file mode 100644 index 000000000000..5e6db8603602 --- /dev/null +++ b/cypress/integration/control_currency.js @@ -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(); + }); + }); +}); diff --git a/frappe/public/js/frappe/form/controls/currency.js b/frappe/public/js/frappe/form/controls/currency.js index a7d30c071b6c..e2f79ba446fd 100644 --- a/frappe/public/js/frappe/form/controls/currency.js +++ b/frappe/public/js/frappe/form/controls/currency.js @@ -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 { diff --git a/frappe/public/js/frappe/form/formatters.js b/frappe/public/js/frappe/form/formatters.js index 637fd7063d91..beddbf512d1f 100644 --- a/frappe/public/js/frappe/form/formatters.js +++ b/frappe/public/js/frappe/form/formatters.js @@ -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) {