2
0
mirror of https://github.com/frappe/books.git synced 2025-01-24 07:38:25 +00:00

fix: set readonly

This commit is contained in:
18alantom 2022-02-23 12:52:14 +05:30
parent 5f4cbe1795
commit f7937935fb
6 changed files with 75 additions and 41 deletions

View File

@ -63,6 +63,7 @@ export default {
label: t`Cancelled`, label: t`Cancelled`,
fieldtype: 'Check', fieldtype: 'Check',
default: 0, default: 0,
readOnly: 1,
}, },
], ],
actions: [ledgerLink], actions: [ledgerLink],

View File

@ -150,6 +150,7 @@ export default {
label: t`Cancelled`, label: t`Cancelled`,
fieldtype: 'Check', fieldtype: 'Check',
default: 0, default: 0,
readOnly: 1,
}, },
], ],

View File

@ -133,6 +133,7 @@ export default {
label: t`Cancelled`, label: t`Cancelled`,
fieldtype: 'Check', fieldtype: 'Check',
default: 0, default: 0,
readOnly: 1,
}, },
], ],

View File

@ -132,6 +132,7 @@ export default {
label: t`Cancelled`, label: t`Cancelled`,
fieldtype: 'Check', fieldtype: 'Check',
default: 0, default: 0,
readOnly: 1,
}, },
], ],

View File

@ -17,65 +17,85 @@ type Exclusion = {
}; };
type Map = { type Map = {
[key: string]: string | boolean; [key: string]: string | boolean | object;
}; };
interface TemplateField { interface TemplateField {
label: string; label: string;
fieldname: string; fieldname: string;
required: boolean; required: boolean;
doctype: string;
options?: string[];
fieldtype: FieldType;
} }
const exclusion: Exclusion = { const exclusion: Exclusion = {
Item: ['image'], 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[] { function getTemplateFields(doctype: string): TemplateField[] {
const fields: TemplateField[] = []; const fields: TemplateField[] = [];
if (!doctype) { if (!doctype) {
return []; return [];
} }
const doctypes: string[] = [doctype];
// @ts-ignore while (doctypes.length > 0) {
const primaryFields: Field[] = frappe.models[doctype].fields; const dt = doctypes.pop();
const tableTypes: string[] = []; if (!dt) {
let exclusionFields: string[] = exclusion[doctype] ?? []; break;
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),
});
} }
);
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; return fields;
} }
@ -106,6 +126,7 @@ export class Importer {
parsedValues: string[][] = []; parsedValues: string[][] = [];
assignedMap: Map = {}; // target: import assignedMap: Map = {}; // target: import
requiredMap: Map = {}; requiredMap: Map = {};
labelFieldMap: Map = {};
constructor(doctype: string) { constructor(doctype: string) {
this.doctype = doctype; this.doctype = doctype;
@ -120,6 +141,10 @@ export class Importer {
acc[k.label] = k.required; acc[k.label] = k.required;
return acc; return acc;
}, {}); }, {});
this.labelFieldMap = this.templateFields.reduce((acc: Map, k) => {
acc[k.label] = k;
return acc;
}, {});
} }
get assignableLabels() { get assignableLabels() {
@ -222,4 +247,6 @@ export class Importer {
} }
// @ts-ignore // @ts-ignore
window.pc = parseCSV; window.im = importable;
// @ts-ignore
window.gtf = getTemplateFields;

View File

@ -23,4 +23,7 @@ export interface Field {
target?: string; target?: string;
default?: unknown; default?: unknown;
required?: number; required?: number;
readOnly?: number;
hidden?: number | Function;
options?: string[];
} }