['required', 'string', new SymbolValidationRule], 'transaction_type' => 'required|string|in:BUY,SELL', 'portfolio_id' => 'required|exists:portfolios,id', 'date' => ['required', 'date_format:Y-m-d', 'before_or_equal:'.now()->toDateString()], 'quantity' => [ 'required', 'numeric', 'gt:0', new QuantityValidationRule($this->portfolio, $this->symbol, $this->transaction_type, $this->date), ], 'currency' => ['required', 'exists:currencies,currency'], 'cost_basis' => 'exclude_if:transaction_type,SELL|min:0|numeric', 'sale_price' => 'exclude_if:transaction_type,BUY|min:0|numeric', ]; } public function mount() { $this->currencies = Currency::list(); $this->currency = auth()->user()->getCurrency(); if (isset($this->transaction)) { $this->currency = $this->transaction->market_data->currency; $this->symbol = $this->transaction->symbol; $this->transaction_type = $this->transaction->transaction_type; $this->portfolio_id = $this->transaction->portfolio_id; $this->date = $this->transaction->date->toDateString(); $this->quantity = $this->transaction->quantity; $this->cost_basis = $this->transaction->cost_basis; $this->sale_price = $this->transaction->sale_price; } else { if (isset($this->symbol)) { $this->currency = MarketData::getMarketData($this->symbol)?->currency; } $this->transaction_type = 'BUY'; $this->portfolio_id = isset($this->portfolio) ? $this->portfolio->id : ''; $this->date = now()->toDateString(); } } public function update() { $this->authorize('fullAccess', $this->portfolio); $this->transaction->update($this->validate()); $this->transaction->save(); $this->success(__('Transaction updated')); $this->dispatch('toggle-manage-transaction'); $this->dispatch('transaction-updated'); } public function save() { if (! isset($this->portfolio)) { $this->portfolio = Portfolio::find($this->portfolio_id); } $this->authorize('fullAccess', $this->portfolio); $validated = $this->validate(); $transaction = $this->portfolio->transactions()->create($validated); $transaction->save(); $this->dispatch('transaction-saved'); $this->success(__('Transaction created'), redirectTo: route('holding.show', ['portfolio' => $this->portfolio->id, 'symbol' => $transaction->symbol])); } public function delete() { $this->authorize('fullAccess', $this->portfolio); $this->transaction->delete(); $this->success(__('Transaction deleted'), redirectTo: route('holding.show', ['portfolio' => $this->portfolio->id, 'symbol' => $this->symbol])); } }; ?>