From 7d77b6fbc8126bc2677b9a1fc27295f189d9e153 Mon Sep 17 00:00:00 2001 From: hackerESQ Date: Wed, 27 Aug 2025 20:22:13 -0500 Subject: [PATCH] feat: add fix command for cost basis for sale transaction --- app/Console/Commands/FixCostBasisForSales.php | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/app/Console/Commands/FixCostBasisForSales.php b/app/Console/Commands/FixCostBasisForSales.php index 2e9544c..eeb885a 100644 --- a/app/Console/Commands/FixCostBasisForSales.php +++ b/app/Console/Commands/FixCostBasisForSales.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Console\Commands; +use App\Models\Portfolio; use App\Models\Transaction; use Illuminate\Console\Command; @@ -15,7 +16,8 @@ class FixCostBasisForSales extends Command * @var string */ protected $signature = 'fix:cost-basis-for-sales - {portfolio_id : The ID of the portfolio to fix.}'; + {--portfolio= : The ID of the portfolio to fix.} + {--user= : The user ID of transactions to fix.}'; /** * The console command description. @@ -42,7 +44,31 @@ class FixCostBasisForSales extends Command public function handle() { - $transactions = Transaction::where(['transaction_type' => 'SELL', 'portfolio_id' => $this->argument('portfolio_id')])->get(); + if (empty($this->option('user')) && empty($this->option('portfolio'))) { + + $this->error('Must provide at least a user or portfolio.'); + + return; + } + + $transactions = Transaction::where(['transaction_type' => 'SELL']); + + if ($this->option('user')) { + + $portfolios = Portfolio::fullAccess($this->option('user'))->get('id') + ->pluck('id') + ->toArray(); + + $transactions->whereIn('portfolio_id', $portfolios); + + } else { + + $transactions->where(['portfolio_id' => $this->option('portfolio')]); + } + + $transactions = $transactions->get(); + + $this->line("Fixing cost basis for {$transactions->count()} sale transactions..."); $transactions->chunk(10)->each(function ($chunk) { @@ -69,5 +95,7 @@ class FixCostBasisForSales extends Command }); }); }); + + $this->line('Done!'); } }