Files
investbrain/resources/views/livewire/manage-transaction-form.blade.php
T

198 lines
6.4 KiB
PHP
Raw Normal View History

2024-08-15 21:35:43 -05:00
<?php
use App\Models\Transaction;
use App\Models\Portfolio;
2024-08-24 22:19:40 -05:00
use App\Rules\SymbolValidationRule;
2024-08-31 22:05:47 -05:00
use App\Rules\QuantityValidationRule;
2024-08-15 21:35:43 -05:00
use Illuminate\Support\Collection;
2024-08-24 22:19:40 -05:00
use Livewire\Attributes\{Computed};
2024-08-15 21:35:43 -05:00
use Livewire\Volt\Component;
use Mary\Traits\Toast;
2024-08-31 22:05:47 -05:00
use Illuminate\Validation\Rule;
2024-08-15 21:35:43 -05:00
new class extends Component {
use Toast;
// props
public ?Portfolio $portfolio;
public ?Transaction $transaction;
2024-08-27 18:12:51 -05:00
public ?String $portfolio_id;
2024-08-15 21:35:43 -05:00
public String $symbol;
public String $transaction_type;
public String $date;
2024-08-15 22:10:43 -05:00
public Float $quantity;
public ?Float $cost_basis;
public ?Float $sale_price;
2024-08-15 21:35:43 -05:00
2024-08-15 22:10:43 -05:00
public Bool $confirmingTransactionDeletion = false;
2024-08-15 21:35:43 -05:00
// methods
2024-08-24 22:19:40 -05:00
public function rules()
{
return [
'symbol' => ['required', 'string', new SymbolValidationRule],
'transaction_type' => 'required|string|in:BUY,SELL',
2024-08-27 18:12:51 -05:00
'portfolio_id' => 'required|exists:portfolios,id',
2024-08-24 22:19:40 -05:00
'date' => 'required|date_format:Y-m-d',
2024-08-31 22:05:47 -05:00
'quantity' => [
'required',
'numeric',
'min:0',
2024-09-06 23:15:52 -05:00
new QuantityValidationRule($this->portfolio, $this->symbol, $this->transaction_type, $this->date)
2024-08-31 22:05:47 -05:00
],
2024-08-24 22:19:40 -05:00
'cost_basis' => 'exclude_if:transaction_type,SELL|min:0|numeric',
'sale_price' => 'exclude_if:transaction_type,BUY|min:0|numeric',
];
}
2024-08-15 21:35:43 -05:00
public function mount()
{
if (isset($this->transaction)) {
$this->symbol = $this->transaction->symbol;
$this->transaction_type = $this->transaction->transaction_type;
2024-08-27 18:12:51 -05:00
$this->portfolio_id = $this->transaction->portfolio_id;
2024-08-15 21:35:43 -05:00
$this->date = $this->transaction->date->format('Y-m-d');
$this->quantity = $this->transaction->quantity;
2024-08-15 22:10:43 -05:00
$this->cost_basis = $this->transaction->cost_basis;
$this->sale_price = $this->transaction->sale_price;
2024-08-15 21:35:43 -05:00
} else {
$this->transaction_type = 'BUY';
2024-08-27 18:12:51 -05:00
$this->portfolio_id = isset($this->portfolio) ? $this->portfolio->id : '';
2024-08-15 21:35:43 -05:00
$this->date = now()->format('Y-m-d');
}
}
public function update()
{
2024-10-21 22:23:20 -05:00
$this->authorize('fullAccess', $this->portfolio);
2024-08-15 21:35:43 -05:00
$this->transaction->update($this->validate());
// $this->transaction->owner_id = auth()->user()->id;
$this->transaction->save();
2024-08-27 18:12:51 -05:00
$this->success(__('Transaction updated'));
$this->dispatch('toggle-manage-transaction');
$this->dispatch('transaction-updated');
2024-08-15 21:35:43 -05:00
}
public function save()
{
2024-10-21 22:23:20 -05:00
$this->authorize('fullAccess', $this->portfolio);
2024-08-27 18:12:51 -05:00
$validated = $this->validate();
if (!isset($this->portfolio)) {
$this->portfolio = Portfolio::find($this->portfolio_id);
}
2024-08-15 21:35:43 -05:00
2024-08-27 18:12:51 -05:00
$transaction = $this->portfolio->transactions()->create($validated);
2024-08-15 21:35:43 -05:00
$transaction->save();
2024-08-27 18:12:51 -05:00
$this->dispatch('transaction-saved');
2024-08-30 20:58:00 -05:00
$this->success(__('Transaction created'), redirectTo: route('holding.show', ['portfolio' => $this->portfolio->id, 'symbol' => $this->symbol]));
2024-08-15 21:35:43 -05:00
}
public function delete()
{
2024-10-21 22:23:20 -05:00
$this->authorize('fullAccess', $this->portfolio);
2024-08-15 21:35:43 -05:00
$this->transaction->delete();
2024-08-30 20:58:00 -05:00
$this->success(__('Transaction deleted'), redirectTo: route('holding.show', ['portfolio' => $this->portfolio->id, 'symbol' => $this->symbol]));
2024-08-15 21:35:43 -05:00
}
}; ?>
2024-08-15 22:10:43 -05:00
<div class="" x-data="{ transaction_type: @entangle('transaction_type') }">
<x-ib-form wire:submit="{{ $transaction ? 'update' : 'save' }}" class="">
2024-08-15 21:35:43 -05:00
2024-08-27 18:12:51 -05:00
@if(empty($portfolio))
<x-select
label="{{ __('Portfolio') }}"
wire:model="portfolio_id"
required
2024-10-22 16:48:53 -05:00
:options="auth()->user()->portfolios()->fullAccess()->get()"
2024-08-27 18:12:51 -05:00
option-label="title"
placeholder="Select a portfolio"
/>
@endif
2024-08-15 21:35:43 -05:00
<x-input label="{{ __('Symbol') }}" wire:model="symbol" required />
<x-select label="{{ __('Transaction Type') }}" :options="[
['id' => 'BUY', 'name' => 'Buy'],
['id' => 'SELL', 'name' => 'Sell']
]" wire:model.live="transaction_type" />
<x-datetime label="{{ __('Transaction Date') }}" wire:model="date" required />
<x-input label="{{ __('Quantity') }}" type="number" step="any" wire:model="quantity" required />
2024-08-15 22:10:43 -05:00
@if($transaction_type == 'SELL')
<x-input
2024-08-27 18:12:51 -05:00
label="{{ __('Sale Price') }}"
2024-08-15 22:10:43 -05:00
wire:model.number="sale_price"
required
prefix="USD"
type="number"
step="any"
/>
2024-08-23 22:33:36 -05:00
{{-- money --}}
2024-08-15 22:10:43 -05:00
@else
<x-input
2024-08-27 18:12:51 -05:00
label="{{ __('Cost Basis') }}"
2024-08-15 22:10:43 -05:00
wire:model.number="cost_basis"
required
prefix="USD"
type="number"
step="any"
/>
2024-08-23 22:33:36 -05:00
{{-- money --}}
2024-08-15 22:10:43 -05:00
@endif
2024-08-15 21:35:43 -05:00
<x-slot:actions>
@if ($transaction)
<x-button
wire:click="$toggle('confirmingTransactionDeletion')"
wire:loading.attr="disabled"
icon="o-trash"
class="btn btn-ghost btn-circle text-error"
title="{{ __('Delete Transaction') }}"
/>
2024-08-15 21:35:43 -05:00
@endif
2024-08-24 22:19:40 -05:00
<x-button
2024-10-18 20:46:22 -05:00
label="{{ $transaction ? __('Update') : __('Create') }}"
2024-08-24 22:19:40 -05:00
type="submit"
icon="o-paper-airplane"
class="btn-primary"
spinner="{{ $transaction ? 'update' : 'save' }}"
/>
2024-08-15 21:35:43 -05:00
</x-slot:actions>
</x-ib-form>
2024-08-15 21:35:43 -05:00
<x-confirmation-modal wire:model.live="confirmingTransactionDeletion">
<x-slot name="title">
{{ __('Delete Transaction') }}
</x-slot>
<x-slot name="content">
{{ __('Are you sure you want to delete this transaction?') }}
</x-slot>
<x-slot name="footer">
<x-button class="btn-outline" wire:click="$toggle('confirmingTransactionDeletion')" wire:loading.attr="disabled">
{{ __('Cancel') }}
</x-secondary-button>
<x-button class="ms-3 btn-error text-white" wire:click="delete" wire:loading.attr="disabled">
{{ __('Delete Transaction') }}
</x-button>
</x-slot>
</x-confirmation-modal>
</div>