PercentageDiscount::calculateAmount()   B
last analyzed

Complexity

Conditions 7
Paths 8

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
eloc 20
c 4
b 1
f 1
dl 0
loc 36
rs 8.6666
cc 7
nc 8
nop 1
1
<?php
2
3
namespace SilverCommerce\Discounts\Model;
4
5
use SilverStripe\ORM\ArrayList;
6
use SilverCommerce\Discounts\Model\Discount;
7
use SilverCommerce\OrdersAdmin\Model\Estimate;
8
use SilverCommerce\TaxAdmin\Helpers\MathsHelper;
9
10
class PercentageDiscount extends Discount
11
{
12
    private static $table_name = 'Discount_Percentage';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
13
14
    private static $description = "A simple cost-based discount";
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
15
16
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
17
        "Amount"    => "Decimal"
18
    ];
19
20
    public function appliedAmount(AppliedDiscount $item)
21
    {
22
        return $this->calculateAmount($item->Estimate());
0 ignored issues
show
Bug introduced by
The method Estimate() does not exist on SilverCommerce\Discounts\Model\AppliedDiscount. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

22
        return $this->calculateAmount($item->/** @scrutinizer ignore-call */ Estimate());
Loading history...
23
    }
24
25
    public function calculateAmount(Estimate $estimate)
26
    {
27
        $cats = $this->Categories();
0 ignored issues
show
Bug introduced by
The method Categories() does not exist on SilverCommerce\Discounts\Model\PercentageDiscount. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

27
        /** @scrutinizer ignore-call */ 
28
        $cats = $this->Categories();
Loading history...
28
        $all_products = ArrayList::create();
29
        $value = $estimate->getSubTotal();
30
        $min = (float) $this->MinOrder;
0 ignored issues
show
Bug Best Practice introduced by
The property MinOrder does not exist on SilverCommerce\Discounts\Model\PercentageDiscount. Since you implemented __get, consider adding a @property annotation.
Loading history...
31
32
        if ($cats->count() > 0) {
33
            $value = 0;
34
            foreach ($cats as $cat) {
35
                $all_products->merge($cat->Products());
36
            }
37
38
            foreach ($estimate->Items() as $line_item) {
39
                $match = $line_item->FindStockItem();
40
                if ($all_products->find('ID', $match->ID)) {
41
                    $value += ($line_item->Quantity * $line_item->UnitPrice);
42
                }
43
            }
44
        }
45
46
        $converted_value = (int) ($value * 100);
47
48
        $converted_amount = $converted_value * ($this->Amount / 100);
0 ignored issues
show
Bug Best Practice introduced by
The property Amount does not exist on SilverCommerce\Discounts\Model\PercentageDiscount. Since you implemented __get, consider adding a @property annotation.
Loading history...
49
50
        $amount = MathsHelper::round($converted_amount, 0)/100;
51
52
        if ($amount > $value) {
53
            $amount = $value;
54
        }
55
56
        if ($value < $min) {
57
            $amount = 0;
58
        }
59
60
        return $amount;
61
    }
62
}
63