Fix initial balance journal entries and add investment account creation

- Fix account creation initial balance bug: the INSERT RETURNING result
  was not being unwrapped correctly from TypeORM's array response, causing
  account.id to be undefined and journal entry lines to have $0 amounts.
  Now uses findOne() after insert for reliable ID retrieval.
- Add missing balancing equity entry line (debit/credit to account 3000/3100)
  so opening balance journal entries are proper double-entry
- Fix account update to use findOne() instead of RETURNING * for consistent
  response format
- Add investment account creation to Accounts page: account type dropdown now
  shows a separator followed by investment types (CD, Money Market, Treasury,
  Savings, Brokerage). Selecting an investment type expands the modal to show
  investment-specific fields (institution, principal, interest rate, purchase
  date, maturity date, account last 4, notes) and posts to /investment-accounts

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-19 15:13:22 -05:00
parent 301f8a7bde
commit b0634f7263
2 changed files with 202 additions and 46 deletions

View File

@@ -20,6 +20,8 @@ import {
Tooltip,
SimpleGrid,
Alert,
Divider,
Textarea,
} from '@mantine/core';
import { DateInput } from '@mantine/dates';
import { useForm } from '@mantine/form';
@@ -39,6 +41,24 @@ import {
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import api from '../../services/api';
const INVESTMENT_TYPES = ['inv_cd', 'inv_money_market', 'inv_treasury', 'inv_savings', 'inv_brokerage'];
const isInvestmentType = (t: string) => INVESTMENT_TYPES.includes(t);
const ACCOUNT_TYPE_OPTIONS = [
{ value: 'asset', label: 'Asset' },
{ value: 'liability', label: 'Liability' },
{ value: 'equity', label: 'Equity' },
{ value: 'income', label: 'Income' },
{ value: 'expense', label: 'Expense' },
{ value: '__divider', label: '── Investment Accounts ──', disabled: true },
{ value: 'inv_cd', label: 'Investment — CD' },
{ value: 'inv_money_market', label: 'Investment — Money Market' },
{ value: 'inv_treasury', label: 'Investment — Treasury' },
{ value: 'inv_savings', label: 'Investment — Savings' },
{ value: 'inv_brokerage', label: 'Investment — Brokerage' },
];
interface Account {
id: string;
account_number: number;
@@ -142,10 +162,19 @@ export function AccountsPage() {
fundType: 'operating',
is1099Reportable: false,
initialBalance: 0,
// Investment fields
institution: '',
accountNumberLast4: '',
principal: 0,
interestRate: 0,
maturityDate: null as Date | null,
purchaseDate: null as Date | null,
investmentNotes: '',
},
validate: {
accountNumber: (v) => (v > 0 ? null : 'Required'),
accountNumber: (v, values) => isInvestmentType(values.accountType) ? null : (v > 0 ? null : 'Required'),
name: (v) => (v.length > 0 ? null : 'Required'),
principal: (v, values) => isInvestmentType(values.accountType) ? (v > 0 ? null : 'Required') : null,
},
});
@@ -168,11 +197,33 @@ export function AccountsPage() {
if (editing) {
return api.put(`/accounts/${editing.id}`, values);
}
// Investment account creation
if (isInvestmentType(values.accountType)) {
const investmentTypeMap: Record<string, string> = {
inv_cd: 'cd', inv_money_market: 'money_market', inv_treasury: 'treasury',
inv_savings: 'savings', inv_brokerage: 'other',
};
return api.post('/investment-accounts', {
name: values.name,
institution: values.institution || null,
account_number_last4: values.accountNumberLast4 || null,
investment_type: investmentTypeMap[values.accountType] || 'other',
fund_type: values.fundType,
principal: values.principal,
interest_rate: values.interestRate || 0,
maturity_date: values.maturityDate ? values.maturityDate.toISOString().split('T')[0] : null,
purchase_date: values.purchaseDate ? values.purchaseDate.toISOString().split('T')[0] : null,
current_value: values.principal,
notes: values.investmentNotes || null,
});
}
return api.post('/accounts', values);
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['accounts'] });
notifications.show({ message: editing ? 'Account updated' : 'Account created', color: 'green' });
queryClient.invalidateQueries({ queryKey: ['investments'] });
const isInv = isInvestmentType(form.values.accountType);
notifications.show({ message: editing ? 'Account updated' : (isInv ? 'Investment created' : 'Account created'), color: 'green' });
close();
setEditing(null);
form.reset();
@@ -421,47 +472,135 @@ export function AccountsPage() {
</Tabs>
{/* Create / Edit Account Modal */}
<Modal opened={opened} onClose={close} title={editing ? 'Edit Account' : 'New Account'} size="md">
<Modal
opened={opened}
onClose={close}
title={editing ? 'Edit Account' : isInvestmentType(form.values.accountType) ? 'New Investment Account' : 'New Account'}
size="md"
>
<form onSubmit={form.onSubmit((values) => createMutation.mutate(values))}>
<Stack>
<NumberInput label="Account Number" required {...form.getInputProps('accountNumber')} />
<TextInput label="Account Name" required {...form.getInputProps('name')} />
<TextInput label="Description" {...form.getInputProps('description')} />
<Group grow>
{!editing && (
<Select
label="Account Type"
required
data={[
{ value: 'asset', label: 'Asset' },
{ value: 'liability', label: 'Liability' },
{ value: 'equity', label: 'Equity' },
{ value: 'income', label: 'Income' },
{ value: 'expense', label: 'Expense' },
]}
data={ACCOUNT_TYPE_OPTIONS}
{...form.getInputProps('accountType')}
/>
<Select
label="Fund Type"
required
data={[
{ value: 'operating', label: 'Operating' },
{ value: 'reserve', label: 'Reserve' },
]}
{...form.getInputProps('fundType')}
/>
</Group>
<Switch label="1099 Reportable" {...form.getInputProps('is1099Reportable', { type: 'checkbox' })} />
{!editing && (
<NumberInput
label="Initial Balance"
description="Opening balance (creates a journal entry)"
prefix="$"
decimalScale={2}
{...form.getInputProps('initialBalance')}
/>
)}
<TextInput
label={isInvestmentType(form.values.accountType) ? 'Investment Name' : 'Account Name'}
placeholder={isInvestmentType(form.values.accountType) ? 'e.g. Reserve CD - 12 Month' : 'e.g. Operating Cash'}
required
{...form.getInputProps('name')}
/>
{!isInvestmentType(form.values.accountType) && (
<>
<NumberInput label="Account Number" required {...form.getInputProps('accountNumber')} />
<TextInput label="Description" {...form.getInputProps('description')} />
{editing && (
<Select
label="Account Type"
required
data={[
{ value: 'asset', label: 'Asset' },
{ value: 'liability', label: 'Liability' },
{ value: 'equity', label: 'Equity' },
{ value: 'income', label: 'Income' },
{ value: 'expense', label: 'Expense' },
]}
{...form.getInputProps('accountType')}
/>
)}
</>
)}
<Select
label="Fund Type"
required
data={[
{ value: 'operating', label: 'Operating' },
{ value: 'reserve', label: 'Reserve' },
]}
{...form.getInputProps('fundType')}
/>
{/* Investment-specific fields */}
{!editing && isInvestmentType(form.values.accountType) && (
<>
<Divider label="Investment Details" labelPosition="center" />
<TextInput
label="Institution"
placeholder="e.g. First National Bank"
{...form.getInputProps('institution')}
/>
<Group grow>
<NumberInput
label="Principal Amount"
required
prefix="$"
decimalScale={2}
thousandSeparator=","
min={0}
{...form.getInputProps('principal')}
/>
<NumberInput
label="Interest Rate (%)"
decimalScale={4}
suffix="%"
min={0}
max={100}
{...form.getInputProps('interestRate')}
/>
</Group>
<Group grow>
<DateInput
label="Purchase Date"
clearable
{...form.getInputProps('purchaseDate')}
/>
<DateInput
label="Maturity Date"
clearable
{...form.getInputProps('maturityDate')}
/>
</Group>
<TextInput
label="Account # (last 4)"
placeholder="1234"
maxLength={4}
{...form.getInputProps('accountNumberLast4')}
/>
<Textarea
label="Notes"
autosize
minRows={2}
maxRows={4}
{...form.getInputProps('investmentNotes')}
/>
</>
)}
{/* Regular account fields */}
{!isInvestmentType(form.values.accountType) && (
<>
<Switch label="1099 Reportable" {...form.getInputProps('is1099Reportable', { type: 'checkbox' })} />
{!editing && (
<NumberInput
label="Initial Balance"
description="Opening balance (creates a journal entry)"
prefix="$"
decimalScale={2}
{...form.getInputProps('initialBalance')}
/>
)}
</>
)}
<Button type="submit" loading={createMutation.isPending}>
{editing ? 'Update' : 'Create'}
{editing ? 'Update' : isInvestmentType(form.values.accountType) ? 'Create Investment' : 'Create Account'}
</Button>
</Stack>
</form>