2
0
mirror of https://github.com/frappe/books.git synced 2024-12-24 20:00:29 +00:00
books/reports/GeneralLedger/GeneralLedger.ts

428 lines
9.9 KiB
TypeScript
Raw Normal View History

2022-05-12 10:04:37 +00:00
import { Fyo, t } from 'fyo';
import { DateTime } from 'luxon';
import { ModelNameEnum } from 'models/types';
import { LedgerReport } from 'reports/LedgerReport';
import {
ColumnField,
GroupedMap,
LedgerEntry,
ReportData,
ReportRow,
} from 'reports/types';
import { Field, FieldTypeEnum } from 'schemas/types';
2022-05-12 10:04:37 +00:00
import { QueryFilter } from 'utils/db/types';
type ReferenceType =
| ModelNameEnum.SalesInvoice
| ModelNameEnum.PurchaseInvoice
| ModelNameEnum.Payment
| ModelNameEnum.JournalEntry
| ModelNameEnum.Shipment
| ModelNameEnum.PurchaseReceipt
| 'All';
export class GeneralLedger extends LedgerReport {
2022-05-12 10:04:37 +00:00
static title = t`General Ledger`;
static reportName = 'general-ledger';
usePagination: boolean = true;
2022-05-18 14:58:35 +00:00
loading: boolean = false;
2022-05-12 10:04:37 +00:00
ascending: boolean = false;
reverted: boolean = false;
referenceType: ReferenceType = 'All';
groupBy: 'none' | 'party' | 'account' | 'referenceName' = 'none';
2022-05-12 10:04:37 +00:00
_rawData: LedgerEntry[] = [];
constructor(fyo: Fyo) {
super(fyo);
}
2022-05-12 10:04:37 +00:00
async setDefaultFilters() {
if (!this.toDate) {
this.toDate = DateTime.now().plus({ days: 1 }).toISODate();
this.fromDate = DateTime.now().minus({ years: 1 }).toISODate();
2022-05-12 10:04:37 +00:00
}
}
async setReportData(filter?: string, force?: boolean) {
2022-05-18 14:58:35 +00:00
this.loading = true;
let sort = true;
if (force || filter !== 'grouped' || this._rawData.length === 0) {
2022-05-12 10:04:37 +00:00
await this._setRawData();
sort = false;
2022-05-12 10:04:37 +00:00
}
const map = this._getGroupedMap(sort);
this._setIndexOnEntries(map);
2022-05-12 10:04:37 +00:00
const { totalDebit, totalCredit } = this._getTotalsAndSetBalance(map);
const consolidated = this._consolidateEntries(map);
/**
* Push a blank row if last row isn't blank
*/
if (consolidated.at(-1)?.name !== -3) {
2022-05-12 10:04:37 +00:00
this._pushBlankEntry(consolidated);
}
/**
* Set the closing row
*/
consolidated.push({
name: -2, // Bold
account: t`Closing`,
date: null,
debit: totalDebit,
credit: totalCredit,
balance: totalDebit - totalCredit,
2022-05-12 10:04:37 +00:00
referenceType: '',
referenceName: '',
party: '',
reverted: false,
reverts: '',
});
this.reportData = this._convertEntriesToReportData(consolidated);
2022-05-18 14:58:35 +00:00
this.loading = false;
2022-05-12 10:04:37 +00:00
}
_setIndexOnEntries(map: GroupedMap) {
let i = 1;
for (const key of map.keys()) {
for (const entry of map.get(key)!) {
entry.index = String(i);
i = i + 1;
}
}
}
2022-05-12 10:04:37 +00:00
_convertEntriesToReportData(entries: LedgerEntry[]): ReportData {
const reportData = [];
for (const entry of entries) {
const row = this._getRowFromEntry(entry, this.columns);
2022-05-12 10:04:37 +00:00
reportData.push(row);
}
return reportData;
}
_getRowFromEntry(entry: LedgerEntry, columns: ColumnField[]): ReportRow {
2022-05-12 10:04:37 +00:00
if (entry.name === -3) {
return {
2022-05-16 10:10:35 +00:00
isEmpty: true,
cells: columns.map((c) => ({
rawValue: '',
value: '',
width: c.width ?? 1,
})),
};
2022-05-12 10:04:37 +00:00
}
const row: ReportRow = { cells: [] };
for (const col of columns) {
const align = col.align ?? 'left';
const width = col.width ?? 1;
const fieldname = col.fieldname;
let value = entry[fieldname as keyof LedgerEntry];
const rawValue = value;
2022-05-12 10:04:37 +00:00
if (value === null || value === undefined) {
value = '';
2022-05-12 10:04:37 +00:00
}
if (value instanceof Date) {
value = this.fyo.format(value, FieldTypeEnum.Date);
}
if (typeof value === 'number' && fieldname !== 'index') {
2022-05-12 10:04:37 +00:00
value = this.fyo.format(value, FieldTypeEnum.Currency);
}
if (typeof value === 'boolean' && fieldname === 'reverted') {
value = value ? t`Reverted` : '';
} else {
value = String(value);
2022-05-12 10:04:37 +00:00
}
if (fieldname === 'referenceType') {
value = this.fyo.schemaMap[value]?.label ?? value;
}
row.cells.push({
2022-05-12 10:04:37 +00:00
italics: entry.name === -1,
bold: entry.name === -2,
value,
rawValue,
2022-05-12 10:04:37 +00:00
align,
width,
2022-05-12 10:04:37 +00:00
});
}
return row;
}
_consolidateEntries(map: GroupedMap) {
const entries: LedgerEntry[] = [];
for (const key of map.keys()) {
entries.push(...map.get(key)!);
/**
* Add blank row for spacing if groupBy
*/
if (this.groupBy !== 'none') {
this._pushBlankEntry(entries);
}
}
return entries;
}
_pushBlankEntry(entries: LedgerEntry[]) {
entries.push({
name: -3, // Empty
account: '',
date: null,
debit: null,
credit: null,
balance: null,
referenceType: '',
referenceName: '',
party: '',
reverted: false,
reverts: '',
});
}
_getTotalsAndSetBalance(map: GroupedMap) {
let totalDebit = 0;
let totalCredit = 0;
2022-05-12 10:04:37 +00:00
for (const key of map.keys()) {
let balance = 0;
let debit = 0;
let credit = 0;
2022-05-12 10:04:37 +00:00
for (const entry of map.get(key)!) {
debit += entry.debit!;
credit += entry.credit!;
2022-05-12 10:04:37 +00:00
const diff = entry.debit! - entry.credit!;
balance += diff;
2022-05-12 10:04:37 +00:00
entry.balance = balance;
}
/**
* Total row incase groupBy is used
*/
if (this.groupBy !== 'none') {
map.get(key)?.push({
name: -1, // Italics
account: t`Total`,
date: null,
debit,
credit,
balance: debit - credit,
2022-05-12 10:04:37 +00:00
referenceType: '',
referenceName: '',
party: '',
reverted: false,
reverts: '',
});
}
/**
* Total debit and credit for the final row
*/
totalDebit += debit;
totalCredit += credit;
2022-05-12 10:04:37 +00:00
}
return { totalDebit, totalCredit };
}
async _getQueryFilters(): Promise<QueryFilter> {
2022-05-12 10:04:37 +00:00
const filters: QueryFilter = {};
const stringFilters = ['account', 'party', 'referenceName'];
for (const sf of stringFilters) {
2022-05-12 10:04:37 +00:00
const value = this[sf];
if (value === undefined) {
continue;
}
filters[sf] = value as string;
}
if (this.referenceType !== 'All') {
filters.referenceType = this.referenceType as string;
}
if (this.toDate) {
filters.date ??= [];
(filters.date as string[]).push('<=', this.toDate as string);
}
if (this.fromDate) {
filters.date ??= [];
(filters.date as string[]).push('>=', this.fromDate as string);
}
if (!this.reverted) {
filters.reverted = false;
}
return filters;
}
getFilters() {
const refTypeOptions = [
{ label: t`All`, value: 'All' },
{ label: t`Sales Invoices`, value: 'SalesInvoice' },
{ label: t`Purchase Invoices`, value: 'PurchaseInvoice' },
{ label: t`Payments`, value: 'Payment' },
{ label: t`Journal Entries`, value: 'JournalEntry' },
];
2022-11-30 05:54:05 +00:00
if (!this.fyo.singles.AccountingSettings?.enableInventory) {
refTypeOptions.push(
{ label: t`Shipment`, value: 'Shipment' },
{ label: t`Purchase Receipt`, value: 'PurchaseReceipt' }
);
}
2022-05-12 10:04:37 +00:00
return [
{
fieldtype: 'Select',
options: refTypeOptions,
label: t`Ref Type`,
2022-05-12 10:04:37 +00:00
fieldname: 'referenceType',
placeholder: t`Ref Type`,
2022-05-12 10:04:37 +00:00
},
{
fieldtype: 'DynamicLink',
2022-11-10 16:45:33 +00:00
label: t`Ref. Name`,
2022-05-12 10:04:37 +00:00
references: 'referenceType',
placeholder: t`Ref Name`,
emptyMessage: t`Change Ref Type`,
2022-05-12 10:04:37 +00:00
fieldname: 'referenceName',
},
{
fieldtype: 'Link',
target: 'Account',
placeholder: t`Account`,
label: t`Account`,
fieldname: 'account',
},
{
fieldtype: 'Link',
target: 'Party',
label: t`Party`,
placeholder: t`Party`,
fieldname: 'party',
},
{
fieldtype: 'Date',
placeholder: t`From Date`,
label: t`From Date`,
fieldname: 'fromDate',
},
{
fieldtype: 'Date',
placeholder: t`To Date`,
label: t`To Date`,
fieldname: 'toDate',
},
{
fieldtype: 'Select',
2022-05-12 10:04:37 +00:00
label: t`Group By`,
fieldname: 'groupBy',
options: [
{ label: t`None`, value: 'none' },
{ label: t`Party`, value: 'party' },
{ label: t`Account`, value: 'account' },
{ label: t`Reference`, value: 'referenceName' },
],
},
{
fieldtype: 'Check',
label: t`Include Cancelled`,
fieldname: 'reverted',
},
{
fieldtype: 'Check',
label: t`Ascending Order`,
fieldname: 'ascending',
},
2022-05-12 10:04:37 +00:00
] as Field[];
}
getColumns(): ColumnField[] {
let columns = [
{
label: '#',
fieldtype: 'Int',
fieldname: 'index',
align: 'right',
width: 0.5,
},
2022-05-12 10:04:37 +00:00
{
label: t`Account`,
fieldtype: 'Link',
fieldname: 'account',
width: 1.5,
},
{
label: t`Date`,
fieldtype: 'Date',
fieldname: 'date',
},
{
label: t`Debit`,
fieldtype: 'Currency',
fieldname: 'debit',
align: 'right',
2022-05-12 10:04:37 +00:00
width: 1.25,
},
{
label: t`Credit`,
fieldtype: 'Currency',
fieldname: 'credit',
align: 'right',
2022-05-12 10:04:37 +00:00
width: 1.25,
},
{
label: t`Balance`,
fieldtype: 'Currency',
fieldname: 'balance',
align: 'right',
2022-05-12 10:04:37 +00:00
width: 1.25,
},
{
label: t`Party`,
fieldtype: 'Link',
fieldname: 'party',
2022-05-12 10:04:37 +00:00
},
{
label: t`Ref Name`,
2022-05-12 10:04:37 +00:00
fieldtype: 'Data',
fieldname: 'referenceName',
},
{
label: t`Ref Type`,
fieldtype: 'Data',
fieldname: 'referenceType',
2022-05-12 10:04:37 +00:00
},
{
label: t`Reverted`,
fieldtype: 'Check',
fieldname: 'reverted',
},
] as ColumnField[];
if (!this.reverted) {
columns = columns.filter((f) => f.fieldname !== 'reverted');
}
return columns;
}
}