2525 */
2626class 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
0 commit comments