| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <?php
- use Illuminate\Database\Migrations\Migration;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Support\Facades\Schema;
- return new class extends Migration {
- /** F1 own-game order tables that accept a coupon; lottery parents and items both carry their share. */
- private const ORDER_TABLES = ['sport_order', 'sport_game_order', 'lhc_order', 'lhc_order_item'];
- public function up(): void
- {
- if (!Schema::hasTable('promotion_coupons')) {
- Schema::create('promotion_coupons', function (Blueprint $t) {
- $t->id(); $t->unsignedBigInteger('activity_id'); $t->unsignedBigInteger('reward_id');
- $t->unsignedBigInteger('user_id'); $t->string('member_id', 64); $t->string('role', 16);
- $t->unsignedBigInteger('inviter_user_id'); $t->unsignedInteger('seq');
- $t->decimal('min_amount', 24, 2); $t->decimal('discount', 24, 2); $t->json('categories');
- $t->dateTime('valid_from'); $t->dateTime('valid_until'); $t->string('status', 16)->default('unused');
- $t->string('used_source', 32)->nullable(); $t->decimal('used_stake', 24, 2)->nullable(); $t->dateTime('used_at')->nullable();
- $t->unique(['reward_id', 'user_id', 'role', 'seq'], 'promotion_coupon_issue');
- $t->index(['member_id', 'status', 'valid_until']); $t->index(['activity_id', 'user_id']); $t->timestamps();
- });
- }
- if (!Schema::hasTable('promotion_coupon_uses')) {
- Schema::create('promotion_coupon_uses', function (Blueprint $t) {
- $t->id(); $t->unsignedBigInteger('coupon_id'); $t->string('source', 32); $t->unsignedBigInteger('order_id');
- $t->decimal('discount', 24, 2); $t->dateTime('refunded_at')->nullable();
- $t->unique(['source', 'order_id']); $t->index('coupon_id'); $t->timestamps();
- });
- }
- foreach (self::ORDER_TABLES as $table) {
- if (!Schema::hasTable($table)) continue;
- Schema::table($table, function (Blueprint $t) use ($table) {
- if (!Schema::hasColumn($table, 'coupon_id')) $t->unsignedBigInteger('coupon_id')->nullable();
- if (!Schema::hasColumn($table, 'coupon_discount')) $t->decimal('coupon_discount', 14, 2)->default(0);
- });
- }
- }
- public function down(): void
- {
- foreach (self::ORDER_TABLES as $table) {
- if (!Schema::hasTable($table)) continue;
- Schema::table($table, function (Blueprint $t) use ($table) {
- foreach (['coupon_id', 'coupon_discount'] as $column) if (Schema::hasColumn($table, $column)) $t->dropColumn($column);
- });
- }
- Schema::dropIfExists('promotion_coupon_uses');
- Schema::dropIfExists('promotion_coupons');
- }
- };
|