Changeset View
Changeset View
Standalone View
Standalone View
src/app/Console/Commands/Wallet/TransactionsCommand.php
<?php | <?php | ||||
namespace App\Console\Commands\Wallet; | namespace App\Console\Commands\Wallet; | ||||
use App\Console\Command; | use App\Console\Command; | ||||
class TransactionsCommand extends Command | class TransactionsCommand extends Command | ||||
{ | { | ||||
/** | /** | ||||
* The name and signature of the console command. | * The name and signature of the console command. | ||||
* | * | ||||
* @var string | * @var string | ||||
*/ | */ | ||||
protected $signature = 'wallet:transactions {--detail} {wallet}'; | protected $signature = 'wallet:transactions {--detail} {--balance} {wallet}'; | ||||
/** | /** | ||||
* The console command description. | * The console command description. | ||||
* | * | ||||
* @var string | * @var string | ||||
*/ | */ | ||||
protected $description = 'List the transactions against a wallet.'; | protected $description = 'List the transactions against a wallet.'; | ||||
/** | /** | ||||
* Execute the console command. | * Execute the console command. | ||||
* | * | ||||
* @return mixed | * @return mixed | ||||
*/ | */ | ||||
public function handle() | public function handle() | ||||
{ | { | ||||
$wallet = $this->getWallet($this->argument('wallet')); | $wallet = $this->getWallet($this->argument('wallet')); | ||||
if (!$wallet) { | if (!$wallet) { | ||||
$this->error("Wallet not found."); | $this->error("Wallet not found."); | ||||
return 1; | return 1; | ||||
} | } | ||||
$wallet->transactions()->orderBy('created_at')->each(function ($transaction) { | $withDetail = $this->option('detail'); | ||||
$balanceMode = $this->option('balance'); | |||||
$balance = 0; | |||||
$transactions = $wallet->transactions()->orderBy('created_at')->cursor(); | |||||
foreach ($transactions as $transaction) { | |||||
$balance += $transaction->amount; | |||||
$this->info( | $this->info( | ||||
sprintf( | sprintf( | ||||
"%s: %s %s", | "%s: %s %s", | ||||
$transaction->id, | $transaction->id, | ||||
$transaction->created_at, | $transaction->created_at, | ||||
$transaction->toString() | $transaction->toString() | ||||
) | ) | ||||
. ($balanceMode ? sprintf(' (balance: %s)', $wallet->money($balance)) : '') | |||||
); | ); | ||||
if ($this->option('detail')) { | if ($withDetail) { | ||||
$elements = \App\Transaction::where('transaction_id', $transaction->id) | $elements = \App\Transaction::where('transaction_id', $transaction->id) | ||||
->orderBy('created_at')->get(); | ->orderBy('created_at')->get(); | ||||
foreach ($elements as $element) { | foreach ($elements as $element) { | ||||
$this->info( | $this->info( | ||||
sprintf( | sprintf( | ||||
" + %s: %s", | " + %s: %s", | ||||
$element->id, | $element->id, | ||||
$element->toString() | $element->toString() | ||||
) | ) | ||||
); | ); | ||||
} | } | ||||
} | } | ||||
}); | } | ||||
} | } | ||||
} | } |