2026_11_03_120000_add_promotion_coupons.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. use Illuminate\Database\Migrations\Migration;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Support\Facades\Schema;
  5. return new class extends Migration {
  6. /** F1 own-game order tables that accept a coupon; lottery parents and items both carry their share. */
  7. private const ORDER_TABLES = ['sport_order', 'sport_game_order', 'lhc_order', 'lhc_order_item'];
  8. public function up(): void
  9. {
  10. if (!Schema::hasTable('promotion_coupons')) {
  11. Schema::create('promotion_coupons', function (Blueprint $t) {
  12. $t->id(); $t->unsignedBigInteger('activity_id'); $t->unsignedBigInteger('reward_id');
  13. $t->unsignedBigInteger('user_id'); $t->string('member_id', 64); $t->string('role', 16);
  14. $t->unsignedBigInteger('inviter_user_id'); $t->unsignedInteger('seq');
  15. $t->decimal('min_amount', 24, 2); $t->decimal('discount', 24, 2); $t->json('categories');
  16. $t->dateTime('valid_from'); $t->dateTime('valid_until'); $t->string('status', 16)->default('unused');
  17. $t->string('used_source', 32)->nullable(); $t->decimal('used_stake', 24, 2)->nullable(); $t->dateTime('used_at')->nullable();
  18. $t->unique(['reward_id', 'user_id', 'role', 'seq'], 'promotion_coupon_issue');
  19. $t->index(['member_id', 'status', 'valid_until']); $t->index(['activity_id', 'user_id']); $t->timestamps();
  20. });
  21. }
  22. if (!Schema::hasTable('promotion_coupon_uses')) {
  23. Schema::create('promotion_coupon_uses', function (Blueprint $t) {
  24. $t->id(); $t->unsignedBigInteger('coupon_id'); $t->string('source', 32); $t->unsignedBigInteger('order_id');
  25. $t->decimal('discount', 24, 2); $t->dateTime('refunded_at')->nullable();
  26. $t->unique(['source', 'order_id']); $t->index('coupon_id'); $t->timestamps();
  27. });
  28. }
  29. foreach (self::ORDER_TABLES as $table) {
  30. if (!Schema::hasTable($table)) continue;
  31. Schema::table($table, function (Blueprint $t) use ($table) {
  32. if (!Schema::hasColumn($table, 'coupon_id')) $t->unsignedBigInteger('coupon_id')->nullable();
  33. if (!Schema::hasColumn($table, 'coupon_discount')) $t->decimal('coupon_discount', 14, 2)->default(0);
  34. });
  35. }
  36. }
  37. public function down(): void
  38. {
  39. foreach (self::ORDER_TABLES as $table) {
  40. if (!Schema::hasTable($table)) continue;
  41. Schema::table($table, function (Blueprint $t) use ($table) {
  42. foreach (['coupon_id', 'coupon_discount'] as $column) if (Schema::hasColumn($table, $column)) $t->dropColumn($column);
  43. });
  44. }
  45. Schema::dropIfExists('promotion_coupon_uses');
  46. Schema::dropIfExists('promotion_coupons');
  47. }
  48. };