From e1f1ffd2024ee60c7b94b9b69ca887cf2abbe4e3 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Thu, 23 Dec 2021 17:08:32 +0530 Subject: [PATCH] fix: init pesa with display too --- index.js | 43 +++++++++++-------- .../doctype/SystemSettings/SystemSettings.js | 9 +++- utils/consts.js | 1 + 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index 1754c156..7c522ea6 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,10 @@ const Observable = require('./utils/observable'); const utils = require('./utils'); const { getMoneyMaker } = require('pesa'); -const { DEFAULT_INTERNAL_PRECISION } = require('./utils/consts'); +const { + DEFAULT_INTERNAL_PRECISION, + DEFAULT_DISPLAY_PRECISION, +} = require('./utils/consts'); module.exports = { initializeAndRegister(customModels = {}, force = false) { @@ -20,27 +23,25 @@ module.exports = { let values; try { // error thrown if migration hasn't taken place - values = await frappe.db.getSingleValues({ - fieldname: 'internalPrecision', - parent: 'SystemSettings', - }); + values = await frappe.db.getSingleValues( + { + fieldname: 'internalPrecision', + parent: 'SystemSettings', + }, + { + fieldname: 'displayPrecision', + parent: 'SystemSettings', + } + ); } catch { values = []; } - let { internalPrecision: precision } = values.reduce( - (acc, { fieldname, value }) => { + let { internalPrecision: precision, displayPrecision: display } = + values.reduce((acc, { fieldname, value }) => { acc[fieldname] = value; return acc; - }, - {} - ); - - if (typeof precision === 'undefined') { - precision = this.getMeta('SystemSettings').fields.find( - (f) => f.fieldname === 'internalPrecision' - )?.default; - } + }, {}); if (typeof precision === 'undefined') { precision = DEFAULT_INTERNAL_PRECISION; @@ -50,7 +51,15 @@ module.exports = { precision = parseInt(precision); } - this.pesa = getMoneyMaker({ currency, precision }); + if (typeof display === 'undefined') { + display = DEFAULT_DISPLAY_PRECISION; + } + + if (typeof display === 'string') { + display = parseInt(display); + } + + this.pesa = getMoneyMaker({ currency, precision, display }); }, init(force) { diff --git a/models/doctype/SystemSettings/SystemSettings.js b/models/doctype/SystemSettings/SystemSettings.js index 92396cfe..c2b29e79 100644 --- a/models/doctype/SystemSettings/SystemSettings.js +++ b/models/doctype/SystemSettings/SystemSettings.js @@ -1,5 +1,9 @@ const { DateTime } = require('luxon'); const { _ } = require('frappejs/utils'); +const { + DEFAULT_DISPLAY_PRECISION, + DEFAULT_INTERNAL_PRECISION, +} = require('../../../utils/consts'); let dateFormatOptions = (() => { let formats = [ @@ -43,7 +47,7 @@ module.exports = { fieldname: 'displayPrecision', label: 'Display Precision', fieldtype: 'Int', - default: 2, + default: DEFAULT_DISPLAY_PRECISION, required: 1, minValue: 0, maxValue: 9, @@ -61,7 +65,8 @@ module.exports = { fieldname: 'internalPrecision', label: 'Internal Precision', fieldtype: 'Int', - default: 11, + minValue: 0, + default: DEFAULT_INTERNAL_PRECISION, description: _( 'Sets the internal precision used for monetary calculations. Above 6 should be sufficient for most currencies.' ), diff --git a/utils/consts.js b/utils/consts.js index f601d1d8..2fc503b5 100644 --- a/utils/consts.js +++ b/utils/consts.js @@ -1 +1,2 @@ export const DEFAULT_INTERNAL_PRECISION = 11; +export const DEFAULT_DISPLAY_PRECISION = 2;