From af498b3d870d5fc0b13360d2b23101a21ffccdd9 Mon Sep 17 00:00:00 2001 From: Josh Angell Date: Thu, 11 Mar 2021 13:08:04 +0000 Subject: [PATCH 1/2] Added logic to send the correct tax element on order level discounts. --- src/controllers/SettingsController.php | 14 +++++++++++++- src/models/Settings.php | 10 ++++++++++ src/services/Orders.php | 11 ++++++++--- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/controllers/SettingsController.php b/src/controllers/SettingsController.php index bd1bfab..53587fa 100644 --- a/src/controllers/SettingsController.php +++ b/src/controllers/SettingsController.php @@ -257,6 +257,18 @@ public function actionSave(): Response $settings->vend_discountProductId = $request->getBodyParam('vend_discountProductId') ?? $settings->vend_discountProductId; $settings->vend_noTaxId = $request->getBodyParam('vend_noTaxId') ?? $settings->vend_noTaxId; + // If we have a discount product (which we should) then we need to fetch the tax info + if ($settings->vend_discountProductId) { + + // Get the discount product from the API again + $discountProductResponse = Vend::$plugin->api->getResponse('products/'.$settings->vend_discountProductId); + if ($discountProductResponse && isset($discountProductResponse['products'])) { + $discountProduct = $discountProductResponse['products'][0]; + $settings->vend_discountTaxId = $discountProduct['tax_id']; + $settings->vend_discountTaxRate = $discountProduct['tax_rate']; + } + } + if (!$settings->validate()) { Craft::$app->getSession()->setError(Craft::t('vend', 'Couldn’t save settings.')); return $this->renderTemplate('vend/settings/general', compact('settings')); @@ -583,4 +595,4 @@ public function actionFeedMe(): Response return $this->renderTemplate('vend/settings/feedme', $variables); } -} \ No newline at end of file +} diff --git a/src/models/Settings.php b/src/models/Settings.php index fb6dbcc..aef2def 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -67,6 +67,16 @@ class Settings extends Model */ public $vend_discountProductId; + /** + * @var string + */ + public $vend_discountTaxId; + + /** + * @var string + */ + public $vend_discountTaxRate; + /** * @var string */ diff --git a/src/services/Orders.php b/src/services/Orders.php index 67c51c0..cecd17a 100644 --- a/src/services/Orders.php +++ b/src/services/Orders.php @@ -317,13 +317,18 @@ public function registerSale(int $orderId) { $totalDiscount = abs($order->getTotalDiscount()); $orderDiscount = $totalDiscount - $totalLineItemsDiscount; if ($orderDiscount > 0) { + + // Process the special discount product tax rate + $orderDiscountTaxAmount = bcmul($orderDiscount, $settings->vend_discountTaxRate, 5); + $orderDiscountWithoutTax = bcsub($orderDiscount, $orderDiscountTaxAmount, 5); + $data['register_sale_products'][] = [ 'product_id' => $settings->vend_discountProductId, 'quantity' => -1, - 'price' => $orderDiscount, + 'price' => $orderDiscountWithoutTax, 'price_set' => 1, - 'tax' => 0, - 'tax_id' => $settings->vend_noTaxId + 'tax' => $orderDiscountTaxAmount, + 'tax_id' => $settings->vend_discountTaxId ]; } From 8437e407ec35ea1ef2c83779871166dc755dfbf7 Mon Sep 17 00:00:00 2001 From: Josh Angell Date: Thu, 11 Mar 2021 13:08:59 +0000 Subject: [PATCH 2/2] Changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8df4da7..fb86d54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p ## Unreleased +## 2.5.6 - 2021-03-11 + +### Fixed +- Fixed an issue where order level discounts weren’t showing the tax inside them + ## 2.5.5 - 2021-01-05 ### Fixed