Skip to content

Commit 9f31d98

Browse files
authored
Merge pull request #126 from BeAPI/issue/68254
Issue/68254
2 parents 50ad06c + 0d52126 commit 9f31d98

File tree

6 files changed

+154
-16
lines changed

6 files changed

+154
-16
lines changed

.lando.dist.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ services:
1717
user: wordpress-test
1818
password: wordpress-test
1919
database: wordpress-test
20+
mailcatcher:
21+
type: mailhog
22+
hogfrom:
23+
- appserver
24+
portforward: true
2025
tooling:
2126
setup-env:
2227
service: appserver
@@ -32,20 +37,27 @@ tooling:
3237
- wp config create --dbname=wordpress --dbuser=wordpress --dbpass=wordpress --dbhost=database --path=/app/wordpress
3338
- echo "➡️ Install WP"
3439
- wp core install --url="https://shoppingfeed-for-woocommerce.lndo.site" --title="Test" --admin_user=admin --admin_password=password --admin_email=wordpress@example.com --skip-email
40+
- wp rewrite structure "/%postname%/"
3541
- echo "➡️ Install Woocommerce"
3642
- wp plugin install --activate woocommerce
3743
- echo "➡️ Install Storefront"
3844
- wp theme install storefront --activate
3945
- echo "➡️ Install WordPress importer"
4046
- wp plugin install wordpress-importer --activate
41-
- echo "➡️ Install Woocommerce"
47+
- echo "➡️ Configure Woocommerce"
4248
- wp option set woocommerce_store_address "Example Address Line 1"
4349
- wp option set woocommerce_store_address_2 "Example Address Line 2"
4450
- wp option set woocommerce_store_city "Example City"
4551
- wp option set woocommerce_default_country "US:CA"
4652
- wp option set woocommerce_store_postcode "94110"
4753
- wp option set woocommerce_currency "EUR"
4854
- wp option set woocommerce_product_type "both"
55+
- wp option set woocommerce_calc_taxes "yes"
56+
- wp option set woocommerce_prices_include_tax "yes"
57+
- wp option set woocommerce_tax_display_shop "incl"
58+
- wp option set woocommerce_tax_display_cart "incl"
59+
- wp wc tax create --country=FR --rate=20 --user=admin
60+
- wp wc tax create --country=FR --rate=5.5 --class=reduced-rate --user=admin
4961
- wp option set woocommerce_allow_tracking "no"
5062
- wp option set woocommerce_enable_checkout_login_reminder "yes"
5163
- wp option set --format=json woocommerce_cod_settings '{"enabled":"yes"}'

src/Admin/Options.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,30 @@ function () use ( $wc_order_statuses ) {
15331533
'sf_orders_settings_import_options'
15341534
);
15351535

1536+
add_settings_field(
1537+
'import_vat_order',
1538+
__( 'VAT', 'shopping-feed' ),
1539+
function () {
1540+
?>
1541+
<label for="import_vat_order">
1542+
<input
1543+
type="checkbox"
1544+
id="import_vat_order"
1545+
name="<?php echo esc_attr( sprintf( '%s[import_vat_order]', self::SF_ORDERS_OPTIONS ) ); ?>"
1546+
value="1"
1547+
<?php checked( '1', isset( $this->sf_orders_options['import_vat_order'] ) ? $this->sf_orders_options['import_vat_order'] : '0' ); ?>
1548+
>
1549+
<?php esc_html_e( 'Import VAT (beta)', 'shopping-feed' ); ?>
1550+
</label>
1551+
<p class="description" id="tagline-description">
1552+
<?php esc_html_e( 'Include VAT when importing orders. The option "Enable taxes" in the Woocommerce general settings must be checked.', 'shopping-feed' ); ?>
1553+
</p>
1554+
<?php
1555+
},
1556+
self::SF_ORDERS_SETTINGS_PAGE,
1557+
'sf_orders_settings_import_options'
1558+
);
1559+
15361560
//mapping
15371561
$sf_actions = Operations::get_available_operations();
15381562

src/Orders/Order.php

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,16 @@
2525
*/
2626
class Order {
2727

28+
public const RATE_ID = 999999999999;
29+
2830
use Marketplace;
2931

3032
/** @var OrderResource $sf_order */
3133
private $sf_order;
3234

35+
/** @var bool $include_vat */
36+
private $include_vat;
37+
3338
/** @var array $shipping_address */
3439
private $shipping_address;
3540

@@ -61,11 +66,13 @@ class Order {
6166
* Order constructor.
6267
* Init all order requirements
6368
*
64-
* @param $sf_order OrderResource
69+
* @param OrderResource $sf_order
70+
* @param bool $include_vat
6571
*/
66-
public function __construct( $sf_order ) {
72+
public function __construct( $sf_order, $include_vat = false ) {
6773

68-
$this->sf_order = $sf_order;
74+
$this->sf_order = $sf_order;
75+
$this->include_vat = $include_vat;
6976

7077
$this->set_shipping_address();
7178
$this->set_billing_address();
@@ -112,7 +119,7 @@ public function add() {
112119

113120
//Payment
114121
try {
115-
$wc_order->set_prices_include_tax( $this->payment->get_total() );
122+
$wc_order->set_prices_include_tax( true );
116123
$wc_order->set_payment_method( $this->payment->get_method() );
117124
} catch ( \Exception $exception ) {
118125
$message = sprintf(
@@ -139,13 +146,23 @@ public function add() {
139146
$shipping_rate = $this->shipping->get_shipping_rate();
140147
$item = new \WC_Order_Item_Shipping();
141148
$item->set_shipping_rate( $shipping_rate );
149+
$item->save();
142150
$wc_order->add_item( $item );
143151
do_action( 'sf_after_order_add_shipping', $item, $wc_order );
144152
} else {
145153
try {
146154
$item = new \WC_Order_Item_Shipping();
147155
$item->set_method_title( $this->shipping->get_method() );
148156
$item->set_total( $this->shipping->get_total() );
157+
if ( $this->include_vat && $this->shipping->get_total_tax() > 0 ) {
158+
$item->set_taxes(
159+
[
160+
'total' => [
161+
self::RATE_ID => $this->shipping->get_total_tax(),
162+
],
163+
]
164+
);
165+
}
149166
$item->save();
150167
$wc_order->add_item( $item );
151168
do_action( 'sf_after_order_add_shipping', $item, $wc_order );
@@ -205,6 +222,33 @@ public function add() {
205222
}
206223
}
207224

225+
if ( $this->include_vat ) {
226+
$total_product_tax = 0;
227+
foreach ( $this->sf_order->getItems() as $item ) {
228+
$total_product_tax += $item->getTaxAmount();
229+
}
230+
231+
$total_shipping_tax = 0;
232+
if ( isset( $this->sf_order->toArray()['additionalFields']['shipping_tax'] ) ) {
233+
$total_shipping_tax = (float) $this->sf_order->toArray()['additionalFields']['shipping_tax'];
234+
}
235+
236+
if ( $total_product_tax > 0 || $total_shipping_tax > 0 ) {
237+
$tax = new \WC_Order_Item_Tax();
238+
$tax->set_props(
239+
[
240+
'rate_code' => 'SF-VAT',
241+
'rate_id' => self::RATE_ID,
242+
'label' => __( 'VAT', 'shopping-feed' ),
243+
'tax_total' => $total_product_tax,
244+
'shipping_tax_total' => $total_shipping_tax,
245+
]
246+
);
247+
$tax->save();
248+
$wc_order->add_item( $tax );
249+
}
250+
}
251+
208252
$wc_order->set_status( $this->status->get_name(), $this->status->get_note() );
209253
$wc_order->calculate_totals( false );
210254
$wc_order->save();
@@ -323,7 +367,7 @@ private function set_products() {
323367
$this->products = array();
324368
}
325369

326-
$products = new Products( $this->sf_order );
370+
$products = new Products( $this->sf_order, $this->include_vat );
327371
$this->products = $products->get_products();
328372
}
329373

src/Orders/Order/Products.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
defined( 'ABSPATH' ) || exit;
77

88
use ShoppingFeed\ShoppingFeedWC\Dependencies\ShoppingFeed\Sdk\Api\Order\{OrderItem, OrderResource};
9+
use ShoppingFeed\ShoppingFeedWC\Orders\Order;
910
use ShoppingFeed\ShoppingFeedWC\ShoppingFeedHelper;
10-
use WC_Product;
1111

1212
/**
1313
* @psalm-consistent-constructor
@@ -19,6 +19,11 @@ class Products {
1919
*/
2020
private $sf_order;
2121

22+
/**
23+
* @var bool $include_vat
24+
*/
25+
private $include_vat;
26+
2227
/**
2328
* @var array $products
2429
*/
@@ -27,10 +32,12 @@ class Products {
2732
/**
2833
* Products constructor.
2934
*
30-
* @param $sf_order OrderResource
35+
* @param OrderResource $sf_order
36+
* @param bool $include_vat
3137
*/
32-
public function __construct( $sf_order ) {
33-
$this->sf_order = $sf_order;
38+
public function __construct( $sf_order, $include_vat = false ) {
39+
$this->sf_order = $sf_order;
40+
$this->include_vat = $include_vat;
3441
$this->set_products();
3542
}
3643

@@ -94,6 +101,17 @@ private function mapping_product( $sf_product ) {
94101
'quantity' => $sf_product_quantity,
95102
);
96103

104+
if ( $this->include_vat && $sf_product->getTaxAmount() > 0 ) {
105+
$args['taxes'] = [
106+
'subtotal' => [
107+
Order::RATE_ID => $sf_product->getTaxAmount(),
108+
],
109+
'total' => [
110+
Order::RATE_ID => $sf_product->getTaxAmount(),
111+
],
112+
];
113+
}
114+
97115
return array(
98116
'args' => $args,
99117
'is_available' => $wc_product->is_in_stock() && $wc_product->has_enough_stock( $sf_product_quantity ),

src/Orders/Order/Shipping.php

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
defined( 'ABSPATH' ) || exit;
77

88
use ShoppingFeed\ShoppingFeedWC\Dependencies\ShoppingFeed\Sdk\Api\Order\OrderResource;
9+
use ShoppingFeed\ShoppingFeedWC\Orders\Order;
910
use ShoppingFeed\ShoppingFeedWC\ShoppingFeedHelper;
1011

1112
/**
@@ -33,6 +34,12 @@ class Shipping {
3334
*/
3435
private $total;
3536

37+
/** @var float $total_tax */
38+
private $total_tax;
39+
40+
/** @var bool $include_vat */
41+
private $include_vat;
42+
3643
/** @var array|\WC_Shipping_Rate $shipping_rate */
3744
private $shipping_rate;
3845

@@ -42,13 +49,16 @@ class Shipping {
4249
/**
4350
* Shipping constructor.
4451
*
45-
* @param $sf_order OrderResource
52+
* @param OrderResource $sf_order
53+
* @param bool $include_vat
4654
*/
47-
public function __construct( $sf_order ) {
48-
$this->sf_order = $sf_order;
55+
public function __construct( $sf_order, $include_vat = false ) {
56+
$this->sf_order = $sf_order;
57+
$this->include_vat = $include_vat;
4958

5059
$this->set_shipping_method_and_colis_number();
5160
$this->set_total();
61+
$this->set_total_tax();
5262
}
5363

5464
/**
@@ -93,11 +103,20 @@ private function set_shipping_rate() {
93103
$shipping_rate = $default_shipping_method;
94104
}
95105

106+
$taxes = [];
107+
if ( $this->include_vat && $this->get_total_tax() > 0 ) {
108+
$taxes = [
109+
'total' => [
110+
Order::RATE_ID => $this->get_total_tax(),
111+
],
112+
];
113+
}
114+
96115
$rate = new \WC_Shipping_Rate(
97116
$shipping_rate['method_rate_id'],
98117
$shipping_rate['method_title'],
99118
$this->get_total_shipping() ? $this->get_total_shipping() : ShoppingFeedHelper::get_sf_default_shipping_fees(),
100-
array(),
119+
$taxes,
101120
$shipping_rate['method_rate_id'],
102121
$shipping_rate['method_id']
103122
);
@@ -106,19 +125,37 @@ private function set_shipping_rate() {
106125
}
107126

108127
/**
128+
* Get total shipping amount.
129+
*
109130
* @return float
110131
*/
111132
public function get_total() {
112133
return $this->total;
113134
}
114135

115136
/**
116-
* Set total
137+
* Set total shipping amount.
117138
*/
118139
public function set_total() {
119140
$this->total = $this->get_total_shipping();
120141
}
121142

143+
/**
144+
* Get total shipping tax amount.
145+
*
146+
* @return float
147+
*/
148+
public function get_total_tax() {
149+
return $this->total_tax;
150+
}
151+
152+
/**
153+
* Set total shipping tax amount.
154+
*/
155+
public function set_total_tax() {
156+
$this->total_tax = isset( $this->sf_order->toArray()['additionalFields']['shipping_tax'] ) ? (float) $this->sf_order->toArray()['additionalFields']['shipping_tax'] : 0;
157+
}
158+
122159
/**
123160
* @return array|\WC_Shipping_Rate $shipping_rate
124161
*/

src/Orders/Orders.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,11 @@ public function get_orders( $sf_account, $since = '' ) {
115115
continue;
116116
}
117117

118+
// Include VAT when importing the order.
119+
$include_vat = (bool) ( ShoppingFeedHelper::get_sf_orders_options()['import_vat_order'] ?? false );
120+
118121
//Init Order
119-
$order = new Order( $sf_order );
122+
$order = new Order( $sf_order, $include_vat );
120123
//Add Order
121124
$order->add();
122125
}

0 commit comments

Comments
 (0)