Post by straightlight » Thu Dec 09, 2021 3:26 am

ADD Creative wrote:
Thu Dec 09, 2021 12:58 am
straightlight wrote:
Wed Dec 08, 2021 11:36 pm
If 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.
Sticking 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.

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.
The $this->currency->format needs to be added to avoid large differences with decimals.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by straightlight » Thu Dec 09, 2021 3:32 am

Added the code now.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by ADD Creative » Thu Dec 09, 2021 11:42 pm

straightlight wrote:
Thu Dec 09, 2021 3:32 am
Added the code now.
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.

www.add-creative.co.uk


Expert Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by straightlight » Fri Dec 10, 2021 4:27 am

ADD Creative wrote:
Thu Dec 09, 2021 11:42 pm
straightlight wrote:
Thu Dec 09, 2021 3:32 am
Added the code now.
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.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by ADD Creative » Fri Dec 10, 2021 7:04 am

straightlight wrote:
Fri Dec 10, 2021 4:27 am
It ain't gonna be with 1 for all solutions, that's for sure.
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.

www.add-creative.co.uk


Expert Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by straightlight » Sat Dec 11, 2021 9:33 pm

If 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


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by ADD Creative » Sat Dec 11, 2021 10:37 pm

straightlight wrote:
Sat Dec 11, 2021 9:33 pm
If 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.
It has nothing to do with currency. You would only use currency format after the calculation if you wanted to convert/display the result.

www.add-creative.co.uk


Expert Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by straightlight » Sun Dec 12, 2021 12:39 am

ADD Creative wrote:
Sat Dec 11, 2021 10:37 pm
straightlight wrote:
Sat Dec 11, 2021 9:33 pm
If 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.
It has nothing to do with currency. You would only use currency format after the calculation if you wanted to convert/display the result.
Parameters are still optional with formatting.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by ADD Creative » Sun Dec 12, 2021 4:07 am

straightlight wrote:
Sun Dec 12, 2021 12:39 am
Parameters are still optional with formatting.
Which again has absolutely nothing to do with the tax calculation.

www.add-creative.co.uk


Expert Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by straightlight » Sun Dec 12, 2021 5:01 am

ADD Creative wrote:
Sun Dec 12, 2021 4:07 am
straightlight wrote:
Sun Dec 12, 2021 12:39 am
Parameters are still optional with formatting.
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.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by ADD Creative » Sun Dec 12, 2021 8:23 am

straightlight wrote:
Sun Dec 12, 2021 5:01 am
The currency format can be used for every amount and rate. Extension payment modules calculates all kinds of prices and rates.
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.

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']);
See, tax calculation then conversion/format.

www.add-creative.co.uk


Expert Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by straightlight » Mon Dec 13, 2021 1:55 am

Still using the value of 1.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by ADD Creative » Mon Dec 13, 2021 4:04 am

straightlight wrote:
Mon Dec 13, 2021 1:55 am
Still using the value of 1.
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'].

www.add-creative.co.uk


Expert Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom

Post by straightlight » Mon Dec 13, 2021 7:10 am

public function format($number, $currency, $value = '', $format = true) {
While we do have to pass the taxRates list, the value of 1 is still user-defined.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by ADD Creative » Mon Dec 13, 2021 6:31 pm

straightlight wrote:
Mon Dec 13, 2021 7:10 am
public function format($number, $currency, $value = '', $format = true) {
While we do have to pass the taxRates list, the value of 1 is still user-defined.
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.

www.add-creative.co.uk


Expert Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom
Who is online

Users browsing this forum: No registered users and 19 guests