= ? LIMIT 1", [$companyId, $now, $now] ); } /** * Create or update subscription for a company */ public static function subscribeCompany(int $companyId, int $planId, int $durationDays = 30, ?string $gateway = null, ?string $ref = null): string { $now = time(); $startsAt = date('Y-m-d H:i:s', $now); $endsAt = date('Y-m-d H:i:s', $now + ($durationDays * 86400)); // Deactivate previous active subscriptions Database::execute( "UPDATE " . static::$table . " SET status = 'expired' WHERE company_id = ? AND status = 'active'", [$companyId] ); return self::create([ 'company_id' => $companyId, 'plan_id' => $planId, 'status' => 'active', 'starts_at' => $startsAt, 'ends_at' => $endsAt, 'payment_gateway' => $gateway, 'subscription_ref' => $ref ]); } }