Skip to content

Commit

Permalink
fix(tax): calculate delivery fee tax (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
joerivanveen authored Mar 26, 2024
1 parent ce6b67d commit 8edf722
Showing 1 changed file with 16 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Cart\Cart;

class MyParcel_Shipment_Checkout
{
const DELIVERY_TYPE_MORNING = 1;
Expand Down Expand Up @@ -281,7 +283,7 @@ public function getDeliveryDateFromSavedData($myparcel_delivery_options)
* @param string prefix
* @return array prices of delivery types
*/
function getDeliveryPrices($price_format = true, $color_format = true, $prefix = '', $taxIncluded = true, $order_id = 0, $value = 0)
function getDeliveryPrices($price_format = true, $color_format = true, $prefix = '', $addTax = true, $order_id = 0, $value = 0)
{
$registry = MyParcel::$registry;
/** @var \Cart\Cart $cart **/
Expand Down Expand Up @@ -330,7 +332,7 @@ function getDeliveryPrices($price_format = true, $color_format = true, $prefix =
$fee = $this->convertPriceToFloat($fee);

//$fee += $this->getMyParcelShippingCost();
if ($taxIncluded) {
if ($addTax) {
$fee_including_tax = $this->getTotalDeliveryTaxAmountFromCart($fee, $cart);
} else {
$fee_including_tax = $fee;
Expand All @@ -353,7 +355,7 @@ function getDeliveryPrices($price_format = true, $color_format = true, $prefix =
$fee = $this->convertPriceToFloat($fee);

//$fee += $this->getMyParcelShippingCost();
if ($taxIncluded) {
if ($addTax) {
$fee_including_tax = $this->getTotalDeliveryTaxAmountFromCart($fee, $cart);
} else {
$fee_including_tax = $fee;
Expand Down Expand Up @@ -394,7 +396,7 @@ function getDeliveryPrices($price_format = true, $color_format = true, $prefix =
$fee = (float)$checkout_settings['belgium_' . $option . '_fee'];
$fee = $this->convertPriceToFloat($fee);
// $fee_including_tax = $this->getTotalDeliveryTaxAmountFromCart($fee, $cart);
if ($taxIncluded) {
if ($addTax) {
$fee_including_tax = $this->getTotalDeliveryTaxAmountFromCart($fee, $cart);
} else {
$fee_including_tax = $fee;
Expand All @@ -411,7 +413,7 @@ function getDeliveryPrices($price_format = true, $color_format = true, $prefix =
$fee = (float)$checkout_settings['belgium_' . $option . '_fee2'];
$fee = $this->convertPriceToFloat($fee);
// $fee_including_tax = $this->getTotalDeliveryTaxAmountFromCart($fee, $cart);
if ($taxIncluded) {
if ($addTax) {
$fee_including_tax = $this->getTotalDeliveryTaxAmountFromCart($fee, $cart);
} else {
$fee_including_tax = $fee;
Expand Down Expand Up @@ -453,48 +455,25 @@ function convertPriceToFloat( $price )

/**
* Calculate a price base on taxes that affect in cart session
* @param float $delivery_fee
* @param float $deliveryFee
* @param Cart $cart
* @return float price with taxes included
*/
function getTotalDeliveryTaxAmountFromCart($delivery_fee, $cart)
function getTotalDeliveryTaxAmountFromCart($deliveryFee, $cart)
{
/** @var Cart\Tax $tax **/
$taxes = $cart->getTaxes();
$total = $delivery_fee;

if (!$taxes || !$cart) {
return $total;
if (! $deliveryFee) {
return 0;
}

if (empty($taxes)) {
return $delivery_fee;
} else {
$total = 0;

foreach ($taxes as $tax_class_id) {
$rates = $tax->getRates($delivery_fee, $tax_class_id);

/**
* Only apply VAT tax to tax shipping price
**/
foreach ($rates as $key => &$rate) {
if (strpos($rate['name'], 'VAT') === false && strpos($rate['name'], 'BTW') === false) {
unset($rates[$key]);
}
}
$registry = MyParcel::$registry;
$tax = $registry->get('tax');

if (!empty($rates)) {
$total += $tax->calculate($delivery_fee, $tax_class_id);
}
}
if ($cart instanceof Cart && ($taxes = $cart->getTaxes())) {

if ($total == 0) {
$total = $delivery_fee;
}
return $tax->calculate($deliveryFee, $taxes, true);
}

return $total;
return $deliveryFee;
}

function formatDeliveryPrice($price, $currency = null, $color_format = true, $prefix = '')
Expand Down

0 comments on commit 8edf722

Please sign in to comment.