feat: adds pgsql compatibility (#72)

This commit is contained in:
hackerESQ
2025-03-10 21:17:24 -05:00
committed by GitHub
parent 9e48f21c8d
commit 7ab6f79e56
15 changed files with 195 additions and 139 deletions
@@ -3,16 +3,16 @@
use App\Models\Holding;
use Livewire\Volt\Component;
new class extends Component {
new class extends Component
{
// props
public Holding $holding;
protected $listeners = [
'transaction-updated' => '$refresh',
'transaction-saved' => '$refresh'
'transaction-saved' => '$refresh',
];
// methods
}; ?>
@@ -1,33 +1,38 @@
<?php
use App\Models\Transaction;
use App\Models\Portfolio;
use App\Rules\SymbolValidationRule;
use App\Models\Transaction;
use App\Rules\QuantityValidationRule;
use Illuminate\Support\Collection;
use Livewire\Attributes\{Computed};
use App\Rules\SymbolValidationRule;
use App\Traits\WithTrimStrings;
use Livewire\Volt\Component;
use Mary\Traits\Toast;
use Illuminate\Validation\Rule;
use App\Traits\WithTrimStrings;
new class extends Component {
new class extends Component
{
use Toast;
use WithTrimStrings;
// props
public ?Portfolio $portfolio;
public ?Transaction $transaction;
public ?String $portfolio_id;
public String $symbol;
public String $transaction_type;
public String $date;
public Float $quantity;
public ?Float $cost_basis;
public ?Float $sale_price;
public ?string $portfolio_id;
public Bool $confirmingTransactionDeletion = false;
public string $symbol;
public string $transaction_type;
public string $date;
public float $quantity;
public ?float $cost_basis;
public ?float $sale_price;
public bool $confirmingTransactionDeletion = false;
// methods
public function rules()
@@ -36,19 +41,19 @@ new class extends Component {
'symbol' => ['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()->format('Y-m-d')],
'date' => ['required', 'date_format:Y-m-d', 'before_or_equal:'.now()->format('Y-m-d')],
'quantity' => [
'required',
'numeric',
'min:0',
new QuantityValidationRule($this->portfolio, $this->symbol, $this->transaction_type, $this->date)
'required',
'numeric',
'min:0',
new QuantityValidationRule($this->portfolio, $this->symbol, $this->transaction_type, $this->date),
],
'cost_basis' => 'exclude_if:transaction_type,SELL|min:0|numeric',
'sale_price' => 'exclude_if:transaction_type,BUY|min:0|numeric',
];
}
public function mount()
public function mount()
{
if (isset($this->transaction)) {
@@ -59,7 +64,7 @@ new class extends Component {
$this->quantity = $this->transaction->quantity;
$this->cost_basis = $this->transaction->cost_basis;
$this->sale_price = $this->transaction->sale_price;
} else {
$this->transaction_type = 'BUY';
$this->portfolio_id = isset($this->portfolio) ? $this->portfolio->id : '';
@@ -70,9 +75,8 @@ new class extends Component {
public function update()
{
$this->authorize('fullAccess', $this->portfolio);
$this->transaction->update($this->validate());
// $this->transaction->owner_id = auth()->user()->id;
$this->transaction->save();
$this->success(__('Transaction updated'));
@@ -83,7 +87,7 @@ new class extends Component {
public function save()
{
if (!isset($this->portfolio)) {
if (! isset($this->portfolio)) {
$this->portfolio = Portfolio::find($this->portfolio_id);
}
@@ -107,6 +111,11 @@ new class extends Component {
$this->success(__('Transaction deleted'), redirectTo: route('holding.show', ['portfolio' => $this->portfolio->id, 'symbol' => $this->symbol]));
}
public function updatedSymbol($value)
{
$this->symbol = strtoupper($value);
}
}; ?>
<div class="" x-data="{ transaction_type: @entangle('transaction_type') }">
@@ -2,29 +2,31 @@
use App\Models\DailyChange;
use App\Models\Portfolio;
use Livewire\Attributes\{Title, Rule};
use Livewire\Volt\Component;
new class extends Component {
new class extends Component
{
// props
public ?Portfolio $portfolio;
public String $name = 'portfolio';
public String $scope = 'YTD';
public Array $scopeOptions = [
public string $name = 'portfolio';
public string $scope = 'YTD';
public array $scopeOptions = [
['id' => '1M', 'name' => '1 month', 'method' => 'subMonths', 'args' => [1]],
['id' => '3M', 'name' => '3 months', 'method' => 'subMonths', 'args' => [3]],
['id' => 'YTD', 'name' => 'Year to date', 'method' => 'startOfYear', 'args' => []],
['id' => '1Y', 'name' => '1 year', 'method' => 'subYears', 'args' => [1]],
['id' => '3Y', 'name' => '3 years', 'method' => 'subYears', 'args' => [3]],
['id' => 'ALL', 'name' => 'All time', 'method' => null]
['id' => 'ALL', 'name' => 'All time', 'method' => null],
];
// data
public Array $chartSeries;
public array $chartSeries;
// methods
public function mount()
public function mount()
{
$this->chartSeries = $this->generatePerformanceData();
}
@@ -33,52 +35,53 @@ new class extends Component {
{
$filterMethod = collect($this->scopeOptions)->where('id', $this->scope)->first();
$dailyChangeQuery = DailyChange::myDailyChanges();
$dailyChangeQuery = DailyChange::myDailyChanges()->selectRaw('
date,
SUM(total_market_value) as total_market_value,
SUM(total_cost_basis) as total_cost_basis,
SUM(total_gain) as total_gain
/* ,
SUM(realized_gains) as realized_gains,
SUM(total_dividends_earned) as total_dividends_earned
*/
');
if (isset($this->portfolio)) {
// portfolio
$dailyChangeQuery->portfolio($this->portfolio->id);
} else {
$dailyChangeQuery->selectRaw('
date,
SUM(total_market_value) as total_market_value,
SUM(total_cost_basis) as total_cost_basis,
SUM(total_gain) as total_gain
/* ,
SUM(realized_gains) as realized_gains,
SUM(total_dividends_earned) as total_dividends_earned
*/
')
->withoutWishlists()
->groupBy('date')
->orderBy('date');
// dashboard
$dailyChangeQuery->withoutWishlists();
}
if ($filterMethod['method']) {
$dailyChangeQuery->whereDate('date', '>=', now()->{$filterMethod['method']}(...$filterMethod['args']));
}
// dd($dailyChangeQuery->toSql());
$dailyChange = $dailyChangeQuery->get();
$dailyChange = $dailyChangeQuery
->orderBy('date')
->groupBy('date')
->get();
return [
'series' => [
[
'name' => __('Market Value'),
'data' => $dailyChange->map(fn($data) => [$data->date, $data->total_market_value])->toArray(),
'data' => $dailyChange->map(fn ($data) => [$data->date, $data->total_market_value])->toArray(),
],
[
'name' => __('Cost Basis'),
'data' => $dailyChange->map(fn($data) => [$data->date, $data->total_cost_basis])->toArray(),
'data' => $dailyChange->map(fn ($data) => [$data->date, $data->total_cost_basis])->toArray(),
],
[
'name' => __('Market Gain'),
'data' => $dailyChange->map(fn($data) => [$data->date, $data->total_gain])->toArray()
'data' => $dailyChange->map(fn ($data) => [$data->date, $data->total_gain])->toArray(),
],
// [
// 'name' => __('Dividends Earned'),
// 'data' => $dailyChange->map(fn($data) => [$data->date, $data->total_dividends_earned])->toArray()
@@ -87,7 +90,7 @@ new class extends Component {
// 'name' => __('Realized Gains'),
// 'data' => $dailyChange->map(fn($data) => [$data->date, $data->realized_gains])->toArray()
// ],
]
],
];
}
@@ -102,7 +105,6 @@ new class extends Component {
{
return collect($this->scopeOptions)->where('id', $scope)->first()['name'];
}
}; ?>
<x-card class="bg-slate-100 dark:bg-base-200 rounded-lg mb-6">