The $this->currency->format needs to be added to avoid large differences with decimals.ADD Creative wrote: ↑Thu Dec 09, 2021 12:58 amSticking with the original example of 20.80 including 2.00 fixed tax and 20% tax. The calculation should be (20.80 - 2.00) / 1.2 = 15.6667.straightlight wrote: ↑Wed Dec 08, 2021 11:36 pmIf it doesn't work at all, it means the value has not been defined originally since the $currency_info['value'] is a dynamic value pulled from the database. However, it may be possible that the: if ($currency_info) { may return false in this case because of the decimal of zeros instead of the starting point of 1 and above.
Lets say you were using a currency with a value of 0.8. What your code gives is (20.80 - 2.00) / 0.96 = 19.5833. which is clearly wrong. Even if you are saying that the 20.80 would actually be 20.80 * 0.8 = 16.64 because it's in the selected currency. You would get (16.64 - 2.00) / 0.96 = 15.25. Which is again wrong.
You can't remove the fixed tax in one currency the the percentage tax in another and expect to get the correct result.
Dedication and passion goes to those who are able to push and merge a project.
Regards,
Straightlight
Programmer / Opencart Tester
Dedication and passion goes to those who are able to push and merge a project.
Regards,
Straightlight
Programmer / Opencart Tester
That is even worse. Now you are doing (20.80 - 2.00) / ((20.80 * 0.80) + 3.328) = 0.9415. An even worse result.
As I have already said you can't remove the fixed tax in one currency the the percentage tax in another and expect to get the correct result. If you need to convert currencies, do before and/or after the calculation.
It ain't gonna be with 1 for all solutions, that's for sure.ADD Creative wrote: ↑Thu Dec 09, 2021 11:42 pmThat is even worse. Now you are doing (20.80 - 2.00) / ((20.80 * 0.80) + 3.328) = 0.9415. An even worse result.
As I have already said you can't remove the fixed tax in one currency the the percentage tax in another and expect to get the correct result. If you need to convert currencies, do before and/or after the calculation.
Dedication and passion goes to those who are able to push and merge a project.
Regards,
Straightlight
Programmer / Opencart Tester
It is always going to be 1 for all solutions as it just using it as a base to get the tax rates. It has noting to do with the currency as all.straightlight wrote: ↑Fri Dec 10, 2021 4:27 amIt ain't gonna be with 1 for all solutions, that's for sure.
Dedication and passion goes to those who are able to push and merge a project.
Regards,
Straightlight
Programmer / Opencart Tester
It has nothing to do with currency. You would only use currency format after the calculation if you wanted to convert/display the result.straightlight wrote: ↑Sat Dec 11, 2021 9:33 pmIf that'd be the case, the $this->currency->format wouldn't contain an optional parameter for currency value since the user-defined one would also be 1 from the library out of the box.
Parameters are still optional with formatting.ADD Creative wrote: ↑Sat Dec 11, 2021 10:37 pmIt has nothing to do with currency. You would only use currency format after the calculation if you wanted to convert/display the result.straightlight wrote: ↑Sat Dec 11, 2021 9:33 pmIf that'd be the case, the $this->currency->format wouldn't contain an optional parameter for currency value since the user-defined one would also be 1 from the library out of the box.
Dedication and passion goes to those who are able to push and merge a project.
Regards,
Straightlight
Programmer / Opencart Tester
Which again has absolutely nothing to do with the tax calculation.
The currency format can be used for every amount and rate. Extension payment modules calculates all kinds of prices and rates.ADD Creative wrote: ↑Sun Dec 12, 2021 4:07 amWhich again has absolutely nothing to do with the tax calculation.
Dedication and passion goes to those who are able to push and merge a project.
Regards,
Straightlight
Programmer / Opencart Tester
Again, has nothing to do with tax calculation. You calculate tax (or in this case ex. tax), then you use currency format as necessary. It really is very simple.straightlight wrote: ↑Sun Dec 12, 2021 5:01 amThe currency format can be used for every amount and rate. Extension payment modules calculates all kinds of prices and rates.
With $number being a price including tax in the default currency.
Code: Select all
$tax_rates = $this->tax->getRates(1, $product_info['tax_class_id'] );
$result = $number;
$rate = 1;
foreach ($tax_rates as $tax_rate) {
if ($tax_rate['type'] == 'F') {
$result -= $tax_rate['amount'];
} else if ($tax_rate['type'] == 'P') {
$rate += $tax_rate['amount'];
}
}
$result /= $rate;
$output_string = $this->currency->format($result, $this->session->data['currency']);
Dedication and passion goes to those who are able to push and merge a project.
Regards,
Straightlight
Programmer / Opencart Tester
Which is entirely correct.
Lets say p is the price excluding tax, t is the percentage tax (e.g. 20%) and i is the price including tax. The calculation for the price including tax would be.
i = p + (p * (t / 100))
You can see this in the code here.
https://github.com/opencart/opencart/bl ... x.php#L113
Now to find the price without tax we need to solve for p.
So factor out p.
i = p(1 + (t / 100))
Rearrange.
p = i / (1 + (t / 100))
See the 1 in there. That's why it's divide by 1 + rate and why it's entirely correct to use a value of 1.
Also passing 1 into getRates in just a small optimisation. As we have to call getRates anyway, we can use the fact that getRates divides the value by 100 then multiplies by the rate and returns that as the amount. We could actually pass any value into getRates and just use the $tax_rate['rate'] / 100 in place of $tax_rate['amount'].
While we do have to pass the taxRates list, the value of 1 is still user-defined.public function format($number, $currency, $value = '', $format = true) {
Dedication and passion goes to those who are able to push and merge a project.
Regards,
Straightlight
Programmer / Opencart Tester
Nowhere is a value of 1 passed to format. I would suggest you read the posts and example code thoroughly. Then do some testing of your own and post the results.straightlight wrote: ↑Mon Dec 13, 2021 7:10 amWhile we do have to pass the taxRates list, the value of 1 is still user-defined.public function format($number, $currency, $value = '', $format = true) {
Users browsing this forum: Bing [Bot] and 511 guests