mirror of
https://github.com/frappe/books.git
synced 2025-02-04 13:08:29 +00:00
feat: add conditional required
, fix formatting
This commit is contained in:
parent
a6890b43ac
commit
b515e2f732
@ -99,7 +99,7 @@ module.exports = class BaseDocument extends Observable {
|
|||||||
this.roundFloats();
|
this.roundFloats();
|
||||||
await this.trigger('change', {
|
await this.trigger('change', {
|
||||||
doc: this,
|
doc: this,
|
||||||
changed: fieldname
|
changed: fieldname,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -194,15 +194,16 @@ module.exports = class BaseDocument extends Observable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
validateMandatory() {
|
validateMandatory() {
|
||||||
|
const fieldValueMap = this.getFieldValueMap();
|
||||||
let checkForMandatory = [this];
|
let checkForMandatory = [this];
|
||||||
let tableFields = this.meta.fields.filter(df => df.fieldtype === 'Table');
|
let tableFields = this.meta.fields.filter((df) => df.fieldtype === 'Table');
|
||||||
tableFields.map(df => {
|
tableFields.map((df) => {
|
||||||
let rows = this[df.fieldname];
|
let rows = this[df.fieldname];
|
||||||
checkForMandatory = [...checkForMandatory, ...rows];
|
checkForMandatory = [...checkForMandatory, ...rows];
|
||||||
});
|
});
|
||||||
|
|
||||||
let missingMandatory = checkForMandatory
|
let missingMandatory = checkForMandatory
|
||||||
.map(doc => getMissingMandatory(doc))
|
.map((doc) => getMissingMandatory(doc))
|
||||||
.filter(Boolean);
|
.filter(Boolean);
|
||||||
|
|
||||||
if (missingMandatory.length > 0) {
|
if (missingMandatory.length > 0) {
|
||||||
@ -212,16 +213,21 @@ module.exports = class BaseDocument extends Observable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getMissingMandatory(doc) {
|
function getMissingMandatory(doc) {
|
||||||
let mandatoryFields = doc.meta.fields.filter(df => df.required);
|
let mandatoryFields = doc.meta.fields.filter((df) => {
|
||||||
|
if (df.required instanceof Function) {
|
||||||
|
return df.required(fieldValueMap);
|
||||||
|
}
|
||||||
|
return df.required;
|
||||||
|
});
|
||||||
let message = mandatoryFields
|
let message = mandatoryFields
|
||||||
.filter(df => {
|
.filter((df) => {
|
||||||
let value = doc[df.fieldname];
|
let value = doc[df.fieldname];
|
||||||
if (df.fieldtype === 'Table') {
|
if (df.fieldtype === 'Table') {
|
||||||
return value == null || value.length === 0;
|
return value == null || value.length === 0;
|
||||||
}
|
}
|
||||||
return value == null || value === '';
|
return value == null || value === '';
|
||||||
})
|
})
|
||||||
.map(df => {
|
.map((df) => {
|
||||||
return `"${df.label}"`;
|
return `"${df.label}"`;
|
||||||
})
|
})
|
||||||
.join(', ');
|
.join(', ');
|
||||||
@ -277,7 +283,7 @@ module.exports = class BaseDocument extends Observable {
|
|||||||
if (!isValid) {
|
if (!isValid) {
|
||||||
throw new frappe.errors.ValidationError(`Invalid phone: ${value}`);
|
throw new frappe.errors.ValidationError(`Invalid phone: ${value}`);
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
return functions[validator.type];
|
return functions[validator.type];
|
||||||
@ -288,7 +294,9 @@ module.exports = class BaseDocument extends Observable {
|
|||||||
for (let field of this.meta.getValidFields()) {
|
for (let field of this.meta.getValidFields()) {
|
||||||
let value = this[field.fieldname];
|
let value = this[field.fieldname];
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
value = value.map(doc => (doc.getValidDict ? doc.getValidDict() : doc));
|
value = value.map((doc) =>
|
||||||
|
doc.getValidDict ? doc.getValidDict() : doc
|
||||||
|
);
|
||||||
}
|
}
|
||||||
data[field.fieldname] = value;
|
data[field.fieldname] = value;
|
||||||
}
|
}
|
||||||
@ -341,7 +349,7 @@ module.exports = class BaseDocument extends Observable {
|
|||||||
|
|
||||||
async loadLinks() {
|
async loadLinks() {
|
||||||
this._links = {};
|
this._links = {};
|
||||||
let inlineLinks = this.meta.fields.filter(df => df.inline);
|
let inlineLinks = this.meta.fields.filter((df) => df.inline);
|
||||||
for (let df of inlineLinks) {
|
for (let df of inlineLinks) {
|
||||||
await this.loadLink(df.fieldname);
|
await this.loadLink(df.fieldname);
|
||||||
}
|
}
|
||||||
@ -367,13 +375,13 @@ module.exports = class BaseDocument extends Observable {
|
|||||||
this.setValues(data);
|
this.setValues(data);
|
||||||
this._dirty = false;
|
this._dirty = false;
|
||||||
this.trigger('change', {
|
this.trigger('change', {
|
||||||
doc: this
|
doc: this,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
clearValues() {
|
clearValues() {
|
||||||
let toClear = ['_dirty', '_notInserted'].concat(
|
let toClear = ['_dirty', '_notInserted'].concat(
|
||||||
this.meta.getValidFields().map(df => df.fieldname)
|
this.meta.getValidFields().map((df) => df.fieldname)
|
||||||
);
|
);
|
||||||
for (let key of toClear) {
|
for (let key of toClear) {
|
||||||
this[key] = null;
|
this[key] = null;
|
||||||
@ -400,7 +408,7 @@ module.exports = class BaseDocument extends Observable {
|
|||||||
throw new frappe.errors.Conflict(
|
throw new frappe.errors.Conflict(
|
||||||
frappe._('Document {0} {1} has been modified after loading', [
|
frappe._('Document {0} {1} has been modified after loading', [
|
||||||
this.doctype,
|
this.doctype,
|
||||||
this.name
|
this.name,
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -412,7 +420,7 @@ module.exports = class BaseDocument extends Observable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// set submit action flag
|
// set submit action flag
|
||||||
this.flags = {}
|
this.flags = {};
|
||||||
if (this.submitted && !currentDoc.submitted) {
|
if (this.submitted && !currentDoc.submitted) {
|
||||||
this.flags.submitAction = true;
|
this.flags.submitAction = true;
|
||||||
}
|
}
|
||||||
@ -506,7 +514,7 @@ module.exports = class BaseDocument extends Observable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (field.fieldtype === 'Table' && Array.isArray(value)) {
|
if (field.fieldtype === 'Table' && Array.isArray(value)) {
|
||||||
value = value.map(row => {
|
value = value.map((row) => {
|
||||||
let doc = this._initChild(row, field.fieldname);
|
let doc = this._initChild(row, field.fieldname);
|
||||||
doc.roundFloats();
|
doc.roundFloats();
|
||||||
return doc;
|
return doc;
|
||||||
@ -519,7 +527,7 @@ module.exports = class BaseDocument extends Observable {
|
|||||||
roundFloats() {
|
roundFloats() {
|
||||||
let fields = this.meta
|
let fields = this.meta
|
||||||
.getValidFields()
|
.getValidFields()
|
||||||
.filter(df => ['Float', 'Currency', 'Table'].includes(df.fieldtype));
|
.filter((df) => ['Float', 'Currency', 'Table'].includes(df.fieldtype));
|
||||||
|
|
||||||
for (let df of fields) {
|
for (let df of fields) {
|
||||||
let value = this[df.fieldname];
|
let value = this[df.fieldname];
|
||||||
@ -528,7 +536,7 @@ module.exports = class BaseDocument extends Observable {
|
|||||||
}
|
}
|
||||||
// child
|
// child
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
value.map(row => row.roundFloats());
|
value.map((row) => row.roundFloats());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// field
|
// field
|
||||||
@ -643,7 +651,7 @@ module.exports = class BaseDocument extends Observable {
|
|||||||
// helper functions
|
// helper functions
|
||||||
getSum(tablefield, childfield) {
|
getSum(tablefield, childfield) {
|
||||||
return (this[tablefield] || [])
|
return (this[tablefield] || [])
|
||||||
.map(d => parseFloat(d[childfield], 10) || 0)
|
.map((d) => parseFloat(d[childfield], 10) || 0)
|
||||||
.reduce((a, b) => a + b, 0);
|
.reduce((a, b) => a + b, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -666,4 +674,22 @@ module.exports = class BaseDocument extends Observable {
|
|||||||
isNew() {
|
isNew() {
|
||||||
return this._notInserted;
|
return this._notInserted;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getFieldValueMap() {
|
||||||
|
const fieldValueMap = this.meta.fields
|
||||||
|
.map(({ fieldname, fieldtype }) => [fieldname, fieldtype])
|
||||||
|
.reduce((obj, [fieldname, fieldtype]) => {
|
||||||
|
let value = this[fieldname];
|
||||||
|
|
||||||
|
if (fieldtype == 'Table') {
|
||||||
|
value = value.map((childTableDoc) =>
|
||||||
|
childTableDoc.getFieldValueMap()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
obj[fieldname] = value;
|
||||||
|
return obj;
|
||||||
|
}, {});
|
||||||
|
return Object.freeze(fieldValueMap);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user