Passed
Push — master ( 6e29fd...6a8746 )
by Will
02:54
created

GlobalTax::TableTitle()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 4
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverShop\Model\Modifiers\Tax;
4
5
use SilverShop\Model\Order;
6
7
/**
8
 * Handles calculation of sales tax on Orders on
9
 * a per-country basis.
10
 *
11
 * @property string $Country
12
 */
13
class GlobalTax extends Base
14
{
15
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
16
        'Country' => 'Varchar',
17
    ];
18
19
    private static $table_name = 'SilverShop_GlobalTaxModifier';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
20
21
    /**
22
     * Tax rates per country
23
     *
24
     * @config
25
     * @var    array
26
     */
27
    private static $country_rates = [];
28
29
    public function value($incoming)
30
    {
31
        $rate = $this->Type == 'Chargable'
0 ignored issues
show
introduced by
The condition $this->Type == 'Chargable' is always false.
Loading history...
32
            ?
33
            $this->Rate()
34
            :
35
            round(1 - (1 / (1 + $this->Rate())), Order::config()->rounding_precision);
36
        return $incoming * $rate;
37
    }
38
39
    public function Rate()
40
    {
41
        // If the order is no longer in cart, rely on the saved data
42
        if ($this->OrderID && !$this->Order()->IsCart()) {
43
            return $this->getField('Rate');
44
        }
45
46
        $rates = self::config()->country_rates;
47
        $country = $this->Country();
48
        if ($country && isset($rates[$country])) {
49
            return $this->Rate = $rates[$country]['rate'];
50
        }
51
        $defaults = self::config()->defaults;
52
        return $this->Rate = $defaults['Rate'];
53
    }
54
55
    public function getTableTitle()
56
    {
57
        $country = $this->Country() ? ' (' . $this->Country() . ') ' : '';
58
59
        return parent::getTableTitle() . $country .
60
            ($this->Type == 'Chargable' ? '' : _t(__CLASS__ . '.Included', ' (included in the above price)'));
0 ignored issues
show
introduced by
The condition $this->Type == 'Chargable' is always false.
Loading history...
61
    }
62
63
    public function Country()
64
    {
65
        if ($this->OrderID && $address = $this->Order()->getBillingAddress()) {
66
            return $address->Country;
67
        }
68
69
        return null;
70
    }
71
72
    public function onBeforeWrite()
73
    {
74
        parent::onBeforeWrite();
75
        // While the order is still in "Cart" status, persist country code to DB
76
        if ($this->OrderID && $this->Order()->IsCart()) {
77
            $this->setField('Country', $this->Country());
78
        }
79
    }
80
}
81