coupon code system eingeführt. coupons werden vom super admin gemanaged. coupons werden mit paddle synchronisiert und dort validiert. plus: einige mobil-optimierungen im tenant admin pwa.

This commit is contained in:
Codex Agent
2025-11-09 20:26:50 +01:00
parent f3c44be76d
commit 082b78cd43
80 changed files with 4855 additions and 435 deletions

View File

@@ -0,0 +1,51 @@
<?php
namespace Database\Factories;
use App\Enums\CouponStatus;
use App\Enums\CouponType;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Coupon>
*/
class CouponFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
$type = $this->faker->randomElement([
CouponType::PERCENTAGE,
CouponType::FLAT,
]);
$amount = $type === CouponType::PERCENTAGE
? $this->faker->numberBetween(5, 40)
: $this->faker->numberBetween(5, 150);
return [
'name' => $this->faker->words(3, true),
'code' => Str::upper(Str::random(8)),
'type' => $type,
'amount' => $amount,
'currency' => $type === CouponType::PERCENTAGE ? null : 'EUR',
'status' => CouponStatus::ACTIVE,
'is_stackable' => false,
'enabled_for_checkout' => true,
'auto_apply' => false,
'usage_limit' => 100,
'per_customer_limit' => 1,
'description' => $this->faker->sentence(),
'metadata' => ['note' => 'factory'],
'starts_at' => now()->subDay(),
'ends_at' => now()->addMonth(),
'paddle_discount_id' => 'dsc_'.Str::upper(Str::random(10)),
'paddle_mode' => 'standard',
];
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Database\Factories;
use App\Models\Coupon;
use App\Models\CouponRedemption;
use App\Models\Package;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\CouponRedemption>
*/
class CouponRedemptionFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'coupon_id' => Coupon::factory(),
'package_id' => Package::factory(),
'tenant_id' => Tenant::factory(),
'user_id' => User::factory(),
'status' => CouponRedemption::STATUS_SUCCESS,
'amount_discounted' => $this->faker->randomFloat(2, 5, 150),
'currency' => 'EUR',
'redeemed_at' => now(),
];
}
}

View File

@@ -0,0 +1,61 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('coupons', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('code')->unique();
$table->string('type', 40);
$table->decimal('amount', 10, 2)->default(0);
$table->char('currency', 3)->nullable();
$table->string('status', 40)->default('draft');
$table->boolean('is_stackable')->default(false);
$table->boolean('enabled_for_checkout')->default(true);
$table->boolean('auto_apply')->default(false);
$table->unsignedInteger('usage_limit')->nullable();
$table->unsignedInteger('per_customer_limit')->nullable();
$table->unsignedInteger('redemptions_count')->default(0);
$table->text('description')->nullable();
$table->json('metadata')->nullable();
$table->timestamp('starts_at')->nullable();
$table->timestamp('ends_at')->nullable();
$table->string('paddle_discount_id')->nullable()->unique();
$table->string('paddle_mode', 40)->default('standard');
$table->json('paddle_snapshot')->nullable();
$table->timestamp('paddle_last_synced_at')->nullable();
$table->foreignIdFor(\App\Models\User::class, 'created_by')->nullable()->constrained('users')->nullOnDelete();
$table->foreignIdFor(\App\Models\User::class, 'updated_by')->nullable()->constrained('users')->nullOnDelete();
$table->timestamps();
$table->softDeletes();
$table->index(['status', 'starts_at', 'ends_at']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('coupons');
}
};

View File

@@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('coupon_package', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(\App\Models\Coupon::class)->constrained()->cascadeOnDelete();
$table->foreignIdFor(\App\Models\Package::class)->constrained()->cascadeOnDelete();
$table->timestamps();
$table->unique(['coupon_id', 'package_id']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('coupon_package');
}
};

View File

@@ -0,0 +1,47 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('coupon_redemptions', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(\App\Models\Coupon::class)->constrained()->cascadeOnDelete();
$table->foreignUuid('checkout_session_id')->nullable()->constrained('checkout_sessions')->nullOnDelete();
$table->foreignIdFor(\App\Models\Package::class)->nullable()->constrained()->nullOnDelete();
$table->foreignIdFor(\App\Models\Tenant::class)->nullable()->constrained()->nullOnDelete();
$table->foreignIdFor(\App\Models\User::class)->nullable()->constrained()->nullOnDelete();
$table->string('paddle_transaction_id')->nullable();
$table->string('status', 40)->default('pending');
$table->text('failure_reason')->nullable();
$table->decimal('amount_discounted', 10, 2)->default(0);
$table->char('currency', 3)->default('EUR');
$table->json('metadata')->nullable();
$table->timestamp('redeemed_at')->nullable();
$table->timestamps();
$table->index(['status', 'redeemed_at']);
$table->unique('paddle_transaction_id');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('coupon_redemptions');
}
};

View File

@@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('checkout_sessions', function (Blueprint $table) {
$table->foreignIdFor(\App\Models\Coupon::class)->nullable()->after('package_snapshot')->constrained()->nullOnDelete();
$table->string('coupon_code')->nullable()->after('coupon_id');
$table->json('coupon_snapshot')->nullable()->after('coupon_code');
$table->decimal('amount_discount', 10, 2)->default(0)->after('amount_total');
$table->json('discount_breakdown')->nullable()->after('amount_discount');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('checkout_sessions', function (Blueprint $table) {
$table->dropConstrainedForeignId('coupon_id');
$table->dropColumn(['coupon_code', 'coupon_snapshot', 'amount_discount', 'discount_breakdown']);
});
}
};