LineItemExtension::updateUnitTax()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 4
c 1
b 1
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace SilverCommerce\Discounts\Extensions;
4
5
use SilverStripe\ORM\DataExtension;
6
use SilverCommerce\Discounts\Model\AppliedDiscount;
7
8
class LineItemExtension extends DataExtension
9
{
10
    private static $belongs_many_many = [
0 ignored issues
show
introduced by
The private property $belongs_many_many is not used, and could be removed.
Loading history...
11
        'Discounts' => AppliedDiscount::class
12
    ];
13
14
    /**
15
     * Get the amount of tax for a single unit of this item
16
     *
17
     * @return float
18
     */
19
    public function updateUnitTax(&$total)
20
    {
21
        $percent = $this->getOwner()->TaxPercentage;
22
        $price = $this->getOwner()->UnitPrice;
23
        $total = (($price - $this->getDiscountTotal()) / 100) * $percent;
24
25
        return $total;
26
    }
27
28
    public function getDiscountTotal()
29
    {
30
        $parent = $this->getOwner()->Parent();
31
        $total = $parent->getDiscountTotal();
32
        $count = $parent->getTotalItems();
33
        if ($count > 0) {
34
            return $total/$count;
35
        }
36
        return 0;
37
    }
38
}
39