2
0
mirror of https://github.com/frappe/books.git synced 2025-01-08 17:24:05 +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`,
fieldtype: 'Check',
default: 0,
readOnly: 1,
},
],
actions: [ledgerLink],

View File

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

View File

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

View File

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

View File

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

View File

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