diff --git a/src/app/Console/Commands/WalletBalance.php b/src/app/Console/Commands/WalletBalance.php new file mode 100644 --- /dev/null +++ b/src/app/Console/Commands/WalletBalance.php @@ -0,0 +1,48 @@ +argument('wallet')); + + if (!$wallet) { + return 1; + } + + $this->info($wallet->balance); + } +} 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,91 @@ +argument('wallet')); + + if (!$wallet) { + return 1; + } + + $balance = $wallet->balance; + + $discount = $wallet->discount ? $wallet->discount->discount : 0; + + $costs = 0; + + $fromDate = \Carbon\Carbon::now()->subMonthsWithoutOverflow(6); + $toDate = \Carbon\Carbon::now()->subMonthsWithoutOverflow(6); + + foreach ($wallet->entitlements as $entitlement) { + if ($entitlement->cost == 0) { + continue; + } + + if ($entitlement->created_at > $fromDate) { + $fromDate = $entitlement->created_at->copy(); + } + + if ($entitlement->updated_at > $toDate) { + $toDate = $entitlement->updated_at->copy(); + } + } + + $entitlements = \App\Entitlement::where('wallet_id', $wallet->id)->orderBy('updated_at'); + + foreach ($entitlements->get() as $entitlement) { + if ($entitlement->cost == 0) { + continue; + } + + $newToDate = $entitlement->updated_at->copy()->addMonthsWithoutOverflow(1); + + $cost = ((100 - $discount) / 100) * $entitlement->cost; + $costs += $cost; + + if ($balance >= $cost) { + $balance -= $cost; + if ($newToDate > $toDate) { + $toDate = $newToDate; + } + } + } + + $this->info("{$wallet->balance} lasts until {$toDate} (a month costs {$costs})"); + } +} diff --git a/src/app/Console/Development/WalletSetBalance.php b/src/app/Console/Development/WalletSetBalance.php new file mode 100644 --- /dev/null +++ b/src/app/Console/Development/WalletSetBalance.php @@ -0,0 +1,50 @@ +argument('wallet')); + + if (!$wallet) { + return 1; + } + + $wallet->balance = $this->argument('balance'); + + $wallet->save(); + } +}