aljawaid / CostControl

Use the new Cost Control section to enable currencies and budgeting in Kanboard. Get live currency rates automatically for over 120 currencies allowing users to compare with manually saved rates. This plugin replaces and extends the features from the original Budget plugin enabling projects to have an associated cost element.
MIT License
4 stars 1 forks source link

Add Live Exchange Rate #1

Closed aljawaid closed 1 year ago

aljawaid commented 1 year ago

Found this https://www.exchangerate-api.com/docs/free which does not require an API

I think the last updated date might be a seperate helper

aljawaid commented 1 year ago

@creecros could you please help?

I have got as far as this:

class LiveRateHelper extends Base
{
    // https://www.exchangerate-api.com/docs/php-currency-api
    // https://www.exchangerate-api.com/docs/free

    public static function getLiveRate($application_currency, $rate_currency)
    {
        // Fetching JSON
        //$req_url = 'https://open.er-api.com/v6/latest/USD';
        $req_url = 'https://open.er-api.com/v6/latest/'.$application_currency;
        $response_json = file_get_contents($req_url);

        // Continuing if we got a result
        if(false !== $response_json) {
            // Try/catch for json_decode operation
            try {
                // Decoding
                $response = json_decode($response_json);

                // Check for success
                if('success' === $response->result) {

                // YOUR APPLICATION CODE HERE, e.g.
                    //$base_price = 1; // Your price in USD
                    // $EUR_price = round(($base_price * $response->conversion_rates->EUR), 2);
                    //$live_rate = round(($base_price * $response->conversion_rates->$rate_currency), 2);

                    $base_currency = $application_currency;
                    foreach ($response as $key => $value) {}
                }

                return $live_rate
            }

            catch(Exception $e) {
            // Handle JSON parse error...
                return 'Error retrieving live rate';
            }
        }
    }
}

I don't know how to do the foreach

This is how I am calling the helper...

<?= $this->helper->liveRateHelper->getLiveRate($application_currency, $rate_currency = $this->text->e($rate['currency'])) ?>
aljawaid commented 1 year ago

ok so I get no errors now but the result is always ZWL and I cant figure out how to do #2 in the same function. would try a new function but there is a rate limit of 24hrs which must be avoided.

Kind of regret deeply into this but I badly need a live currency in kanboard for my daily work

aljawaid commented 1 year ago

its always the last currency code and rate, no matter what i try

image

aljawaid commented 1 year ago

@creecros any feedback on where i am going wrong?

aljawaid commented 1 year ago

https://stackoverflow.com/questions/75924468/how-can-i-fix-this-helper-function-in-kanboard-to-get-specific-data-from-a-json