fix: currency precision formatter (#21293)

This commit is contained in:
Ankush Menat 2023-06-09 00:35:20 +05:30 committed by GitHub
parent e1764d5a4b
commit 155465c58d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 4 deletions

View file

@ -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();
});
});
});

View file

@ -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 {

View file

@ -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) {