feat: wip journal and general ledger dyanmic columns

This commit is contained in:
Ahmed Bouhuolia
2024-01-06 20:16:22 +02:00
parent c71836ec27
commit 79f3f1b63d
15 changed files with 615 additions and 214 deletions

View File

@@ -12,6 +12,7 @@ import {
import classNames from 'classnames';
import { DashboardActionsBar, FormattedMessage as T, Icon } from '@/components';
import { GeneralLedgerSheetExportMenu } from './components';
import { useGeneralLedgerContext } from './GeneralLedgerProvider';
import { compose } from '@/utils';
@@ -84,11 +85,18 @@ function GeneralLedgerActionsBar({
icon={<Icon icon="print-16" iconSize={16} />}
text={<T id={'print'} />}
/>
<Button
className={Classes.MINIMAL}
icon={<Icon icon="file-export-16" iconSize={16} />}
text={<T id={'export'} />}
/>
<Popover
content={<GeneralLedgerSheetExportMenu />}
interactionKind={PopoverInteractionKind.CLICK}
placement="bottom-start"
minimal
>
<Button
className={Classes.MINIMAL}
icon={<Icon icon="file-export-16" iconSize={16} />}
text={<T id={'export'} />}
/>
</Popover>
</NavbarGroup>
</DashboardActionsBar>
);

View File

@@ -10,7 +10,7 @@ const GeneralLedgerHeaderDimensionsPanelContext = React.createContext();
/**
* General Ledger Header Dimensions Panel provider.
* @returns
* @returns {JSX.Element}
*/
function GeneralLedgerHeaderDimensionsPanelProvider({ query, ...props }) {
// Features guard.

View File

@@ -29,6 +29,7 @@ function GeneralLedgerProvider({ query, ...props }) {
sheetRefresh: refetch,
isFetching,
isLoading,
httpRequest: requestQuery
};
return (
<FinancialReportPage name={'general-ledger-sheet'}>

View File

@@ -13,7 +13,7 @@ import {
} from '@/components';
import { useGeneralLedgerContext } from './GeneralLedgerProvider';
import { useGeneralLedgerTableColumns } from './components';
import { useGeneralLedgerTableColumns } from './dynamicColumns';
/**
* General ledger table.
@@ -21,7 +21,7 @@ import { useGeneralLedgerTableColumns } from './components';
export default function GeneralLedgerTable({ companyName }) {
// General ledger context.
const {
generalLedger: { tableRows, query },
generalLedger: { query, table },
isLoading,
} = useGeneralLedgerContext();
@@ -30,8 +30,8 @@ export default function GeneralLedgerTable({ companyName }) {
// Default expanded rows of general ledger table.
const expandedRows = useMemo(
() => defaultExpanderReducer(tableRows, 1),
[tableRows],
() => defaultExpanderReducer(table.rows, 1),
[table.rows],
);
return (
@@ -48,7 +48,7 @@ export default function GeneralLedgerTable({ companyName }) {
'this_report_does_not_contain_any_data_between_date_period',
)}
columns={columns}
data={tableRows}
data={table.rows}
rowClassNames={tableRowTypesToClassnames}
expanded={expandedRows}
virtualizedRows={true}

View File

@@ -1,107 +1,31 @@
// @ts-nocheck
import React from 'react';
import intl from 'react-intl-universal';
import { Button } from '@blueprintjs/core';
import { FormattedMessage as T, Icon, If } from '@/components';
import React, { useRef } from 'react';
import classNames from 'classnames';
import {
Button,
Classes,
Intent,
Menu,
MenuItem,
ProgressBar,
Text,
} from '@blueprintjs/core';
import {
FormattedMessage as T,
Icon,
If,
Stack,
AppToaster,
} from '@/components';
import { getColumnWidth } from '@/utils';
import { useGeneralLedgerContext } from './GeneralLedgerProvider';
import FinancialLoadingBar from '../FinancialLoadingBar';
import { FinancialComputeAlert } from '../FinancialReportPage';
import { Align } from '@/constants';
/**
* Retrieve the general ledger table columns.
*/
export function useGeneralLedgerTableColumns() {
// General ledger context.
const {
generalLedger: { tableRows },
} = useGeneralLedgerContext();
return React.useMemo(
() => [
{
Header: intl.get('date'),
accessor: 'date',
className: 'date',
width: 120,
},
{
Header: intl.get('account_name'),
accessor: 'name',
className: 'name',
textOverview: true,
},
{
Header: intl.get('transaction_type'),
accessor: 'reference_type_formatted',
className: 'transaction_type',
width: 125,
textOverview: true,
},
{
Header: intl.get('transaction_number'),
accessor: 'reference_id',
className: 'transaction_number',
width: 100,
textOverview: true,
},
{
Header: intl.get('description'),
accessor: 'note',
className: 'description',
textOverview: true,
},
{
Header: intl.get('credit'),
accessor: 'formatted_credit',
className: 'credit',
width: getColumnWidth(tableRows, 'formatted_credit', {
minWidth: 100,
magicSpacing: 10,
}),
textOverview: true,
align: Align.Right,
},
{
Header: intl.get('debit'),
accessor: 'formatted_debit',
className: 'debit',
width: getColumnWidth(tableRows, 'formatted_debit', {
minWidth: 100,
magicSpacing: 10,
}),
textOverview: true,
align: Align.Right,
},
{
Header: intl.get('amount'),
accessor: 'formatted_amount',
className: 'amount',
width: getColumnWidth(tableRows, 'formatted_amount', {
minWidth: 100,
magicSpacing: 10,
}),
textOverview: true,
align: Align.Right,
},
{
Header: intl.get('running_balance'),
accessor: 'formatted_running_balance',
className: 'running_balance',
width: getColumnWidth(tableRows, 'formatted_running_balance', {
minWidth: 100,
magicSpacing: 10,
}),
textOverview: true,
align: Align.Right,
},
],
[tableRows],
);
}
import {
useGeneralLedgerSheetCsvExport,
useGeneralLedgerSheetXlsxExport,
} from '@/hooks/query';
/**
* General ledger sheet alerts.
@@ -144,3 +68,93 @@ export function GeneralLedgerSheetLoadingBar() {
</If>
);
}
/**
* Renders the G/L sheet export menu.
* @returns {JSX.Element}
*/
export const GeneralLedgerSheetExportMenu = () => {
const toastKey = useRef(null);
const commonToastConfig = {
isCloseButtonShown: true,
timeout: 2000,
};
const { httpRequest } = useGeneralLedgerContext();
const openProgressToast = (amount: number) => {
return (
<Stack spacing={8}>
<Text>The report has been exported successfully.</Text>
<ProgressBar
className={classNames('toast-progress', {
[Classes.PROGRESS_NO_STRIPES]: amount >= 100,
})}
intent={amount < 100 ? Intent.PRIMARY : Intent.SUCCESS}
value={amount / 100}
/>
</Stack>
);
};
// Export the report to xlsx.
const { mutateAsync: xlsxExport } = useGeneralLedgerSheetXlsxExport(
httpRequest,
{
onDownloadProgress: (xlsxExportProgress: number) => {
if (!toastKey.current) {
toastKey.current = AppToaster.show({
message: openProgressToast(xlsxExportProgress),
...commonToastConfig,
});
} else {
AppToaster.show(
{
message: openProgressToast(xlsxExportProgress),
...commonToastConfig,
},
toastKey.current,
);
}
},
},
);
// Export the report to csv.
const { mutateAsync: csvExport } = useGeneralLedgerSheetCsvExport(
httpRequest,
{
onDownloadProgress: (xlsxExportProgress: number) => {
if (!toastKey.current) {
toastKey.current = AppToaster.show({
message: openProgressToast(xlsxExportProgress),
...commonToastConfig,
});
} else {
AppToaster.show(
{
message: openProgressToast(xlsxExportProgress),
...commonToastConfig,
},
toastKey.current,
);
}
},
},
);
// Handle csv export button click.
const handleCsvExportBtnClick = () => {
csvExport().then(() => {});
};
// Handle xlsx export button click.
const handleXlsxExportBtnClick = () => {
xlsxExport().then(() => {});
};
return (
<Menu>
<MenuItem
text={'XLSX (Microsoft Excel)'}
onClick={handleXlsxExportBtnClick}
/>
<MenuItem text={'CSV'} onClick={handleCsvExportBtnClick} />
</Menu>
);
};

View File

@@ -0,0 +1,117 @@
// @ts-nocheck
import { getColumnWidth } from '@/utils';
import * as R from 'ramda';
import { useGeneralLedgerContext } from './GeneralLedgerProvider';
import { Align } from '@/constants';
const getTableCellValueAccessor = (index) => `cells[${index}].value`;
const getReportColWidth = (data, accessor, headerText) => {
return getColumnWidth(
data,
accessor,
{ magicSpacing: 10, minWidth: 100 },
headerText,
);
};
/**
* Account name column mapper.
*/
const commonColumnMapper = R.curry((data, column) => {
const accessor = getTableCellValueAccessor(column.cell_index);
return {
key: column.key,
Header: column.label,
accessor,
className: column.key,
textOverview: true,
};
});
/**
* Numeric columns accessor.
*/
const numericColumnAccessor = R.curry((data, column) => {
const accessor = getTableCellValueAccessor(column.cell_index);
const width = getReportColWidth(data, accessor, column.label);
return {
...column,
align: Align.Right,
width,
};
});
/**
* Date column accessor.
*/
const dateColumnAccessor = R.curry((column) => {
return {
...column,
width: 120,
};
});
/**
* Transaction type column accessor.
*/
const transactionTypeColumnAccessor = (column) => {
return {
...column,
width: 125,
};
};
/**
* Transaction number column accessor.
*/
const transactionIdColumnAccessor = (column) => {
return {
...column,
width: 100,
};
};
const dynamiColumnMapper = R.curry((data, column) => {
const _numericColumnAccessor = numericColumnAccessor(data);
return R.compose(
R.when(R.pathEq(['key'], 'date'), dateColumnAccessor),
R.when(
R.pathEq(['key'], 'reference_type'),
transactionTypeColumnAccessor,
),
R.when(
R.pathEq(['key'], 'reference_number'),
transactionIdColumnAccessor,
),
R.when(R.pathEq(['key'], 'credit'), _numericColumnAccessor),
R.when(R.pathEq(['key'], 'debit'), _numericColumnAccessor),
R.when(R.pathEq(['key'], 'amount'), _numericColumnAccessor),
R.when(R.pathEq(['key'], 'running_balance'), _numericColumnAccessor),
commonColumnMapper(data),
)(column);
});
/**
* Composes the dynamic columns that fetched from request to columns to table component.
*/
export const dynamicColumns = R.curry((data, columns) => {
return R.map(dynamiColumnMapper(data), columns);
});
/**
* Retrieves the G/L sheet table columns for table component.
*/
export const useGeneralLedgerTableColumns = () => {
const { generalLedger } = useGeneralLedgerContext();
if (!generalLedger) {
throw new Error('asdfadsf');
}
const { table } = generalLedger;
return dynamicColumns(table.rows, table.columns);
};