mirror of
https://github.com/frappe/books.git
synced 2024-12-24 11:55:46 +00:00
fix: update calcs
- set child doc defaults - remove Currency from rounding
This commit is contained in:
parent
4b0cf18b38
commit
e214891156
@ -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,18 +108,13 @@ 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 {
|
||||
} 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,7 +168,8 @@ 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;
|
||||
@ -185,8 +184,9 @@ module.exports = class BaseDocument extends Observable {
|
||||
data.name = frappe.getRandomString();
|
||||
}
|
||||
|
||||
return new BaseDocument(data);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user