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

150 lines
3.8 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
}
clear_handlers() {
this.handlers = {};
}
add_handler(key, method) {
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) {
this[fieldname] = await this.validate_field(fieldname, value);
2018-01-31 13:04:46 +00:00
await this.trigger('change', { doc: this, fieldname: fieldname, value: value });
2018-01-12 12:25:07 +00:00
}
set_name() {
// assign a random name by default
// override this to set a name
if (!this.name) {
this.name = Math.random().toString(36).substr(3);
}
}
set_keywords() {
let keywords = [];
for (let fieldname of this.meta.get_keyword_fields()) {
keywords.push(this[fieldname]);
}
this.keywords = keywords.join(', ');
}
get meta() {
if (!this._meta) {
this._meta = frappe.get_meta(this.doctype);
}
return this._meta;
}
append(key, document) {
if (!this[key]) {
this[key] = [];
}
this[key].push(this.init_doc(document));
}
init_doc(data) {
if (data.prototype instanceof Document) {
return data;
} else {
return new Document(d);
}
}
2018-01-31 13:04:46 +00:00
async validate_field(key, value) {
2018-01-31 12:56:21 +00:00
let field = this.meta.get_field(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
}
get_valid_dict() {
let doc = {};
2018-01-31 13:04:46 +00:00
for (let field of this.meta.get_valid_fields()) {
2018-01-31 12:56:21 +00:00
doc[field.fieldname] = this.get(field.fieldname);
2018-01-12 12:25:07 +00:00
}
return doc;
}
set_standard_values() {
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) {
Object.assign(this, data);
} else {
throw new frappe.errors.NotFound(`Not Found: ${this.doctype} ${this.name}`);
}
2018-01-12 12:25:07 +00:00
}
async insert() {
this.set_name();
this.set_standard_values();
this.set_keywords();
await this.trigger('validate');
await this.trigger('before_insert');
2018-01-12 12:25:07 +00:00
await frappe.db.insert(this.doctype, this.get_valid_dict());
await this.trigger('after_insert');
await this.trigger('after_save');
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
}
}
}
}
async update() {
this.set_standard_values();
this.set_keywords();
await this.trigger('validate');
await this.trigger('before_update');
2018-01-12 12:25:07 +00:00
await frappe.db.update(this.doctype, this.get_valid_dict());
await this.trigger('after_update');
await this.trigger('after_save');
2018-01-12 12:25:07 +00:00
return this;
}
2018-01-24 11:52:05 +00:00
};