![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/domains/soundstudiopro.com/private_html/cron/ |
<?php
/**
* Subscription Period Usage Initialization Cron Job
*
* This cron ensures all active subscriptions have usage records for their current period.
* It's a backup in case webhooks fail to initialize usage on subscription renewal.
*
* Run daily (e.g., via cron: 0 2 * * *) to catch any missed renewals
*/
// Prevent web access
if (php_sapi_name() !== 'cli' && (!isset($_GET['cron_key']) || $_GET['cron_key'] !== 'YOUR_SECRET_CRON_KEY')) {
die("Access denied");
}
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../utils/subscription_helpers.php';
$pdo = getDBConnection();
echo "=== Subscription Period Usage Check - " . date('Y-m-d H:i:s') . " ===\n";
try {
// Get all active subscriptions
$stmt = $pdo->prepare("
SELECT us.id, us.user_id, us.plan_name, us.current_period_start, us.current_period_end
FROM user_subscriptions us
WHERE us.status = 'active'
AND us.current_period_end > NOW()
");
$stmt->execute();
$subscriptions = $stmt->fetchAll(PDO::FETCH_ASSOC);
$initialized_count = 0;
$error_count = 0;
$plans_config = require __DIR__ . '/../config/subscription_plans.php';
foreach ($subscriptions as $sub) {
try {
$user_id = $sub['user_id'];
$subscription_id = $sub['id'];
$plan_name = $sub['plan_name'];
$period_start = $sub['current_period_start'];
// Convert period_start to datetime string if needed
if (is_numeric($period_start)) {
$period_start = date('Y-m-d H:i:s', $period_start);
}
// Check if usage record exists for this period
$check_stmt = $pdo->prepare("
SELECT id FROM monthly_track_usage
WHERE user_id = ? AND subscription_period_start = ?
");
$check_stmt->execute([$user_id, $period_start]);
$existing = $check_stmt->fetch();
if (!$existing) {
// No usage record for current period - create one
$track_limit = 5; // Default
if (isset($plans_config[$plan_name])) {
$track_limit = $plans_config[$plan_name]['tracks_per_month'];
}
$year_month = date('Y-m', strtotime($period_start)); // For backward compatibility
$stmt = $pdo->prepare("
INSERT INTO monthly_track_usage (
user_id, subscription_id, subscription_period_start,
year_month, tracks_created, track_limit, reset_at
)
VALUES (?, ?, ?, ?, 0, ?, NOW())
");
$stmt->execute([
$user_id,
$subscription_id,
$period_start,
$year_month,
$track_limit
]);
$initialized_count++;
echo "✓ Initialized usage for user {$user_id} ({$plan_name} plan) - Period: {$period_start}, Limit: {$track_limit}\n";
}
} catch (Exception $e) {
$error_count++;
echo "✗ Error processing subscription {$sub['id']}: " . $e->getMessage() . "\n";
}
}
echo "\n=== Summary ===\n";
echo "Total active subscriptions: " . count($subscriptions) . "\n";
echo "Usage records initialized: {$initialized_count}\n";
echo "Errors: {$error_count}\n";
// Log to file
$log_entry = [
'timestamp' => date('Y-m-d H:i:s'),
'subscriptions_checked' => count($subscriptions),
'initialized' => $initialized_count,
'errors' => $error_count
];
$log_file = __DIR__ . '/../logs/subscription_usage_check.log';
file_put_contents($log_file, json_encode($log_entry) . "\n", FILE_APPEND | LOCK_EX);
} catch (Exception $e) {
echo "Fatal error: " . $e->getMessage() . "\n";
$error_log = [
'timestamp' => date('Y-m-d H:i:s'),
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString()
];
$log_file = __DIR__ . '/../logs/subscription_usage_check.log';
file_put_contents($log_file, json_encode($error_log) . "\n", FILE_APPEND | LOCK_EX);
}
echo "\n=== Check Complete ===\n";
?>