including working export button
This commit is contained in:
hackerESQ
2024-08-27 22:06:10 -05:00
parent e21814714d
commit 12f3003a3a
16 changed files with 39 additions and 164 deletions
-6
View File
@@ -3,9 +3,6 @@
namespace App\Exports; namespace App\Exports;
use App\Exports\Sheets\DailyChangesSheet; use App\Exports\Sheets\DailyChangesSheet;
use App\Exports\Sheets\SplitsSheet;
use App\Exports\Sheets\DividendsSheet;
use App\Exports\Sheets\MarketDataSheet;
use App\Exports\Sheets\PortfoliosSheet; use App\Exports\Sheets\PortfoliosSheet;
use App\Exports\Sheets\TransactionsSheet; use App\Exports\Sheets\TransactionsSheet;
use Maatwebsite\Excel\Concerns\Exportable; use Maatwebsite\Excel\Concerns\Exportable;
@@ -23,9 +20,6 @@ class BackupExport implements WithMultipleSheets
return [ return [
new PortfoliosSheet, new PortfoliosSheet,
new TransactionsSheet, new TransactionsSheet,
new MarketDataSheet,
new DividendsSheet,
new SplitsSheet,
new DailyChangesSheet new DailyChangesSheet
]; ];
} }
+3 -3
View File
@@ -13,13 +13,13 @@ class DailyChangesSheet implements FromCollection, WithHeadings, WithTitle
{ {
return [ return [
'Date', 'Date',
'User', 'Portfolio',
'Total Market Value', 'Total Market Value',
'Total Cost Basis', 'Total Cost Basis',
'Total Gain Loss', 'Total Gain Loss',
'Total Dividends', 'Total Dividends',
'Realized Gains', 'Realized Gains',
'Notes' 'Annotation'
]; ];
} }
@@ -28,7 +28,7 @@ class DailyChangesSheet implements FromCollection, WithHeadings, WithTitle
*/ */
public function collection() public function collection()
{ {
return DailyChange::myDailyChanges()->get(); return auth()->user()->daily_changes;
} }
/** /**
-39
View File
@@ -1,39 +0,0 @@
<?php
namespace App\Exports\Sheets;
use App\Models\Dividend;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithTitle;
class DividendsSheet implements FromCollection, WithHeadings, WithTitle
{
public function headings(): array
{
return [
'ID',
'Date',
'Symbol',
'Amount',
'Created',
'Updated',
];
}
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return Dividend::get();
}
/**
* @return string
*/
public function title(): string
{
return 'Dividends';
}
}
-42
View File
@@ -1,42 +0,0 @@
<?php
namespace App\Exports\Sheets;
use App\Models\MarketData;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithTitle;
class MarketDataSheet implements FromCollection, WithHeadings, WithTitle
{
public function headings(): array
{
return [
'Symbol',
'Name',
'Market Value',
'52 Week Low',
'52 Week High',
'Dividend Date',
'Splits Synced To Holdings At',
'Created',
'Updated',
];
}
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return MarketData::get();
}
/**
* @return string
*/
public function title(): string
{
return 'Market Data';
}
}
+2 -2
View File
@@ -17,7 +17,7 @@ class PortfoliosSheet implements FromCollection, WithHeadings, WithTitle
'Notes', 'Notes',
'Wishlist', 'Wishlist',
'Created', 'Created',
'Updated', 'Updated'
]; ];
} }
@@ -26,7 +26,7 @@ class PortfoliosSheet implements FromCollection, WithHeadings, WithTitle
*/ */
public function collection() public function collection()
{ {
return Portfolio::myPortfolios()->get(); return auth()->user()->portfolios;
} }
/** /**
-39
View File
@@ -1,39 +0,0 @@
<?php
namespace App\Exports\Sheets;
use App\Models\Split;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithTitle;
class SplitsSheet implements FromCollection, WithHeadings, WithTitle
{
public function headings(): array
{
return [
'ID',
'Date',
'Symbol',
'Split',
'Created',
'Updated',
];
}
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return Split::get();
}
/**
* @return string
*/
public function title(): string
{
return 'Splits';
}
}
+12 -4
View File
@@ -12,10 +12,10 @@ class TransactionsSheet implements FromCollection, WithHeadings, WithTitle
public function headings(): array public function headings(): array
{ {
return [ return [
'ID', 'Transaction ID',
'Symbol', 'Symbol',
'Portfolio', 'Portfolio ID',
'Transaction', 'Transaction Type',
'Quantity', 'Quantity',
'Cost Basis', 'Cost Basis',
'Sale Price', 'Sale Price',
@@ -23,6 +23,14 @@ class TransactionsSheet implements FromCollection, WithHeadings, WithTitle
'Date', 'Date',
'Created', 'Created',
'Updated', 'Updated',
'Company Name',
'Portfolio Title',
'Market Value',
'52 Week Low',
'52 Week High',
'Market Data Refresh Date',
'Gain/Loss Dollars',
'Owner ID'
]; ];
} }
@@ -31,7 +39,7 @@ class TransactionsSheet implements FromCollection, WithHeadings, WithTitle
*/ */
public function collection() public function collection()
{ {
return Transaction::myTransactions()->get(); return auth()->user()->transactions;
} }
/** /**
@@ -22,7 +22,6 @@ class PortfolioController extends Controller
*/ */
public function show(Portfolio $portfolio) public function show(Portfolio $portfolio)
{ {
// get portfolio metrics // get portfolio metrics
$metrics = cache()->tags(['metrics', 'portfolio', auth()->user()->id, $portfolio->id])->remember( $metrics = cache()->tags(['metrics', 'portfolio', auth()->user()->id, $portfolio->id])->remember(
'portfolio-metrics-' . $portfolio->id, 'portfolio-metrics-' . $portfolio->id,
+1 -6
View File
@@ -33,18 +33,13 @@ class DailyChange extends Model
'date' => 'datetime', 'date' => 'datetime',
]; ];
public function scopeMyDailyChanges($query)
{
return $query->where('user_id', auth()->user()->id);
}
public function scopePortfolio($query, $portfolio) public function scopePortfolio($query, $portfolio)
{ {
return $query->where('portfolio_id', $portfolio); return $query->where('portfolio_id', $portfolio);
} }
public function portfolio() public function portfolio()
{ {
return $this->belongsTo(Portfolio::class); return $this->belongsTo(Portfolio::class);
} }
} }
-7
View File
@@ -35,8 +35,6 @@ class Portfolio extends Model
protected $with = ['users', 'transactions']; protected $with = ['users', 'transactions'];
protected $appends = ['owner_id'];
public function users() public function users()
{ {
return $this->belongsToMany(User::class)->withPivot('owner'); return $this->belongsToMany(User::class)->withPivot('owner');
@@ -69,11 +67,6 @@ class Portfolio extends Model
return $this->hasMany(DailyChange::class); return $this->hasMany(DailyChange::class);
} }
public function scopeMyPortfolios()
{
return $this->whereRelation('users', 'id', auth()->user()->id);
}
public function scopeWithoutWishlists() public function scopeWithoutWishlists()
{ {
return $this->where(['wishlist' => false]); return $this->where(['wishlist' => false]);
-7
View File
@@ -97,13 +97,6 @@ class Transaction extends Model
return $query->where('symbol', $symbol); return $query->where('symbol', $symbol);
} }
public function scopeMyTransactions()
{
return $this->whereHas('portfolio', function ($query) {
return $query->whereRelation('users', 'id', auth()->user()->id);
});
}
public function refreshMarketData() public function refreshMarketData()
{ {
return MarketData::getMarketData($this->attributes['symbol']); return MarketData::getMarketData($this->attributes['symbol']);
+5
View File
@@ -54,6 +54,11 @@ class User extends Authenticatable
return $this->belongsToMany(Portfolio::class)->withPivot('owner'); return $this->belongsToMany(Portfolio::class)->withPivot('owner');
} }
public function daily_changes()
{
return $this->hasManyDeep(DailyChange::class, ['portfolio_user', Portfolio::class]);
}
public function holdings(): HasManyDeep public function holdings(): HasManyDeep
{ {
return $this->hasManyDeep(Holding::class, ['portfolio_user', Portfolio::class]) return $this->hasManyDeep(Holding::class, ['portfolio_user', Portfolio::class])
+2 -2
View File
@@ -18,7 +18,7 @@ class Spotlight
return $results; return $results;
} }
$portfolios = Portfolio::myPortfolios()->where('title', 'LIKE', '%'.$request->input('search').'%')->limit(5)->get(); $portfolios = $request->user()->portfolios()->where('title', 'LIKE', '%'.$request->input('search').'%')->limit(5)->get();
$portfolios->each(function($portfolio) use ($results) { $portfolios->each(function($portfolio) use ($results) {
$results->push([ $results->push([
@@ -29,7 +29,7 @@ class Spotlight
]); ]);
}); });
$holdings = Holding::myHoldings()->where('symbol', 'LIKE', '%'.$request->input('search').'%')->limit(5)->get(); $holdings = $request->user()->holdings()->where('holdings.symbol', 'LIKE', '%'.$request->input('search').'%')->limit(5)->get();
$holdings->each(function($holding) use ($results) { $holdings->each(function($holding) use ($results) {
$results->push([ $results->push([
+1
View File
@@ -15,6 +15,7 @@
"Save": "Save", "Save": "Save",
"Close": "Close", "Close": "Close",
"Hang on! You're doing that too much.": "Hang on! You're doing that too much.",
"Delete Account": "Delete Account", "Delete Account": "Delete Account",
"Permanently delete your account.": "Permanently delete your account.", "Permanently delete your account.": "Permanently delete your account.",
"Once your account is deleted, all of its resources and data will be permanently deleted. Before deleting your account, please download any data or information that you wish to retain.": "Once your account is deleted, all of its resources and data will be permanently deleted. Before deleting your account, please download any data or information that you wish to retain.", "Once your account is deleted, all of its resources and data will be permanently deleted. Before deleting your account, please download any data or information that you wish to retain.": "Once your account is deleted, all of its resources and data will be permanently deleted. Before deleting your account, please download any data or information that you wish to retain.",
+1
View File
@@ -15,6 +15,7 @@
"Save": "Guardar", "Save": "Guardar",
"Close": "Cerrar", "Close": "Cerrar",
"Hang on! You're doing that too much.": "¡Por favor espere un momento!",
"Delete Account": "Eliminar Cuenta", "Delete Account": "Eliminar Cuenta",
"Permanently delete your account.": "Elimina tu cuenta de forma permanente.", "Permanently delete your account.": "Elimina tu cuenta de forma permanente.",
"Once your account is deleted, all of its resources and data will be permanently deleted. Before deleting your account, please download any data or information that you wish to retain.": "Una vez que tu cuenta sea eliminada, todos sus recursos y datos serán eliminados de forma permanente. Antes de eliminar tu cuenta, descarga cualquier dato o información que desees conservar.", "Once your account is deleted, all of its resources and data will be permanently deleted. Before deleting your account, please download any data or information that you wish to retain.": "Una vez que tu cuenta sea eliminada, todos sus recursos y datos serán eliminados de forma permanente. Antes de eliminar tu cuenta, descarga cualquier dato o información que desees conservar.",
@@ -2,25 +2,31 @@
use Livewire\Volt\Component; use Livewire\Volt\Component;
use Mary\Traits\Toast; use Mary\Traits\Toast;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\BackupExport;
use Illuminate\Support\Facades\RateLimiter;
new class extends Component { new class extends Component {
use Toast; use Toast;
// props // props
// methods // methods
public function mount() public function export()
{ {
if (!RateLimiter::attempt('export:'.auth()->user()->id, $perMinute = 3, fn()=>null)) {
// $this->error(__('Hang on! You\'re doing that too much.'));
return;
}
return Excel::download(new BackupExport, now()->format('Y_m_d') . '_investbrain_backup.xlsx');
} }
}; ?> }; ?>
<div> <div>
<x-button type="submit"> <x-button type="submit" @click="$wire.export">
{{ __('Download Export') }} {{ __('Download Export') }}
</x-button> </x-button>
</div> </div>