diff --git a/src/app/Console/Commands/User/EntitlementsCommand.php b/src/app/Console/Commands/User/EntitlementsCommand.php --- a/src/app/Console/Commands/User/EntitlementsCommand.php +++ b/src/app/Console/Commands/User/EntitlementsCommand.php @@ -11,7 +11,7 @@ * * @var string */ - protected $signature = 'user:entitlements {userid}'; + protected $signature = 'user:entitlements {user}'; /** * The console command description. @@ -27,7 +27,7 @@ */ public function handle() { - $user = $this->getUser($this->argument('userid')); + $user = $this->getUser($this->argument('user')); if (!$user) { $this->error("User not found."); diff --git a/src/app/Console/Commands/Wallet/EntitlementsCommand.php b/src/app/Console/Commands/Wallet/EntitlementsCommand.php new file mode 100644 --- /dev/null +++ b/src/app/Console/Commands/Wallet/EntitlementsCommand.php @@ -0,0 +1,79 @@ +getWallet($this->argument('wallet')); + + if (!$wallet) { + $this->error("Wallet not found."); + return 1; + } + + $details = $this->option('details'); + + $discount = $wallet->getDiscountRate(); + + $entitlements = $wallet->entitlements() + ->select('skus.title', 'entitlements.*') + ->join('skus', 'skus.id', '=', 'entitlements.sku_id') + ->orderBy('entitleable_type') + ->orderBy('entitleable_id') + ->orderBy('sku_id') + ->orderBy('created_at') + ->get(); + + foreach ($entitlements as $entitlement) { + // sanity check, deleted entitleable? + if (!$entitlement->entitleable) { + $this->info("{$entitlement->id}: DELETED {$entitlement->entitleable_type} {$entitlement->entitleable_id}"); + continue; + } + + $title = $entitlement->title; // @phpstan-ignore-line + $cost = $wallet->money((int) ($entitlement->cost * $discount)); + $entitleableTitle = $entitlement->entitleable->toString(); + $add = ''; + + if ($details) { + $add = sprintf( + "(created: %s, updated: %s", + $entitlement->created_at->toDateString(), + $entitlement->updated_at->toDateString() + ); + + if ($discount) { + $add .= sprintf(", cost: %s", $wallet->money($entitlement->cost)); + } + + $add .= ")"; + } + + $this->info("{$entitlement->id}: {$entitleableTitle} ({$title}) {$cost}{$add}"); + } + } +} diff --git a/src/tests/Feature/Console/Wallet/EntitlementsTest.php b/src/tests/Feature/Console/Wallet/EntitlementsTest.php new file mode 100644 --- /dev/null +++ b/src/tests/Feature/Console/Wallet/EntitlementsTest.php @@ -0,0 +1,30 @@ +assertSame(1, $code); + $this->assertSame("Wallet not found.", $output); + + $user = $this->getTestUser('john@kolab.org'); + $wallet = $user->wallets()->first(); + + $code = \Artisan::call("wallet:entitlements {$wallet->id} --details"); + $output = trim(\Artisan::output()); + + $this->assertSame(0, $code); + $this->assertTrue(strpos($output, "john@kolab.org (mailbox) 5,00 CHF") !== false); + $this->assertTrue(strpos($output, "john@kolab.org (groupware) 4,90 CHF") !== false); + } +}