Post by Dutch Pride Code » Sun Dec 05, 2021 6:44 pm

Hi everybody,

How do I subtract the tax from a price with tax?

Customers in my project should be able to enter a price without worrying about taxes, so I need to subtract the tax from it before storing it into a database.

In the example below I use OpenCarts default "Taxable goods" setting: 20% VAT + $2,00 Eco-Tax .

Code: Select all

$number = 20.80

// Get $number tax:
$tax = $this->tax->getTax( $number , $product_info['tax_class_id'] , $this->config->get('config_tax') );

// Subtract tax from total price:
$result = $this->currency->format( ( $number - $tax ) , $this->session->data['currency'] );
Obviously the result here is incorrect ($14,64) because it gets the tax of $number, which is already a price with tax.

The calculation should be:
(20.80 - 2.00) / 120 * 100 = 15.6667

OpenCart 3.0.3.8 (Default)
Last edited by Dutch Pride Code on Wed Dec 08, 2021 12:22 am, edited 2 times in total.

User avatar
Active Member

Posts

Joined
Sun Jan 26, 2020 9:46 pm

Post by JNeuhoff » Sun Dec 05, 2021 11:54 pm

Your enquiry doesn't make sense. Customers don't enter prices, they only choose products on the frontend pages for purchase.
The OpenCart database already stores products' prices with any taxes.

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by Dutch Pride Code » Mon Dec 06, 2021 12:15 am

JNeuhoff wrote:
Sun Dec 05, 2021 11:54 pm
Your enquiry doesn't make sense. Customers don't enter prices
Sorry. To clarify this a bit more...

For my project I need customers to be able to enter a price somewhere in the front, and usually customers should not be bothered with taxes and calculations. It is assumed all prices are with tax included (on the store front). So when asked to enter a price, they will enter a price. But to put this price into my database I need the price without tax, so I can show an ex. tax price in the back end where I view it (admin).

User avatar
Active Member

Posts

Joined
Sun Jan 26, 2020 9:46 pm

Post by by mona » Mon Dec 06, 2021 12:27 am

This would be out of the scope of free support and you would be better off posting in the commercial section viewforum.php?f=88.
You also might be better posting those types of questions on stackoverflow https://stackoverflow.com

DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am

Post by Dutch Pride Code » Mon Dec 06, 2021 12:33 am

by mona wrote:
Mon Dec 06, 2021 12:27 am
This would be out of the scope of free support and you would be better off posting in the commercial section viewforum.php?f=88.
You also might be better posting those types of questions on stackoverflow https://stackoverflow.com
So there is no method in OpenCart that subtracts a tax rate from a total price ???
Seems a bit odd to me, but if there is no way I need to reinvent the wheel.

User avatar
Active Member

Posts

Joined
Sun Jan 26, 2020 9:46 pm

Post by by mona » Mon Dec 06, 2021 12:41 am

Not within opencart core no. You have prices displayed with or without tax and the tax can be added during the cart process.
As I said, you would be better off posting it in stackoverflow.

This question is outside of the scope of free support, it is a specific requirement project and has no value to the community as a whole.
Please understand that if this forum becomes another stackoverflow for developers - it will alienate the real users for whom this forum was designed.

DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am

Post by Dutch Pride Code » Mon Dec 06, 2021 1:08 am

I aways thought this place was for all users, there are so many coding solutions to be found here. And I don't see how this is commercial support though, it wasn't a request for custom work and I don't share a paid extension. Consider this question not asked, I understand.

User avatar
Active Member

Posts

Joined
Sun Jan 26, 2020 9:46 pm

Post by by mona » Mon Dec 06, 2021 1:56 am

EMGX wrote:
Mon Dec 06, 2021 1:08 am
And I don't see how this is commercial support though, it wasn't a request for custom work
Fair enough, so to kind of categorise that a little better ..

Examples of genuine free support would be
My sessions table is full and slowing my site down, how do I fix that ?
How do I set up SEO with Opencart?
How to I add options to my products?


Examples of simple community benefit support that one may or may not choose to assist would be
How do I change the colour of the text in the footer?
How to I remove the telephone from the header?
or even .. how do I get a confirmation before logging out in case I accidentally press the button (which is just about in the scope of free support but not really)


Examples of custom work outside the scope of free support would be
How do I auto add a product when another product is purchased?
How do I add images to the search function in the header?
How do I add the shipping and taxes calculator to the mini cart in the header?
How do I remove the taxes when a customer enters his price on some form of multi-seller set up?


Examples of where stackoverflow should be used rather than here would be
How do I add write an sql statement to add a field into xyz?
How do I write a piece of php code to subtract taxes on full priced goods?
How an I write a twig statement to do xyz?

DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am

Post by Dutch Pride Code » Mon Dec 06, 2021 5:06 am

Ahh, my bad. But I can't remove this question. Just like all the other people with posts on this forum that have zero replies but still exist somewhere on the web, to the annoyance of people searching for a solution to their problem. Somebody one day will find this post searching for the solution to their tax problem and getting disappointed.

User avatar
Active Member

Posts

Joined
Sun Jan 26, 2020 9:46 pm

Post by ADD Creative » Mon Dec 06, 2021 5:47 am

EMGX wrote:
Mon Dec 06, 2021 1:08 am
I aways thought this place was for all users, there are so many coding solutions to be found here. And I don't see how this is commercial support though, it wasn't a request for custom work and I don't share a paid extension. Consider this question not asked, I understand.
There is no quick method I can think of. But you could try something like.

Code: Select all

$tax_rates = $this->tax->getRates(1, $product_info['tax_class_id'] );

$result = $number;

foreach ($tax_rates as $tax_rate) {
	if ($tax_rate['type'] == 'F') {
		$result -= $tax_rate['amount'];
	}
}

$rate = 0;

foreach ($tax_rates as $tax_rate) {
	if ($tax_rate['type'] == 'P') {
		$rate += $tax_rate['amount'];
	}
}

$result /= 1 + $rate;
 

www.add-creative.co.uk


Expert Member

Posts

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

Post by by mona » Mon Dec 06, 2021 6:22 am

My heart bleeds for those who can be disappointed at such trivialities :laugh:

So you do understand the point exactly, developers are better off to ask coding questions on a forum that is built for developers - stackoverflow.com. In this way when people search they will be directed to the correct place, designed for these types of questions and will not be disappointed .. we are on the same page.

DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am

Post by paulfeakins » Mon Dec 06, 2021 8:25 pm

EMGX wrote:
Sun Dec 05, 2021 6:44 pm
How do I subtract the tax from a price with tax?
Did you search for an extension?

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Guru Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by Dutch Pride Code » Wed Dec 08, 2021 12:18 am

paulfeakins wrote:
Mon Dec 06, 2021 8:25 pm
Did you search for an extension?
As I'd like to build it myself I think this would be unnecessary. Any extension recommendations are still more than welcome (for the people who find this threat in the future).

ADD Creative wrote:
Mon Dec 06, 2021 5:47 am
There is no quick method I can think of. But you could try something like.

Code: Select all

$tax_rates = $this->tax->getRates(1, $product_info['tax_class_id'] );

$result = $number;

foreach ($tax_rates as $tax_rate) {
	if ($tax_rate['type'] == 'F') {
		$result -= $tax_rate['amount'];
	}
}

$rate = 0;

foreach ($tax_rates as $tax_rate) {
	if ($tax_rate['type'] == 'P') {
		$rate += $tax_rate['amount'];
	}
}

$result /= 1 + $rate;
 
I actually figured out a very similar solution to get the tax rates and subtract them. It works but this is even better ;D Many thanks!

User avatar
Active Member

Posts

Joined
Sun Jan 26, 2020 9:46 pm

Post by straightlight » Wed Dec 08, 2021 2:01 am

Code: Select all

// Currency
$code = '';
		
$this->load->model('localisation/currency');
		
$currencies = $this->model_localisation_currency->getCurrencies();
		
if (isset($this->session->data['currency'])) {
	$code = $this->session->data['currency'];
}
		
if (isset($this->request->cookie['currency']) && !array_key_exists($code, $currencies)) {
	$code = $this->request->cookie['currency'];
}
		
if (!array_key_exists($code, $currencies)) {
	$code = $this->config->get('config_currency');
}

$currency_info = $this->model_localisation_currency->getCurrencyByCode($code);

if ($currency_info) {
	$tax_rates = $this->tax->getRates($this->currency->format($number, $code, $currency_info['value'], false), $product_info['tax_class_id']);

	$result = $number;

	foreach ($tax_rates as $tax_rate) {
		if ($tax_rate['type'] == 'F') {
			$result -= $tax_rate['amount'];
		}
	}

	$rate = 0;

	foreach ($tax_rates as $tax_rate) {
		if ($tax_rate['type'] == 'P') {
			$rate += $tax_rate['amount'];
		}
	}

	$result /= $this->currency->format($number, $code, $currency_info['value'], false) + $rate;
}
Last edited by straightlight on Thu Dec 09, 2021 3:31 am, edited 2 times in total.

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 » Wed Dec 08, 2021 8:34 am

straightlight wrote:
Wed Dec 08, 2021 2:01 am

Code: Select all

// Currency
$code = '';
		
$this->load->model('localisation/currency');
		
$currencies = $this->model_localisation_currency->getCurrencies();
		
if (isset($this->session->data['currency'])) {
	$code = $this->session->data['currency'];
}
		
if (isset($this->request->cookie['currency']) && !array_key_exists($code, $currencies)) {
	$code = $this->request->cookie['currency'];
}
		
if (!array_key_exists($code, $currencies)) {
	$code = $this->config->get('config_currency');
}

$currency_info = $this->model_localisation_currency->getCurrencyByCode($code);

if ($currency_info) {
	$tax_rates = $this->tax->getRates($currency_info['value'], $product_info['tax_class_id']);

	$result = $number;

	foreach ($tax_rates as $tax_rate) {
		if ($tax_rate['type'] == 'F') {
			$result -= $tax_rate['amount'];
		}
	}

	$rate = 0;

	foreach ($tax_rates as $tax_rate) {
		if ($tax_rate['type'] == 'P') {
			$rate += $tax_rate['amount'];
		}
	}

	$result /= $currency_info['value'] + $rate;
}
You need to test that. It won't work at all if the currency value isn't 1.

www.add-creative.co.uk


Expert Member

Posts

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

Post by ADD Creative » Wed Dec 08, 2021 8:38 am

EMGX wrote:
Wed Dec 08, 2021 12:18 am
I actually figured out a very similar solution to get the tax rates and subtract them. It works but this is even better ;D Many thanks!
Slightly smaller version.

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;

www.add-creative.co.uk


Expert Member

Posts

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

Post by straightlight » Wed Dec 08, 2021 11:08 pm

ADD Creative wrote:
Wed Dec 08, 2021 8:34 am
straightlight wrote:
Wed Dec 08, 2021 2:01 am

Code: Select all

// Currency
$code = '';
		
$this->load->model('localisation/currency');
		
$currencies = $this->model_localisation_currency->getCurrencies();
		
if (isset($this->session->data['currency'])) {
	$code = $this->session->data['currency'];
}
		
if (isset($this->request->cookie['currency']) && !array_key_exists($code, $currencies)) {
	$code = $this->request->cookie['currency'];
}
		
if (!array_key_exists($code, $currencies)) {
	$code = $this->config->get('config_currency');
}

$currency_info = $this->model_localisation_currency->getCurrencyByCode($code);

if ($currency_info) {
	$tax_rates = $this->tax->getRates($currency_info['value'], $product_info['tax_class_id']);

	$result = $number;

	foreach ($tax_rates as $tax_rate) {
		if ($tax_rate['type'] == 'F') {
			$result -= $tax_rate['amount'];
		}
	}

	$rate = 0;

	foreach ($tax_rates as $tax_rate) {
		if ($tax_rate['type'] == 'P') {
			$rate += $tax_rate['amount'];
		}
	}

	$result /= $currency_info['value'] + $rate;
}
You need to test that. It won't work at all if the currency value isn't 1.
I'd have to disagree on that one. The value of 1 does not cover all solutions to calculate tax rates, at least not anymore, since the currency value can change up to 5 times a day instead of a single since the apparition of Rest API currency services. I'm still not sure why the 5th one applies by currency services but the first four changes do apply everyday and, sometimes, the 4th one in the array, does change a second time throughout the same day but may not be recurrent.

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 » Wed Dec 08, 2021 11:31 pm

straightlight wrote:
Wed Dec 08, 2021 11:08 pm
I'd have to disagree on that one. The value of 1 does not cover all solutions to calculate tax rates, at least not anymore, since the currency value can change up to 5 times a day instead of a single since the apparition of Rest API currency services. I'm still not sure why the 5th one applies by currency services but the first four changes do apply everyday and, sometimes, the 4th one in the array, does change a second time throughout the same day but may not be recurrent.
The reverse tax calculation has nothing at all to do with the currency. If you actually test the code you posted you will find it doesn't work at all if the currency value isn't 1.

www.add-creative.co.uk


Expert Member

Posts

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

Post by straightlight » Wed Dec 08, 2021 11:36 pm

ADD Creative wrote:
Wed Dec 08, 2021 11:31 pm
straightlight wrote:
Wed Dec 08, 2021 11:08 pm
I'd have to disagree on that one. The value of 1 does not cover all solutions to calculate tax rates, at least not anymore, since the currency value can change up to 5 times a day instead of a single since the apparition of Rest API currency services. I'm still not sure why the 5th one applies by currency services but the first four changes do apply everyday and, sometimes, the 4th one in the array, does change a second time throughout the same day but may not be recurrent.
The reverse tax calculation has nothing at all to do with the currency. If you actually test the code you posted you will find it doesn't work at all if the currency value isn't 1.
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.

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 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.

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 104 guests