Completed
Push — master ( a3070a...453ef0 )
by Will
15s queued 12s
created

OrderStatusLog::requireDefaultRecords()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 26
rs 9.8666
1
<?php
2
3
namespace SilverShop\Model;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\ORM\DB;
7
use SilverStripe\ORM\FieldType\DBDate;
8
use SilverStripe\Security\Member;
9
use SilverStripe\Security\Security;
10
11
/**
12
 * Data class that keeps a log of a single
13
 * status of an order.
14
 *
15
 * @property string $Title
16
 * @property string $Note
17
 * @property string $DispatchedBy
18
 * @property DBDate $DispatchedOn
19
 * @property string $DispatchTicket
20
 * @property string $PaymentCode
21
 * @property bool $PaymentOK
22
 * @property bool $SentToCustomer Whether or not this entry has been sent to the customer (e.g. via OrderEmailNotifier)
23
 * @property bool $VisibleToCustomer Whether or not this entry should be visible to the customer (e.g. on order details)
24
 * @property int $AuthorID
25
 * @property int $OrderID
26
 *
27
 * @method Member Author()
28
 * @method Order Order()
29
 */
30
class OrderStatusLog extends DataObject
31
{
32
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
33
        'Title' => 'Varchar(100)',
34
        'Note' => 'Text',
35
        'DispatchedBy' => 'Varchar(100)',
36
        'DispatchedOn' => 'Date',
37
        'DispatchTicket' => 'Varchar(100)',
38
        'PaymentCode' => 'Varchar(100)',
39
        'PaymentOK' => 'Boolean',
40
        'SentToCustomer' => 'Boolean',
41
        'VisibleToCustomer' => 'Boolean',
42
    ];
43
44
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
45
        'Author' => Member::class,
46
        'Order' => Order::class,
47
    ];
48
49
    private static $searchable_fields = [
0 ignored issues
show
introduced by
The private property $searchable_fields is not used, and could be removed.
Loading history...
50
        'Order.Reference' => [
51
            'filter' => 'PartialMatchFilter',
52
            'title' => 'Order No'
53
        ],
54
        'Order.FirstName' => [
55
            'filter' => 'PartialMatchFilter',
56
            'title' => 'First Name'
57
        ],
58
        'Order.Email' => [
59
            'filter' => 'PartialMatchFilter',
60
            'title' => 'Email'
61
        ]
62
    ];
63
64
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
65
        'Order.Reference' => 'Order No',
66
        'Created' => 'Created',
67
        'Order.Name' => 'Name',
68
        'Order.LatestEmail' => 'Email',
69
        'Title' => 'Title',
70
        'SentToCustomer' => 'Emailed',
71
        'VisibleToCustomer' => 'Visible to customer?'
72
    ];
73
74
    private static $singular_name = 'Order Log Entry';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
75
76
    private static $plural_name = 'Order Status Log Entries';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
77
78
    private static $default_sort = '"Created" DESC';
0 ignored issues
show
introduced by
The private property $default_sort is not used, and could be removed.
Loading history...
79
80
    private static $table_name = 'SilverShop_OrderStatusLog';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
81
82
    public function canCreate($member = null, $context = [])
83
    {
84
        return false;
85
    }
86
87
    public function canDelete($member = null)
88
    {
89
        return false;
90
    }
91
92
    public function canEdit($member = null)
93
    {
94
        return false;
95
    }
96
97
    public function populateDefaults()
98
    {
99
        parent::populateDefaults();
100
        $this->updateWithLastInfo();
101
    }
102
103
    public function requireDefaultRecords()
104
    {
105
        parent::requireDefaultRecords();
106
107
        // If there are existing records with SentToCustomer=true and there are no records with VisibleToCustomer=true,
108
        // then we assume this is an upgrade (if a record was sent to the customer, then by definition it's visible to
109
        // the customer. However, we use the count check to ensure we only make the database change up until at least
110
        // one record has VisibleToCustomer = true (to avoid resetting it in future)
111
        if (OrderStatusLog::get()->filter('VisibleToCustomer', true)->count() == 0) {
112
            // We don't have any records with VisibleToCustomer true, so update all records with SentToCustomer = true
113
            $toUpdate = OrderStatusLog::get()->filter('SentToCustomer', true);
114
            $updated = 0;
115
116
            /** @var OrderStatusLog $log */
117
            foreach ($toUpdate as $log) {
118
                $log->VisibleToCustomer = true;
119
                $log->write();
120
                $updated++;
121
            }
122
123
            $message = sprintf(
124
                'Migrated %d records to new format (set VisibleToCustomer=true where SentToCustomer=true)',
125
                $updated
126
            );
127
128
            DB::alteration_message($message, 'changed');
129
        }
130
    }
131
132
    public function onBeforeWrite()
133
    {
134
        parent::onBeforeWrite();
135
        if (!$this->AuthorID && ($member = Security::getCurrentUser())) {
136
            $this->AuthorID = $member->ID;
137
        }
138
        if (!$this->Title) {
139
            $this->Title = 'Order Update';
140
        }
141
    }
142
143
    public function validate()
144
    {
145
        $validationResult = parent::validate();
146
        if (!$this->OrderID) {
147
            $validationResult->addError('there is no order id for Order Status Log');
148
        }
149
        return $validationResult;
150
    }
151
152
    protected function updateWithLastInfo()
153
    {
154
        if ($this->OrderID) {
155
            /** @var OrderStatusLog $latestLog */
156
            $latestLog = OrderStatusLog::get()
157
                ->filter('OrderID', $this->OrderID)
158
                ->sort('Created', 'DESC')
159
                ->first();
160
161
            if ($latestLog) {
0 ignored issues
show
introduced by
$latestLog is of type SilverShop\Model\OrderStatusLog, thus it always evaluated to true.
Loading history...
162
                $this->DispatchedBy = $latestLog->DispatchedBy;
163
                $this->DispatchedOn = $latestLog->DispatchedOn;
164
                $this->DispatchTicket = $latestLog->DispatchTicket;
165
                $this->PaymentCode = $latestLog->PaymentCode;
166
                $this->PaymentOK = $latestLog->PaymentOK;
167
                $this->SentToCustomer = $latestLog->SentToCustomer;
168
                $this->VisibleToCustomer = $latestLog->VisibleToCustomer;
169
            }
170
        }
171
    }
172
}
173