diff --git a/fyo/model/doc.ts b/fyo/model/doc.ts index 8800f91f..c089448f 100644 --- a/fyo/model/doc.ts +++ b/fyo/model/doc.ts @@ -291,6 +291,7 @@ export class Doc extends Observable { this[field.fieldname] = defaultValue; } } + async remove(fieldname: string, idx: number) { const childDocs = ((this[fieldname] ?? []) as Doc[]).filter( (row, i) => row.idx !== idx || i !== idx @@ -318,8 +319,25 @@ export class Doc extends Observable { this[fieldname] = childDocs; } + _setChildDocsParent() { + for (const { fieldname } of this.tableFields) { + const value = this.get(fieldname); + if (!Array.isArray(value)) { + continue; + } + + for (const childDoc of value) { + if (childDoc.parent) { + continue; + } + + childDoc.parent = this.name; + } + } + } + _getChildDoc(docValueMap: Doc | DocValueMap, fieldname: string): Doc { - if (!this.name) { + if (!this.name && this.schema.naming !== 'manual') { this.name = getRandomString(); } @@ -672,6 +690,7 @@ export class Doc extends Observable { async _preSync() { this._setChildDocsIdx(); + this._setChildDocsParent(); await this._applyFormula(); await this._validateSync(); await this.trigger('validate'); diff --git a/models/baseModels/Account/Account.ts b/models/baseModels/Account/Account.ts index 97c5238f..7c620d1c 100644 --- a/models/baseModels/Account/Account.ts +++ b/models/baseModels/Account/Account.ts @@ -4,6 +4,7 @@ import { DefaultMap, FiltersMap, ListViewSettings, + RequiredMap, TreeViewSettings, } from 'fyo/model/types'; import { QueryFilter } from 'utils/db/types'; @@ -26,6 +27,16 @@ export class Account extends Doc { return !this.isDebit; } + required: RequiredMap = { + /** + * Added here cause rootAccounts don't have parents + * they are created during initialization. if this is + * added to the schema it will cause NOT NULL errors + */ + + parentAccount: () => true, + }; + static defaults: DefaultMap = { /** * NestedSet indices are actually not used diff --git a/schemas/app/Account.json b/schemas/app/Account.json index 4ae0797a..d9f861d1 100644 --- a/schemas/app/Account.json +++ b/schemas/app/Account.json @@ -1,6 +1,7 @@ { "name": "Account", "label": "Account", + "naming": "manual", "isSingle": false, "isTree": true, "fields": [ @@ -37,14 +38,13 @@ "label": "Expense" } ], - "readOnly": true + "required": true }, { "fieldname": "parentAccount", "label": "Parent Account", "fieldtype": "Link", - "target": "Account", - "readOnly": true + "target": "Account" }, { "fieldname": "accountType", @@ -140,7 +140,7 @@ "fieldname": "isGroup", "label": "Is Group", "fieldtype": "Check", - "readOnly": true + "default": false } ], "quickEditFields": [ diff --git a/schemas/app/Color.json b/schemas/app/Color.json index 0e88ca16..8950e53c 100644 --- a/schemas/app/Color.json +++ b/schemas/app/Color.json @@ -1,6 +1,7 @@ { "name": "Color", "label": "Color", + "naming": "manual", "fields": [ { "fieldname": "name", diff --git a/schemas/app/Currency.json b/schemas/app/Currency.json index 362b7a79..acbdcf3a 100644 --- a/schemas/app/Currency.json +++ b/schemas/app/Currency.json @@ -1,6 +1,7 @@ { "name": "Currency", "label": "Currency", + "naming": "manual", "isSingle": false, "fields": [ { diff --git a/schemas/app/JournalEntry.json b/schemas/app/JournalEntry.json index 94344202..ea65318b 100644 --- a/schemas/app/JournalEntry.json +++ b/schemas/app/JournalEntry.json @@ -1,6 +1,7 @@ { "label": "Journal Entry", "name": "JournalEntry", + "naming": "numberSeries", "isSubmittable": true, "fields": [ { diff --git a/schemas/app/NumberSeries.json b/schemas/app/NumberSeries.json index 0f917a76..2a4023ba 100644 --- a/schemas/app/NumberSeries.json +++ b/schemas/app/NumberSeries.json @@ -1,6 +1,7 @@ { "name": "NumberSeries", "label": "Number Series", + "naming": "manual", "isSingle": false, "isChild": false, "fields": [ diff --git a/schemas/app/Payment.json b/schemas/app/Payment.json index 730713f0..3482e028 100644 --- a/schemas/app/Payment.json +++ b/schemas/app/Payment.json @@ -1,6 +1,7 @@ { "name": "Payment", "label": "Payment", + "naming": "numberSeries", "isSingle": false, "isChild": false, "isSubmittable": true, diff --git a/schemas/app/PurchaseInvoice.json b/schemas/app/PurchaseInvoice.json index 739579c1..d22389ec 100644 --- a/schemas/app/PurchaseInvoice.json +++ b/schemas/app/PurchaseInvoice.json @@ -1,6 +1,7 @@ { "name": "PurchaseInvoice", "label": "Purchase Invoice", + "naming": "numberSeries", "isSingle": false, "isChild": false, "isSubmittable": true, diff --git a/schemas/app/SalesInvoice.json b/schemas/app/SalesInvoice.json index 042a9c0a..a2228307 100644 --- a/schemas/app/SalesInvoice.json +++ b/schemas/app/SalesInvoice.json @@ -1,6 +1,7 @@ { "name": "SalesInvoice", "label": "Sales Invoice", + "naming": "numberSeries", "isSingle": false, "isChild": false, "isSubmittable": true, diff --git a/schemas/app/Tax.json b/schemas/app/Tax.json index 5f7d7b1e..efc57225 100644 --- a/schemas/app/Tax.json +++ b/schemas/app/Tax.json @@ -1,6 +1,7 @@ { "name": "Tax", "label": "Tax", + "naming": "manual", "isSingle": false, "isChild": false, "fields": [ diff --git a/schemas/core/PatchRun.json b/schemas/core/PatchRun.json index a4617873..b92a39db 100644 --- a/schemas/core/PatchRun.json +++ b/schemas/core/PatchRun.json @@ -1,6 +1,7 @@ { "name": "PatchRun", "label": "Patch Run", + "naming": "manual", "fields": [ { "fieldname": "name",