mirror of
https://github.com/frappe/books.git
synced 2024-11-10 15:50:56 +00:00
fix: Singles
- Initialize defaults for SIngles - Cast values when loading a Single document
This commit is contained in:
parent
fef4f96e78
commit
d19182b583
@ -36,6 +36,38 @@ module.exports = class Database extends Observable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
await this.commit();
|
await this.commit();
|
||||||
|
await this.initializeSingles();
|
||||||
|
}
|
||||||
|
|
||||||
|
async initializeSingles() {
|
||||||
|
let singleDoctypes = frappe
|
||||||
|
.getModels(model => model.isSingle)
|
||||||
|
.map(model => model.name);
|
||||||
|
|
||||||
|
for (let doctype of singleDoctypes) {
|
||||||
|
if (await this.singleExists(doctype)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let meta = frappe.getMeta(doctype);
|
||||||
|
if (meta.fields.every(df => df.default == null)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let defaultValues = meta.fields.reduce((doc, df) => {
|
||||||
|
if (df.default != null) {
|
||||||
|
doc[df.fieldname] = df.default;
|
||||||
|
}
|
||||||
|
return doc;
|
||||||
|
}, {});
|
||||||
|
await this.updateSingle(doctype, defaultValues);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async singleExists(doctype) {
|
||||||
|
let res = await this.knex('SingleValue')
|
||||||
|
.count('parent as count')
|
||||||
|
.where('parent', doctype)
|
||||||
|
.first();
|
||||||
|
return res.count > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
tableExists(table) {
|
tableExists(table) {
|
||||||
@ -245,7 +277,7 @@ module.exports = class Database extends Observable {
|
|||||||
|
|
||||||
// insert parent
|
// insert parent
|
||||||
if (meta.isSingle) {
|
if (meta.isSingle) {
|
||||||
await this.updateSingle(meta, doc, doctype);
|
await this.updateSingle(doctype, doc);
|
||||||
} else {
|
} else {
|
||||||
await this.insertOne(baseDoctype, doc);
|
await this.insertOne(baseDoctype, doc);
|
||||||
}
|
}
|
||||||
@ -288,7 +320,7 @@ module.exports = class Database extends Observable {
|
|||||||
|
|
||||||
// update parent
|
// update parent
|
||||||
if (meta.isSingle) {
|
if (meta.isSingle) {
|
||||||
await this.updateSingle(meta, doc, doctype);
|
await this.updateSingle(doctype, doc);
|
||||||
} else {
|
} else {
|
||||||
await this.updateOne(baseDoctype, doc);
|
await this.updateOne(baseDoctype, doc);
|
||||||
}
|
}
|
||||||
@ -348,11 +380,12 @@ module.exports = class Database extends Observable {
|
|||||||
.delete();
|
.delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateSingle(meta, doc, doctype) {
|
async updateSingle(doctype, doc) {
|
||||||
|
let meta = frappe.getMeta(doctype);
|
||||||
await this.deleteSingleValues(doctype);
|
await this.deleteSingleValues(doctype);
|
||||||
for (let field of meta.getValidFields({ withChildren: false })) {
|
for (let field of meta.getValidFields({ withChildren: false })) {
|
||||||
let value = doc[field.fieldname];
|
let value = doc[field.fieldname];
|
||||||
if (value) {
|
if (value != null) {
|
||||||
let singleValue = frappe.newDoc({
|
let singleValue = frappe.newDoc({
|
||||||
doctype: 'SingleValue',
|
doctype: 'SingleValue',
|
||||||
parent: doctype,
|
parent: doctype,
|
||||||
|
@ -86,19 +86,19 @@ module.exports = class BaseDocument extends Observable {
|
|||||||
if (this.meta.isChild && this.parentdoc) {
|
if (this.meta.isChild && this.parentdoc) {
|
||||||
await this.parentdoc.applyChange(this.parentfield);
|
await this.parentdoc.applyChange(this.parentfield);
|
||||||
} else {
|
} else {
|
||||||
await this.applyChange(fieldname);
|
await this.applyChange(fieldname);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
async applyChange(fieldname) {
|
async applyChange(fieldname) {
|
||||||
await this.applyFormula(fieldname);
|
await this.applyFormula(fieldname);
|
||||||
this.roundFloats();
|
this.roundFloats();
|
||||||
await this.trigger('change', {
|
await this.trigger('change', {
|
||||||
doc: this,
|
doc: this,
|
||||||
changed: fieldname
|
changed: fieldname
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
setDefaults() {
|
setDefaults() {
|
||||||
for (let field of this.meta.fields) {
|
for (let field of this.meta.fields) {
|
||||||
@ -121,6 +121,21 @@ module.exports = class BaseDocument extends Observable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
} else if (['Float', 'Currency'].includes(field.fieldtype)) {
|
||||||
|
value = parseFloat(value);
|
||||||
|
}
|
||||||
|
this[field.fieldname] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
setKeywords() {
|
setKeywords() {
|
||||||
let keywords = [];
|
let keywords = [];
|
||||||
for (let fieldname of this.meta.getKeywordFields()) {
|
for (let fieldname of this.meta.getKeywordFields()) {
|
||||||
@ -229,6 +244,7 @@ module.exports = class BaseDocument extends Observable {
|
|||||||
this.syncValues(data);
|
this.syncValues(data);
|
||||||
if (this.meta.isSingle) {
|
if (this.meta.isSingle) {
|
||||||
this.setDefaults();
|
this.setDefaults();
|
||||||
|
this.castValues();
|
||||||
}
|
}
|
||||||
await this.loadLinks();
|
await this.loadLinks();
|
||||||
} else {
|
} else {
|
||||||
@ -249,13 +265,13 @@ module.exports = class BaseDocument extends Observable {
|
|||||||
async loadLink(fieldname) {
|
async loadLink(fieldname) {
|
||||||
this._links = this._links || {};
|
this._links = this._links || {};
|
||||||
let df = this.meta.getField(fieldname);
|
let df = this.meta.getField(fieldname);
|
||||||
if (this[df.fieldname]) {
|
if (this[df.fieldname]) {
|
||||||
this._links[df.fieldname] = await frappe.getDoc(
|
this._links[df.fieldname] = await frappe.getDoc(
|
||||||
df.target,
|
df.target,
|
||||||
this[df.fieldname]
|
this[df.fieldname]
|
||||||
);
|
);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
getLink(fieldname) {
|
getLink(fieldname) {
|
||||||
return this._links ? this._links[fieldname] : null;
|
return this._links ? this._links[fieldname] : null;
|
||||||
|
@ -50,7 +50,9 @@ module.exports = class BaseMeta extends BaseDocument {
|
|||||||
|
|
||||||
// attach default precision to Float and Currency
|
// attach default precision to Float and Currency
|
||||||
if (['Float', 'Currency'].includes(df.fieldtype)) {
|
if (['Float', 'Currency'].includes(df.fieldtype)) {
|
||||||
let defaultPrecision = frappe.SystemSettings ? frappe.SystemSettings.floatPrecision : 2;
|
let defaultPrecision = frappe.SystemSettings
|
||||||
|
? frappe.SystemSettings.floatPrecision
|
||||||
|
: 2;
|
||||||
df.precision = df.precision || defaultPrecision;
|
df.precision = df.precision || defaultPrecision;
|
||||||
}
|
}
|
||||||
return df;
|
return df;
|
||||||
|
Loading…
Reference in New Issue
Block a user