186 lines
5.0 KiB
TypeScript

import { Checkbox, Input, Typography } from 'antd';
import type { ColumnsType } from 'antd/es/table';
import type { GoodwillRow, GoodwillTreeRow } from '../../../types/goodwill/goodwill';
import { chessTableCellFormatValue } from '../../../utils/chessTableCellFormatValue';
const { Text } = Typography;
interface GetColumnsParams {
units: string;
decimals: number;
isEditMode: boolean;
editedRows: Record<string, Partial<GoodwillRow>>;
onRowEdit: (rowId: string, field: keyof GoodwillRow, value: any) => void;
}
export const getGoodwillColumns = ({
units,
decimals,
isEditMode,
editedRows,
onRowEdit,
}: GetColumnsParams): ColumnsType<GoodwillTreeRow> => {
const formatMoney = (val: string | undefined) => {
if (!val || val === '0' || val === '0.00') return '—';
return chessTableCellFormatValue(val, units, decimals);
};
return [
{
title: 'Учет в отчете',
key: 'include_in_kfo',
width: 120,
align: 'center',
render: (_, row) => {
const isChecked = row.isGroup ? row.include_in_kfo : (editedRows[row.key]?.include_in_kfo ?? row.include_in_kfo);
return (
<Checkbox disabled={!isEditMode} checked={isChecked} onChange={(e) => onRowEdit(row.key, 'include_in_kfo', e.target.checked)} />
);
},
},
{
title: 'Наименование дочерней организации',
key: 'name',
width: 300,
render: (_, row) => (row.isGroup ? <Text strong>{row.head_legal_entity.name}</Text> : row.subsidiary_legal_entity.name),
},
{
title: 'Доля владения',
key: 'ownership_share',
width: 150,
align: 'right',
onCell: (row) => {
if (row.isGroup) return {};
const isChecked = editedRows[row.key]?.include_in_kfo ?? row.include_in_kfo;
if (!isChecked) return {};
const val = editedRows[row.key]?.ownership_share ?? row.ownership_share;
const isShareEmpty = !val || String(val).trim() === '';
const isInvalid = isShareEmpty;
return {
style: {
backgroundColor: isInvalid ? '#fffbe6' : '#f6ffed',
},
};
},
render: (_, row) => {
if (row.isGroup) return null;
const val = editedRows[row.key]?.ownership_share ?? row.ownership_share;
const isChecked = editedRows[row.key]?.include_in_kfo ?? row.include_in_kfo;
if (isEditMode && isChecked) {
return (
<Input
type='number'
min={0}
max={100}
step={1}
style={{ width: '100%' }}
value={val ? Math.floor(Number(val)) : ''}
onChange={(e) => {
const intValue = e.target.value.replace(/\D/g, '');
onRowEdit(row.key, 'ownership_share', intValue);
}}
placeholder='%'
/>
);
}
return val ? `${Math.floor(Number(val))}%` : '—';
},
},
{
title: 'Фондовые вложения головной организации',
dataIndex: 'financial_investment_amount',
key: 'financial_investment_amount',
width: 150,
align: 'right',
render: (val, row) => (row.isGroup ? null : formatMoney(val)),
},
{
title: 'Доля участия головной организации в уставном капитале',
dataIndex: 'participation_amount',
key: 'participation_amount',
width: 150,
align: 'right',
render: (val, row) => (row.isGroup ? null : formatMoney(val)),
},
{
title: 'Уставный капитал',
dataIndex: 'charter_capital_amount',
key: 'charter_capital_amount',
width: 150,
align: 'right',
render: (val, row) => (row.isGroup ? null : formatMoney(val)),
},
{
title: 'Гудвилл (+/-)',
dataIndex: 'goodwill_amount',
key: 'goodwill_amount',
width: 150,
align: 'right',
render: (val, row) => {
if (row.isGroup) return null;
const formatted = formatMoney(val);
if (formatted === '—') return <Text type='secondary'>—</Text>;
const isNegative = String(val).trim().startsWith('-');
return (
<Text strong style={{ color: isNegative ? '#cf1322' : '#3f8600' }}>
{formatted}
</Text>
);
},
},
{
title: 'Комментарий',
key: 'comment',
width: 200,
onCell: (row) => {
if (row.isGroup) return {};
const isChecked = editedRows[row.key]?.include_in_kfo ?? row.include_in_kfo;
if (!isChecked) return {};
const comment = editedRows[row.key]?.comment ?? row.comment;
const isCommentEmpty = !comment || String(comment).trim() === '';
const isInvalid = isCommentEmpty;
return {
style: {
backgroundColor: isInvalid ? '#fffbe6' : '#f6ffed',
},
};
},
render: (_, row) => {
if (row.isGroup) return null;
const val = editedRows[row.key]?.comment ?? row.comment;
const isChecked = editedRows[row.key]?.include_in_kfo ?? row.include_in_kfo;
if (isEditMode && isChecked) {
return (
<Input
style={{ width: '100%' }}
value={val ?? ''}
onChange={(e) => onRowEdit(row.key, 'comment', e.target.value)}
placeholder='Комментарий'
/>
);
}
return val || '—';
},
},
];
};