|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Helpers; |
| 4 | + |
| 5 | +use App\Models\Setting\Setting; |
| 6 | +use App\Models\Checkout\Currency; |
| 7 | + |
| 8 | +class CurrencyHelper |
| 9 | +{ |
| 10 | + public static function getSetPriceFormat($priceWithTwoDecimals) |
| 11 | + { |
| 12 | + $settings = Setting::first(); |
| 13 | + $isCommaTheDecimalSeparator = $settings->comma_is_decimal_separator; |
| 14 | + $isIntegerPriceUsed = $settings->use_integer_prices; |
| 15 | + |
| 16 | + $priceStringToReturn = $priceWithTwoDecimals; |
| 17 | + if( ($isIntegerPriceUsed) && ($priceWithTwoDecimals < 1000) ) |
| 18 | + { |
| 19 | + return intval($priceStringToReturn); |
| 20 | + } |
| 21 | + |
| 22 | + if( (!$isIntegerPriceUsed) && ($priceWithTwoDecimals < 1000) ) |
| 23 | + { |
| 24 | + $priceString = strval($priceWithTwoDecimals); |
| 25 | + if($isCommaTheDecimalSeparator) |
| 26 | + { |
| 27 | + return str_replace('.', ',', $priceString); |
| 28 | + } |
| 29 | + else |
| 30 | + { |
| 31 | + return $priceStringToReturn; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + if( ($isIntegerPriceUsed) && ($priceWithTwoDecimals >= 1000) ) |
| 36 | + { |
| 37 | + $priceString = strval(intval($priceStringToReturn)); |
| 38 | + if($isCommaTheDecimalSeparator) |
| 39 | + { |
| 40 | + return number_format($priceString, null, null, '.'); |
| 41 | + } |
| 42 | + else |
| 43 | + { |
| 44 | + return number_format($priceString, null, null, ','); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if( (!$isIntegerPriceUsed) && ($priceWithTwoDecimals >= 1000) ) |
| 49 | + { |
| 50 | + $priceStringWithTwoDecimals = strval($priceWithTwoDecimals); |
| 51 | + $positionOfPeriod = strpos($priceStringWithTwoDecimals, '.'); |
| 52 | + $priceStringInt = substr($priceStringWithTwoDecimals, 0, $positionOfPeriod); |
| 53 | + $decimalsString = substr($priceStringWithTwoDecimals, $positionOfPeriod + 1); |
| 54 | + |
| 55 | + if($isCommaTheDecimalSeparator) |
| 56 | + { |
| 57 | + $separatedPriceInt = number_format($priceStringInt, null, null, '.'); |
| 58 | + return $separatedPriceInt . ',' . $decimalsString; |
| 59 | + } |
| 60 | + else |
| 61 | + { |
| 62 | + $separatedPriceInt = number_format($priceStringInt, null, null, ','); |
| 63 | + return $separatedPriceInt . '.' . $decimalsString; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return $priceStringToReturn; |
| 68 | + } |
| 69 | + |
| 70 | + public static function getCurrencyString() |
| 71 | + { |
| 72 | + $settings = Setting::first(); |
| 73 | + $currencyText = $settings->currency; |
| 74 | + $adjustedCurrencyText = $currencyText . ' '; |
| 75 | + if( $settings->use_currency_symbol == 1 ) |
| 76 | + { |
| 77 | + if( $currencyText == 'USD' || $currencyText == 'EUR' || $currencyText == 'GBP' ) |
| 78 | + { |
| 79 | + $currencyData = Currency::where('name', $currencyText)->first(); |
| 80 | + if( !is_null($currencyData) ) |
| 81 | + { |
| 82 | + $adjustedCurrencyText = $currencyData->symbol; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return $adjustedCurrencyText; |
| 88 | + } |
| 89 | +} |
0 commit comments