From 73f2d8f07f151b139b1a21a812f3017af48c6ebf Mon Sep 17 00:00:00 2001 From: cp6 Date: Sun, 29 May 2022 16:46:55 +1000 Subject: [PATCH] Added dashboard currency setting Added dashboard currency setting --- app/Http/Controllers/HomeController.php | 3 ++- app/Http/Controllers/SettingsController.php | 6 +++-- app/Models/Home.php | 29 ++++++++++++--------- app/Models/Pricing.php | 21 +++++++++++++++ app/Models/Settings.php | 1 + resources/views/home.blade.php | 8 +++--- resources/views/settings/index.blade.php | 7 +++++ 7 files changed, 56 insertions(+), 19 deletions(-) diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index a1f0ce9..d5d5bfc 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -65,7 +65,8 @@ class HomeController extends Controller 'due_soon' => $due_soon, 'newest' => $recently_added, 'execution_time' => number_format($p->getTimeTaken(), 2), - 'servers_summary' => $server_summary + 'servers_summary' => $server_summary, + 'currency' => Session::get('dashboard_currency') ); return view('home', compact('information')); diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index 6bc444c..cac7965 100644 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -33,7 +33,8 @@ class SettingsController extends Controller 'default_currency' => 'required', 'default_server_os' => 'required', 'due_soon_amount' => 'required|integer|between:0,12', - 'recently_added_amount' => 'required|integer|between:0,12' + 'recently_added_amount' => 'required|integer|between:0,12', + 'currency' => 'required|string|size:3' ]); DB::table('settings') @@ -51,7 +52,8 @@ class SettingsController extends Controller 'default_currency' => $request->default_currency, 'default_server_os' => $request->default_server_os, 'due_soon_amount' => $request->due_soon_amount, - 'recently_added_amount' => $request->recently_added_amount + 'recently_added_amount' => $request->recently_added_amount, + 'dashboard_currency' => $request->currency, ]); Settings::setSettingsToSession($settings); diff --git a/app/Models/Home.php b/app/Models/Home.php index 30586ce..baeddd5 100644 --- a/app/Models/Home.php +++ b/app/Models/Home.php @@ -119,24 +119,29 @@ class Home extends Model $total_cost_weekly = $total_cost_pm = $inactive_count = 0; foreach ($pricing as $price) { if ($price['active'] === 1) { + if (Session::get('dashboard_currency') !== 'USD') { + $the_price = Pricing::convertFromUSD($price['as_usd'], Session::get('dashboard_currency')); + } else { + $the_price = $price['as_usd']; + } if ($price['term'] === 1) {//1 month - $total_cost_weekly += ($price['as_usd'] / 4); - $total_cost_pm += $price['as_usd']; + $total_cost_weekly += ($the_price / 4); + $total_cost_pm += $the_price; } elseif ($price['term'] === 2) {//3 months - $total_cost_weekly += ($price['as_usd'] / 12); - $total_cost_pm += ($price['as_usd'] / 3); + $total_cost_weekly += ($the_price / 12); + $total_cost_pm += ($the_price / 3); } elseif ($price['term'] === 3) {// 6 month - $total_cost_weekly += ($price['as_usd'] / 24); - $total_cost_pm += ($price['as_usd'] / 6); + $total_cost_weekly += ($the_price / 24); + $total_cost_pm += ($the_price / 6); } elseif ($price['term'] === 4) {// 1 year - $total_cost_weekly += ($price['as_usd'] / 48); - $total_cost_pm += ($price['as_usd'] / 12); + $total_cost_weekly += ($the_price / 48); + $total_cost_pm += ($the_price / 12); } elseif ($price['term'] === 5) {//2 years - $total_cost_weekly += ($price['as_usd'] / 96); - $total_cost_pm += ($price['as_usd'] / 24); + $total_cost_weekly += ($the_price / 96); + $total_cost_pm += ($the_price / 24); } elseif ($price['term'] === 6) {//3 years - $total_cost_weekly += ($price['as_usd'] / 144); - $total_cost_pm += ($price['as_usd'] / 36); + $total_cost_weekly += ($the_price / 144); + $total_cost_pm += ($the_price / 36); } } else { $inactive_count++; diff --git a/app/Models/Pricing.php b/app/Models/Pricing.php index e021362..0a99d9c 100644 --- a/app/Models/Pricing.php +++ b/app/Models/Pricing.php @@ -14,6 +14,27 @@ class Pricing extends Model protected $fillable = ['service_id', 'service_type', 'currency', 'price', 'term', 'as_usd', 'usd_per_month', 'next_due_date']; + public static function convertFromUSD(string $amount, string $convert_to): float + {//Code rates update from an API?? + if ($convert_to === 'AUD') { + return (1.39 * $amount); + } elseif ($convert_to === "USD") { + return $amount; + } elseif ($convert_to === "GBP") { + return (0.79 * $amount); + } elseif ($convert_to === "EUR") { + return (0.93 * $amount); + } elseif ($convert_to === "NZD") { + return (1.53 * $amount); + } elseif ($convert_to === "JPY") { + return (127.12 * $amount); + } elseif ($convert_to === "CAD") { + return (1.27 * $amount); + } else { + return $amount; + } + } + public function convertToUSD(string $amount, string $convert_from): float { if ($convert_from === 'AUD') { diff --git a/app/Models/Settings.php b/app/Models/Settings.php index 1f584b4..a5625c7 100644 --- a/app/Models/Settings.php +++ b/app/Models/Settings.php @@ -40,6 +40,7 @@ class Settings extends Model Session::put('default_server_os', $settings[0]->default_server_os ?? 1); Session::put('due_soon_amount', $settings[0]->due_soon_amount ?? 6); Session::put('recently_added_amount', $settings[0]->recently_added_amount ?? 6); + Session::put('dashboard_currency', $settings[0]->dashboard_currency ?? 'USD'); Session::save(); } diff --git a/resources/views/home.blade.php b/resources/views/home.blade.php index 5f21cf3..a772331 100644 --- a/resources/views/home.blade.php +++ b/resources/views/home.blade.php @@ -71,7 +71,7 @@
-

{{$information['total_cost_weekly']}} USD

+

{{$information['total_cost_weekly']}} {{$information['currency']}}

Weekly cost

@@ -81,7 +81,7 @@
-

{{$information['total_cost_monthly']}} USD +

{{$information['total_cost_monthly']}} {{$information['currency']}}

Monthly cost

@@ -92,7 +92,7 @@
-

{{$information['total_cost_yearly']}} USD

+

{{$information['total_cost_yearly']}} {{$information['currency']}}

Yearly cost

@@ -102,7 +102,7 @@
-

{{$information['total_cost_2_yearly']}} USD +

{{$information['total_cost_2_yearly']}} {{$information['currency']}}

2 yearly cost

diff --git a/resources/views/settings/index.blade.php b/resources/views/settings/index.blade.php index 5ca3e47..716b6b1 100644 --- a/resources/views/settings/index.blade.php +++ b/resources/views/settings/index.blade.php @@ -214,6 +214,13 @@
+
+
+ + {{$setting[0]->dashboard_currency}} + +
+
Update settings