Skip to content

Commit

Permalink
Calculate base taxes for b2b orders. Exclude b2b orders (with and wit…
Browse files Browse the repository at this point in the history
…hout tax exemption) from OSS reports.
  • Loading branch information
dennisnissle committed Feb 5, 2024
1 parent 31c3f58 commit 534fc47
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 25 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"require": {
"composer/installers": "^1.9.0",
"automattic/jetpack-autoloader": "^2.6.0",
"vendidero/woocommerce-eu-tax-helper": "2.0.1"
"vendidero/woocommerce-eu-tax-helper": "2.0.2"
},
"require-dev": {
"phpunit/phpunit": "6.5.14",
Expand Down
30 changes: 15 additions & 15 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion one-stop-shop-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Description: Comply with the One Stop Shop procedure while using WooCommerce.
* Author: vendidero
* Author URI: https://vendidero.de
* Version: 1.5.1
* Version: 1.6.0
* Requires PHP: 5.6
* License: GPLv3
* Tested up to: 6.4
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "one-stop-shop-woocommerce",
"title": "One Stop Shop for WooCommerce",
"version": "1.5.1",
"version": "1.6.0",
"homepage": "https://vendidero.de",
"repository": {
"type": "git",
Expand Down
6 changes: 5 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Requires at least: 5.4
Tested up to: 6.4
WC requires at least: 3.9
WC tested up to: 8.5
Stable tag: 1.5.1
Stable tag: 1.6.0
Requires PHP: 5.6
License: GPLv3
License URI: http://www.gnu.org/licenses/gpl-3.0.html
Expand Down Expand Up @@ -61,6 +61,10 @@ Bug reports may be filed via our [GitHub repository](https://github.com/vendider
3. Create a new report

== Changelog ==
= 1.6.0 =
* Improvement: Exclude b2b orders (without and with UID, e.g. non-taxes) from OSS
* Improvement: Calculate base location taxes for b2b checkouts

= 1.5.1 =
* Improvement: Refresh luxembourg tax rate

Expand Down
14 changes: 12 additions & 2 deletions src/AsyncReportGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected function get_order_taxable_country( $order ) {
return Helper::get_base_country();
}

$taxable_country_type = ! empty( $order->get_shipping_country() ) ? 'shipping' : 'billing';
$taxable_country_type = $order->has_shipping_address() ? 'shipping' : 'billing';
$taxable_country = 'shipping' === $taxable_country_type ? $order->get_shipping_country() : $order->get_billing_country();

if ( $this->has_local_pickup( $order ) ) {
Expand Down Expand Up @@ -124,7 +124,7 @@ protected function has_local_pickup( $order ) {
* @return mixed
*/
protected function get_order_taxable_postcode( $order ) {
$taxable_type = ! empty( $order->get_shipping_postcode() ) ? 'shipping' : 'billing';
$taxable_type = $order->has_shipping_address() ? 'shipping' : 'billing';
$taxable_postcode = 'shipping' === $taxable_type ? $order->get_shipping_postcode() : $order->get_billing_postcode();

if ( $this->has_local_pickup( $order ) ) {
Expand All @@ -142,12 +142,22 @@ protected function get_order_taxable_postcode( $order ) {
protected function include_order( $order ) {
$taxable_country = $this->get_order_taxable_country( $order );
$taxable_postcode = $this->get_order_taxable_postcode( $order );
$has_company = Tax::order_has_taxable_company( $order );
$included = true;

if ( ! Helper::is_eu_vat_country( $taxable_country, $taxable_postcode ) || Helper::get_base_country() === $taxable_country ) {
$included = false;
}

/**
* Do not include b2b orders as only consumer-related orders are relevant for OSS.
*/
if ( $has_company ) {
Package::extended_log( sprintf( 'Order #%1$s is a b2b order.', $this->get_order_number( $order ) ) );

$included = false;
}

if ( floatval( $order->get_total_tax() ) === 0.0 ) {
$included = false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Package {
*
* @var string
*/
const VERSION = '1.5.1';
const VERSION = '1.6.0';

/**
* Init the package
Expand Down
64 changes: 61 additions & 3 deletions src/Tax.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public static function init() {
add_action( 'woocommerce_before_save_order_item', array( __CLASS__, 'maybe_filter_order_item_tax_class' ) );

add_filter( 'woocommerce_adjust_non_base_location_prices', array( __CLASS__, 'disable_location_price' ), 250 );
add_filter( 'woocommerce_customer_taxable_address', array( __CLASS__, 'vat_exempt_taxable_address' ), 10 );
add_filter( 'woocommerce_customer_taxable_address', array( __CLASS__, 'b2b_taxable_customer_location' ), 10 );
add_filter( 'woocommerce_order_get_tax_location', array( __CLASS__, 'b2b_taxable_order_location' ), 10, 2 );

add_action( 'woocommerce_before_calculate_totals', array( __CLASS__, 'invalidate_shipping_session' ), 100 );
}
Expand Down Expand Up @@ -100,8 +101,11 @@ public static function invalidate_shipping_session( $cart ) {
*
* @return array|mixed
*/
public static function vat_exempt_taxable_address( $location ) {
if ( Helper::current_request_has_vat_exempt() && apply_filters( 'oss_woocommerce_force_base_tax_rate_for_vat_exempt_net_calculation', true ) ) {
public static function b2b_taxable_customer_location( $location ) {
if (
( Helper::current_request_has_vat_exempt() && apply_filters( 'oss_woocommerce_force_base_tax_rate_for_vat_exempt_net_calculation', true ) ) ||
( Helper::current_request_is_b2b() && apply_filters( 'oss_woocommerce_force_base_tax_rate_for_b2b', true ) )
) {
$location = array(
WC()->countries->get_base_country(),
WC()->countries->get_base_state(),
Expand All @@ -113,6 +117,60 @@ public static function vat_exempt_taxable_address( $location ) {
return $location;
}

/**
* @param \WC_Order $order
*
* @return mixed
*/
public static function order_has_taxable_company( $order ) {
if ( ! is_callable( array( $order, 'get_shipping_company' ) ) ) {
return false;
}

$taxable_type = $order->has_shipping_address() ? 'shipping' : 'billing';
$taxable_company = 'shipping' === $taxable_type ? $order->get_shipping_company() : $order->get_billing_company();
$has_taxable_company = false;

if ( ! empty( $taxable_company ) ) {
$has_taxable_company = true;
}

if ( ! apply_filters( 'oss_woocommerce_force_base_tax_rate_for_b2b', true ) ) {
$has_taxable_company = false;
}

return apply_filters( 'oss_woocommerce_order_has_taxable_company', $has_taxable_company, $order );
}

/**
* @param array $args
* @param \WC_Order $order
*
* @return array
*/
public static function b2b_taxable_order_location( $args, $order ) {
$has_vat_exempt = apply_filters( 'woocommerce_order_is_vat_exempt', 'yes' === $order->get_meta( 'is_vat_exempt' ), $order );

if ( isset( $args['company'] ) ) {
$has_company = apply_filters( 'oss_woocommerce_order_has_taxable_company', ! empty( $args['company'] ), $order );
} else {
$has_company = self::order_has_taxable_company( $order );
}

if (
( $has_vat_exempt && apply_filters( 'oss_woocommerce_force_base_tax_rate_for_vat_exempt_net_calculation', true ) ) ||
( $has_company && apply_filters( 'oss_woocommerce_force_base_tax_rate_for_b2b', true ) )
) {
$args['country'] = Helper::get_base_country();

$args['state'] = WC()->countries->get_base_state();
$args['postcode'] = WC()->countries->get_base_postcode();
$args['city'] = WC()->countries->get_base_city();
}

return $args;
}

/**
* Maybe reset order item tax class while recalculating order totals as the product tax class
* filter will only work for the initial add to order event.
Expand Down

0 comments on commit 534fc47

Please sign in to comment.