2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00

fix: update calcs

- set child doc defaults
- remove Currency from rounding
This commit is contained in:
18alantom 2021-12-27 16:57:58 +05:30
parent 4b0cf18b38
commit e214891156

View File

@ -1,6 +1,7 @@
const frappe = require('frappejs');
const Observable = require('frappejs/utils/observable');
const naming = require('./naming');
const { isPesa } = require('../utils/index');
const { round } = require('frappejs/utils/numberFormat');
const { DEFAULT_INTERNAL_PRECISION } = require('../utils/consts');
@ -107,17 +108,12 @@ module.exports = class BaseDocument extends Observable {
setDefaults() {
for (let field of this.meta.fields) {
if (this[field.fieldname] == null) {
let defaultValue = null;
let defaultValue = getPreDefaultValues(field.fieldtype);
if (field.fieldtype === 'Table') {
defaultValue = [];
}
if (field.default) {
if (typeof field.default === 'function') {
defaultValue = field.default(this);
} else {
defaultValue = field.default;
}
if (typeof field.default === 'function') {
defaultValue = field.default(this);
} else if (field.default !== undefined) {
defaultValue = field.default;
}
this[field.fieldname] = defaultValue;
@ -137,8 +133,10 @@ module.exports = class BaseDocument extends Observable {
}
if (['Int', 'Check'].includes(field.fieldtype)) {
value = parseInt(value, 10);
} else if (['Float', 'Currency'].includes(field.fieldtype)) {
} else if (field.fieldtype === 'Float') {
value = parseFloat(value);
} else if (field.fieldtype === 'Currency' && !isPesa(value)) {
value = frappe.pesa(value);
}
this[field.fieldname] = value;
}
@ -170,23 +168,25 @@ module.exports = class BaseDocument extends Observable {
_initChild(data, key) {
if (data instanceof BaseDocument) {
return data;
} else {
data.doctype = this.meta.getField(key).childtype;
data.parent = this.name;
data.parenttype = this.doctype;
data.parentfield = key;
data.parentdoc = this;
if (!data.idx) {
data.idx = (this[key] || []).length;
}
if (!data.name) {
data.name = frappe.getRandomString();
}
return new BaseDocument(data);
}
data.doctype = this.meta.getField(key).childtype;
data.parent = this.name;
data.parenttype = this.doctype;
data.parentfield = key;
data.parentdoc = this;
if (!data.idx) {
data.idx = (this[key] || []).length;
}
if (!data.name) {
data.name = frappe.getRandomString();
}
const childDoc = new BaseDocument(data);
childDoc.setDefaults();
return childDoc;
}
validateInsert() {
@ -509,7 +509,7 @@ module.exports = class BaseDocument extends Observable {
return;
}
if (['Float', 'Currency'].includes(field.fieldtype)) {
if ('Float' === field.fieldtype) {
value = this.round(value, field);
}
@ -527,7 +527,7 @@ module.exports = class BaseDocument extends Observable {
roundFloats() {
let fields = this.meta
.getValidFields()
.filter((df) => ['Float', 'Currency', 'Table'].includes(df.fieldtype));
.filter((df) => ['Float', 'Table'].includes(df.fieldtype));
for (let df of fields) {
let value = this[df.fieldname];
@ -659,10 +659,21 @@ module.exports = class BaseDocument extends Observable {
}
// helper functions
getSum(tablefield, childfield) {
return (this[tablefield] || [])
.map((d) => parseFloat(d[childfield], 10) || 0)
.reduce((a, b) => a + b, 0);
getSum(tablefield, childfield, convertToFloat = true) {
const sum = (this[tablefield] || [])
.map((d) => {
const value = d[childfield] ?? 0;
if (!isPesa(value)) {
return frappe.pesa(value);
}
return value;
})
.reduce((a, b) => a.add(b), frappe.pesa(0));
if (convertToFloat) {
return sum.float;
}
return sum;
}
getFrom(doctype, name, fieldname) {
@ -690,3 +701,17 @@ module.exports = class BaseDocument extends Observable {
}, {});
}
};
function getPreDefaultValues(fieldtype) {
switch (fieldtype) {
case 'Table':
return [];
case 'Currency':
return frappe.pesa(0.0);
case 'Int':
case 'Float':
return 0;
default:
return null;
}
}