Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F117891155
D1063.1775364339.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
11 KB
Referenced Files
None
Subscribers
None
D1063.1775364339.diff
View Options
diff --git a/src/app/Console/Commands/DiscountList.php b/src/app/Console/Commands/DiscountList.php
new file mode 100644
--- /dev/null
+++ b/src/app/Console/Commands/DiscountList.php
@@ -0,0 +1,52 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Discount;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\DB;
+
+class DiscountList extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'discount:list';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'List available (active) discounts';
+
+ /**
+ * Create a new command instance.
+ *
+ * @return void
+ */
+ public function __construct()
+ {
+ parent::__construct();
+ }
+
+ /**
+ * Execute the console command.
+ *
+ * @return mixed
+ */
+ public function handle()
+ {
+ Discount::where('active', true)->orderBy('discount')->get()->each(function ($discount) {
+ $name = $discount->description;
+
+ if ($discount->code) {
+ $name .= " [{$discount->code}]";
+ }
+
+ $this->info("{$discount->discount}%: {$name} ({$discount->id})");
+ });
+ }
+}
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,68 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+
+class UserDiscount extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'user:discount {user} {discount}';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = "Apply a discount to all of the user's wallets";
+
+ /**
+ * Create a new command instance.
+ *
+ * @return void
+ */
+ public function __construct()
+ {
+ parent::__construct();
+ }
+
+ /**
+ * Execute the console command.
+ *
+ * @return mixed
+ */
+ public function handle()
+ {
+ $user = \App\User::where('email', $this->argument('user'))->first();
+
+ if (!$user) {
+ return 1;
+ }
+
+ $this->info("Found user {$user->id}");
+
+ if ($this->argument('discount') === '0') {
+ $discount = null;
+ } else {
+ $discount = \App\Discount::find($this->argument('discount'));
+
+ if (!$discount) {
+ return 1;
+ }
+ }
+
+ foreach ($user->wallets as $wallet) {
+ if (!$discount) {
+ $wallet->discount()->dissociate();
+ } else {
+ $wallet->discount()->associate($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 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+
+class UserWallets extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'user:wallets {user}';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'List wallets for a user';
+
+ /**
+ * Create a new command instance.
+ *
+ * @return void
+ */
+ public function __construct()
+ {
+ parent::__construct();
+ }
+
+ /**
+ * Execute the console command.
+ *
+ * @return mixed
+ */
+ public function handle()
+ {
+ $user = \App\User::where('email', $this->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,64 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+
+class WalletDiscount extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'wallet:discount {wallet} {discount}';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = 'Apply a discount to a wallet';
+
+ /**
+ * Create a new command instance.
+ *
+ * @return void
+ */
+ public function __construct()
+ {
+ parent::__construct();
+ }
+
+ /**
+ * Execute the console command.
+ *
+ * @return mixed
+ */
+ public function handle()
+ {
+ $wallet = \App\Wallet::where('id', $this->argument('wallet'))->first();
+
+ if (!$wallet) {
+ return 1;
+ }
+
+ // FIXME: I see two possible issues with handling the discount argument
+ // 1. We can't make sure user didn't meant ID=10, but 10%
+ // 2. Using '0' for delete might also be not obvious
+
+ if ($this->argument('discount') === '0') {
+ $wallet->discount()->dissociate();
+ } else {
+ $discount = \App\Discount::find($this->argument('discount'));
+
+ if (!$discount) {
+ return 1;
+ }
+
+ $wallet->discount()->associate($discount);
+ }
+
+ $wallet->save();
+ }
+}
diff --git a/src/app/Discount.php b/src/app/Discount.php
new file mode 100644
--- /dev/null
+++ b/src/app/Discount.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace App;
+
+use Illuminate\Database\Eloquent\Model;
+use Spatie\Translatable\HasTranslations;
+
+/**
+ * The eloquent definition of a Discount.
+ */
+class Discount extends Model
+{
+ use HasTranslations;
+
+ protected $casts = [
+ 'discount' => 'integer',
+ ];
+
+ protected $fillable = [
+ 'active',
+ 'code',
+ 'description',
+ 'discount',
+ ];
+
+ /** @var array Translatable properties */
+ public $translatable = [
+ 'description',
+ ];
+
+ /**
+ * Discount value mutator
+ *
+ * @throws \Exception
+ */
+ public function setDiscountAttribute($discount)
+ {
+ $discount = (int) $discount;
+
+ if ($discount < 0 || $discount > 100) {
+ throw new \Exception("Invalid discount value, expected integer in range of 0-100");
+ }
+
+ $this->attributes['discount'] = $discount;
+ }
+
+ /**
+ * List of wallets with this discount assigned.
+ *
+ * @return \Illuminate\Database\Eloquent\Relations\HasMany
+ */
+ public function wallets()
+ {
+ return $this->hasMany('App\Wallet');
+ }
+}
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,7 @@
];
protected $nullable = [
- 'description'
+ 'description',
];
protected $casts = [
@@ -59,6 +59,8 @@
public function chargeEntitlements($apply = true)
{
$charges = 0;
+ $discount = $this->discount ? $this->discount->discount : 0;
+ $discount = (100 - $discount) / 100;
foreach ($this->entitlements()->get()->fresh() as $entitlement) {
// This entitlement has been created less than or equal to 14 days ago (this is at
@@ -76,7 +78,9 @@
if ($entitlement->updated_at <= Carbon::now()->subMonths(1)) {
$diff = $entitlement->updated_at->diffInMonths(Carbon::now());
- $charges += $entitlement->cost * $diff;
+ $cost = (int) ($entitlement->cost * $discount * $diff);
+
+ $charges += $cost;
// if we're in dry-run, you know...
if (!$apply) {
@@ -86,13 +90,23 @@
$entitlement->updated_at = $entitlement->updated_at->copy()->addMonths($diff);
$entitlement->save();
- $this->debit($entitlement->cost * $diff);
+ $this->debit($cost);
}
}
return $charges;
}
+ /**
+ * The discount assigned to the wallet.
+ *
+ * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
+ */
+ public function discount()
+ {
+ return $this->belongsTo('App\Discount', 'discount_id', 'id');
+ }
+
/**
* Calculate the expected charges to this wallet.
*
diff --git a/src/database/migrations/2020_03_30_100000_create_discounts.php b/src/database/migrations/2020_03_30_100000_create_discounts.php
new file mode 100644
--- /dev/null
+++ b/src/database/migrations/2020_03_30_100000_create_discounts.php
@@ -0,0 +1,56 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateDiscounts extends Migration
+{
+ /**
+ * Run the migrations.
+ *
+ * @return void
+ */
+ public function up()
+ {
+ Schema::dropIfExists('discounts');
+ Schema::create(
+ 'discounts',
+ function (Blueprint $table) {
+ $table->increments('id');
+ $table->tinyInteger('discount')->unsigned();
+ $table->text('description');
+ $table->string('code', 32)->nullable();
+ $table->boolean('active')->default(false);
+ $table->timestamps();
+ }
+ );
+
+ Schema::table(
+ 'wallets',
+ function (Blueprint $table) {
+ $table->integer('discount_id')->nullable();
+
+ $table->foreign('discount_id')->references('id')
+ ->on('discounts')->onDelete('set null');
+ }
+ );
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::dropIfExists('discounts');
+
+ Schema::table(
+ 'wallets',
+ function (Blueprint $table) {
+ $table->dropColumn('discount_id');
+ }
+ );
+ }
+}
diff --git a/src/database/seeds/DatabaseSeeder.php b/src/database/seeds/DatabaseSeeder.php
--- a/src/database/seeds/DatabaseSeeder.php
+++ b/src/database/seeds/DatabaseSeeder.php
@@ -13,6 +13,7 @@
{
$this->call(
[
+ DiscountSeeder::class,
DomainSeeder::class,
SkuSeeder::class,
PackageSeeder::class,
diff --git a/src/database/seeds/DiscountSeeder.php b/src/database/seeds/DiscountSeeder.php
new file mode 100644
--- /dev/null
+++ b/src/database/seeds/DiscountSeeder.php
@@ -0,0 +1,40 @@
+<?php
+
+use App\Discount;
+use Illuminate\Database\Seeder;
+
+class DiscountSeeder extends Seeder
+{
+ /**
+ * Run the database seeds.
+ *
+ * @return void
+ */
+ public function run()
+ {
+ Discount::create(
+ [
+ 'description' => 'Free Account',
+ 'discount' => 100,
+ 'active' => true,
+ ]
+ );
+
+ Discount::create(
+ [
+ 'description' => 'Student or Educational Institution',
+ 'discount' => 30,
+ 'active' => true,
+ ]
+ );
+
+ Discount::create(
+ [
+ 'description' => 'Test voucher',
+ 'discount' => 10,
+ 'active' => true,
+ 'code' => 'TEST',
+ ]
+ );
+ }
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Apr 5, 4:45 AM (6 h, 43 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18832363
Default Alt Text
D1063.1775364339.diff (11 KB)
Attached To
Mode
D1063: Wallet discounts
Attached
Detach File
Event Timeline