/home/thegtkjw/cresviafinancial.com/project__549d4a8/app/Helpers/helper.php
<?php

use App\Models\Currency;
use App\Models\Generalsetting;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

    if(!function_exists('globalCurrency')){
        function globalCurrency(){
            $currency = Session::get('currency') ?  DB::table('currencies')->where('id','=',Session::get('currency'))->first() : DB::table('currencies')->where('is_default','=',1)->first();
            return $currency;
        }
    }

    if(!function_exists('showPrice')){
        function showPrice($price,$currency){
            $gs = Generalsetting::first();

            $price = round(($price) * $currency->value,2);
            if($gs->currency_format == 0){
                return $currency->sign. $price;
            }
            else{
                return $price. $currency->sign;
            }
        }
    }

    if(!function_exists('showNameAmount')){
        function showNameAmount($amount){
            $gs = Generalsetting::first();
            $currency = globalCurrency();

            $price = round(($amount) * $currency->value,2);
            if($gs->currency_format == 0){
                return $currency->name.' '. $price;
            }
            else{
                return $price.' '. $currency->name;
            }
        }
    }

    if(!function_exists('showAmountSign')){
        function showAmountSign($amount){
            $gs = Generalsetting::first();
            $currency = globalCurrency();

            $price = round(($amount) * $currency->value,2);
            if($gs->currency_format == 0){
                return $currency->name.' '. $price;
            }
            else{
                return $price.' '. $currency->sign;
            }
        }
    }

    if(!function_exists('convertedAmount')){
        function convertedAmount($price){
            $currency = globalCurrency();

            $price = round(($price) * $currency->value,2);
            return $price;
        }
    }

    if(!function_exists('convertedApiAmount')){
        function convertedApiAmount($price,$currencyId){
            $currency = Currency::findOrFail($currencyId);

            $price = round(($price) * $currency->value,2);
            return $price;
        }
    }

    if(!function_exists('apiCurrencyAmount')){
        function apiCurrencyAmount($amount,$currencyId){
            $currency = Currency::findOrFail($currencyId);
            return $amount/$currency->value;
        }
    }

    if(!function_exists('apiConvertedAmount')){
        function apiConvertedAmount($amount){
            $user = auth()->user();

            $gs = Generalsetting::first();
            $currency = Currency::findOrFail($user->currency_id ?? 1);

            $price = round(($amount) * $currency->value,2);
            if($gs->currency_format == 0){
                return $currency->name.' '. $price;
            }
            else{
                return $price.' '. $currency->sign;
            }
        }
    }
    
    if(!function_exists('apiConvertedCurrencyAmount')){
        function apiConvertedCurrencyAmount($amount,$currencyId){
            $gs = Generalsetting::first();
            $currency = Currency::findOrFail($currencyId);

            $price = round(($amount) * $currency->value,2);
            if($gs->currency_format == 0){
                return $currency->name.' '. $price;
            }
            else{
                return $price.' '. $currency->sign;
            }
        }
    }

    if(!function_exists('requestFilter')){
        function requestFilter($name){
            return str_replace(' ', '_', $name);
        }
    }

    if(!function_exists('baseCurrencyAmount')){
        function baseCurrencyAmount($amount){
            $currency = globalCurrency();
            return $amount/$currency->value;
        }
    }

    if(!function_exists('convertedPrice')){
        function convertedPrice($price,$currency){
        return $price = $price * $currency->value;
        }
    }

    if(!function_exists('defaultCurr')){
        function defaultCurr(){
        return Currency::where('is_default','=',1)->first();
        }
    }
?>