Add variable expense actuals logging
API for expense categories and actuals with full CRUD. Paycheck view shows actuals section and includes them in remaining balance calculation. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -22,7 +22,94 @@ function formatPayDate(dateStr) {
|
||||
return `${MONTH_NAMES[month - 1]} ${day}, ${year}`;
|
||||
}
|
||||
|
||||
function PaycheckColumn({ paycheck, onBillPaidToggle }) {
|
||||
function todayISO() {
|
||||
return new Date().toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
function PaycheckColumn({ paycheck, onBillPaidToggle, categories }) {
|
||||
const [actuals, setActuals] = useState([]);
|
||||
const [actualsLoading, setActualsLoading] = useState(false);
|
||||
const [actualsError, setActualsError] = useState(null);
|
||||
|
||||
const [formCategoryId, setFormCategoryId] = useState('');
|
||||
const [formAmount, setFormAmount] = useState('');
|
||||
const [formNote, setFormNote] = useState('');
|
||||
const [formDate, setFormDate] = useState(todayISO());
|
||||
const [formSubmitting, setFormSubmitting] = useState(false);
|
||||
const [formError, setFormError] = useState(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!paycheck) return;
|
||||
loadActuals(paycheck.id);
|
||||
}, [paycheck?.id]);
|
||||
|
||||
async function loadActuals(paycheckId) {
|
||||
setActualsLoading(true);
|
||||
setActualsError(null);
|
||||
try {
|
||||
const res = await fetch(`/api/actuals?paycheck_id=${paycheckId}`);
|
||||
if (!res.ok) throw new Error(`Server error: ${res.status}`);
|
||||
const data = await res.json();
|
||||
setActuals(data);
|
||||
} catch (err) {
|
||||
setActualsError(err.message);
|
||||
} finally {
|
||||
setActualsLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleAddActual(e) {
|
||||
e.preventDefault();
|
||||
if (!formAmount) {
|
||||
setFormError('Amount is required');
|
||||
return;
|
||||
}
|
||||
|
||||
setFormSubmitting(true);
|
||||
setFormError(null);
|
||||
try {
|
||||
const res = await fetch('/api/actuals', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
paycheck_id: paycheck.id,
|
||||
category_id: formCategoryId || null,
|
||||
amount: parseFloat(formAmount),
|
||||
note: formNote || null,
|
||||
date: formDate || todayISO(),
|
||||
}),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const body = await res.json();
|
||||
throw new Error(body.error || `Server error: ${res.status}`);
|
||||
}
|
||||
// Refresh the actuals list
|
||||
await loadActuals(paycheck.id);
|
||||
// Reset form fields (keep date as today)
|
||||
setFormCategoryId('');
|
||||
setFormAmount('');
|
||||
setFormNote('');
|
||||
setFormDate(todayISO());
|
||||
} catch (err) {
|
||||
setFormError(err.message);
|
||||
} finally {
|
||||
setFormSubmitting(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleDeleteActual(id) {
|
||||
try {
|
||||
const res = await fetch(`/api/actuals/${id}`, { method: 'DELETE' });
|
||||
if (!res.ok) {
|
||||
const body = await res.json();
|
||||
throw new Error(body.error || `Server error: ${res.status}`);
|
||||
}
|
||||
setActuals(prev => prev.filter(a => a.id !== id));
|
||||
} catch (err) {
|
||||
alert(`Failed to delete actual: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (!paycheck) {
|
||||
return (
|
||||
<div style={styles.column}>
|
||||
@@ -34,7 +121,8 @@ function PaycheckColumn({ paycheck, onBillPaidToggle }) {
|
||||
const net = parseFloat(paycheck.net) || 0;
|
||||
const billsTotal = paycheck.bills.reduce((sum, b) => sum + (parseFloat(b.effective_amount) || 0), 0);
|
||||
const otesTotal = paycheck.one_time_expenses.reduce((sum, e) => sum + (parseFloat(e.amount) || 0), 0);
|
||||
const remaining = net - billsTotal - otesTotal;
|
||||
const actualsTotal = actuals.reduce((sum, a) => sum + (parseFloat(a.amount) || 0), 0);
|
||||
const remaining = net - billsTotal - otesTotal - actualsTotal;
|
||||
const remainingColor = remaining >= 0 ? '#2a7a2a' : '#c0392b';
|
||||
|
||||
return (
|
||||
@@ -100,6 +188,88 @@ function PaycheckColumn({ paycheck, onBillPaidToggle }) {
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div style={styles.section}>
|
||||
<div style={styles.sectionLabel}>Variable Spending</div>
|
||||
<div style={styles.divider} />
|
||||
|
||||
{actualsLoading && <div style={styles.emptyNote}>Loading…</div>}
|
||||
{actualsError && <div style={styles.actualsError}>Error: {actualsError}</div>}
|
||||
|
||||
{!actualsLoading && actuals.length === 0 && (
|
||||
<div style={styles.emptyNote}>(none)</div>
|
||||
)}
|
||||
|
||||
{actuals.map((actual) => (
|
||||
<div key={actual.id} style={styles.actualRow}>
|
||||
<div style={styles.actualMain}>
|
||||
<span style={styles.actualCategory}>
|
||||
{actual.category_name || <em style={{ color: '#aaa' }}>Uncategorized</em>}
|
||||
</span>
|
||||
<span style={styles.actualAmount}>{formatCurrency(actual.amount)}</span>
|
||||
</div>
|
||||
<div style={styles.actualMeta}>
|
||||
{actual.note && <span style={styles.actualNote}>{actual.note}</span>}
|
||||
<span style={styles.actualDate}>{actual.date}</span>
|
||||
<button
|
||||
style={styles.deleteButton}
|
||||
onClick={() => handleDeleteActual(actual.id)}
|
||||
title="Remove"
|
||||
>
|
||||
×
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
|
||||
<form onSubmit={handleAddActual} style={styles.actualForm}>
|
||||
<div style={styles.actualFormRow}>
|
||||
<select
|
||||
value={formCategoryId}
|
||||
onChange={e => setFormCategoryId(e.target.value)}
|
||||
style={styles.formSelect}
|
||||
>
|
||||
<option value="">— Category —</option>
|
||||
{categories.map(cat => (
|
||||
<option key={cat.id} value={cat.id}>{cat.name}</option>
|
||||
))}
|
||||
</select>
|
||||
<input
|
||||
type="number"
|
||||
placeholder="Amount"
|
||||
value={formAmount}
|
||||
onChange={e => setFormAmount(e.target.value)}
|
||||
step="0.01"
|
||||
min="0"
|
||||
style={styles.formInput}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div style={styles.actualFormRow}>
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Note (optional)"
|
||||
value={formNote}
|
||||
onChange={e => setFormNote(e.target.value)}
|
||||
style={{ ...styles.formInput, flex: 2 }}
|
||||
/>
|
||||
<input
|
||||
type="date"
|
||||
value={formDate}
|
||||
onChange={e => setFormDate(e.target.value)}
|
||||
style={styles.formInput}
|
||||
/>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={formSubmitting}
|
||||
style={styles.addButton}
|
||||
>
|
||||
Add
|
||||
</button>
|
||||
</div>
|
||||
{formError && <div style={styles.formError}>{formError}</div>}
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div style={styles.remainingRow}>
|
||||
<span style={styles.remainingLabel}>Remaining:</span>
|
||||
<span style={{ ...styles.remainingAmount, color: remainingColor }}>
|
||||
@@ -117,11 +287,16 @@ function PaycheckView() {
|
||||
const [paychecks, setPaychecks] = useState([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState(null);
|
||||
const [categories, setCategories] = useState([]);
|
||||
|
||||
useEffect(() => {
|
||||
loadPaychecks(year, month);
|
||||
}, [year, month]);
|
||||
|
||||
useEffect(() => {
|
||||
loadCategories();
|
||||
}, []);
|
||||
|
||||
async function loadPaychecks(y, m) {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
@@ -137,6 +312,17 @@ function PaycheckView() {
|
||||
}
|
||||
}
|
||||
|
||||
async function loadCategories() {
|
||||
try {
|
||||
const res = await fetch('/api/expense-categories');
|
||||
if (!res.ok) throw new Error(`Server error: ${res.status}`);
|
||||
const data = await res.json();
|
||||
setCategories(data);
|
||||
} catch (err) {
|
||||
console.error('Failed to load expense categories:', err.message);
|
||||
}
|
||||
}
|
||||
|
||||
function prevMonth() {
|
||||
if (month === 1) {
|
||||
setYear(y => y - 1);
|
||||
@@ -222,8 +408,16 @@ function PaycheckView() {
|
||||
<div style={styles.loadingMsg}>Loading...</div>
|
||||
) : (
|
||||
<div style={styles.grid}>
|
||||
<PaycheckColumn paycheck={pc1} onBillPaidToggle={handleBillPaidToggle} />
|
||||
<PaycheckColumn paycheck={pc2} onBillPaidToggle={handleBillPaidToggle} />
|
||||
<PaycheckColumn
|
||||
paycheck={pc1}
|
||||
onBillPaidToggle={handleBillPaidToggle}
|
||||
categories={categories}
|
||||
/>
|
||||
<PaycheckColumn
|
||||
paycheck={pc2}
|
||||
onBillPaidToggle={handleBillPaidToggle}
|
||||
categories={categories}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
@@ -376,6 +570,97 @@ const styles = {
|
||||
fontVariantNumeric: 'tabular-nums',
|
||||
color: '#333',
|
||||
},
|
||||
// Actuals styles
|
||||
actualsError: {
|
||||
color: '#c0392b',
|
||||
fontSize: '0.85rem',
|
||||
marginBottom: '0.4rem',
|
||||
},
|
||||
actualRow: {
|
||||
marginBottom: '0.5rem',
|
||||
fontSize: '0.9rem',
|
||||
},
|
||||
actualMain: {
|
||||
display: 'flex',
|
||||
justifyContent: 'space-between',
|
||||
alignItems: 'baseline',
|
||||
},
|
||||
actualCategory: {
|
||||
fontWeight: '500',
|
||||
color: '#333',
|
||||
},
|
||||
actualAmount: {
|
||||
fontVariantNumeric: 'tabular-nums',
|
||||
color: '#333',
|
||||
marginLeft: '0.5rem',
|
||||
},
|
||||
actualMeta: {
|
||||
display: 'flex',
|
||||
gap: '0.5rem',
|
||||
alignItems: 'center',
|
||||
fontSize: '0.8rem',
|
||||
color: '#888',
|
||||
marginTop: '1px',
|
||||
},
|
||||
actualNote: {
|
||||
fontStyle: 'italic',
|
||||
flex: 1,
|
||||
},
|
||||
actualDate: {
|
||||
whiteSpace: 'nowrap',
|
||||
},
|
||||
deleteButton: {
|
||||
background: 'none',
|
||||
border: 'none',
|
||||
cursor: 'pointer',
|
||||
color: '#c0392b',
|
||||
fontSize: '1rem',
|
||||
lineHeight: '1',
|
||||
padding: '0 2px',
|
||||
opacity: 0.7,
|
||||
},
|
||||
actualForm: {
|
||||
marginTop: '0.75rem',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
gap: '0.4rem',
|
||||
},
|
||||
actualFormRow: {
|
||||
display: 'flex',
|
||||
gap: '0.4rem',
|
||||
flexWrap: 'wrap',
|
||||
},
|
||||
formSelect: {
|
||||
flex: 1,
|
||||
minWidth: '100px',
|
||||
fontSize: '0.85rem',
|
||||
padding: '0.3rem 0.4rem',
|
||||
border: '1px solid #ccc',
|
||||
borderRadius: '4px',
|
||||
},
|
||||
formInput: {
|
||||
flex: 1,
|
||||
minWidth: '70px',
|
||||
fontSize: '0.85rem',
|
||||
padding: '0.3rem 0.4rem',
|
||||
border: '1px solid #ccc',
|
||||
borderRadius: '4px',
|
||||
},
|
||||
addButton: {
|
||||
padding: '0.3rem 0.75rem',
|
||||
fontSize: '0.85rem',
|
||||
cursor: 'pointer',
|
||||
border: '1px solid #bbb',
|
||||
borderRadius: '4px',
|
||||
background: '#e8f0e8',
|
||||
color: '#2a7a2a',
|
||||
fontWeight: '600',
|
||||
whiteSpace: 'nowrap',
|
||||
},
|
||||
formError: {
|
||||
color: '#c0392b',
|
||||
fontSize: '0.8rem',
|
||||
},
|
||||
remainingRow: {
|
||||
display: 'flex',
|
||||
justifyContent: 'space-between',
|
||||
|
||||
Reference in New Issue
Block a user