2
0
mirror of https://github.com/frappe/books.git synced 2024-12-25 04:06:04 +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 frappe = require('frappejs');
const Observable = require('frappejs/utils/observable'); const Observable = require('frappejs/utils/observable');
const naming = require('./naming'); const naming = require('./naming');
const { isPesa } = require('../utils/index');
const { round } = require('frappejs/utils/numberFormat'); const { round } = require('frappejs/utils/numberFormat');
const { DEFAULT_INTERNAL_PRECISION } = require('../utils/consts'); const { DEFAULT_INTERNAL_PRECISION } = require('../utils/consts');
@ -107,17 +108,12 @@ module.exports = class BaseDocument extends Observable {
setDefaults() { setDefaults() {
for (let field of this.meta.fields) { for (let field of this.meta.fields) {
if (this[field.fieldname] == null) { if (this[field.fieldname] == null) {
let defaultValue = null; let defaultValue = getPreDefaultValues(field.fieldtype);
if (field.fieldtype === 'Table') { if (typeof field.default === 'function') {
defaultValue = []; defaultValue = field.default(this);
} } else if (field.default !== undefined) {
if (field.default) { defaultValue = field.default;
if (typeof field.default === 'function') {
defaultValue = field.default(this);
} else {
defaultValue = field.default;
}
} }
this[field.fieldname] = defaultValue; this[field.fieldname] = defaultValue;
@ -137,8 +133,10 @@ module.exports = class BaseDocument extends Observable {
} }
if (['Int', 'Check'].includes(field.fieldtype)) { if (['Int', 'Check'].includes(field.fieldtype)) {
value = parseInt(value, 10); value = parseInt(value, 10);
} else if (['Float', 'Currency'].includes(field.fieldtype)) { } else if (field.fieldtype === 'Float') {
value = parseFloat(value); value = parseFloat(value);
} else if (field.fieldtype === 'Currency' && !isPesa(value)) {
value = frappe.pesa(value);
} }
this[field.fieldname] = value; this[field.fieldname] = value;
} }
@ -170,23 +168,25 @@ module.exports = class BaseDocument extends Observable {
_initChild(data, key) { _initChild(data, key) {
if (data instanceof BaseDocument) { if (data instanceof BaseDocument) {
return data; 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() { validateInsert() {
@ -509,7 +509,7 @@ module.exports = class BaseDocument extends Observable {
return; return;
} }
if (['Float', 'Currency'].includes(field.fieldtype)) { if ('Float' === field.fieldtype) {
value = this.round(value, field); value = this.round(value, field);
} }
@ -527,7 +527,7 @@ module.exports = class BaseDocument extends Observable {
roundFloats() { roundFloats() {
let fields = this.meta let fields = this.meta
.getValidFields() .getValidFields()
.filter((df) => ['Float', 'Currency', 'Table'].includes(df.fieldtype)); .filter((df) => ['Float', 'Table'].includes(df.fieldtype));
for (let df of fields) { for (let df of fields) {
let value = this[df.fieldname]; let value = this[df.fieldname];
@ -659,10 +659,21 @@ module.exports = class BaseDocument extends Observable {
} }
// helper functions // helper functions
getSum(tablefield, childfield) { getSum(tablefield, childfield, convertToFloat = true) {
return (this[tablefield] || []) const sum = (this[tablefield] || [])
.map((d) => parseFloat(d[childfield], 10) || 0) .map((d) => {
.reduce((a, b) => a + b, 0); 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) { 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;
}
}