-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change currency rate on the fly #9
Comments
Of course there was a smarter solution for this! :) $newRate = 4.0
$money = money(100)->convertTo(
new \ArchTech\Money\Currency('EUR', 'Euro', $newRate, '', 'EUR')
);
// $money->value(); // should now be 400 |
In this case the order should store the exchange rate that was used at the time of purchase. I see your point about changing the currency rate to format the value correctly. Hmm. Right now the package expects Updating the rate for the entire currency would be wrong here, because you could have e.g. a navbar with the cart total, and you only want to change how some value is rendered on a specific part of the page (like a "show order" table). The code you just posted seems to be a good solution. I checked the But perhaps we could add something like: money(100)->convertTo(EUR::class, rate: $newRate); As in, the ability to specify overrides after the currency code. I'll re-open this issue to consider that in the future. I'd also advise storing the value that the user actually paid, and converting that to the base currency if needed. There probably isn't a difference when you handle the math well, but it seems like a better practice to store what the user actually paid rather than having to compute it from some other value. |
That solution would be really nice, no need for duplication then. :)
I save the current rate of what the customer paid. In my case i store everything in SEK with a currency rate. Its easier to maintain and report statistics on. To make this possible i use cast for returning a DataType class with this money package and some custom functions. // your model
protected $casts = ['price' => MoneyCast::class]; // Returns a DataType with logical stuff in it // Usage
$item->price->value; // Original value, i do all the calculations on
$item->price->currencyValue; // Calculated currency value (used for payments etc)
$item->price->formatted; // Formatted in selected currency or related currency and rate
// and more |
Before I start something unnecessary, I thought I would check if there is a good way to update an exchange rate on the fly?
Use case
You have an order with a specific exchange rate. A couple of days later, the course has changed and you want to present what they paid.
Note, In my system, all prices are saved in a base currency.
An idea would be something similar
The text was updated successfully, but these errors were encountered: