frappe / erpnext

Free and Open Source Enterprise Resource Planning (ERP)
https://erpnext.com
GNU General Public License v3.0
22.05k stars 7.39k forks source link

Currency Exchange Settings possibility to parse array #32825

Open artemartamonov opened 2 years ago

artemartamonov commented 2 years ago

We tried to create a custom service provider for currency exchange. We have to use the National Bank of Poland to calculate the right exchange rate. Now we have the following: image When we try to save the Custom Provider we get the following issue:

Invalid result key. Response: {"table":"A","currency":"rupia indyjska","code":"INR","rates":[{"no":"212/A/NBP/2022","effectiveDate":"2022-11-02","mid":0.057363}]}

The service provider returns an array of exchange rates. We must always get the "mid" result key for the first element of the array because of the description of the API for a single record. I didn't find any description or documentation on how to do it. Is it possible to implement it?

Thank you!

AuntMaungGreenPhyto commented 1 year ago

You may modify "get_exchange_rate" the code as follow

settings = frappe.get_cached_doc("Currency Exchange Settings")

        req_params = {
            "transaction_date": transaction_date,
            "from_currency": from_currency,
            "to_currency": to_currency,
        }
        params = {}
        for row in settings.req_params:
            params[row.key] = format_ces_api(row.value, req_params)

        response = requests.get(format_ces_api(settings.api_endpoint, req_params), params=params)

        # expire in 6 hours
        response.raise_for_status()
        value = response.json()

        for res_key in settings.result_key:
            if  isinstance(value,dict):
                value = value[format_ces_api(str(res_key.key), req_params)]
            elif  isinstance(value,list):
                value = value[0][format_ces_api(str(res_key.key.lower()), req_params)]
artemartamonov commented 1 year ago

Hi @AuntMaungGreenPhyto, Thank you for your comment. But I get the same issue with it. I think that the problem is connected with format_ces_api. I am not pretty sure how this method actually works/