From 758727b29651845c367044f52903588c34a362ab Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Sun, 1 May 2022 13:09:03 +0530 Subject: [PATCH] fix(ui/ux): cleanup forms - table scroll issue, etc --- fyo/model/doc.ts | 84 ++--- fyo/model/helpers.ts | 6 + .../JournalEntryAccount.ts | 8 +- schemas/app/PurchaseInvoice.json | 3 +- src/components/Controls/Currency.vue | 3 +- src/components/Controls/Table.vue | 53 ++- src/components/Dropdown.vue | 5 +- src/components/PageHeader.vue | 6 +- src/components/Popover.vue | 3 +- src/pages/DataImport.vue | 30 +- src/pages/InvoiceForm.vue | 349 ++++++++++-------- src/pages/JournalEntryForm.vue | 183 ++++----- src/pages/ListView/ListView.vue | 18 +- src/pages/PrintView/PrintView.vue | 20 +- src/pages/QuickEditForm.vue | 4 +- src/pages/Report.vue | 26 +- 16 files changed, 421 insertions(+), 380 deletions(-) diff --git a/fyo/model/doc.ts b/fyo/model/doc.ts index 74cdfd81..ae9f88e7 100644 --- a/fyo/model/doc.ts +++ b/fyo/model/doc.ts @@ -24,7 +24,8 @@ import { areDocValuesEqual, getMissingMandatoryMessage, getPreDefaultValues, - shouldApplyFormula + setChildDocIdx, + shouldApplyFormula, } from './helpers'; import { setName } from './naming'; import { @@ -239,60 +240,53 @@ 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 + ); - async append(fieldname: string, docValueMap: Doc | DocValueMap = {}) { - // push child row and trigger change - this.push(fieldname, docValueMap); - this._dirty = true; + setChildDocIdx(childDocs); + this[fieldname] = childDocs; + this._setDirty(true); return await this._applyChange(fieldname); } - async remove(fieldname: string, idx: number) { - let rows = this[fieldname] as Doc[] | undefined; - if (!Array.isArray(rows)) { - return; - } - + async append(fieldname: string, docValueMap: DocValueMap = {}) { + this.push(fieldname, docValueMap); this._setDirty(true); - rows = rows.filter((row, i) => row.idx !== idx || i !== idx)!; - rows.forEach((row, i) => { - row.idx = i; - }); - this[fieldname] = rows; return await this._applyChange(fieldname); } push(fieldname: string, docValueMap: Doc | DocValueMap = {}) { - // push child row without triggering change - this[fieldname] ??= []; - const childDoc = this._getChildDoc(docValueMap, fieldname); - childDoc.parentdoc = this; - (this[fieldname] as Doc[]).push(childDoc); + const childDocs = [ + (this[fieldname] ?? []) as Doc[], + this._getChildDoc(docValueMap, fieldname), + ].flat(); + + setChildDocIdx(childDocs); + this[fieldname] = childDocs; } _getChildDoc(docValueMap: Doc | DocValueMap, fieldname: string): Doc { + docValueMap.name ??= getRandomString(); + + // Child Meta Fields + docValueMap.parent ??= this.name; + docValueMap.parentSchemaName ??= this.schemaName; + docValueMap.parentFieldname ??= fieldname; + if (docValueMap instanceof Doc) { + docValueMap.parentdoc ??= this; return docValueMap; } - const data: Record = Object.assign({}, docValueMap); - - data.parent = this.name; - data.parentSchemaName = this.schemaName; - data.parentFieldname = fieldname; - data.parentdoc = this; - - if (!data.idx) { - data.idx = ((this[fieldname] as Doc[]) || []).length; - } - - if (!data.name) { - data.name = getRandomString(); - } - - const targetField = this.fieldMap[fieldname] as TargetField; - const schema = this.fyo.schemaMap[targetField.target] as Schema; - const childDoc = new Doc(schema, data as DocValueMap, this.fyo); + const childSchemaName = (this.fieldMap[fieldname] as TargetField).target; + const childDoc = this.fyo.doc.getNewDoc( + childSchemaName, + docValueMap, + false + ); + childDoc.parentdoc = this; return childDoc; } @@ -522,7 +516,7 @@ export class Doc extends Observable { } } - async _applyFormula(fieldname?: string) { + async _applyFormula(fieldname?: string): Promise { const doc = this; let changed = false; @@ -532,15 +526,7 @@ export class Doc extends Observable { // children for (const row of childDocs) { - const formulaFields = Object.keys(this.formulas).map( - (fn) => this.fieldMap[fn] - ); - - changed ||= await this._applyFormulaForFields( - formulaFields, - row, - fieldname - ); + changed ||= (await row?._applyFormula()) ?? false; } // parent or child row diff --git a/fyo/model/helpers.ts b/fyo/model/helpers.ts index 368b9529..be8f8271 100644 --- a/fyo/model/helpers.ts +++ b/fyo/model/helpers.ts @@ -108,3 +108,9 @@ export function shouldApplyFormula(field: Field, doc: Doc, fieldname?: string) { const value = doc.get(field.fieldname); return getIsNullOrUndef(value); } + +export function setChildDocIdx(childDocs: Doc[]) { + for (const idx in childDocs) { + childDocs[idx].idx = +idx; + } +} diff --git a/models/baseModels/JournalEntryAccount/JournalEntryAccount.ts b/models/baseModels/JournalEntryAccount/JournalEntryAccount.ts index 6e12a762..af762e95 100644 --- a/models/baseModels/JournalEntryAccount/JournalEntryAccount.ts +++ b/models/baseModels/JournalEntryAccount/JournalEntryAccount.ts @@ -4,8 +4,12 @@ import Money from 'pesa/dist/types/src/money'; export class JournalEntryAccount extends Doc { getAutoDebitCredit(type: 'debit' | 'credit') { - const otherType = type === 'debit' ? 'credit' : 'debit'; + const currentValue = this.get(type) as Money; + if (!currentValue.isZero()) { + return; + } + const otherType = type === 'debit' ? 'credit' : 'debit'; const otherTypeValue = this.get(otherType) as Money; if (!otherTypeValue.isZero()) { return this.fyo.pesa(0); @@ -26,11 +30,9 @@ export class JournalEntryAccount extends Doc { formulas: FormulaMap = { debit: { formula: async () => this.getAutoDebitCredit('debit'), - dependsOn: ['credit'], }, credit: { formula: async () => this.getAutoDebitCredit('credit'), - dependsOn: ['debit'], }, }; diff --git a/schemas/app/PurchaseInvoice.json b/schemas/app/PurchaseInvoice.json index 72fc530b..06433221 100644 --- a/schemas/app/PurchaseInvoice.json +++ b/schemas/app/PurchaseInvoice.json @@ -91,6 +91,7 @@ { "fieldname": "terms", "label": "Terms", + "placeholder": "Add invoice terms", "fieldtype": "Text" }, { @@ -103,4 +104,4 @@ } ], "keywordFields": ["name", "party", "numberSeries"] -} \ No newline at end of file +} diff --git a/src/components/Controls/Currency.vue b/src/components/Controls/Currency.vue index 207c6c18..2ef9a373 100644 --- a/src/components/Controls/Currency.vue +++ b/src/components/Controls/Currency.vue @@ -29,6 +29,7 @@