Jake Vanderwerf
2025-10-18 b0194e10a87e16797a568d8a30d53ebecd27d8a4
inc/registry/CheckCustomTables.php
@@ -72,6 +72,13 @@
            error_log("JVB: Error in dashboard section: " . $e->getMessage());
         }
         try {
            if (array_key_exists('referrals', $this->JVB_SITE) && $this->JVB_SITE['referrals']) {
               $tables = array_merge($tables, $this->referralTables());
            }
         } catch (Exception $e) {
            error_log("JVB: Error in referrals section: " . $e->getMessage());
         }
         // RE-ENABLE favourites tables
         try {
            if ($this->JVB_SITE['favourites']) {
@@ -1146,6 +1153,68 @@
    }
   /**
    * Create referral tracking tables
    *
    * Call this from the main table creation method in CheckCustomTables.php:
    * $tables = array_merge($tables, $this->referralTables());
    */
   protected function referralTables(): array
   {
      $tables = [];
      // Main referrals table
      $tables['referrals'] = "(
        `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
        `referrer_id` bigint(20) unsigned NOT NULL,
        `referee_id` bigint(20) unsigned NOT NULL,
        `referee_name` varchar(255) NOT NULL,
        `referee_email` varchar(255) NOT NULL,
        `referee_phone` varchar(50) DEFAULT NULL,
        `referral_code` varchar(50) NOT NULL,
        `status` enum('pending', 'treated', 'cancelled') DEFAULT 'pending',
        `referred_at` datetime NOT NULL,
        `treated_at` datetime DEFAULT NULL,
        `notes` text DEFAULT NULL,
        PRIMARY KEY (`id`),
        UNIQUE KEY `referee_unique` (`referee_id`),
        KEY `referrer_idx` (`referrer_id`),
        KEY `status_idx` (`status`),
        KEY `code_idx` (`referral_code`),
        KEY `date_idx` (`referred_at`),
        CONSTRAINT `{$this->base}referral_referrer_fk` FOREIGN KEY (`referrer_id`)
            REFERENCES {$this->wpdb->users} (`ID`) ON DELETE CASCADE,
        CONSTRAINT `{$this->base}referral_referee_fk` FOREIGN KEY (`referee_id`)
            REFERENCES {$this->wpdb->users} (`ID`) ON DELETE CASCADE
    )";
      // Rewards table
      $tables['referral_rewards'] = "(
        `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
        `referral_id` bigint(20) unsigned NOT NULL,
        `user_id` bigint(20) unsigned NOT NULL,
        `reward_type` enum('referrer', 'referee') NOT NULL,
        `amount` decimal(10,2) NOT NULL,
        `reward_calculation` varchar(20) DEFAULT NULL COMMENT 'percentage or fixed',
        `status` enum('available', 'redeemed', 'expired', 'cancelled') DEFAULT 'available',
        `created_at` datetime NOT NULL,
        `redeemed_at` datetime DEFAULT NULL,
        `expires_at` datetime DEFAULT NULL,
        `notes` text DEFAULT NULL,
        PRIMARY KEY (`id`),
        KEY `referral_idx` (`referral_id`),
        KEY `user_idx` (`user_id`),
        KEY `status_idx` (`status`),
        KEY `type_idx` (`reward_type`),
        CONSTRAINT `{$this->base}reward_referral_fk` FOREIGN KEY (`referral_id`)
            REFERENCES {$this->wpdb->prefix}" . BASE . "referrals (`id`) ON DELETE CASCADE,
        CONSTRAINT `{$this->base}reward_user_fk` FOREIGN KEY (`user_id`)
            REFERENCES {$this->wpdb->users} (`ID`) ON DELETE CASCADE
    )";
      return $tables;
   }
    /*******************************************************************************************
     * These methods help create a content-type taxonomy, like the tattoo shops in edmonton.ink
     *     To set up, ensure that some fields in the registered taxonomy include 'content_table' => true
@@ -1538,4 +1607,5 @@
        return "(\n    " . implode(",\n    ", $allDefinitions) . "\n)";
    }
}