Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F117754717
D1150.1775200582.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
5 KB
Referenced Files
None
Subscribers
None
D1150.1775200582.diff
View Options
diff --git a/src/app/Console/Commands/WalletCharge.php b/src/app/Console/Commands/WalletCharge.php
--- a/src/app/Console/Commands/WalletCharge.php
+++ b/src/app/Console/Commands/WalletCharge.php
@@ -52,11 +52,6 @@
);
$wallet->chargeEntitlements();
-
- if ($wallet->balance < 0) {
- // Disabled for now
- // \App\Jobs\WalletPayment::dispatch($wallet);
- }
}
}
}
diff --git a/src/app/Console/Commands/WalletUntil.php b/src/app/Console/Commands/WalletUntil.php
new file mode 100644
--- /dev/null
+++ b/src/app/Console/Commands/WalletUntil.php
@@ -0,0 +1,66 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+
+class WalletUntil extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'wallet:until {wallet}';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'Show until when the balance on a wallet lasts.';
+
+ /**
+ * Create a new command instance.
+ *
+ * @return void
+ */
+ public function __construct()
+ {
+ parent::__construct();
+ }
+
+ /**
+ * Execute the console command.
+ *
+ * @return mixed
+ */
+ public function handle()
+ {
+ $wallet = \App\Wallet::find($this->argument('wallet'));
+
+ if (!$wallet) {
+ return 1;
+ }
+
+
+ $balance = $wallet->balance;
+
+ // retrieve any expected charges
+ $expectedCharge = $wallet->expectedCharges();
+
+ // get the costs per day for all entitlements billed against this wallet
+ $costsPerDay = $wallet->costsPerDay();
+
+ // the number of days this balance, minus the expected charges, would last
+ $daysDelta = ($balance - $expectedCharge) / $costsPerDay;
+
+ // calculate from the last entitlement billed.
+ $entitlement = \App\Entitlement::where('wallet_id', $wallet->id)
+ ->orderBy('updated_at', 'desc')->first();
+
+ $lastsUntil = $entitlement->updated_at->copy()->addDays($daysDelta);
+
+ $this->info("lasts until: {$lastsUntil}");
+ }
+}
diff --git a/src/app/Entitlement.php b/src/app/Entitlement.php
--- a/src/app/Entitlement.php
+++ b/src/app/Entitlement.php
@@ -52,6 +52,26 @@
'cost' => 'integer',
];
+ /**
+ * Return the costs per day for this entitlement.
+ *
+ * @return float
+ */
+ public function costsPerDay()
+ {
+ if ($this->cost == 0) {
+ return (float) 0;
+ }
+
+ $discount = $this->wallet->getDiscountRate();
+
+ $daysInLastMonth = \App\Utils::daysInLastMonth();
+
+ $costsPerDay = (float) ($this->cost * $discount) / $daysInLastMonth;
+
+ return $costsPerDay;
+ }
+
/**
* Create a transaction record for this entitlement.
*
diff --git a/src/app/Utils.php b/src/app/Utils.php
--- a/src/app/Utils.php
+++ b/src/app/Utils.php
@@ -24,7 +24,7 @@
$start = new Carbon('first day of last month');
$end = new Carbon('last day of last month');
- return $start->diffInDays($end);
+ return $start->diffInDays($end) + 1;
}
/**
diff --git a/src/app/Wallet.php b/src/app/Wallet.php
--- a/src/app/Wallet.php
+++ b/src/app/Wallet.php
@@ -131,6 +131,22 @@
);
}
+ /**
+ * Retrieve the costs per day of everything charged to this wallet.
+ *
+ * @return float
+ */
+ public function costsPerDay()
+ {
+ $costs = (float) 0;
+
+ foreach ($this->entitlements as $entitlement) {
+ $costs += $entitlement->costsPerDay();
+ }
+
+ return $costs;
+ }
+
/**
* Add an amount of pecunia to this wallet's balance.
*
diff --git a/src/tests/Feature/EntitlementTest.php b/src/tests/Feature/EntitlementTest.php
--- a/src/tests/Feature/EntitlementTest.php
+++ b/src/tests/Feature/EntitlementTest.php
@@ -32,6 +32,25 @@
parent::tearDown();
}
+ public function testCostsPerDay(): void
+ {
+ // 444
+ // 28 days: 15.86
+ // 31 days: 14.32
+ $user = $this->getTestUser('entitlement-test@kolabnow.com');
+ $package = Package::where('title', 'kolab')->first();
+ $mailbox = Sku::where('title', 'mailbox')->first();
+
+ $user->assignPackage($package);
+
+ $entitlement = $user->entitlements->where('sku_id', $mailbox->id)->first();
+
+ $costsPerDay = $entitlement->costsPerDay();
+
+ $this->assertTrue($costsPerDay < 15.86);
+ $this->assertTrue($costsPerDay > 14.32);
+ }
+
/**
* Tests for User::AddEntitlement()
*/
diff --git a/src/tests/Feature/WalletTest.php b/src/tests/Feature/WalletTest.php
--- a/src/tests/Feature/WalletTest.php
+++ b/src/tests/Feature/WalletTest.php
@@ -40,6 +40,26 @@
parent::tearDown();
}
+ public function testCostsPerDay(): void
+ {
+ // 999
+ // 28 days: 35.68
+ // 31 days: 32.22
+ $user = $this->getTestUser('jane@kolabnow.com');
+
+ $package = Package::where('title', 'kolab')->first();
+ $mailbox = Sku::where('title', 'mailbox')->first();
+
+ $user->assignPackage($package);
+
+ $wallet = $user->wallets()->first();
+
+ $costsPerDay = $wallet->costsPerDay();
+
+ $this->assertTrue($costsPerDay < 35.68);
+ $this->assertTrue($costsPerDay > 32.22);
+ }
+
/**
* Verify a wallet is created, when a user is created.
*/
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Apr 3, 7:16 AM (18 h, 49 m ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18822981
Default Alt Text
D1150.1775200582.diff (5 KB)
Attached To
Mode
D1150: Wallet magic
Attached
Detach File
Event Timeline