diff --git a/src/app/Console/Commands/User/EntitlementsCommand.php b/src/app/Console/Commands/User/EntitlementsCommand.php index 331fe3ca..4a6631e9 100644 --- a/src/app/Console/Commands/User/EntitlementsCommand.php +++ b/src/app/Console/Commands/User/EntitlementsCommand.php @@ -1,52 +1,52 @@ getUser($this->argument('userid')); + $user = $this->getUser($this->argument('user')); if (!$user) { $this->error("User not found."); return 1; } $skus_counted = []; foreach ($user->entitlements as $entitlement) { if (!array_key_exists($entitlement->sku_id, $skus_counted)) { $skus_counted[$entitlement->sku_id] = 1; } else { $skus_counted[$entitlement->sku_id] += 1; } } foreach ($skus_counted as $id => $qty) { $sku = \App\Sku::find($id); $this->info("{$sku->title}: {$qty}"); } } } diff --git a/src/app/Console/Commands/Wallet/EntitlementsCommand.php b/src/app/Console/Commands/Wallet/EntitlementsCommand.php new file mode 100644 index 00000000..13da2cef --- /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 index 00000000..5bb260cc --- /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); + } +}