2022-01-20 20:57:29 +00:00
|
|
|
const frappe = require('frappe');
|
|
|
|
const Observable = require('frappe/utils/observable');
|
2018-03-05 16:45:21 +00:00
|
|
|
const naming = require('./naming');
|
2021-12-27 11:27:58 +00:00
|
|
|
const { isPesa } = require('../utils/index');
|
2021-12-23 11:26:47 +00:00
|
|
|
const { DEFAULT_INTERNAL_PRECISION } = require('../utils/consts');
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2018-02-13 11:54:57 +00:00
|
|
|
module.exports = class BaseDocument extends Observable {
|
2019-07-30 11:01:06 +00:00
|
|
|
constructor(data) {
|
|
|
|
super();
|
|
|
|
this.fetchValuesCache = {};
|
|
|
|
this.flags = {};
|
|
|
|
this.setup();
|
2019-10-08 11:16:54 +00:00
|
|
|
this.setValues(data);
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setup() {
|
|
|
|
// add listeners
|
|
|
|
}
|
|
|
|
|
2019-10-08 11:16:54 +00:00
|
|
|
setValues(data) {
|
|
|
|
for (let fieldname in data) {
|
|
|
|
let value = data[fieldname];
|
2019-10-19 14:35:13 +00:00
|
|
|
if (fieldname.startsWith('_')) {
|
|
|
|
// private property
|
|
|
|
this[fieldname] = value;
|
|
|
|
} else if (Array.isArray(value)) {
|
2019-10-08 11:16:54 +00:00
|
|
|
for (let row of value) {
|
2019-12-20 06:26:52 +00:00
|
|
|
this.push(fieldname, row);
|
2019-10-08 11:16:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this[fieldname] = value;
|
|
|
|
}
|
|
|
|
}
|
2019-10-19 14:35:13 +00:00
|
|
|
// set unset fields as null
|
|
|
|
for (let field of this.meta.getValidFields()) {
|
|
|
|
// check for null or undefined
|
|
|
|
if (this[field.fieldname] == null) {
|
|
|
|
this[field.fieldname] = null;
|
|
|
|
}
|
|
|
|
}
|
2019-10-08 11:16:54 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 11:01:06 +00:00
|
|
|
get meta() {
|
|
|
|
if (this.isCustom) {
|
|
|
|
this._meta = frappe.createMeta(this.fields);
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
2019-07-30 11:01:06 +00:00
|
|
|
if (!this._meta) {
|
|
|
|
this._meta = frappe.getMeta(this.doctype);
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
2019-07-30 11:01:06 +00:00
|
|
|
return this._meta;
|
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2019-07-30 11:01:06 +00:00
|
|
|
async getSettings() {
|
|
|
|
if (!this._settings) {
|
|
|
|
this._settings = await frappe.getSingle(this.meta.settings);
|
2018-02-12 12:01:31 +00:00
|
|
|
}
|
2019-07-30 11:01:06 +00:00
|
|
|
return this._settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set value and trigger change
|
|
|
|
async set(fieldname, value) {
|
|
|
|
if (typeof fieldname === 'object') {
|
|
|
|
const valueDict = fieldname;
|
|
|
|
for (let fieldname in valueDict) {
|
|
|
|
await this.set(fieldname, valueDict[fieldname]);
|
|
|
|
}
|
|
|
|
return;
|
2018-02-12 12:01:31 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 11:01:06 +00:00
|
|
|
if (this[fieldname] !== value) {
|
|
|
|
this._dirty = true;
|
2019-11-28 17:39:43 +00:00
|
|
|
// if child is dirty, parent is dirty too
|
|
|
|
if (this.meta.isChild && this.parentdoc) {
|
|
|
|
this.parentdoc._dirty = true;
|
|
|
|
}
|
|
|
|
|
2019-10-08 11:16:54 +00:00
|
|
|
if (Array.isArray(value)) {
|
|
|
|
this[fieldname] = [];
|
2020-02-03 17:55:44 +00:00
|
|
|
value.forEach((row, i) => {
|
2019-10-08 11:16:54 +00:00
|
|
|
this.append(fieldname, row);
|
2020-02-03 17:55:44 +00:00
|
|
|
row.idx = i;
|
|
|
|
});
|
2019-10-08 11:16:54 +00:00
|
|
|
} else {
|
2019-12-20 06:22:16 +00:00
|
|
|
await this.validateField(fieldname, value);
|
|
|
|
this[fieldname] = value;
|
2019-10-08 11:16:54 +00:00
|
|
|
}
|
2019-12-02 12:20:21 +00:00
|
|
|
|
|
|
|
// always run applyChange from the parentdoc
|
|
|
|
if (this.meta.isChild && this.parentdoc) {
|
2021-10-14 18:09:32 +00:00
|
|
|
await this.applyChange(fieldname);
|
2019-12-02 12:20:21 +00:00
|
|
|
await this.parentdoc.applyChange(this.parentfield);
|
|
|
|
} else {
|
2019-12-18 18:22:47 +00:00
|
|
|
await this.applyChange(fieldname);
|
|
|
|
}
|
2018-02-16 13:13:46 +00:00
|
|
|
}
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async applyChange(fieldname) {
|
2019-12-02 12:20:21 +00:00
|
|
|
await this.applyFormula(fieldname);
|
|
|
|
this.roundFloats();
|
2019-12-18 18:22:47 +00:00
|
|
|
await this.trigger('change', {
|
|
|
|
doc: this,
|
2021-11-05 11:52:59 +00:00
|
|
|
changed: fieldname,
|
2019-12-18 18:22:47 +00:00
|
|
|
});
|
|
|
|
}
|
2019-07-30 11:01:06 +00:00
|
|
|
|
|
|
|
setDefaults() {
|
|
|
|
for (let field of this.meta.fields) {
|
2019-10-19 14:36:53 +00:00
|
|
|
if (this[field.fieldname] == null) {
|
2021-12-27 11:27:58 +00:00
|
|
|
let defaultValue = getPreDefaultValues(field.fieldtype);
|
2019-07-30 11:01:06 +00:00
|
|
|
|
2021-12-27 11:27:58 +00:00
|
|
|
if (typeof field.default === 'function') {
|
|
|
|
defaultValue = field.default(this);
|
|
|
|
} else if (field.default !== undefined) {
|
|
|
|
defaultValue = field.default;
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 05:36:17 +00:00
|
|
|
if (field.fieldtype === 'Currency' && !isPesa(defaultValue)) {
|
|
|
|
defaultValue = frappe.pesa(defaultValue);
|
|
|
|
}
|
|
|
|
|
2019-07-30 11:01:06 +00:00
|
|
|
this[field.fieldname] = defaultValue;
|
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
2019-10-19 14:36:53 +00:00
|
|
|
|
|
|
|
if (this.meta.basedOn && this.meta.filters) {
|
|
|
|
this.setValues(this.meta.filters);
|
|
|
|
}
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2019-12-18 18:22:47 +00:00
|
|
|
castValues() {
|
|
|
|
for (let field of this.meta.fields) {
|
|
|
|
let value = this[field.fieldname];
|
|
|
|
if (value == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (['Int', 'Check'].includes(field.fieldtype)) {
|
|
|
|
value = parseInt(value, 10);
|
2021-12-27 11:27:58 +00:00
|
|
|
} else if (field.fieldtype === 'Float') {
|
2019-12-18 18:22:47 +00:00
|
|
|
value = parseFloat(value);
|
2021-12-27 11:27:58 +00:00
|
|
|
} else if (field.fieldtype === 'Currency' && !isPesa(value)) {
|
|
|
|
value = frappe.pesa(value);
|
2019-12-18 18:22:47 +00:00
|
|
|
}
|
|
|
|
this[field.fieldname] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 11:01:06 +00:00
|
|
|
setKeywords() {
|
|
|
|
let keywords = [];
|
|
|
|
for (let fieldname of this.meta.getKeywordFields()) {
|
|
|
|
keywords.push(this[fieldname]);
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
2019-07-30 11:01:06 +00:00
|
|
|
this.keywords = keywords.join(', ');
|
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2019-10-08 11:16:54 +00:00
|
|
|
append(key, document = {}) {
|
2019-12-20 06:26:52 +00:00
|
|
|
// push child row and trigger change
|
|
|
|
this.push(key, document);
|
|
|
|
this._dirty = true;
|
|
|
|
this.applyChange(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
push(key, document = {}) {
|
|
|
|
// push child row without triggering change
|
2019-07-30 11:01:06 +00:00
|
|
|
if (!this[key]) {
|
|
|
|
this[key] = [];
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
2019-10-08 11:16:54 +00:00
|
|
|
this[key].push(this._initChild(document, key));
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 11:16:54 +00:00
|
|
|
_initChild(data, key) {
|
|
|
|
if (data instanceof BaseDocument) {
|
2019-07-30 11:01:06 +00:00
|
|
|
return data;
|
2021-12-27 11:27:58 +00:00
|
|
|
}
|
2019-10-08 11:16:54 +00:00
|
|
|
|
2021-12-27 11:27:58 +00:00
|
|
|
data.doctype = this.meta.getField(key).childtype;
|
|
|
|
data.parent = this.name;
|
|
|
|
data.parenttype = this.doctype;
|
|
|
|
data.parentfield = key;
|
|
|
|
data.parentdoc = this;
|
2019-10-08 11:16:54 +00:00
|
|
|
|
2021-12-27 11:27:58 +00:00
|
|
|
if (!data.idx) {
|
|
|
|
data.idx = (this[key] || []).length;
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
2021-12-27 11:27:58 +00:00
|
|
|
|
|
|
|
if (!data.name) {
|
|
|
|
data.name = frappe.getRandomString();
|
|
|
|
}
|
|
|
|
|
|
|
|
const childDoc = new BaseDocument(data);
|
|
|
|
childDoc.setDefaults();
|
|
|
|
return childDoc;
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2019-12-04 18:43:08 +00:00
|
|
|
validateInsert() {
|
|
|
|
this.validateMandatory();
|
2019-12-27 10:30:49 +00:00
|
|
|
this.validateFields();
|
2019-12-04 18:43:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
validateMandatory() {
|
2019-12-24 10:10:31 +00:00
|
|
|
let checkForMandatory = [this];
|
2021-11-05 11:52:59 +00:00
|
|
|
let tableFields = this.meta.fields.filter((df) => df.fieldtype === 'Table');
|
|
|
|
tableFields.map((df) => {
|
2019-12-20 06:27:29 +00:00
|
|
|
let rows = this[df.fieldname];
|
2019-12-24 10:10:31 +00:00
|
|
|
checkForMandatory = [...checkForMandatory, ...rows];
|
2019-12-04 18:43:08 +00:00
|
|
|
});
|
2019-12-20 06:27:29 +00:00
|
|
|
|
2019-12-24 10:10:31 +00:00
|
|
|
let missingMandatory = checkForMandatory
|
2021-11-05 11:52:59 +00:00
|
|
|
.map((doc) => getMissingMandatory(doc))
|
2019-12-24 10:10:31 +00:00
|
|
|
.filter(Boolean);
|
|
|
|
|
2019-12-20 06:27:29 +00:00
|
|
|
if (missingMandatory.length > 0) {
|
|
|
|
let fields = missingMandatory.join('\n');
|
2022-01-24 06:12:01 +00:00
|
|
|
let message = frappe.t('Value missing for {0}', fields);
|
2019-12-04 18:43:08 +00:00
|
|
|
throw new frappe.errors.MandatoryError(message);
|
|
|
|
}
|
2019-12-20 06:27:29 +00:00
|
|
|
|
|
|
|
function getMissingMandatory(doc) {
|
2021-11-05 11:52:59 +00:00
|
|
|
let mandatoryFields = doc.meta.fields.filter((df) => {
|
|
|
|
if (df.required instanceof Function) {
|
2021-11-25 09:18:02 +00:00
|
|
|
return df.required(doc);
|
2021-11-05 11:52:59 +00:00
|
|
|
}
|
|
|
|
return df.required;
|
|
|
|
});
|
2019-12-20 06:27:29 +00:00
|
|
|
let message = mandatoryFields
|
2021-11-05 11:52:59 +00:00
|
|
|
.filter((df) => {
|
2019-12-20 06:27:29 +00:00
|
|
|
let value = doc[df.fieldname];
|
|
|
|
if (df.fieldtype === 'Table') {
|
|
|
|
return value == null || value.length === 0;
|
|
|
|
}
|
|
|
|
return value == null || value === '';
|
|
|
|
})
|
2021-11-05 11:52:59 +00:00
|
|
|
.map((df) => {
|
2019-12-20 06:27:29 +00:00
|
|
|
return `"${df.label}"`;
|
|
|
|
})
|
|
|
|
.join(', ');
|
|
|
|
|
2019-12-24 10:10:31 +00:00
|
|
|
if (message && doc.meta.isChild) {
|
2019-12-20 06:27:29 +00:00
|
|
|
let parentfield = doc.parentdoc.meta.getField(doc.parentfield);
|
|
|
|
message = `${parentfield.label} Row ${doc.idx + 1}: ${message}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return message;
|
|
|
|
}
|
2019-12-04 18:43:08 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 10:30:49 +00:00
|
|
|
async validateFields() {
|
2019-12-27 16:33:16 +00:00
|
|
|
let fields = this.meta.fields;
|
2019-12-27 10:30:49 +00:00
|
|
|
for (let field of fields) {
|
|
|
|
await this.validateField(field.fieldname, this.get(field.fieldname));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 11:01:06 +00:00
|
|
|
async validateField(key, value) {
|
|
|
|
let field = this.meta.getField(key);
|
2019-12-20 06:22:16 +00:00
|
|
|
if (!field) {
|
|
|
|
throw new frappe.errors.InvalidFieldError(`Invalid field ${key}`);
|
|
|
|
}
|
|
|
|
if (field.fieldtype == 'Select') {
|
|
|
|
this.meta.validateSelect(field, value);
|
|
|
|
}
|
2020-01-01 08:09:31 +00:00
|
|
|
if (field.validate && value != null) {
|
2019-12-27 10:25:23 +00:00
|
|
|
let validator = null;
|
|
|
|
if (typeof field.validate === 'object') {
|
|
|
|
validator = this.getValidateFunction(field.validate);
|
|
|
|
}
|
|
|
|
if (typeof field.validate === 'function') {
|
|
|
|
validator = field.validate;
|
|
|
|
}
|
|
|
|
if (validator) {
|
|
|
|
await validator(value, this);
|
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
|
2019-12-27 10:25:23 +00:00
|
|
|
getValidateFunction(validator) {
|
|
|
|
let functions = {
|
|
|
|
email(value) {
|
|
|
|
let isValid = /(.+)@(.+){2,}\.(.+){2,}/.test(value);
|
|
|
|
if (!isValid) {
|
|
|
|
throw new frappe.errors.ValidationError(`Invalid email: ${value}`);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
phone(value) {
|
|
|
|
let isValid = /[+]{0,1}[\d ]+/.test(value);
|
|
|
|
if (!isValid) {
|
|
|
|
throw new frappe.errors.ValidationError(`Invalid phone: ${value}`);
|
|
|
|
}
|
2021-11-05 11:52:59 +00:00
|
|
|
},
|
2019-12-27 10:25:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return functions[validator.type];
|
|
|
|
}
|
|
|
|
|
2019-07-30 11:01:06 +00:00
|
|
|
getValidDict() {
|
|
|
|
let data = {};
|
|
|
|
for (let field of this.meta.getValidFields()) {
|
2019-11-15 07:45:16 +00:00
|
|
|
let value = this[field.fieldname];
|
|
|
|
if (Array.isArray(value)) {
|
2021-11-05 11:52:59 +00:00
|
|
|
value = value.map((doc) =>
|
|
|
|
doc.getValidDict ? doc.getValidDict() : doc
|
|
|
|
);
|
2019-11-15 07:45:16 +00:00
|
|
|
}
|
|
|
|
data[field.fieldname] = value;
|
2018-03-05 16:45:21 +00:00
|
|
|
}
|
2019-07-30 11:01:06 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
setStandardValues() {
|
|
|
|
// set standard values on server-side only
|
|
|
|
if (frappe.isServer) {
|
2019-12-20 06:24:48 +00:00
|
|
|
if (this.isSubmittable && this.submitted == null) {
|
2019-07-30 11:01:06 +00:00
|
|
|
this.submitted = 0;
|
|
|
|
}
|
2018-03-05 16:45:21 +00:00
|
|
|
|
2019-12-20 06:24:48 +00:00
|
|
|
let now = new Date().toISOString();
|
2019-07-30 11:01:06 +00:00
|
|
|
if (!this.owner) {
|
|
|
|
this.owner = frappe.session.user;
|
|
|
|
}
|
2018-05-07 04:23:20 +00:00
|
|
|
|
2019-07-30 11:01:06 +00:00
|
|
|
if (!this.creation) {
|
|
|
|
this.creation = now;
|
|
|
|
}
|
2018-05-07 04:23:20 +00:00
|
|
|
|
2019-12-20 06:24:48 +00:00
|
|
|
this.updateModified();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateModified() {
|
|
|
|
if (frappe.isServer) {
|
|
|
|
let now = new Date().toISOString();
|
2019-12-20 06:27:29 +00:00
|
|
|
this.modifiedBy = frappe.session.user;
|
2019-07-30 11:01:06 +00:00
|
|
|
this.modified = now;
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
|
|
|
let data = await frappe.db.get(this.doctype, this.name);
|
2019-12-09 19:57:26 +00:00
|
|
|
if (data && data.name) {
|
2019-07-30 11:01:06 +00:00
|
|
|
this.syncValues(data);
|
|
|
|
if (this.meta.isSingle) {
|
|
|
|
this.setDefaults();
|
2019-12-18 18:22:47 +00:00
|
|
|
this.castValues();
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
2019-11-15 07:45:16 +00:00
|
|
|
await this.loadLinks();
|
2019-07-30 11:01:06 +00:00
|
|
|
} else {
|
2019-11-15 07:45:16 +00:00
|
|
|
throw new frappe.errors.NotFoundError(
|
2019-07-30 11:01:06 +00:00
|
|
|
`Not Found: ${this.doctype} ${this.name}`
|
|
|
|
);
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 07:45:16 +00:00
|
|
|
async loadLinks() {
|
|
|
|
this._links = {};
|
2021-11-05 11:52:59 +00:00
|
|
|
let inlineLinks = this.meta.fields.filter((df) => df.inline);
|
2019-11-15 07:45:16 +00:00
|
|
|
for (let df of inlineLinks) {
|
2019-11-28 17:39:20 +00:00
|
|
|
await this.loadLink(df.fieldname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadLink(fieldname) {
|
|
|
|
this._links = this._links || {};
|
|
|
|
let df = this.meta.getField(fieldname);
|
2019-12-18 18:22:47 +00:00
|
|
|
if (this[df.fieldname]) {
|
|
|
|
this._links[df.fieldname] = await frappe.getDoc(
|
|
|
|
df.target,
|
|
|
|
this[df.fieldname]
|
|
|
|
);
|
2019-11-15 07:45:16 +00:00
|
|
|
}
|
2019-12-18 18:22:47 +00:00
|
|
|
}
|
2019-11-15 07:45:16 +00:00
|
|
|
|
|
|
|
getLink(fieldname) {
|
|
|
|
return this._links ? this._links[fieldname] : null;
|
|
|
|
}
|
|
|
|
|
2019-07-30 11:01:06 +00:00
|
|
|
syncValues(data) {
|
|
|
|
this.clearValues();
|
2019-10-19 14:35:13 +00:00
|
|
|
this.setValues(data);
|
2019-07-30 11:01:06 +00:00
|
|
|
this._dirty = false;
|
|
|
|
this.trigger('change', {
|
2021-11-05 11:52:59 +00:00
|
|
|
doc: this,
|
2019-07-30 11:01:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
clearValues() {
|
2019-10-29 20:54:05 +00:00
|
|
|
let toClear = ['_dirty', '_notInserted'].concat(
|
2021-11-05 11:52:59 +00:00
|
|
|
this.meta.getValidFields().map((df) => df.fieldname)
|
2019-10-29 20:54:05 +00:00
|
|
|
);
|
|
|
|
for (let key of toClear) {
|
2019-11-15 07:45:16 +00:00
|
|
|
this[key] = null;
|
2018-02-01 11:07:36 +00:00
|
|
|
}
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setChildIdx() {
|
|
|
|
// renumber children
|
|
|
|
for (let field of this.meta.getValidFields()) {
|
|
|
|
if (field.fieldtype === 'Table') {
|
|
|
|
for (let i = 0; i < (this[field.fieldname] || []).length; i++) {
|
|
|
|
this[field.fieldname][i].idx = i;
|
2018-02-01 11:07:36 +00:00
|
|
|
}
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
2018-02-01 11:07:36 +00:00
|
|
|
}
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async compareWithCurrentDoc() {
|
|
|
|
if (frappe.isServer && !this.isNew()) {
|
|
|
|
let currentDoc = await frappe.db.get(this.doctype, this.name);
|
|
|
|
|
|
|
|
// check for conflict
|
|
|
|
if (currentDoc && this.modified != currentDoc.modified) {
|
|
|
|
throw new frappe.errors.Conflict(
|
2022-01-24 06:12:01 +00:00
|
|
|
frappe.t('Document {0} {1} has been modified after loading', [
|
2019-07-30 11:01:06 +00:00
|
|
|
this.doctype,
|
2021-11-05 11:52:59 +00:00
|
|
|
this.name,
|
2019-07-30 11:01:06 +00:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.submitted && !this.meta.isSubmittable) {
|
|
|
|
throw new frappe.errors.ValidationError(
|
2022-01-24 06:12:01 +00:00
|
|
|
frappe.t('Document type {1} is not submittable', [this.doctype])
|
2019-07-30 11:01:06 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// set submit action flag
|
2021-11-05 11:52:59 +00:00
|
|
|
this.flags = {};
|
2019-07-30 11:01:06 +00:00
|
|
|
if (this.submitted && !currentDoc.submitted) {
|
|
|
|
this.flags.submitAction = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentDoc.submitted && !this.submitted) {
|
|
|
|
this.flags.revertAction = true;
|
|
|
|
}
|
2018-02-07 13:23:52 +00:00
|
|
|
}
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
2018-02-07 13:23:52 +00:00
|
|
|
|
2019-12-02 12:20:21 +00:00
|
|
|
async applyFormula(fieldname) {
|
2019-07-30 11:01:06 +00:00
|
|
|
if (!this.meta.hasFormula()) {
|
|
|
|
return false;
|
2018-02-27 16:29:14 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 11:01:06 +00:00
|
|
|
let doc = this;
|
2019-12-02 12:20:21 +00:00
|
|
|
let changed = false;
|
2019-07-30 11:01:06 +00:00
|
|
|
|
|
|
|
// children
|
|
|
|
for (let tablefield of this.meta.getTableFields()) {
|
|
|
|
let formulaFields = frappe
|
|
|
|
.getMeta(tablefield.childtype)
|
|
|
|
.getFormulaFields();
|
|
|
|
if (formulaFields.length) {
|
2021-10-27 08:36:50 +00:00
|
|
|
const value = this[tablefield.fieldname] || [];
|
2021-10-20 06:58:09 +00:00
|
|
|
for (let row of value) {
|
2019-07-30 11:01:06 +00:00
|
|
|
for (let field of formulaFields) {
|
|
|
|
if (shouldApplyFormula(field, row)) {
|
2019-12-02 12:20:21 +00:00
|
|
|
let val = await this.getValueFromFormula(field, row);
|
|
|
|
let previousVal = row[field.fieldname];
|
|
|
|
if (val !== undefined && previousVal !== val) {
|
2019-07-30 11:01:06 +00:00
|
|
|
row[field.fieldname] = val;
|
2019-12-02 12:20:21 +00:00
|
|
|
changed = true;
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
2018-02-08 11:45:32 +00:00
|
|
|
}
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
2018-02-08 11:45:32 +00:00
|
|
|
}
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
2018-02-07 13:23:52 +00:00
|
|
|
}
|
|
|
|
|
2019-10-19 14:36:29 +00:00
|
|
|
// parent or child row
|
2019-07-30 11:01:06 +00:00
|
|
|
for (let field of this.meta.getFormulaFields()) {
|
|
|
|
if (shouldApplyFormula(field, doc)) {
|
2019-12-02 12:20:21 +00:00
|
|
|
let previousVal = doc[field.fieldname];
|
|
|
|
let val = await this.getValueFromFormula(field, doc);
|
|
|
|
if (val !== undefined && previousVal !== val) {
|
2019-07-30 11:01:06 +00:00
|
|
|
doc[field.fieldname] = val;
|
2019-12-02 12:20:21 +00:00
|
|
|
changed = true;
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 12:20:21 +00:00
|
|
|
return changed;
|
2018-03-05 16:45:21 +00:00
|
|
|
|
2019-07-30 11:01:06 +00:00
|
|
|
function shouldApplyFormula(field, doc) {
|
|
|
|
if (field.readOnly) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-02 12:20:21 +00:00
|
|
|
if (
|
|
|
|
fieldname &&
|
|
|
|
field.formulaDependsOn &&
|
|
|
|
field.formulaDependsOn.includes(fieldname)
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-03-05 16:45:21 +00:00
|
|
|
|
2019-07-30 11:01:06 +00:00
|
|
|
if (!frappe.isServer || frappe.isElectron) {
|
|
|
|
if (doc[field.fieldname] == null || doc[field.fieldname] == '') {
|
|
|
|
return true;
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
|
|
|
return false;
|
2018-01-12 12:25:07 +00:00
|
|
|
}
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 12:20:21 +00:00
|
|
|
async getValueFromFormula(field, doc) {
|
|
|
|
let value;
|
|
|
|
|
|
|
|
if (doc.meta.isChild) {
|
|
|
|
value = await field.formula(doc, doc.parentdoc);
|
|
|
|
} else {
|
|
|
|
value = await field.formula(doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-27 11:27:58 +00:00
|
|
|
if ('Float' === field.fieldtype) {
|
2019-12-26 13:46:02 +00:00
|
|
|
value = this.round(value, field);
|
2019-12-02 12:20:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (field.fieldtype === 'Table' && Array.isArray(value)) {
|
2021-11-05 11:52:59 +00:00
|
|
|
value = value.map((row) => {
|
2019-12-02 12:20:21 +00:00
|
|
|
let doc = this._initChild(row, field.fieldname);
|
|
|
|
doc.roundFloats();
|
|
|
|
return doc;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
roundFloats() {
|
|
|
|
let fields = this.meta
|
|
|
|
.getValidFields()
|
2021-12-27 11:27:58 +00:00
|
|
|
.filter((df) => ['Float', 'Table'].includes(df.fieldtype));
|
2019-12-02 12:20:21 +00:00
|
|
|
|
|
|
|
for (let df of fields) {
|
|
|
|
let value = this[df.fieldname];
|
|
|
|
if (value == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// child
|
|
|
|
if (Array.isArray(value)) {
|
2021-11-05 11:52:59 +00:00
|
|
|
value.map((row) => row.roundFloats());
|
2019-12-02 12:20:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// field
|
2019-12-26 13:46:02 +00:00
|
|
|
let roundedValue = this.round(value, df);
|
2019-12-02 12:20:21 +00:00
|
|
|
if (roundedValue && value !== roundedValue) {
|
|
|
|
this[df.fieldname] = roundedValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 19:38:43 +00:00
|
|
|
async setName() {
|
|
|
|
await naming.setName(this);
|
|
|
|
}
|
|
|
|
|
2019-07-30 11:01:06 +00:00
|
|
|
async commit() {
|
|
|
|
// re-run triggers
|
|
|
|
this.setKeywords();
|
|
|
|
this.setChildIdx();
|
|
|
|
await this.applyFormula();
|
|
|
|
await this.trigger('validate');
|
|
|
|
}
|
|
|
|
|
|
|
|
async insert() {
|
2019-10-29 19:38:43 +00:00
|
|
|
await this.setName();
|
2019-12-20 06:24:48 +00:00
|
|
|
this.setStandardValues();
|
2019-07-30 11:01:06 +00:00
|
|
|
await this.commit();
|
2019-12-04 18:43:08 +00:00
|
|
|
await this.validateInsert();
|
2019-07-30 11:01:06 +00:00
|
|
|
await this.trigger('beforeInsert');
|
|
|
|
|
2019-10-19 14:29:41 +00:00
|
|
|
let oldName = this.name;
|
2019-07-30 11:01:06 +00:00
|
|
|
const data = await frappe.db.insert(this.doctype, this.getValidDict());
|
|
|
|
this.syncValues(data);
|
|
|
|
|
2019-10-19 14:29:41 +00:00
|
|
|
if (oldName !== this.name) {
|
|
|
|
frappe.removeFromCache(this.doctype, oldName);
|
|
|
|
}
|
|
|
|
|
2019-07-30 11:01:06 +00:00
|
|
|
await this.trigger('afterInsert');
|
|
|
|
await this.trigger('afterSave');
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-12-27 06:38:06 +00:00
|
|
|
async update(...args) {
|
|
|
|
if (args.length) {
|
|
|
|
await this.set(...args);
|
|
|
|
}
|
2019-07-30 11:01:06 +00:00
|
|
|
await this.compareWithCurrentDoc();
|
|
|
|
await this.commit();
|
|
|
|
await this.trigger('beforeUpdate');
|
|
|
|
|
|
|
|
// before submit
|
|
|
|
if (this.flags.submitAction) await this.trigger('beforeSubmit');
|
|
|
|
if (this.flags.revertAction) await this.trigger('beforeRevert');
|
|
|
|
|
2019-12-20 06:24:48 +00:00
|
|
|
// update modifiedBy and modified
|
|
|
|
this.updateModified();
|
|
|
|
|
2019-07-30 11:01:06 +00:00
|
|
|
const data = await frappe.db.update(this.doctype, this.getValidDict());
|
|
|
|
this.syncValues(data);
|
|
|
|
|
|
|
|
await this.trigger('afterUpdate');
|
|
|
|
await this.trigger('afterSave');
|
|
|
|
|
|
|
|
// after submit
|
|
|
|
if (this.flags.submitAction) await this.trigger('afterSubmit');
|
|
|
|
if (this.flags.revertAction) await this.trigger('afterRevert');
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-08 14:05:37 +00:00
|
|
|
async insertOrUpdate() {
|
2019-11-15 07:45:16 +00:00
|
|
|
if (this._notInserted) {
|
2021-11-08 14:05:37 +00:00
|
|
|
return await this.insert();
|
2019-11-15 07:45:16 +00:00
|
|
|
} else {
|
2021-11-08 14:05:37 +00:00
|
|
|
return await this.update();
|
2019-11-15 07:45:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 11:01:06 +00:00
|
|
|
async delete() {
|
|
|
|
await this.trigger('beforeDelete');
|
|
|
|
await frappe.db.delete(this.doctype, this.name);
|
|
|
|
await this.trigger('afterDelete');
|
|
|
|
}
|
|
|
|
|
2021-11-08 14:05:37 +00:00
|
|
|
async submitOrRevert(isSubmit) {
|
|
|
|
const wasSubmitted = this.submitted;
|
|
|
|
this.submitted = isSubmit;
|
|
|
|
try {
|
|
|
|
await this.update();
|
|
|
|
} catch (e) {
|
|
|
|
this.submitted = wasSubmitted;
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 11:01:06 +00:00
|
|
|
async submit() {
|
2021-11-15 12:30:34 +00:00
|
|
|
this.cancelled = 0;
|
2021-11-08 14:05:37 +00:00
|
|
|
await this.submitOrRevert(1);
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async revert() {
|
2021-11-08 14:05:37 +00:00
|
|
|
await this.submitOrRevert(0);
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
|
|
|
|
2019-10-05 21:46:14 +00:00
|
|
|
async rename(newName) {
|
|
|
|
await this.trigger('beforeRename');
|
|
|
|
await frappe.db.rename(this.doctype, this.name, newName);
|
|
|
|
this.name = newName;
|
|
|
|
await this.trigger('afterRename');
|
|
|
|
}
|
|
|
|
|
2019-07-30 11:01:06 +00:00
|
|
|
// trigger methods on the class if they match
|
|
|
|
// with the trigger name
|
|
|
|
async trigger(event, params) {
|
|
|
|
if (this[event]) {
|
|
|
|
await this[event](params);
|
2018-02-09 12:55:55 +00:00
|
|
|
}
|
2019-07-30 11:01:06 +00:00
|
|
|
await super.trigger(event, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper functions
|
2021-12-27 11:27:58 +00:00
|
|
|
getSum(tablefield, childfield, convertToFloat = true) {
|
|
|
|
const sum = (this[tablefield] || [])
|
|
|
|
.map((d) => {
|
|
|
|
const value = d[childfield] ?? 0;
|
|
|
|
if (!isPesa(value)) {
|
2021-12-29 06:13:39 +00:00
|
|
|
try {
|
|
|
|
return frappe.pesa(value);
|
|
|
|
} catch (err) {
|
|
|
|
err.message += ` value: '${value}' of type: ${typeof value}, fieldname: '${tablefield}', childfield: '${childfield}'`;
|
|
|
|
throw err;
|
|
|
|
}
|
2021-12-27 11:27:58 +00:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
})
|
|
|
|
.reduce((a, b) => a.add(b), frappe.pesa(0));
|
|
|
|
|
|
|
|
if (convertToFloat) {
|
|
|
|
return sum.float;
|
|
|
|
}
|
|
|
|
return sum;
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 18:21:45 +00:00
|
|
|
getFrom(doctype, name, fieldname) {
|
2019-07-30 11:01:06 +00:00
|
|
|
if (!name) return '';
|
2019-12-18 18:21:45 +00:00
|
|
|
return frappe.db.getCachedValue(doctype, name, fieldname);
|
2019-07-30 11:01:06 +00:00
|
|
|
}
|
2018-11-09 19:07:14 +00:00
|
|
|
|
2019-12-26 13:46:02 +00:00
|
|
|
round(value, df = null) {
|
|
|
|
if (typeof df === 'string') {
|
|
|
|
df = this.meta.getField(df);
|
|
|
|
}
|
2021-12-23 11:26:47 +00:00
|
|
|
const precision =
|
|
|
|
frappe.SystemSettings.internalPrecision ?? DEFAULT_INTERNAL_PRECISION;
|
2021-12-31 08:10:17 +00:00
|
|
|
return frappe.pesa(value).clip(precision).float;
|
2019-12-26 13:46:02 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 11:01:06 +00:00
|
|
|
isNew() {
|
|
|
|
return this._notInserted;
|
|
|
|
}
|
2021-11-05 11:52:59 +00:00
|
|
|
|
2021-11-25 09:18:02 +00:00
|
|
|
getFieldMetaMap() {
|
|
|
|
return this.meta.fields.reduce((obj, meta) => {
|
|
|
|
obj[meta.fieldname] = meta;
|
|
|
|
return obj;
|
|
|
|
}, {});
|
|
|
|
}
|
2019-07-30 11:01:06 +00:00
|
|
|
};
|
2021-12-27 11:27:58 +00:00
|
|
|
|
|
|
|
function getPreDefaultValues(fieldtype) {
|
|
|
|
switch (fieldtype) {
|
|
|
|
case 'Table':
|
|
|
|
return [];
|
|
|
|
case 'Currency':
|
|
|
|
return frappe.pesa(0.0);
|
|
|
|
case 'Int':
|
|
|
|
case 'Float':
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|