2022-05-12 10:04:37 +00:00
|
|
|
import { Fyo } from 'fyo';
|
|
|
|
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;
|
|
|
|
|
|
|
|
fyo: Fyo;
|
2022-05-15 18:48:57 +00:00
|
|
|
columns: ColumnField[] = [];
|
|
|
|
filters: Field[] = [];
|
2022-05-12 10:04:37 +00:00
|
|
|
reportData: ReportData;
|
2022-05-16 06:54:58 +00:00
|
|
|
usePagination: boolean = false;
|
2022-05-30 11:34:25 +00:00
|
|
|
shouldRefresh: boolean = 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 = [];
|
2022-05-15 18:48:57 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 07:04:33 +00:00
|
|
|
get title() {
|
|
|
|
// @ts-ignore
|
|
|
|
return this.constructor.title;
|
|
|
|
}
|
|
|
|
|
|
|
|
get reportName() {
|
|
|
|
// @ts-ignore
|
|
|
|
return this.constructor.reportName;
|
|
|
|
}
|
|
|
|
|
2022-05-15 18:48:57 +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;
|
|
|
|
}
|
|
|
|
|
2022-05-27 07:15:07 +00:00
|
|
|
async set(key: string, value: RawValue, callPostSet: boolean = true) {
|
2022-05-12 10:04:37 +00:00
|
|
|
const field = this.filters.find((f) => f.fieldname === key);
|
2022-05-13 13:51:26 +00:00
|
|
|
if (field === undefined) {
|
2022-05-12 10:04:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const prevValue = this[key];
|
|
|
|
if (prevValue === value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-13 13:51:26 +00:00
|
|
|
if (getIsNullOrUndef(value)) {
|
|
|
|
delete this[key];
|
|
|
|
} else {
|
|
|
|
this[key] = value;
|
|
|
|
}
|
|
|
|
|
2022-05-27 07:15:07 +00:00
|
|
|
if (callPostSet) {
|
2022-05-30 11:34:25 +00:00
|
|
|
await this.updateData(key);
|
2022-05-27 07:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-30 11:34:25 +00:00
|
|
|
async updateData(key?: string, force?: boolean) {
|
2022-05-16 07:49:01 +00:00
|
|
|
await this.setDefaultFilters();
|
2022-05-15 18:48:57 +00:00
|
|
|
this.filters = await this.getFilters();
|
|
|
|
this.columns = await this.getColumns();
|
2022-05-30 11:34:25 +00:00
|
|
|
await this.setReportData(key, force);
|
2022-05-12 10:04:37 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 08:46:05 +00:00
|
|
|
/**
|
|
|
|
* Should first check if filter value is set
|
|
|
|
* and update only if it is not set.
|
|
|
|
*/
|
|
|
|
async setDefaultFilters() {}
|
|
|
|
abstract getActions(): Action[];
|
2022-05-15 18:48:57 +00:00
|
|
|
abstract getFilters(): Field[] | Promise<Field[]>;
|
|
|
|
abstract getColumns(): ColumnField[] | Promise<ColumnField[]>;
|
2022-05-30 11:34:25 +00:00
|
|
|
abstract setReportData(filter?: string, force?: boolean): Promise<void>;
|
2022-05-12 10:04:37 +00:00
|
|
|
}
|