diff --git a/index.js b/index.js index e51c7755..19bf2be4 100644 --- a/index.js +++ b/index.js @@ -12,17 +12,28 @@ module.exports = { this.registerModels(customModels); }, - async initializeMoneyMaker() { + async initializeMoneyMaker(currency) { + currency ??= 'XXX'; + // to be called after db initialization - const { currency, internalPrecision: precision } = ( - await frappe.db.getSingleValues( - { fieldname: 'currency', parent: 'AccountingSettings' }, - { fieldname: 'internalPrecision', parent: 'SystemSettings' } - ) - ).reduce((acc, { fieldname, value }) => { - acc[fieldname] = value; - return acc; - }, {}); + let values; + try { + // error thrown if migration hasn't taken place + values = await frappe.db.getSingleValues({ + fieldname: 'internalPrecision', + parent: 'SystemSettings', + }); + } catch { + values = []; + } + + let { internalPrecision: precision } = values.reduce( + (acc, { fieldname, value }) => { + acc[fieldname] = value; + return acc; + }, + {} + ); if (typeof precision === 'undefined') { precision = this.getMeta('SystemSettings').fields.find( @@ -31,9 +42,7 @@ module.exports = { } if (typeof precision === 'undefined') { - throw new frappe.errors.NotFoundError( - 'SystemSettings internalPrecision value is undefined' - ); + precision = 11; } if (typeof precision.value === 'string') { diff --git a/models/doctype/SystemSettings/SystemSettings.js b/models/doctype/SystemSettings/SystemSettings.js index 7c6ae875..ac68075e 100644 --- a/models/doctype/SystemSettings/SystemSettings.js +++ b/models/doctype/SystemSettings/SystemSettings.js @@ -52,7 +52,7 @@ module.exports = { fieldname: 'internalPrecision', label: 'Internal Precision', fieldtype: 'Int', - default: 12, + default: 11, description: _( 'Sets the internal precision used for monetary calculations. Above 6 should be sufficient for most currencies.' ), diff --git a/package.json b/package.json index a1c5855b..27b8794a 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "multer": "^1.4.3", "node-fetch": "^3.0.0", "nunjucks": "^3.2.3", - "pesa": "^1.0.3", + "pesa": "^1.0.4", "postcss": "^8.3.11", "postcss-loader": "^6.2.0", "sass-loader": "^12.3.0", diff --git a/yarn.lock b/yarn.lock index 3caaa78e..84e6bdc5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3417,10 +3417,10 @@ performance-now@^2.1.0: resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b" integrity sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns= -pesa@^1.0.3: - version "1.0.3" - resolved "https://registry.yarnpkg.com/pesa/-/pesa-1.0.3.tgz#e0eab7a13a6a8d0cfd1cbc0214aece6befd63a74" - integrity sha512-UGw3TPnQKAcM0EhPzQO17cXtibBcJCCqlB/hjfYtVPFAgDOIJ2kv6az9Fwq5Gflp2R7hODZlJy1cG+eL70od/g== +pesa@^1.0.4: + version "1.0.4" + resolved "https://registry.yarnpkg.com/pesa/-/pesa-1.0.4.tgz#aaed9760b319d1435ea663bdb96c0a99f88cbd53" + integrity sha512-SuXEyavfSv1GGk625Mgdmw8ZJvNaMQ6C5e413zxTpkdLmDdTwWidhlQDDbFuI+Drj1GBHu/L20NVsoRUjUjU5w== pg-connection-string@2.5.0: version "2.5.0"