Add manual salary entry
All checks were successful
Deploy to VPS / test (push) Successful in 1m50s
Deploy to VPS / deploy (push) Successful in 1m37s

This commit is contained in:
Carlos Escalante
2026-07-14 18:25:13 -06:00
parent 47cb1826a8
commit 17a0c63abe
4 changed files with 200 additions and 3 deletions

View File

@@ -0,0 +1,177 @@
import { useState } from 'react';
import { toast } from 'sonner';
import api from '@/lib/api';
import { formatLocalDatetime } from '@/lib/format';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import { Alert, AlertDescription } from '@/components/ui/alert';
import { AlertCircle } from 'lucide-react';
interface Props {
onClose: () => void;
onSaved: () => void;
}
export default function SalaryEntryDialog({ onClose, onSaved }: Props) {
const [form, setForm] = useState({
amount: '',
currency: 'CRC',
date: formatLocalDatetime(new Date()),
bank: 'BAC',
reference: '',
notes: '',
});
const [saving, setSaving] = useState(false);
const [error, setError] = useState('');
const handleSubmit = async (event: React.FormEvent) => {
event.preventDefault();
const amount = Number(form.amount);
if (!Number.isFinite(amount) || amount <= 0) {
setError('Ingresá un monto mayor que cero.');
return;
}
setSaving(true);
setError('');
try {
await api.post('/transactions/', {
amount,
currency: form.currency,
date: form.date,
bank: form.bank,
merchant: 'Salario',
transaction_type: 'SALARY',
source: 'TRANSFER',
reference: form.reference.trim() || null,
notes: form.notes.trim() || null,
});
toast.success('Salario registrado');
onSaved();
onClose();
} catch (err) {
const status = err && typeof err === 'object' && 'response' in err
? (err as { response: { status: number } }).response.status
: null;
setError(
status === 409
? 'Ya existe una transacción con ese comprobante.'
: 'No se pudo registrar el salario. Intentá de nuevo.',
);
} finally {
setSaving(false);
}
};
return (
<Dialog open onOpenChange={(open) => { if (!open) onClose(); }}>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>Registrar salario</DialogTitle>
<DialogDescription>
Guardá el depósito recibido mientras configurás una nueva automatización.
</DialogDescription>
</DialogHeader>
<form onSubmit={handleSubmit} className="space-y-5">
{error && (
<Alert variant="destructive">
<AlertCircle className="h-4 w-4" />
<AlertDescription>{error}</AlertDescription>
</Alert>
)}
<div className="grid grid-cols-2 gap-4">
<div className="space-y-2">
<Label htmlFor="salary-amount">Monto</Label>
<Input
id="salary-amount"
type="number"
min="0.01"
step="0.01"
inputMode="decimal"
value={form.amount}
onChange={(e) => setForm({ ...form, amount: e.target.value })}
placeholder="0.00"
autoFocus
required
/>
</div>
<div className="space-y-2">
<Label>Moneda</Label>
<Select value={form.currency} onValueChange={(value) => value && setForm({ ...form, currency: value })}>
<SelectTrigger><SelectValue /></SelectTrigger>
<SelectContent>
<SelectItem value="CRC">CRC ()</SelectItem>
<SelectItem value="USD">USD ($)</SelectItem>
<SelectItem value="EUR">EUR ()</SelectItem>
</SelectContent>
</Select>
</div>
<div className="col-span-2 space-y-2">
<Label htmlFor="salary-date">Fecha del depósito</Label>
<Input
id="salary-date"
type="datetime-local"
value={form.date}
onChange={(e) => setForm({ ...form, date: e.target.value })}
required
/>
</div>
<div className="space-y-2">
<Label>Banco</Label>
<Select value={form.bank} onValueChange={(value) => value && setForm({ ...form, bank: value })}>
<SelectTrigger><SelectValue /></SelectTrigger>
<SelectContent>
<SelectItem value="BAC">BAC</SelectItem>
<SelectItem value="BCR">BCR</SelectItem>
<SelectItem value="DAVIVIENDA">Davivienda</SelectItem>
</SelectContent>
</Select>
</div>
<div className="space-y-2">
<Label htmlFor="salary-reference">Comprobante</Label>
<Input
id="salary-reference"
value={form.reference}
onChange={(e) => setForm({ ...form, reference: e.target.value })}
placeholder="Opcional"
/>
</div>
<div className="col-span-2 space-y-2">
<Label htmlFor="salary-notes">Nota</Label>
<Input
id="salary-notes"
value={form.notes}
onChange={(e) => setForm({ ...form, notes: e.target.value })}
placeholder="Ej. Quincena de julio"
/>
</div>
</div>
<DialogFooter>
<Button type="button" variant="outline" onClick={onClose}>Cancelar</Button>
<Button type="submit" disabled={saving}>{saving ? 'Guardando…' : 'Registrar salario'}</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
);
}

View File

@@ -1,7 +1,7 @@
import { useMemo } from 'react';
import { useMemo, useState } from 'react';
import { useQuery } from '@tanstack/react-query';
import { type ColumnDef } from '@tanstack/react-table';
import { Landmark, RefreshCw, Download } from 'lucide-react';
import { Landmark, RefreshCw, Download, Plus } from 'lucide-react';
import { type Transaction, getSalarios, getSalariosSummary } from '@/lib/api';
import { formatAmount, formatDate } from '@/lib/format';
@@ -13,8 +13,10 @@ import { DataTableColumnHeader } from '@/components/ui/data-table-column-header'
import { Card, CardContent } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import SalaryEntryDialog from '@/components/SalaryEntryDialog';
export default function Salarios() {
const [entryOpen, setEntryOpen] = useState(false);
const query = useQuery({
queryKey: ['salarios'],
queryFn: async () => {
@@ -95,6 +97,10 @@ export default function Salarios() {
subtitle="Historial de depósitos salariales"
actions={
<>
<Button size="sm" onClick={() => setEntryOpen(true)}>
<Plus className="w-4 h-4 mr-2" aria-hidden="true" />
Registrar salario
</Button>
<Button
variant="outline"
size="sm"
@@ -152,6 +158,12 @@ export default function Salarios() {
</CardContent>
</Card>
)}
{entryOpen && (
<SalaryEntryDialog
onClose={() => setEntryOpen(false)}
onSaved={() => { void query.refetch(); }}
/>
)}
</div>
);
}

View File

@@ -3,6 +3,11 @@ import react from "@vitejs/plugin-react-oxc";
import tailwindcss from "@tailwindcss/vite";
import path from "path";
// Browser requests arrive at Vite, which then proxies them to the backend.
// Locally the backend is published on the host; Docker Compose supplies the
// service hostname instead (see docker-compose.yml).
const backendUrl = process.env.BACKEND_URL ?? "http://localhost:8001";
export default defineConfig({
plugins: [react(), tailwindcss()],
resolve: {
@@ -19,7 +24,7 @@ export default defineConfig({
},
// All other API calls → Python backend
"/api": {
target: "http://localhost:8001",
target: backendUrl,
changeOrigin: true,
},
},