diff --git a/Dockerfile b/Dockerfile index fc63be7..2e46935 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,9 +10,10 @@ RUN apk add --no-cache sqlite-dev \ && docker-php-ext-enable pdo pdo_sqlite # Install additional PHP extensions and dependencies -RUN apk add --no-cache libpng libpng-dev libjpeg-turbo libjpeg-turbo-dev freetype freetype-dev curl autoconf libgomp \ +RUN apk add --no-cache libpng libpng-dev libjpeg-turbo libjpeg-turbo-dev freetype freetype-dev curl autoconf libgomp icu-dev \ && docker-php-ext-configure gd --with-freetype --with-jpeg \ - && docker-php-ext-install -j$(nproc) gd + && docker-php-ext-install -j$(nproc) gd \ + && docker-php-ext-install intl # Install Imagick extension RUN apk add --no-cache imagemagick imagemagick-dev \ diff --git a/README.md b/README.md index 6714f99..679e3d5 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ See instructions to run Wallos below. - curl - gd - imagick + - intl - openssl - sqlite3 diff --git a/endpoints/subscriptions/get.php b/endpoints/subscriptions/get.php index 5c47754..bb6f256 100644 --- a/endpoints/subscriptions/get.php +++ b/endpoints/subscriptions/get.php @@ -2,6 +2,7 @@ require_once '../../includes/connect_endpoint.php'; session_start(); + require_once '../../includes/currency_formatter.php'; require_once '../../includes/getdbkeys.php'; include_once '../../includes/list_subscriptions.php'; @@ -44,7 +45,7 @@ $frequency = $subscription['frequency']; $print[$id]['billing_cycle'] = getBillingCycle($cycle, $frequency); $paymentMethodId = $subscription['payment_method_id']; - $print[$id]['currency'] = $currencies[$subscription['currency_id']]['symbol']; + $print[$id]['currency_code'] = $currencies[$subscription['currency_id']]['code']; $currencyId = $subscription['currency_id']; $print[$id]['next_payment'] = date('M d, Y', strtotime($subscription['next_payment'])); $print[$id]['payment_method_icon'] = "images/uploads/icons/" . $payment_methods[$paymentMethodId]['icon']; @@ -55,16 +56,15 @@ if (isset($_COOKIE['convertCurrency']) && $_COOKIE['convertCurrency'] === 'true' && $currencyId != $mainCurrencyId) { $print[$id]['price'] = getPriceConverted($print[$id]['price'], $currencyId, $db); - $print[$id]['currency'] = $currencies[$mainCurrencyId]['symbol']; + $print[$id]['currency_code'] = $currencies[$mainCurrencyId]['code']; } if (isset($_COOKIE['showMonthlyPrice']) && $_COOKIE['showMonthlyPrice'] === 'true') { $print[$id]['price'] = getPricePerMonth($cycle, $frequency, $print[$id]['price']); } - $print[$id]['price'] = number_format($print[$id]['price'], 2, ".", ""); } if (isset($print)) { - printSubscriptons($print, $sort, $categories, $members); + printSubscriptions($print, $sort, $categories, $members); } if (count($subscriptions) == 0) { diff --git a/includes/currency_formatter.php b/includes/currency_formatter.php new file mode 100644 index 0000000..a1b3268 --- /dev/null +++ b/includes/currency_formatter.php @@ -0,0 +1,20 @@ +formatCurrency($amount, $currency); + } +} diff --git a/includes/header.php b/includes/header.php index f81e87d..5fa2a26 100644 --- a/includes/header.php +++ b/includes/header.php @@ -2,6 +2,7 @@ require_once 'connect.php'; require_once 'checkuser.php'; require_once 'checksession.php'; + require_once 'currency_formatter.php'; if ($userCount == 0) { $db->close(); diff --git a/includes/list_subscriptions.php b/includes/list_subscriptions.php index a8893cc..697a262 100644 --- a/includes/list_subscriptions.php +++ b/includes/list_subscriptions.php @@ -54,7 +54,7 @@ } } - function printSubscriptons($subscriptions, $sort, $categories, $members) { + function printSubscriptions($subscriptions, $sort, $categories, $members) { if ($sort === "price") { usort($subscriptions, function($a, $b) { return $a['price'] < $b['price'] ? 1 : -1; @@ -89,7 +89,7 @@ = $subscription['next_payment'] ?> - = $subscription['price'] ?>= $subscription['currency'] ?> + = CurrencyFormatter::format($subscription['price'], $subscription['currency_code']) ?> diff --git a/index.php b/index.php index efbeddf..5dec75d 100644 --- a/index.php +++ b/index.php @@ -72,7 +72,7 @@ $frequency = $subscription['frequency']; $print[$id]['billing_cycle'] = getBillingCycle($cycle, $frequency); $paymentMethodId = $subscription['payment_method_id']; - $print[$id]['currency'] = $currencies[$subscription['currency_id']]['symbol']; + $print[$id]['currency_code'] = $currencies[$subscription['currency_id']]['code']; $currencyId = $subscription['currency_id']; $print[$id]['next_payment'] = date('M d, Y', strtotime($subscription['next_payment'])); $print[$id]['payment_method_icon'] = "images/uploads/icons/" . $payment_methods[$paymentMethodId]['icon']; @@ -83,16 +83,15 @@ if (isset($_COOKIE['convertCurrency']) && $_COOKIE['convertCurrency'] === 'true' && $currencyId != $mainCurrencyId) { $print[$id]['price'] = getPriceConverted($print[$id]['price'], $currencyId, $db); - $print[$id]['currency'] = $currencies[$mainCurrencyId]['symbol']; + $print[$id]['currency_code'] = $currencies[$mainCurrencyId]['code']; } if (isset($_COOKIE['showMonthlyPrice']) && $_COOKIE['showMonthlyPrice'] === 'true') { $print[$id]['price'] = getPricePerMonth($cycle, $frequency, $print[$id]['price']); } - $print[$id]['price'] = number_format($print[$id]['price'], 2, ".", ""); } if (isset($print)) { - printSubscriptons($print, $sort, $categories, $members); + printSubscriptions($print, $sort, $categories, $members); } $db->close(); diff --git a/scripts/stats.js b/scripts/stats.js index a74f153..478e3ae 100644 --- a/scripts/stats.js +++ b/scripts/stats.js @@ -1,4 +1,4 @@ -function loadGraph(container, dataPoints, symbol, run) { +function loadGraph(container, dataPoints, currency, run) { if (run) { var ctx = document.getElementById(container).getContext('2d'); @@ -8,7 +8,7 @@ function loadGraph(container, dataPoints, symbol, run) { datasets: [{ data: dataPoints.map(point => point.y), }], - labels: dataPoints.map(point => `${point.label} (${point.y}${symbol})`), + labels: dataPoints.map(point => `(${new Intl.NumberFormat(navigator.language, { style: 'currency', currency }).format(point.y)})`), }, options: { animation: { diff --git a/stats.php b/stats.php index 1843073..00f0d29 100644 --- a/stats.php +++ b/stats.php @@ -64,15 +64,15 @@ while ($row = $result->fetchArray(SQLITE3_ASSOC)) { $categoryCost[$categoryId]['name'] = $row['name']; } -// Get symbol of main currency to display on statistics -$query = "SELECT c.symbol +// Get code of main currency to display on statistics +$query = "SELECT c.code FROM currencies c INNER JOIN user u ON c.id = u.main_currency WHERE u.id = 1"; $stmt = $db->prepare($query); $result = $stmt->execute(); $row = $result->fetchArray(SQLITE3_ASSOC); -$symbol = $row['symbol']; +$code = $row['code']; // Calculate active subscriptions @@ -113,16 +113,6 @@ if ($result) { $mostExpensiveSubscription = $price; } - $memberCost[$payerId]['cost'] = number_format($memberCost[$payerId]['cost'], 2, ".", ""); - if ((int)$memberCost[$payerId]['cost'] == $memberCost[$payerId]['cost']) { - $memberCost[$payerId]['cost'] = (int)$memberCost[$payerId]['cost']; - } - - $categoryCost[$categoryId]['cost'] = number_format($categoryCost[$categoryId]['cost'], 2, ".", ""); - if ((int)$categoryCost[$categoryId]['cost'] == $categoryCost[$categoryId]['cost']) { - $categoryCost[$categoryId]['cost'] = (int)$categoryCost[$categoryId]['cost']; - } - // Calculate ammount due this month $nextPaymentDate = DateTime::createFromFormat('Y-m-d', trim($next_payment)); $tomorrow = new DateTime('tomorrow'); @@ -144,26 +134,12 @@ if ($result) { } } - $mostExpensiveSubscription = number_format($mostExpensiveSubscription, 2, ".", ""); // Calculate yearly price $totalCostPerYear = $totalCostPerMonth * 12; - $totalCostPerYear = number_format($totalCostPerYear, 2, ".", ""); - if ((int)$totalCostPerYear == $totalCostPerYear) { - $totalCostPerYear = (int)$totalCostPerYear; - } // Calculate average subscription monthly cost $averageSubscriptionCost = $totalCostPerMonth / $activeSubscriptions; - $averageSubscriptionCost = number_format($averageSubscriptionCost, 2, ".", ""); - if ((int)$averageSubscriptionCost == $averageSubscriptionCost) { - $averageSubscriptionCost = (int)$averageSubscriptionCost; - } - - $totalCostPerMonth = number_format($totalCostPerMonth, 2, ".", ""); - if ((int)$totalCostPerMonth == $totalCostPerMonth) { - $totalCostPerMonth = (int)$totalCostPerMonth; - } } else { $totalCostPerYear = 0; $averageSubscriptionCost = 0; @@ -179,23 +155,23 @@ if ($result) { Active Subscriptions - = $totalCostPerMonth ?>= $symbol ?> + = CurrencyFormatter::format($totalCostPerMonth, $code) ?> Monthly Cost - = $totalCostPerYear ?>= $symbol ?> + = CurrencyFormatter::format($totalCostPerYear, $code) ?> Yearly Cost - = $averageSubscriptionCost ?>= $symbol ?> + = CurrencyFormatter::format($averageSubscriptionCost, $code) ?> Average Monthly Subscription Cost - = $mostExpensiveSubscription ?>= $symbol ?> + = CurrencyFormatter::format($mostExpensiveSubscription, $code) ?> Most Expensive Subscription Cost - = number_format($amountDueThisMonth, 2, ".", "") ?>= $symbol ?> + = CurrencyFormatter::format($amountDueThisMonth, $code) ?> Amount due this month