nova-money-field icon indicating copy to clipboard operation
nova-money-field copied to clipboard

dynamic currency symbol

Open R00118189 opened this issue 6 years ago • 5 comments

Is it possible to get currency symbol from other field value? in the DB I am storing monetary values in two fields: amount (in minor units) and currency (as a lowercase currency symbol 'eur','usd','gbp') it would be nice if formatting would depend on currency field value.

R00118189 avatar Dec 24 '18 12:12 R00118189

You can use $this->currency in Nova resource then.

vyuldashev avatar Dec 24 '18 18:12 vyuldashev

Works for index & detail view however not for form. I'm using Money::make('Amount', strtoupper($this->currency))->storedInMinorUnits(), and getting this error:

"message": "Cannot find currency ",
    "exception": "Money\\Exception\\UnknownCurrencyException",
    "file": "/Users/XXX/XXX/XXX/vendor/moneyphp/money/src/Currencies/AggregateCurrencies.php",
    "line": 60,

R00118189 avatar Dec 24 '18 22:12 R00118189

$this->currency ?? 'USD'?

vyuldashev avatar Dec 24 '18 22:12 vyuldashev

no luck, same error, however $this->currency ? $this->currency : 'USD' worked when i \Log::info $this->currency in fields function, it logs twice: first exact value from the model and second time empty string, have no clue why.

[2018-12-24 22:41:19] local.INFO: EUR  
[2018-12-24 22:41:20] local.INFO:   
[2018-12-24 22:41:25] local.INFO:   
[2018-12-24 22:41:25] local.INFO:   
[2018-12-24 22:41:25] local.INFO: EUR  
[2018-12-24 22:41:25] local.INFO: USD  
[2018-12-24 22:41:25] local.INFO:   

R00118189 avatar Dec 24 '18 22:12 R00118189

I had a similar problem as @R00118189 , using $this->currency would work perfectly when in list view, but fail when in detail view BUT ONLY if I also outputted the related table. Using $this->currency ? $this->currency : 'USD' worked.

I should point out the error I got was:

Argument 1 passed to Vyuldashev\NovaMoneyField\Money::subUnits() must be of the type string, null given, called in /home/eugene/code/bas/vendor/vyuldashev/nova-money-field/src/Money.php on line 29 {"userId":1,"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0):

eugenefvdm avatar Mar 21 '20 04:03 eugenefvdm