2
0
mirror of https://github.com/frappe/books.git synced 2024-12-23 11:29:03 +00:00

fix: init pesa with display too

This commit is contained in:
18alantom 2021-12-23 17:08:32 +05:30
parent 23189aaf93
commit e1f1ffd202
3 changed files with 34 additions and 19 deletions

View File

@ -1,7 +1,10 @@
const Observable = require('./utils/observable'); const Observable = require('./utils/observable');
const utils = require('./utils'); const utils = require('./utils');
const { getMoneyMaker } = require('pesa'); const { getMoneyMaker } = require('pesa');
const { DEFAULT_INTERNAL_PRECISION } = require('./utils/consts'); const {
DEFAULT_INTERNAL_PRECISION,
DEFAULT_DISPLAY_PRECISION,
} = require('./utils/consts');
module.exports = { module.exports = {
initializeAndRegister(customModels = {}, force = false) { initializeAndRegister(customModels = {}, force = false) {
@ -20,27 +23,25 @@ module.exports = {
let values; let values;
try { try {
// error thrown if migration hasn't taken place // error thrown if migration hasn't taken place
values = await frappe.db.getSingleValues({ values = await frappe.db.getSingleValues(
fieldname: 'internalPrecision', {
parent: 'SystemSettings', fieldname: 'internalPrecision',
}); parent: 'SystemSettings',
},
{
fieldname: 'displayPrecision',
parent: 'SystemSettings',
}
);
} catch { } catch {
values = []; values = [];
} }
let { internalPrecision: precision } = values.reduce( let { internalPrecision: precision, displayPrecision: display } =
(acc, { fieldname, value }) => { values.reduce((acc, { fieldname, value }) => {
acc[fieldname] = value; acc[fieldname] = value;
return acc; return acc;
}, }, {});
{}
);
if (typeof precision === 'undefined') {
precision = this.getMeta('SystemSettings').fields.find(
(f) => f.fieldname === 'internalPrecision'
)?.default;
}
if (typeof precision === 'undefined') { if (typeof precision === 'undefined') {
precision = DEFAULT_INTERNAL_PRECISION; precision = DEFAULT_INTERNAL_PRECISION;
@ -50,7 +51,15 @@ module.exports = {
precision = parseInt(precision); 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) { init(force) {

View File

@ -1,5 +1,9 @@
const { DateTime } = require('luxon'); const { DateTime } = require('luxon');
const { _ } = require('frappejs/utils'); const { _ } = require('frappejs/utils');
const {
DEFAULT_DISPLAY_PRECISION,
DEFAULT_INTERNAL_PRECISION,
} = require('../../../utils/consts');
let dateFormatOptions = (() => { let dateFormatOptions = (() => {
let formats = [ let formats = [
@ -43,7 +47,7 @@ module.exports = {
fieldname: 'displayPrecision', fieldname: 'displayPrecision',
label: 'Display Precision', label: 'Display Precision',
fieldtype: 'Int', fieldtype: 'Int',
default: 2, default: DEFAULT_DISPLAY_PRECISION,
required: 1, required: 1,
minValue: 0, minValue: 0,
maxValue: 9, maxValue: 9,
@ -61,7 +65,8 @@ module.exports = {
fieldname: 'internalPrecision', fieldname: 'internalPrecision',
label: 'Internal Precision', label: 'Internal Precision',
fieldtype: 'Int', fieldtype: 'Int',
default: 11, minValue: 0,
default: DEFAULT_INTERNAL_PRECISION,
description: _( description: _(
'Sets the internal precision used for monetary calculations. Above 6 should be sufficient for most currencies.' 'Sets the internal precision used for monetary calculations. Above 6 should be sufficient for most currencies.'
), ),

View File

@ -1 +1,2 @@
export const DEFAULT_INTERNAL_PRECISION = 11; export const DEFAULT_INTERNAL_PRECISION = 11;
export const DEFAULT_DISPLAY_PRECISION = 2;