336 lines
16 KiB
PHP
336 lines
16 KiB
PHP
<?php
|
|
|
|
use Illuminate\Database\Migrations\Migration;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
return new class extends Migration
|
|
{
|
|
public function up(): void
|
|
{
|
|
// Packages table
|
|
if (!Schema::hasTable('packages')) {
|
|
Schema::create('packages', function (Blueprint $table) {
|
|
$table->id();
|
|
$table->string('name');
|
|
$table->enum('type', ['endcustomer', 'reseller']);
|
|
$table->decimal('price', 8, 2);
|
|
$table->integer('max_photos')->nullable();
|
|
$table->integer('max_guests')->nullable();
|
|
$table->integer('gallery_days')->nullable();
|
|
$table->integer('max_tasks')->nullable();
|
|
$table->boolean('watermark_allowed')->default(true);
|
|
$table->boolean('branding_allowed')->default(false);
|
|
$table->integer('max_events_per_year')->nullable();
|
|
$table->timestamp('expires_after')->nullable();
|
|
$table->json('features')->nullable();
|
|
$table->timestamps();
|
|
$table->index(['type', 'price']);
|
|
});
|
|
|
|
// Seed standard packages if empty
|
|
if (DB::table('packages')->count() == 0) {
|
|
DB::table('packages')->insert([
|
|
[
|
|
'name' => 'Free/Test',
|
|
'type' => 'endcustomer',
|
|
'price' => 0.00,
|
|
'max_photos' => 30,
|
|
'max_guests' => 10,
|
|
'gallery_days' => 3,
|
|
'max_tasks' => 1,
|
|
'watermark_allowed' => true,
|
|
'branding_allowed' => false,
|
|
'max_events_per_year' => null,
|
|
'expires_after' => null,
|
|
'features' => json_encode([]),
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
],
|
|
[
|
|
'name' => 'Starter',
|
|
'type' => 'endcustomer',
|
|
'price' => 19.00,
|
|
'max_photos' => 300,
|
|
'max_guests' => 50,
|
|
'gallery_days' => 14,
|
|
'max_tasks' => 5,
|
|
'watermark_allowed' => true,
|
|
'branding_allowed' => false,
|
|
'max_events_per_year' => null,
|
|
'expires_after' => null,
|
|
'features' => json_encode([]),
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
],
|
|
[
|
|
'name' => 'Reseller S',
|
|
'type' => 'reseller',
|
|
'price' => 149.00,
|
|
'max_photos' => null,
|
|
'max_guests' => null,
|
|
'gallery_days' => null,
|
|
'max_tasks' => null,
|
|
'watermark_allowed' => true,
|
|
'branding_allowed' => true,
|
|
'max_events_per_year' => 5,
|
|
'expires_after' => now()->addYear(),
|
|
'features' => json_encode(['limited_branding']),
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
],
|
|
// Add more as needed
|
|
]);
|
|
}
|
|
}
|
|
|
|
// Event Packages
|
|
if (!Schema::hasTable('event_packages')) {
|
|
Schema::create('event_packages', function (Blueprint $table) {
|
|
$table->id();
|
|
$table->foreignId('event_id')->constrained()->cascadeOnDelete();
|
|
$table->foreignId('package_id')->constrained()->cascadeOnDelete();
|
|
$table->decimal('purchased_price', 8, 2);
|
|
$table->timestamp('purchased_at');
|
|
$table->integer('used_photos')->default(0);
|
|
$table->timestamps();
|
|
$table->index('event_id');
|
|
});
|
|
}
|
|
|
|
// Tenant Packages
|
|
if (!Schema::hasTable('tenant_packages')) {
|
|
Schema::create('tenant_packages', function (Blueprint $table) {
|
|
$table->id();
|
|
$table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
|
|
$table->foreignId('package_id')->constrained()->cascadeOnDelete();
|
|
$table->decimal('price', 8, 2);
|
|
$table->timestamp('purchased_at');
|
|
$table->timestamp('expires_at');
|
|
$table->integer('used_events')->default(0);
|
|
$table->boolean('active')->default(true);
|
|
$table->timestamps();
|
|
$table->index(['tenant_id', 'active']);
|
|
});
|
|
}
|
|
|
|
// Package Purchases
|
|
if (!Schema::hasTable('package_purchases')) {
|
|
Schema::create('package_purchases', function (Blueprint $table) {
|
|
$table->id();
|
|
$table->foreignId('tenant_id')->nullable()->constrained();
|
|
$table->foreignId('event_id')->nullable()->constrained();
|
|
$table->foreignId('package_id')->constrained();
|
|
$table->string('provider_id');
|
|
$table->decimal('price', 8, 2);
|
|
$table->timestamp('purchased_at');
|
|
$table->enum('type', ['endcustomer_event', 'reseller_subscription']);
|
|
$table->json('metadata')->nullable();
|
|
$table->string('ip_address')->nullable();
|
|
$table->string('user_agent')->nullable();
|
|
$table->boolean('refunded')->default(false);
|
|
$table->timestamps();
|
|
$table->index(['tenant_id', 'created_at']);
|
|
});
|
|
}
|
|
|
|
// Purchase History
|
|
if (!Schema::hasTable('purchase_history')) {
|
|
Schema::create('purchase_history', function (Blueprint $table) {
|
|
$table->string('id', 255)->primary();
|
|
$table->string('tenant_id', 255);
|
|
$table->string('package_id', 255);
|
|
$table->integer('credits_added')->default(0);
|
|
$table->decimal('price', 10, 2)->default(0);
|
|
$table->string('currency', 3)->default('EUR');
|
|
$table->string('platform', 50);
|
|
$table->string('transaction_id', 255)->nullable();
|
|
$table->timestamp('purchased_at')->useCurrent();
|
|
$table->timestamp('created_at')->useCurrent();
|
|
$table->foreign('tenant_id')->references('id')->on('tenants');
|
|
$table->index('tenant_id');
|
|
$table->index('purchased_at');
|
|
$table->index('transaction_id');
|
|
});
|
|
}
|
|
|
|
// Add subscription fields to tenants if missing
|
|
if (Schema::hasTable('tenants')) {
|
|
if (!Schema::hasColumn('tenants', 'subscription_tier')) {
|
|
Schema::table('tenants', function (Blueprint $table) {
|
|
$table->string('subscription_tier')->default('free')->after('event_credits_balance');
|
|
});
|
|
}
|
|
if (!Schema::hasColumn('tenants', 'subscription_status')) {
|
|
Schema::table('tenants', function (Blueprint $table) {
|
|
$table->enum('subscription_status', ['free', 'active', 'suspended', 'expired'])->default('free')->after('subscription_tier');
|
|
});
|
|
}
|
|
if (!Schema::hasColumn('tenants', 'subscription_expires_at')) {
|
|
Schema::table('tenants', function (Blueprint $table) {
|
|
$table->timestamp('subscription_expires_at')->nullable()->after('subscription_status');
|
|
});
|
|
}
|
|
if (!Schema::hasColumn('tenants', 'total_revenue')) {
|
|
Schema::table('tenants', function (Blueprint $table) {
|
|
$table->decimal('total_revenue', 10, 2)->default(0.00)->after('subscription_expires_at');
|
|
});
|
|
}
|
|
}
|
|
|
|
// Idempotent migration from credits to packages (only if old tables exist and new don't have data)
|
|
if (Schema::hasTable('event_credits_ledger') && DB::table('tenant_packages')->count() == 0) {
|
|
// Migrate tenant credits to tenant_packages (Free package)
|
|
$freePackageId = DB::table('packages')->where('name', 'Free/Test')->value('id');
|
|
if ($freePackageId) {
|
|
DB::table('tenants')->where('event_credits_balance', '>', 0)->chunk(100, function ($tenants) use ($freePackageId) {
|
|
foreach ($tenants as $tenant) {
|
|
DB::table('tenant_packages')->insertOrIgnore([
|
|
'tenant_id' => $tenant->id,
|
|
'package_id' => $freePackageId,
|
|
'price' => 0.00,
|
|
'purchased_at' => $tenant->free_event_granted_at ?? now(),
|
|
'expires_at' => now()->addDays(30),
|
|
'used_events' => min($tenant->event_credits_balance, 1),
|
|
'active' => true,
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
|
|
DB::table('package_purchases')->insertOrIgnore([
|
|
'tenant_id' => $tenant->id,
|
|
'event_id' => null,
|
|
'package_id' => $freePackageId,
|
|
'provider_id' => 'migration_free',
|
|
'price' => 0.00,
|
|
'type' => 'reseller_subscription',
|
|
'metadata' => json_encode(['migrated_from_credits' => $tenant->event_credits_balance]),
|
|
'ip_address' => null,
|
|
'user_agent' => null,
|
|
'refunded' => false,
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
}
|
|
});
|
|
|
|
// Migrate event purchases if old data exists
|
|
if (Schema::hasTable('event_purchases')) {
|
|
DB::table('event_purchases')->join('events', 'event_purchases.event_id', '=', 'events.id')->chunk(100, function ($purchases) use ($freePackageId) {
|
|
foreach ($purchases as $purchase) {
|
|
DB::table('event_packages')->insertOrIgnore([
|
|
'event_id' => $purchase->event_id,
|
|
'package_id' => $freePackageId,
|
|
'purchased_price' => $purchase->amount ?? 0.00,
|
|
'purchased_at' => $purchase->purchased_at ?? now(),
|
|
'used_photos' => 0,
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
|
|
DB::table('package_purchases')->insertOrIgnore([
|
|
'tenant_id' => $purchase->tenant_id,
|
|
'event_id' => $purchase->event_id,
|
|
'package_id' => $freePackageId,
|
|
'provider_id' => $purchase->provider ?? 'migration',
|
|
'price' => $purchase->amount ?? 0.00,
|
|
'type' => 'endcustomer_event',
|
|
'metadata' => json_encode(['migrated_from_event_purchases' => true]),
|
|
'ip_address' => null,
|
|
'user_agent' => null,
|
|
'refunded' => false,
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
// Conditional drop of old credits tables and fields (only if migration happened or old structures exist)
|
|
if (Schema::hasTable('event_credits_ledger')) {
|
|
Schema::dropIfExists('event_credits_ledger');
|
|
}
|
|
if (Schema::hasTable('event_purchases')) {
|
|
Schema::dropIfExists('event_purchases');
|
|
}
|
|
if (Schema::hasTable('purchase_history') && DB::table('package_purchases')->count() > 0) { // Only drop if new data exists
|
|
Schema::dropIfExists('purchase_history');
|
|
}
|
|
|
|
// Drop old fields from tenants if new system is in place
|
|
if (Schema::hasTable('tenants')) {
|
|
$oldFields = ['event_credits_balance', 'free_event_granted_at'];
|
|
foreach ($oldFields as $field) {
|
|
if (Schema::hasColumn('tenants', $field) && DB::table('tenant_packages')->count() > 0) {
|
|
Schema::table('tenants', function (Blueprint $table) use ($field) {
|
|
$table->dropColumn($field);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public function down(): void
|
|
{
|
|
if (app()->environment('local', 'testing')) {
|
|
// Reverse drops and adds
|
|
if (!Schema::hasTable('purchase_history')) {
|
|
Schema::create('purchase_history', function (Blueprint $table) {
|
|
$table->id();
|
|
$table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
|
|
$table->string('package_id', 255);
|
|
$table->integer('credits_added')->default(0);
|
|
$table->decimal('price', 10, 2)->default(0);
|
|
$table->string('provider_id');
|
|
$table->timestamps();
|
|
});
|
|
}
|
|
if (!Schema::hasTable('event_purchases')) {
|
|
Schema::create('event_purchases', function (Blueprint $table) {
|
|
$table->id();
|
|
$table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
|
|
$table->unsignedInteger('events_purchased')->default(1);
|
|
$table->decimal('amount', 10, 2);
|
|
$table->string('currency', 3)->default('EUR');
|
|
$table->string('provider', 32);
|
|
$table->string('external_receipt_id')->nullable();
|
|
$table->string('status', 16)->default('pending');
|
|
$table->timestamp('purchased_at')->nullable();
|
|
$table->timestamps();
|
|
$table->index(['tenant_id', 'purchased_at']);
|
|
});
|
|
}
|
|
if (!Schema::hasTable('event_credits_ledger')) {
|
|
Schema::create('event_credits_ledger', function (Blueprint $table) {
|
|
$table->id();
|
|
$table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
|
|
$table->integer('delta');
|
|
$table->string('reason', 32);
|
|
$table->foreignId('related_purchase_id')->nullable()->constrained('event_purchases')->nullOnDelete();
|
|
$table->text('note')->nullable();
|
|
$table->timestamps();
|
|
$table->index(['tenant_id', 'created_at']);
|
|
});
|
|
}
|
|
|
|
// Re-add old fields to tenants
|
|
Schema::table('tenants', function (Blueprint $table) {
|
|
if (!Schema::hasColumn('tenants', 'event_credits_balance')) {
|
|
$table->integer('event_credits_balance')->default(1);
|
|
}
|
|
if (!Schema::hasColumn('tenants', 'free_event_granted_at')) {
|
|
$table->timestamp('free_event_granted_at')->nullable();
|
|
}
|
|
});
|
|
|
|
// Drop new tables
|
|
Schema::dropIfExists('package_purchases');
|
|
Schema::dropIfExists('tenant_packages');
|
|
Schema::dropIfExists('event_packages');
|
|
Schema::dropIfExists('packages');
|
|
}
|
|
}
|
|
}; |