2
0
mirror of https://github.com/frappe/books.git synced 2024-11-08 23:00:56 +00:00
books/reports/Report.ts

102 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-05-12 10:04:37 +00:00
import { Fyo } from 'fyo';
import { Converter } from 'fyo/core/converter';
import { DocValue } from 'fyo/core/types';
2022-05-12 10:04:37 +00:00
import { Action } from 'fyo/model/types';
import Observable from 'fyo/utils/observable';
import { Field, RawValue } from 'schemas/types';
import { getIsNullOrUndef } from 'utils';
import { ColumnField, ReportData } from './types';
export abstract class Report extends Observable<RawValue> {
static title: string;
static reportName: string;
static isInventory = false;
2022-05-12 10:04:37 +00:00
fyo: Fyo;
columns: ColumnField[] = [];
filters: Field[] = [];
2022-05-12 10:04:37 +00:00
reportData: ReportData;
usePagination = false;
shouldRefresh = false;
2022-05-18 14:58:35 +00:00
abstract loading: boolean;
2022-05-12 10:04:37 +00:00
constructor(fyo: Fyo) {
super();
this.fyo = fyo;
this.reportData = [];
}
get title(): string {
return (this.constructor as typeof Report).title;
2022-05-18 07:04:33 +00:00
}
get reportName(): string {
return (this.constructor as typeof Report).reportName;
2022-05-18 07:04:33 +00:00
}
async initialize() {
/**
* Not in constructor cause possibly async.
*/
await this.setDefaultFilters();
this.filters = await this.getFilters();
this.columns = await this.getColumns();
await this.setReportData();
2022-05-12 10:04:37 +00:00
}
get filterMap() {
const filterMap: Record<string, RawValue> = {};
for (const { fieldname } of this.filters) {
const value = this.get(fieldname);
if (getIsNullOrUndef(value)) {
continue;
}
filterMap[fieldname] = value;
}
return filterMap;
}
async set(key: string, value: DocValue, callPostSet = true) {
2022-05-12 10:04:37 +00:00
const field = this.filters.find((f) => f.fieldname === key);
if (field === undefined) {
2022-05-12 10:04:37 +00:00
return;
}
value = Converter.toRawValue(value, field, this.fyo);
2022-05-12 10:04:37 +00:00
const prevValue = this[key];
if (prevValue === value) {
return;
}
if (getIsNullOrUndef(value)) {
delete this[key];
} else {
this[key] = value;
}
2022-05-27 07:15:07 +00:00
if (callPostSet) {
await this.updateData(key);
2022-05-27 07:15:07 +00:00
}
}
async updateData(key?: string, force?: boolean) {
2022-05-16 07:49:01 +00:00
await this.setDefaultFilters();
this.filters = await this.getFilters();
this.columns = await this.getColumns();
await this.setReportData(key, force);
2022-05-12 10:04:37 +00:00
}
/**
* Should first check if filter value is set
* and update only if it is not set.
*/
abstract setDefaultFilters(): void | Promise<void>;
abstract getActions(): Action[];
abstract getFilters(): Field[] | Promise<Field[]>;
abstract getColumns(): ColumnField[] | Promise<ColumnField[]>;
abstract setReportData(filter?: string, force?: boolean): Promise<void>;
2022-05-12 10:04:37 +00:00
}