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

236 lines
6.1 KiB
JavaScript
Raw Normal View History

2018-01-16 06:09:17 +00:00
const frappe = require('frappejs');
2018-01-12 12:25:07 +00:00
2018-01-24 11:52:05 +00:00
module.exports = class BaseDocument {
2018-01-12 12:25:07 +00:00
constructor(data) {
this.handlers = {};
this.setup();
Object.assign(this, data);
}
setup() {
// add handlers
}
2018-02-08 09:38:47 +00:00
clearHandlers() {
2018-01-12 12:25:07 +00:00
this.handlers = {};
}
2018-02-08 09:38:47 +00:00
addHandler(key, method) {
2018-01-12 12:25:07 +00:00
if (!this.handlers[key]) {
this.handlers[key] = [];
}
this.handlers[key].push(method || key);
}
get(fieldname) {
return this[fieldname];
2018-01-12 12:25:07 +00:00
}
// set value and trigger change
async set(fieldname, value) {
2018-02-08 11:45:32 +00:00
this[fieldname] = await this.validateField(fieldname, value);
if (this.applyFormulae()) {
// multiple changes
await this.trigger('change', { doc: this });
} else {
// no other change, trigger control refresh
await this.trigger('change', { doc: this, fieldname: fieldname, value: value });
}
2018-01-12 12:25:07 +00:00
}
2018-02-08 11:45:32 +00:00
setName() {
2018-01-12 12:25:07 +00:00
// assign a random name by default
// override this to set a name
if (!this.name) {
this.name = frappe.getRandomName();
2018-01-12 12:25:07 +00:00
}
}
2018-02-08 11:45:32 +00:00
setKeywords() {
2018-01-12 12:25:07 +00:00
let keywords = [];
for (let fieldname of this.meta.getKeywordFields()) {
2018-01-12 12:25:07 +00:00
keywords.push(this[fieldname]);
}
this.keywords = keywords.join(', ');
}
get meta() {
if (!this._meta) {
this._meta = frappe.getMeta(this.doctype);
2018-01-12 12:25:07 +00:00
}
return this._meta;
}
append(key, document) {
if (!this[key]) {
this[key] = [];
}
2018-02-08 11:45:32 +00:00
this[key].push(this.initDoc(document));
2018-01-12 12:25:07 +00:00
}
2018-02-08 11:45:32 +00:00
initDoc(data) {
2018-01-12 12:25:07 +00:00
if (data.prototype instanceof Document) {
return data;
} else {
2018-02-01 09:29:25 +00:00
return new Document(data);
2018-01-12 12:25:07 +00:00
}
}
2018-02-08 11:45:32 +00:00
async validateField(key, value) {
let field = this.meta.getField(key);
2018-01-31 13:04:46 +00:00
if (field && field.fieldtype == 'Select') {
2018-01-31 12:56:21 +00:00
return this.meta.validate_select(field, value);
2018-01-12 12:25:07 +00:00
}
return value;
2018-01-12 12:25:07 +00:00
}
2018-02-08 11:45:32 +00:00
getValidDict() {
2018-02-01 11:07:36 +00:00
let data = {};
for (let field of this.meta.getValidFields()) {
2018-02-01 11:07:36 +00:00
data[field.fieldname] = this[field.fieldname];
2018-01-12 12:25:07 +00:00
}
2018-02-01 11:07:36 +00:00
return data;
2018-01-12 12:25:07 +00:00
}
2018-02-08 11:45:32 +00:00
setStandardValues() {
2018-01-12 12:25:07 +00:00
let now = new Date();
if (this.docstatus === null || this.docstatus === undefined) {
this.docstatus = 0;
}
if (!this.owner) {
this.owner = frappe.session.user;
this.creation = now;
}
this.modified_by = frappe.session.user;
this.modified = now;
}
async load() {
let data = await frappe.db.get(this.doctype, this.name);
if (data.name) {
2018-02-08 11:45:32 +00:00
this.syncValues(data);
} else {
throw new frappe.errors.NotFound(`Not Found: ${this.doctype} ${this.name}`);
}
2018-01-12 12:25:07 +00:00
}
2018-02-08 11:45:32 +00:00
syncValues(data) {
this.clearValues();
2018-02-01 11:07:36 +00:00
Object.assign(this, data);
}
2018-02-08 11:45:32 +00:00
clearValues() {
for (let field of this.meta.getValidFields()) {
2018-02-01 11:07:36 +00:00
if(this[field.fieldname]) {
delete this[field.fieldname];
}
}
}
2018-02-08 11:45:32 +00:00
setChildIdx() {
2018-02-07 13:23:52 +00:00
// renumber children
for (let field of this.meta.getValidFields()) {
2018-02-07 13:23:52 +00:00
if (field.fieldtype==='Table') {
for(let i=0; i < (this[field.fieldname] || []).length; i++) {
this[field.fieldname][i].idx = i;
}
}
}
}
2018-02-08 11:45:32 +00:00
applyFormulae() {
if (!this.hasFormulae()) {
return false;
}
let doc;
// children
for (let tablefield of this.meta.getTableFields()) {
let formulaFields = frappe.getMeta(tablefield.childtype).getFormulaFields();
if (formulaFields.length) {
// for each row
for (doc of this[tablefield.fieldname]) {
for (let field of formulaFields) {
doc[field.fieldname] = eval(field.formula);
}
}
}
}
// parent
doc = this;
for (let field of this.meta.getFormulaFields()) {
doc[field.fieldname] = eval(field.formula);
}
return true;
}
hasFormulae() {
if (this._hasFormulae===undefined) {
this._hasFormulae = false;
if (this.meta.getFormulaFields().length) {
this._hasFormulae = true;
} else {
for (let tablefield of this.meta.getTableFields()) {
if (frappe.getMeta(tablefield.childtype).getFormulaFields().length) {
this._hasFormulae = true;
break;
}
}
}
}
return this._hasFormulae;
}
2018-02-07 13:23:52 +00:00
async commit() {
// re-run triggers
2018-02-08 11:45:32 +00:00
this.setName();
this.setStandardValues();
this.setKeywords();
this.setChildIdx();
this.applyFormulae();
await this.trigger('validate');
2018-02-07 13:23:52 +00:00
await this.trigger('commit');
}
async insert() {
await this.commit();
await this.trigger('before_insert');
2018-02-08 11:45:32 +00:00
this.syncValues(await frappe.db.insert(this.doctype, this.getValidDict()));
await this.trigger('after_insert');
await this.trigger('after_save');
2018-02-01 11:07:36 +00:00
return this;
}
2018-02-07 13:23:52 +00:00
async update() {
await this.commit();
2018-02-01 11:07:36 +00:00
await this.trigger('before_update');
2018-02-08 11:45:32 +00:00
this.syncValues(await frappe.db.update(this.doctype, this.getValidDict()));
2018-02-01 11:07:36 +00:00
await this.trigger('after_update');
await this.trigger('after_save');
return this;
2018-01-12 12:25:07 +00:00
}
async delete() {
await this.trigger('before_delete');
await frappe.db.delete(this.doctype, this.name);
await this.trigger('after_delete');
}
async trigger(key, params) {
if (this.handlers[key]) {
for (let method of this.handlers[key]) {
if (typeof method === 'string') {
await this[method](params);
} else {
await method(params);
2018-01-12 12:25:07 +00:00
}
}
}
}
2018-01-24 11:52:05 +00:00
};