From 49c0c94ec9c1da43a06e1ce1939c83e451b71027 Mon Sep 17 00:00:00 2001 From: Jaroslav Drzik Date: Tue, 2 Jan 2024 22:13:27 +0100 Subject: [PATCH] caclulate rate currency result --- .../Controllers/ProductsCompareController.php | 10 +++++++-- app/Models/CurrencyRates.php | 13 ++++++++++++ lang/id.json | 4 +++- resources/js/Layouts/GuestLayout.vue | 21 ++++++++----------- resources/js/Pages/IkeaRoot.vue | 12 +++++++++++ 5 files changed, 45 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/ProductsCompareController.php b/app/Http/Controllers/ProductsCompareController.php index f1a7e8e..cd7157b 100644 --- a/app/Http/Controllers/ProductsCompareController.php +++ b/app/Http/Controllers/ProductsCompareController.php @@ -17,6 +17,7 @@ class ProductsCompareController extends Controller { $codes = $request->input("codes"); $countries = $request->input("countries"); + $currency = $request->input("currency"); Log::info("{codes} {countries}", ["codes" => $codes, "countries" => $countries]); @@ -45,9 +46,14 @@ class ProductsCompareController extends Controller $products = $products->get(); $currencyRates = CurrencyRates::rates2EUR("Y"); + if ($currency == "EUR") { + $coef = 1; + } else { + $coef = floatval(CurrencyRates::where('currency',$currency)->first()->rate); + } - $products = $products->map(function ($product) use ($currencyRates) { - $product["salesPrice"] = round(floatval($product["salesPrice"]) / $currencyRates[$product["country"]], 2); + $products = $products->map(function ($product) use ($currencyRates, $coef) { + $product["salesPrice"] = round(floatval(($product["salesPrice"]) / $currencyRates[$product["country"]]) * $coef, 2); return $product; }); Log::info("{products}", ["products" => $products]); diff --git a/app/Models/CurrencyRates.php b/app/Models/CurrencyRates.php index 5b78efe..d89d633 100644 --- a/app/Models/CurrencyRates.php +++ b/app/Models/CurrencyRates.php @@ -47,4 +47,17 @@ class CurrencyRates extends Model return [$item["country_code"] => $rates[$item["currency_code"]]]; }); } + + protected function currencyCode() { + return CountryCode::active("Y")->mapWithKeys(function ($item) { + return [$item['currency_code'] => $item['country_code']]; + }); + } + + protected function countryCurrency() { + return CountryCode::active("Y")->mapWithKeys(function ($item) { + return [$item["country_code"] => $item["currency_code"]]; + }); + } + } diff --git a/lang/id.json b/lang/id.json index 7f39e09..757bda4 100644 --- a/lang/id.json +++ b/lang/id.json @@ -105,5 +105,7 @@ "about": "About", "exachange": "Exachange", "exchange": "Exchange", - "referenceerror: rates is not defined": "ReferenceError: rates is not defined" + "referenceerror: rates is not defined": "ReferenceError: rates is not defined", + "referenceerror: currency is not defined": "ReferenceError: currency is not defined", + "typeerror: can't access property \"unshift\", currency.velue is undefined": "TypeError: can't access property \"unshift\", currency.velue is undefined" } \ No newline at end of file diff --git a/resources/js/Layouts/GuestLayout.vue b/resources/js/Layouts/GuestLayout.vue index 27a1c36..adfbe17 100644 --- a/resources/js/Layouts/GuestLayout.vue +++ b/resources/js/Layouts/GuestLayout.vue @@ -4,10 +4,11 @@ import { getCurrentInstance, ref, onMounted, onUnmounted, nextTick, computed } f import { FwbDropdown, FwbListGroup, FwbListGroupItem } from 'flowbite-vue'; import LogoIkea from '@/assets/Ikea_logo.svg'; import { - FwbNavbar, - FwbNavbarCollapse, - FwbNavbarLink, - FwbNavbarLogo, + FwbSelect, + FwbNavbar, + FwbNavbarCollapse, + FwbNavbarLink, + FwbNavbarLogo, } from 'flowbite-vue'; const menu = ref([]) @@ -41,12 +42,12 @@ onMounted(fetch_menu); - - \ No newline at end of file + \ No newline at end of file diff --git a/resources/js/Pages/IkeaRoot.vue b/resources/js/Pages/IkeaRoot.vue index 3d61798..471e9bb 100644 --- a/resources/js/Pages/IkeaRoot.vue +++ b/resources/js/Pages/IkeaRoot.vue @@ -40,12 +40,14 @@ const type = 'GeoChart'; const form = useForm({ countries: '', codes: '', + currency: 'EUR', }); const ccountry = ref([]); const ccountry_filter = ref([['Country'],]); const ccountry_list = ref(['TEST']); const selected = ref(null); const article = ref(''); +const currency = ref([]); const hrates = ref([ { text: "Currency", value: "currency", sortable: true }, { text: "Country", value: "country_name", sortable: true }, @@ -112,6 +114,8 @@ const fetch_rates = async () => { try { const response = await axios.get(route('rates.index')) rates.value = response.data; + currency.value = rates.value.map((currency) => currency.currency); + currency.value.unshift('EUR'); console.log("RATE=", rates.value); } catch (e) { const response = await Swal.fire({ @@ -198,6 +202,13 @@ const submit = () => { +
+ Prepocet do meny +
+
+ +
+