2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 11:29:00 +00:00

fix: Run formula for child docs passing parentdoc

This commit is contained in:
Faris Ansari 2019-10-19 20:06:29 +05:30
parent 133c8880d4
commit ab57eef2eb

View File

@ -135,15 +135,19 @@ module.exports = class BaseDocument extends Observable {
this[key] = [];
}
this[key].push(this._initChild(document, key));
this._dirty = true;
this.applyChange(key);
}
_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;
@ -301,10 +305,15 @@ module.exports = class BaseDocument extends Observable {
}
}
// parent
// parent or child row
for (let field of this.meta.getFormulaFields()) {
if (shouldApplyFormula(field, doc)) {
const val = await field.formula(doc);
let val;
if (this.meta.isChild) {
val = await field.formula(doc, this.parentdoc);
} else {
val = await field.formula(doc);
}
if (val !== false && val !== undefined) {
doc[field.fieldname] = val;
}