diff --git a/src/app/Console/Commands/UserDiscount.php b/src/app/Console/Commands/UserDiscount.php new file mode 100644 --- /dev/null +++ b/src/app/Console/Commands/UserDiscount.php @@ -0,0 +1,55 @@ +argument('user'))->first(); + + if (!$user) { + return 1; + } + + $this->info("Found user {$user->id}"); + + $discount = (int)$this->argument('discount'); + + foreach ($user->wallets as $wallet) { + $wallet->discount = $discount; + $wallet->save(); + } + } +} diff --git a/src/app/Console/Commands/UserWallets.php b/src/app/Console/Commands/UserWallets.php new file mode 100644 --- /dev/null +++ b/src/app/Console/Commands/UserWallets.php @@ -0,0 +1,50 @@ +argument('user'))->first(); + + if (!$user) { + return 1; + } + + foreach ($user->wallets as $wallet) { + $this->info("{$wallet->id} {$wallet->description}"); + } + } +} diff --git a/src/app/Console/Commands/WalletDiscount.php b/src/app/Console/Commands/WalletDiscount.php new file mode 100644 --- /dev/null +++ b/src/app/Console/Commands/WalletDiscount.php @@ -0,0 +1,49 @@ +argument('wallet'))->first(); + + if (!$wallet) { + return 1; + } + + $wallet->discount = $this->argument('discount'); + $wallet->save(); + } +} diff --git a/src/app/Wallet.php b/src/app/Wallet.php --- a/src/app/Wallet.php +++ b/src/app/Wallet.php @@ -33,7 +33,8 @@ ]; protected $nullable = [ - 'description' + 'description', + 'discount' ]; protected $casts = [ @@ -76,7 +77,11 @@ if ($entitlement->updated_at <= Carbon::now()->subMonths(1)) { $diff = $entitlement->updated_at->diffInMonths(Carbon::now()); - $charges += $entitlement->cost * $diff; + \Log::info("discount: {$this->discount}"); + + $cost = (int)($entitlement->cost * ((100 - $this->discount) / 100) * $diff); + + $charges += $cost; // if we're in dry-run, you know... if (!$apply) { @@ -86,7 +91,7 @@ $entitlement->updated_at = $entitlement->updated_at->copy()->addMonths($diff); $entitlement->save(); - $this->debit($entitlement->cost * $diff); + $this->debit($cost * $diff); } } diff --git a/src/database/migrations/2020_03_26_074731_add_wallet_discount_column.php b/src/database/migrations/2020_03_26_074731_add_wallet_discount_column.php new file mode 100644 --- /dev/null +++ b/src/database/migrations/2020_03_26_074731_add_wallet_discount_column.php @@ -0,0 +1,39 @@ +integer('discount')->default(0)->nullable(); + } + ); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table( + 'wallets', + function (Blueprint $table) { + $table->dropColumn(['discount']); + } + ); + } +}