EstimateExtension::onAfterWrite()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 4
nc 2
nop 0
1
<?php
2
3
namespace SilverCommerce\Discounts\Extensions;
4
5
use SilverStripe\Dev\Debug;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\TextField;
8
use SilverStripe\ORM\DataExtension;
9
use SilverStripe\Forms\NumericField;
10
use SilverStripe\Forms\DropdownField;
11
use SilverCommerce\Discounts\Model\Discount;
12
use SilverCommerce\Discounts\DiscountFactory;
13
use SilverCommerce\TaxAdmin\Helpers\MathsHelper;
14
use SilverCommerce\Discounts\Model\AppliedDiscount;
15
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
16
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
17
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
18
19
/**
20
 * Add extra fields to an estimate (to track the discount)
21
 */
22
class EstimateExtension extends DataExtension
23
{
24
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
25
        'Discounts' => AppliedDiscount::class
26
    ];
27
28
    private static $casting = [
0 ignored issues
show
introduced by
The private property $casting is not used, and could be removed.
Loading history...
29
        'DiscountDetails'   => 'Varchar',
30
        'DiscountTotal'   => 'Currency'
31
    ];
32
33
    /**
34
     * Find the specific discount object for this order
35
     *
36
     * @return Discount
37
     */
38
    public function findDiscount($code)
39
    {
40
        $discount = $this->owner->Discounts()->find('Code', $code);
41
42
        return $discount;
43
    }
44
45
    /**
46
     * Does the current estimate have a discount?
47
     */
48
    public function hasDiscount()
49
    {
50
        return (ceil($this->owner->getDiscountTotal())) ? true : false;
51
    }
52
53
    public function recalculateDiscounts()
54
    {
55
        if ($this->owner->Discounts()->Count() > 0) {
56
            foreach ($this->owner->Discounts() as $discount) {
57
                $discount->Value = $discount->updateDiscount();
58
            }
59
        }
60
    }
61
62
    /**
63
     * get the total of all discounts applied to this estimate.
64
     *
65
     * @return void
66
     */
67
    public function getDiscountTotal()
68
    {
69
        $total = 0;
70
71
        if ($this->owner->Discounts()->Count() > 0) {
72
            foreach ($this->owner->Discounts() as $discount) {
73
                $total += $discount->Value;
74
            }
75
        }
76
77
        $this->owner->extend("updateDiscountTotal", $total);
78
79
        return $total;
80
    }
81
82
    /**
83
     * update the total price with the discount reduction.
84
     *
85
     * @param  [type] $total
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
86
     * @return void
87
     */
88
    public function updateTotal(&$total)
89
    {
90
        $total -= $this->owner->getDiscountTotal();
91
    }
92
93
    /**
94
     * Add discount info to an estimate
95
     *
96
     * @param FieldList $fields Current field list
97
     */
98
    public function updateCMSFields(FieldList $fields)
99
    {
100
        $main = $fields->findOrMakeTab("Root.Main");
101
        $statuses = $this->owner->config()->get("statuses");
0 ignored issues
show
Unused Code introduced by
The assignment to $statuses is dead and can be removed.
Loading history...
102
        $details = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $details is dead and can be removed.
Loading history...
103
        $totals = null;
104
        $misc = null;
105
        $discounts = $fields->dataFieldByName('Discounts');
106
107
        // Switch unlink action to delete
108
        if ($discounts) {
0 ignored issues
show
introduced by
$discounts is of type SilverStripe\Forms\FormField, thus it always evaluated to true.
Loading history...
109
            $discounts
110
                ->getConfig()
111
                ->removeComponentsByType(GridFieldDeleteAction::class)
112
                ->removeComponentsByType(GridFieldAddExistingAutocompleter::class)
113
                ->addComponent(new GridFieldDeleteAction());
114
        }
115
116
        $discount_code = $fields->dataFieldByName('DiscountCode');
117
        $discount_amount = $fields->dataFieldByName('DiscountTotal');
118
119
        // Manually loop through fields to find info composite field, as
120
        // fieldByName cannot reliably find this.
121
        foreach ($main->getChildren() as $field) {
122
            if ($field->getName() == "OrdersDetails") {
123
                foreach ($field->getChildren() as $field) {
0 ignored issues
show
Comprehensibility Bug introduced by
$field is overwriting a variable from outer foreach loop.
Loading history...
124
                    if ($field->getName() == "OrdersDetailsTotals") {
125
                        $totals = $field;
126
                    }
127
                    if ($field->getName() == "OrdersDetailsMisc") {
128
                        $misc = $field;
129
                    }
130
                }
131
            }
132
        }
133
134
        if ($totals && $discount_amount) {
135
            $totals->insertBefore(
136
                "TotalValue",
137
                $discount_amount
138
            );
139
        }
140
141
        if ($misc && $discount_code) {
142
            $misc->push(
143
                $discount_code
144
            );
145
        }
146
    }
147
148
    /**
149
     * if necessary, recalculate the discount amount when estimate is saved.
150
     *
151
     * @return void
152
     */
153
    public function onAfterWrite()
154
    {
155
        if ($this->owner->Discounts()->exists() && (!method_exists($this->owner, 'isPaid') || !$this->owner->isPaid())) {
156
            $this->recalculateDiscounts();
157
        }
158
    }
159
}
160