Skip to content

Commit

Permalink
Check country restrictions on shipping taxes (fixes #10)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-kuendig committed Oct 11, 2018
1 parent 6de7a65 commit 4b98c53
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 36 deletions.
4 changes: 2 additions & 2 deletions classes/totals/ShippingTotal.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ protected function calculateTaxes(): float

$price = $this->total;

$totalTaxPercentage = $this->method->taxes->sum('percentageDecimal');
$totalTaxPercentage = $this->totals->shippingTaxes->sum('percentageDecimal');

return $this->method->taxes->reduce(function ($total, Tax $tax) use ($price, $totalTaxPercentage) {
return $this->totals->shippingTaxes->reduce(function ($total, Tax $tax) use ($price, $totalTaxPercentage) {
return $total += $price / (1 + $totalTaxPercentage) * $tax->percentageDecimal;
}, 0);
}
Expand Down
23 changes: 17 additions & 6 deletions classes/totals/TotalsCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,18 @@ class TotalsCalculator
* @var Collection
*/
protected $appliedDiscounts;
/**
* @var Collection
*/
public $shippingTaxes;

public function __construct(Cart $cart)
{
$this->cart = $cart->load(
'products',
'products.data.taxes',
'shipping_method',
'shipping_method.taxes',
'shipping_method.taxes.countries',
'shipping_method.rates',
'discounts'
);
Expand All @@ -87,6 +91,14 @@ protected function calculate()
$this->productTaxes = $this->calculateProductTaxes();
$this->productPostTaxes = $this->productPreTaxes + $this->productTaxes;

$this->shippingTaxes = optional($this->cart->shipping_method->taxes)->filter(function (Tax $tax) {
// If no shipping address is available only include taxes that have no country restrictions.
if ($this->cart->shipping_address === null) {
return $tax->countries->count() === 0;
}
return $tax->countries->pluck('id')->search($this->cart->shipping_address->country_id) !== false;
});

$this->shippingTotal = new ShippingTotal($this->cart->shipping_method, $this);
$this->totalPreTaxes = $this->productPreTaxes + $this->shippingTotal->totalPreTaxes();

Expand All @@ -113,11 +125,10 @@ protected function getTaxTotals(): Collection
{
$shippingTaxes = new Collection();
$shippingTotal = $this->shippingTotal->totalPreTaxesOriginal();
if ($this->cart->shipping_method) {
$shippingTaxes = optional($this->cart->shipping_method)->taxes
->map(function (Tax $tax) use ($shippingTotal) {
return new TaxTotal($shippingTotal, $tax);
});
if ($this->shippingTaxes) {
$shippingTaxes = $this->shippingTaxes->map(function (Tax $tax) use ($shippingTotal) {
return new TaxTotal($shippingTotal, $tax);
});
}

$productTaxes = $this->cart->products->flatMap(function (CartProduct $product) {
Expand Down
9 changes: 8 additions & 1 deletion components/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Cart extends MallComponent
public $defaultMinQuantity = 1;
public $defaultMaxQuantity = 100;
public $showDiscountApplier = true;
public $showTaxes = true;
public $productPage;

public function componentDetails()
Expand All @@ -31,7 +32,12 @@ public function defineProperties()
return [
'showDiscountApplier' => [
'type' => 'checkbox',
'name' => 'offline.mall::lang.components.cart.properties.showDiscountApplier.name',
'title' => 'offline.mall::lang.components.cart.properties.showDiscountApplier.title',
'default' => 1,
],
'showTaxes' => [
'type' => 'checkbox',
'title' => 'offline.mall::lang.components.cart.properties.showTaxes.name',
'default' => 1,
],
];
Expand Down Expand Up @@ -103,5 +109,6 @@ protected function setData()
$this->setVar('cart', $cart);
$this->setVar('productPage', GeneralSettings::get('product_page'));
$this->setVar('showDiscountApplier', $this->property('showDiscountApplier'));
$this->setVar('showTaxes', $this->property('showTaxes'));
}
}
52 changes: 27 additions & 25 deletions components/cart/table/taxes.htm
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
{% for entry in taxes %}
<tr class="mall-cart__taxes mall-cart__taxes--{{ loop.index }}">
<td colspan="2"></td>
<td class="text-right">
{{ entry.tax.name }}
</td>
<td class="text-right">
{{ entry.tax.percentage }} %
</td>
<td class="text-right">
{{ entry.total | money }}
</td>
</tr>
{% endfor %}
{% if __SELF__.showTaxes %}
{% for entry in taxes %}
<tr class="mall-cart__taxes mall-cart__taxes--{{ loop.index }}">
<td colspan="2"></td>
<td class="text-right">
{{ entry.tax.name }}
</td>
<td class="text-right">
{{ entry.tax.percentage }} %
</td>
<td class="text-right">
{{ entry.total | money }}
</td>
</tr>
{% endfor %}

{% if taxes.count > 1 %}
<tr class="mall-cart__taxes-total">
<td colspan="2"></td>
<td class="text-right">
{{ 'mall.total.taxes' | _ }}
</td>
<td></td>
<td class="text-right">
{{ cart.totals.totalTaxes | money }}
</td>
</tr>
{% if taxes.count > 1 %}
<tr class="mall-cart__taxes-total">
<td colspan="2"></td>
<td class="text-right">
{{ 'mall.total.taxes' | _ }}
</td>
<td></td>
<td class="text-right">
{{ cart.totals.totalTaxes | money }}
</td>
</tr>
{% endif %}
{% endif %}
10 changes: 9 additions & 1 deletion lang/de/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,10 +588,18 @@
],
],
'cart' => [
'details' => [
'details' => [
'name' => 'Warenkorb',
'description' => 'Zeigt den Warenkorb an',
],
'properties' => [
'showDiscountApplier' => [
'title' => 'Rabatt-Code-Feld anzeigen',
],
'showTaxes' => [
'title' => 'Steuern ausweisen',
],
],
],
'checkout' => [
'details' => [
Expand Down
10 changes: 9 additions & 1 deletion lang/en/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,10 +590,18 @@
'added_to_cart' => 'Added product successfully',
],
'cart' => [
'details' => [
'details' => [
'name' => 'Cart',
'description' => 'Displays the shopping cart',
],
'properties' => [
'showDiscountApplier' => [
'title' => 'Show discount applier',
],
'showTaxes' => [
'title' => 'Show taxes',
],
],
],
'checkout' => [
'details' => [
Expand Down

0 comments on commit 4b98c53

Please sign in to comment.