2025-07-10 17:20:48 +02:00
|
|
|
|
import { Button } from '@/components/ui/button';
|
|
|
|
|
|
import { Input } from '@/components/ui/input';
|
|
|
|
|
|
import { Label } from '@/components/ui/label';
|
|
|
|
|
|
import InputError from '@/components/InputError';
|
|
|
|
|
|
import { useForm } from '@inertiajs/react';
|
|
|
|
|
|
import { LoaderCircle } from 'lucide-react';
|
|
|
|
|
|
import { FormEventHandler, useEffect } from 'react';
|
2025-07-13 04:06:37 +02:00
|
|
|
|
import ComponentTitle from '@/components/ui/ComponentTitle';
|
2025-07-10 17:20:48 +02:00
|
|
|
|
|
|
|
|
|
|
interface PurchaseFormData {
|
|
|
|
|
|
date: string;
|
|
|
|
|
|
shares: string;
|
|
|
|
|
|
price_per_share: string;
|
|
|
|
|
|
total_cost: string;
|
2025-07-10 18:04:58 +02:00
|
|
|
|
[key: string]: string;
|
2025-07-10 17:20:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-10 18:04:58 +02:00
|
|
|
|
interface AddPurchaseFormProps {
|
|
|
|
|
|
onSuccess?: () => void;
|
2025-07-13 04:06:37 +02:00
|
|
|
|
onCancel?: () => void;
|
2025-07-10 18:04:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-13 04:06:37 +02:00
|
|
|
|
export default function AddPurchaseForm({ onSuccess, onCancel }: AddPurchaseFormProps) {
|
2025-07-10 17:20:48 +02:00
|
|
|
|
const { data, setData, post, processing, errors, reset } = useForm<PurchaseFormData>({
|
|
|
|
|
|
date: new Date().toISOString().split('T')[0], // Today's date in YYYY-MM-DD format
|
|
|
|
|
|
shares: '',
|
|
|
|
|
|
price_per_share: '',
|
|
|
|
|
|
total_cost: '',
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Auto-calculate total cost when shares or price changes
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
if (data.shares && data.price_per_share) {
|
|
|
|
|
|
const shares = parseFloat(data.shares);
|
|
|
|
|
|
const pricePerShare = parseFloat(data.price_per_share);
|
2025-07-13 04:06:37 +02:00
|
|
|
|
|
2025-07-10 17:20:48 +02:00
|
|
|
|
if (!isNaN(shares) && !isNaN(pricePerShare)) {
|
|
|
|
|
|
const totalCost = (shares * pricePerShare).toFixed(2);
|
|
|
|
|
|
setData('total_cost', totalCost);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [data.shares, data.price_per_share, setData]);
|
|
|
|
|
|
|
|
|
|
|
|
const submit: FormEventHandler = (e) => {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
|
|
|
|
post(route('purchases.store'), {
|
|
|
|
|
|
onSuccess: () => {
|
|
|
|
|
|
reset();
|
|
|
|
|
|
setData('date', new Date().toISOString().split('T')[0]);
|
2025-07-10 18:04:58 +02:00
|
|
|
|
if (onSuccess) {
|
|
|
|
|
|
onSuccess();
|
|
|
|
|
|
}
|
2025-07-10 17:20:48 +02:00
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
2025-07-13 04:06:37 +02:00
|
|
|
|
<div className="w-full">
|
2025-07-10 18:04:58 +02:00
|
|
|
|
<div className="space-y-4">
|
2025-07-13 04:06:37 +02:00
|
|
|
|
<ComponentTitle>ADD PURCHASE</ComponentTitle>
|
2025-07-10 17:20:48 +02:00
|
|
|
|
<form onSubmit={submit} className="space-y-4">
|
|
|
|
|
|
<div>
|
2025-07-13 04:06:37 +02:00
|
|
|
|
<Label htmlFor="date" className="text-red-400 font-mono text-xs uppercase tracking-wider">> Purchase Date</Label>
|
2025-07-10 17:20:48 +02:00
|
|
|
|
<Input
|
|
|
|
|
|
id="date"
|
|
|
|
|
|
type="date"
|
|
|
|
|
|
value={data.date}
|
|
|
|
|
|
onChange={(e) => setData('date', e.target.value)}
|
|
|
|
|
|
max={new Date().toISOString().split('T')[0]}
|
2025-07-13 04:06:37 +02:00
|
|
|
|
className="bg-black border-red-500 text-red-400 focus:border-red-300 font-mono text-sm rounded-none border-2 focus:ring-0 focus:outline-none transition-all glow-red"
|
2025-07-10 17:20:48 +02:00
|
|
|
|
/>
|
|
|
|
|
|
<InputError message={errors.date} />
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div>
|
2025-07-13 04:06:37 +02:00
|
|
|
|
<Label htmlFor="shares" className="text-red-400 font-mono text-xs uppercase tracking-wider">> Number of Shares</Label>
|
2025-07-10 17:20:48 +02:00
|
|
|
|
<Input
|
|
|
|
|
|
id="shares"
|
|
|
|
|
|
type="number"
|
|
|
|
|
|
step="0.000001"
|
|
|
|
|
|
min="0"
|
|
|
|
|
|
placeholder="1.234567"
|
|
|
|
|
|
value={data.shares}
|
|
|
|
|
|
onChange={(e) => setData('shares', e.target.value)}
|
2025-07-13 04:06:37 +02:00
|
|
|
|
className="bg-black border-red-500 text-red-400 focus:border-red-300 font-mono text-sm rounded-none border-2 focus:ring-0 focus:outline-none placeholder:text-red-400/40 transition-all glow-red"
|
2025-07-10 17:20:48 +02:00
|
|
|
|
/>
|
|
|
|
|
|
<InputError message={errors.shares} />
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div>
|
2025-07-13 04:06:37 +02:00
|
|
|
|
<Label htmlFor="price_per_share" className="text-red-400 font-mono text-xs uppercase tracking-wider">> Price per Share (€)</Label>
|
2025-07-10 17:20:48 +02:00
|
|
|
|
<Input
|
|
|
|
|
|
id="price_per_share"
|
|
|
|
|
|
type="number"
|
|
|
|
|
|
step="0.01"
|
|
|
|
|
|
min="0"
|
|
|
|
|
|
placeholder="123.45"
|
|
|
|
|
|
value={data.price_per_share}
|
|
|
|
|
|
onChange={(e) => setData('price_per_share', e.target.value)}
|
2025-07-13 04:06:37 +02:00
|
|
|
|
className="bg-black border-red-500 text-red-400 focus:border-red-300 font-mono text-sm rounded-none border-2 focus:ring-0 focus:outline-none placeholder:text-red-400/40 transition-all glow-red"
|
2025-07-10 17:20:48 +02:00
|
|
|
|
/>
|
|
|
|
|
|
<InputError message={errors.price_per_share} />
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div>
|
2025-07-13 04:06:37 +02:00
|
|
|
|
<Label htmlFor="total_cost" className="text-red-400 font-mono text-xs uppercase tracking-wider">> Total Cost (€)</Label>
|
2025-07-10 17:20:48 +02:00
|
|
|
|
<Input
|
|
|
|
|
|
id="total_cost"
|
|
|
|
|
|
type="number"
|
|
|
|
|
|
step="0.01"
|
|
|
|
|
|
min="0"
|
|
|
|
|
|
placeholder="1234.56"
|
|
|
|
|
|
value={data.total_cost}
|
|
|
|
|
|
onChange={(e) => setData('total_cost', e.target.value)}
|
2025-07-13 04:06:37 +02:00
|
|
|
|
className="bg-black border-red-500 text-red-400 focus:border-red-300 font-mono text-sm rounded-none border-2 focus:ring-0 focus:outline-none placeholder:text-red-400/40 transition-all glow-red"
|
2025-07-10 17:20:48 +02:00
|
|
|
|
/>
|
2025-07-13 04:06:37 +02:00
|
|
|
|
<p className="text-xs text-red-400/60 mt-1 font-mono">
|
|
|
|
|
|
[AUTO-CALC] shares × price
|
2025-07-10 17:20:48 +02:00
|
|
|
|
</p>
|
|
|
|
|
|
<InputError message={errors.total_cost} />
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2025-07-13 04:06:37 +02:00
|
|
|
|
<div className="flex gap-3 pt-2">
|
|
|
|
|
|
<Button
|
|
|
|
|
|
type="submit"
|
|
|
|
|
|
disabled={processing}
|
|
|
|
|
|
className="flex-1 bg-red-500 hover:bg-red-500 text-black font-mono text-sm font-bold border-red-500 rounded-none border-2 uppercase tracking-wider transition-all glow-red"
|
|
|
|
|
|
>
|
|
|
|
|
|
{processing && <LoaderCircle className="mr-2 h-4 w-4 animate-spin" />}
|
|
|
|
|
|
[EXECUTE]
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
{onCancel && (
|
|
|
|
|
|
<Button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
variant="outline"
|
|
|
|
|
|
onClick={onCancel}
|
|
|
|
|
|
className="flex-1 bg-black border-red-500 text-red-400 hover:bg-red-950 hover:text-red-300 font-mono text-sm font-bold rounded-none border-2 uppercase tracking-wider transition-all glow-red"
|
|
|
|
|
|
>
|
|
|
|
|
|
[ABORT]
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
2025-07-10 17:20:48 +02:00
|
|
|
|
</form>
|
2025-07-10 18:04:58 +02:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2025-07-10 17:20:48 +02:00
|
|
|
|
);
|
2025-07-13 04:06:37 +02:00
|
|
|
|
}
|