2
0
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:
Faris Ansari 2019-12-18 23:52:47 +05:30
parent fef4f96e78
commit d19182b583
3 changed files with 69 additions and 18 deletions

View File

@ -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,

View File

@ -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 {

View File

@ -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;