From f7937935fb8b65df6e5e1addd58c18a41fdd1ee9 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Wed, 23 Feb 2022 12:52:14 +0530 Subject: [PATCH] fix: set readonly --- models/doctype/JournalEntry/JournalEntry.js | 1 + models/doctype/Payment/Payment.js | 1 + .../PurchaseInvoice/PurchaseInvoice.js | 1 + models/doctype/SalesInvoice/SalesInvoice.js | 1 + src/dataImport.ts | 109 +++++++++++------- src/types/model.ts | 3 + 6 files changed, 75 insertions(+), 41 deletions(-) diff --git a/models/doctype/JournalEntry/JournalEntry.js b/models/doctype/JournalEntry/JournalEntry.js index 53fcd6dc..eb26d034 100644 --- a/models/doctype/JournalEntry/JournalEntry.js +++ b/models/doctype/JournalEntry/JournalEntry.js @@ -63,6 +63,7 @@ export default { label: t`Cancelled`, fieldtype: 'Check', default: 0, + readOnly: 1, }, ], actions: [ledgerLink], diff --git a/models/doctype/Payment/Payment.js b/models/doctype/Payment/Payment.js index a3a62bf9..5773141c 100644 --- a/models/doctype/Payment/Payment.js +++ b/models/doctype/Payment/Payment.js @@ -150,6 +150,7 @@ export default { label: t`Cancelled`, fieldtype: 'Check', default: 0, + readOnly: 1, }, ], diff --git a/models/doctype/PurchaseInvoice/PurchaseInvoice.js b/models/doctype/PurchaseInvoice/PurchaseInvoice.js index 7e9d3af0..ea2640eb 100644 --- a/models/doctype/PurchaseInvoice/PurchaseInvoice.js +++ b/models/doctype/PurchaseInvoice/PurchaseInvoice.js @@ -133,6 +133,7 @@ export default { label: t`Cancelled`, fieldtype: 'Check', default: 0, + readOnly: 1, }, ], diff --git a/models/doctype/SalesInvoice/SalesInvoice.js b/models/doctype/SalesInvoice/SalesInvoice.js index 6e27ad4d..072c19e5 100644 --- a/models/doctype/SalesInvoice/SalesInvoice.js +++ b/models/doctype/SalesInvoice/SalesInvoice.js @@ -132,6 +132,7 @@ export default { label: t`Cancelled`, fieldtype: 'Check', default: 0, + readOnly: 1, }, ], diff --git a/src/dataImport.ts b/src/dataImport.ts index 5fbb1ceb..bc4cd635 100644 --- a/src/dataImport.ts +++ b/src/dataImport.ts @@ -17,65 +17,85 @@ type Exclusion = { }; type Map = { - [key: string]: string | boolean; + [key: string]: string | boolean | object; }; interface TemplateField { label: string; fieldname: string; required: boolean; + doctype: string; + options?: string[]; + fieldtype: FieldType; } const exclusion: Exclusion = { Item: ['image'], + Supplier: ['address', 'outstandingAmount', 'supplier', 'image', 'customer'], + Customer: ['address', 'outstandingAmount', 'supplier', 'image', 'customer'], }; +function getFilteredDocFields(doctype: string): [TemplateField[], string[]] { + const fields: TemplateField[] = []; + // @ts-ignore + const primaryFields: Field[] = frappe.models[doctype].fields; + const tableTypes: string[] = []; + const exclusionFields: string[] = exclusion[doctype] ?? []; + + primaryFields.forEach( + ({ + label, + fieldtype, + childtype, + fieldname, + readOnly, + required, + hidden, + options, + }) => { + if ( + readOnly || + (hidden && typeof hidden === 'number') || + exclusionFields.includes(fieldname) + ) { + return; + } + + if (fieldtype === FieldType.Table && childtype) { + tableTypes.push(childtype); + return; + } + + fields.push({ + label, + fieldname, + doctype, + options, + fieldtype, + required: Boolean(required ?? false), + }); + } + ); + + return [fields, tableTypes]; +} + function getTemplateFields(doctype: string): TemplateField[] { const fields: TemplateField[] = []; if (!doctype) { return []; } - - // @ts-ignore - const primaryFields: Field[] = frappe.models[doctype].fields; - const tableTypes: string[] = []; - let exclusionFields: string[] = exclusion[doctype] ?? []; - - primaryFields.forEach( - ({ label, fieldtype, childtype, fieldname, required }) => { - if (exclusionFields.includes(fieldname)) { - return; - } - - if (fieldtype === FieldType.Table && childtype) { - tableTypes.push(childtype); - } - - fields.push({ - label, - fieldname, - required: Boolean(required ?? false), - }); + const doctypes: string[] = [doctype]; + while (doctypes.length > 0) { + const dt = doctypes.pop(); + if (!dt) { + break; } - ); - - tableTypes.forEach((childtype) => { - exclusionFields = exclusion[childtype] ?? []; - - // @ts-ignore - const childFields: Field[] = frappe.models[childtype].fields; - childFields.forEach(({ label, fieldtype, fieldname, required }) => { - if ( - exclusionFields.includes(fieldname) || - fieldtype === FieldType.Table - ) { - return; - } - - fields.push({ label, fieldname, required: Boolean(required ?? false) }); - }); - }); + const [templateFields, tableTypes] = getFilteredDocFields(dt); + fields.push(...templateFields); + doctypes.push(...tableTypes); + } return fields; } @@ -106,6 +126,7 @@ export class Importer { parsedValues: string[][] = []; assignedMap: Map = {}; // target: import requiredMap: Map = {}; + labelFieldMap: Map = {}; constructor(doctype: string) { this.doctype = doctype; @@ -120,6 +141,10 @@ export class Importer { acc[k.label] = k.required; return acc; }, {}); + this.labelFieldMap = this.templateFields.reduce((acc: Map, k) => { + acc[k.label] = k; + return acc; + }, {}); } get assignableLabels() { @@ -222,4 +247,6 @@ export class Importer { } // @ts-ignore -window.pc = parseCSV; +window.im = importable; +// @ts-ignore +window.gtf = getTemplateFields; diff --git a/src/types/model.ts b/src/types/model.ts index d1e05552..0393932e 100644 --- a/src/types/model.ts +++ b/src/types/model.ts @@ -23,4 +23,7 @@ export interface Field { target?: string; default?: unknown; required?: number; + readOnly?: number; + hidden?: number | Function; + options?: string[]; }