2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 15:50:56 +00:00

Formula enhancements

- apply formula on server side only if the field is read only
- skip applying formula if the value is already set
This commit is contained in:
Faris Ansari 2018-09-23 13:45:53 +05:30
parent b96ea40b99
commit fb4295a12b

View File

@ -227,24 +227,41 @@ module.exports = class BaseDocument extends Observable {
// for each row
for (let row of this[tablefield.fieldname]) {
for (let field of formulaFields) {
if (shouldApplyFormula(field, row)) {
const val = await field.formula(row, doc);
if (val !== false) {
if (val !== false && val !== undefined) {
row[field.fieldname] = val;
}
}
}
}
}
}
// parent
for (let field of this.meta.getFormulaFields()) {
if (shouldApplyFormula(field, doc)) {
const val = await field.formula(doc);
if (val !== false) {
if (val !== false && val !== undefined) {
doc[field.fieldname] = val;
}
}
}
return true;
function shouldApplyFormula (field, doc) {
if (frappe.isServer) {
if (field.readOnly) {
return true;
}
} else {
if (doc[field.fieldname] == null) {
return true;
}
}
return false;
}
}
async commit() {