false, "title" => "Missing parameters" ]; echo json_encode($response); exit; } $month = $_REQUEST['month']; $year = $_REQUEST['year']; $apiKey = $_REQUEST['api_key']; $sql = "SELECT * FROM user WHERE api_key = :apiKey"; $stmt = $db->prepare($sql); $stmt->bindValue(':apiKey', $apiKey); $result = $stmt->execute(); $user = $result->fetchArray(SQLITE3_ASSOC); $sql = "SELECT * FROM last_exchange_update"; $result = $db->query($sql); $lastExchangeUpdate = $result->fetchArray(SQLITE3_ASSOC); $userId = $user['id']; $userCurrencyId = $user['main_currency']; $needsCurrencyConversion = false; $canConvertCurrency = empty($lastExchangeUpdate['date']) ? false : true; $sql = "SELECT * FROM currencies WHERE id = :currencyId"; $stmt = $db->prepare($sql); $stmt->bindValue(':currencyId', $userCurrencyId); $result = $stmt->execute(); $currency = $result->fetchArray(SQLITE3_ASSOC); $currency_code = $currency['code']; $currency_symbol = $currency['symbol']; $title = date('F Y', strtotime($year . '-' . $month . '-01')); $monthlyCost = 0; $notes = []; $sql = "SELECT * FROM subscriptions WHERE user_id = :userId AND inactive = 0"; $stmt = $db->prepare($sql); $stmt->bindValue(':userId', $userId); $result = $stmt->execute(); $subscriptions = []; while ($subscription = $result->fetchArray(SQLITE3_ASSOC)) { $subscriptions[] = $subscription; if ($subscription['currency_id'] !== $userCurrencyId) { $needsCurrencyConversion = true; } } if ($needsCurrencyConversion) { if (!$canConvertCurrency) { $notes[] = "You are using multiple currencies, but the exchange rates have not been updated yet. Please check your Fixer API Key."; } else { $sql = "SELECT * FROM currencies WHERE user_id = :userId"; $stmt = $db->prepare($sql); $stmt->bindValue(':userId', $userId); $result = $stmt->execute(); $currencies = []; while ($currency = $result->fetchArray(SQLITE3_ASSOC)) { $currencies[$currency['id']] = $currency['rate']; } } } // Calculate the monthly cost based on the next_payment_date, payment cycle, and payment frequency foreach ($subscriptions as $subscription) { $nextPaymentDate = strtotime($subscription['next_payment']); $cycle = $subscription['cycle']; // Integer from 1 to 4 $frequency = $subscription['frequency']; // Determine the strtotime increment string based on cycle switch ($cycle) { case 1: // Days $incrementString = "+{$frequency} days"; break; case 2: // Weeks $incrementString = "+{$frequency} weeks"; break; case 3: // Months $incrementString = "+{$frequency} months"; break; case 4: // Years $incrementString = "+{$frequency} years"; break; default: $incrementString = "+{$frequency} months"; // Default case, if needed } // Calculate the start of the month $startOfMonth = strtotime($year . '-' . str_pad($month, 2, '0', STR_PAD_LEFT) . '-01'); // Find the first payment date of the month by moving backwards $startDate = $nextPaymentDate; while ($startDate > $startOfMonth) { $startDate = strtotime("-" . $incrementString, $startDate); } // Calculate the monthly cost for ($date = $startDate; $date <= strtotime("+1 month", $startOfMonth); $date = strtotime($incrementString, $date)) { if (date('Y-m', $date) == $year . '-' . str_pad($month, 2, '0', STR_PAD_LEFT)) { $price = $subscription['price']; if ($userCurrencyId !== $subscription['currency_id']) { $price *= $currencies[$userCurrencyId] / $currencies[$subscription['currency_id']]; } $monthlyCost += $price; } } } $formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY); $localizedMonthlyCost = $formatter->formatCurrency($monthlyCost, $currency_code); echo json_encode([ 'success' => true, 'title' => $title, 'monthly_cost' => number_format($monthlyCost, 2), 'localized_monthly_cost' => $localizedMonthlyCost, 'currency_code' => $currency_code, 'currency_symbol' => $currency_symbol, 'notes' => $notes ], JSON_UNESCAPED_UNICODE); } ?>