2022-05-12 15:34:37 +05:30
|
|
|
import { Fyo } from 'fyo';
|
2023-03-02 15:50:03 +05:30
|
|
|
import { Converter } from 'fyo/core/converter';
|
|
|
|
import { DocValue } from 'fyo/core/types';
|
2022-05-12 15:34:37 +05:30
|
|
|
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;
|
2023-06-22 14:22:54 +05:30
|
|
|
static isInventory = false;
|
2022-05-12 15:34:37 +05:30
|
|
|
|
|
|
|
fyo: Fyo;
|
2022-05-16 00:18:57 +05:30
|
|
|
columns: ColumnField[] = [];
|
|
|
|
filters: Field[] = [];
|
2022-05-12 15:34:37 +05:30
|
|
|
reportData: ReportData;
|
2023-06-22 14:22:54 +05:30
|
|
|
usePagination = false;
|
|
|
|
shouldRefresh = false;
|
2022-05-18 20:28:35 +05:30
|
|
|
abstract loading: boolean;
|
2022-05-12 15:34:37 +05:30
|
|
|
|
|
|
|
constructor(fyo: Fyo) {
|
|
|
|
super();
|
|
|
|
this.fyo = fyo;
|
|
|
|
this.reportData = [];
|
2022-05-16 00:18:57 +05:30
|
|
|
}
|
|
|
|
|
2023-06-22 14:22:54 +05:30
|
|
|
get title(): string {
|
|
|
|
return (this.constructor as typeof Report).title;
|
2022-05-18 12:34:33 +05:30
|
|
|
}
|
|
|
|
|
2023-06-22 14:22:54 +05:30
|
|
|
get reportName(): string {
|
|
|
|
return (this.constructor as typeof Report).reportName;
|
2022-05-18 12:34:33 +05:30
|
|
|
}
|
|
|
|
|
2022-05-16 00:18:57 +05:30
|
|
|
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 15:34:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-06-22 14:22:54 +05:30
|
|
|
async set(key: string, value: DocValue, callPostSet = true) {
|
2022-05-12 15:34:37 +05:30
|
|
|
const field = this.filters.find((f) => f.fieldname === key);
|
2022-05-13 19:21:26 +05:30
|
|
|
if (field === undefined) {
|
2022-05-12 15:34:37 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-02 15:50:03 +05:30
|
|
|
value = Converter.toRawValue(value, field, this.fyo);
|
2022-05-12 15:34:37 +05:30
|
|
|
const prevValue = this[key];
|
|
|
|
if (prevValue === value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-13 19:21:26 +05:30
|
|
|
if (getIsNullOrUndef(value)) {
|
|
|
|
delete this[key];
|
|
|
|
} else {
|
|
|
|
this[key] = value;
|
|
|
|
}
|
|
|
|
|
2022-05-27 12:45:07 +05:30
|
|
|
if (callPostSet) {
|
2022-05-30 17:04:25 +05:30
|
|
|
await this.updateData(key);
|
2022-05-27 12:45:07 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-30 17:04:25 +05:30
|
|
|
async updateData(key?: string, force?: boolean) {
|
2022-05-16 13:19:01 +05:30
|
|
|
await this.setDefaultFilters();
|
2022-05-16 00:18:57 +05:30
|
|
|
this.filters = await this.getFilters();
|
|
|
|
this.columns = await this.getColumns();
|
2022-05-30 17:04:25 +05:30
|
|
|
await this.setReportData(key, force);
|
2022-05-12 15:34:37 +05:30
|
|
|
}
|
|
|
|
|
2022-05-14 14:16:05 +05:30
|
|
|
/**
|
|
|
|
* Should first check if filter value is set
|
|
|
|
* and update only if it is not set.
|
|
|
|
*/
|
2023-06-22 14:22:54 +05:30
|
|
|
abstract setDefaultFilters(): void | Promise<void>;
|
2022-05-14 14:16:05 +05:30
|
|
|
abstract getActions(): Action[];
|
2022-05-16 00:18:57 +05:30
|
|
|
abstract getFilters(): Field[] | Promise<Field[]>;
|
|
|
|
abstract getColumns(): ColumnField[] | Promise<ColumnField[]>;
|
2022-05-30 17:04:25 +05:30
|
|
|
abstract setReportData(filter?: string, force?: boolean): Promise<void>;
|
2022-05-12 15:34:37 +05:30
|
|
|
}
|