2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +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 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({
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) {

View File

@ -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.'
),

View File

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